builtin-sched.c 44 KB

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