builtin-sched.c 43 KB

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