builtin-sched.c 43 KB

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