builtin-sched.c 44 KB

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