builtin-sched.c 44 KB

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