builtin-timechart.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. #define SUPPORT_OLD_POWER_EVENTS 1
  35. #define PWR_EVENT_EXIT -1
  36. static unsigned int numcpus;
  37. static u64 min_freq; /* Lowest CPU frequency seen */
  38. static u64 max_freq; /* Highest CPU frequency seen */
  39. static u64 turbo_frequency;
  40. static u64 first_time, last_time;
  41. static bool power_only;
  42. struct per_pid;
  43. struct per_pidcomm;
  44. struct cpu_sample;
  45. struct power_event;
  46. struct wake_event;
  47. struct sample_wrapper;
  48. /*
  49. * Datastructure layout:
  50. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  51. * Each "pid" entry, has a list of "comm"s.
  52. * this is because we want to track different programs different, while
  53. * exec will reuse the original pid (by design).
  54. * Each comm has a list of samples that will be used to draw
  55. * final graph.
  56. */
  57. struct per_pid {
  58. struct per_pid *next;
  59. int pid;
  60. int ppid;
  61. u64 start_time;
  62. u64 end_time;
  63. u64 total_time;
  64. int display;
  65. struct per_pidcomm *all;
  66. struct per_pidcomm *current;
  67. };
  68. struct per_pidcomm {
  69. struct per_pidcomm *next;
  70. u64 start_time;
  71. u64 end_time;
  72. u64 total_time;
  73. int Y;
  74. int display;
  75. long state;
  76. u64 state_since;
  77. char *comm;
  78. struct cpu_sample *samples;
  79. };
  80. struct sample_wrapper {
  81. struct sample_wrapper *next;
  82. u64 timestamp;
  83. unsigned char data[0];
  84. };
  85. #define TYPE_NONE 0
  86. #define TYPE_RUNNING 1
  87. #define TYPE_WAITING 2
  88. #define TYPE_BLOCKED 3
  89. struct cpu_sample {
  90. struct cpu_sample *next;
  91. u64 start_time;
  92. u64 end_time;
  93. int type;
  94. int cpu;
  95. };
  96. static struct per_pid *all_data;
  97. #define CSTATE 1
  98. #define PSTATE 2
  99. struct power_event {
  100. struct power_event *next;
  101. int type;
  102. int state;
  103. u64 start_time;
  104. u64 end_time;
  105. int cpu;
  106. };
  107. struct wake_event {
  108. struct wake_event *next;
  109. int waker;
  110. int wakee;
  111. u64 time;
  112. };
  113. static struct power_event *power_events;
  114. static struct wake_event *wake_events;
  115. struct process_filter;
  116. struct process_filter {
  117. char *name;
  118. int pid;
  119. struct process_filter *next;
  120. };
  121. static struct process_filter *process_filter;
  122. static struct per_pid *find_create_pid(int pid)
  123. {
  124. struct per_pid *cursor = all_data;
  125. while (cursor) {
  126. if (cursor->pid == pid)
  127. return cursor;
  128. cursor = cursor->next;
  129. }
  130. cursor = zalloc(sizeof(*cursor));
  131. assert(cursor != NULL);
  132. cursor->pid = pid;
  133. cursor->next = all_data;
  134. all_data = cursor;
  135. return cursor;
  136. }
  137. static void pid_set_comm(int pid, char *comm)
  138. {
  139. struct per_pid *p;
  140. struct per_pidcomm *c;
  141. p = find_create_pid(pid);
  142. c = p->all;
  143. while (c) {
  144. if (c->comm && strcmp(c->comm, comm) == 0) {
  145. p->current = c;
  146. return;
  147. }
  148. if (!c->comm) {
  149. c->comm = strdup(comm);
  150. p->current = c;
  151. return;
  152. }
  153. c = c->next;
  154. }
  155. c = zalloc(sizeof(*c));
  156. assert(c != NULL);
  157. c->comm = strdup(comm);
  158. p->current = c;
  159. c->next = p->all;
  160. p->all = c;
  161. }
  162. static void pid_fork(int pid, int ppid, u64 timestamp)
  163. {
  164. struct per_pid *p, *pp;
  165. p = find_create_pid(pid);
  166. pp = find_create_pid(ppid);
  167. p->ppid = ppid;
  168. if (pp->current && pp->current->comm && !p->current)
  169. pid_set_comm(pid, pp->current->comm);
  170. p->start_time = timestamp;
  171. if (p->current) {
  172. p->current->start_time = timestamp;
  173. p->current->state_since = timestamp;
  174. }
  175. }
  176. static void pid_exit(int pid, u64 timestamp)
  177. {
  178. struct per_pid *p;
  179. p = find_create_pid(pid);
  180. p->end_time = timestamp;
  181. if (p->current)
  182. p->current->end_time = timestamp;
  183. }
  184. static void
  185. pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
  186. {
  187. struct per_pid *p;
  188. struct per_pidcomm *c;
  189. struct cpu_sample *sample;
  190. p = find_create_pid(pid);
  191. c = p->current;
  192. if (!c) {
  193. c = zalloc(sizeof(*c));
  194. assert(c != NULL);
  195. p->current = c;
  196. c->next = p->all;
  197. p->all = c;
  198. }
  199. sample = zalloc(sizeof(*sample));
  200. assert(sample != NULL);
  201. sample->start_time = start;
  202. sample->end_time = end;
  203. sample->type = type;
  204. sample->next = c->samples;
  205. sample->cpu = cpu;
  206. c->samples = sample;
  207. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  208. c->total_time += (end-start);
  209. p->total_time += (end-start);
  210. }
  211. if (c->start_time == 0 || c->start_time > start)
  212. c->start_time = start;
  213. if (p->start_time == 0 || p->start_time > start)
  214. p->start_time = start;
  215. }
  216. #define MAX_CPUS 4096
  217. static u64 cpus_cstate_start_times[MAX_CPUS];
  218. static int cpus_cstate_state[MAX_CPUS];
  219. static u64 cpus_pstate_start_times[MAX_CPUS];
  220. static u64 cpus_pstate_state[MAX_CPUS];
  221. static int process_comm_event(struct perf_tool *tool __maybe_unused,
  222. union perf_event *event,
  223. struct perf_sample *sample __maybe_unused,
  224. struct machine *machine __maybe_unused)
  225. {
  226. pid_set_comm(event->comm.tid, event->comm.comm);
  227. return 0;
  228. }
  229. static int process_fork_event(struct perf_tool *tool __maybe_unused,
  230. union perf_event *event,
  231. struct perf_sample *sample __maybe_unused,
  232. struct machine *machine __maybe_unused)
  233. {
  234. pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
  235. return 0;
  236. }
  237. static int process_exit_event(struct perf_tool *tool __maybe_unused,
  238. union perf_event *event,
  239. struct perf_sample *sample __maybe_unused,
  240. struct machine *machine __maybe_unused)
  241. {
  242. pid_exit(event->fork.pid, event->fork.time);
  243. return 0;
  244. }
  245. struct trace_entry {
  246. unsigned short type;
  247. unsigned char flags;
  248. unsigned char preempt_count;
  249. int pid;
  250. int lock_depth;
  251. };
  252. #ifdef SUPPORT_OLD_POWER_EVENTS
  253. static int use_old_power_events;
  254. struct power_entry_old {
  255. struct trace_entry te;
  256. u64 type;
  257. u64 value;
  258. u64 cpu_id;
  259. };
  260. #endif
  261. struct power_processor_entry {
  262. struct trace_entry te;
  263. u32 state;
  264. u32 cpu_id;
  265. };
  266. #define TASK_COMM_LEN 16
  267. struct wakeup_entry {
  268. struct trace_entry te;
  269. char comm[TASK_COMM_LEN];
  270. int pid;
  271. int prio;
  272. int success;
  273. };
  274. struct sched_switch {
  275. struct trace_entry te;
  276. char prev_comm[TASK_COMM_LEN];
  277. int prev_pid;
  278. int prev_prio;
  279. long prev_state; /* Arjan weeps. */
  280. char next_comm[TASK_COMM_LEN];
  281. int next_pid;
  282. int next_prio;
  283. };
  284. static void c_state_start(int cpu, u64 timestamp, int state)
  285. {
  286. cpus_cstate_start_times[cpu] = timestamp;
  287. cpus_cstate_state[cpu] = state;
  288. }
  289. static void c_state_end(int cpu, u64 timestamp)
  290. {
  291. struct power_event *pwr = zalloc(sizeof(*pwr));
  292. if (!pwr)
  293. return;
  294. pwr->state = cpus_cstate_state[cpu];
  295. pwr->start_time = cpus_cstate_start_times[cpu];
  296. pwr->end_time = timestamp;
  297. pwr->cpu = cpu;
  298. pwr->type = CSTATE;
  299. pwr->next = power_events;
  300. power_events = pwr;
  301. }
  302. static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
  303. {
  304. struct power_event *pwr;
  305. if (new_freq > 8000000) /* detect invalid data */
  306. return;
  307. pwr = zalloc(sizeof(*pwr));
  308. if (!pwr)
  309. return;
  310. pwr->state = cpus_pstate_state[cpu];
  311. pwr->start_time = cpus_pstate_start_times[cpu];
  312. pwr->end_time = timestamp;
  313. pwr->cpu = cpu;
  314. pwr->type = PSTATE;
  315. pwr->next = power_events;
  316. if (!pwr->start_time)
  317. pwr->start_time = first_time;
  318. power_events = pwr;
  319. cpus_pstate_state[cpu] = new_freq;
  320. cpus_pstate_start_times[cpu] = timestamp;
  321. if ((u64)new_freq > max_freq)
  322. max_freq = new_freq;
  323. if (new_freq < min_freq || min_freq == 0)
  324. min_freq = new_freq;
  325. if (new_freq == max_freq - 1000)
  326. turbo_frequency = max_freq;
  327. }
  328. static void
  329. sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
  330. {
  331. struct per_pid *p;
  332. struct wakeup_entry *wake = (void *)te;
  333. struct wake_event *we = zalloc(sizeof(*we));
  334. if (!we)
  335. return;
  336. we->time = timestamp;
  337. we->waker = pid;
  338. if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
  339. we->waker = -1;
  340. we->wakee = wake->pid;
  341. we->next = wake_events;
  342. wake_events = we;
  343. p = find_create_pid(we->wakee);
  344. if (p && p->current && p->current->state == TYPE_NONE) {
  345. p->current->state_since = timestamp;
  346. p->current->state = TYPE_WAITING;
  347. }
  348. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  349. pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
  350. p->current->state_since = timestamp;
  351. p->current->state = TYPE_WAITING;
  352. }
  353. }
  354. static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
  355. {
  356. struct per_pid *p = NULL, *prev_p;
  357. struct sched_switch *sw = (void *)te;
  358. prev_p = find_create_pid(sw->prev_pid);
  359. p = find_create_pid(sw->next_pid);
  360. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  361. pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
  362. if (p && p->current) {
  363. if (p->current->state != TYPE_NONE)
  364. pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
  365. p->current->state_since = timestamp;
  366. p->current->state = TYPE_RUNNING;
  367. }
  368. if (prev_p->current) {
  369. prev_p->current->state = TYPE_NONE;
  370. prev_p->current->state_since = timestamp;
  371. if (sw->prev_state & 2)
  372. prev_p->current->state = TYPE_BLOCKED;
  373. if (sw->prev_state == 0)
  374. prev_p->current->state = TYPE_WAITING;
  375. }
  376. }
  377. typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  378. struct perf_sample *sample);
  379. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  380. union perf_event *event __maybe_unused,
  381. struct perf_sample *sample,
  382. struct perf_evsel *evsel,
  383. struct machine *machine __maybe_unused)
  384. {
  385. if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
  386. if (!first_time || first_time > sample->time)
  387. first_time = sample->time;
  388. if (last_time < sample->time)
  389. last_time = sample->time;
  390. }
  391. if (sample->cpu > numcpus)
  392. numcpus = sample->cpu;
  393. if (evsel->handler.func != NULL) {
  394. tracepoint_handler f = evsel->handler.func;
  395. return f(evsel, sample);
  396. }
  397. return 0;
  398. }
  399. static int
  400. process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
  401. struct perf_sample *sample)
  402. {
  403. struct power_processor_entry *ppe = sample->raw_data;
  404. if (ppe->state == (u32) PWR_EVENT_EXIT)
  405. c_state_end(ppe->cpu_id, sample->time);
  406. else
  407. c_state_start(ppe->cpu_id, sample->time, ppe->state);
  408. return 0;
  409. }
  410. static int
  411. process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
  412. struct perf_sample *sample)
  413. {
  414. struct power_processor_entry *ppe = sample->raw_data;
  415. p_state_change(ppe->cpu_id, sample->time, ppe->state);
  416. return 0;
  417. }
  418. static int
  419. process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
  420. struct perf_sample *sample)
  421. {
  422. struct trace_entry *te = sample->raw_data;
  423. sched_wakeup(sample->cpu, sample->time, sample->pid, te);
  424. return 0;
  425. }
  426. static int
  427. process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
  428. struct perf_sample *sample)
  429. {
  430. struct trace_entry *te = sample->raw_data;
  431. sched_switch(sample->cpu, sample->time, te);
  432. return 0;
  433. }
  434. #ifdef SUPPORT_OLD_POWER_EVENTS
  435. static int
  436. process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
  437. struct perf_sample *sample)
  438. {
  439. struct power_entry_old *peo = sample->raw_data;
  440. c_state_start(peo->cpu_id, sample->time, peo->value);
  441. return 0;
  442. }
  443. static int
  444. process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
  445. struct perf_sample *sample)
  446. {
  447. c_state_end(sample->cpu, sample->time);
  448. return 0;
  449. }
  450. static int
  451. process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
  452. struct perf_sample *sample)
  453. {
  454. struct power_entry_old *peo = sample->raw_data;
  455. p_state_change(peo->cpu_id, sample->time, peo->value);
  456. return 0;
  457. }
  458. #endif /* SUPPORT_OLD_POWER_EVENTS */
  459. /*
  460. * After the last sample we need to wrap up the current C/P state
  461. * and close out each CPU for these.
  462. */
  463. static void end_sample_processing(void)
  464. {
  465. u64 cpu;
  466. struct power_event *pwr;
  467. for (cpu = 0; cpu <= numcpus; cpu++) {
  468. /* C state */
  469. #if 0
  470. pwr = zalloc(sizeof(*pwr));
  471. if (!pwr)
  472. return;
  473. pwr->state = cpus_cstate_state[cpu];
  474. pwr->start_time = cpus_cstate_start_times[cpu];
  475. pwr->end_time = last_time;
  476. pwr->cpu = cpu;
  477. pwr->type = CSTATE;
  478. pwr->next = power_events;
  479. power_events = pwr;
  480. #endif
  481. /* P state */
  482. pwr = zalloc(sizeof(*pwr));
  483. if (!pwr)
  484. return;
  485. pwr->state = cpus_pstate_state[cpu];
  486. pwr->start_time = cpus_pstate_start_times[cpu];
  487. pwr->end_time = last_time;
  488. pwr->cpu = cpu;
  489. pwr->type = PSTATE;
  490. pwr->next = power_events;
  491. if (!pwr->start_time)
  492. pwr->start_time = first_time;
  493. if (!pwr->state)
  494. pwr->state = min_freq;
  495. power_events = pwr;
  496. }
  497. }
  498. /*
  499. * Sort the pid datastructure
  500. */
  501. static void sort_pids(void)
  502. {
  503. struct per_pid *new_list, *p, *cursor, *prev;
  504. /* sort by ppid first, then by pid, lowest to highest */
  505. new_list = NULL;
  506. while (all_data) {
  507. p = all_data;
  508. all_data = p->next;
  509. p->next = NULL;
  510. if (new_list == NULL) {
  511. new_list = p;
  512. p->next = NULL;
  513. continue;
  514. }
  515. prev = NULL;
  516. cursor = new_list;
  517. while (cursor) {
  518. if (cursor->ppid > p->ppid ||
  519. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  520. /* must insert before */
  521. if (prev) {
  522. p->next = prev->next;
  523. prev->next = p;
  524. cursor = NULL;
  525. continue;
  526. } else {
  527. p->next = new_list;
  528. new_list = p;
  529. cursor = NULL;
  530. continue;
  531. }
  532. }
  533. prev = cursor;
  534. cursor = cursor->next;
  535. if (!cursor)
  536. prev->next = p;
  537. }
  538. }
  539. all_data = new_list;
  540. }
  541. static void draw_c_p_states(void)
  542. {
  543. struct power_event *pwr;
  544. pwr = power_events;
  545. /*
  546. * two pass drawing so that the P state bars are on top of the C state blocks
  547. */
  548. while (pwr) {
  549. if (pwr->type == CSTATE)
  550. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  551. pwr = pwr->next;
  552. }
  553. pwr = power_events;
  554. while (pwr) {
  555. if (pwr->type == PSTATE) {
  556. if (!pwr->state)
  557. pwr->state = min_freq;
  558. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  559. }
  560. pwr = pwr->next;
  561. }
  562. }
  563. static void draw_wakeups(void)
  564. {
  565. struct wake_event *we;
  566. struct per_pid *p;
  567. struct per_pidcomm *c;
  568. we = wake_events;
  569. while (we) {
  570. int from = 0, to = 0;
  571. char *task_from = NULL, *task_to = NULL;
  572. /* locate the column of the waker and wakee */
  573. p = all_data;
  574. while (p) {
  575. if (p->pid == we->waker || p->pid == we->wakee) {
  576. c = p->all;
  577. while (c) {
  578. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  579. if (p->pid == we->waker && !from) {
  580. from = c->Y;
  581. task_from = strdup(c->comm);
  582. }
  583. if (p->pid == we->wakee && !to) {
  584. to = c->Y;
  585. task_to = strdup(c->comm);
  586. }
  587. }
  588. c = c->next;
  589. }
  590. c = p->all;
  591. while (c) {
  592. if (p->pid == we->waker && !from) {
  593. from = c->Y;
  594. task_from = strdup(c->comm);
  595. }
  596. if (p->pid == we->wakee && !to) {
  597. to = c->Y;
  598. task_to = strdup(c->comm);
  599. }
  600. c = c->next;
  601. }
  602. }
  603. p = p->next;
  604. }
  605. if (!task_from) {
  606. task_from = malloc(40);
  607. sprintf(task_from, "[%i]", we->waker);
  608. }
  609. if (!task_to) {
  610. task_to = malloc(40);
  611. sprintf(task_to, "[%i]", we->wakee);
  612. }
  613. if (we->waker == -1)
  614. svg_interrupt(we->time, to);
  615. else if (from && to && abs(from - to) == 1)
  616. svg_wakeline(we->time, from, to);
  617. else
  618. svg_partial_wakeline(we->time, from, task_from, to, task_to);
  619. we = we->next;
  620. free(task_from);
  621. free(task_to);
  622. }
  623. }
  624. static void draw_cpu_usage(void)
  625. {
  626. struct per_pid *p;
  627. struct per_pidcomm *c;
  628. struct cpu_sample *sample;
  629. p = all_data;
  630. while (p) {
  631. c = p->all;
  632. while (c) {
  633. sample = c->samples;
  634. while (sample) {
  635. if (sample->type == TYPE_RUNNING)
  636. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  637. sample = sample->next;
  638. }
  639. c = c->next;
  640. }
  641. p = p->next;
  642. }
  643. }
  644. static void draw_process_bars(void)
  645. {
  646. struct per_pid *p;
  647. struct per_pidcomm *c;
  648. struct cpu_sample *sample;
  649. int Y = 0;
  650. Y = 2 * numcpus + 2;
  651. p = all_data;
  652. while (p) {
  653. c = p->all;
  654. while (c) {
  655. if (!c->display) {
  656. c->Y = 0;
  657. c = c->next;
  658. continue;
  659. }
  660. svg_box(Y, c->start_time, c->end_time, "process");
  661. sample = c->samples;
  662. while (sample) {
  663. if (sample->type == TYPE_RUNNING)
  664. svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
  665. if (sample->type == TYPE_BLOCKED)
  666. svg_box(Y, sample->start_time, sample->end_time, "blocked");
  667. if (sample->type == TYPE_WAITING)
  668. svg_waiting(Y, sample->start_time, sample->end_time);
  669. sample = sample->next;
  670. }
  671. if (c->comm) {
  672. char comm[256];
  673. if (c->total_time > 5000000000) /* 5 seconds */
  674. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  675. else
  676. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  677. svg_text(Y, c->start_time, comm);
  678. }
  679. c->Y = Y;
  680. Y++;
  681. c = c->next;
  682. }
  683. p = p->next;
  684. }
  685. }
  686. static void add_process_filter(const char *string)
  687. {
  688. int pid = strtoull(string, NULL, 10);
  689. struct process_filter *filt = malloc(sizeof(*filt));
  690. if (!filt)
  691. return;
  692. filt->name = strdup(string);
  693. filt->pid = pid;
  694. filt->next = process_filter;
  695. process_filter = filt;
  696. }
  697. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  698. {
  699. struct process_filter *filt;
  700. if (!process_filter)
  701. return 1;
  702. filt = process_filter;
  703. while (filt) {
  704. if (filt->pid && p->pid == filt->pid)
  705. return 1;
  706. if (strcmp(filt->name, c->comm) == 0)
  707. return 1;
  708. filt = filt->next;
  709. }
  710. return 0;
  711. }
  712. static int determine_display_tasks_filtered(void)
  713. {
  714. struct per_pid *p;
  715. struct per_pidcomm *c;
  716. int count = 0;
  717. p = all_data;
  718. while (p) {
  719. p->display = 0;
  720. if (p->start_time == 1)
  721. p->start_time = first_time;
  722. /* no exit marker, task kept running to the end */
  723. if (p->end_time == 0)
  724. p->end_time = last_time;
  725. c = p->all;
  726. while (c) {
  727. c->display = 0;
  728. if (c->start_time == 1)
  729. c->start_time = first_time;
  730. if (passes_filter(p, c)) {
  731. c->display = 1;
  732. p->display = 1;
  733. count++;
  734. }
  735. if (c->end_time == 0)
  736. c->end_time = last_time;
  737. c = c->next;
  738. }
  739. p = p->next;
  740. }
  741. return count;
  742. }
  743. static int determine_display_tasks(u64 threshold)
  744. {
  745. struct per_pid *p;
  746. struct per_pidcomm *c;
  747. int count = 0;
  748. if (process_filter)
  749. return determine_display_tasks_filtered();
  750. p = all_data;
  751. while (p) {
  752. p->display = 0;
  753. if (p->start_time == 1)
  754. p->start_time = first_time;
  755. /* no exit marker, task kept running to the end */
  756. if (p->end_time == 0)
  757. p->end_time = last_time;
  758. if (p->total_time >= threshold && !power_only)
  759. p->display = 1;
  760. c = p->all;
  761. while (c) {
  762. c->display = 0;
  763. if (c->start_time == 1)
  764. c->start_time = first_time;
  765. if (c->total_time >= threshold && !power_only) {
  766. c->display = 1;
  767. count++;
  768. }
  769. if (c->end_time == 0)
  770. c->end_time = last_time;
  771. c = c->next;
  772. }
  773. p = p->next;
  774. }
  775. return count;
  776. }
  777. #define TIME_THRESH 10000000
  778. static void write_svg_file(const char *filename)
  779. {
  780. u64 i;
  781. int count;
  782. numcpus++;
  783. count = determine_display_tasks(TIME_THRESH);
  784. /* We'd like to show at least 15 tasks; be less picky if we have fewer */
  785. if (count < 15)
  786. count = determine_display_tasks(TIME_THRESH / 10);
  787. open_svg(filename, numcpus, count, first_time, last_time);
  788. svg_time_grid();
  789. svg_legenda();
  790. for (i = 0; i < numcpus; i++)
  791. svg_cpu_box(i, max_freq, turbo_frequency);
  792. draw_cpu_usage();
  793. draw_process_bars();
  794. draw_c_p_states();
  795. draw_wakeups();
  796. svg_close();
  797. }
  798. static int __cmd_timechart(const char *output_name)
  799. {
  800. struct perf_tool perf_timechart = {
  801. .comm = process_comm_event,
  802. .fork = process_fork_event,
  803. .exit = process_exit_event,
  804. .sample = process_sample_event,
  805. .ordered_samples = true,
  806. };
  807. const struct perf_evsel_str_handler power_tracepoints[] = {
  808. { "power:cpu_idle", process_sample_cpu_idle },
  809. { "power:cpu_frequency", process_sample_cpu_frequency },
  810. { "sched:sched_wakeup", process_sample_sched_wakeup },
  811. { "sched:sched_switch", process_sample_sched_switch },
  812. #ifdef SUPPORT_OLD_POWER_EVENTS
  813. { "power:power_start", process_sample_power_start },
  814. { "power:power_end", process_sample_power_end },
  815. { "power:power_frequency", process_sample_power_frequency },
  816. #endif
  817. };
  818. struct perf_session *session = perf_session__new(input_name, O_RDONLY,
  819. 0, false, &perf_timechart);
  820. int ret = -EINVAL;
  821. if (session == NULL)
  822. return -ENOMEM;
  823. if (!perf_session__has_traces(session, "timechart record"))
  824. goto out_delete;
  825. if (perf_session__set_tracepoints_handlers(session,
  826. power_tracepoints)) {
  827. pr_err("Initializing session tracepoint handlers failed\n");
  828. goto out_delete;
  829. }
  830. ret = perf_session__process_events(session, &perf_timechart);
  831. if (ret)
  832. goto out_delete;
  833. end_sample_processing();
  834. sort_pids();
  835. write_svg_file(output_name);
  836. pr_info("Written %2.1f seconds of trace to %s.\n",
  837. (last_time - first_time) / 1000000000.0, output_name);
  838. out_delete:
  839. perf_session__delete(session);
  840. return ret;
  841. }
  842. static int __cmd_record(int argc, const char **argv)
  843. {
  844. #ifdef SUPPORT_OLD_POWER_EVENTS
  845. const char * const record_old_args[] = {
  846. "record", "-a", "-R", "-c", "1",
  847. "-e", "power:power_start",
  848. "-e", "power:power_end",
  849. "-e", "power:power_frequency",
  850. "-e", "sched:sched_wakeup",
  851. "-e", "sched:sched_switch",
  852. };
  853. #endif
  854. const char * const record_new_args[] = {
  855. "record", "-a", "-R", "-c", "1",
  856. "-e", "power:cpu_frequency",
  857. "-e", "power:cpu_idle",
  858. "-e", "sched:sched_wakeup",
  859. "-e", "sched:sched_switch",
  860. };
  861. unsigned int rec_argc, i, j;
  862. const char **rec_argv;
  863. const char * const *record_args = record_new_args;
  864. unsigned int record_elems = ARRAY_SIZE(record_new_args);
  865. #ifdef SUPPORT_OLD_POWER_EVENTS
  866. if (!is_valid_tracepoint("power:cpu_idle") &&
  867. is_valid_tracepoint("power:power_start")) {
  868. use_old_power_events = 1;
  869. record_args = record_old_args;
  870. record_elems = ARRAY_SIZE(record_old_args);
  871. }
  872. #endif
  873. rec_argc = record_elems + argc - 1;
  874. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  875. if (rec_argv == NULL)
  876. return -ENOMEM;
  877. for (i = 0; i < record_elems; i++)
  878. rec_argv[i] = strdup(record_args[i]);
  879. for (j = 1; j < (unsigned int)argc; j++, i++)
  880. rec_argv[i] = argv[j];
  881. return cmd_record(i, rec_argv, NULL);
  882. }
  883. static int
  884. parse_process(const struct option *opt __maybe_unused, const char *arg,
  885. int __maybe_unused unset)
  886. {
  887. if (arg)
  888. add_process_filter(arg);
  889. return 0;
  890. }
  891. int cmd_timechart(int argc, const char **argv,
  892. const char *prefix __maybe_unused)
  893. {
  894. const char *output_name = "output.svg";
  895. const struct option options[] = {
  896. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  897. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  898. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  899. OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
  900. OPT_CALLBACK('p', "process", NULL, "process",
  901. "process selector. Pass a pid or process name.",
  902. parse_process),
  903. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  904. "Look for files with symbols relative to this directory"),
  905. OPT_END()
  906. };
  907. const char * const timechart_usage[] = {
  908. "perf timechart [<options>] {record}",
  909. NULL
  910. };
  911. argc = parse_options(argc, argv, options, timechart_usage,
  912. PARSE_OPT_STOP_AT_NON_OPTION);
  913. symbol__init();
  914. if (argc && !strncmp(argv[0], "rec", 3))
  915. return __cmd_record(argc, argv);
  916. else if (argc)
  917. usage_with_options(timechart_usage, options);
  918. setup_pager();
  919. return __cmd_timechart(output_name);
  920. }