builtin-timechart.c 47 KB

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