builtin-sched.c 50 KB

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