builtin-sched.c 44 KB

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