builtin-sched.c 43 KB

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