builtin-timechart.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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 <traceevent/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. sample = c->io_samples;
  989. for (sample = c->io_samples; sample; sample = sample->next) {
  990. double h = (double)sample->bytes / c->max_bytes;
  991. if (tchart->skip_eagain &&
  992. sample->err == -EAGAIN)
  993. continue;
  994. if (sample->err)
  995. h = 1;
  996. if (sample->type == IOTYPE_SYNC)
  997. svg_fbox(Y,
  998. sample->start_time,
  999. sample->end_time,
  1000. 1,
  1001. sample->err ? "error" : "sync",
  1002. sample->fd,
  1003. sample->err,
  1004. sample->merges);
  1005. else if (sample->type == IOTYPE_POLL)
  1006. svg_fbox(Y,
  1007. sample->start_time,
  1008. sample->end_time,
  1009. 1,
  1010. sample->err ? "error" : "poll",
  1011. sample->fd,
  1012. sample->err,
  1013. sample->merges);
  1014. else if (sample->type == IOTYPE_READ)
  1015. svg_ubox(Y,
  1016. sample->start_time,
  1017. sample->end_time,
  1018. h,
  1019. sample->err ? "error" : "disk",
  1020. sample->fd,
  1021. sample->err,
  1022. sample->merges);
  1023. else if (sample->type == IOTYPE_WRITE)
  1024. svg_lbox(Y,
  1025. sample->start_time,
  1026. sample->end_time,
  1027. h,
  1028. sample->err ? "error" : "disk",
  1029. sample->fd,
  1030. sample->err,
  1031. sample->merges);
  1032. else if (sample->type == IOTYPE_RX)
  1033. svg_ubox(Y,
  1034. sample->start_time,
  1035. sample->end_time,
  1036. h,
  1037. sample->err ? "error" : "net",
  1038. sample->fd,
  1039. sample->err,
  1040. sample->merges);
  1041. else if (sample->type == IOTYPE_TX)
  1042. svg_lbox(Y,
  1043. sample->start_time,
  1044. sample->end_time,
  1045. h,
  1046. sample->err ? "error" : "net",
  1047. sample->fd,
  1048. sample->err,
  1049. sample->merges);
  1050. }
  1051. suf = "";
  1052. bytes = c->total_bytes;
  1053. if (bytes > 1024) {
  1054. bytes = bytes / 1024;
  1055. suf = "K";
  1056. }
  1057. if (bytes > 1024) {
  1058. bytes = bytes / 1024;
  1059. suf = "M";
  1060. }
  1061. if (bytes > 1024) {
  1062. bytes = bytes / 1024;
  1063. suf = "G";
  1064. }
  1065. sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
  1066. svg_text(Y, c->start_time, comm);
  1067. c->Y = Y;
  1068. Y++;
  1069. c = c->next;
  1070. }
  1071. p = p->next;
  1072. }
  1073. }
  1074. static void draw_process_bars(struct timechart *tchart)
  1075. {
  1076. struct per_pid *p;
  1077. struct per_pidcomm *c;
  1078. struct cpu_sample *sample;
  1079. int Y = 0;
  1080. Y = 2 * tchart->numcpus + 2;
  1081. p = tchart->all_data;
  1082. while (p) {
  1083. c = p->all;
  1084. while (c) {
  1085. if (!c->display) {
  1086. c->Y = 0;
  1087. c = c->next;
  1088. continue;
  1089. }
  1090. svg_box(Y, c->start_time, c->end_time, "process");
  1091. sample = c->samples;
  1092. while (sample) {
  1093. if (sample->type == TYPE_RUNNING)
  1094. svg_running(Y, sample->cpu,
  1095. sample->start_time,
  1096. sample->end_time,
  1097. sample->backtrace);
  1098. if (sample->type == TYPE_BLOCKED)
  1099. svg_blocked(Y, sample->cpu,
  1100. sample->start_time,
  1101. sample->end_time,
  1102. sample->backtrace);
  1103. if (sample->type == TYPE_WAITING)
  1104. svg_waiting(Y, sample->cpu,
  1105. sample->start_time,
  1106. sample->end_time,
  1107. sample->backtrace);
  1108. sample = sample->next;
  1109. }
  1110. if (c->comm) {
  1111. char comm[256];
  1112. if (c->total_time > 5000000000) /* 5 seconds */
  1113. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / (double)NSEC_PER_SEC);
  1114. else
  1115. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / (double)NSEC_PER_MSEC);
  1116. svg_text(Y, c->start_time, comm);
  1117. }
  1118. c->Y = Y;
  1119. Y++;
  1120. c = c->next;
  1121. }
  1122. p = p->next;
  1123. }
  1124. }
  1125. static void add_process_filter(const char *string)
  1126. {
  1127. int pid = strtoull(string, NULL, 10);
  1128. struct process_filter *filt = malloc(sizeof(*filt));
  1129. if (!filt)
  1130. return;
  1131. filt->name = strdup(string);
  1132. filt->pid = pid;
  1133. filt->next = process_filter;
  1134. process_filter = filt;
  1135. }
  1136. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  1137. {
  1138. struct process_filter *filt;
  1139. if (!process_filter)
  1140. return 1;
  1141. filt = process_filter;
  1142. while (filt) {
  1143. if (filt->pid && p->pid == filt->pid)
  1144. return 1;
  1145. if (strcmp(filt->name, c->comm) == 0)
  1146. return 1;
  1147. filt = filt->next;
  1148. }
  1149. return 0;
  1150. }
  1151. static int determine_display_tasks_filtered(struct timechart *tchart)
  1152. {
  1153. struct per_pid *p;
  1154. struct per_pidcomm *c;
  1155. int count = 0;
  1156. p = tchart->all_data;
  1157. while (p) {
  1158. p->display = 0;
  1159. if (p->start_time == 1)
  1160. p->start_time = tchart->first_time;
  1161. /* no exit marker, task kept running to the end */
  1162. if (p->end_time == 0)
  1163. p->end_time = tchart->last_time;
  1164. c = p->all;
  1165. while (c) {
  1166. c->display = 0;
  1167. if (c->start_time == 1)
  1168. c->start_time = tchart->first_time;
  1169. if (passes_filter(p, c)) {
  1170. c->display = 1;
  1171. p->display = 1;
  1172. count++;
  1173. }
  1174. if (c->end_time == 0)
  1175. c->end_time = tchart->last_time;
  1176. c = c->next;
  1177. }
  1178. p = p->next;
  1179. }
  1180. return count;
  1181. }
  1182. static int determine_display_tasks(struct timechart *tchart, u64 threshold)
  1183. {
  1184. struct per_pid *p;
  1185. struct per_pidcomm *c;
  1186. int count = 0;
  1187. p = tchart->all_data;
  1188. while (p) {
  1189. p->display = 0;
  1190. if (p->start_time == 1)
  1191. p->start_time = tchart->first_time;
  1192. /* no exit marker, task kept running to the end */
  1193. if (p->end_time == 0)
  1194. p->end_time = tchart->last_time;
  1195. if (p->total_time >= threshold)
  1196. p->display = 1;
  1197. c = p->all;
  1198. while (c) {
  1199. c->display = 0;
  1200. if (c->start_time == 1)
  1201. c->start_time = tchart->first_time;
  1202. if (c->total_time >= threshold) {
  1203. c->display = 1;
  1204. count++;
  1205. }
  1206. if (c->end_time == 0)
  1207. c->end_time = tchart->last_time;
  1208. c = c->next;
  1209. }
  1210. p = p->next;
  1211. }
  1212. return count;
  1213. }
  1214. static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
  1215. {
  1216. struct per_pid *p;
  1217. struct per_pidcomm *c;
  1218. int count = 0;
  1219. p = timechart->all_data;
  1220. while (p) {
  1221. /* no exit marker, task kept running to the end */
  1222. if (p->end_time == 0)
  1223. p->end_time = timechart->last_time;
  1224. c = p->all;
  1225. while (c) {
  1226. c->display = 0;
  1227. if (c->total_bytes >= threshold) {
  1228. c->display = 1;
  1229. count++;
  1230. }
  1231. if (c->end_time == 0)
  1232. c->end_time = timechart->last_time;
  1233. c = c->next;
  1234. }
  1235. p = p->next;
  1236. }
  1237. return count;
  1238. }
  1239. #define BYTES_THRESH (1 * 1024 * 1024)
  1240. #define TIME_THRESH 10000000
  1241. static void write_svg_file(struct timechart *tchart, const char *filename)
  1242. {
  1243. u64 i;
  1244. int count;
  1245. int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
  1246. if (tchart->power_only)
  1247. tchart->proc_num = 0;
  1248. /* We'd like to show at least proc_num tasks;
  1249. * be less picky if we have fewer */
  1250. do {
  1251. if (process_filter)
  1252. count = determine_display_tasks_filtered(tchart);
  1253. else if (tchart->io_events)
  1254. count = determine_display_io_tasks(tchart, thresh);
  1255. else
  1256. count = determine_display_tasks(tchart, thresh);
  1257. thresh /= 10;
  1258. } while (!process_filter && thresh && count < tchart->proc_num);
  1259. if (!tchart->proc_num)
  1260. count = 0;
  1261. if (tchart->io_events) {
  1262. open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
  1263. svg_time_grid(0.5);
  1264. svg_io_legenda();
  1265. draw_io_bars(tchart);
  1266. } else {
  1267. open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
  1268. svg_time_grid(0);
  1269. svg_legenda();
  1270. for (i = 0; i < tchart->numcpus; i++)
  1271. svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
  1272. draw_cpu_usage(tchart);
  1273. if (tchart->proc_num)
  1274. draw_process_bars(tchart);
  1275. if (!tchart->tasks_only)
  1276. draw_c_p_states(tchart);
  1277. if (tchart->proc_num)
  1278. draw_wakeups(tchart);
  1279. }
  1280. svg_close();
  1281. }
  1282. static int process_header(struct perf_file_section *section __maybe_unused,
  1283. struct perf_header *ph,
  1284. int feat,
  1285. int fd __maybe_unused,
  1286. void *data)
  1287. {
  1288. struct timechart *tchart = data;
  1289. switch (feat) {
  1290. case HEADER_NRCPUS:
  1291. tchart->numcpus = ph->env.nr_cpus_avail;
  1292. break;
  1293. case HEADER_CPU_TOPOLOGY:
  1294. if (!tchart->topology)
  1295. break;
  1296. if (svg_build_topology_map(&ph->env))
  1297. fprintf(stderr, "problem building topology\n");
  1298. break;
  1299. default:
  1300. break;
  1301. }
  1302. return 0;
  1303. }
  1304. static int __cmd_timechart(struct timechart *tchart, const char *output_name)
  1305. {
  1306. const struct evsel_str_handler power_tracepoints[] = {
  1307. { "power:cpu_idle", process_sample_cpu_idle },
  1308. { "power:cpu_frequency", process_sample_cpu_frequency },
  1309. { "sched:sched_wakeup", process_sample_sched_wakeup },
  1310. { "sched:sched_switch", process_sample_sched_switch },
  1311. #ifdef SUPPORT_OLD_POWER_EVENTS
  1312. { "power:power_start", process_sample_power_start },
  1313. { "power:power_end", process_sample_power_end },
  1314. { "power:power_frequency", process_sample_power_frequency },
  1315. #endif
  1316. { "syscalls:sys_enter_read", process_enter_read },
  1317. { "syscalls:sys_enter_pread64", process_enter_read },
  1318. { "syscalls:sys_enter_readv", process_enter_read },
  1319. { "syscalls:sys_enter_preadv", process_enter_read },
  1320. { "syscalls:sys_enter_write", process_enter_write },
  1321. { "syscalls:sys_enter_pwrite64", process_enter_write },
  1322. { "syscalls:sys_enter_writev", process_enter_write },
  1323. { "syscalls:sys_enter_pwritev", process_enter_write },
  1324. { "syscalls:sys_enter_sync", process_enter_sync },
  1325. { "syscalls:sys_enter_sync_file_range", process_enter_sync },
  1326. { "syscalls:sys_enter_fsync", process_enter_sync },
  1327. { "syscalls:sys_enter_msync", process_enter_sync },
  1328. { "syscalls:sys_enter_recvfrom", process_enter_rx },
  1329. { "syscalls:sys_enter_recvmmsg", process_enter_rx },
  1330. { "syscalls:sys_enter_recvmsg", process_enter_rx },
  1331. { "syscalls:sys_enter_sendto", process_enter_tx },
  1332. { "syscalls:sys_enter_sendmsg", process_enter_tx },
  1333. { "syscalls:sys_enter_sendmmsg", process_enter_tx },
  1334. { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
  1335. { "syscalls:sys_enter_epoll_wait", process_enter_poll },
  1336. { "syscalls:sys_enter_poll", process_enter_poll },
  1337. { "syscalls:sys_enter_ppoll", process_enter_poll },
  1338. { "syscalls:sys_enter_pselect6", process_enter_poll },
  1339. { "syscalls:sys_enter_select", process_enter_poll },
  1340. { "syscalls:sys_exit_read", process_exit_read },
  1341. { "syscalls:sys_exit_pread64", process_exit_read },
  1342. { "syscalls:sys_exit_readv", process_exit_read },
  1343. { "syscalls:sys_exit_preadv", process_exit_read },
  1344. { "syscalls:sys_exit_write", process_exit_write },
  1345. { "syscalls:sys_exit_pwrite64", process_exit_write },
  1346. { "syscalls:sys_exit_writev", process_exit_write },
  1347. { "syscalls:sys_exit_pwritev", process_exit_write },
  1348. { "syscalls:sys_exit_sync", process_exit_sync },
  1349. { "syscalls:sys_exit_sync_file_range", process_exit_sync },
  1350. { "syscalls:sys_exit_fsync", process_exit_sync },
  1351. { "syscalls:sys_exit_msync", process_exit_sync },
  1352. { "syscalls:sys_exit_recvfrom", process_exit_rx },
  1353. { "syscalls:sys_exit_recvmmsg", process_exit_rx },
  1354. { "syscalls:sys_exit_recvmsg", process_exit_rx },
  1355. { "syscalls:sys_exit_sendto", process_exit_tx },
  1356. { "syscalls:sys_exit_sendmsg", process_exit_tx },
  1357. { "syscalls:sys_exit_sendmmsg", process_exit_tx },
  1358. { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
  1359. { "syscalls:sys_exit_epoll_wait", process_exit_poll },
  1360. { "syscalls:sys_exit_poll", process_exit_poll },
  1361. { "syscalls:sys_exit_ppoll", process_exit_poll },
  1362. { "syscalls:sys_exit_pselect6", process_exit_poll },
  1363. { "syscalls:sys_exit_select", process_exit_poll },
  1364. };
  1365. struct perf_data data = {
  1366. .path = input_name,
  1367. .mode = PERF_DATA_MODE_READ,
  1368. .force = tchart->force,
  1369. };
  1370. struct perf_session *session;
  1371. int ret = -EINVAL;
  1372. perf_tool__init(&tchart->tool, /*ordered_events=*/true);
  1373. tchart->tool.comm = process_comm_event;
  1374. tchart->tool.fork = process_fork_event;
  1375. tchart->tool.exit = process_exit_event;
  1376. tchart->tool.sample = process_sample_event;
  1377. session = perf_session__new(&data, &tchart->tool);
  1378. if (IS_ERR(session))
  1379. return PTR_ERR(session);
  1380. symbol__init(&session->header.env);
  1381. (void)perf_header__process_sections(&session->header,
  1382. perf_data__fd(session->data),
  1383. tchart,
  1384. process_header);
  1385. if (!perf_session__has_traces(session, "timechart record"))
  1386. goto out_delete;
  1387. if (perf_session__set_tracepoints_handlers(session,
  1388. power_tracepoints)) {
  1389. pr_err("Initializing session tracepoint handlers failed\n");
  1390. goto out_delete;
  1391. }
  1392. ret = perf_session__process_events(session);
  1393. if (ret)
  1394. goto out_delete;
  1395. end_sample_processing(tchart);
  1396. sort_pids(tchart);
  1397. write_svg_file(tchart, output_name);
  1398. pr_info("Written %2.1f seconds of trace to %s.\n",
  1399. (tchart->last_time - tchart->first_time) / (double)NSEC_PER_SEC, output_name);
  1400. out_delete:
  1401. perf_session__delete(session);
  1402. return ret;
  1403. }
  1404. static int timechart__io_record(int argc, const char **argv)
  1405. {
  1406. unsigned int rec_argc, i;
  1407. const char **rec_argv;
  1408. const char **p;
  1409. char *filter = NULL;
  1410. const char * const common_args[] = {
  1411. "record", "-a", "-R", "-c", "1",
  1412. };
  1413. unsigned int common_args_nr = ARRAY_SIZE(common_args);
  1414. const char * const disk_events[] = {
  1415. "syscalls:sys_enter_read",
  1416. "syscalls:sys_enter_pread64",
  1417. "syscalls:sys_enter_readv",
  1418. "syscalls:sys_enter_preadv",
  1419. "syscalls:sys_enter_write",
  1420. "syscalls:sys_enter_pwrite64",
  1421. "syscalls:sys_enter_writev",
  1422. "syscalls:sys_enter_pwritev",
  1423. "syscalls:sys_enter_sync",
  1424. "syscalls:sys_enter_sync_file_range",
  1425. "syscalls:sys_enter_fsync",
  1426. "syscalls:sys_enter_msync",
  1427. "syscalls:sys_exit_read",
  1428. "syscalls:sys_exit_pread64",
  1429. "syscalls:sys_exit_readv",
  1430. "syscalls:sys_exit_preadv",
  1431. "syscalls:sys_exit_write",
  1432. "syscalls:sys_exit_pwrite64",
  1433. "syscalls:sys_exit_writev",
  1434. "syscalls:sys_exit_pwritev",
  1435. "syscalls:sys_exit_sync",
  1436. "syscalls:sys_exit_sync_file_range",
  1437. "syscalls:sys_exit_fsync",
  1438. "syscalls:sys_exit_msync",
  1439. };
  1440. unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
  1441. const char * const net_events[] = {
  1442. "syscalls:sys_enter_recvfrom",
  1443. "syscalls:sys_enter_recvmmsg",
  1444. "syscalls:sys_enter_recvmsg",
  1445. "syscalls:sys_enter_sendto",
  1446. "syscalls:sys_enter_sendmsg",
  1447. "syscalls:sys_enter_sendmmsg",
  1448. "syscalls:sys_exit_recvfrom",
  1449. "syscalls:sys_exit_recvmmsg",
  1450. "syscalls:sys_exit_recvmsg",
  1451. "syscalls:sys_exit_sendto",
  1452. "syscalls:sys_exit_sendmsg",
  1453. "syscalls:sys_exit_sendmmsg",
  1454. };
  1455. unsigned int net_events_nr = ARRAY_SIZE(net_events);
  1456. const char * const poll_events[] = {
  1457. "syscalls:sys_enter_epoll_pwait",
  1458. "syscalls:sys_enter_epoll_wait",
  1459. "syscalls:sys_enter_poll",
  1460. "syscalls:sys_enter_ppoll",
  1461. "syscalls:sys_enter_pselect6",
  1462. "syscalls:sys_enter_select",
  1463. "syscalls:sys_exit_epoll_pwait",
  1464. "syscalls:sys_exit_epoll_wait",
  1465. "syscalls:sys_exit_poll",
  1466. "syscalls:sys_exit_ppoll",
  1467. "syscalls:sys_exit_pselect6",
  1468. "syscalls:sys_exit_select",
  1469. };
  1470. unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
  1471. rec_argc = common_args_nr +
  1472. disk_events_nr * 4 +
  1473. net_events_nr * 4 +
  1474. poll_events_nr * 4 +
  1475. argc;
  1476. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1477. if (rec_argv == NULL)
  1478. return -ENOMEM;
  1479. if (asprintf(&filter, "common_pid != %d", getpid()) < 0) {
  1480. free(rec_argv);
  1481. return -ENOMEM;
  1482. }
  1483. p = rec_argv;
  1484. for (i = 0; i < common_args_nr; i++)
  1485. *p++ = strdup(common_args[i]);
  1486. for (i = 0; i < disk_events_nr; i++) {
  1487. if (!is_valid_tracepoint(disk_events[i])) {
  1488. rec_argc -= 4;
  1489. continue;
  1490. }
  1491. *p++ = "-e";
  1492. *p++ = strdup(disk_events[i]);
  1493. *p++ = "--filter";
  1494. *p++ = filter;
  1495. }
  1496. for (i = 0; i < net_events_nr; i++) {
  1497. if (!is_valid_tracepoint(net_events[i])) {
  1498. rec_argc -= 4;
  1499. continue;
  1500. }
  1501. *p++ = "-e";
  1502. *p++ = strdup(net_events[i]);
  1503. *p++ = "--filter";
  1504. *p++ = filter;
  1505. }
  1506. for (i = 0; i < poll_events_nr; i++) {
  1507. if (!is_valid_tracepoint(poll_events[i])) {
  1508. rec_argc -= 4;
  1509. continue;
  1510. }
  1511. *p++ = "-e";
  1512. *p++ = strdup(poll_events[i]);
  1513. *p++ = "--filter";
  1514. *p++ = filter;
  1515. }
  1516. for (i = 0; i < (unsigned int)argc; i++)
  1517. *p++ = argv[i];
  1518. return cmd_record(rec_argc, rec_argv);
  1519. }
  1520. static int timechart__record(struct timechart *tchart, int argc, const char **argv)
  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",
  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 struct option timechart_common_options[] = {
  1641. OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
  1642. OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only, "output processes data only"),
  1643. OPT_END()
  1644. };
  1645. const struct option timechart_options[] = {
  1646. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1647. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1648. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  1649. OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
  1650. "highlight tasks. Pass duration in ns or process name.",
  1651. parse_highlight),
  1652. OPT_CALLBACK('p', "process", NULL, "process",
  1653. "process selector. Pass a pid or process name.",
  1654. parse_process),
  1655. OPT_CALLBACK(0, "symfs", NULL, "directory",
  1656. "Look for files with symbols relative to this directory",
  1657. symbol__config_symfs),
  1658. OPT_INTEGER('n', "proc-num", &tchart.proc_num,
  1659. "min. number of tasks to print"),
  1660. OPT_BOOLEAN('t', "topology", &tchart.topology,
  1661. "sort CPUs according to topology"),
  1662. OPT_BOOLEAN(0, "io-skip-eagain", &tchart.skip_eagain,
  1663. "skip EAGAIN errors"),
  1664. OPT_CALLBACK(0, "io-min-time", &tchart.min_time, "time",
  1665. "all IO faster than min-time will visually appear longer",
  1666. parse_time),
  1667. OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
  1668. "merge events that are merge-dist us apart",
  1669. parse_time),
  1670. OPT_BOOLEAN('f', "force", &tchart.force, "don't complain, do it"),
  1671. OPT_PARENT(timechart_common_options),
  1672. };
  1673. const char * const timechart_subcommands[] = { "record", NULL };
  1674. const char *timechart_usage[] = {
  1675. "perf timechart [<options>] {record}",
  1676. NULL
  1677. };
  1678. const struct option timechart_record_options[] = {
  1679. OPT_BOOLEAN('I', "io-only", &tchart.io_only,
  1680. "record only IO data"),
  1681. OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
  1682. OPT_PARENT(timechart_common_options),
  1683. };
  1684. const char * const timechart_record_usage[] = {
  1685. "perf timechart record [<options>]",
  1686. NULL
  1687. };
  1688. int ret;
  1689. cpus_cstate_start_times = calloc(MAX_CPUS, sizeof(*cpus_cstate_start_times));
  1690. if (!cpus_cstate_start_times)
  1691. return -ENOMEM;
  1692. cpus_cstate_state = calloc(MAX_CPUS, sizeof(*cpus_cstate_state));
  1693. if (!cpus_cstate_state) {
  1694. ret = -ENOMEM;
  1695. goto out;
  1696. }
  1697. cpus_pstate_start_times = calloc(MAX_CPUS, sizeof(*cpus_pstate_start_times));
  1698. if (!cpus_pstate_start_times) {
  1699. ret = -ENOMEM;
  1700. goto out;
  1701. }
  1702. cpus_pstate_state = calloc(MAX_CPUS, sizeof(*cpus_pstate_state));
  1703. if (!cpus_pstate_state) {
  1704. ret = -ENOMEM;
  1705. goto out;
  1706. }
  1707. argc = parse_options_subcommand(argc, argv, timechart_options, timechart_subcommands,
  1708. timechart_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  1709. if (tchart.power_only && tchart.tasks_only) {
  1710. pr_err("-P and -T options cannot be used at the same time.\n");
  1711. ret = -1;
  1712. goto out;
  1713. }
  1714. if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
  1715. argc = parse_options(argc, argv, timechart_record_options,
  1716. timechart_record_usage,
  1717. PARSE_OPT_STOP_AT_NON_OPTION);
  1718. if (tchart.power_only && tchart.tasks_only) {
  1719. pr_err("-P and -T options cannot be used at the same time.\n");
  1720. ret = -1;
  1721. goto out;
  1722. }
  1723. if (tchart.io_only)
  1724. ret = timechart__io_record(argc, argv);
  1725. else
  1726. ret = timechart__record(&tchart, argc, argv);
  1727. goto out;
  1728. } else if (argc)
  1729. usage_with_options(timechart_usage, timechart_options);
  1730. setup_pager();
  1731. ret = __cmd_timechart(&tchart, output_name);
  1732. out:
  1733. zfree(&cpus_cstate_start_times);
  1734. zfree(&cpus_cstate_state);
  1735. zfree(&cpus_pstate_start_times);
  1736. zfree(&cpus_pstate_state);
  1737. return ret;
  1738. }