builtin-sched.c 44 KB

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