builtin-sched.c 44 KB

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