builtin-timechart.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. /*
  2. * builtin-timechart.c - make an svg timechart of system activity
  3. *
  4. * (C) Copyright 2009 Intel Corporation
  5. *
  6. * Authors:
  7. * Arjan van de Ven <arjan@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #include <traceevent/event-parse.h>
  15. #include "builtin.h"
  16. #include "util/util.h"
  17. #include "util/color.h"
  18. #include <linux/list.h>
  19. #include "util/cache.h"
  20. #include "util/evlist.h"
  21. #include "util/evsel.h"
  22. #include <linux/rbtree.h>
  23. #include "util/symbol.h"
  24. #include "util/callchain.h"
  25. #include "util/strlist.h"
  26. #include "perf.h"
  27. #include "util/header.h"
  28. #include "util/parse-options.h"
  29. #include "util/parse-events.h"
  30. #include "util/event.h"
  31. #include "util/session.h"
  32. #include "util/svghelper.h"
  33. #include "util/tool.h"
  34. #include "util/data.h"
  35. #include "util/debug.h"
  36. #define SUPPORT_OLD_POWER_EVENTS 1
  37. #define PWR_EVENT_EXIT -1
  38. struct per_pid;
  39. struct power_event;
  40. struct wake_event;
  41. struct timechart {
  42. struct perf_tool tool;
  43. struct per_pid *all_data;
  44. struct power_event *power_events;
  45. struct wake_event *wake_events;
  46. int proc_num;
  47. unsigned int numcpus;
  48. u64 min_freq, /* Lowest CPU frequency seen */
  49. max_freq, /* Highest CPU frequency seen */
  50. turbo_frequency,
  51. first_time, last_time;
  52. bool power_only,
  53. tasks_only,
  54. with_backtrace,
  55. topology;
  56. /* IO related settings */
  57. u64 io_events;
  58. bool io_only,
  59. skip_eagain;
  60. u64 min_time,
  61. merge_dist;
  62. };
  63. struct per_pidcomm;
  64. struct cpu_sample;
  65. struct io_sample;
  66. /*
  67. * Datastructure layout:
  68. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  69. * Each "pid" entry, has a list of "comm"s.
  70. * this is because we want to track different programs different, while
  71. * exec will reuse the original pid (by design).
  72. * Each comm has a list of samples that will be used to draw
  73. * final graph.
  74. */
  75. struct per_pid {
  76. struct per_pid *next;
  77. int pid;
  78. int ppid;
  79. u64 start_time;
  80. u64 end_time;
  81. u64 total_time;
  82. u64 total_bytes;
  83. int display;
  84. struct per_pidcomm *all;
  85. struct per_pidcomm *current;
  86. };
  87. struct per_pidcomm {
  88. struct per_pidcomm *next;
  89. u64 start_time;
  90. u64 end_time;
  91. u64 total_time;
  92. u64 max_bytes;
  93. u64 total_bytes;
  94. int Y;
  95. int display;
  96. long state;
  97. u64 state_since;
  98. char *comm;
  99. struct cpu_sample *samples;
  100. struct io_sample *io_samples;
  101. };
  102. struct sample_wrapper {
  103. struct sample_wrapper *next;
  104. u64 timestamp;
  105. unsigned char data[0];
  106. };
  107. #define TYPE_NONE 0
  108. #define TYPE_RUNNING 1
  109. #define TYPE_WAITING 2
  110. #define TYPE_BLOCKED 3
  111. struct cpu_sample {
  112. struct cpu_sample *next;
  113. u64 start_time;
  114. u64 end_time;
  115. int type;
  116. int cpu;
  117. const char *backtrace;
  118. };
  119. enum {
  120. IOTYPE_READ,
  121. IOTYPE_WRITE,
  122. IOTYPE_SYNC,
  123. IOTYPE_TX,
  124. IOTYPE_RX,
  125. IOTYPE_POLL,
  126. };
  127. struct io_sample {
  128. struct io_sample *next;
  129. u64 start_time;
  130. u64 end_time;
  131. u64 bytes;
  132. int type;
  133. int fd;
  134. int err;
  135. int merges;
  136. };
  137. #define CSTATE 1
  138. #define PSTATE 2
  139. struct power_event {
  140. struct power_event *next;
  141. int type;
  142. int state;
  143. u64 start_time;
  144. u64 end_time;
  145. int cpu;
  146. };
  147. struct wake_event {
  148. struct wake_event *next;
  149. int waker;
  150. int wakee;
  151. u64 time;
  152. const char *backtrace;
  153. };
  154. struct process_filter {
  155. char *name;
  156. int pid;
  157. struct process_filter *next;
  158. };
  159. static struct process_filter *process_filter;
  160. static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
  161. {
  162. struct per_pid *cursor = tchart->all_data;
  163. while (cursor) {
  164. if (cursor->pid == pid)
  165. return cursor;
  166. cursor = cursor->next;
  167. }
  168. cursor = zalloc(sizeof(*cursor));
  169. assert(cursor != NULL);
  170. cursor->pid = pid;
  171. cursor->next = tchart->all_data;
  172. tchart->all_data = cursor;
  173. return cursor;
  174. }
  175. static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
  176. {
  177. struct per_pid *p;
  178. struct per_pidcomm *c;
  179. p = find_create_pid(tchart, pid);
  180. c = p->all;
  181. while (c) {
  182. if (c->comm && strcmp(c->comm, comm) == 0) {
  183. p->current = c;
  184. return;
  185. }
  186. if (!c->comm) {
  187. c->comm = strdup(comm);
  188. p->current = c;
  189. return;
  190. }
  191. c = c->next;
  192. }
  193. c = zalloc(sizeof(*c));
  194. assert(c != NULL);
  195. c->comm = strdup(comm);
  196. p->current = c;
  197. c->next = p->all;
  198. p->all = c;
  199. }
  200. static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
  201. {
  202. struct per_pid *p, *pp;
  203. p = find_create_pid(tchart, pid);
  204. pp = find_create_pid(tchart, ppid);
  205. p->ppid = ppid;
  206. if (pp->current && pp->current->comm && !p->current)
  207. pid_set_comm(tchart, pid, pp->current->comm);
  208. p->start_time = timestamp;
  209. if (p->current && !p->current->start_time) {
  210. p->current->start_time = timestamp;
  211. p->current->state_since = timestamp;
  212. }
  213. }
  214. static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
  215. {
  216. struct per_pid *p;
  217. p = find_create_pid(tchart, pid);
  218. p->end_time = timestamp;
  219. if (p->current)
  220. p->current->end_time = timestamp;
  221. }
  222. static void pid_put_sample(struct timechart *tchart, int pid, int type,
  223. unsigned int cpu, u64 start, u64 end,
  224. const char *backtrace)
  225. {
  226. struct per_pid *p;
  227. struct per_pidcomm *c;
  228. struct cpu_sample *sample;
  229. p = find_create_pid(tchart, pid);
  230. c = p->current;
  231. if (!c) {
  232. c = zalloc(sizeof(*c));
  233. assert(c != NULL);
  234. p->current = c;
  235. c->next = p->all;
  236. p->all = c;
  237. }
  238. sample = zalloc(sizeof(*sample));
  239. assert(sample != NULL);
  240. sample->start_time = start;
  241. sample->end_time = end;
  242. sample->type = type;
  243. sample->next = c->samples;
  244. sample->cpu = cpu;
  245. sample->backtrace = backtrace;
  246. c->samples = sample;
  247. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  248. c->total_time += (end-start);
  249. p->total_time += (end-start);
  250. }
  251. if (c->start_time == 0 || c->start_time > start)
  252. c->start_time = start;
  253. if (p->start_time == 0 || p->start_time > start)
  254. p->start_time = start;
  255. }
  256. #define MAX_CPUS 4096
  257. static u64 cpus_cstate_start_times[MAX_CPUS];
  258. static int cpus_cstate_state[MAX_CPUS];
  259. static u64 cpus_pstate_start_times[MAX_CPUS];
  260. static u64 cpus_pstate_state[MAX_CPUS];
  261. static int process_comm_event(struct perf_tool *tool,
  262. union perf_event *event,
  263. struct perf_sample *sample __maybe_unused,
  264. struct machine *machine __maybe_unused)
  265. {
  266. struct timechart *tchart = container_of(tool, struct timechart, tool);
  267. pid_set_comm(tchart, event->comm.tid, event->comm.comm);
  268. return 0;
  269. }
  270. static int process_fork_event(struct perf_tool *tool,
  271. union perf_event *event,
  272. struct perf_sample *sample __maybe_unused,
  273. struct machine *machine __maybe_unused)
  274. {
  275. struct timechart *tchart = container_of(tool, struct timechart, tool);
  276. pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
  277. return 0;
  278. }
  279. static int process_exit_event(struct perf_tool *tool,
  280. union perf_event *event,
  281. struct perf_sample *sample __maybe_unused,
  282. struct machine *machine __maybe_unused)
  283. {
  284. struct timechart *tchart = container_of(tool, struct timechart, tool);
  285. pid_exit(tchart, event->fork.pid, event->fork.time);
  286. return 0;
  287. }
  288. #ifdef SUPPORT_OLD_POWER_EVENTS
  289. static int use_old_power_events;
  290. #endif
  291. static void c_state_start(int cpu, u64 timestamp, int state)
  292. {
  293. cpus_cstate_start_times[cpu] = timestamp;
  294. cpus_cstate_state[cpu] = state;
  295. }
  296. static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
  297. {
  298. struct power_event *pwr = zalloc(sizeof(*pwr));
  299. if (!pwr)
  300. return;
  301. pwr->state = cpus_cstate_state[cpu];
  302. pwr->start_time = cpus_cstate_start_times[cpu];
  303. pwr->end_time = timestamp;
  304. pwr->cpu = cpu;
  305. pwr->type = CSTATE;
  306. pwr->next = tchart->power_events;
  307. tchart->power_events = pwr;
  308. }
  309. static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
  310. {
  311. struct power_event *pwr;
  312. if (new_freq > 8000000) /* detect invalid data */
  313. return;
  314. pwr = zalloc(sizeof(*pwr));
  315. if (!pwr)
  316. return;
  317. pwr->state = cpus_pstate_state[cpu];
  318. pwr->start_time = cpus_pstate_start_times[cpu];
  319. pwr->end_time = timestamp;
  320. pwr->cpu = cpu;
  321. pwr->type = PSTATE;
  322. pwr->next = tchart->power_events;
  323. if (!pwr->start_time)
  324. pwr->start_time = tchart->first_time;
  325. tchart->power_events = pwr;
  326. cpus_pstate_state[cpu] = new_freq;
  327. cpus_pstate_start_times[cpu] = timestamp;
  328. if ((u64)new_freq > tchart->max_freq)
  329. tchart->max_freq = new_freq;
  330. if (new_freq < tchart->min_freq || tchart->min_freq == 0)
  331. tchart->min_freq = new_freq;
  332. if (new_freq == tchart->max_freq - 1000)
  333. tchart->turbo_frequency = tchart->max_freq;
  334. }
  335. static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
  336. int waker, int wakee, u8 flags, const char *backtrace)
  337. {
  338. struct per_pid *p;
  339. struct wake_event *we = zalloc(sizeof(*we));
  340. if (!we)
  341. return;
  342. we->time = timestamp;
  343. we->waker = waker;
  344. we->backtrace = backtrace;
  345. if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
  346. we->waker = -1;
  347. we->wakee = wakee;
  348. we->next = tchart->wake_events;
  349. tchart->wake_events = we;
  350. p = find_create_pid(tchart, we->wakee);
  351. if (p && p->current && p->current->state == TYPE_NONE) {
  352. p->current->state_since = timestamp;
  353. p->current->state = TYPE_WAITING;
  354. }
  355. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  356. pid_put_sample(tchart, p->pid, p->current->state, cpu,
  357. p->current->state_since, timestamp, NULL);
  358. p->current->state_since = timestamp;
  359. p->current->state = TYPE_WAITING;
  360. }
  361. }
  362. static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
  363. int prev_pid, int next_pid, u64 prev_state,
  364. const char *backtrace)
  365. {
  366. struct per_pid *p = NULL, *prev_p;
  367. prev_p = find_create_pid(tchart, prev_pid);
  368. p = find_create_pid(tchart, next_pid);
  369. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  370. pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
  371. prev_p->current->state_since, timestamp,
  372. backtrace);
  373. if (p && p->current) {
  374. if (p->current->state != TYPE_NONE)
  375. pid_put_sample(tchart, next_pid, p->current->state, cpu,
  376. p->current->state_since, timestamp,
  377. backtrace);
  378. p->current->state_since = timestamp;
  379. p->current->state = TYPE_RUNNING;
  380. }
  381. if (prev_p->current) {
  382. prev_p->current->state = TYPE_NONE;
  383. prev_p->current->state_since = timestamp;
  384. if (prev_state & 2)
  385. prev_p->current->state = TYPE_BLOCKED;
  386. if (prev_state == 0)
  387. prev_p->current->state = TYPE_WAITING;
  388. }
  389. }
  390. static const char *cat_backtrace(union perf_event *event,
  391. struct perf_sample *sample,
  392. struct machine *machine)
  393. {
  394. struct addr_location al;
  395. unsigned int i;
  396. char *p = NULL;
  397. size_t p_len;
  398. u8 cpumode = PERF_RECORD_MISC_USER;
  399. struct addr_location tal;
  400. struct ip_callchain *chain = sample->callchain;
  401. FILE *f = open_memstream(&p, &p_len);
  402. if (!f) {
  403. perror("open_memstream error");
  404. return NULL;
  405. }
  406. if (!chain)
  407. goto exit;
  408. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  409. fprintf(stderr, "problem processing %d event, skipping it.\n",
  410. event->header.type);
  411. goto exit;
  412. }
  413. for (i = 0; i < chain->nr; i++) {
  414. u64 ip;
  415. if (callchain_param.order == ORDER_CALLEE)
  416. ip = chain->ips[i];
  417. else
  418. ip = chain->ips[chain->nr - i - 1];
  419. if (ip >= PERF_CONTEXT_MAX) {
  420. switch (ip) {
  421. case PERF_CONTEXT_HV:
  422. cpumode = PERF_RECORD_MISC_HYPERVISOR;
  423. break;
  424. case PERF_CONTEXT_KERNEL:
  425. cpumode = PERF_RECORD_MISC_KERNEL;
  426. break;
  427. case PERF_CONTEXT_USER:
  428. cpumode = PERF_RECORD_MISC_USER;
  429. break;
  430. default:
  431. pr_debug("invalid callchain context: "
  432. "%"PRId64"\n", (s64) ip);
  433. /*
  434. * It seems the callchain is corrupted.
  435. * Discard all.
  436. */
  437. zfree(&p);
  438. goto exit;
  439. }
  440. continue;
  441. }
  442. tal.filtered = 0;
  443. thread__find_addr_location(al.thread, cpumode,
  444. MAP__FUNCTION, ip, &tal);
  445. if (tal.sym)
  446. fprintf(f, "..... %016" PRIx64 " %s\n", ip,
  447. tal.sym->name);
  448. else
  449. fprintf(f, "..... %016" PRIx64 "\n", ip);
  450. }
  451. exit:
  452. fclose(f);
  453. return p;
  454. }
  455. typedef int (*tracepoint_handler)(struct timechart *tchart,
  456. struct perf_evsel *evsel,
  457. struct perf_sample *sample,
  458. const char *backtrace);
  459. static int process_sample_event(struct perf_tool *tool,
  460. union perf_event *event,
  461. struct perf_sample *sample,
  462. struct perf_evsel *evsel,
  463. struct machine *machine)
  464. {
  465. struct timechart *tchart = container_of(tool, struct timechart, tool);
  466. if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
  467. if (!tchart->first_time || tchart->first_time > sample->time)
  468. tchart->first_time = sample->time;
  469. if (tchart->last_time < sample->time)
  470. tchart->last_time = sample->time;
  471. }
  472. if (evsel->handler != NULL) {
  473. tracepoint_handler f = evsel->handler;
  474. return f(tchart, evsel, sample,
  475. cat_backtrace(event, sample, machine));
  476. }
  477. return 0;
  478. }
  479. static int
  480. process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
  481. struct perf_evsel *evsel,
  482. struct perf_sample *sample,
  483. const char *backtrace __maybe_unused)
  484. {
  485. u32 state = perf_evsel__intval(evsel, sample, "state");
  486. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  487. if (state == (u32)PWR_EVENT_EXIT)
  488. c_state_end(tchart, cpu_id, sample->time);
  489. else
  490. c_state_start(cpu_id, sample->time, state);
  491. return 0;
  492. }
  493. static int
  494. process_sample_cpu_frequency(struct timechart *tchart,
  495. struct perf_evsel *evsel,
  496. struct perf_sample *sample,
  497. const char *backtrace __maybe_unused)
  498. {
  499. u32 state = perf_evsel__intval(evsel, sample, "state");
  500. u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  501. p_state_change(tchart, cpu_id, sample->time, state);
  502. return 0;
  503. }
  504. static int
  505. process_sample_sched_wakeup(struct timechart *tchart,
  506. struct perf_evsel *evsel,
  507. struct perf_sample *sample,
  508. const char *backtrace)
  509. {
  510. u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
  511. int waker = perf_evsel__intval(evsel, sample, "common_pid");
  512. int wakee = perf_evsel__intval(evsel, sample, "pid");
  513. sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
  514. return 0;
  515. }
  516. static int
  517. process_sample_sched_switch(struct timechart *tchart,
  518. struct perf_evsel *evsel,
  519. struct perf_sample *sample,
  520. const char *backtrace)
  521. {
  522. int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
  523. int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
  524. u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
  525. sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
  526. prev_state, backtrace);
  527. return 0;
  528. }
  529. #ifdef SUPPORT_OLD_POWER_EVENTS
  530. static int
  531. process_sample_power_start(struct timechart *tchart __maybe_unused,
  532. struct perf_evsel *evsel,
  533. struct perf_sample *sample,
  534. const char *backtrace __maybe_unused)
  535. {
  536. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  537. u64 value = perf_evsel__intval(evsel, sample, "value");
  538. c_state_start(cpu_id, sample->time, value);
  539. return 0;
  540. }
  541. static int
  542. process_sample_power_end(struct timechart *tchart,
  543. struct perf_evsel *evsel __maybe_unused,
  544. struct perf_sample *sample,
  545. const char *backtrace __maybe_unused)
  546. {
  547. c_state_end(tchart, sample->cpu, sample->time);
  548. return 0;
  549. }
  550. static int
  551. process_sample_power_frequency(struct timechart *tchart,
  552. struct perf_evsel *evsel,
  553. struct perf_sample *sample,
  554. const char *backtrace __maybe_unused)
  555. {
  556. u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
  557. u64 value = perf_evsel__intval(evsel, sample, "value");
  558. p_state_change(tchart, cpu_id, sample->time, value);
  559. return 0;
  560. }
  561. #endif /* SUPPORT_OLD_POWER_EVENTS */
  562. /*
  563. * After the last sample we need to wrap up the current C/P state
  564. * and close out each CPU for these.
  565. */
  566. static void end_sample_processing(struct timechart *tchart)
  567. {
  568. u64 cpu;
  569. struct power_event *pwr;
  570. for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
  571. /* C state */
  572. #if 0
  573. pwr = zalloc(sizeof(*pwr));
  574. if (!pwr)
  575. return;
  576. pwr->state = cpus_cstate_state[cpu];
  577. pwr->start_time = cpus_cstate_start_times[cpu];
  578. pwr->end_time = tchart->last_time;
  579. pwr->cpu = cpu;
  580. pwr->type = CSTATE;
  581. pwr->next = tchart->power_events;
  582. tchart->power_events = pwr;
  583. #endif
  584. /* P state */
  585. pwr = zalloc(sizeof(*pwr));
  586. if (!pwr)
  587. return;
  588. pwr->state = cpus_pstate_state[cpu];
  589. pwr->start_time = cpus_pstate_start_times[cpu];
  590. pwr->end_time = tchart->last_time;
  591. pwr->cpu = cpu;
  592. pwr->type = PSTATE;
  593. pwr->next = tchart->power_events;
  594. if (!pwr->start_time)
  595. pwr->start_time = tchart->first_time;
  596. if (!pwr->state)
  597. pwr->state = tchart->min_freq;
  598. tchart->power_events = pwr;
  599. }
  600. }
  601. static int pid_begin_io_sample(struct timechart *tchart, int pid, int type,
  602. u64 start, int fd)
  603. {
  604. struct per_pid *p = find_create_pid(tchart, pid);
  605. struct per_pidcomm *c = p->current;
  606. struct io_sample *sample;
  607. struct io_sample *prev;
  608. if (!c) {
  609. c = zalloc(sizeof(*c));
  610. if (!c)
  611. return -ENOMEM;
  612. p->current = c;
  613. c->next = p->all;
  614. p->all = c;
  615. }
  616. prev = c->io_samples;
  617. if (prev && prev->start_time && !prev->end_time) {
  618. pr_warning("Skip invalid start event: "
  619. "previous event already started!\n");
  620. /* remove previous event that has been started,
  621. * we are not sure we will ever get an end for it */
  622. c->io_samples = prev->next;
  623. free(prev);
  624. return 0;
  625. }
  626. sample = zalloc(sizeof(*sample));
  627. if (!sample)
  628. return -ENOMEM;
  629. sample->start_time = start;
  630. sample->type = type;
  631. sample->fd = fd;
  632. sample->next = c->io_samples;
  633. c->io_samples = sample;
  634. if (c->start_time == 0 || c->start_time > start)
  635. c->start_time = start;
  636. return 0;
  637. }
  638. static int pid_end_io_sample(struct timechart *tchart, int pid, int type,
  639. u64 end, long ret)
  640. {
  641. struct per_pid *p = find_create_pid(tchart, pid);
  642. struct per_pidcomm *c = p->current;
  643. struct io_sample *sample, *prev;
  644. if (!c) {
  645. pr_warning("Invalid pidcomm!\n");
  646. return -1;
  647. }
  648. sample = c->io_samples;
  649. if (!sample) /* skip partially captured events */
  650. return 0;
  651. if (sample->end_time) {
  652. pr_warning("Skip invalid end event: "
  653. "previous event already ended!\n");
  654. return 0;
  655. }
  656. if (sample->type != type) {
  657. pr_warning("Skip invalid end event: invalid event type!\n");
  658. return 0;
  659. }
  660. sample->end_time = end;
  661. prev = sample->next;
  662. /* we want to be able to see small and fast transfers, so make them
  663. * at least min_time long, but don't overlap them */
  664. if (sample->end_time - sample->start_time < tchart->min_time)
  665. sample->end_time = sample->start_time + tchart->min_time;
  666. if (prev && sample->start_time < prev->end_time) {
  667. if (prev->err) /* try to make errors more visible */
  668. sample->start_time = prev->end_time;
  669. else
  670. prev->end_time = sample->start_time;
  671. }
  672. if (ret < 0) {
  673. sample->err = ret;
  674. } else if (type == IOTYPE_READ || type == IOTYPE_WRITE ||
  675. type == IOTYPE_TX || type == IOTYPE_RX) {
  676. if ((u64)ret > c->max_bytes)
  677. c->max_bytes = ret;
  678. c->total_bytes += ret;
  679. p->total_bytes += ret;
  680. sample->bytes = ret;
  681. }
  682. /* merge two requests to make svg smaller and render-friendly */
  683. if (prev &&
  684. prev->type == sample->type &&
  685. prev->err == sample->err &&
  686. prev->fd == sample->fd &&
  687. prev->end_time + tchart->merge_dist >= sample->start_time) {
  688. sample->bytes += prev->bytes;
  689. sample->merges += prev->merges + 1;
  690. sample->start_time = prev->start_time;
  691. sample->next = prev->next;
  692. free(prev);
  693. if (!sample->err && sample->bytes > c->max_bytes)
  694. c->max_bytes = sample->bytes;
  695. }
  696. tchart->io_events++;
  697. return 0;
  698. }
  699. static int
  700. process_enter_read(struct timechart *tchart,
  701. struct perf_evsel *evsel,
  702. struct perf_sample *sample)
  703. {
  704. long fd = perf_evsel__intval(evsel, sample, "fd");
  705. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_READ,
  706. sample->time, fd);
  707. }
  708. static int
  709. process_exit_read(struct timechart *tchart,
  710. struct perf_evsel *evsel,
  711. struct perf_sample *sample)
  712. {
  713. long ret = perf_evsel__intval(evsel, sample, "ret");
  714. return pid_end_io_sample(tchart, sample->tid, IOTYPE_READ,
  715. sample->time, ret);
  716. }
  717. static int
  718. process_enter_write(struct timechart *tchart,
  719. struct perf_evsel *evsel,
  720. struct perf_sample *sample)
  721. {
  722. long fd = perf_evsel__intval(evsel, sample, "fd");
  723. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_WRITE,
  724. sample->time, fd);
  725. }
  726. static int
  727. process_exit_write(struct timechart *tchart,
  728. struct perf_evsel *evsel,
  729. struct perf_sample *sample)
  730. {
  731. long ret = perf_evsel__intval(evsel, sample, "ret");
  732. return pid_end_io_sample(tchart, sample->tid, IOTYPE_WRITE,
  733. sample->time, ret);
  734. }
  735. static int
  736. process_enter_sync(struct timechart *tchart,
  737. struct perf_evsel *evsel,
  738. struct perf_sample *sample)
  739. {
  740. long fd = perf_evsel__intval(evsel, sample, "fd");
  741. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_SYNC,
  742. sample->time, fd);
  743. }
  744. static int
  745. process_exit_sync(struct timechart *tchart,
  746. struct perf_evsel *evsel,
  747. struct perf_sample *sample)
  748. {
  749. long ret = perf_evsel__intval(evsel, sample, "ret");
  750. return pid_end_io_sample(tchart, sample->tid, IOTYPE_SYNC,
  751. sample->time, ret);
  752. }
  753. static int
  754. process_enter_tx(struct timechart *tchart,
  755. struct perf_evsel *evsel,
  756. struct perf_sample *sample)
  757. {
  758. long fd = perf_evsel__intval(evsel, sample, "fd");
  759. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_TX,
  760. sample->time, fd);
  761. }
  762. static int
  763. process_exit_tx(struct timechart *tchart,
  764. struct perf_evsel *evsel,
  765. struct perf_sample *sample)
  766. {
  767. long ret = perf_evsel__intval(evsel, sample, "ret");
  768. return pid_end_io_sample(tchart, sample->tid, IOTYPE_TX,
  769. sample->time, ret);
  770. }
  771. static int
  772. process_enter_rx(struct timechart *tchart,
  773. struct perf_evsel *evsel,
  774. struct perf_sample *sample)
  775. {
  776. long fd = perf_evsel__intval(evsel, sample, "fd");
  777. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_RX,
  778. sample->time, fd);
  779. }
  780. static int
  781. process_exit_rx(struct timechart *tchart,
  782. struct perf_evsel *evsel,
  783. struct perf_sample *sample)
  784. {
  785. long ret = perf_evsel__intval(evsel, sample, "ret");
  786. return pid_end_io_sample(tchart, sample->tid, IOTYPE_RX,
  787. sample->time, ret);
  788. }
  789. static int
  790. process_enter_poll(struct timechart *tchart,
  791. struct perf_evsel *evsel,
  792. struct perf_sample *sample)
  793. {
  794. long fd = perf_evsel__intval(evsel, sample, "fd");
  795. return pid_begin_io_sample(tchart, sample->tid, IOTYPE_POLL,
  796. sample->time, fd);
  797. }
  798. static int
  799. process_exit_poll(struct timechart *tchart,
  800. struct perf_evsel *evsel,
  801. struct perf_sample *sample)
  802. {
  803. long ret = perf_evsel__intval(evsel, sample, "ret");
  804. return pid_end_io_sample(tchart, sample->tid, IOTYPE_POLL,
  805. sample->time, ret);
  806. }
  807. /*
  808. * Sort the pid datastructure
  809. */
  810. static void sort_pids(struct timechart *tchart)
  811. {
  812. struct per_pid *new_list, *p, *cursor, *prev;
  813. /* sort by ppid first, then by pid, lowest to highest */
  814. new_list = NULL;
  815. while (tchart->all_data) {
  816. p = tchart->all_data;
  817. tchart->all_data = p->next;
  818. p->next = NULL;
  819. if (new_list == NULL) {
  820. new_list = p;
  821. p->next = NULL;
  822. continue;
  823. }
  824. prev = NULL;
  825. cursor = new_list;
  826. while (cursor) {
  827. if (cursor->ppid > p->ppid ||
  828. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  829. /* must insert before */
  830. if (prev) {
  831. p->next = prev->next;
  832. prev->next = p;
  833. cursor = NULL;
  834. continue;
  835. } else {
  836. p->next = new_list;
  837. new_list = p;
  838. cursor = NULL;
  839. continue;
  840. }
  841. }
  842. prev = cursor;
  843. cursor = cursor->next;
  844. if (!cursor)
  845. prev->next = p;
  846. }
  847. }
  848. tchart->all_data = new_list;
  849. }
  850. static void draw_c_p_states(struct timechart *tchart)
  851. {
  852. struct power_event *pwr;
  853. pwr = tchart->power_events;
  854. /*
  855. * two pass drawing so that the P state bars are on top of the C state blocks
  856. */
  857. while (pwr) {
  858. if (pwr->type == CSTATE)
  859. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  860. pwr = pwr->next;
  861. }
  862. pwr = tchart->power_events;
  863. while (pwr) {
  864. if (pwr->type == PSTATE) {
  865. if (!pwr->state)
  866. pwr->state = tchart->min_freq;
  867. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  868. }
  869. pwr = pwr->next;
  870. }
  871. }
  872. static void draw_wakeups(struct timechart *tchart)
  873. {
  874. struct wake_event *we;
  875. struct per_pid *p;
  876. struct per_pidcomm *c;
  877. we = tchart->wake_events;
  878. while (we) {
  879. int from = 0, to = 0;
  880. char *task_from = NULL, *task_to = NULL;
  881. /* locate the column of the waker and wakee */
  882. p = tchart->all_data;
  883. while (p) {
  884. if (p->pid == we->waker || p->pid == we->wakee) {
  885. c = p->all;
  886. while (c) {
  887. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  888. if (p->pid == we->waker && !from) {
  889. from = c->Y;
  890. task_from = strdup(c->comm);
  891. }
  892. if (p->pid == we->wakee && !to) {
  893. to = c->Y;
  894. task_to = strdup(c->comm);
  895. }
  896. }
  897. c = c->next;
  898. }
  899. c = p->all;
  900. while (c) {
  901. if (p->pid == we->waker && !from) {
  902. from = c->Y;
  903. task_from = strdup(c->comm);
  904. }
  905. if (p->pid == we->wakee && !to) {
  906. to = c->Y;
  907. task_to = strdup(c->comm);
  908. }
  909. c = c->next;
  910. }
  911. }
  912. p = p->next;
  913. }
  914. if (!task_from) {
  915. task_from = malloc(40);
  916. sprintf(task_from, "[%i]", we->waker);
  917. }
  918. if (!task_to) {
  919. task_to = malloc(40);
  920. sprintf(task_to, "[%i]", we->wakee);
  921. }
  922. if (we->waker == -1)
  923. svg_interrupt(we->time, to, we->backtrace);
  924. else if (from && to && abs(from - to) == 1)
  925. svg_wakeline(we->time, from, to, we->backtrace);
  926. else
  927. svg_partial_wakeline(we->time, from, task_from, to,
  928. task_to, we->backtrace);
  929. we = we->next;
  930. free(task_from);
  931. free(task_to);
  932. }
  933. }
  934. static void draw_cpu_usage(struct timechart *tchart)
  935. {
  936. struct per_pid *p;
  937. struct per_pidcomm *c;
  938. struct cpu_sample *sample;
  939. p = tchart->all_data;
  940. while (p) {
  941. c = p->all;
  942. while (c) {
  943. sample = c->samples;
  944. while (sample) {
  945. if (sample->type == TYPE_RUNNING) {
  946. svg_process(sample->cpu,
  947. sample->start_time,
  948. sample->end_time,
  949. p->pid,
  950. c->comm,
  951. sample->backtrace);
  952. }
  953. sample = sample->next;
  954. }
  955. c = c->next;
  956. }
  957. p = p->next;
  958. }
  959. }
  960. static void draw_io_bars(struct timechart *tchart)
  961. {
  962. const char *suf;
  963. double bytes;
  964. char comm[256];
  965. struct per_pid *p;
  966. struct per_pidcomm *c;
  967. struct io_sample *sample;
  968. int Y = 1;
  969. p = tchart->all_data;
  970. while (p) {
  971. c = p->all;
  972. while (c) {
  973. if (!c->display) {
  974. c->Y = 0;
  975. c = c->next;
  976. continue;
  977. }
  978. svg_box(Y, c->start_time, c->end_time, "process3");
  979. sample = c->io_samples;
  980. for (sample = c->io_samples; sample; sample = sample->next) {
  981. double h = (double)sample->bytes / c->max_bytes;
  982. if (tchart->skip_eagain &&
  983. sample->err == -EAGAIN)
  984. continue;
  985. if (sample->err)
  986. h = 1;
  987. if (sample->type == IOTYPE_SYNC)
  988. svg_fbox(Y,
  989. sample->start_time,
  990. sample->end_time,
  991. 1,
  992. sample->err ? "error" : "sync",
  993. sample->fd,
  994. sample->err,
  995. sample->merges);
  996. else if (sample->type == IOTYPE_POLL)
  997. svg_fbox(Y,
  998. sample->start_time,
  999. sample->end_time,
  1000. 1,
  1001. sample->err ? "error" : "poll",
  1002. sample->fd,
  1003. sample->err,
  1004. sample->merges);
  1005. else if (sample->type == IOTYPE_READ)
  1006. svg_ubox(Y,
  1007. sample->start_time,
  1008. sample->end_time,
  1009. h,
  1010. sample->err ? "error" : "disk",
  1011. sample->fd,
  1012. sample->err,
  1013. sample->merges);
  1014. else if (sample->type == IOTYPE_WRITE)
  1015. svg_lbox(Y,
  1016. sample->start_time,
  1017. sample->end_time,
  1018. h,
  1019. sample->err ? "error" : "disk",
  1020. sample->fd,
  1021. sample->err,
  1022. sample->merges);
  1023. else if (sample->type == IOTYPE_RX)
  1024. svg_ubox(Y,
  1025. sample->start_time,
  1026. sample->end_time,
  1027. h,
  1028. sample->err ? "error" : "net",
  1029. sample->fd,
  1030. sample->err,
  1031. sample->merges);
  1032. else if (sample->type == IOTYPE_TX)
  1033. svg_lbox(Y,
  1034. sample->start_time,
  1035. sample->end_time,
  1036. h,
  1037. sample->err ? "error" : "net",
  1038. sample->fd,
  1039. sample->err,
  1040. sample->merges);
  1041. }
  1042. suf = "";
  1043. bytes = c->total_bytes;
  1044. if (bytes > 1024) {
  1045. bytes = bytes / 1024;
  1046. suf = "K";
  1047. }
  1048. if (bytes > 1024) {
  1049. bytes = bytes / 1024;
  1050. suf = "M";
  1051. }
  1052. if (bytes > 1024) {
  1053. bytes = bytes / 1024;
  1054. suf = "G";
  1055. }
  1056. sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
  1057. svg_text(Y, c->start_time, comm);
  1058. c->Y = Y;
  1059. Y++;
  1060. c = c->next;
  1061. }
  1062. p = p->next;
  1063. }
  1064. }
  1065. static void draw_process_bars(struct timechart *tchart)
  1066. {
  1067. struct per_pid *p;
  1068. struct per_pidcomm *c;
  1069. struct cpu_sample *sample;
  1070. int Y = 0;
  1071. Y = 2 * tchart->numcpus + 2;
  1072. p = tchart->all_data;
  1073. while (p) {
  1074. c = p->all;
  1075. while (c) {
  1076. if (!c->display) {
  1077. c->Y = 0;
  1078. c = c->next;
  1079. continue;
  1080. }
  1081. svg_box(Y, c->start_time, c->end_time, "process");
  1082. sample = c->samples;
  1083. while (sample) {
  1084. if (sample->type == TYPE_RUNNING)
  1085. svg_running(Y, sample->cpu,
  1086. sample->start_time,
  1087. sample->end_time,
  1088. sample->backtrace);
  1089. if (sample->type == TYPE_BLOCKED)
  1090. svg_blocked(Y, sample->cpu,
  1091. sample->start_time,
  1092. sample->end_time,
  1093. sample->backtrace);
  1094. if (sample->type == TYPE_WAITING)
  1095. svg_waiting(Y, sample->cpu,
  1096. sample->start_time,
  1097. sample->end_time,
  1098. sample->backtrace);
  1099. sample = sample->next;
  1100. }
  1101. if (c->comm) {
  1102. char comm[256];
  1103. if (c->total_time > 5000000000) /* 5 seconds */
  1104. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  1105. else
  1106. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  1107. svg_text(Y, c->start_time, comm);
  1108. }
  1109. c->Y = Y;
  1110. Y++;
  1111. c = c->next;
  1112. }
  1113. p = p->next;
  1114. }
  1115. }
  1116. static void add_process_filter(const char *string)
  1117. {
  1118. int pid = strtoull(string, NULL, 10);
  1119. struct process_filter *filt = malloc(sizeof(*filt));
  1120. if (!filt)
  1121. return;
  1122. filt->name = strdup(string);
  1123. filt->pid = pid;
  1124. filt->next = process_filter;
  1125. process_filter = filt;
  1126. }
  1127. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  1128. {
  1129. struct process_filter *filt;
  1130. if (!process_filter)
  1131. return 1;
  1132. filt = process_filter;
  1133. while (filt) {
  1134. if (filt->pid && p->pid == filt->pid)
  1135. return 1;
  1136. if (strcmp(filt->name, c->comm) == 0)
  1137. return 1;
  1138. filt = filt->next;
  1139. }
  1140. return 0;
  1141. }
  1142. static int determine_display_tasks_filtered(struct timechart *tchart)
  1143. {
  1144. struct per_pid *p;
  1145. struct per_pidcomm *c;
  1146. int count = 0;
  1147. p = tchart->all_data;
  1148. while (p) {
  1149. p->display = 0;
  1150. if (p->start_time == 1)
  1151. p->start_time = tchart->first_time;
  1152. /* no exit marker, task kept running to the end */
  1153. if (p->end_time == 0)
  1154. p->end_time = tchart->last_time;
  1155. c = p->all;
  1156. while (c) {
  1157. c->display = 0;
  1158. if (c->start_time == 1)
  1159. c->start_time = tchart->first_time;
  1160. if (passes_filter(p, c)) {
  1161. c->display = 1;
  1162. p->display = 1;
  1163. count++;
  1164. }
  1165. if (c->end_time == 0)
  1166. c->end_time = tchart->last_time;
  1167. c = c->next;
  1168. }
  1169. p = p->next;
  1170. }
  1171. return count;
  1172. }
  1173. static int determine_display_tasks(struct timechart *tchart, u64 threshold)
  1174. {
  1175. struct per_pid *p;
  1176. struct per_pidcomm *c;
  1177. int count = 0;
  1178. p = tchart->all_data;
  1179. while (p) {
  1180. p->display = 0;
  1181. if (p->start_time == 1)
  1182. p->start_time = tchart->first_time;
  1183. /* no exit marker, task kept running to the end */
  1184. if (p->end_time == 0)
  1185. p->end_time = tchart->last_time;
  1186. if (p->total_time >= threshold)
  1187. p->display = 1;
  1188. c = p->all;
  1189. while (c) {
  1190. c->display = 0;
  1191. if (c->start_time == 1)
  1192. c->start_time = tchart->first_time;
  1193. if (c->total_time >= threshold) {
  1194. c->display = 1;
  1195. count++;
  1196. }
  1197. if (c->end_time == 0)
  1198. c->end_time = tchart->last_time;
  1199. c = c->next;
  1200. }
  1201. p = p->next;
  1202. }
  1203. return count;
  1204. }
  1205. static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
  1206. {
  1207. struct per_pid *p;
  1208. struct per_pidcomm *c;
  1209. int count = 0;
  1210. p = timechart->all_data;
  1211. while (p) {
  1212. /* no exit marker, task kept running to the end */
  1213. if (p->end_time == 0)
  1214. p->end_time = timechart->last_time;
  1215. c = p->all;
  1216. while (c) {
  1217. c->display = 0;
  1218. if (c->total_bytes >= threshold) {
  1219. c->display = 1;
  1220. count++;
  1221. }
  1222. if (c->end_time == 0)
  1223. c->end_time = timechart->last_time;
  1224. c = c->next;
  1225. }
  1226. p = p->next;
  1227. }
  1228. return count;
  1229. }
  1230. #define BYTES_THRESH (1 * 1024 * 1024)
  1231. #define TIME_THRESH 10000000
  1232. static void write_svg_file(struct timechart *tchart, const char *filename)
  1233. {
  1234. u64 i;
  1235. int count;
  1236. int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
  1237. if (tchart->power_only)
  1238. tchart->proc_num = 0;
  1239. /* We'd like to show at least proc_num tasks;
  1240. * be less picky if we have fewer */
  1241. do {
  1242. if (process_filter)
  1243. count = determine_display_tasks_filtered(tchart);
  1244. else if (tchart->io_events)
  1245. count = determine_display_io_tasks(tchart, thresh);
  1246. else
  1247. count = determine_display_tasks(tchart, thresh);
  1248. thresh /= 10;
  1249. } while (!process_filter && thresh && count < tchart->proc_num);
  1250. if (!tchart->proc_num)
  1251. count = 0;
  1252. if (tchart->io_events) {
  1253. open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
  1254. svg_time_grid(0.5);
  1255. svg_io_legenda();
  1256. draw_io_bars(tchart);
  1257. } else {
  1258. open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
  1259. svg_time_grid(0);
  1260. svg_legenda();
  1261. for (i = 0; i < tchart->numcpus; i++)
  1262. svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
  1263. draw_cpu_usage(tchart);
  1264. if (tchart->proc_num)
  1265. draw_process_bars(tchart);
  1266. if (!tchart->tasks_only)
  1267. draw_c_p_states(tchart);
  1268. if (tchart->proc_num)
  1269. draw_wakeups(tchart);
  1270. }
  1271. svg_close();
  1272. }
  1273. static int process_header(struct perf_file_section *section __maybe_unused,
  1274. struct perf_header *ph,
  1275. int feat,
  1276. int fd __maybe_unused,
  1277. void *data)
  1278. {
  1279. struct timechart *tchart = data;
  1280. switch (feat) {
  1281. case HEADER_NRCPUS:
  1282. tchart->numcpus = ph->env.nr_cpus_avail;
  1283. break;
  1284. case HEADER_CPU_TOPOLOGY:
  1285. if (!tchart->topology)
  1286. break;
  1287. if (svg_build_topology_map(ph->env.sibling_cores,
  1288. ph->env.nr_sibling_cores,
  1289. ph->env.sibling_threads,
  1290. ph->env.nr_sibling_threads))
  1291. fprintf(stderr, "problem building topology\n");
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. return 0;
  1297. }
  1298. static int __cmd_timechart(struct timechart *tchart, const char *output_name)
  1299. {
  1300. const struct perf_evsel_str_handler power_tracepoints[] = {
  1301. { "power:cpu_idle", process_sample_cpu_idle },
  1302. { "power:cpu_frequency", process_sample_cpu_frequency },
  1303. { "sched:sched_wakeup", process_sample_sched_wakeup },
  1304. { "sched:sched_switch", process_sample_sched_switch },
  1305. #ifdef SUPPORT_OLD_POWER_EVENTS
  1306. { "power:power_start", process_sample_power_start },
  1307. { "power:power_end", process_sample_power_end },
  1308. { "power:power_frequency", process_sample_power_frequency },
  1309. #endif
  1310. { "syscalls:sys_enter_read", process_enter_read },
  1311. { "syscalls:sys_enter_pread64", process_enter_read },
  1312. { "syscalls:sys_enter_readv", process_enter_read },
  1313. { "syscalls:sys_enter_preadv", process_enter_read },
  1314. { "syscalls:sys_enter_write", process_enter_write },
  1315. { "syscalls:sys_enter_pwrite64", process_enter_write },
  1316. { "syscalls:sys_enter_writev", process_enter_write },
  1317. { "syscalls:sys_enter_pwritev", process_enter_write },
  1318. { "syscalls:sys_enter_sync", process_enter_sync },
  1319. { "syscalls:sys_enter_sync_file_range", process_enter_sync },
  1320. { "syscalls:sys_enter_fsync", process_enter_sync },
  1321. { "syscalls:sys_enter_msync", process_enter_sync },
  1322. { "syscalls:sys_enter_recvfrom", process_enter_rx },
  1323. { "syscalls:sys_enter_recvmmsg", process_enter_rx },
  1324. { "syscalls:sys_enter_recvmsg", process_enter_rx },
  1325. { "syscalls:sys_enter_sendto", process_enter_tx },
  1326. { "syscalls:sys_enter_sendmsg", process_enter_tx },
  1327. { "syscalls:sys_enter_sendmmsg", process_enter_tx },
  1328. { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
  1329. { "syscalls:sys_enter_epoll_wait", process_enter_poll },
  1330. { "syscalls:sys_enter_poll", process_enter_poll },
  1331. { "syscalls:sys_enter_ppoll", process_enter_poll },
  1332. { "syscalls:sys_enter_pselect6", process_enter_poll },
  1333. { "syscalls:sys_enter_select", process_enter_poll },
  1334. { "syscalls:sys_exit_read", process_exit_read },
  1335. { "syscalls:sys_exit_pread64", process_exit_read },
  1336. { "syscalls:sys_exit_readv", process_exit_read },
  1337. { "syscalls:sys_exit_preadv", process_exit_read },
  1338. { "syscalls:sys_exit_write", process_exit_write },
  1339. { "syscalls:sys_exit_pwrite64", process_exit_write },
  1340. { "syscalls:sys_exit_writev", process_exit_write },
  1341. { "syscalls:sys_exit_pwritev", process_exit_write },
  1342. { "syscalls:sys_exit_sync", process_exit_sync },
  1343. { "syscalls:sys_exit_sync_file_range", process_exit_sync },
  1344. { "syscalls:sys_exit_fsync", process_exit_sync },
  1345. { "syscalls:sys_exit_msync", process_exit_sync },
  1346. { "syscalls:sys_exit_recvfrom", process_exit_rx },
  1347. { "syscalls:sys_exit_recvmmsg", process_exit_rx },
  1348. { "syscalls:sys_exit_recvmsg", process_exit_rx },
  1349. { "syscalls:sys_exit_sendto", process_exit_tx },
  1350. { "syscalls:sys_exit_sendmsg", process_exit_tx },
  1351. { "syscalls:sys_exit_sendmmsg", process_exit_tx },
  1352. { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
  1353. { "syscalls:sys_exit_epoll_wait", process_exit_poll },
  1354. { "syscalls:sys_exit_poll", process_exit_poll },
  1355. { "syscalls:sys_exit_ppoll", process_exit_poll },
  1356. { "syscalls:sys_exit_pselect6", process_exit_poll },
  1357. { "syscalls:sys_exit_select", process_exit_poll },
  1358. };
  1359. struct perf_data_file file = {
  1360. .path = input_name,
  1361. .mode = PERF_DATA_MODE_READ,
  1362. };
  1363. struct perf_session *session = perf_session__new(&file, false,
  1364. &tchart->tool);
  1365. int ret = -EINVAL;
  1366. if (session == NULL)
  1367. return -1;
  1368. symbol__init(&session->header.env);
  1369. (void)perf_header__process_sections(&session->header,
  1370. perf_data_file__fd(session->file),
  1371. tchart,
  1372. process_header);
  1373. if (!perf_session__has_traces(session, "timechart record"))
  1374. goto out_delete;
  1375. if (perf_session__set_tracepoints_handlers(session,
  1376. power_tracepoints)) {
  1377. pr_err("Initializing session tracepoint handlers failed\n");
  1378. goto out_delete;
  1379. }
  1380. ret = perf_session__process_events(session, &tchart->tool);
  1381. if (ret)
  1382. goto out_delete;
  1383. end_sample_processing(tchart);
  1384. sort_pids(tchart);
  1385. write_svg_file(tchart, output_name);
  1386. pr_info("Written %2.1f seconds of trace to %s.\n",
  1387. (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
  1388. out_delete:
  1389. perf_session__delete(session);
  1390. return ret;
  1391. }
  1392. static int timechart__io_record(int argc, const char **argv)
  1393. {
  1394. unsigned int rec_argc, i;
  1395. const char **rec_argv;
  1396. const char **p;
  1397. char *filter = NULL;
  1398. const char * const common_args[] = {
  1399. "record", "-a", "-R", "-c", "1",
  1400. };
  1401. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  1402. const char * const disk_events[] = {
  1403. "syscalls:sys_enter_read",
  1404. "syscalls:sys_enter_pread64",
  1405. "syscalls:sys_enter_readv",
  1406. "syscalls:sys_enter_preadv",
  1407. "syscalls:sys_enter_write",
  1408. "syscalls:sys_enter_pwrite64",
  1409. "syscalls:sys_enter_writev",
  1410. "syscalls:sys_enter_pwritev",
  1411. "syscalls:sys_enter_sync",
  1412. "syscalls:sys_enter_sync_file_range",
  1413. "syscalls:sys_enter_fsync",
  1414. "syscalls:sys_enter_msync",
  1415. "syscalls:sys_exit_read",
  1416. "syscalls:sys_exit_pread64",
  1417. "syscalls:sys_exit_readv",
  1418. "syscalls:sys_exit_preadv",
  1419. "syscalls:sys_exit_write",
  1420. "syscalls:sys_exit_pwrite64",
  1421. "syscalls:sys_exit_writev",
  1422. "syscalls:sys_exit_pwritev",
  1423. "syscalls:sys_exit_sync",
  1424. "syscalls:sys_exit_sync_file_range",
  1425. "syscalls:sys_exit_fsync",
  1426. "syscalls:sys_exit_msync",
  1427. };
  1428. unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
  1429. const char * const net_events[] = {
  1430. "syscalls:sys_enter_recvfrom",
  1431. "syscalls:sys_enter_recvmmsg",
  1432. "syscalls:sys_enter_recvmsg",
  1433. "syscalls:sys_enter_sendto",
  1434. "syscalls:sys_enter_sendmsg",
  1435. "syscalls:sys_enter_sendmmsg",
  1436. "syscalls:sys_exit_recvfrom",
  1437. "syscalls:sys_exit_recvmmsg",
  1438. "syscalls:sys_exit_recvmsg",
  1439. "syscalls:sys_exit_sendto",
  1440. "syscalls:sys_exit_sendmsg",
  1441. "syscalls:sys_exit_sendmmsg",
  1442. };
  1443. unsigned int net_events_nr = ARRAY_SIZE(net_events);
  1444. const char * const poll_events[] = {
  1445. "syscalls:sys_enter_epoll_pwait",
  1446. "syscalls:sys_enter_epoll_wait",
  1447. "syscalls:sys_enter_poll",
  1448. "syscalls:sys_enter_ppoll",
  1449. "syscalls:sys_enter_pselect6",
  1450. "syscalls:sys_enter_select",
  1451. "syscalls:sys_exit_epoll_pwait",
  1452. "syscalls:sys_exit_epoll_wait",
  1453. "syscalls:sys_exit_poll",
  1454. "syscalls:sys_exit_ppoll",
  1455. "syscalls:sys_exit_pselect6",
  1456. "syscalls:sys_exit_select",
  1457. };
  1458. unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
  1459. rec_argc = common_args_nr +
  1460. disk_events_nr * 4 +
  1461. net_events_nr * 4 +
  1462. poll_events_nr * 4 +
  1463. argc;
  1464. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1465. if (rec_argv == NULL)
  1466. return -ENOMEM;
  1467. if (asprintf(&filter, "common_pid != %d", getpid()) < 0)
  1468. return -ENOMEM;
  1469. p = rec_argv;
  1470. for (i = 0; i < common_args_nr; i++)
  1471. *p++ = strdup(common_args[i]);
  1472. for (i = 0; i < disk_events_nr; i++) {
  1473. if (!is_valid_tracepoint(disk_events[i])) {
  1474. rec_argc -= 4;
  1475. continue;
  1476. }
  1477. *p++ = "-e";
  1478. *p++ = strdup(disk_events[i]);
  1479. *p++ = "--filter";
  1480. *p++ = filter;
  1481. }
  1482. for (i = 0; i < net_events_nr; i++) {
  1483. if (!is_valid_tracepoint(net_events[i])) {
  1484. rec_argc -= 4;
  1485. continue;
  1486. }
  1487. *p++ = "-e";
  1488. *p++ = strdup(net_events[i]);
  1489. *p++ = "--filter";
  1490. *p++ = filter;
  1491. }
  1492. for (i = 0; i < poll_events_nr; i++) {
  1493. if (!is_valid_tracepoint(poll_events[i])) {
  1494. rec_argc -= 4;
  1495. continue;
  1496. }
  1497. *p++ = "-e";
  1498. *p++ = strdup(poll_events[i]);
  1499. *p++ = "--filter";
  1500. *p++ = filter;
  1501. }
  1502. for (i = 0; i < (unsigned int)argc; i++)
  1503. *p++ = argv[i];
  1504. return cmd_record(rec_argc, rec_argv, NULL);
  1505. }
  1506. static int timechart__record(struct timechart *tchart, int argc, const char **argv)
  1507. {
  1508. unsigned int rec_argc, i, j;
  1509. const char **rec_argv;
  1510. const char **p;
  1511. unsigned int record_elems;
  1512. const char * const common_args[] = {
  1513. "record", "-a", "-R", "-c", "1",
  1514. };
  1515. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  1516. const char * const backtrace_args[] = {
  1517. "-g",
  1518. };
  1519. unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
  1520. const char * const power_args[] = {
  1521. "-e", "power:cpu_frequency",
  1522. "-e", "power:cpu_idle",
  1523. };
  1524. unsigned int power_args_nr = ARRAY_SIZE(power_args);
  1525. const char * const old_power_args[] = {
  1526. #ifdef SUPPORT_OLD_POWER_EVENTS
  1527. "-e", "power:power_start",
  1528. "-e", "power:power_end",
  1529. "-e", "power:power_frequency",
  1530. #endif
  1531. };
  1532. unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
  1533. const char * const tasks_args[] = {
  1534. "-e", "sched:sched_wakeup",
  1535. "-e", "sched:sched_switch",
  1536. };
  1537. unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
  1538. #ifdef SUPPORT_OLD_POWER_EVENTS
  1539. if (!is_valid_tracepoint("power:cpu_idle") &&
  1540. is_valid_tracepoint("power:power_start")) {
  1541. use_old_power_events = 1;
  1542. power_args_nr = 0;
  1543. } else {
  1544. old_power_args_nr = 0;
  1545. }
  1546. #endif
  1547. if (tchart->power_only)
  1548. tasks_args_nr = 0;
  1549. if (tchart->tasks_only) {
  1550. power_args_nr = 0;
  1551. old_power_args_nr = 0;
  1552. }
  1553. if (!tchart->with_backtrace)
  1554. backtrace_args_no = 0;
  1555. record_elems = common_args_nr + tasks_args_nr +
  1556. power_args_nr + old_power_args_nr + backtrace_args_no;
  1557. rec_argc = record_elems + argc;
  1558. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1559. if (rec_argv == NULL)
  1560. return -ENOMEM;
  1561. p = rec_argv;
  1562. for (i = 0; i < common_args_nr; i++)
  1563. *p++ = strdup(common_args[i]);
  1564. for (i = 0; i < backtrace_args_no; i++)
  1565. *p++ = strdup(backtrace_args[i]);
  1566. for (i = 0; i < tasks_args_nr; i++)
  1567. *p++ = strdup(tasks_args[i]);
  1568. for (i = 0; i < power_args_nr; i++)
  1569. *p++ = strdup(power_args[i]);
  1570. for (i = 0; i < old_power_args_nr; i++)
  1571. *p++ = strdup(old_power_args[i]);
  1572. for (j = 0; j < (unsigned int)argc; j++)
  1573. *p++ = argv[j];
  1574. return cmd_record(rec_argc, rec_argv, NULL);
  1575. }
  1576. static int
  1577. parse_process(const struct option *opt __maybe_unused, const char *arg,
  1578. int __maybe_unused unset)
  1579. {
  1580. if (arg)
  1581. add_process_filter(arg);
  1582. return 0;
  1583. }
  1584. static int
  1585. parse_highlight(const struct option *opt __maybe_unused, const char *arg,
  1586. int __maybe_unused unset)
  1587. {
  1588. unsigned long duration = strtoul(arg, NULL, 0);
  1589. if (svg_highlight || svg_highlight_name)
  1590. return -1;
  1591. if (duration)
  1592. svg_highlight = duration;
  1593. else
  1594. svg_highlight_name = strdup(arg);
  1595. return 0;
  1596. }
  1597. static int
  1598. parse_time(const struct option *opt, const char *arg, int __maybe_unused unset)
  1599. {
  1600. char unit = 'n';
  1601. u64 *value = opt->value;
  1602. if (sscanf(arg, "%" PRIu64 "%cs", value, &unit) > 0) {
  1603. switch (unit) {
  1604. case 'm':
  1605. *value *= 1000000;
  1606. break;
  1607. case 'u':
  1608. *value *= 1000;
  1609. break;
  1610. case 'n':
  1611. break;
  1612. default:
  1613. return -1;
  1614. }
  1615. }
  1616. return 0;
  1617. }
  1618. int cmd_timechart(int argc, const char **argv,
  1619. const char *prefix __maybe_unused)
  1620. {
  1621. struct timechart tchart = {
  1622. .tool = {
  1623. .comm = process_comm_event,
  1624. .fork = process_fork_event,
  1625. .exit = process_exit_event,
  1626. .sample = process_sample_event,
  1627. .ordered_events = true,
  1628. },
  1629. .proc_num = 15,
  1630. .min_time = 1000000,
  1631. .merge_dist = 1000,
  1632. };
  1633. const char *output_name = "output.svg";
  1634. const struct option timechart_options[] = {
  1635. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1636. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1637. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  1638. OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
  1639. "highlight tasks. Pass duration in ns or process name.",
  1640. parse_highlight),
  1641. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1642. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1643. "output processes data only"),
  1644. OPT_CALLBACK('p', "process", NULL, "process",
  1645. "process selector. Pass a pid or process name.",
  1646. parse_process),
  1647. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1648. "Look for files with symbols relative to this directory"),
  1649. OPT_INTEGER('n', "proc-num", &tchart.proc_num,
  1650. "min. number of tasks to print"),
  1651. OPT_BOOLEAN('t', "topology", &tchart.topology,
  1652. "sort CPUs according to topology"),
  1653. OPT_BOOLEAN(0, "io-skip-eagain", &tchart.skip_eagain,
  1654. "skip EAGAIN errors"),
  1655. OPT_CALLBACK(0, "io-min-time", &tchart.min_time, "time",
  1656. "all IO faster than min-time will visually appear longer",
  1657. parse_time),
  1658. OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
  1659. "merge events that are merge-dist us apart",
  1660. parse_time),
  1661. OPT_END()
  1662. };
  1663. const char * const timechart_usage[] = {
  1664. "perf timechart [<options>] {record}",
  1665. NULL
  1666. };
  1667. const struct option timechart_record_options[] = {
  1668. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1669. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
  1670. "output processes data only"),
  1671. OPT_BOOLEAN('I', "io-only", &tchart.io_only,
  1672. "record only IO data"),
  1673. OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
  1674. OPT_END()
  1675. };
  1676. const char * const timechart_record_usage[] = {
  1677. "perf timechart record [<options>]",
  1678. NULL
  1679. };
  1680. argc = parse_options(argc, argv, timechart_options, timechart_usage,
  1681. PARSE_OPT_STOP_AT_NON_OPTION);
  1682. if (tchart.power_only && tchart.tasks_only) {
  1683. pr_err("-P and -T options cannot be used at the same time.\n");
  1684. return -1;
  1685. }
  1686. if (argc && !strncmp(argv[0], "rec", 3)) {
  1687. argc = parse_options(argc, argv, timechart_record_options,
  1688. timechart_record_usage,
  1689. PARSE_OPT_STOP_AT_NON_OPTION);
  1690. if (tchart.power_only && tchart.tasks_only) {
  1691. pr_err("-P and -T options cannot be used at the same time.\n");
  1692. return -1;
  1693. }
  1694. if (tchart.io_only)
  1695. return timechart__io_record(argc, argv);
  1696. else
  1697. return timechart__record(&tchart, argc, argv);
  1698. } else if (argc)
  1699. usage_with_options(timechart_usage, timechart_options);
  1700. setup_pager();
  1701. return __cmd_timechart(&tchart, output_name);
  1702. }