builtin-sched.c 43 KB

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