builtin-sched.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347
  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/cloexec.h"
  13. #include "util/thread_map.h"
  14. #include "util/color.h"
  15. #include "util/stat.h"
  16. #include "util/callchain.h"
  17. #include "util/time-utils.h"
  18. #include <subcmd/parse-options.h>
  19. #include "util/trace-event.h"
  20. #include "util/debug.h"
  21. #include <linux/log2.h>
  22. #include <sys/prctl.h>
  23. #include <sys/resource.h>
  24. #include <semaphore.h>
  25. #include <pthread.h>
  26. #include <math.h>
  27. #include <api/fs/fs.h>
  28. #include <linux/time64.h>
  29. #define PR_SET_NAME 15 /* Set process name */
  30. #define MAX_CPUS 4096
  31. #define COMM_LEN 20
  32. #define SYM_LEN 129
  33. #define MAX_PID 1024000
  34. struct sched_atom;
  35. struct task_desc {
  36. unsigned long nr;
  37. unsigned long pid;
  38. char comm[COMM_LEN];
  39. unsigned long nr_events;
  40. unsigned long curr_event;
  41. struct sched_atom **atoms;
  42. pthread_t thread;
  43. sem_t sleep_sem;
  44. sem_t ready_for_work;
  45. sem_t work_done_sem;
  46. u64 cpu_usage;
  47. };
  48. enum sched_event_type {
  49. SCHED_EVENT_RUN,
  50. SCHED_EVENT_SLEEP,
  51. SCHED_EVENT_WAKEUP,
  52. SCHED_EVENT_MIGRATION,
  53. };
  54. struct sched_atom {
  55. enum sched_event_type type;
  56. int specific_wait;
  57. u64 timestamp;
  58. u64 duration;
  59. unsigned long nr;
  60. sem_t *wait_sem;
  61. struct task_desc *wakee;
  62. };
  63. #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
  64. enum thread_state {
  65. THREAD_SLEEPING = 0,
  66. THREAD_WAIT_CPU,
  67. THREAD_SCHED_IN,
  68. THREAD_IGNORE
  69. };
  70. struct work_atom {
  71. struct list_head list;
  72. enum thread_state state;
  73. u64 sched_out_time;
  74. u64 wake_up_time;
  75. u64 sched_in_time;
  76. u64 runtime;
  77. };
  78. struct work_atoms {
  79. struct list_head work_list;
  80. struct thread *thread;
  81. struct rb_node node;
  82. u64 max_lat;
  83. u64 max_lat_at;
  84. u64 total_lat;
  85. u64 nb_atoms;
  86. u64 total_runtime;
  87. int num_merged;
  88. };
  89. typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
  90. struct perf_sched;
  91. struct trace_sched_handler {
  92. int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  93. struct perf_sample *sample, struct machine *machine);
  94. int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  95. struct perf_sample *sample, struct machine *machine);
  96. int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
  97. struct perf_sample *sample, struct machine *machine);
  98. /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
  99. int (*fork_event)(struct perf_sched *sched, union perf_event *event,
  100. struct machine *machine);
  101. int (*migrate_task_event)(struct perf_sched *sched,
  102. struct perf_evsel *evsel,
  103. struct perf_sample *sample,
  104. struct machine *machine);
  105. };
  106. #define COLOR_PIDS PERF_COLOR_BLUE
  107. #define COLOR_CPUS PERF_COLOR_BG_RED
  108. struct perf_sched_map {
  109. DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
  110. int *comp_cpus;
  111. bool comp;
  112. struct thread_map *color_pids;
  113. const char *color_pids_str;
  114. struct cpu_map *color_cpus;
  115. const char *color_cpus_str;
  116. struct cpu_map *cpus;
  117. const char *cpus_str;
  118. };
  119. struct perf_sched {
  120. struct perf_tool tool;
  121. const char *sort_order;
  122. unsigned long nr_tasks;
  123. struct task_desc **pid_to_task;
  124. struct task_desc **tasks;
  125. const struct trace_sched_handler *tp_handler;
  126. pthread_mutex_t start_work_mutex;
  127. pthread_mutex_t work_done_wait_mutex;
  128. int profile_cpu;
  129. /*
  130. * Track the current task - that way we can know whether there's any
  131. * weird events, such as a task being switched away that is not current.
  132. */
  133. int max_cpu;
  134. u32 curr_pid[MAX_CPUS];
  135. struct thread *curr_thread[MAX_CPUS];
  136. char next_shortname1;
  137. char next_shortname2;
  138. unsigned int replay_repeat;
  139. unsigned long nr_run_events;
  140. unsigned long nr_sleep_events;
  141. unsigned long nr_wakeup_events;
  142. unsigned long nr_sleep_corrections;
  143. unsigned long nr_run_events_optimized;
  144. unsigned long targetless_wakeups;
  145. unsigned long multitarget_wakeups;
  146. unsigned long nr_runs;
  147. unsigned long nr_timestamps;
  148. unsigned long nr_unordered_timestamps;
  149. unsigned long nr_context_switch_bugs;
  150. unsigned long nr_events;
  151. unsigned long nr_lost_chunks;
  152. unsigned long nr_lost_events;
  153. u64 run_measurement_overhead;
  154. u64 sleep_measurement_overhead;
  155. u64 start_time;
  156. u64 cpu_usage;
  157. u64 runavg_cpu_usage;
  158. u64 parent_cpu_usage;
  159. u64 runavg_parent_cpu_usage;
  160. u64 sum_runtime;
  161. u64 sum_fluct;
  162. u64 run_avg;
  163. u64 all_runtime;
  164. u64 all_count;
  165. u64 cpu_last_switched[MAX_CPUS];
  166. struct rb_root atom_root, sorted_atom_root, merged_atom_root;
  167. struct list_head sort_list, cmp_pid;
  168. bool force;
  169. bool skip_merge;
  170. struct perf_sched_map map;
  171. /* options for timehist command */
  172. bool summary;
  173. bool summary_only;
  174. bool idle_hist;
  175. bool show_callchain;
  176. unsigned int max_stack;
  177. bool show_cpu_visual;
  178. bool show_wakeups;
  179. bool show_migrations;
  180. u64 skipped_samples;
  181. const char *time_str;
  182. struct perf_time_interval ptime;
  183. struct perf_time_interval hist_time;
  184. };
  185. /* per thread run time data */
  186. struct thread_runtime {
  187. u64 last_time; /* time of previous sched in/out event */
  188. u64 dt_run; /* run time */
  189. u64 dt_wait; /* time between CPU access (off cpu) */
  190. u64 dt_delay; /* time between wakeup and sched-in */
  191. u64 ready_to_run; /* time of wakeup */
  192. struct stats run_stats;
  193. u64 total_run_time;
  194. u64 migrations;
  195. };
  196. /* per event run time data */
  197. struct evsel_runtime {
  198. u64 *last_time; /* time this event was last seen per cpu */
  199. u32 ncpu; /* highest cpu slot allocated */
  200. };
  201. /* per cpu idle time data */
  202. struct idle_thread_runtime {
  203. struct thread_runtime tr;
  204. struct thread *last_thread;
  205. struct rb_root sorted_root;
  206. struct callchain_root callchain;
  207. struct callchain_cursor cursor;
  208. };
  209. /* track idle times per cpu */
  210. static struct thread **idle_threads;
  211. static int idle_max_cpu;
  212. static char idle_comm[] = "<idle>";
  213. static u64 get_nsecs(void)
  214. {
  215. struct timespec ts;
  216. clock_gettime(CLOCK_MONOTONIC, &ts);
  217. return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
  218. }
  219. static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
  220. {
  221. u64 T0 = get_nsecs(), T1;
  222. do {
  223. T1 = get_nsecs();
  224. } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
  225. }
  226. static void sleep_nsecs(u64 nsecs)
  227. {
  228. struct timespec ts;
  229. ts.tv_nsec = nsecs % 999999999;
  230. ts.tv_sec = nsecs / 999999999;
  231. nanosleep(&ts, NULL);
  232. }
  233. static void calibrate_run_measurement_overhead(struct perf_sched *sched)
  234. {
  235. u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
  236. int i;
  237. for (i = 0; i < 10; i++) {
  238. T0 = get_nsecs();
  239. burn_nsecs(sched, 0);
  240. T1 = get_nsecs();
  241. delta = T1-T0;
  242. min_delta = min(min_delta, delta);
  243. }
  244. sched->run_measurement_overhead = min_delta;
  245. printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
  246. }
  247. static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
  248. {
  249. u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
  250. int i;
  251. for (i = 0; i < 10; i++) {
  252. T0 = get_nsecs();
  253. sleep_nsecs(10000);
  254. T1 = get_nsecs();
  255. delta = T1-T0;
  256. min_delta = min(min_delta, delta);
  257. }
  258. min_delta -= 10000;
  259. sched->sleep_measurement_overhead = min_delta;
  260. printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
  261. }
  262. static struct sched_atom *
  263. get_new_event(struct task_desc *task, u64 timestamp)
  264. {
  265. struct sched_atom *event = zalloc(sizeof(*event));
  266. unsigned long idx = task->nr_events;
  267. size_t size;
  268. event->timestamp = timestamp;
  269. event->nr = idx;
  270. task->nr_events++;
  271. size = sizeof(struct sched_atom *) * task->nr_events;
  272. task->atoms = realloc(task->atoms, size);
  273. BUG_ON(!task->atoms);
  274. task->atoms[idx] = event;
  275. return event;
  276. }
  277. static struct sched_atom *last_event(struct task_desc *task)
  278. {
  279. if (!task->nr_events)
  280. return NULL;
  281. return task->atoms[task->nr_events - 1];
  282. }
  283. static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
  284. u64 timestamp, u64 duration)
  285. {
  286. struct sched_atom *event, *curr_event = last_event(task);
  287. /*
  288. * optimize an existing RUN event by merging this one
  289. * to it:
  290. */
  291. if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
  292. sched->nr_run_events_optimized++;
  293. curr_event->duration += duration;
  294. return;
  295. }
  296. event = get_new_event(task, timestamp);
  297. event->type = SCHED_EVENT_RUN;
  298. event->duration = duration;
  299. sched->nr_run_events++;
  300. }
  301. static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
  302. u64 timestamp, struct task_desc *wakee)
  303. {
  304. struct sched_atom *event, *wakee_event;
  305. event = get_new_event(task, timestamp);
  306. event->type = SCHED_EVENT_WAKEUP;
  307. event->wakee = wakee;
  308. wakee_event = last_event(wakee);
  309. if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
  310. sched->targetless_wakeups++;
  311. return;
  312. }
  313. if (wakee_event->wait_sem) {
  314. sched->multitarget_wakeups++;
  315. return;
  316. }
  317. wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
  318. sem_init(wakee_event->wait_sem, 0, 0);
  319. wakee_event->specific_wait = 1;
  320. event->wait_sem = wakee_event->wait_sem;
  321. sched->nr_wakeup_events++;
  322. }
  323. static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
  324. u64 timestamp, u64 task_state __maybe_unused)
  325. {
  326. struct sched_atom *event = get_new_event(task, timestamp);
  327. event->type = SCHED_EVENT_SLEEP;
  328. sched->nr_sleep_events++;
  329. }
  330. static struct task_desc *register_pid(struct perf_sched *sched,
  331. unsigned long pid, const char *comm)
  332. {
  333. struct task_desc *task;
  334. static int pid_max;
  335. if (sched->pid_to_task == NULL) {
  336. if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
  337. pid_max = MAX_PID;
  338. BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
  339. }
  340. if (pid >= (unsigned long)pid_max) {
  341. BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
  342. sizeof(struct task_desc *))) == NULL);
  343. while (pid >= (unsigned long)pid_max)
  344. sched->pid_to_task[pid_max++] = NULL;
  345. }
  346. task = sched->pid_to_task[pid];
  347. if (task)
  348. return task;
  349. task = zalloc(sizeof(*task));
  350. task->pid = pid;
  351. task->nr = sched->nr_tasks;
  352. strcpy(task->comm, comm);
  353. /*
  354. * every task starts in sleeping state - this gets ignored
  355. * if there's no wakeup pointing to this sleep state:
  356. */
  357. add_sched_event_sleep(sched, task, 0, 0);
  358. sched->pid_to_task[pid] = task;
  359. sched->nr_tasks++;
  360. sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
  361. BUG_ON(!sched->tasks);
  362. sched->tasks[task->nr] = task;
  363. if (verbose)
  364. printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
  365. return task;
  366. }
  367. static void print_task_traces(struct perf_sched *sched)
  368. {
  369. struct task_desc *task;
  370. unsigned long i;
  371. for (i = 0; i < sched->nr_tasks; i++) {
  372. task = sched->tasks[i];
  373. printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
  374. task->nr, task->comm, task->pid, task->nr_events);
  375. }
  376. }
  377. static void add_cross_task_wakeups(struct perf_sched *sched)
  378. {
  379. struct task_desc *task1, *task2;
  380. unsigned long i, j;
  381. for (i = 0; i < sched->nr_tasks; i++) {
  382. task1 = sched->tasks[i];
  383. j = i + 1;
  384. if (j == sched->nr_tasks)
  385. j = 0;
  386. task2 = sched->tasks[j];
  387. add_sched_event_wakeup(sched, task1, 0, task2);
  388. }
  389. }
  390. static void perf_sched__process_event(struct perf_sched *sched,
  391. struct sched_atom *atom)
  392. {
  393. int ret = 0;
  394. switch (atom->type) {
  395. case SCHED_EVENT_RUN:
  396. burn_nsecs(sched, atom->duration);
  397. break;
  398. case SCHED_EVENT_SLEEP:
  399. if (atom->wait_sem)
  400. ret = sem_wait(atom->wait_sem);
  401. BUG_ON(ret);
  402. break;
  403. case SCHED_EVENT_WAKEUP:
  404. if (atom->wait_sem)
  405. ret = sem_post(atom->wait_sem);
  406. BUG_ON(ret);
  407. break;
  408. case SCHED_EVENT_MIGRATION:
  409. break;
  410. default:
  411. BUG_ON(1);
  412. }
  413. }
  414. static u64 get_cpu_usage_nsec_parent(void)
  415. {
  416. struct rusage ru;
  417. u64 sum;
  418. int err;
  419. err = getrusage(RUSAGE_SELF, &ru);
  420. BUG_ON(err);
  421. sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
  422. sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
  423. return sum;
  424. }
  425. static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
  426. {
  427. struct perf_event_attr attr;
  428. char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
  429. int fd;
  430. struct rlimit limit;
  431. bool need_privilege = false;
  432. memset(&attr, 0, sizeof(attr));
  433. attr.type = PERF_TYPE_SOFTWARE;
  434. attr.config = PERF_COUNT_SW_TASK_CLOCK;
  435. force_again:
  436. fd = sys_perf_event_open(&attr, 0, -1, -1,
  437. perf_event_open_cloexec_flag());
  438. if (fd < 0) {
  439. if (errno == EMFILE) {
  440. if (sched->force) {
  441. BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
  442. limit.rlim_cur += sched->nr_tasks - cur_task;
  443. if (limit.rlim_cur > limit.rlim_max) {
  444. limit.rlim_max = limit.rlim_cur;
  445. need_privilege = true;
  446. }
  447. if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
  448. if (need_privilege && errno == EPERM)
  449. strcpy(info, "Need privilege\n");
  450. } else
  451. goto force_again;
  452. } else
  453. strcpy(info, "Have a try with -f option\n");
  454. }
  455. pr_err("Error: sys_perf_event_open() syscall returned "
  456. "with %d (%s)\n%s", fd,
  457. str_error_r(errno, sbuf, sizeof(sbuf)), info);
  458. exit(EXIT_FAILURE);
  459. }
  460. return fd;
  461. }
  462. static u64 get_cpu_usage_nsec_self(int fd)
  463. {
  464. u64 runtime;
  465. int ret;
  466. ret = read(fd, &runtime, sizeof(runtime));
  467. BUG_ON(ret != sizeof(runtime));
  468. return runtime;
  469. }
  470. struct sched_thread_parms {
  471. struct task_desc *task;
  472. struct perf_sched *sched;
  473. int fd;
  474. };
  475. static void *thread_func(void *ctx)
  476. {
  477. struct sched_thread_parms *parms = ctx;
  478. struct task_desc *this_task = parms->task;
  479. struct perf_sched *sched = parms->sched;
  480. u64 cpu_usage_0, cpu_usage_1;
  481. unsigned long i, ret;
  482. char comm2[22];
  483. int fd = parms->fd;
  484. zfree(&parms);
  485. sprintf(comm2, ":%s", this_task->comm);
  486. prctl(PR_SET_NAME, comm2);
  487. if (fd < 0)
  488. return NULL;
  489. again:
  490. ret = sem_post(&this_task->ready_for_work);
  491. BUG_ON(ret);
  492. ret = pthread_mutex_lock(&sched->start_work_mutex);
  493. BUG_ON(ret);
  494. ret = pthread_mutex_unlock(&sched->start_work_mutex);
  495. BUG_ON(ret);
  496. cpu_usage_0 = get_cpu_usage_nsec_self(fd);
  497. for (i = 0; i < this_task->nr_events; i++) {
  498. this_task->curr_event = i;
  499. perf_sched__process_event(sched, this_task->atoms[i]);
  500. }
  501. cpu_usage_1 = get_cpu_usage_nsec_self(fd);
  502. this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
  503. ret = sem_post(&this_task->work_done_sem);
  504. BUG_ON(ret);
  505. ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
  506. BUG_ON(ret);
  507. ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
  508. BUG_ON(ret);
  509. goto again;
  510. }
  511. static void create_tasks(struct perf_sched *sched)
  512. {
  513. struct task_desc *task;
  514. pthread_attr_t attr;
  515. unsigned long i;
  516. int err;
  517. err = pthread_attr_init(&attr);
  518. BUG_ON(err);
  519. err = pthread_attr_setstacksize(&attr,
  520. (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
  521. BUG_ON(err);
  522. err = pthread_mutex_lock(&sched->start_work_mutex);
  523. BUG_ON(err);
  524. err = pthread_mutex_lock(&sched->work_done_wait_mutex);
  525. BUG_ON(err);
  526. for (i = 0; i < sched->nr_tasks; i++) {
  527. struct sched_thread_parms *parms = malloc(sizeof(*parms));
  528. BUG_ON(parms == NULL);
  529. parms->task = task = sched->tasks[i];
  530. parms->sched = sched;
  531. parms->fd = self_open_counters(sched, i);
  532. sem_init(&task->sleep_sem, 0, 0);
  533. sem_init(&task->ready_for_work, 0, 0);
  534. sem_init(&task->work_done_sem, 0, 0);
  535. task->curr_event = 0;
  536. err = pthread_create(&task->thread, &attr, thread_func, parms);
  537. BUG_ON(err);
  538. }
  539. }
  540. static void wait_for_tasks(struct perf_sched *sched)
  541. {
  542. u64 cpu_usage_0, cpu_usage_1;
  543. struct task_desc *task;
  544. unsigned long i, ret;
  545. sched->start_time = get_nsecs();
  546. sched->cpu_usage = 0;
  547. pthread_mutex_unlock(&sched->work_done_wait_mutex);
  548. for (i = 0; i < sched->nr_tasks; i++) {
  549. task = sched->tasks[i];
  550. ret = sem_wait(&task->ready_for_work);
  551. BUG_ON(ret);
  552. sem_init(&task->ready_for_work, 0, 0);
  553. }
  554. ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
  555. BUG_ON(ret);
  556. cpu_usage_0 = get_cpu_usage_nsec_parent();
  557. pthread_mutex_unlock(&sched->start_work_mutex);
  558. for (i = 0; i < sched->nr_tasks; i++) {
  559. task = sched->tasks[i];
  560. ret = sem_wait(&task->work_done_sem);
  561. BUG_ON(ret);
  562. sem_init(&task->work_done_sem, 0, 0);
  563. sched->cpu_usage += task->cpu_usage;
  564. task->cpu_usage = 0;
  565. }
  566. cpu_usage_1 = get_cpu_usage_nsec_parent();
  567. if (!sched->runavg_cpu_usage)
  568. sched->runavg_cpu_usage = sched->cpu_usage;
  569. sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat;
  570. sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
  571. if (!sched->runavg_parent_cpu_usage)
  572. sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
  573. sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
  574. sched->parent_cpu_usage)/sched->replay_repeat;
  575. ret = pthread_mutex_lock(&sched->start_work_mutex);
  576. BUG_ON(ret);
  577. for (i = 0; i < sched->nr_tasks; i++) {
  578. task = sched->tasks[i];
  579. sem_init(&task->sleep_sem, 0, 0);
  580. task->curr_event = 0;
  581. }
  582. }
  583. static void run_one_test(struct perf_sched *sched)
  584. {
  585. u64 T0, T1, delta, avg_delta, fluct;
  586. T0 = get_nsecs();
  587. wait_for_tasks(sched);
  588. T1 = get_nsecs();
  589. delta = T1 - T0;
  590. sched->sum_runtime += delta;
  591. sched->nr_runs++;
  592. avg_delta = sched->sum_runtime / sched->nr_runs;
  593. if (delta < avg_delta)
  594. fluct = avg_delta - delta;
  595. else
  596. fluct = delta - avg_delta;
  597. sched->sum_fluct += fluct;
  598. if (!sched->run_avg)
  599. sched->run_avg = delta;
  600. sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
  601. printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
  602. printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
  603. printf("cpu: %0.2f / %0.2f",
  604. (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
  605. #if 0
  606. /*
  607. * rusage statistics done by the parent, these are less
  608. * accurate than the sched->sum_exec_runtime based statistics:
  609. */
  610. printf(" [%0.2f / %0.2f]",
  611. (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
  612. (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
  613. #endif
  614. printf("\n");
  615. if (sched->nr_sleep_corrections)
  616. printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
  617. sched->nr_sleep_corrections = 0;
  618. }
  619. static void test_calibrations(struct perf_sched *sched)
  620. {
  621. u64 T0, T1;
  622. T0 = get_nsecs();
  623. burn_nsecs(sched, NSEC_PER_MSEC);
  624. T1 = get_nsecs();
  625. printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
  626. T0 = get_nsecs();
  627. sleep_nsecs(NSEC_PER_MSEC);
  628. T1 = get_nsecs();
  629. printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
  630. }
  631. static int
  632. replay_wakeup_event(struct perf_sched *sched,
  633. struct perf_evsel *evsel, struct perf_sample *sample,
  634. struct machine *machine __maybe_unused)
  635. {
  636. const char *comm = perf_evsel__strval(evsel, sample, "comm");
  637. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  638. struct task_desc *waker, *wakee;
  639. if (verbose) {
  640. printf("sched_wakeup event %p\n", evsel);
  641. printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
  642. }
  643. waker = register_pid(sched, sample->tid, "<unknown>");
  644. wakee = register_pid(sched, pid, comm);
  645. add_sched_event_wakeup(sched, waker, sample->time, wakee);
  646. return 0;
  647. }
  648. static int replay_switch_event(struct perf_sched *sched,
  649. struct perf_evsel *evsel,
  650. struct perf_sample *sample,
  651. struct machine *machine __maybe_unused)
  652. {
  653. const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
  654. *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
  655. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  656. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  657. const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  658. struct task_desc *prev, __maybe_unused *next;
  659. u64 timestamp0, timestamp = sample->time;
  660. int cpu = sample->cpu;
  661. s64 delta;
  662. if (verbose)
  663. printf("sched_switch event %p\n", evsel);
  664. if (cpu >= MAX_CPUS || cpu < 0)
  665. return 0;
  666. timestamp0 = sched->cpu_last_switched[cpu];
  667. if (timestamp0)
  668. delta = timestamp - timestamp0;
  669. else
  670. delta = 0;
  671. if (delta < 0) {
  672. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  673. return -1;
  674. }
  675. pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
  676. prev_comm, prev_pid, next_comm, next_pid, delta);
  677. prev = register_pid(sched, prev_pid, prev_comm);
  678. next = register_pid(sched, next_pid, next_comm);
  679. sched->cpu_last_switched[cpu] = timestamp;
  680. add_sched_event_run(sched, prev, timestamp, delta);
  681. add_sched_event_sleep(sched, prev, timestamp, prev_state);
  682. return 0;
  683. }
  684. static int replay_fork_event(struct perf_sched *sched,
  685. union perf_event *event,
  686. struct machine *machine)
  687. {
  688. struct thread *child, *parent;
  689. child = machine__findnew_thread(machine, event->fork.pid,
  690. event->fork.tid);
  691. parent = machine__findnew_thread(machine, event->fork.ppid,
  692. event->fork.ptid);
  693. if (child == NULL || parent == NULL) {
  694. pr_debug("thread does not exist on fork event: child %p, parent %p\n",
  695. child, parent);
  696. goto out_put;
  697. }
  698. if (verbose) {
  699. printf("fork event\n");
  700. printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
  701. printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
  702. }
  703. register_pid(sched, parent->tid, thread__comm_str(parent));
  704. register_pid(sched, child->tid, thread__comm_str(child));
  705. out_put:
  706. thread__put(child);
  707. thread__put(parent);
  708. return 0;
  709. }
  710. struct sort_dimension {
  711. const char *name;
  712. sort_fn_t cmp;
  713. struct list_head list;
  714. };
  715. static int
  716. thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
  717. {
  718. struct sort_dimension *sort;
  719. int ret = 0;
  720. BUG_ON(list_empty(list));
  721. list_for_each_entry(sort, list, list) {
  722. ret = sort->cmp(l, r);
  723. if (ret)
  724. return ret;
  725. }
  726. return ret;
  727. }
  728. static struct work_atoms *
  729. thread_atoms_search(struct rb_root *root, struct thread *thread,
  730. struct list_head *sort_list)
  731. {
  732. struct rb_node *node = root->rb_node;
  733. struct work_atoms key = { .thread = thread };
  734. while (node) {
  735. struct work_atoms *atoms;
  736. int cmp;
  737. atoms = container_of(node, struct work_atoms, node);
  738. cmp = thread_lat_cmp(sort_list, &key, atoms);
  739. if (cmp > 0)
  740. node = node->rb_left;
  741. else if (cmp < 0)
  742. node = node->rb_right;
  743. else {
  744. BUG_ON(thread != atoms->thread);
  745. return atoms;
  746. }
  747. }
  748. return NULL;
  749. }
  750. static void
  751. __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
  752. struct list_head *sort_list)
  753. {
  754. struct rb_node **new = &(root->rb_node), *parent = NULL;
  755. while (*new) {
  756. struct work_atoms *this;
  757. int cmp;
  758. this = container_of(*new, struct work_atoms, node);
  759. parent = *new;
  760. cmp = thread_lat_cmp(sort_list, data, this);
  761. if (cmp > 0)
  762. new = &((*new)->rb_left);
  763. else
  764. new = &((*new)->rb_right);
  765. }
  766. rb_link_node(&data->node, parent, new);
  767. rb_insert_color(&data->node, root);
  768. }
  769. static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
  770. {
  771. struct work_atoms *atoms = zalloc(sizeof(*atoms));
  772. if (!atoms) {
  773. pr_err("No memory at %s\n", __func__);
  774. return -1;
  775. }
  776. atoms->thread = thread__get(thread);
  777. INIT_LIST_HEAD(&atoms->work_list);
  778. __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
  779. return 0;
  780. }
  781. static char sched_out_state(u64 prev_state)
  782. {
  783. const char *str = TASK_STATE_TO_CHAR_STR;
  784. return str[prev_state];
  785. }
  786. static int
  787. add_sched_out_event(struct work_atoms *atoms,
  788. char run_state,
  789. u64 timestamp)
  790. {
  791. struct work_atom *atom = zalloc(sizeof(*atom));
  792. if (!atom) {
  793. pr_err("Non memory at %s", __func__);
  794. return -1;
  795. }
  796. atom->sched_out_time = timestamp;
  797. if (run_state == 'R') {
  798. atom->state = THREAD_WAIT_CPU;
  799. atom->wake_up_time = atom->sched_out_time;
  800. }
  801. list_add_tail(&atom->list, &atoms->work_list);
  802. return 0;
  803. }
  804. static void
  805. add_runtime_event(struct work_atoms *atoms, u64 delta,
  806. u64 timestamp __maybe_unused)
  807. {
  808. struct work_atom *atom;
  809. BUG_ON(list_empty(&atoms->work_list));
  810. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  811. atom->runtime += delta;
  812. atoms->total_runtime += delta;
  813. }
  814. static void
  815. add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
  816. {
  817. struct work_atom *atom;
  818. u64 delta;
  819. if (list_empty(&atoms->work_list))
  820. return;
  821. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  822. if (atom->state != THREAD_WAIT_CPU)
  823. return;
  824. if (timestamp < atom->wake_up_time) {
  825. atom->state = THREAD_IGNORE;
  826. return;
  827. }
  828. atom->state = THREAD_SCHED_IN;
  829. atom->sched_in_time = timestamp;
  830. delta = atom->sched_in_time - atom->wake_up_time;
  831. atoms->total_lat += delta;
  832. if (delta > atoms->max_lat) {
  833. atoms->max_lat = delta;
  834. atoms->max_lat_at = timestamp;
  835. }
  836. atoms->nb_atoms++;
  837. }
  838. static int latency_switch_event(struct perf_sched *sched,
  839. struct perf_evsel *evsel,
  840. struct perf_sample *sample,
  841. struct machine *machine)
  842. {
  843. const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  844. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  845. const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  846. struct work_atoms *out_events, *in_events;
  847. struct thread *sched_out, *sched_in;
  848. u64 timestamp0, timestamp = sample->time;
  849. int cpu = sample->cpu, err = -1;
  850. s64 delta;
  851. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  852. timestamp0 = sched->cpu_last_switched[cpu];
  853. sched->cpu_last_switched[cpu] = timestamp;
  854. if (timestamp0)
  855. delta = timestamp - timestamp0;
  856. else
  857. delta = 0;
  858. if (delta < 0) {
  859. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  860. return -1;
  861. }
  862. sched_out = machine__findnew_thread(machine, -1, prev_pid);
  863. sched_in = machine__findnew_thread(machine, -1, next_pid);
  864. if (sched_out == NULL || sched_in == NULL)
  865. goto out_put;
  866. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  867. if (!out_events) {
  868. if (thread_atoms_insert(sched, sched_out))
  869. goto out_put;
  870. out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
  871. if (!out_events) {
  872. pr_err("out-event: Internal tree error");
  873. goto out_put;
  874. }
  875. }
  876. if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
  877. return -1;
  878. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  879. if (!in_events) {
  880. if (thread_atoms_insert(sched, sched_in))
  881. goto out_put;
  882. in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
  883. if (!in_events) {
  884. pr_err("in-event: Internal tree error");
  885. goto out_put;
  886. }
  887. /*
  888. * Take came in we have not heard about yet,
  889. * add in an initial atom in runnable state:
  890. */
  891. if (add_sched_out_event(in_events, 'R', timestamp))
  892. goto out_put;
  893. }
  894. add_sched_in_event(in_events, timestamp);
  895. err = 0;
  896. out_put:
  897. thread__put(sched_out);
  898. thread__put(sched_in);
  899. return err;
  900. }
  901. static int latency_runtime_event(struct perf_sched *sched,
  902. struct perf_evsel *evsel,
  903. struct perf_sample *sample,
  904. struct machine *machine)
  905. {
  906. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  907. const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  908. struct thread *thread = machine__findnew_thread(machine, -1, pid);
  909. struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  910. u64 timestamp = sample->time;
  911. int cpu = sample->cpu, err = -1;
  912. if (thread == NULL)
  913. return -1;
  914. BUG_ON(cpu >= MAX_CPUS || cpu < 0);
  915. if (!atoms) {
  916. if (thread_atoms_insert(sched, thread))
  917. goto out_put;
  918. atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
  919. if (!atoms) {
  920. pr_err("in-event: Internal tree error");
  921. goto out_put;
  922. }
  923. if (add_sched_out_event(atoms, 'R', timestamp))
  924. goto out_put;
  925. }
  926. add_runtime_event(atoms, runtime, timestamp);
  927. err = 0;
  928. out_put:
  929. thread__put(thread);
  930. return err;
  931. }
  932. static int latency_wakeup_event(struct perf_sched *sched,
  933. struct perf_evsel *evsel,
  934. struct perf_sample *sample,
  935. struct machine *machine)
  936. {
  937. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  938. struct work_atoms *atoms;
  939. struct work_atom *atom;
  940. struct thread *wakee;
  941. u64 timestamp = sample->time;
  942. int err = -1;
  943. wakee = machine__findnew_thread(machine, -1, pid);
  944. if (wakee == NULL)
  945. return -1;
  946. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  947. if (!atoms) {
  948. if (thread_atoms_insert(sched, wakee))
  949. goto out_put;
  950. atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
  951. if (!atoms) {
  952. pr_err("wakeup-event: Internal tree error");
  953. goto out_put;
  954. }
  955. if (add_sched_out_event(atoms, 'S', timestamp))
  956. goto out_put;
  957. }
  958. BUG_ON(list_empty(&atoms->work_list));
  959. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  960. /*
  961. * As we do not guarantee the wakeup event happens when
  962. * task is out of run queue, also may happen when task is
  963. * on run queue and wakeup only change ->state to TASK_RUNNING,
  964. * then we should not set the ->wake_up_time when wake up a
  965. * task which is on run queue.
  966. *
  967. * You WILL be missing events if you've recorded only
  968. * one CPU, or are only looking at only one, so don't
  969. * skip in this case.
  970. */
  971. if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
  972. goto out_ok;
  973. sched->nr_timestamps++;
  974. if (atom->sched_out_time > timestamp) {
  975. sched->nr_unordered_timestamps++;
  976. goto out_ok;
  977. }
  978. atom->state = THREAD_WAIT_CPU;
  979. atom->wake_up_time = timestamp;
  980. out_ok:
  981. err = 0;
  982. out_put:
  983. thread__put(wakee);
  984. return err;
  985. }
  986. static int latency_migrate_task_event(struct perf_sched *sched,
  987. struct perf_evsel *evsel,
  988. struct perf_sample *sample,
  989. struct machine *machine)
  990. {
  991. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  992. u64 timestamp = sample->time;
  993. struct work_atoms *atoms;
  994. struct work_atom *atom;
  995. struct thread *migrant;
  996. int err = -1;
  997. /*
  998. * Only need to worry about migration when profiling one CPU.
  999. */
  1000. if (sched->profile_cpu == -1)
  1001. return 0;
  1002. migrant = machine__findnew_thread(machine, -1, pid);
  1003. if (migrant == NULL)
  1004. return -1;
  1005. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  1006. if (!atoms) {
  1007. if (thread_atoms_insert(sched, migrant))
  1008. goto out_put;
  1009. register_pid(sched, migrant->tid, thread__comm_str(migrant));
  1010. atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
  1011. if (!atoms) {
  1012. pr_err("migration-event: Internal tree error");
  1013. goto out_put;
  1014. }
  1015. if (add_sched_out_event(atoms, 'R', timestamp))
  1016. goto out_put;
  1017. }
  1018. BUG_ON(list_empty(&atoms->work_list));
  1019. atom = list_entry(atoms->work_list.prev, struct work_atom, list);
  1020. atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
  1021. sched->nr_timestamps++;
  1022. if (atom->sched_out_time > timestamp)
  1023. sched->nr_unordered_timestamps++;
  1024. err = 0;
  1025. out_put:
  1026. thread__put(migrant);
  1027. return err;
  1028. }
  1029. static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
  1030. {
  1031. int i;
  1032. int ret;
  1033. u64 avg;
  1034. char max_lat_at[32];
  1035. if (!work_list->nb_atoms)
  1036. return;
  1037. /*
  1038. * Ignore idle threads:
  1039. */
  1040. if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
  1041. return;
  1042. sched->all_runtime += work_list->total_runtime;
  1043. sched->all_count += work_list->nb_atoms;
  1044. if (work_list->num_merged > 1)
  1045. ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
  1046. else
  1047. ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
  1048. for (i = 0; i < 24 - ret; i++)
  1049. printf(" ");
  1050. avg = work_list->total_lat / work_list->nb_atoms;
  1051. timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
  1052. printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n",
  1053. (double)work_list->total_runtime / NSEC_PER_MSEC,
  1054. work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
  1055. (double)work_list->max_lat / NSEC_PER_MSEC,
  1056. max_lat_at);
  1057. }
  1058. static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
  1059. {
  1060. if (l->thread == r->thread)
  1061. return 0;
  1062. if (l->thread->tid < r->thread->tid)
  1063. return -1;
  1064. if (l->thread->tid > r->thread->tid)
  1065. return 1;
  1066. return (int)(l->thread - r->thread);
  1067. }
  1068. static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
  1069. {
  1070. u64 avgl, avgr;
  1071. if (!l->nb_atoms)
  1072. return -1;
  1073. if (!r->nb_atoms)
  1074. return 1;
  1075. avgl = l->total_lat / l->nb_atoms;
  1076. avgr = r->total_lat / r->nb_atoms;
  1077. if (avgl < avgr)
  1078. return -1;
  1079. if (avgl > avgr)
  1080. return 1;
  1081. return 0;
  1082. }
  1083. static int max_cmp(struct work_atoms *l, struct work_atoms *r)
  1084. {
  1085. if (l->max_lat < r->max_lat)
  1086. return -1;
  1087. if (l->max_lat > r->max_lat)
  1088. return 1;
  1089. return 0;
  1090. }
  1091. static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
  1092. {
  1093. if (l->nb_atoms < r->nb_atoms)
  1094. return -1;
  1095. if (l->nb_atoms > r->nb_atoms)
  1096. return 1;
  1097. return 0;
  1098. }
  1099. static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
  1100. {
  1101. if (l->total_runtime < r->total_runtime)
  1102. return -1;
  1103. if (l->total_runtime > r->total_runtime)
  1104. return 1;
  1105. return 0;
  1106. }
  1107. static int sort_dimension__add(const char *tok, struct list_head *list)
  1108. {
  1109. size_t i;
  1110. static struct sort_dimension avg_sort_dimension = {
  1111. .name = "avg",
  1112. .cmp = avg_cmp,
  1113. };
  1114. static struct sort_dimension max_sort_dimension = {
  1115. .name = "max",
  1116. .cmp = max_cmp,
  1117. };
  1118. static struct sort_dimension pid_sort_dimension = {
  1119. .name = "pid",
  1120. .cmp = pid_cmp,
  1121. };
  1122. static struct sort_dimension runtime_sort_dimension = {
  1123. .name = "runtime",
  1124. .cmp = runtime_cmp,
  1125. };
  1126. static struct sort_dimension switch_sort_dimension = {
  1127. .name = "switch",
  1128. .cmp = switch_cmp,
  1129. };
  1130. struct sort_dimension *available_sorts[] = {
  1131. &pid_sort_dimension,
  1132. &avg_sort_dimension,
  1133. &max_sort_dimension,
  1134. &switch_sort_dimension,
  1135. &runtime_sort_dimension,
  1136. };
  1137. for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
  1138. if (!strcmp(available_sorts[i]->name, tok)) {
  1139. list_add_tail(&available_sorts[i]->list, list);
  1140. return 0;
  1141. }
  1142. }
  1143. return -1;
  1144. }
  1145. static void perf_sched__sort_lat(struct perf_sched *sched)
  1146. {
  1147. struct rb_node *node;
  1148. struct rb_root *root = &sched->atom_root;
  1149. again:
  1150. for (;;) {
  1151. struct work_atoms *data;
  1152. node = rb_first(root);
  1153. if (!node)
  1154. break;
  1155. rb_erase(node, root);
  1156. data = rb_entry(node, struct work_atoms, node);
  1157. __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
  1158. }
  1159. if (root == &sched->atom_root) {
  1160. root = &sched->merged_atom_root;
  1161. goto again;
  1162. }
  1163. }
  1164. static int process_sched_wakeup_event(struct perf_tool *tool,
  1165. struct perf_evsel *evsel,
  1166. struct perf_sample *sample,
  1167. struct machine *machine)
  1168. {
  1169. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1170. if (sched->tp_handler->wakeup_event)
  1171. return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
  1172. return 0;
  1173. }
  1174. union map_priv {
  1175. void *ptr;
  1176. bool color;
  1177. };
  1178. static bool thread__has_color(struct thread *thread)
  1179. {
  1180. union map_priv priv = {
  1181. .ptr = thread__priv(thread),
  1182. };
  1183. return priv.color;
  1184. }
  1185. static struct thread*
  1186. map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
  1187. {
  1188. struct thread *thread = machine__findnew_thread(machine, pid, tid);
  1189. union map_priv priv = {
  1190. .color = false,
  1191. };
  1192. if (!sched->map.color_pids || !thread || thread__priv(thread))
  1193. return thread;
  1194. if (thread_map__has(sched->map.color_pids, tid))
  1195. priv.color = true;
  1196. thread__set_priv(thread, priv.ptr);
  1197. return thread;
  1198. }
  1199. static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
  1200. struct perf_sample *sample, struct machine *machine)
  1201. {
  1202. const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1203. struct thread *sched_in;
  1204. int new_shortname;
  1205. u64 timestamp0, timestamp = sample->time;
  1206. s64 delta;
  1207. int i, this_cpu = sample->cpu;
  1208. int cpus_nr;
  1209. bool new_cpu = false;
  1210. const char *color = PERF_COLOR_NORMAL;
  1211. char stimestamp[32];
  1212. BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
  1213. if (this_cpu > sched->max_cpu)
  1214. sched->max_cpu = this_cpu;
  1215. if (sched->map.comp) {
  1216. cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
  1217. if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
  1218. sched->map.comp_cpus[cpus_nr++] = this_cpu;
  1219. new_cpu = true;
  1220. }
  1221. } else
  1222. cpus_nr = sched->max_cpu;
  1223. timestamp0 = sched->cpu_last_switched[this_cpu];
  1224. sched->cpu_last_switched[this_cpu] = timestamp;
  1225. if (timestamp0)
  1226. delta = timestamp - timestamp0;
  1227. else
  1228. delta = 0;
  1229. if (delta < 0) {
  1230. pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
  1231. return -1;
  1232. }
  1233. sched_in = map__findnew_thread(sched, machine, -1, next_pid);
  1234. if (sched_in == NULL)
  1235. return -1;
  1236. sched->curr_thread[this_cpu] = thread__get(sched_in);
  1237. printf(" ");
  1238. new_shortname = 0;
  1239. if (!sched_in->shortname[0]) {
  1240. if (!strcmp(thread__comm_str(sched_in), "swapper")) {
  1241. /*
  1242. * Don't allocate a letter-number for swapper:0
  1243. * as a shortname. Instead, we use '.' for it.
  1244. */
  1245. sched_in->shortname[0] = '.';
  1246. sched_in->shortname[1] = ' ';
  1247. } else {
  1248. sched_in->shortname[0] = sched->next_shortname1;
  1249. sched_in->shortname[1] = sched->next_shortname2;
  1250. if (sched->next_shortname1 < 'Z') {
  1251. sched->next_shortname1++;
  1252. } else {
  1253. sched->next_shortname1 = 'A';
  1254. if (sched->next_shortname2 < '9')
  1255. sched->next_shortname2++;
  1256. else
  1257. sched->next_shortname2 = '0';
  1258. }
  1259. }
  1260. new_shortname = 1;
  1261. }
  1262. for (i = 0; i < cpus_nr; i++) {
  1263. int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
  1264. struct thread *curr_thread = sched->curr_thread[cpu];
  1265. const char *pid_color = color;
  1266. const char *cpu_color = color;
  1267. if (curr_thread && thread__has_color(curr_thread))
  1268. pid_color = COLOR_PIDS;
  1269. if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
  1270. continue;
  1271. if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
  1272. cpu_color = COLOR_CPUS;
  1273. if (cpu != this_cpu)
  1274. color_fprintf(stdout, color, " ");
  1275. else
  1276. color_fprintf(stdout, cpu_color, "*");
  1277. if (sched->curr_thread[cpu])
  1278. color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
  1279. else
  1280. color_fprintf(stdout, color, " ");
  1281. }
  1282. if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
  1283. goto out;
  1284. timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
  1285. color_fprintf(stdout, color, " %12s secs ", stimestamp);
  1286. if (new_shortname || (verbose && sched_in->tid)) {
  1287. const char *pid_color = color;
  1288. if (thread__has_color(sched_in))
  1289. pid_color = COLOR_PIDS;
  1290. color_fprintf(stdout, pid_color, "%s => %s:%d",
  1291. sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
  1292. }
  1293. if (sched->map.comp && new_cpu)
  1294. color_fprintf(stdout, color, " (CPU %d)", this_cpu);
  1295. out:
  1296. color_fprintf(stdout, color, "\n");
  1297. thread__put(sched_in);
  1298. return 0;
  1299. }
  1300. static int process_sched_switch_event(struct perf_tool *tool,
  1301. struct perf_evsel *evsel,
  1302. struct perf_sample *sample,
  1303. struct machine *machine)
  1304. {
  1305. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1306. int this_cpu = sample->cpu, err = 0;
  1307. u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
  1308. next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  1309. if (sched->curr_pid[this_cpu] != (u32)-1) {
  1310. /*
  1311. * Are we trying to switch away a PID that is
  1312. * not current?
  1313. */
  1314. if (sched->curr_pid[this_cpu] != prev_pid)
  1315. sched->nr_context_switch_bugs++;
  1316. }
  1317. if (sched->tp_handler->switch_event)
  1318. err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
  1319. sched->curr_pid[this_cpu] = next_pid;
  1320. return err;
  1321. }
  1322. static int process_sched_runtime_event(struct perf_tool *tool,
  1323. struct perf_evsel *evsel,
  1324. struct perf_sample *sample,
  1325. struct machine *machine)
  1326. {
  1327. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1328. if (sched->tp_handler->runtime_event)
  1329. return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
  1330. return 0;
  1331. }
  1332. static int perf_sched__process_fork_event(struct perf_tool *tool,
  1333. union perf_event *event,
  1334. struct perf_sample *sample,
  1335. struct machine *machine)
  1336. {
  1337. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1338. /* run the fork event through the perf machineruy */
  1339. perf_event__process_fork(tool, event, sample, machine);
  1340. /* and then run additional processing needed for this command */
  1341. if (sched->tp_handler->fork_event)
  1342. return sched->tp_handler->fork_event(sched, event, machine);
  1343. return 0;
  1344. }
  1345. static int process_sched_migrate_task_event(struct perf_tool *tool,
  1346. struct perf_evsel *evsel,
  1347. struct perf_sample *sample,
  1348. struct machine *machine)
  1349. {
  1350. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1351. if (sched->tp_handler->migrate_task_event)
  1352. return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
  1353. return 0;
  1354. }
  1355. typedef int (*tracepoint_handler)(struct perf_tool *tool,
  1356. struct perf_evsel *evsel,
  1357. struct perf_sample *sample,
  1358. struct machine *machine);
  1359. static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
  1360. union perf_event *event __maybe_unused,
  1361. struct perf_sample *sample,
  1362. struct perf_evsel *evsel,
  1363. struct machine *machine)
  1364. {
  1365. int err = 0;
  1366. if (evsel->handler != NULL) {
  1367. tracepoint_handler f = evsel->handler;
  1368. err = f(tool, evsel, sample, machine);
  1369. }
  1370. return err;
  1371. }
  1372. static int perf_sched__read_events(struct perf_sched *sched)
  1373. {
  1374. const struct perf_evsel_str_handler handlers[] = {
  1375. { "sched:sched_switch", process_sched_switch_event, },
  1376. { "sched:sched_stat_runtime", process_sched_runtime_event, },
  1377. { "sched:sched_wakeup", process_sched_wakeup_event, },
  1378. { "sched:sched_wakeup_new", process_sched_wakeup_event, },
  1379. { "sched:sched_migrate_task", process_sched_migrate_task_event, },
  1380. };
  1381. struct perf_session *session;
  1382. struct perf_data_file file = {
  1383. .path = input_name,
  1384. .mode = PERF_DATA_MODE_READ,
  1385. .force = sched->force,
  1386. };
  1387. int rc = -1;
  1388. session = perf_session__new(&file, false, &sched->tool);
  1389. if (session == NULL) {
  1390. pr_debug("No Memory for session\n");
  1391. return -1;
  1392. }
  1393. symbol__init(&session->header.env);
  1394. if (perf_session__set_tracepoints_handlers(session, handlers))
  1395. goto out_delete;
  1396. if (perf_session__has_traces(session, "record -R")) {
  1397. int err = perf_session__process_events(session);
  1398. if (err) {
  1399. pr_err("Failed to process events, error %d", err);
  1400. goto out_delete;
  1401. }
  1402. sched->nr_events = session->evlist->stats.nr_events[0];
  1403. sched->nr_lost_events = session->evlist->stats.total_lost;
  1404. sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
  1405. }
  1406. rc = 0;
  1407. out_delete:
  1408. perf_session__delete(session);
  1409. return rc;
  1410. }
  1411. /*
  1412. * scheduling times are printed as msec.usec
  1413. */
  1414. static inline void print_sched_time(unsigned long long nsecs, int width)
  1415. {
  1416. unsigned long msecs;
  1417. unsigned long usecs;
  1418. msecs = nsecs / NSEC_PER_MSEC;
  1419. nsecs -= msecs * NSEC_PER_MSEC;
  1420. usecs = nsecs / NSEC_PER_USEC;
  1421. printf("%*lu.%03lu ", width, msecs, usecs);
  1422. }
  1423. /*
  1424. * returns runtime data for event, allocating memory for it the
  1425. * first time it is used.
  1426. */
  1427. static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
  1428. {
  1429. struct evsel_runtime *r = evsel->priv;
  1430. if (r == NULL) {
  1431. r = zalloc(sizeof(struct evsel_runtime));
  1432. evsel->priv = r;
  1433. }
  1434. return r;
  1435. }
  1436. /*
  1437. * save last time event was seen per cpu
  1438. */
  1439. static void perf_evsel__save_time(struct perf_evsel *evsel,
  1440. u64 timestamp, u32 cpu)
  1441. {
  1442. struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
  1443. if (r == NULL)
  1444. return;
  1445. if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
  1446. int i, n = __roundup_pow_of_two(cpu+1);
  1447. void *p = r->last_time;
  1448. p = realloc(r->last_time, n * sizeof(u64));
  1449. if (!p)
  1450. return;
  1451. r->last_time = p;
  1452. for (i = r->ncpu; i < n; ++i)
  1453. r->last_time[i] = (u64) 0;
  1454. r->ncpu = n;
  1455. }
  1456. r->last_time[cpu] = timestamp;
  1457. }
  1458. /* returns last time this event was seen on the given cpu */
  1459. static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
  1460. {
  1461. struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
  1462. if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
  1463. return 0;
  1464. return r->last_time[cpu];
  1465. }
  1466. static int comm_width = 30;
  1467. static char *timehist_get_commstr(struct thread *thread)
  1468. {
  1469. static char str[32];
  1470. const char *comm = thread__comm_str(thread);
  1471. pid_t tid = thread->tid;
  1472. pid_t pid = thread->pid_;
  1473. int n;
  1474. if (pid == 0)
  1475. n = scnprintf(str, sizeof(str), "%s", comm);
  1476. else if (tid != pid)
  1477. n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
  1478. else
  1479. n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
  1480. if (n > comm_width)
  1481. comm_width = n;
  1482. return str;
  1483. }
  1484. static void timehist_header(struct perf_sched *sched)
  1485. {
  1486. u32 ncpus = sched->max_cpu + 1;
  1487. u32 i, j;
  1488. printf("%15s %6s ", "time", "cpu");
  1489. if (sched->show_cpu_visual) {
  1490. printf(" ");
  1491. for (i = 0, j = 0; i < ncpus; ++i) {
  1492. printf("%x", j++);
  1493. if (j > 15)
  1494. j = 0;
  1495. }
  1496. printf(" ");
  1497. }
  1498. printf(" %-*s %9s %9s %9s", comm_width,
  1499. "task name", "wait time", "sch delay", "run time");
  1500. printf("\n");
  1501. /*
  1502. * units row
  1503. */
  1504. printf("%15s %-6s ", "", "");
  1505. if (sched->show_cpu_visual)
  1506. printf(" %*s ", ncpus, "");
  1507. printf(" %-*s %9s %9s %9s\n", comm_width,
  1508. "[tid/pid]", "(msec)", "(msec)", "(msec)");
  1509. /*
  1510. * separator
  1511. */
  1512. printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
  1513. if (sched->show_cpu_visual)
  1514. printf(" %.*s ", ncpus, graph_dotted_line);
  1515. printf(" %.*s %.9s %.9s %.9s", comm_width,
  1516. graph_dotted_line, graph_dotted_line, graph_dotted_line,
  1517. graph_dotted_line);
  1518. printf("\n");
  1519. }
  1520. static void timehist_print_sample(struct perf_sched *sched,
  1521. struct perf_sample *sample,
  1522. struct addr_location *al,
  1523. struct thread *thread,
  1524. u64 t)
  1525. {
  1526. struct thread_runtime *tr = thread__priv(thread);
  1527. u32 max_cpus = sched->max_cpu + 1;
  1528. char tstr[64];
  1529. timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
  1530. printf("%15s [%04d] ", tstr, sample->cpu);
  1531. if (sched->show_cpu_visual) {
  1532. u32 i;
  1533. char c;
  1534. printf(" ");
  1535. for (i = 0; i < max_cpus; ++i) {
  1536. /* flag idle times with 'i'; others are sched events */
  1537. if (i == sample->cpu)
  1538. c = (thread->tid == 0) ? 'i' : 's';
  1539. else
  1540. c = ' ';
  1541. printf("%c", c);
  1542. }
  1543. printf(" ");
  1544. }
  1545. printf(" %-*s ", comm_width, timehist_get_commstr(thread));
  1546. print_sched_time(tr->dt_wait, 6);
  1547. print_sched_time(tr->dt_delay, 6);
  1548. print_sched_time(tr->dt_run, 6);
  1549. if (sched->show_wakeups)
  1550. printf(" %-*s", comm_width, "");
  1551. if (thread->tid == 0)
  1552. goto out;
  1553. if (sched->show_callchain)
  1554. printf(" ");
  1555. sample__fprintf_sym(sample, al, 0,
  1556. EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
  1557. EVSEL__PRINT_CALLCHAIN_ARROW |
  1558. EVSEL__PRINT_SKIP_IGNORED,
  1559. &callchain_cursor, stdout);
  1560. out:
  1561. printf("\n");
  1562. }
  1563. /*
  1564. * Explanation of delta-time stats:
  1565. *
  1566. * t = time of current schedule out event
  1567. * tprev = time of previous sched out event
  1568. * also time of schedule-in event for current task
  1569. * last_time = time of last sched change event for current task
  1570. * (i.e, time process was last scheduled out)
  1571. * ready_to_run = time of wakeup for current task
  1572. *
  1573. * -----|------------|------------|------------|------
  1574. * last ready tprev t
  1575. * time to run
  1576. *
  1577. * |-------- dt_wait --------|
  1578. * |- dt_delay -|-- dt_run --|
  1579. *
  1580. * dt_run = run time of current task
  1581. * dt_wait = time between last schedule out event for task and tprev
  1582. * represents time spent off the cpu
  1583. * dt_delay = time between wakeup and schedule-in of task
  1584. */
  1585. static void timehist_update_runtime_stats(struct thread_runtime *r,
  1586. u64 t, u64 tprev)
  1587. {
  1588. r->dt_delay = 0;
  1589. r->dt_wait = 0;
  1590. r->dt_run = 0;
  1591. if (tprev) {
  1592. r->dt_run = t - tprev;
  1593. if (r->ready_to_run) {
  1594. if (r->ready_to_run > tprev)
  1595. pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
  1596. else
  1597. r->dt_delay = tprev - r->ready_to_run;
  1598. }
  1599. if (r->last_time > tprev)
  1600. pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
  1601. else if (r->last_time)
  1602. r->dt_wait = tprev - r->last_time;
  1603. }
  1604. update_stats(&r->run_stats, r->dt_run);
  1605. r->total_run_time += r->dt_run;
  1606. }
  1607. static bool is_idle_sample(struct perf_sample *sample,
  1608. struct perf_evsel *evsel)
  1609. {
  1610. /* pid 0 == swapper == idle task */
  1611. if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0)
  1612. return perf_evsel__intval(evsel, sample, "prev_pid") == 0;
  1613. return sample->pid == 0;
  1614. }
  1615. static void save_task_callchain(struct perf_sched *sched,
  1616. struct perf_sample *sample,
  1617. struct perf_evsel *evsel,
  1618. struct machine *machine)
  1619. {
  1620. struct callchain_cursor *cursor = &callchain_cursor;
  1621. struct thread *thread;
  1622. /* want main thread for process - has maps */
  1623. thread = machine__findnew_thread(machine, sample->pid, sample->pid);
  1624. if (thread == NULL) {
  1625. pr_debug("Failed to get thread for pid %d.\n", sample->pid);
  1626. return;
  1627. }
  1628. if (!symbol_conf.use_callchain || sample->callchain == NULL)
  1629. return;
  1630. if (thread__resolve_callchain(thread, cursor, evsel, sample,
  1631. NULL, NULL, sched->max_stack + 2) != 0) {
  1632. if (verbose)
  1633. error("Failed to resolve callchain. Skipping\n");
  1634. return;
  1635. }
  1636. callchain_cursor_commit(cursor);
  1637. while (true) {
  1638. struct callchain_cursor_node *node;
  1639. struct symbol *sym;
  1640. node = callchain_cursor_current(cursor);
  1641. if (node == NULL)
  1642. break;
  1643. sym = node->sym;
  1644. if (sym && sym->name) {
  1645. if (!strcmp(sym->name, "schedule") ||
  1646. !strcmp(sym->name, "__schedule") ||
  1647. !strcmp(sym->name, "preempt_schedule"))
  1648. sym->ignore = 1;
  1649. }
  1650. callchain_cursor_advance(cursor);
  1651. }
  1652. }
  1653. static int init_idle_thread(struct thread *thread)
  1654. {
  1655. struct idle_thread_runtime *itr;
  1656. thread__set_comm(thread, idle_comm, 0);
  1657. itr = zalloc(sizeof(*itr));
  1658. if (itr == NULL)
  1659. return -ENOMEM;
  1660. init_stats(&itr->tr.run_stats);
  1661. callchain_init(&itr->callchain);
  1662. callchain_cursor_reset(&itr->cursor);
  1663. thread__set_priv(thread, itr);
  1664. return 0;
  1665. }
  1666. /*
  1667. * Track idle stats per cpu by maintaining a local thread
  1668. * struct for the idle task on each cpu.
  1669. */
  1670. static int init_idle_threads(int ncpu)
  1671. {
  1672. int i, ret;
  1673. idle_threads = zalloc(ncpu * sizeof(struct thread *));
  1674. if (!idle_threads)
  1675. return -ENOMEM;
  1676. idle_max_cpu = ncpu;
  1677. /* allocate the actual thread struct if needed */
  1678. for (i = 0; i < ncpu; ++i) {
  1679. idle_threads[i] = thread__new(0, 0);
  1680. if (idle_threads[i] == NULL)
  1681. return -ENOMEM;
  1682. ret = init_idle_thread(idle_threads[i]);
  1683. if (ret < 0)
  1684. return ret;
  1685. }
  1686. return 0;
  1687. }
  1688. static void free_idle_threads(void)
  1689. {
  1690. int i;
  1691. if (idle_threads == NULL)
  1692. return;
  1693. for (i = 0; i < idle_max_cpu; ++i) {
  1694. if ((idle_threads[i]))
  1695. thread__delete(idle_threads[i]);
  1696. }
  1697. free(idle_threads);
  1698. }
  1699. static struct thread *get_idle_thread(int cpu)
  1700. {
  1701. /*
  1702. * expand/allocate array of pointers to local thread
  1703. * structs if needed
  1704. */
  1705. if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
  1706. int i, j = __roundup_pow_of_two(cpu+1);
  1707. void *p;
  1708. p = realloc(idle_threads, j * sizeof(struct thread *));
  1709. if (!p)
  1710. return NULL;
  1711. idle_threads = (struct thread **) p;
  1712. for (i = idle_max_cpu; i < j; ++i)
  1713. idle_threads[i] = NULL;
  1714. idle_max_cpu = j;
  1715. }
  1716. /* allocate a new thread struct if needed */
  1717. if (idle_threads[cpu] == NULL) {
  1718. idle_threads[cpu] = thread__new(0, 0);
  1719. if (idle_threads[cpu]) {
  1720. if (init_idle_thread(idle_threads[cpu]) < 0)
  1721. return NULL;
  1722. }
  1723. }
  1724. return idle_threads[cpu];
  1725. }
  1726. static void save_idle_callchain(struct idle_thread_runtime *itr,
  1727. struct perf_sample *sample)
  1728. {
  1729. if (!symbol_conf.use_callchain || sample->callchain == NULL)
  1730. return;
  1731. callchain_cursor__copy(&itr->cursor, &callchain_cursor);
  1732. }
  1733. /*
  1734. * handle runtime stats saved per thread
  1735. */
  1736. static struct thread_runtime *thread__init_runtime(struct thread *thread)
  1737. {
  1738. struct thread_runtime *r;
  1739. r = zalloc(sizeof(struct thread_runtime));
  1740. if (!r)
  1741. return NULL;
  1742. init_stats(&r->run_stats);
  1743. thread__set_priv(thread, r);
  1744. return r;
  1745. }
  1746. static struct thread_runtime *thread__get_runtime(struct thread *thread)
  1747. {
  1748. struct thread_runtime *tr;
  1749. tr = thread__priv(thread);
  1750. if (tr == NULL) {
  1751. tr = thread__init_runtime(thread);
  1752. if (tr == NULL)
  1753. pr_debug("Failed to malloc memory for runtime data.\n");
  1754. }
  1755. return tr;
  1756. }
  1757. static struct thread *timehist_get_thread(struct perf_sched *sched,
  1758. struct perf_sample *sample,
  1759. struct machine *machine,
  1760. struct perf_evsel *evsel)
  1761. {
  1762. struct thread *thread;
  1763. if (is_idle_sample(sample, evsel)) {
  1764. thread = get_idle_thread(sample->cpu);
  1765. if (thread == NULL)
  1766. pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
  1767. } else {
  1768. /* there were samples with tid 0 but non-zero pid */
  1769. thread = machine__findnew_thread(machine, sample->pid,
  1770. sample->tid ?: sample->pid);
  1771. if (thread == NULL) {
  1772. pr_debug("Failed to get thread for tid %d. skipping sample.\n",
  1773. sample->tid);
  1774. }
  1775. save_task_callchain(sched, sample, evsel, machine);
  1776. if (sched->idle_hist) {
  1777. struct thread *idle;
  1778. struct idle_thread_runtime *itr;
  1779. idle = get_idle_thread(sample->cpu);
  1780. if (idle == NULL) {
  1781. pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
  1782. return NULL;
  1783. }
  1784. itr = thread__priv(idle);
  1785. if (itr == NULL)
  1786. return NULL;
  1787. itr->last_thread = thread;
  1788. /* copy task callchain when entering to idle */
  1789. if (perf_evsel__intval(evsel, sample, "next_pid") == 0)
  1790. save_idle_callchain(itr, sample);
  1791. }
  1792. }
  1793. return thread;
  1794. }
  1795. static bool timehist_skip_sample(struct perf_sched *sched,
  1796. struct thread *thread,
  1797. struct perf_evsel *evsel,
  1798. struct perf_sample *sample)
  1799. {
  1800. bool rc = false;
  1801. if (thread__is_filtered(thread)) {
  1802. rc = true;
  1803. sched->skipped_samples++;
  1804. }
  1805. if (sched->idle_hist) {
  1806. if (strcmp(perf_evsel__name(evsel), "sched:sched_switch"))
  1807. rc = true;
  1808. else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 &&
  1809. perf_evsel__intval(evsel, sample, "next_pid") != 0)
  1810. rc = true;
  1811. }
  1812. return rc;
  1813. }
  1814. static void timehist_print_wakeup_event(struct perf_sched *sched,
  1815. struct perf_evsel *evsel,
  1816. struct perf_sample *sample,
  1817. struct machine *machine,
  1818. struct thread *awakened)
  1819. {
  1820. struct thread *thread;
  1821. char tstr[64];
  1822. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  1823. if (thread == NULL)
  1824. return;
  1825. /* show wakeup unless both awakee and awaker are filtered */
  1826. if (timehist_skip_sample(sched, thread, evsel, sample) &&
  1827. timehist_skip_sample(sched, awakened, evsel, sample)) {
  1828. return;
  1829. }
  1830. timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
  1831. printf("%15s [%04d] ", tstr, sample->cpu);
  1832. if (sched->show_cpu_visual)
  1833. printf(" %*s ", sched->max_cpu + 1, "");
  1834. printf(" %-*s ", comm_width, timehist_get_commstr(thread));
  1835. /* dt spacer */
  1836. printf(" %9s %9s %9s ", "", "", "");
  1837. printf("awakened: %s", timehist_get_commstr(awakened));
  1838. printf("\n");
  1839. }
  1840. static int timehist_sched_wakeup_event(struct perf_tool *tool,
  1841. union perf_event *event __maybe_unused,
  1842. struct perf_evsel *evsel,
  1843. struct perf_sample *sample,
  1844. struct machine *machine)
  1845. {
  1846. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1847. struct thread *thread;
  1848. struct thread_runtime *tr = NULL;
  1849. /* want pid of awakened task not pid in sample */
  1850. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  1851. thread = machine__findnew_thread(machine, 0, pid);
  1852. if (thread == NULL)
  1853. return -1;
  1854. tr = thread__get_runtime(thread);
  1855. if (tr == NULL)
  1856. return -1;
  1857. if (tr->ready_to_run == 0)
  1858. tr->ready_to_run = sample->time;
  1859. /* show wakeups if requested */
  1860. if (sched->show_wakeups &&
  1861. !perf_time__skip_sample(&sched->ptime, sample->time))
  1862. timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
  1863. return 0;
  1864. }
  1865. static void timehist_print_migration_event(struct perf_sched *sched,
  1866. struct perf_evsel *evsel,
  1867. struct perf_sample *sample,
  1868. struct machine *machine,
  1869. struct thread *migrated)
  1870. {
  1871. struct thread *thread;
  1872. char tstr[64];
  1873. u32 max_cpus = sched->max_cpu + 1;
  1874. u32 ocpu, dcpu;
  1875. if (sched->summary_only)
  1876. return;
  1877. max_cpus = sched->max_cpu + 1;
  1878. ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
  1879. dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
  1880. thread = machine__findnew_thread(machine, sample->pid, sample->tid);
  1881. if (thread == NULL)
  1882. return;
  1883. if (timehist_skip_sample(sched, thread, evsel, sample) &&
  1884. timehist_skip_sample(sched, migrated, evsel, sample)) {
  1885. return;
  1886. }
  1887. timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
  1888. printf("%15s [%04d] ", tstr, sample->cpu);
  1889. if (sched->show_cpu_visual) {
  1890. u32 i;
  1891. char c;
  1892. printf(" ");
  1893. for (i = 0; i < max_cpus; ++i) {
  1894. c = (i == sample->cpu) ? 'm' : ' ';
  1895. printf("%c", c);
  1896. }
  1897. printf(" ");
  1898. }
  1899. printf(" %-*s ", comm_width, timehist_get_commstr(thread));
  1900. /* dt spacer */
  1901. printf(" %9s %9s %9s ", "", "", "");
  1902. printf("migrated: %s", timehist_get_commstr(migrated));
  1903. printf(" cpu %d => %d", ocpu, dcpu);
  1904. printf("\n");
  1905. }
  1906. static int timehist_migrate_task_event(struct perf_tool *tool,
  1907. union perf_event *event __maybe_unused,
  1908. struct perf_evsel *evsel,
  1909. struct perf_sample *sample,
  1910. struct machine *machine)
  1911. {
  1912. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1913. struct thread *thread;
  1914. struct thread_runtime *tr = NULL;
  1915. /* want pid of migrated task not pid in sample */
  1916. const u32 pid = perf_evsel__intval(evsel, sample, "pid");
  1917. thread = machine__findnew_thread(machine, 0, pid);
  1918. if (thread == NULL)
  1919. return -1;
  1920. tr = thread__get_runtime(thread);
  1921. if (tr == NULL)
  1922. return -1;
  1923. tr->migrations++;
  1924. /* show migrations if requested */
  1925. timehist_print_migration_event(sched, evsel, sample, machine, thread);
  1926. return 0;
  1927. }
  1928. static int timehist_sched_change_event(struct perf_tool *tool,
  1929. union perf_event *event,
  1930. struct perf_evsel *evsel,
  1931. struct perf_sample *sample,
  1932. struct machine *machine)
  1933. {
  1934. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  1935. struct perf_time_interval *ptime = &sched->ptime;
  1936. struct addr_location al;
  1937. struct thread *thread;
  1938. struct thread_runtime *tr = NULL;
  1939. u64 tprev, t = sample->time;
  1940. int rc = 0;
  1941. if (machine__resolve(machine, &al, sample) < 0) {
  1942. pr_err("problem processing %d event. skipping it\n",
  1943. event->header.type);
  1944. rc = -1;
  1945. goto out;
  1946. }
  1947. thread = timehist_get_thread(sched, sample, machine, evsel);
  1948. if (thread == NULL) {
  1949. rc = -1;
  1950. goto out;
  1951. }
  1952. if (timehist_skip_sample(sched, thread, evsel, sample))
  1953. goto out;
  1954. tr = thread__get_runtime(thread);
  1955. if (tr == NULL) {
  1956. rc = -1;
  1957. goto out;
  1958. }
  1959. tprev = perf_evsel__get_time(evsel, sample->cpu);
  1960. /*
  1961. * If start time given:
  1962. * - sample time is under window user cares about - skip sample
  1963. * - tprev is under window user cares about - reset to start of window
  1964. */
  1965. if (ptime->start && ptime->start > t)
  1966. goto out;
  1967. if (tprev && ptime->start > tprev)
  1968. tprev = ptime->start;
  1969. /*
  1970. * If end time given:
  1971. * - previous sched event is out of window - we are done
  1972. * - sample time is beyond window user cares about - reset it
  1973. * to close out stats for time window interest
  1974. */
  1975. if (ptime->end) {
  1976. if (tprev > ptime->end)
  1977. goto out;
  1978. if (t > ptime->end)
  1979. t = ptime->end;
  1980. }
  1981. if (!sched->idle_hist || thread->tid == 0) {
  1982. timehist_update_runtime_stats(tr, t, tprev);
  1983. if (sched->idle_hist) {
  1984. struct idle_thread_runtime *itr = (void *)tr;
  1985. struct thread_runtime *last_tr;
  1986. BUG_ON(thread->tid != 0);
  1987. if (itr->last_thread == NULL)
  1988. goto out;
  1989. /* add current idle time as last thread's runtime */
  1990. last_tr = thread__get_runtime(itr->last_thread);
  1991. if (last_tr == NULL)
  1992. goto out;
  1993. timehist_update_runtime_stats(last_tr, t, tprev);
  1994. /*
  1995. * remove delta time of last thread as it's not updated
  1996. * and otherwise it will show an invalid value next
  1997. * time. we only care total run time and run stat.
  1998. */
  1999. last_tr->dt_run = 0;
  2000. last_tr->dt_wait = 0;
  2001. last_tr->dt_delay = 0;
  2002. if (itr->cursor.nr)
  2003. callchain_append(&itr->callchain, &itr->cursor, t - tprev);
  2004. itr->last_thread = NULL;
  2005. }
  2006. }
  2007. if (!sched->summary_only)
  2008. timehist_print_sample(sched, sample, &al, thread, t);
  2009. out:
  2010. if (sched->hist_time.start == 0 && t >= ptime->start)
  2011. sched->hist_time.start = t;
  2012. if (ptime->end == 0 || t <= ptime->end)
  2013. sched->hist_time.end = t;
  2014. if (tr) {
  2015. /* time of this sched_switch event becomes last time task seen */
  2016. tr->last_time = sample->time;
  2017. /* sched out event for task so reset ready to run time */
  2018. tr->ready_to_run = 0;
  2019. }
  2020. perf_evsel__save_time(evsel, sample->time, sample->cpu);
  2021. return rc;
  2022. }
  2023. static int timehist_sched_switch_event(struct perf_tool *tool,
  2024. union perf_event *event,
  2025. struct perf_evsel *evsel,
  2026. struct perf_sample *sample,
  2027. struct machine *machine __maybe_unused)
  2028. {
  2029. return timehist_sched_change_event(tool, event, evsel, sample, machine);
  2030. }
  2031. static int process_lost(struct perf_tool *tool __maybe_unused,
  2032. union perf_event *event,
  2033. struct perf_sample *sample,
  2034. struct machine *machine __maybe_unused)
  2035. {
  2036. char tstr[64];
  2037. timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
  2038. printf("%15s ", tstr);
  2039. printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
  2040. return 0;
  2041. }
  2042. static void print_thread_runtime(struct thread *t,
  2043. struct thread_runtime *r)
  2044. {
  2045. double mean = avg_stats(&r->run_stats);
  2046. float stddev;
  2047. printf("%*s %5d %9" PRIu64 " ",
  2048. comm_width, timehist_get_commstr(t), t->ppid,
  2049. (u64) r->run_stats.n);
  2050. print_sched_time(r->total_run_time, 8);
  2051. stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
  2052. print_sched_time(r->run_stats.min, 6);
  2053. printf(" ");
  2054. print_sched_time((u64) mean, 6);
  2055. printf(" ");
  2056. print_sched_time(r->run_stats.max, 6);
  2057. printf(" ");
  2058. printf("%5.2f", stddev);
  2059. printf(" %5" PRIu64, r->migrations);
  2060. printf("\n");
  2061. }
  2062. struct total_run_stats {
  2063. u64 sched_count;
  2064. u64 task_count;
  2065. u64 total_run_time;
  2066. };
  2067. static int __show_thread_runtime(struct thread *t, void *priv)
  2068. {
  2069. struct total_run_stats *stats = priv;
  2070. struct thread_runtime *r;
  2071. if (thread__is_filtered(t))
  2072. return 0;
  2073. r = thread__priv(t);
  2074. if (r && r->run_stats.n) {
  2075. stats->task_count++;
  2076. stats->sched_count += r->run_stats.n;
  2077. stats->total_run_time += r->total_run_time;
  2078. print_thread_runtime(t, r);
  2079. }
  2080. return 0;
  2081. }
  2082. static int show_thread_runtime(struct thread *t, void *priv)
  2083. {
  2084. if (t->dead)
  2085. return 0;
  2086. return __show_thread_runtime(t, priv);
  2087. }
  2088. static int show_deadthread_runtime(struct thread *t, void *priv)
  2089. {
  2090. if (!t->dead)
  2091. return 0;
  2092. return __show_thread_runtime(t, priv);
  2093. }
  2094. static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
  2095. {
  2096. const char *sep = " <- ";
  2097. struct callchain_list *chain;
  2098. size_t ret = 0;
  2099. char bf[1024];
  2100. bool first;
  2101. if (node == NULL)
  2102. return 0;
  2103. ret = callchain__fprintf_folded(fp, node->parent);
  2104. first = (ret == 0);
  2105. list_for_each_entry(chain, &node->val, list) {
  2106. if (chain->ip >= PERF_CONTEXT_MAX)
  2107. continue;
  2108. if (chain->ms.sym && chain->ms.sym->ignore)
  2109. continue;
  2110. ret += fprintf(fp, "%s%s", first ? "" : sep,
  2111. callchain_list__sym_name(chain, bf, sizeof(bf),
  2112. false));
  2113. first = false;
  2114. }
  2115. return ret;
  2116. }
  2117. static size_t timehist_print_idlehist_callchain(struct rb_root *root)
  2118. {
  2119. size_t ret = 0;
  2120. FILE *fp = stdout;
  2121. struct callchain_node *chain;
  2122. struct rb_node *rb_node = rb_first(root);
  2123. printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
  2124. printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
  2125. graph_dotted_line);
  2126. while (rb_node) {
  2127. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  2128. rb_node = rb_next(rb_node);
  2129. ret += fprintf(fp, " ");
  2130. print_sched_time(chain->hit, 12);
  2131. ret += 16; /* print_sched_time returns 2nd arg + 4 */
  2132. ret += fprintf(fp, " %8d ", chain->count);
  2133. ret += callchain__fprintf_folded(fp, chain);
  2134. ret += fprintf(fp, "\n");
  2135. }
  2136. return ret;
  2137. }
  2138. static void timehist_print_summary(struct perf_sched *sched,
  2139. struct perf_session *session)
  2140. {
  2141. struct machine *m = &session->machines.host;
  2142. struct total_run_stats totals;
  2143. u64 task_count;
  2144. struct thread *t;
  2145. struct thread_runtime *r;
  2146. int i;
  2147. u64 hist_time = sched->hist_time.end - sched->hist_time.start;
  2148. memset(&totals, 0, sizeof(totals));
  2149. if (sched->idle_hist) {
  2150. printf("\nIdle-time summary\n");
  2151. printf("%*s parent sched-out ", comm_width, "comm");
  2152. printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
  2153. } else {
  2154. printf("\nRuntime summary\n");
  2155. printf("%*s parent sched-in ", comm_width, "comm");
  2156. printf(" run-time min-run avg-run max-run stddev migrations\n");
  2157. }
  2158. printf("%*s (count) ", comm_width, "");
  2159. printf(" (msec) (msec) (msec) (msec) %%\n");
  2160. printf("%.117s\n", graph_dotted_line);
  2161. machine__for_each_thread(m, show_thread_runtime, &totals);
  2162. task_count = totals.task_count;
  2163. if (!task_count)
  2164. printf("<no still running tasks>\n");
  2165. printf("\nTerminated tasks:\n");
  2166. machine__for_each_thread(m, show_deadthread_runtime, &totals);
  2167. if (task_count == totals.task_count)
  2168. printf("<no terminated tasks>\n");
  2169. /* CPU idle stats not tracked when samples were skipped */
  2170. if (sched->skipped_samples && !sched->idle_hist)
  2171. return;
  2172. printf("\nIdle stats:\n");
  2173. for (i = 0; i < idle_max_cpu; ++i) {
  2174. t = idle_threads[i];
  2175. if (!t)
  2176. continue;
  2177. r = thread__priv(t);
  2178. if (r && r->run_stats.n) {
  2179. totals.sched_count += r->run_stats.n;
  2180. printf(" CPU %2d idle for ", i);
  2181. print_sched_time(r->total_run_time, 6);
  2182. printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
  2183. } else
  2184. printf(" CPU %2d idle entire time window\n", i);
  2185. }
  2186. if (sched->idle_hist && symbol_conf.use_callchain) {
  2187. callchain_param.mode = CHAIN_FOLDED;
  2188. callchain_param.value = CCVAL_PERIOD;
  2189. callchain_register_param(&callchain_param);
  2190. printf("\nIdle stats by callchain:\n");
  2191. for (i = 0; i < idle_max_cpu; ++i) {
  2192. struct idle_thread_runtime *itr;
  2193. t = idle_threads[i];
  2194. if (!t)
  2195. continue;
  2196. itr = thread__priv(t);
  2197. if (itr == NULL)
  2198. continue;
  2199. callchain_param.sort(&itr->sorted_root, &itr->callchain,
  2200. 0, &callchain_param);
  2201. printf(" CPU %2d:", i);
  2202. print_sched_time(itr->tr.total_run_time, 6);
  2203. printf(" msec\n");
  2204. timehist_print_idlehist_callchain(&itr->sorted_root);
  2205. printf("\n");
  2206. }
  2207. }
  2208. printf("\n"
  2209. " Total number of unique tasks: %" PRIu64 "\n"
  2210. "Total number of context switches: %" PRIu64 "\n",
  2211. totals.task_count, totals.sched_count);
  2212. printf(" Total run time (msec): ");
  2213. print_sched_time(totals.total_run_time, 2);
  2214. printf("\n");
  2215. printf(" Total scheduling time (msec): ");
  2216. print_sched_time(hist_time, 2);
  2217. printf(" (x %d)\n", sched->max_cpu);
  2218. }
  2219. typedef int (*sched_handler)(struct perf_tool *tool,
  2220. union perf_event *event,
  2221. struct perf_evsel *evsel,
  2222. struct perf_sample *sample,
  2223. struct machine *machine);
  2224. static int perf_timehist__process_sample(struct perf_tool *tool,
  2225. union perf_event *event,
  2226. struct perf_sample *sample,
  2227. struct perf_evsel *evsel,
  2228. struct machine *machine)
  2229. {
  2230. struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
  2231. int err = 0;
  2232. int this_cpu = sample->cpu;
  2233. if (this_cpu > sched->max_cpu)
  2234. sched->max_cpu = this_cpu;
  2235. if (evsel->handler != NULL) {
  2236. sched_handler f = evsel->handler;
  2237. err = f(tool, event, evsel, sample, machine);
  2238. }
  2239. return err;
  2240. }
  2241. static int timehist_check_attr(struct perf_sched *sched,
  2242. struct perf_evlist *evlist)
  2243. {
  2244. struct perf_evsel *evsel;
  2245. struct evsel_runtime *er;
  2246. list_for_each_entry(evsel, &evlist->entries, node) {
  2247. er = perf_evsel__get_runtime(evsel);
  2248. if (er == NULL) {
  2249. pr_err("Failed to allocate memory for evsel runtime data\n");
  2250. return -1;
  2251. }
  2252. if (sched->show_callchain &&
  2253. !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
  2254. pr_info("Samples do not have callchains.\n");
  2255. sched->show_callchain = 0;
  2256. symbol_conf.use_callchain = 0;
  2257. }
  2258. }
  2259. return 0;
  2260. }
  2261. static int perf_sched__timehist(struct perf_sched *sched)
  2262. {
  2263. const struct perf_evsel_str_handler handlers[] = {
  2264. { "sched:sched_switch", timehist_sched_switch_event, },
  2265. { "sched:sched_wakeup", timehist_sched_wakeup_event, },
  2266. { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
  2267. };
  2268. const struct perf_evsel_str_handler migrate_handlers[] = {
  2269. { "sched:sched_migrate_task", timehist_migrate_task_event, },
  2270. };
  2271. struct perf_data_file file = {
  2272. .path = input_name,
  2273. .mode = PERF_DATA_MODE_READ,
  2274. .force = sched->force,
  2275. };
  2276. struct perf_session *session;
  2277. struct perf_evlist *evlist;
  2278. int err = -1;
  2279. /*
  2280. * event handlers for timehist option
  2281. */
  2282. sched->tool.sample = perf_timehist__process_sample;
  2283. sched->tool.mmap = perf_event__process_mmap;
  2284. sched->tool.comm = perf_event__process_comm;
  2285. sched->tool.exit = perf_event__process_exit;
  2286. sched->tool.fork = perf_event__process_fork;
  2287. sched->tool.lost = process_lost;
  2288. sched->tool.attr = perf_event__process_attr;
  2289. sched->tool.tracing_data = perf_event__process_tracing_data;
  2290. sched->tool.build_id = perf_event__process_build_id;
  2291. sched->tool.ordered_events = true;
  2292. sched->tool.ordering_requires_timestamps = true;
  2293. symbol_conf.use_callchain = sched->show_callchain;
  2294. session = perf_session__new(&file, false, &sched->tool);
  2295. if (session == NULL)
  2296. return -ENOMEM;
  2297. evlist = session->evlist;
  2298. symbol__init(&session->header.env);
  2299. if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
  2300. pr_err("Invalid time string\n");
  2301. return -EINVAL;
  2302. }
  2303. if (timehist_check_attr(sched, evlist) != 0)
  2304. goto out;
  2305. setup_pager();
  2306. /* setup per-evsel handlers */
  2307. if (perf_session__set_tracepoints_handlers(session, handlers))
  2308. goto out;
  2309. /* sched_switch event at a minimum needs to exist */
  2310. if (!perf_evlist__find_tracepoint_by_name(session->evlist,
  2311. "sched:sched_switch")) {
  2312. pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
  2313. goto out;
  2314. }
  2315. if (sched->show_migrations &&
  2316. perf_session__set_tracepoints_handlers(session, migrate_handlers))
  2317. goto out;
  2318. /* pre-allocate struct for per-CPU idle stats */
  2319. sched->max_cpu = session->header.env.nr_cpus_online;
  2320. if (sched->max_cpu == 0)
  2321. sched->max_cpu = 4;
  2322. if (init_idle_threads(sched->max_cpu))
  2323. goto out;
  2324. /* summary_only implies summary option, but don't overwrite summary if set */
  2325. if (sched->summary_only)
  2326. sched->summary = sched->summary_only;
  2327. if (!sched->summary_only)
  2328. timehist_header(sched);
  2329. err = perf_session__process_events(session);
  2330. if (err) {
  2331. pr_err("Failed to process events, error %d", err);
  2332. goto out;
  2333. }
  2334. sched->nr_events = evlist->stats.nr_events[0];
  2335. sched->nr_lost_events = evlist->stats.total_lost;
  2336. sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
  2337. if (sched->summary)
  2338. timehist_print_summary(sched, session);
  2339. out:
  2340. free_idle_threads();
  2341. perf_session__delete(session);
  2342. return err;
  2343. }
  2344. static void print_bad_events(struct perf_sched *sched)
  2345. {
  2346. if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
  2347. printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
  2348. (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
  2349. sched->nr_unordered_timestamps, sched->nr_timestamps);
  2350. }
  2351. if (sched->nr_lost_events && sched->nr_events) {
  2352. printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
  2353. (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
  2354. sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
  2355. }
  2356. if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
  2357. printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
  2358. (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
  2359. sched->nr_context_switch_bugs, sched->nr_timestamps);
  2360. if (sched->nr_lost_events)
  2361. printf(" (due to lost events?)");
  2362. printf("\n");
  2363. }
  2364. }
  2365. static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
  2366. {
  2367. struct rb_node **new = &(root->rb_node), *parent = NULL;
  2368. struct work_atoms *this;
  2369. const char *comm = thread__comm_str(data->thread), *this_comm;
  2370. while (*new) {
  2371. int cmp;
  2372. this = container_of(*new, struct work_atoms, node);
  2373. parent = *new;
  2374. this_comm = thread__comm_str(this->thread);
  2375. cmp = strcmp(comm, this_comm);
  2376. if (cmp > 0) {
  2377. new = &((*new)->rb_left);
  2378. } else if (cmp < 0) {
  2379. new = &((*new)->rb_right);
  2380. } else {
  2381. this->num_merged++;
  2382. this->total_runtime += data->total_runtime;
  2383. this->nb_atoms += data->nb_atoms;
  2384. this->total_lat += data->total_lat;
  2385. list_splice(&data->work_list, &this->work_list);
  2386. if (this->max_lat < data->max_lat) {
  2387. this->max_lat = data->max_lat;
  2388. this->max_lat_at = data->max_lat_at;
  2389. }
  2390. zfree(&data);
  2391. return;
  2392. }
  2393. }
  2394. data->num_merged++;
  2395. rb_link_node(&data->node, parent, new);
  2396. rb_insert_color(&data->node, root);
  2397. }
  2398. static void perf_sched__merge_lat(struct perf_sched *sched)
  2399. {
  2400. struct work_atoms *data;
  2401. struct rb_node *node;
  2402. if (sched->skip_merge)
  2403. return;
  2404. while ((node = rb_first(&sched->atom_root))) {
  2405. rb_erase(node, &sched->atom_root);
  2406. data = rb_entry(node, struct work_atoms, node);
  2407. __merge_work_atoms(&sched->merged_atom_root, data);
  2408. }
  2409. }
  2410. static int perf_sched__lat(struct perf_sched *sched)
  2411. {
  2412. struct rb_node *next;
  2413. setup_pager();
  2414. if (perf_sched__read_events(sched))
  2415. return -1;
  2416. perf_sched__merge_lat(sched);
  2417. perf_sched__sort_lat(sched);
  2418. printf("\n -----------------------------------------------------------------------------------------------------------------\n");
  2419. printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
  2420. printf(" -----------------------------------------------------------------------------------------------------------------\n");
  2421. next = rb_first(&sched->sorted_atom_root);
  2422. while (next) {
  2423. struct work_atoms *work_list;
  2424. work_list = rb_entry(next, struct work_atoms, node);
  2425. output_lat_thread(sched, work_list);
  2426. next = rb_next(next);
  2427. thread__zput(work_list->thread);
  2428. }
  2429. printf(" -----------------------------------------------------------------------------------------------------------------\n");
  2430. printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
  2431. (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
  2432. printf(" ---------------------------------------------------\n");
  2433. print_bad_events(sched);
  2434. printf("\n");
  2435. return 0;
  2436. }
  2437. static int setup_map_cpus(struct perf_sched *sched)
  2438. {
  2439. struct cpu_map *map;
  2440. sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
  2441. if (sched->map.comp) {
  2442. sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
  2443. if (!sched->map.comp_cpus)
  2444. return -1;
  2445. }
  2446. if (!sched->map.cpus_str)
  2447. return 0;
  2448. map = cpu_map__new(sched->map.cpus_str);
  2449. if (!map) {
  2450. pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
  2451. return -1;
  2452. }
  2453. sched->map.cpus = map;
  2454. return 0;
  2455. }
  2456. static int setup_color_pids(struct perf_sched *sched)
  2457. {
  2458. struct thread_map *map;
  2459. if (!sched->map.color_pids_str)
  2460. return 0;
  2461. map = thread_map__new_by_tid_str(sched->map.color_pids_str);
  2462. if (!map) {
  2463. pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
  2464. return -1;
  2465. }
  2466. sched->map.color_pids = map;
  2467. return 0;
  2468. }
  2469. static int setup_color_cpus(struct perf_sched *sched)
  2470. {
  2471. struct cpu_map *map;
  2472. if (!sched->map.color_cpus_str)
  2473. return 0;
  2474. map = cpu_map__new(sched->map.color_cpus_str);
  2475. if (!map) {
  2476. pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
  2477. return -1;
  2478. }
  2479. sched->map.color_cpus = map;
  2480. return 0;
  2481. }
  2482. static int perf_sched__map(struct perf_sched *sched)
  2483. {
  2484. if (setup_map_cpus(sched))
  2485. return -1;
  2486. if (setup_color_pids(sched))
  2487. return -1;
  2488. if (setup_color_cpus(sched))
  2489. return -1;
  2490. setup_pager();
  2491. if (perf_sched__read_events(sched))
  2492. return -1;
  2493. print_bad_events(sched);
  2494. return 0;
  2495. }
  2496. static int perf_sched__replay(struct perf_sched *sched)
  2497. {
  2498. unsigned long i;
  2499. calibrate_run_measurement_overhead(sched);
  2500. calibrate_sleep_measurement_overhead(sched);
  2501. test_calibrations(sched);
  2502. if (perf_sched__read_events(sched))
  2503. return -1;
  2504. printf("nr_run_events: %ld\n", sched->nr_run_events);
  2505. printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
  2506. printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
  2507. if (sched->targetless_wakeups)
  2508. printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
  2509. if (sched->multitarget_wakeups)
  2510. printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
  2511. if (sched->nr_run_events_optimized)
  2512. printf("run atoms optimized: %ld\n",
  2513. sched->nr_run_events_optimized);
  2514. print_task_traces(sched);
  2515. add_cross_task_wakeups(sched);
  2516. create_tasks(sched);
  2517. printf("------------------------------------------------------------\n");
  2518. for (i = 0; i < sched->replay_repeat; i++)
  2519. run_one_test(sched);
  2520. return 0;
  2521. }
  2522. static void setup_sorting(struct perf_sched *sched, const struct option *options,
  2523. const char * const usage_msg[])
  2524. {
  2525. char *tmp, *tok, *str = strdup(sched->sort_order);
  2526. for (tok = strtok_r(str, ", ", &tmp);
  2527. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  2528. if (sort_dimension__add(tok, &sched->sort_list) < 0) {
  2529. usage_with_options_msg(usage_msg, options,
  2530. "Unknown --sort key: `%s'", tok);
  2531. }
  2532. }
  2533. free(str);
  2534. sort_dimension__add("pid", &sched->cmp_pid);
  2535. }
  2536. static int __cmd_record(int argc, const char **argv)
  2537. {
  2538. unsigned int rec_argc, i, j;
  2539. const char **rec_argv;
  2540. const char * const record_args[] = {
  2541. "record",
  2542. "-a",
  2543. "-R",
  2544. "-m", "1024",
  2545. "-c", "1",
  2546. "-e", "sched:sched_switch",
  2547. "-e", "sched:sched_stat_wait",
  2548. "-e", "sched:sched_stat_sleep",
  2549. "-e", "sched:sched_stat_iowait",
  2550. "-e", "sched:sched_stat_runtime",
  2551. "-e", "sched:sched_process_fork",
  2552. "-e", "sched:sched_wakeup",
  2553. "-e", "sched:sched_wakeup_new",
  2554. "-e", "sched:sched_migrate_task",
  2555. };
  2556. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  2557. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  2558. if (rec_argv == NULL)
  2559. return -ENOMEM;
  2560. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  2561. rec_argv[i] = strdup(record_args[i]);
  2562. for (j = 1; j < (unsigned int)argc; j++, i++)
  2563. rec_argv[i] = argv[j];
  2564. BUG_ON(i != rec_argc);
  2565. return cmd_record(i, rec_argv, NULL);
  2566. }
  2567. int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
  2568. {
  2569. const char default_sort_order[] = "avg, max, switch, runtime";
  2570. struct perf_sched sched = {
  2571. .tool = {
  2572. .sample = perf_sched__process_tracepoint_sample,
  2573. .comm = perf_event__process_comm,
  2574. .lost = perf_event__process_lost,
  2575. .fork = perf_sched__process_fork_event,
  2576. .ordered_events = true,
  2577. },
  2578. .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
  2579. .sort_list = LIST_HEAD_INIT(sched.sort_list),
  2580. .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
  2581. .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
  2582. .sort_order = default_sort_order,
  2583. .replay_repeat = 10,
  2584. .profile_cpu = -1,
  2585. .next_shortname1 = 'A',
  2586. .next_shortname2 = '0',
  2587. .skip_merge = 0,
  2588. .show_callchain = 1,
  2589. .max_stack = 5,
  2590. };
  2591. const struct option sched_options[] = {
  2592. OPT_STRING('i', "input", &input_name, "file",
  2593. "input file name"),
  2594. OPT_INCR('v', "verbose", &verbose,
  2595. "be more verbose (show symbol address, etc)"),
  2596. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  2597. "dump raw trace in ASCII"),
  2598. OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
  2599. OPT_END()
  2600. };
  2601. const struct option latency_options[] = {
  2602. OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
  2603. "sort by key(s): runtime, switch, avg, max"),
  2604. OPT_INTEGER('C', "CPU", &sched.profile_cpu,
  2605. "CPU to profile on"),
  2606. OPT_BOOLEAN('p', "pids", &sched.skip_merge,
  2607. "latency stats per pid instead of per comm"),
  2608. OPT_PARENT(sched_options)
  2609. };
  2610. const struct option replay_options[] = {
  2611. OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
  2612. "repeat the workload replay N times (-1: infinite)"),
  2613. OPT_PARENT(sched_options)
  2614. };
  2615. const struct option map_options[] = {
  2616. OPT_BOOLEAN(0, "compact", &sched.map.comp,
  2617. "map output in compact mode"),
  2618. OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
  2619. "highlight given pids in map"),
  2620. OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
  2621. "highlight given CPUs in map"),
  2622. OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
  2623. "display given CPUs in map"),
  2624. OPT_PARENT(sched_options)
  2625. };
  2626. const struct option timehist_options[] = {
  2627. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2628. "file", "vmlinux pathname"),
  2629. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  2630. "file", "kallsyms pathname"),
  2631. OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
  2632. "Display call chains if present (default on)"),
  2633. OPT_UINTEGER(0, "max-stack", &sched.max_stack,
  2634. "Maximum number of functions to display backtrace."),
  2635. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  2636. "Look for files with symbols relative to this directory"),
  2637. OPT_BOOLEAN('s', "summary", &sched.summary_only,
  2638. "Show only syscall summary with statistics"),
  2639. OPT_BOOLEAN('S', "with-summary", &sched.summary,
  2640. "Show all syscalls and summary with statistics"),
  2641. OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
  2642. OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
  2643. OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
  2644. OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
  2645. OPT_STRING(0, "time", &sched.time_str, "str",
  2646. "Time span for analysis (start,stop)"),
  2647. OPT_PARENT(sched_options)
  2648. };
  2649. const char * const latency_usage[] = {
  2650. "perf sched latency [<options>]",
  2651. NULL
  2652. };
  2653. const char * const replay_usage[] = {
  2654. "perf sched replay [<options>]",
  2655. NULL
  2656. };
  2657. const char * const map_usage[] = {
  2658. "perf sched map [<options>]",
  2659. NULL
  2660. };
  2661. const char * const timehist_usage[] = {
  2662. "perf sched timehist [<options>]",
  2663. NULL
  2664. };
  2665. const char *const sched_subcommands[] = { "record", "latency", "map",
  2666. "replay", "script",
  2667. "timehist", NULL };
  2668. const char *sched_usage[] = {
  2669. NULL,
  2670. NULL
  2671. };
  2672. struct trace_sched_handler lat_ops = {
  2673. .wakeup_event = latency_wakeup_event,
  2674. .switch_event = latency_switch_event,
  2675. .runtime_event = latency_runtime_event,
  2676. .migrate_task_event = latency_migrate_task_event,
  2677. };
  2678. struct trace_sched_handler map_ops = {
  2679. .switch_event = map_switch_event,
  2680. };
  2681. struct trace_sched_handler replay_ops = {
  2682. .wakeup_event = replay_wakeup_event,
  2683. .switch_event = replay_switch_event,
  2684. .fork_event = replay_fork_event,
  2685. };
  2686. unsigned int i;
  2687. for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
  2688. sched.curr_pid[i] = -1;
  2689. argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
  2690. sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2691. if (!argc)
  2692. usage_with_options(sched_usage, sched_options);
  2693. /*
  2694. * Aliased to 'perf script' for now:
  2695. */
  2696. if (!strcmp(argv[0], "script"))
  2697. return cmd_script(argc, argv, prefix);
  2698. if (!strncmp(argv[0], "rec", 3)) {
  2699. return __cmd_record(argc, argv);
  2700. } else if (!strncmp(argv[0], "lat", 3)) {
  2701. sched.tp_handler = &lat_ops;
  2702. if (argc > 1) {
  2703. argc = parse_options(argc, argv, latency_options, latency_usage, 0);
  2704. if (argc)
  2705. usage_with_options(latency_usage, latency_options);
  2706. }
  2707. setup_sorting(&sched, latency_options, latency_usage);
  2708. return perf_sched__lat(&sched);
  2709. } else if (!strcmp(argv[0], "map")) {
  2710. if (argc) {
  2711. argc = parse_options(argc, argv, map_options, map_usage, 0);
  2712. if (argc)
  2713. usage_with_options(map_usage, map_options);
  2714. }
  2715. sched.tp_handler = &map_ops;
  2716. setup_sorting(&sched, latency_options, latency_usage);
  2717. return perf_sched__map(&sched);
  2718. } else if (!strncmp(argv[0], "rep", 3)) {
  2719. sched.tp_handler = &replay_ops;
  2720. if (argc) {
  2721. argc = parse_options(argc, argv, replay_options, replay_usage, 0);
  2722. if (argc)
  2723. usage_with_options(replay_usage, replay_options);
  2724. }
  2725. return perf_sched__replay(&sched);
  2726. } else if (!strcmp(argv[0], "timehist")) {
  2727. if (argc) {
  2728. argc = parse_options(argc, argv, timehist_options,
  2729. timehist_usage, 0);
  2730. if (argc)
  2731. usage_with_options(timehist_usage, timehist_options);
  2732. }
  2733. if (sched.show_wakeups && sched.summary_only) {
  2734. pr_err(" Error: -s and -w are mutually exclusive.\n");
  2735. parse_options_usage(timehist_usage, timehist_options, "s", true);
  2736. parse_options_usage(NULL, timehist_options, "w", true);
  2737. return -EINVAL;
  2738. }
  2739. return perf_sched__timehist(&sched);
  2740. } else {
  2741. usage_with_options(sched_usage, sched_options);
  2742. }
  2743. return 0;
  2744. }