builtin-sched.c 52 KB

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