builtin-sched.c 44 KB

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