builtin-sched.c 52 KB

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