builtin-sched.c 44 KB

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