builtin-sched.c 44 KB

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