builtin-sched.c 44 KB

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