builtin-timechart.c 48 KB

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