builtin-timechart.c 47 KB

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