builtin-sched.c 44 KB

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