builtin-sched.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. int (*fork_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  89. struct perf_sample *sample);
  90. int (*migrate_task_event)(struct perf_sched *sched,
  91. struct perf_evsel *evsel,
  92. struct perf_sample *sample,
  93. struct machine *machine);
  94. };
  95. struct perf_sched {
  96. struct perf_tool tool;
  97. const char *input_name;
  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, struct perf_evsel *evsel,
  583. struct perf_sample *sample)
  584. {
  585. const char *parent_comm = perf_evsel__strval(evsel, sample, "parent_comm"),
  586. *child_comm = perf_evsel__strval(evsel, sample, "child_comm");
  587. const u32 parent_pid = perf_evsel__intval(evsel, sample, "parent_pid"),
  588. child_pid = perf_evsel__intval(evsel, sample, "child_pid");
  589. if (verbose) {
  590. printf("sched_fork event %p\n", evsel);
  591. printf("... parent: %s/%d\n", parent_comm, parent_pid);
  592. printf("... child: %s/%d\n", child_comm, child_pid);
  593. }
  594. register_pid(sched, parent_pid, parent_comm);
  595. register_pid(sched, child_pid, child_comm);
  596. return 0;
  597. }
  598. struct sort_dimension {
  599. const char *name;
  600. sort_fn_t cmp;
  601. struct list_head list;
  602. };
  603. static int
  604. thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
  605. {
  606. struct sort_dimension *sort;
  607. int ret = 0;
  608. BUG_ON(list_empty(list));
  609. list_for_each_entry(sort, list, list) {
  610. ret = sort->cmp(l, r);
  611. if (ret)
  612. return ret;
  613. }
  614. return ret;
  615. }
  616. static struct work_atoms *
  617. thread_atoms_search(struct rb_root *root, struct thread *thread,
  618. struct list_head *sort_list)
  619. {
  620. struct rb_node *node = root->rb_node;
  621. struct work_atoms key = { .thread = thread };
  622. while (node) {
  623. struct work_atoms *atoms;
  624. int cmp;
  625. atoms = container_of(node, struct work_atoms, node);
  626. cmp = thread_lat_cmp(sort_list, &key, atoms);
  627. if (cmp > 0)
  628. node = node->rb_left;
  629. else if (cmp < 0)
  630. node = node->rb_right;
  631. else {
  632. BUG_ON(thread != atoms->thread);
  633. return atoms;
  634. }
  635. }
  636. return NULL;
  637. }
  638. static void
  639. __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
  640. struct list_head *sort_list)
  641. {
  642. struct rb_node **new = &(root->rb_node), *parent = NULL;
  643. while (*new) {
  644. struct work_atoms *this;
  645. int cmp;
  646. this = container_of(*new, struct work_atoms, node);
  647. parent = *new;
  648. cmp = thread_lat_cmp(sort_list, data, this);
  649. if (cmp > 0)
  650. new = &((*new)->rb_left);
  651. else
  652. new = &((*new)->rb_right);
  653. }
  654. rb_link_node(&data->node, parent, new);
  655. rb_insert_color(&data->node, root);
  656. }
  657. static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
  658. {
  659. struct work_atoms *atoms = zalloc(sizeof(*atoms));
  660. if (!atoms) {
  661. pr_err("No memory at %s\n", __func__);
  662. return -1;
  663. }
  664. atoms->thread = thread;
  665. INIT_LIST_HEAD(&atoms->work_list);
  666. __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
  667. return 0;
  668. }
  669. static int latency_fork_event(struct perf_sched *sched __maybe_unused,
  670. struct perf_evsel *evsel __maybe_unused,
  671. struct perf_sample *sample __maybe_unused)
  672. {
  673. /* should insert the newcomer */
  674. return 0;
  675. }
  676. static char sched_out_state(u64 prev_state)
  677. {
  678. const char *str = TASK_STATE_TO_CHAR_STR;
  679. return str[prev_state];
  680. }
  681. static int
  682. add_sched_out_event(struct work_atoms *atoms,
  683. char run_state,
  684. u64 timestamp)
  685. {
  686. struct work_atom *atom = zalloc(sizeof(*atom));
  687. if (!atom) {
  688. pr_err("Non memory at %s", __func__);
  689. return -1;
  690. }
  691. atom->sched_out_time = timestamp;
  692. if (run_state == 'R') {
  693. atom->state = THREAD_WAIT_CPU;
  694. atom->wake_up_time = atom->sched_out_time;
  695. }
  696. list_add_tail(&atom->list, &atoms->work_list);
  697. return 0;
  698. }
  699. static void
  700. add_runtime_event(struct work_atoms *atoms, u64 delta,
  701. u64 timestamp __maybe_unused)
  702. {
  703. struct work_atom *atom;
  704. BUG_ON(list_empty(&atoms->work_list));
  705. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  706. atom->runtime += delta;
  707. atoms->total_runtime += delta;
  708. }
  709. static void
  710. add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
  711. {
  712. struct work_atom *atom;
  713. u64 delta;
  714. if (list_empty(&atoms->work_list))
  715. return;
  716. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  717. if (atom->state != THREAD_WAIT_CPU)
  718. return;
  719. if (timestamp < atom->wake_up_time) {
  720. atom->state = THREAD_IGNORE;
  721. return;
  722. }
  723. atom->state = THREAD_SCHED_IN;
  724. atom->sched_in_time = timestamp;
  725. delta = atom->sched_in_time - atom->wake_up_time;
  726. atoms->total_lat += delta;
  727. if (delta > atoms->max_lat) {
  728. atoms->max_lat = delta;
  729. atoms->max_lat_at = timestamp;
  730. }
  731. atoms->nb_atoms++;
  732. }
  733. static int latency_switch_event(struct perf_sched *sched,
  734. struct perf_evsel *evsel,
  735. struct perf_sample *sample,
  736. struct machine *machine)
  737. {
  738. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  739. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  740. const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  741. struct work_atoms *out_events, *in_events;
  742. struct thread *sched_out, *sched_in;
  743. u64 timestamp0, timestamp = sample->time;
  744. int cpu = sample->cpu;
  745. s64 delta;
  746. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  747. timestamp0 = sched->cpu_last_switched[cpu];
  748. sched->cpu_last_switched[cpu] = timestamp;
  749. if (timestamp0)
  750. delta = timestamp - timestamp0;
  751. else
  752. delta = 0;
  753. if (delta < 0) {
  754. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  755. return -1;
  756. }
  757. sched_out = machine__findnew_thread(machine, prev_pid);
  758. sched_in = machine__findnew_thread(machine, next_pid);
  759. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  760. if (!out_events) {
  761. if (thread_atoms_insert(sched, sched_out))
  762. return -1;
  763. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  764. if (!out_events) {
  765. pr_err("out-event: Internal tree error");
  766. return -1;
  767. }
  768. }
  769. if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
  770. return -1;
  771. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  772. if (!in_events) {
  773. if (thread_atoms_insert(sched, sched_in))
  774. return -1;
  775. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  776. if (!in_events) {
  777. pr_err("in-event: Internal tree error");
  778. return -1;
  779. }
  780. /*
  781. * Take came in we have not heard about yet,
  782. * add in an initial atom in runnable state:
  783. */
  784. if (add_sched_out_event(in_events, 'R', timestamp))
  785. return -1;
  786. }
  787. add_sched_in_event(in_events, timestamp);
  788. return 0;
  789. }
  790. static int latency_runtime_event(struct perf_sched *sched,
  791. struct perf_evsel *evsel,
  792. struct perf_sample *sample,
  793. struct machine *machine)
  794. {
  795. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  796. const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  797. struct thread *thread = machine__findnew_thread(machine, pid);
  798. struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  799. u64 timestamp = sample->time;
  800. int cpu = sample->cpu;
  801. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  802. if (!atoms) {
  803. if (thread_atoms_insert(sched, thread))
  804. return -1;
  805. atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  806. if (!atoms) {
  807. pr_err("in-event: Internal tree error");
  808. return -1;
  809. }
  810. if (add_sched_out_event(atoms, 'R', timestamp))
  811. return -1;
  812. }
  813. add_runtime_event(atoms, runtime, timestamp);
  814. return 0;
  815. }
  816. static int latency_wakeup_event(struct perf_sched *sched,
  817. struct perf_evsel *evsel,
  818. struct perf_sample *sample,
  819. struct machine *machine)
  820. {
  821. const u32 pid = perf_evsel__intval(evsel, sample, "pid"),
  822. success = perf_evsel__intval(evsel, sample, "success");
  823. struct work_atoms *atoms;
  824. struct work_atom *atom;
  825. struct thread *wakee;
  826. u64 timestamp = sample->time;
  827. /* Note for later, it may be interesting to observe the failing cases */
  828. if (!success)
  829. return 0;
  830. wakee = machine__findnew_thread(machine, pid);
  831. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  832. if (!atoms) {
  833. if (thread_atoms_insert(sched, wakee))
  834. return -1;
  835. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  836. if (!atoms) {
  837. pr_err("wakeup-event: Internal tree error");
  838. return -1;
  839. }
  840. if (add_sched_out_event(atoms, 'S', timestamp))
  841. return -1;
  842. }
  843. BUG_ON(list_empty(&atoms->work_list));
  844. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  845. /*
  846. * You WILL be missing events if you've recorded only
  847. * one CPU, or are only looking at only one, so don't
  848. * make useless noise.
  849. */
  850. if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
  851. sched->nr_state_machine_bugs++;
  852. sched->nr_timestamps++;
  853. if (atom->sched_out_time > timestamp) {
  854. sched->nr_unordered_timestamps++;
  855. return 0;
  856. }
  857. atom->state = THREAD_WAIT_CPU;
  858. atom->wake_up_time = timestamp;
  859. return 0;
  860. }
  861. static int latency_migrate_task_event(struct perf_sched *sched,
  862. struct perf_evsel *evsel,
  863. struct perf_sample *sample,
  864. struct machine *machine)
  865. {
  866. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  867. u64 timestamp = sample->time;
  868. struct work_atoms *atoms;
  869. struct work_atom *atom;
  870. struct thread *migrant;
  871. /*
  872. * Only need to worry about migration when profiling one CPU.
  873. */
  874. if (sched->profile_cpu == -1)
  875. return 0;
  876. migrant = machine__findnew_thread(machine, pid);
  877. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  878. if (!atoms) {
  879. if (thread_atoms_insert(sched, migrant))
  880. return -1;
  881. register_pid(sched, migrant->pid, migrant->comm);
  882. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  883. if (!atoms) {
  884. pr_err("migration-event: Internal tree error");
  885. return -1;
  886. }
  887. if (add_sched_out_event(atoms, 'R', timestamp))
  888. return -1;
  889. }
  890. BUG_ON(list_empty(&atoms->work_list));
  891. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  892. atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
  893. sched->nr_timestamps++;
  894. if (atom->sched_out_time > timestamp)
  895. sched->nr_unordered_timestamps++;
  896. return 0;
  897. }
  898. static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
  899. {
  900. int i;
  901. int ret;
  902. u64 avg;
  903. if (!work_list->nb_atoms)
  904. return;
  905. /*
  906. * Ignore idle threads:
  907. */
  908. if (!strcmp(work_list->thread->comm, "swapper"))
  909. return;
  910. sched->all_runtime += work_list->total_runtime;
  911. sched->all_count += work_list->nb_atoms;
  912. ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
  913. for (i = 0; i < 24 - ret; i++)
  914. printf(" ");
  915. avg = work_list->total_lat / work_list->nb_atoms;
  916. printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
  917. (double)work_list->total_runtime / 1e6,
  918. work_list->nb_atoms, (double)avg / 1e6,
  919. (double)work_list->max_lat / 1e6,
  920. (double)work_list->max_lat_at / 1e9);
  921. }
  922. static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
  923. {
  924. if (l->thread->pid < r->thread->pid)
  925. return -1;
  926. if (l->thread->pid > r->thread->pid)
  927. return 1;
  928. return 0;
  929. }
  930. static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
  931. {
  932. u64 avgl, avgr;
  933. if (!l->nb_atoms)
  934. return -1;
  935. if (!r->nb_atoms)
  936. return 1;
  937. avgl = l->total_lat / l->nb_atoms;
  938. avgr = r->total_lat / r->nb_atoms;
  939. if (avgl < avgr)
  940. return -1;
  941. if (avgl > avgr)
  942. return 1;
  943. return 0;
  944. }
  945. static int max_cmp(struct work_atoms *l, struct work_atoms *r)
  946. {
  947. if (l->max_lat < r->max_lat)
  948. return -1;
  949. if (l->max_lat > r->max_lat)
  950. return 1;
  951. return 0;
  952. }
  953. static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
  954. {
  955. if (l->nb_atoms < r->nb_atoms)
  956. return -1;
  957. if (l->nb_atoms > r->nb_atoms)
  958. return 1;
  959. return 0;
  960. }
  961. static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
  962. {
  963. if (l->total_runtime < r->total_runtime)
  964. return -1;
  965. if (l->total_runtime > r->total_runtime)
  966. return 1;
  967. return 0;
  968. }
  969. static int sort_dimension__add(const char *tok, struct list_head *list)
  970. {
  971. size_t i;
  972. static struct sort_dimension avg_sort_dimension = {
  973. .name = "avg",
  974. .cmp = avg_cmp,
  975. };
  976. static struct sort_dimension max_sort_dimension = {
  977. .name = "max",
  978. .cmp = max_cmp,
  979. };
  980. static struct sort_dimension pid_sort_dimension = {
  981. .name = "pid",
  982. .cmp = pid_cmp,
  983. };
  984. static struct sort_dimension runtime_sort_dimension = {
  985. .name = "runtime",
  986. .cmp = runtime_cmp,
  987. };
  988. static struct sort_dimension switch_sort_dimension = {
  989. .name = "switch",
  990. .cmp = switch_cmp,
  991. };
  992. struct sort_dimension *available_sorts[] = {
  993. &pid_sort_dimension,
  994. &avg_sort_dimension,
  995. &max_sort_dimension,
  996. &switch_sort_dimension,
  997. &runtime_sort_dimension,
  998. };
  999. for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
  1000. if (!strcmp(available_sorts[i]->name, tok)) {
  1001. list_add_tail(&available_sorts[i]->list, list);
  1002. return 0;
  1003. }
  1004. }
  1005. return -1;
  1006. }
  1007. static void perf_sched__sort_lat(struct perf_sched *sched)
  1008. {
  1009. struct rb_node *node;
  1010. for (;;) {
  1011. struct work_atoms *data;
  1012. node = rb_first(&sched->atom_root);
  1013. if (!node)
  1014. break;
  1015. rb_erase(node, &sched->atom_root);
  1016. data = rb_entry(node, struct work_atoms, node);
  1017. __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
  1018. }
  1019. }
  1020. static int process_sched_wakeup_event(struct perf_tool *tool,
  1021. struct perf_evsel *evsel,
  1022. struct perf_sample *sample,
  1023. struct machine *machine)
  1024. {
  1025. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1026. if (sched->tp_handler->wakeup_event)
  1027. return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
  1028. return 0;
  1029. }
  1030. static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
  1031. struct perf_sample *sample, struct machine *machine)
  1032. {
  1033. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  1034. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1035. struct thread *sched_out __maybe_unused, *sched_in;
  1036. int new_shortname;
  1037. u64 timestamp0, timestamp = sample->time;
  1038. s64 delta;
  1039. int cpu, this_cpu = sample->cpu;
  1040. BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
  1041. if (this_cpu > sched->max_cpu)
  1042. sched->max_cpu = this_cpu;
  1043. timestamp0 = sched->cpu_last_switched[this_cpu];
  1044. sched->cpu_last_switched[this_cpu] = timestamp;
  1045. if (timestamp0)
  1046. delta = timestamp - timestamp0;
  1047. else
  1048. delta = 0;
  1049. if (delta < 0) {
  1050. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  1051. return -1;
  1052. }
  1053. sched_out = machine__findnew_thread(machine, prev_pid);
  1054. sched_in = machine__findnew_thread(machine, next_pid);
  1055. sched->curr_thread[this_cpu] = sched_in;
  1056. printf(" ");
  1057. new_shortname = 0;
  1058. if (!sched_in->shortname[0]) {
  1059. sched_in->shortname[0] = sched->next_shortname1;
  1060. sched_in->shortname[1] = sched->next_shortname2;
  1061. if (sched->next_shortname1 < 'Z') {
  1062. sched->next_shortname1++;
  1063. } else {
  1064. sched->next_shortname1='A';
  1065. if (sched->next_shortname2 < '9') {
  1066. sched->next_shortname2++;
  1067. } else {
  1068. sched->next_shortname2='0';
  1069. }
  1070. }
  1071. new_shortname = 1;
  1072. }
  1073. for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
  1074. if (cpu != this_cpu)
  1075. printf(" ");
  1076. else
  1077. printf("*");
  1078. if (sched->curr_thread[cpu]) {
  1079. if (sched->curr_thread[cpu]->pid)
  1080. printf("%2s ", sched->curr_thread[cpu]->shortname);
  1081. else
  1082. printf(". ");
  1083. } else
  1084. printf(" ");
  1085. }
  1086. printf(" %12.6f secs ", (double)timestamp/1e9);
  1087. if (new_shortname) {
  1088. printf("%s => %s:%d\n",
  1089. sched_in->shortname, sched_in->comm, sched_in->pid);
  1090. } else {
  1091. printf("\n");
  1092. }
  1093. return 0;
  1094. }
  1095. static int process_sched_switch_event(struct perf_tool *tool,
  1096. struct perf_evsel *evsel,
  1097. struct perf_sample *sample,
  1098. struct machine *machine)
  1099. {
  1100. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1101. int this_cpu = sample->cpu, err = 0;
  1102. u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  1103. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1104. if (sched->curr_pid[this_cpu] != (u32)-1) {
  1105. /*
  1106. * Are we trying to switch away a PID that is
  1107. * not current?
  1108. */
  1109. if (sched->curr_pid[this_cpu] != prev_pid)
  1110. sched->nr_context_switch_bugs++;
  1111. }
  1112. if (sched->tp_handler->switch_event)
  1113. err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
  1114. sched->curr_pid[this_cpu] = next_pid;
  1115. return err;
  1116. }
  1117. static int process_sched_runtime_event(struct perf_tool *tool,
  1118. struct perf_evsel *evsel,
  1119. struct perf_sample *sample,
  1120. struct machine *machine)
  1121. {
  1122. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1123. if (sched->tp_handler->runtime_event)
  1124. return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
  1125. return 0;
  1126. }
  1127. static int process_sched_fork_event(struct perf_tool *tool,
  1128. struct perf_evsel *evsel,
  1129. struct perf_sample *sample,
  1130. struct machine *machine __maybe_unused)
  1131. {
  1132. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1133. if (sched->tp_handler->fork_event)
  1134. return sched->tp_handler->fork_event(sched, evsel, sample);
  1135. return 0;
  1136. }
  1137. static int process_sched_exit_event(struct perf_tool *tool __maybe_unused,
  1138. struct perf_evsel *evsel,
  1139. struct perf_sample *sample __maybe_unused,
  1140. struct machine *machine __maybe_unused)
  1141. {
  1142. pr_debug("sched_exit event %p\n", evsel);
  1143. return 0;
  1144. }
  1145. static int process_sched_migrate_task_event(struct perf_tool *tool,
  1146. struct perf_evsel *evsel,
  1147. struct perf_sample *sample,
  1148. struct machine *machine)
  1149. {
  1150. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1151. if (sched->tp_handler->migrate_task_event)
  1152. return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
  1153. return 0;
  1154. }
  1155. typedef int (*tracepoint_handler)(struct perf_tool *tool,
  1156. struct perf_evsel *evsel,
  1157. struct perf_sample *sample,
  1158. struct machine *machine);
  1159. static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
  1160. union perf_event *event __maybe_unused,
  1161. struct perf_sample *sample,
  1162. struct perf_evsel *evsel,
  1163. struct machine *machine)
  1164. {
  1165. struct thread *thread = machine__findnew_thread(machine, sample->tid);
  1166. int err = 0;
  1167. if (thread == NULL) {
  1168. pr_debug("problem processing %s event, skipping it.\n",
  1169. perf_evsel__name(evsel));
  1170. return -1;
  1171. }
  1172. evsel->hists.stats.total_period += sample->period;
  1173. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  1174. if (evsel->handler.func != NULL) {
  1175. tracepoint_handler f = evsel->handler.func;
  1176. err = f(tool, evsel, sample, machine);
  1177. }
  1178. return err;
  1179. }
  1180. static int perf_sched__read_events(struct perf_sched *sched, bool destroy,
  1181. struct perf_session **psession)
  1182. {
  1183. const struct perf_evsel_str_handler handlers[] = {
  1184. { "sched:sched_switch", process_sched_switch_event, },
  1185. { "sched:sched_stat_runtime", process_sched_runtime_event, },
  1186. { "sched:sched_wakeup", process_sched_wakeup_event, },
  1187. { "sched:sched_wakeup_new", process_sched_wakeup_event, },
  1188. { "sched:sched_process_fork", process_sched_fork_event, },
  1189. { "sched:sched_process_exit", process_sched_exit_event, },
  1190. { "sched:sched_migrate_task", process_sched_migrate_task_event, },
  1191. };
  1192. struct perf_session *session;
  1193. session = perf_session__new(sched->input_name, O_RDONLY, 0, false, &sched->tool);
  1194. if (session == NULL) {
  1195. pr_debug("No Memory for session\n");
  1196. return -1;
  1197. }
  1198. if (perf_session__set_tracepoints_handlers(session, handlers))
  1199. goto out_delete;
  1200. if (perf_session__has_traces(session, "record -R")) {
  1201. int err = perf_session__process_events(session, &sched->tool);
  1202. if (err) {
  1203. pr_err("Failed to process events, error %d", err);
  1204. goto out_delete;
  1205. }
  1206. sched->nr_events = session->hists.stats.nr_events[0];
  1207. sched->nr_lost_events = session->hists.stats.total_lost;
  1208. sched->nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
  1209. }
  1210. if (destroy)
  1211. perf_session__delete(session);
  1212. if (psession)
  1213. *psession = session;
  1214. return 0;
  1215. out_delete:
  1216. perf_session__delete(session);
  1217. return -1;
  1218. }
  1219. static void print_bad_events(struct perf_sched *sched)
  1220. {
  1221. if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
  1222. printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
  1223. (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
  1224. sched->nr_unordered_timestamps, sched->nr_timestamps);
  1225. }
  1226. if (sched->nr_lost_events && sched->nr_events) {
  1227. printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
  1228. (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
  1229. sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
  1230. }
  1231. if (sched->nr_state_machine_bugs && sched->nr_timestamps) {
  1232. printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
  1233. (double)sched->nr_state_machine_bugs/(double)sched->nr_timestamps*100.0,
  1234. sched->nr_state_machine_bugs, sched->nr_timestamps);
  1235. if (sched->nr_lost_events)
  1236. printf(" (due to lost events?)");
  1237. printf("\n");
  1238. }
  1239. if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
  1240. printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
  1241. (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
  1242. sched->nr_context_switch_bugs, sched->nr_timestamps);
  1243. if (sched->nr_lost_events)
  1244. printf(" (due to lost events?)");
  1245. printf("\n");
  1246. }
  1247. }
  1248. static int perf_sched__lat(struct perf_sched *sched)
  1249. {
  1250. struct rb_node *next;
  1251. struct perf_session *session;
  1252. setup_pager();
  1253. if (perf_sched__read_events(sched, false, &session))
  1254. return -1;
  1255. perf_sched__sort_lat(sched);
  1256. printf("\n ---------------------------------------------------------------------------------------------------------------\n");
  1257. printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
  1258. printf(" ---------------------------------------------------------------------------------------------------------------\n");
  1259. next = rb_first(&sched->sorted_atom_root);
  1260. while (next) {
  1261. struct work_atoms *work_list;
  1262. work_list = rb_entry(next, struct work_atoms, node);
  1263. output_lat_thread(sched, work_list);
  1264. next = rb_next(next);
  1265. }
  1266. printf(" -----------------------------------------------------------------------------------------\n");
  1267. printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
  1268. (double)sched->all_runtime / 1e6, sched->all_count);
  1269. printf(" ---------------------------------------------------\n");
  1270. print_bad_events(sched);
  1271. printf("\n");
  1272. perf_session__delete(session);
  1273. return 0;
  1274. }
  1275. static int perf_sched__map(struct perf_sched *sched)
  1276. {
  1277. sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
  1278. setup_pager();
  1279. if (perf_sched__read_events(sched, true, NULL))
  1280. return -1;
  1281. print_bad_events(sched);
  1282. return 0;
  1283. }
  1284. static int perf_sched__replay(struct perf_sched *sched)
  1285. {
  1286. unsigned long i;
  1287. calibrate_run_measurement_overhead(sched);
  1288. calibrate_sleep_measurement_overhead(sched);
  1289. test_calibrations(sched);
  1290. if (perf_sched__read_events(sched, true, NULL))
  1291. return -1;
  1292. printf("nr_run_events: %ld\n", sched->nr_run_events);
  1293. printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
  1294. printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
  1295. if (sched->targetless_wakeups)
  1296. printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
  1297. if (sched->multitarget_wakeups)
  1298. printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
  1299. if (sched->nr_run_events_optimized)
  1300. printf("run atoms optimized: %ld\n",
  1301. sched->nr_run_events_optimized);
  1302. print_task_traces(sched);
  1303. add_cross_task_wakeups(sched);
  1304. create_tasks(sched);
  1305. printf("------------------------------------------------------------\n");
  1306. for (i = 0; i < sched->replay_repeat; i++)
  1307. run_one_test(sched);
  1308. return 0;
  1309. }
  1310. static void setup_sorting(struct perf_sched *sched, const struct option *options,
  1311. const char * const usage_msg[])
  1312. {
  1313. char *tmp, *tok, *str = strdup(sched->sort_order);
  1314. for (tok = strtok_r(str, ", ", &tmp);
  1315. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1316. if (sort_dimension__add(tok, &sched->sort_list) < 0) {
  1317. error("Unknown --sort key: `%s'", tok);
  1318. usage_with_options(usage_msg, options);
  1319. }
  1320. }
  1321. free(str);
  1322. sort_dimension__add("pid", &sched->cmp_pid);
  1323. }
  1324. static int __cmd_record(int argc, const char **argv)
  1325. {
  1326. unsigned int rec_argc, i, j;
  1327. const char **rec_argv;
  1328. const char * const record_args[] = {
  1329. "record",
  1330. "-a",
  1331. "-R",
  1332. "-f",
  1333. "-m", "1024",
  1334. "-c", "1",
  1335. "-e", "sched:sched_switch",
  1336. "-e", "sched:sched_stat_wait",
  1337. "-e", "sched:sched_stat_sleep",
  1338. "-e", "sched:sched_stat_iowait",
  1339. "-e", "sched:sched_stat_runtime",
  1340. "-e", "sched:sched_process_exit",
  1341. "-e", "sched:sched_process_fork",
  1342. "-e", "sched:sched_wakeup",
  1343. "-e", "sched:sched_migrate_task",
  1344. };
  1345. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  1346. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1347. if (rec_argv == NULL)
  1348. return -ENOMEM;
  1349. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1350. rec_argv[i] = strdup(record_args[i]);
  1351. for (j = 1; j < (unsigned int)argc; j++, i++)
  1352. rec_argv[i] = argv[j];
  1353. BUG_ON(i != rec_argc);
  1354. return cmd_record(i, rec_argv, NULL);
  1355. }
  1356. int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
  1357. {
  1358. const char default_sort_order[] = "avg, max, switch, runtime";
  1359. struct perf_sched sched = {
  1360. .tool = {
  1361. .sample = perf_sched__process_tracepoint_sample,
  1362. .comm = perf_event__process_comm,
  1363. .lost = perf_event__process_lost,
  1364. .fork = perf_event__process_task,
  1365. .ordered_samples = true,
  1366. },
  1367. .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
  1368. .sort_list = LIST_HEAD_INIT(sched.sort_list),
  1369. .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
  1370. .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
  1371. .curr_pid = { [0 ... MAX_CPUS - 1] = -1 },
  1372. .sort_order = default_sort_order,
  1373. .replay_repeat = 10,
  1374. .profile_cpu = -1,
  1375. .next_shortname1 = 'A',
  1376. .next_shortname2 = '0',
  1377. };
  1378. const struct option latency_options[] = {
  1379. OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
  1380. "sort by key(s): runtime, switch, avg, max"),
  1381. OPT_INCR('v', "verbose", &verbose,
  1382. "be more verbose (show symbol address, etc)"),
  1383. OPT_INTEGER('C', "CPU", &sched.profile_cpu,
  1384. "CPU to profile on"),
  1385. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1386. "dump raw trace in ASCII"),
  1387. OPT_END()
  1388. };
  1389. const struct option replay_options[] = {
  1390. OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
  1391. "repeat the workload replay N times (-1: infinite)"),
  1392. OPT_INCR('v', "verbose", &verbose,
  1393. "be more verbose (show symbol address, etc)"),
  1394. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1395. "dump raw trace in ASCII"),
  1396. OPT_END()
  1397. };
  1398. const struct option sched_options[] = {
  1399. OPT_STRING('i', "input", &sched.input_name, "file",
  1400. "input file name"),
  1401. OPT_INCR('v', "verbose", &verbose,
  1402. "be more verbose (show symbol address, etc)"),
  1403. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1404. "dump raw trace in ASCII"),
  1405. OPT_END()
  1406. };
  1407. const char * const latency_usage[] = {
  1408. "perf sched latency [<options>]",
  1409. NULL
  1410. };
  1411. const char * const replay_usage[] = {
  1412. "perf sched replay [<options>]",
  1413. NULL
  1414. };
  1415. const char * const sched_usage[] = {
  1416. "perf sched [<options>] {record|latency|map|replay|script}",
  1417. NULL
  1418. };
  1419. struct trace_sched_handler lat_ops = {
  1420. .wakeup_event = latency_wakeup_event,
  1421. .switch_event = latency_switch_event,
  1422. .runtime_event = latency_runtime_event,
  1423. .fork_event = latency_fork_event,
  1424. .migrate_task_event = latency_migrate_task_event,
  1425. };
  1426. struct trace_sched_handler map_ops = {
  1427. .switch_event = map_switch_event,
  1428. };
  1429. struct trace_sched_handler replay_ops = {
  1430. .wakeup_event = replay_wakeup_event,
  1431. .switch_event = replay_switch_event,
  1432. .fork_event = replay_fork_event,
  1433. };
  1434. argc = parse_options(argc, argv, sched_options, sched_usage,
  1435. PARSE_OPT_STOP_AT_NON_OPTION);
  1436. if (!argc)
  1437. usage_with_options(sched_usage, sched_options);
  1438. /*
  1439. * Aliased to 'perf script' for now:
  1440. */
  1441. if (!strcmp(argv[0], "script"))
  1442. return cmd_script(argc, argv, prefix);
  1443. symbol__init();
  1444. if (!strncmp(argv[0], "rec", 3)) {
  1445. return __cmd_record(argc, argv);
  1446. } else if (!strncmp(argv[0], "lat", 3)) {
  1447. sched.tp_handler = &lat_ops;
  1448. if (argc > 1) {
  1449. argc = parse_options(argc, argv, latency_options, latency_usage, 0);
  1450. if (argc)
  1451. usage_with_options(latency_usage, latency_options);
  1452. }
  1453. setup_sorting(&sched, latency_options, latency_usage);
  1454. return perf_sched__lat(&sched);
  1455. } else if (!strcmp(argv[0], "map")) {
  1456. sched.tp_handler = &map_ops;
  1457. setup_sorting(&sched, latency_options, latency_usage);
  1458. return perf_sched__map(&sched);
  1459. } else if (!strncmp(argv[0], "rep", 3)) {
  1460. sched.tp_handler = &replay_ops;
  1461. if (argc) {
  1462. argc = parse_options(argc, argv, replay_options, replay_usage, 0);
  1463. if (argc)
  1464. usage_with_options(replay_usage, replay_options);
  1465. }
  1466. return perf_sched__replay(&sched);
  1467. } else {
  1468. usage_with_options(sched_usage, sched_options);
  1469. }
  1470. return 0;
  1471. }