builtin-sched.c 42 KB

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