builtin-timechart.c 47 KB

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