builtin-timechart.c 47 KB

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