builtin-sched.c 44 KB

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