builtin-timechart.c 21 KB

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