builtin-sched.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/util.h"
  4. #include "util/evlist.h"
  5. #include "util/cache.h"
  6. #include "util/evsel.h"
  7. #include "util/symbol.h"
  8. #include "util/thread.h"
  9. #include "util/header.h"
  10. #include "util/session.h"
  11. #include "util/tool.h"
  12. #include "util/parse-options.h"
  13. #include "util/trace-event.h"
  14. #include "util/debug.h"
  15. #include <sys/prctl.h>
  16. #include <sys/resource.h>
  17. #include <semaphore.h>
  18. #include <pthread.h>
  19. #include <math.h>
  20. #define PR_SET_NAME 15 /* Set process name */
  21. #define MAX_CPUS 4096
  22. #define COMM_LEN 20
  23. #define SYM_LEN 129
  24. #define MAX_PID 65536
  25. struct sched_atom;
  26. struct task_desc {
  27. unsigned long nr;
  28. unsigned long pid;
  29. char comm[COMM_LEN];
  30. unsigned long nr_events;
  31. unsigned long curr_event;
  32. struct sched_atom **atoms;
  33. pthread_t thread;
  34. sem_t sleep_sem;
  35. sem_t ready_for_work;
  36. sem_t work_done_sem;
  37. u64 cpu_usage;
  38. };
  39. enum sched_event_type {
  40. SCHED_EVENT_RUN,
  41. SCHED_EVENT_SLEEP,
  42. SCHED_EVENT_WAKEUP,
  43. SCHED_EVENT_MIGRATION,
  44. };
  45. struct sched_atom {
  46. enum sched_event_type type;
  47. int specific_wait;
  48. u64 timestamp;
  49. u64 duration;
  50. unsigned long nr;
  51. sem_t *wait_sem;
  52. struct task_desc *wakee;
  53. };
  54. #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
  55. enum thread_state {
  56. THREAD_SLEEPING = 0,
  57. THREAD_WAIT_CPU,
  58. THREAD_SCHED_IN,
  59. THREAD_IGNORE
  60. };
  61. struct work_atom {
  62. struct list_head list;
  63. enum thread_state state;
  64. u64 sched_out_time;
  65. u64 wake_up_time;
  66. u64 sched_in_time;
  67. u64 runtime;
  68. };
  69. struct work_atoms {
  70. struct list_head work_list;
  71. struct thread *thread;
  72. struct rb_node node;
  73. u64 max_lat;
  74. u64 max_lat_at;
  75. u64 total_lat;
  76. u64 nb_atoms;
  77. u64 total_runtime;
  78. };
  79. typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
  80. struct perf_sched;
  81. struct trace_sched_handler {
  82. int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  83. struct perf_sample *sample, struct machine *machine);
  84. int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  85. struct perf_sample *sample, struct machine *machine);
  86. int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  87. struct perf_sample *sample, struct machine *machine);
  88. int (*fork_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  89. struct perf_sample *sample);
  90. int (*migrate_task_event)(struct perf_sched *sched,
  91. struct perf_evsel *evsel,
  92. struct perf_sample *sample,
  93. struct machine *machine);
  94. };
  95. struct perf_sched {
  96. struct perf_tool tool;
  97. const char *sort_order;
  98. unsigned long nr_tasks;
  99. struct task_desc *pid_to_task[MAX_PID];
  100. struct task_desc **tasks;
  101. const struct trace_sched_handler *tp_handler;
  102. pthread_mutex_t start_work_mutex;
  103. pthread_mutex_t work_done_wait_mutex;
  104. int profile_cpu;
  105. /*
  106. * Track the current task - that way we can know whether there's any
  107. * weird events, such as a task being switched away that is not current.
  108. */
  109. int max_cpu;
  110. u32 curr_pid[MAX_CPUS];
  111. struct thread *curr_thread[MAX_CPUS];
  112. char next_shortname1;
  113. char next_shortname2;
  114. unsigned int replay_repeat;
  115. unsigned long nr_run_events;
  116. unsigned long nr_sleep_events;
  117. unsigned long nr_wakeup_events;
  118. unsigned long nr_sleep_corrections;
  119. unsigned long nr_run_events_optimized;
  120. unsigned long targetless_wakeups;
  121. unsigned long multitarget_wakeups;
  122. unsigned long nr_runs;
  123. unsigned long nr_timestamps;
  124. unsigned long nr_unordered_timestamps;
  125. unsigned long nr_state_machine_bugs;
  126. unsigned long nr_context_switch_bugs;
  127. unsigned long nr_events;
  128. unsigned long nr_lost_chunks;
  129. unsigned long nr_lost_events;
  130. u64 run_measurement_overhead;
  131. u64 sleep_measurement_overhead;
  132. u64 start_time;
  133. u64 cpu_usage;
  134. u64 runavg_cpu_usage;
  135. u64 parent_cpu_usage;
  136. u64 runavg_parent_cpu_usage;
  137. u64 sum_runtime;
  138. u64 sum_fluct;
  139. u64 run_avg;
  140. u64 all_runtime;
  141. u64 all_count;
  142. u64 cpu_last_switched[MAX_CPUS];
  143. struct rb_root atom_root, sorted_atom_root;
  144. struct list_head sort_list, cmp_pid;
  145. };
  146. static u64 get_nsecs(void)
  147. {
  148. struct timespec ts;
  149. clock_gettime(CLOCK_MONOTONIC, &ts);
  150. return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  151. }
  152. static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
  153. {
  154. u64 T0 = get_nsecs(), T1;
  155. do {
  156. T1 = get_nsecs();
  157. } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
  158. }
  159. static void sleep_nsecs(u64 nsecs)
  160. {
  161. struct timespec ts;
  162. ts.tv_nsec = nsecs % 999999999;
  163. ts.tv_sec = nsecs / 999999999;
  164. nanosleep(&ts, NULL);
  165. }
  166. static void calibrate_run_measurement_overhead(struct perf_sched *sched)
  167. {
  168. u64 T0, T1, delta, min_delta = 1000000000ULL;
  169. int i;
  170. for (i = 0; i < 10; i++) {
  171. T0 = get_nsecs();
  172. burn_nsecs(sched, 0);
  173. T1 = get_nsecs();
  174. delta = T1-T0;
  175. min_delta = min(min_delta, delta);
  176. }
  177. sched->run_measurement_overhead = min_delta;
  178. printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
  179. }
  180. static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
  181. {
  182. u64 T0, T1, delta, min_delta = 1000000000ULL;
  183. int i;
  184. for (i = 0; i < 10; i++) {
  185. T0 = get_nsecs();
  186. sleep_nsecs(10000);
  187. T1 = get_nsecs();
  188. delta = T1-T0;
  189. min_delta = min(min_delta, delta);
  190. }
  191. min_delta -= 10000;
  192. sched->sleep_measurement_overhead = min_delta;
  193. printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
  194. }
  195. static struct sched_atom *
  196. get_new_event(struct task_desc *task, u64 timestamp)
  197. {
  198. struct sched_atom *event = zalloc(sizeof(*event));
  199. unsigned long idx = task->nr_events;
  200. size_t size;
  201. event->timestamp = timestamp;
  202. event->nr = idx;
  203. task->nr_events++;
  204. size = sizeof(struct sched_atom *) * task->nr_events;
  205. task->atoms = realloc(task->atoms, size);
  206. BUG_ON(!task->atoms);
  207. task->atoms[idx] = event;
  208. return event;
  209. }
  210. static struct sched_atom *last_event(struct task_desc *task)
  211. {
  212. if (!task->nr_events)
  213. return NULL;
  214. return task->atoms[task->nr_events - 1];
  215. }
  216. static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
  217. u64 timestamp, u64 duration)
  218. {
  219. struct sched_atom *event, *curr_event = last_event(task);
  220. /*
  221. * optimize an existing RUN event by merging this one
  222. * to it:
  223. */
  224. if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
  225. sched->nr_run_events_optimized++;
  226. curr_event->duration += duration;
  227. return;
  228. }
  229. event = get_new_event(task, timestamp);
  230. event->type = SCHED_EVENT_RUN;
  231. event->duration = duration;
  232. sched->nr_run_events++;
  233. }
  234. static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
  235. u64 timestamp, struct task_desc *wakee)
  236. {
  237. struct sched_atom *event, *wakee_event;
  238. event = get_new_event(task, timestamp);
  239. event->type = SCHED_EVENT_WAKEUP;
  240. event->wakee = wakee;
  241. wakee_event = last_event(wakee);
  242. if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
  243. sched->targetless_wakeups++;
  244. return;
  245. }
  246. if (wakee_event->wait_sem) {
  247. sched->multitarget_wakeups++;
  248. return;
  249. }
  250. wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
  251. sem_init(wakee_event->wait_sem, 0, 0);
  252. wakee_event->specific_wait = 1;
  253. event->wait_sem = wakee_event->wait_sem;
  254. sched->nr_wakeup_events++;
  255. }
  256. static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
  257. u64 timestamp, u64 task_state __maybe_unused)
  258. {
  259. struct sched_atom *event = get_new_event(task, timestamp);
  260. event->type = SCHED_EVENT_SLEEP;
  261. sched->nr_sleep_events++;
  262. }
  263. static struct task_desc *register_pid(struct perf_sched *sched,
  264. unsigned long pid, const char *comm)
  265. {
  266. struct task_desc *task;
  267. BUG_ON(pid >= MAX_PID);
  268. task = sched->pid_to_task[pid];
  269. if (task)
  270. return task;
  271. task = zalloc(sizeof(*task));
  272. task->pid = pid;
  273. task->nr = sched->nr_tasks;
  274. strcpy(task->comm, comm);
  275. /*
  276. * every task starts in sleeping state - this gets ignored
  277. * if there's no wakeup pointing to this sleep state:
  278. */
  279. add_sched_event_sleep(sched, task, 0, 0);
  280. sched->pid_to_task[pid] = task;
  281. sched->nr_tasks++;
  282. sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_task *));
  283. BUG_ON(!sched->tasks);
  284. sched->tasks[task->nr] = task;
  285. if (verbose)
  286. printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
  287. return task;
  288. }
  289. static void print_task_traces(struct perf_sched *sched)
  290. {
  291. struct task_desc *task;
  292. unsigned long i;
  293. for (i = 0; i < sched->nr_tasks; i++) {
  294. task = sched->tasks[i];
  295. printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
  296. task->nr, task->comm, task->pid, task->nr_events);
  297. }
  298. }
  299. static void add_cross_task_wakeups(struct perf_sched *sched)
  300. {
  301. struct task_desc *task1, *task2;
  302. unsigned long i, j;
  303. for (i = 0; i < sched->nr_tasks; i++) {
  304. task1 = sched->tasks[i];
  305. j = i + 1;
  306. if (j == sched->nr_tasks)
  307. j = 0;
  308. task2 = sched->tasks[j];
  309. add_sched_event_wakeup(sched, task1, 0, task2);
  310. }
  311. }
  312. static void perf_sched__process_event(struct perf_sched *sched,
  313. struct sched_atom *atom)
  314. {
  315. int ret = 0;
  316. switch (atom->type) {
  317. case SCHED_EVENT_RUN:
  318. burn_nsecs(sched, atom->duration);
  319. break;
  320. case SCHED_EVENT_SLEEP:
  321. if (atom->wait_sem)
  322. ret = sem_wait(atom->wait_sem);
  323. BUG_ON(ret);
  324. break;
  325. case SCHED_EVENT_WAKEUP:
  326. if (atom->wait_sem)
  327. ret = sem_post(atom->wait_sem);
  328. BUG_ON(ret);
  329. break;
  330. case SCHED_EVENT_MIGRATION:
  331. break;
  332. default:
  333. BUG_ON(1);
  334. }
  335. }
  336. static u64 get_cpu_usage_nsec_parent(void)
  337. {
  338. struct rusage ru;
  339. u64 sum;
  340. int err;
  341. err = getrusage(RUSAGE_SELF, &ru);
  342. BUG_ON(err);
  343. sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
  344. sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
  345. return sum;
  346. }
  347. static int self_open_counters(void)
  348. {
  349. struct perf_event_attr attr;
  350. int fd;
  351. memset(&attr, 0, sizeof(attr));
  352. attr.type = PERF_TYPE_SOFTWARE;
  353. attr.config = PERF_COUNT_SW_TASK_CLOCK;
  354. fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
  355. if (fd < 0)
  356. pr_err("Error: sys_perf_event_open() syscall returned "
  357. "with %d (%s)\n", fd, strerror(errno));
  358. return fd;
  359. }
  360. static u64 get_cpu_usage_nsec_self(int fd)
  361. {
  362. u64 runtime;
  363. int ret;
  364. ret = read(fd, &runtime, sizeof(runtime));
  365. BUG_ON(ret != sizeof(runtime));
  366. return runtime;
  367. }
  368. struct sched_thread_parms {
  369. struct task_desc *task;
  370. struct perf_sched *sched;
  371. };
  372. static void *thread_func(void *ctx)
  373. {
  374. struct sched_thread_parms *parms = ctx;
  375. struct task_desc *this_task = parms->task;
  376. struct perf_sched *sched = parms->sched;
  377. u64 cpu_usage_0, cpu_usage_1;
  378. unsigned long i, ret;
  379. char comm2[22];
  380. int fd;
  381. free(parms);
  382. sprintf(comm2, ":%s", this_task->comm);
  383. prctl(PR_SET_NAME, comm2);
  384. fd = self_open_counters();
  385. if (fd < 0)
  386. return NULL;
  387. again:
  388. ret = sem_post(&this_task->ready_for_work);
  389. BUG_ON(ret);
  390. ret = pthread_mutex_lock(&sched->start_work_mutex);
  391. BUG_ON(ret);
  392. ret = pthread_mutex_unlock(&sched->start_work_mutex);
  393. BUG_ON(ret);
  394. cpu_usage_0 = get_cpu_usage_nsec_self(fd);
  395. for (i = 0; i < this_task->nr_events; i++) {
  396. this_task->curr_event = i;
  397. perf_sched__process_event(sched, this_task->atoms[i]);
  398. }
  399. cpu_usage_1 = get_cpu_usage_nsec_self(fd);
  400. this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
  401. ret = sem_post(&this_task->work_done_sem);
  402. BUG_ON(ret);
  403. ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
  404. BUG_ON(ret);
  405. ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
  406. BUG_ON(ret);
  407. goto again;
  408. }
  409. static void create_tasks(struct perf_sched *sched)
  410. {
  411. struct task_desc *task;
  412. pthread_attr_t attr;
  413. unsigned long i;
  414. int err;
  415. err = pthread_attr_init(&attr);
  416. BUG_ON(err);
  417. err = pthread_attr_setstacksize(&attr,
  418. (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
  419. BUG_ON(err);
  420. err = pthread_mutex_lock(&sched->start_work_mutex);
  421. BUG_ON(err);
  422. err = pthread_mutex_lock(&sched->work_done_wait_mutex);
  423. BUG_ON(err);
  424. for (i = 0; i < sched->nr_tasks; i++) {
  425. struct sched_thread_parms *parms = malloc(sizeof(*parms));
  426. BUG_ON(parms == NULL);
  427. parms->task = task = sched->tasks[i];
  428. parms->sched = sched;
  429. sem_init(&task->sleep_sem, 0, 0);
  430. sem_init(&task->ready_for_work, 0, 0);
  431. sem_init(&task->work_done_sem, 0, 0);
  432. task->curr_event = 0;
  433. err = pthread_create(&task->thread, &attr, thread_func, parms);
  434. BUG_ON(err);
  435. }
  436. }
  437. static void wait_for_tasks(struct perf_sched *sched)
  438. {
  439. u64 cpu_usage_0, cpu_usage_1;
  440. struct task_desc *task;
  441. unsigned long i, ret;
  442. sched->start_time = get_nsecs();
  443. sched->cpu_usage = 0;
  444. pthread_mutex_unlock(&sched->work_done_wait_mutex);
  445. for (i = 0; i < sched->nr_tasks; i++) {
  446. task = sched->tasks[i];
  447. ret = sem_wait(&task->ready_for_work);
  448. BUG_ON(ret);
  449. sem_init(&task->ready_for_work, 0, 0);
  450. }
  451. ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
  452. BUG_ON(ret);
  453. cpu_usage_0 = get_cpu_usage_nsec_parent();
  454. pthread_mutex_unlock(&sched->start_work_mutex);
  455. for (i = 0; i < sched->nr_tasks; i++) {
  456. task = sched->tasks[i];
  457. ret = sem_wait(&task->work_done_sem);
  458. BUG_ON(ret);
  459. sem_init(&task->work_done_sem, 0, 0);
  460. sched->cpu_usage += task->cpu_usage;
  461. task->cpu_usage = 0;
  462. }
  463. cpu_usage_1 = get_cpu_usage_nsec_parent();
  464. if (!sched->runavg_cpu_usage)
  465. sched->runavg_cpu_usage = sched->cpu_usage;
  466. sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
  467. sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
  468. if (!sched->runavg_parent_cpu_usage)
  469. sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
  470. sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
  471. sched->parent_cpu_usage)/10;
  472. ret = pthread_mutex_lock(&sched->start_work_mutex);
  473. BUG_ON(ret);
  474. for (i = 0; i < sched->nr_tasks; i++) {
  475. task = sched->tasks[i];
  476. sem_init(&task->sleep_sem, 0, 0);
  477. task->curr_event = 0;
  478. }
  479. }
  480. static void run_one_test(struct perf_sched *sched)
  481. {
  482. u64 T0, T1, delta, avg_delta, fluct;
  483. T0 = get_nsecs();
  484. wait_for_tasks(sched);
  485. T1 = get_nsecs();
  486. delta = T1 - T0;
  487. sched->sum_runtime += delta;
  488. sched->nr_runs++;
  489. avg_delta = sched->sum_runtime / sched->nr_runs;
  490. if (delta < avg_delta)
  491. fluct = avg_delta - delta;
  492. else
  493. fluct = delta - avg_delta;
  494. sched->sum_fluct += fluct;
  495. if (!sched->run_avg)
  496. sched->run_avg = delta;
  497. sched->run_avg = (sched->run_avg * 9 + delta) / 10;
  498. printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
  499. printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
  500. printf("cpu: %0.2f / %0.2f",
  501. (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
  502. #if 0
  503. /*
  504. * rusage statistics done by the parent, these are less
  505. * accurate than the sched->sum_exec_runtime based statistics:
  506. */
  507. printf(" [%0.2f / %0.2f]",
  508. (double)sched->parent_cpu_usage/1e6,
  509. (double)sched->runavg_parent_cpu_usage/1e6);
  510. #endif
  511. printf("\n");
  512. if (sched->nr_sleep_corrections)
  513. printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
  514. sched->nr_sleep_corrections = 0;
  515. }
  516. static void test_calibrations(struct perf_sched *sched)
  517. {
  518. u64 T0, T1;
  519. T0 = get_nsecs();
  520. burn_nsecs(sched, 1e6);
  521. T1 = get_nsecs();
  522. printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
  523. T0 = get_nsecs();
  524. sleep_nsecs(1e6);
  525. T1 = get_nsecs();
  526. printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
  527. }
  528. static int
  529. replay_wakeup_event(struct perf_sched *sched,
  530. struct perf_evsel *evsel, struct perf_sample *sample,
  531. struct machine *machine __maybe_unused)
  532. {
  533. const char *comm = perf_evsel__strval(evsel, sample, "comm");
  534. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  535. struct task_desc *waker, *wakee;
  536. if (verbose) {
  537. printf("sched_wakeup event %p\n", evsel);
  538. printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
  539. }
  540. waker = register_pid(sched, sample->tid, "<unknown>");
  541. wakee = register_pid(sched, pid, comm);
  542. add_sched_event_wakeup(sched, waker, sample->time, wakee);
  543. return 0;
  544. }
  545. static int replay_switch_event(struct perf_sched *sched,
  546. struct perf_evsel *evsel,
  547. struct perf_sample *sample,
  548. struct machine *machine __maybe_unused)
  549. {
  550. const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
  551. *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
  552. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  553. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  554. const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  555. struct task_desc *prev, __maybe_unused *next;
  556. u64 timestamp0, timestamp = sample->time;
  557. int cpu = sample->cpu;
  558. s64 delta;
  559. if (verbose)
  560. printf("sched_switch event %p\n", evsel);
  561. if (cpu >= MAX_CPUS || cpu < 0)
  562. return 0;
  563. timestamp0 = sched->cpu_last_switched[cpu];
  564. if (timestamp0)
  565. delta = timestamp - timestamp0;
  566. else
  567. delta = 0;
  568. if (delta < 0) {
  569. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  570. return -1;
  571. }
  572. pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
  573. prev_comm, prev_pid, next_comm, next_pid, delta);
  574. prev = register_pid(sched, prev_pid, prev_comm);
  575. next = register_pid(sched, next_pid, next_comm);
  576. sched->cpu_last_switched[cpu] = timestamp;
  577. add_sched_event_run(sched, prev, timestamp, delta);
  578. add_sched_event_sleep(sched, prev, timestamp, prev_state);
  579. return 0;
  580. }
  581. static int replay_fork_event(struct perf_sched *sched, struct perf_evsel *evsel,
  582. struct perf_sample *sample)
  583. {
  584. const char *parent_comm = perf_evsel__strval(evsel, sample, "parent_comm"),
  585. *child_comm = perf_evsel__strval(evsel, sample, "child_comm");
  586. const u32 parent_pid = perf_evsel__intval(evsel, sample, "parent_pid"),
  587. child_pid = perf_evsel__intval(evsel, sample, "child_pid");
  588. if (verbose) {
  589. printf("sched_fork event %p\n", evsel);
  590. printf("... parent: %s/%d\n", parent_comm, parent_pid);
  591. printf("... child: %s/%d\n", child_comm, child_pid);
  592. }
  593. register_pid(sched, parent_pid, parent_comm);
  594. register_pid(sched, child_pid, child_comm);
  595. return 0;
  596. }
  597. struct sort_dimension {
  598. const char *name;
  599. sort_fn_t cmp;
  600. struct list_head list;
  601. };
  602. static int
  603. thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
  604. {
  605. struct sort_dimension *sort;
  606. int ret = 0;
  607. BUG_ON(list_empty(list));
  608. list_for_each_entry(sort, list, list) {
  609. ret = sort->cmp(l, r);
  610. if (ret)
  611. return ret;
  612. }
  613. return ret;
  614. }
  615. static struct work_atoms *
  616. thread_atoms_search(struct rb_root *root, struct thread *thread,
  617. struct list_head *sort_list)
  618. {
  619. struct rb_node *node = root->rb_node;
  620. struct work_atoms key = { .thread = thread };
  621. while (node) {
  622. struct work_atoms *atoms;
  623. int cmp;
  624. atoms = container_of(node, struct work_atoms, node);
  625. cmp = thread_lat_cmp(sort_list, &key, atoms);
  626. if (cmp > 0)
  627. node = node->rb_left;
  628. else if (cmp < 0)
  629. node = node->rb_right;
  630. else {
  631. BUG_ON(thread != atoms->thread);
  632. return atoms;
  633. }
  634. }
  635. return NULL;
  636. }
  637. static void
  638. __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
  639. struct list_head *sort_list)
  640. {
  641. struct rb_node **new = &(root->rb_node), *parent = NULL;
  642. while (*new) {
  643. struct work_atoms *this;
  644. int cmp;
  645. this = container_of(*new, struct work_atoms, node);
  646. parent = *new;
  647. cmp = thread_lat_cmp(sort_list, data, this);
  648. if (cmp > 0)
  649. new = &((*new)->rb_left);
  650. else
  651. new = &((*new)->rb_right);
  652. }
  653. rb_link_node(&data->node, parent, new);
  654. rb_insert_color(&data->node, root);
  655. }
  656. static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
  657. {
  658. struct work_atoms *atoms = zalloc(sizeof(*atoms));
  659. if (!atoms) {
  660. pr_err("No memory at %s\n", __func__);
  661. return -1;
  662. }
  663. atoms->thread = thread;
  664. INIT_LIST_HEAD(&atoms->work_list);
  665. __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
  666. return 0;
  667. }
  668. static int latency_fork_event(struct perf_sched *sched __maybe_unused,
  669. struct perf_evsel *evsel __maybe_unused,
  670. struct perf_sample *sample __maybe_unused)
  671. {
  672. /* should insert the newcomer */
  673. return 0;
  674. }
  675. static char sched_out_state(u64 prev_state)
  676. {
  677. const char *str = TASK_STATE_TO_CHAR_STR;
  678. return str[prev_state];
  679. }
  680. static int
  681. add_sched_out_event(struct work_atoms *atoms,
  682. char run_state,
  683. u64 timestamp)
  684. {
  685. struct work_atom *atom = zalloc(sizeof(*atom));
  686. if (!atom) {
  687. pr_err("Non memory at %s", __func__);
  688. return -1;
  689. }
  690. atom->sched_out_time = timestamp;
  691. if (run_state == 'R') {
  692. atom->state = THREAD_WAIT_CPU;
  693. atom->wake_up_time = atom->sched_out_time;
  694. }
  695. list_add_tail(&atom->list, &atoms->work_list);
  696. return 0;
  697. }
  698. static void
  699. add_runtime_event(struct work_atoms *atoms, u64 delta,
  700. u64 timestamp __maybe_unused)
  701. {
  702. struct work_atom *atom;
  703. BUG_ON(list_empty(&atoms->work_list));
  704. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  705. atom->runtime += delta;
  706. atoms->total_runtime += delta;
  707. }
  708. static void
  709. add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
  710. {
  711. struct work_atom *atom;
  712. u64 delta;
  713. if (list_empty(&atoms->work_list))
  714. return;
  715. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  716. if (atom->state != THREAD_WAIT_CPU)
  717. return;
  718. if (timestamp < atom->wake_up_time) {
  719. atom->state = THREAD_IGNORE;
  720. return;
  721. }
  722. atom->state = THREAD_SCHED_IN;
  723. atom->sched_in_time = timestamp;
  724. delta = atom->sched_in_time - atom->wake_up_time;
  725. atoms->total_lat += delta;
  726. if (delta > atoms->max_lat) {
  727. atoms->max_lat = delta;
  728. atoms->max_lat_at = timestamp;
  729. }
  730. atoms->nb_atoms++;
  731. }
  732. static int latency_switch_event(struct perf_sched *sched,
  733. struct perf_evsel *evsel,
  734. struct perf_sample *sample,
  735. struct machine *machine)
  736. {
  737. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  738. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  739. const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  740. struct work_atoms *out_events, *in_events;
  741. struct thread *sched_out, *sched_in;
  742. u64 timestamp0, timestamp = sample->time;
  743. int cpu = sample->cpu;
  744. s64 delta;
  745. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  746. timestamp0 = sched->cpu_last_switched[cpu];
  747. sched->cpu_last_switched[cpu] = timestamp;
  748. if (timestamp0)
  749. delta = timestamp - timestamp0;
  750. else
  751. delta = 0;
  752. if (delta < 0) {
  753. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  754. return -1;
  755. }
  756. sched_out = machine__findnew_thread(machine, prev_pid);
  757. sched_in = machine__findnew_thread(machine, next_pid);
  758. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  759. if (!out_events) {
  760. if (thread_atoms_insert(sched, sched_out))
  761. return -1;
  762. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  763. if (!out_events) {
  764. pr_err("out-event: Internal tree error");
  765. return -1;
  766. }
  767. }
  768. if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
  769. return -1;
  770. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  771. if (!in_events) {
  772. if (thread_atoms_insert(sched, sched_in))
  773. return -1;
  774. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  775. if (!in_events) {
  776. pr_err("in-event: Internal tree error");
  777. return -1;
  778. }
  779. /*
  780. * Take came in we have not heard about yet,
  781. * add in an initial atom in runnable state:
  782. */
  783. if (add_sched_out_event(in_events, 'R', timestamp))
  784. return -1;
  785. }
  786. add_sched_in_event(in_events, timestamp);
  787. return 0;
  788. }
  789. static int latency_runtime_event(struct perf_sched *sched,
  790. struct perf_evsel *evsel,
  791. struct perf_sample *sample,
  792. struct machine *machine)
  793. {
  794. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  795. const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  796. struct thread *thread = machine__findnew_thread(machine, pid);
  797. struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  798. u64 timestamp = sample->time;
  799. int cpu = sample->cpu;
  800. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  801. if (!atoms) {
  802. if (thread_atoms_insert(sched, thread))
  803. return -1;
  804. atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  805. if (!atoms) {
  806. pr_err("in-event: Internal tree error");
  807. return -1;
  808. }
  809. if (add_sched_out_event(atoms, 'R', timestamp))
  810. return -1;
  811. }
  812. add_runtime_event(atoms, runtime, timestamp);
  813. return 0;
  814. }
  815. static int latency_wakeup_event(struct perf_sched *sched,
  816. struct perf_evsel *evsel,
  817. struct perf_sample *sample,
  818. struct machine *machine)
  819. {
  820. const u32 pid = perf_evsel__intval(evsel, sample, "pid"),
  821. success = perf_evsel__intval(evsel, sample, "success");
  822. struct work_atoms *atoms;
  823. struct work_atom *atom;
  824. struct thread *wakee;
  825. u64 timestamp = sample->time;
  826. /* Note for later, it may be interesting to observe the failing cases */
  827. if (!success)
  828. return 0;
  829. wakee = machine__findnew_thread(machine, pid);
  830. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  831. if (!atoms) {
  832. if (thread_atoms_insert(sched, wakee))
  833. return -1;
  834. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  835. if (!atoms) {
  836. pr_err("wakeup-event: Internal tree error");
  837. return -1;
  838. }
  839. if (add_sched_out_event(atoms, 'S', timestamp))
  840. return -1;
  841. }
  842. BUG_ON(list_empty(&atoms->work_list));
  843. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  844. /*
  845. * You WILL be missing events if you've recorded only
  846. * one CPU, or are only looking at only one, so don't
  847. * make useless noise.
  848. */
  849. if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
  850. sched->nr_state_machine_bugs++;
  851. sched->nr_timestamps++;
  852. if (atom->sched_out_time > timestamp) {
  853. sched->nr_unordered_timestamps++;
  854. return 0;
  855. }
  856. atom->state = THREAD_WAIT_CPU;
  857. atom->wake_up_time = timestamp;
  858. return 0;
  859. }
  860. static int latency_migrate_task_event(struct perf_sched *sched,
  861. struct perf_evsel *evsel,
  862. struct perf_sample *sample,
  863. struct machine *machine)
  864. {
  865. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  866. u64 timestamp = sample->time;
  867. struct work_atoms *atoms;
  868. struct work_atom *atom;
  869. struct thread *migrant;
  870. /*
  871. * Only need to worry about migration when profiling one CPU.
  872. */
  873. if (sched->profile_cpu == -1)
  874. return 0;
  875. migrant = machine__findnew_thread(machine, pid);
  876. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  877. if (!atoms) {
  878. if (thread_atoms_insert(sched, migrant))
  879. return -1;
  880. register_pid(sched, migrant->pid, migrant->comm);
  881. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  882. if (!atoms) {
  883. pr_err("migration-event: Internal tree error");
  884. return -1;
  885. }
  886. if (add_sched_out_event(atoms, 'R', timestamp))
  887. return -1;
  888. }
  889. BUG_ON(list_empty(&atoms->work_list));
  890. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  891. atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
  892. sched->nr_timestamps++;
  893. if (atom->sched_out_time > timestamp)
  894. sched->nr_unordered_timestamps++;
  895. return 0;
  896. }
  897. static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
  898. {
  899. int i;
  900. int ret;
  901. u64 avg;
  902. if (!work_list->nb_atoms)
  903. return;
  904. /*
  905. * Ignore idle threads:
  906. */
  907. if (!strcmp(work_list->thread->comm, "swapper"))
  908. return;
  909. sched->all_runtime += work_list->total_runtime;
  910. sched->all_count += work_list->nb_atoms;
  911. ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
  912. for (i = 0; i < 24 - ret; i++)
  913. printf(" ");
  914. avg = work_list->total_lat / work_list->nb_atoms;
  915. printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
  916. (double)work_list->total_runtime / 1e6,
  917. work_list->nb_atoms, (double)avg / 1e6,
  918. (double)work_list->max_lat / 1e6,
  919. (double)work_list->max_lat_at / 1e9);
  920. }
  921. static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
  922. {
  923. if (l->thread->pid < r->thread->pid)
  924. return -1;
  925. if (l->thread->pid > r->thread->pid)
  926. return 1;
  927. return 0;
  928. }
  929. static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
  930. {
  931. u64 avgl, avgr;
  932. if (!l->nb_atoms)
  933. return -1;
  934. if (!r->nb_atoms)
  935. return 1;
  936. avgl = l->total_lat / l->nb_atoms;
  937. avgr = r->total_lat / r->nb_atoms;
  938. if (avgl < avgr)
  939. return -1;
  940. if (avgl > avgr)
  941. return 1;
  942. return 0;
  943. }
  944. static int max_cmp(struct work_atoms *l, struct work_atoms *r)
  945. {
  946. if (l->max_lat < r->max_lat)
  947. return -1;
  948. if (l->max_lat > r->max_lat)
  949. return 1;
  950. return 0;
  951. }
  952. static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
  953. {
  954. if (l->nb_atoms < r->nb_atoms)
  955. return -1;
  956. if (l->nb_atoms > r->nb_atoms)
  957. return 1;
  958. return 0;
  959. }
  960. static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
  961. {
  962. if (l->total_runtime < r->total_runtime)
  963. return -1;
  964. if (l->total_runtime > r->total_runtime)
  965. return 1;
  966. return 0;
  967. }
  968. static int sort_dimension__add(const char *tok, struct list_head *list)
  969. {
  970. size_t i;
  971. static struct sort_dimension avg_sort_dimension = {
  972. .name = "avg",
  973. .cmp = avg_cmp,
  974. };
  975. static struct sort_dimension max_sort_dimension = {
  976. .name = "max",
  977. .cmp = max_cmp,
  978. };
  979. static struct sort_dimension pid_sort_dimension = {
  980. .name = "pid",
  981. .cmp = pid_cmp,
  982. };
  983. static struct sort_dimension runtime_sort_dimension = {
  984. .name = "runtime",
  985. .cmp = runtime_cmp,
  986. };
  987. static struct sort_dimension switch_sort_dimension = {
  988. .name = "switch",
  989. .cmp = switch_cmp,
  990. };
  991. struct sort_dimension *available_sorts[] = {
  992. &pid_sort_dimension,
  993. &avg_sort_dimension,
  994. &max_sort_dimension,
  995. &switch_sort_dimension,
  996. &runtime_sort_dimension,
  997. };
  998. for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
  999. if (!strcmp(available_sorts[i]->name, tok)) {
  1000. list_add_tail(&available_sorts[i]->list, list);
  1001. return 0;
  1002. }
  1003. }
  1004. return -1;
  1005. }
  1006. static void perf_sched__sort_lat(struct perf_sched *sched)
  1007. {
  1008. struct rb_node *node;
  1009. for (;;) {
  1010. struct work_atoms *data;
  1011. node = rb_first(&sched->atom_root);
  1012. if (!node)
  1013. break;
  1014. rb_erase(node, &sched->atom_root);
  1015. data = rb_entry(node, struct work_atoms, node);
  1016. __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
  1017. }
  1018. }
  1019. static int process_sched_wakeup_event(struct perf_tool *tool,
  1020. struct perf_evsel *evsel,
  1021. struct perf_sample *sample,
  1022. struct machine *machine)
  1023. {
  1024. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1025. if (sched->tp_handler->wakeup_event)
  1026. return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
  1027. return 0;
  1028. }
  1029. static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
  1030. struct perf_sample *sample, struct machine *machine)
  1031. {
  1032. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  1033. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1034. struct thread *sched_out __maybe_unused, *sched_in;
  1035. int new_shortname;
  1036. u64 timestamp0, timestamp = sample->time;
  1037. s64 delta;
  1038. int cpu, this_cpu = sample->cpu;
  1039. BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
  1040. if (this_cpu > sched->max_cpu)
  1041. sched->max_cpu = this_cpu;
  1042. timestamp0 = sched->cpu_last_switched[this_cpu];
  1043. sched->cpu_last_switched[this_cpu] = timestamp;
  1044. if (timestamp0)
  1045. delta = timestamp - timestamp0;
  1046. else
  1047. delta = 0;
  1048. if (delta < 0) {
  1049. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  1050. return -1;
  1051. }
  1052. sched_out = machine__findnew_thread(machine, prev_pid);
  1053. sched_in = machine__findnew_thread(machine, next_pid);
  1054. sched->curr_thread[this_cpu] = sched_in;
  1055. printf(" ");
  1056. new_shortname = 0;
  1057. if (!sched_in->shortname[0]) {
  1058. sched_in->shortname[0] = sched->next_shortname1;
  1059. sched_in->shortname[1] = sched->next_shortname2;
  1060. if (sched->next_shortname1 < 'Z') {
  1061. sched->next_shortname1++;
  1062. } else {
  1063. sched->next_shortname1='A';
  1064. if (sched->next_shortname2 < '9') {
  1065. sched->next_shortname2++;
  1066. } else {
  1067. sched->next_shortname2='0';
  1068. }
  1069. }
  1070. new_shortname = 1;
  1071. }
  1072. for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
  1073. if (cpu != this_cpu)
  1074. printf(" ");
  1075. else
  1076. printf("*");
  1077. if (sched->curr_thread[cpu]) {
  1078. if (sched->curr_thread[cpu]->pid)
  1079. printf("%2s ", sched->curr_thread[cpu]->shortname);
  1080. else
  1081. printf(". ");
  1082. } else
  1083. printf(" ");
  1084. }
  1085. printf(" %12.6f secs ", (double)timestamp/1e9);
  1086. if (new_shortname) {
  1087. printf("%s => %s:%d\n",
  1088. sched_in->shortname, sched_in->comm, sched_in->pid);
  1089. } else {
  1090. printf("\n");
  1091. }
  1092. return 0;
  1093. }
  1094. static int process_sched_switch_event(struct perf_tool *tool,
  1095. struct perf_evsel *evsel,
  1096. struct perf_sample *sample,
  1097. struct machine *machine)
  1098. {
  1099. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1100. int this_cpu = sample->cpu, err = 0;
  1101. u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  1102. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1103. if (sched->curr_pid[this_cpu] != (u32)-1) {
  1104. /*
  1105. * Are we trying to switch away a PID that is
  1106. * not current?
  1107. */
  1108. if (sched->curr_pid[this_cpu] != prev_pid)
  1109. sched->nr_context_switch_bugs++;
  1110. }
  1111. if (sched->tp_handler->switch_event)
  1112. err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
  1113. sched->curr_pid[this_cpu] = next_pid;
  1114. return err;
  1115. }
  1116. static int process_sched_runtime_event(struct perf_tool *tool,
  1117. struct perf_evsel *evsel,
  1118. struct perf_sample *sample,
  1119. struct machine *machine)
  1120. {
  1121. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1122. if (sched->tp_handler->runtime_event)
  1123. return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
  1124. return 0;
  1125. }
  1126. static int process_sched_fork_event(struct perf_tool *tool,
  1127. struct perf_evsel *evsel,
  1128. struct perf_sample *sample,
  1129. struct machine *machine __maybe_unused)
  1130. {
  1131. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1132. if (sched->tp_handler->fork_event)
  1133. return sched->tp_handler->fork_event(sched, evsel, sample);
  1134. return 0;
  1135. }
  1136. static int process_sched_exit_event(struct perf_tool *tool __maybe_unused,
  1137. struct perf_evsel *evsel,
  1138. struct perf_sample *sample __maybe_unused,
  1139. struct machine *machine __maybe_unused)
  1140. {
  1141. pr_debug("sched_exit event %p\n", evsel);
  1142. return 0;
  1143. }
  1144. static int process_sched_migrate_task_event(struct perf_tool *tool,
  1145. struct perf_evsel *evsel,
  1146. struct perf_sample *sample,
  1147. struct machine *machine)
  1148. {
  1149. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1150. if (sched->tp_handler->migrate_task_event)
  1151. return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
  1152. return 0;
  1153. }
  1154. typedef int (*tracepoint_handler)(struct perf_tool *tool,
  1155. struct perf_evsel *evsel,
  1156. struct perf_sample *sample,
  1157. struct machine *machine);
  1158. static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
  1159. union perf_event *event __maybe_unused,
  1160. struct perf_sample *sample,
  1161. struct perf_evsel *evsel,
  1162. struct machine *machine)
  1163. {
  1164. struct thread *thread = machine__findnew_thread(machine, sample->tid);
  1165. int err = 0;
  1166. if (thread == NULL) {
  1167. pr_debug("problem processing %s event, skipping it.\n",
  1168. perf_evsel__name(evsel));
  1169. return -1;
  1170. }
  1171. evsel->hists.stats.total_period += sample->period;
  1172. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  1173. if (evsel->handler.func != NULL) {
  1174. tracepoint_handler f = evsel->handler.func;
  1175. err = f(tool, evsel, sample, machine);
  1176. }
  1177. return err;
  1178. }
  1179. static int perf_sched__read_events(struct perf_sched *sched, bool destroy,
  1180. struct perf_session **psession)
  1181. {
  1182. const struct perf_evsel_str_handler handlers[] = {
  1183. { "sched:sched_switch", process_sched_switch_event, },
  1184. { "sched:sched_stat_runtime", process_sched_runtime_event, },
  1185. { "sched:sched_wakeup", process_sched_wakeup_event, },
  1186. { "sched:sched_wakeup_new", process_sched_wakeup_event, },
  1187. { "sched:sched_process_fork", process_sched_fork_event, },
  1188. { "sched:sched_process_exit", process_sched_exit_event, },
  1189. { "sched:sched_migrate_task", process_sched_migrate_task_event, },
  1190. };
  1191. struct perf_session *session;
  1192. session = perf_session__new(input_name, O_RDONLY, 0, false, &sched->tool);
  1193. if (session == NULL) {
  1194. pr_debug("No Memory for session\n");
  1195. return -1;
  1196. }
  1197. if (perf_session__set_tracepoints_handlers(session, handlers))
  1198. goto out_delete;
  1199. if (perf_session__has_traces(session, "record -R")) {
  1200. int err = perf_session__process_events(session, &sched->tool);
  1201. if (err) {
  1202. pr_err("Failed to process events, error %d", err);
  1203. goto out_delete;
  1204. }
  1205. sched->nr_events = session->hists.stats.nr_events[0];
  1206. sched->nr_lost_events = session->hists.stats.total_lost;
  1207. sched->nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
  1208. }
  1209. if (destroy)
  1210. perf_session__delete(session);
  1211. if (psession)
  1212. *psession = session;
  1213. return 0;
  1214. out_delete:
  1215. perf_session__delete(session);
  1216. return -1;
  1217. }
  1218. static void print_bad_events(struct perf_sched *sched)
  1219. {
  1220. if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
  1221. printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
  1222. (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
  1223. sched->nr_unordered_timestamps, sched->nr_timestamps);
  1224. }
  1225. if (sched->nr_lost_events && sched->nr_events) {
  1226. printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
  1227. (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
  1228. sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
  1229. }
  1230. if (sched->nr_state_machine_bugs && sched->nr_timestamps) {
  1231. printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
  1232. (double)sched->nr_state_machine_bugs/(double)sched->nr_timestamps*100.0,
  1233. sched->nr_state_machine_bugs, sched->nr_timestamps);
  1234. if (sched->nr_lost_events)
  1235. printf(" (due to lost events?)");
  1236. printf("\n");
  1237. }
  1238. if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
  1239. printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
  1240. (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
  1241. sched->nr_context_switch_bugs, sched->nr_timestamps);
  1242. if (sched->nr_lost_events)
  1243. printf(" (due to lost events?)");
  1244. printf("\n");
  1245. }
  1246. }
  1247. static int perf_sched__lat(struct perf_sched *sched)
  1248. {
  1249. struct rb_node *next;
  1250. struct perf_session *session;
  1251. setup_pager();
  1252. if (perf_sched__read_events(sched, false, &session))
  1253. return -1;
  1254. perf_sched__sort_lat(sched);
  1255. printf("\n ---------------------------------------------------------------------------------------------------------------\n");
  1256. printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
  1257. printf(" ---------------------------------------------------------------------------------------------------------------\n");
  1258. next = rb_first(&sched->sorted_atom_root);
  1259. while (next) {
  1260. struct work_atoms *work_list;
  1261. work_list = rb_entry(next, struct work_atoms, node);
  1262. output_lat_thread(sched, work_list);
  1263. next = rb_next(next);
  1264. }
  1265. printf(" -----------------------------------------------------------------------------------------\n");
  1266. printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
  1267. (double)sched->all_runtime / 1e6, sched->all_count);
  1268. printf(" ---------------------------------------------------\n");
  1269. print_bad_events(sched);
  1270. printf("\n");
  1271. perf_session__delete(session);
  1272. return 0;
  1273. }
  1274. static int perf_sched__map(struct perf_sched *sched)
  1275. {
  1276. sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
  1277. setup_pager();
  1278. if (perf_sched__read_events(sched, true, NULL))
  1279. return -1;
  1280. print_bad_events(sched);
  1281. return 0;
  1282. }
  1283. static int perf_sched__replay(struct perf_sched *sched)
  1284. {
  1285. unsigned long i;
  1286. calibrate_run_measurement_overhead(sched);
  1287. calibrate_sleep_measurement_overhead(sched);
  1288. test_calibrations(sched);
  1289. if (perf_sched__read_events(sched, true, NULL))
  1290. return -1;
  1291. printf("nr_run_events: %ld\n", sched->nr_run_events);
  1292. printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
  1293. printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
  1294. if (sched->targetless_wakeups)
  1295. printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
  1296. if (sched->multitarget_wakeups)
  1297. printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
  1298. if (sched->nr_run_events_optimized)
  1299. printf("run atoms optimized: %ld\n",
  1300. sched->nr_run_events_optimized);
  1301. print_task_traces(sched);
  1302. add_cross_task_wakeups(sched);
  1303. create_tasks(sched);
  1304. printf("------------------------------------------------------------\n");
  1305. for (i = 0; i < sched->replay_repeat; i++)
  1306. run_one_test(sched);
  1307. return 0;
  1308. }
  1309. static void setup_sorting(struct perf_sched *sched, const struct option *options,
  1310. const char * const usage_msg[])
  1311. {
  1312. char *tmp, *tok, *str = strdup(sched->sort_order);
  1313. for (tok = strtok_r(str, ", ", &tmp);
  1314. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1315. if (sort_dimension__add(tok, &sched->sort_list) < 0) {
  1316. error("Unknown --sort key: `%s'", tok);
  1317. usage_with_options(usage_msg, options);
  1318. }
  1319. }
  1320. free(str);
  1321. sort_dimension__add("pid", &sched->cmp_pid);
  1322. }
  1323. static int __cmd_record(int argc, const char **argv)
  1324. {
  1325. unsigned int rec_argc, i, j;
  1326. const char **rec_argv;
  1327. const char * const record_args[] = {
  1328. "record",
  1329. "-a",
  1330. "-R",
  1331. "-f",
  1332. "-m", "1024",
  1333. "-c", "1",
  1334. "-e", "sched:sched_switch",
  1335. "-e", "sched:sched_stat_wait",
  1336. "-e", "sched:sched_stat_sleep",
  1337. "-e", "sched:sched_stat_iowait",
  1338. "-e", "sched:sched_stat_runtime",
  1339. "-e", "sched:sched_process_exit",
  1340. "-e", "sched:sched_process_fork",
  1341. "-e", "sched:sched_wakeup",
  1342. "-e", "sched:sched_migrate_task",
  1343. };
  1344. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  1345. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1346. if (rec_argv == NULL)
  1347. return -ENOMEM;
  1348. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1349. rec_argv[i] = strdup(record_args[i]);
  1350. for (j = 1; j < (unsigned int)argc; j++, i++)
  1351. rec_argv[i] = argv[j];
  1352. BUG_ON(i != rec_argc);
  1353. return cmd_record(i, rec_argv, NULL);
  1354. }
  1355. int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
  1356. {
  1357. const char default_sort_order[] = "avg, max, switch, runtime";
  1358. struct perf_sched sched = {
  1359. .tool = {
  1360. .sample = perf_sched__process_tracepoint_sample,
  1361. .comm = perf_event__process_comm,
  1362. .lost = perf_event__process_lost,
  1363. .exit = perf_event__process_exit,
  1364. .fork = perf_event__process_fork,
  1365. .ordered_samples = true,
  1366. },
  1367. .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
  1368. .sort_list = LIST_HEAD_INIT(sched.sort_list),
  1369. .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
  1370. .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
  1371. .curr_pid = { [0 ... MAX_CPUS - 1] = -1 },
  1372. .sort_order = default_sort_order,
  1373. .replay_repeat = 10,
  1374. .profile_cpu = -1,
  1375. .next_shortname1 = 'A',
  1376. .next_shortname2 = '0',
  1377. };
  1378. const struct option latency_options[] = {
  1379. OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
  1380. "sort by key(s): runtime, switch, avg, max"),
  1381. OPT_INCR('v', "verbose", &verbose,
  1382. "be more verbose (show symbol address, etc)"),
  1383. OPT_INTEGER('C', "CPU", &sched.profile_cpu,
  1384. "CPU to profile on"),
  1385. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1386. "dump raw trace in ASCII"),
  1387. OPT_END()
  1388. };
  1389. const struct option replay_options[] = {
  1390. OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
  1391. "repeat the workload replay N times (-1: infinite)"),
  1392. OPT_INCR('v', "verbose", &verbose,
  1393. "be more verbose (show symbol address, etc)"),
  1394. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1395. "dump raw trace in ASCII"),
  1396. OPT_END()
  1397. };
  1398. const struct option sched_options[] = {
  1399. OPT_STRING('i', "input", &input_name, "file",
  1400. "input file name"),
  1401. OPT_INCR('v', "verbose", &verbose,
  1402. "be more verbose (show symbol address, etc)"),
  1403. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1404. "dump raw trace in ASCII"),
  1405. OPT_END()
  1406. };
  1407. const char * const latency_usage[] = {
  1408. "perf sched latency [<options>]",
  1409. NULL
  1410. };
  1411. const char * const replay_usage[] = {
  1412. "perf sched replay [<options>]",
  1413. NULL
  1414. };
  1415. const char * const sched_usage[] = {
  1416. "perf sched [<options>] {record|latency|map|replay|script}",
  1417. NULL
  1418. };
  1419. struct trace_sched_handler lat_ops = {
  1420. .wakeup_event = latency_wakeup_event,
  1421. .switch_event = latency_switch_event,
  1422. .runtime_event = latency_runtime_event,
  1423. .fork_event = latency_fork_event,
  1424. .migrate_task_event = latency_migrate_task_event,
  1425. };
  1426. struct trace_sched_handler map_ops = {
  1427. .switch_event = map_switch_event,
  1428. };
  1429. struct trace_sched_handler replay_ops = {
  1430. .wakeup_event = replay_wakeup_event,
  1431. .switch_event = replay_switch_event,
  1432. .fork_event = replay_fork_event,
  1433. };
  1434. argc = parse_options(argc, argv, sched_options, sched_usage,
  1435. PARSE_OPT_STOP_AT_NON_OPTION);
  1436. if (!argc)
  1437. usage_with_options(sched_usage, sched_options);
  1438. /*
  1439. * Aliased to 'perf script' for now:
  1440. */
  1441. if (!strcmp(argv[0], "script"))
  1442. return cmd_script(argc, argv, prefix);
  1443. symbol__init();
  1444. if (!strncmp(argv[0], "rec", 3)) {
  1445. return __cmd_record(argc, argv);
  1446. } else if (!strncmp(argv[0], "lat", 3)) {
  1447. sched.tp_handler = &lat_ops;
  1448. if (argc > 1) {
  1449. argc = parse_options(argc, argv, latency_options, latency_usage, 0);
  1450. if (argc)
  1451. usage_with_options(latency_usage, latency_options);
  1452. }
  1453. setup_sorting(&sched, latency_options, latency_usage);
  1454. return perf_sched__lat(&sched);
  1455. } else if (!strcmp(argv[0], "map")) {
  1456. sched.tp_handler = &map_ops;
  1457. setup_sorting(&sched, latency_options, latency_usage);
  1458. return perf_sched__map(&sched);
  1459. } else if (!strncmp(argv[0], "rep", 3)) {
  1460. sched.tp_handler = &replay_ops;
  1461. if (argc) {
  1462. argc = parse_options(argc, argv, replay_options, replay_usage, 0);
  1463. if (argc)
  1464. usage_with_options(replay_usage, replay_options);
  1465. }
  1466. return perf_sched__replay(&sched);
  1467. } else {
  1468. usage_with_options(sched_usage, sched_options);
  1469. }
  1470. return 0;
  1471. }