builtin-sched.c 43 KB

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