builtin-sched.c 44 KB

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