builtin-sched.c 43 KB

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