builtin-timechart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 sample_data *sample __used,
  224. struct perf_session *session __used)
  225. {
  226. pid_set_comm(event->comm.tid, event->comm.comm);
  227. return 0;
  228. }
  229. static int process_fork_event(event_t *event, struct sample_data *sample __used,
  230. struct perf_session *session __used)
  231. {
  232. pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
  233. return 0;
  234. }
  235. static int process_exit_event(event_t *event, struct sample_data *sample __used,
  236. struct perf_session *session __used)
  237. {
  238. pid_exit(event->fork.pid, event->fork.time);
  239. return 0;
  240. }
  241. struct trace_entry {
  242. unsigned short type;
  243. unsigned char flags;
  244. unsigned char preempt_count;
  245. int pid;
  246. int lock_depth;
  247. };
  248. struct power_entry {
  249. struct trace_entry te;
  250. u64 type;
  251. u64 value;
  252. u64 cpu_id;
  253. };
  254. #define TASK_COMM_LEN 16
  255. struct wakeup_entry {
  256. struct trace_entry te;
  257. char comm[TASK_COMM_LEN];
  258. int pid;
  259. int prio;
  260. int success;
  261. };
  262. /*
  263. * trace_flag_type is an enumeration that holds different
  264. * states when a trace occurs. These are:
  265. * IRQS_OFF - interrupts were disabled
  266. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  267. * NEED_RESCED - reschedule is requested
  268. * HARDIRQ - inside an interrupt handler
  269. * SOFTIRQ - inside a softirq handler
  270. */
  271. enum trace_flag_type {
  272. TRACE_FLAG_IRQS_OFF = 0x01,
  273. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  274. TRACE_FLAG_NEED_RESCHED = 0x04,
  275. TRACE_FLAG_HARDIRQ = 0x08,
  276. TRACE_FLAG_SOFTIRQ = 0x10,
  277. };
  278. struct sched_switch {
  279. struct trace_entry te;
  280. char prev_comm[TASK_COMM_LEN];
  281. int prev_pid;
  282. int prev_prio;
  283. long prev_state; /* Arjan weeps. */
  284. char next_comm[TASK_COMM_LEN];
  285. int next_pid;
  286. int next_prio;
  287. };
  288. static void c_state_start(int cpu, u64 timestamp, int state)
  289. {
  290. cpus_cstate_start_times[cpu] = timestamp;
  291. cpus_cstate_state[cpu] = state;
  292. }
  293. static void c_state_end(int cpu, u64 timestamp)
  294. {
  295. struct power_event *pwr;
  296. pwr = malloc(sizeof(struct power_event));
  297. if (!pwr)
  298. return;
  299. memset(pwr, 0, sizeof(struct power_event));
  300. pwr->state = cpus_cstate_state[cpu];
  301. pwr->start_time = cpus_cstate_start_times[cpu];
  302. pwr->end_time = timestamp;
  303. pwr->cpu = cpu;
  304. pwr->type = CSTATE;
  305. pwr->next = power_events;
  306. power_events = pwr;
  307. }
  308. static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
  309. {
  310. struct power_event *pwr;
  311. pwr = malloc(sizeof(struct power_event));
  312. if (new_freq > 8000000) /* detect invalid data */
  313. return;
  314. if (!pwr)
  315. return;
  316. memset(pwr, 0, sizeof(struct power_event));
  317. pwr->state = cpus_pstate_state[cpu];
  318. pwr->start_time = cpus_pstate_start_times[cpu];
  319. pwr->end_time = timestamp;
  320. pwr->cpu = cpu;
  321. pwr->type = PSTATE;
  322. pwr->next = power_events;
  323. if (!pwr->start_time)
  324. pwr->start_time = first_time;
  325. power_events = pwr;
  326. cpus_pstate_state[cpu] = new_freq;
  327. cpus_pstate_start_times[cpu] = timestamp;
  328. if ((u64)new_freq > max_freq)
  329. max_freq = new_freq;
  330. if (new_freq < min_freq || min_freq == 0)
  331. min_freq = new_freq;
  332. if (new_freq == max_freq - 1000)
  333. turbo_frequency = max_freq;
  334. }
  335. static void
  336. sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
  337. {
  338. struct wake_event *we;
  339. struct per_pid *p;
  340. struct wakeup_entry *wake = (void *)te;
  341. we = malloc(sizeof(struct wake_event));
  342. if (!we)
  343. return;
  344. memset(we, 0, sizeof(struct wake_event));
  345. we->time = timestamp;
  346. we->waker = pid;
  347. if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
  348. we->waker = -1;
  349. we->wakee = wake->pid;
  350. we->next = wake_events;
  351. wake_events = we;
  352. p = find_create_pid(we->wakee);
  353. if (p && p->current && p->current->state == TYPE_NONE) {
  354. p->current->state_since = timestamp;
  355. p->current->state = TYPE_WAITING;
  356. }
  357. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  358. pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
  359. p->current->state_since = timestamp;
  360. p->current->state = TYPE_WAITING;
  361. }
  362. }
  363. static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
  364. {
  365. struct per_pid *p = NULL, *prev_p;
  366. struct sched_switch *sw = (void *)te;
  367. prev_p = find_create_pid(sw->prev_pid);
  368. p = find_create_pid(sw->next_pid);
  369. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  370. pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
  371. if (p && p->current) {
  372. if (p->current->state != TYPE_NONE)
  373. pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
  374. p->current->state_since = timestamp;
  375. p->current->state = TYPE_RUNNING;
  376. }
  377. if (prev_p->current) {
  378. prev_p->current->state = TYPE_NONE;
  379. prev_p->current->state_since = timestamp;
  380. if (sw->prev_state & 2)
  381. prev_p->current->state = TYPE_BLOCKED;
  382. if (sw->prev_state == 0)
  383. prev_p->current->state = TYPE_WAITING;
  384. }
  385. }
  386. static int process_sample_event(event_t *event __used,
  387. struct sample_data *sample,
  388. struct perf_session *session)
  389. {
  390. struct trace_entry *te;
  391. if (session->sample_type & PERF_SAMPLE_TIME) {
  392. if (!first_time || first_time > sample->time)
  393. first_time = sample->time;
  394. if (last_time < sample->time)
  395. last_time = sample->time;
  396. }
  397. te = (void *)sample->raw_data;
  398. if (session->sample_type & PERF_SAMPLE_RAW && sample->raw_size > 0) {
  399. char *event_str;
  400. struct power_entry *pe;
  401. pe = (void *)te;
  402. event_str = perf_header__find_event(te->type);
  403. if (!event_str)
  404. return 0;
  405. if (strcmp(event_str, "power:power_start") == 0)
  406. c_state_start(pe->cpu_id, sample->time, pe->value);
  407. if (strcmp(event_str, "power:power_end") == 0)
  408. c_state_end(pe->cpu_id, sample->time);
  409. if (strcmp(event_str, "power:power_frequency") == 0)
  410. p_state_change(pe->cpu_id, sample->time, pe->value);
  411. if (strcmp(event_str, "sched:sched_wakeup") == 0)
  412. sched_wakeup(sample->cpu, sample->time, sample->pid, te);
  413. if (strcmp(event_str, "sched:sched_switch") == 0)
  414. sched_switch(sample->cpu, sample->time, te);
  415. }
  416. return 0;
  417. }
  418. /*
  419. * After the last sample we need to wrap up the current C/P state
  420. * and close out each CPU for these.
  421. */
  422. static void end_sample_processing(void)
  423. {
  424. u64 cpu;
  425. struct power_event *pwr;
  426. for (cpu = 0; cpu <= numcpus; cpu++) {
  427. pwr = malloc(sizeof(struct power_event));
  428. if (!pwr)
  429. return;
  430. memset(pwr, 0, sizeof(struct power_event));
  431. /* C state */
  432. #if 0
  433. pwr->state = cpus_cstate_state[cpu];
  434. pwr->start_time = cpus_cstate_start_times[cpu];
  435. pwr->end_time = last_time;
  436. pwr->cpu = cpu;
  437. pwr->type = CSTATE;
  438. pwr->next = power_events;
  439. power_events = pwr;
  440. #endif
  441. /* P state */
  442. pwr = malloc(sizeof(struct power_event));
  443. if (!pwr)
  444. return;
  445. memset(pwr, 0, sizeof(struct power_event));
  446. pwr->state = cpus_pstate_state[cpu];
  447. pwr->start_time = cpus_pstate_start_times[cpu];
  448. pwr->end_time = last_time;
  449. pwr->cpu = cpu;
  450. pwr->type = PSTATE;
  451. pwr->next = power_events;
  452. if (!pwr->start_time)
  453. pwr->start_time = first_time;
  454. if (!pwr->state)
  455. pwr->state = min_freq;
  456. power_events = pwr;
  457. }
  458. }
  459. /*
  460. * Sort the pid datastructure
  461. */
  462. static void sort_pids(void)
  463. {
  464. struct per_pid *new_list, *p, *cursor, *prev;
  465. /* sort by ppid first, then by pid, lowest to highest */
  466. new_list = NULL;
  467. while (all_data) {
  468. p = all_data;
  469. all_data = p->next;
  470. p->next = NULL;
  471. if (new_list == NULL) {
  472. new_list = p;
  473. p->next = NULL;
  474. continue;
  475. }
  476. prev = NULL;
  477. cursor = new_list;
  478. while (cursor) {
  479. if (cursor->ppid > p->ppid ||
  480. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  481. /* must insert before */
  482. if (prev) {
  483. p->next = prev->next;
  484. prev->next = p;
  485. cursor = NULL;
  486. continue;
  487. } else {
  488. p->next = new_list;
  489. new_list = p;
  490. cursor = NULL;
  491. continue;
  492. }
  493. }
  494. prev = cursor;
  495. cursor = cursor->next;
  496. if (!cursor)
  497. prev->next = p;
  498. }
  499. }
  500. all_data = new_list;
  501. }
  502. static void draw_c_p_states(void)
  503. {
  504. struct power_event *pwr;
  505. pwr = power_events;
  506. /*
  507. * two pass drawing so that the P state bars are on top of the C state blocks
  508. */
  509. while (pwr) {
  510. if (pwr->type == CSTATE)
  511. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  512. pwr = pwr->next;
  513. }
  514. pwr = power_events;
  515. while (pwr) {
  516. if (pwr->type == PSTATE) {
  517. if (!pwr->state)
  518. pwr->state = min_freq;
  519. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  520. }
  521. pwr = pwr->next;
  522. }
  523. }
  524. static void draw_wakeups(void)
  525. {
  526. struct wake_event *we;
  527. struct per_pid *p;
  528. struct per_pidcomm *c;
  529. we = wake_events;
  530. while (we) {
  531. int from = 0, to = 0;
  532. char *task_from = NULL, *task_to = NULL;
  533. /* locate the column of the waker and wakee */
  534. p = all_data;
  535. while (p) {
  536. if (p->pid == we->waker || p->pid == we->wakee) {
  537. c = p->all;
  538. while (c) {
  539. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  540. if (p->pid == we->waker && !from) {
  541. from = c->Y;
  542. task_from = strdup(c->comm);
  543. }
  544. if (p->pid == we->wakee && !to) {
  545. to = c->Y;
  546. task_to = strdup(c->comm);
  547. }
  548. }
  549. c = c->next;
  550. }
  551. c = p->all;
  552. while (c) {
  553. if (p->pid == we->waker && !from) {
  554. from = c->Y;
  555. task_from = strdup(c->comm);
  556. }
  557. if (p->pid == we->wakee && !to) {
  558. to = c->Y;
  559. task_to = strdup(c->comm);
  560. }
  561. c = c->next;
  562. }
  563. }
  564. p = p->next;
  565. }
  566. if (!task_from) {
  567. task_from = malloc(40);
  568. sprintf(task_from, "[%i]", we->waker);
  569. }
  570. if (!task_to) {
  571. task_to = malloc(40);
  572. sprintf(task_to, "[%i]", we->wakee);
  573. }
  574. if (we->waker == -1)
  575. svg_interrupt(we->time, to);
  576. else if (from && to && abs(from - to) == 1)
  577. svg_wakeline(we->time, from, to);
  578. else
  579. svg_partial_wakeline(we->time, from, task_from, to, task_to);
  580. we = we->next;
  581. free(task_from);
  582. free(task_to);
  583. }
  584. }
  585. static void draw_cpu_usage(void)
  586. {
  587. struct per_pid *p;
  588. struct per_pidcomm *c;
  589. struct cpu_sample *sample;
  590. p = all_data;
  591. while (p) {
  592. c = p->all;
  593. while (c) {
  594. sample = c->samples;
  595. while (sample) {
  596. if (sample->type == TYPE_RUNNING)
  597. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  598. sample = sample->next;
  599. }
  600. c = c->next;
  601. }
  602. p = p->next;
  603. }
  604. }
  605. static void draw_process_bars(void)
  606. {
  607. struct per_pid *p;
  608. struct per_pidcomm *c;
  609. struct cpu_sample *sample;
  610. int Y = 0;
  611. Y = 2 * numcpus + 2;
  612. p = all_data;
  613. while (p) {
  614. c = p->all;
  615. while (c) {
  616. if (!c->display) {
  617. c->Y = 0;
  618. c = c->next;
  619. continue;
  620. }
  621. svg_box(Y, c->start_time, c->end_time, "process");
  622. sample = c->samples;
  623. while (sample) {
  624. if (sample->type == TYPE_RUNNING)
  625. svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
  626. if (sample->type == TYPE_BLOCKED)
  627. svg_box(Y, sample->start_time, sample->end_time, "blocked");
  628. if (sample->type == TYPE_WAITING)
  629. svg_waiting(Y, sample->start_time, sample->end_time);
  630. sample = sample->next;
  631. }
  632. if (c->comm) {
  633. char comm[256];
  634. if (c->total_time > 5000000000) /* 5 seconds */
  635. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  636. else
  637. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  638. svg_text(Y, c->start_time, comm);
  639. }
  640. c->Y = Y;
  641. Y++;
  642. c = c->next;
  643. }
  644. p = p->next;
  645. }
  646. }
  647. static void add_process_filter(const char *string)
  648. {
  649. struct process_filter *filt;
  650. int pid;
  651. pid = strtoull(string, NULL, 10);
  652. filt = malloc(sizeof(struct process_filter));
  653. if (!filt)
  654. return;
  655. filt->name = strdup(string);
  656. filt->pid = pid;
  657. filt->next = process_filter;
  658. process_filter = filt;
  659. }
  660. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  661. {
  662. struct process_filter *filt;
  663. if (!process_filter)
  664. return 1;
  665. filt = process_filter;
  666. while (filt) {
  667. if (filt->pid && p->pid == filt->pid)
  668. return 1;
  669. if (strcmp(filt->name, c->comm) == 0)
  670. return 1;
  671. filt = filt->next;
  672. }
  673. return 0;
  674. }
  675. static int determine_display_tasks_filtered(void)
  676. {
  677. struct per_pid *p;
  678. struct per_pidcomm *c;
  679. int count = 0;
  680. p = all_data;
  681. while (p) {
  682. p->display = 0;
  683. if (p->start_time == 1)
  684. p->start_time = first_time;
  685. /* no exit marker, task kept running to the end */
  686. if (p->end_time == 0)
  687. p->end_time = last_time;
  688. c = p->all;
  689. while (c) {
  690. c->display = 0;
  691. if (c->start_time == 1)
  692. c->start_time = first_time;
  693. if (passes_filter(p, c)) {
  694. c->display = 1;
  695. p->display = 1;
  696. count++;
  697. }
  698. if (c->end_time == 0)
  699. c->end_time = last_time;
  700. c = c->next;
  701. }
  702. p = p->next;
  703. }
  704. return count;
  705. }
  706. static int determine_display_tasks(u64 threshold)
  707. {
  708. struct per_pid *p;
  709. struct per_pidcomm *c;
  710. int count = 0;
  711. if (process_filter)
  712. return determine_display_tasks_filtered();
  713. p = all_data;
  714. while (p) {
  715. p->display = 0;
  716. if (p->start_time == 1)
  717. p->start_time = first_time;
  718. /* no exit marker, task kept running to the end */
  719. if (p->end_time == 0)
  720. p->end_time = last_time;
  721. if (p->total_time >= threshold && !power_only)
  722. p->display = 1;
  723. c = p->all;
  724. while (c) {
  725. c->display = 0;
  726. if (c->start_time == 1)
  727. c->start_time = first_time;
  728. if (c->total_time >= threshold && !power_only) {
  729. c->display = 1;
  730. count++;
  731. }
  732. if (c->end_time == 0)
  733. c->end_time = last_time;
  734. c = c->next;
  735. }
  736. p = p->next;
  737. }
  738. return count;
  739. }
  740. #define TIME_THRESH 10000000
  741. static void write_svg_file(const char *filename)
  742. {
  743. u64 i;
  744. int count;
  745. numcpus++;
  746. count = determine_display_tasks(TIME_THRESH);
  747. /* We'd like to show at least 15 tasks; be less picky if we have fewer */
  748. if (count < 15)
  749. count = determine_display_tasks(TIME_THRESH / 10);
  750. open_svg(filename, numcpus, count, first_time, last_time);
  751. svg_time_grid();
  752. svg_legenda();
  753. for (i = 0; i < numcpus; i++)
  754. svg_cpu_box(i, max_freq, turbo_frequency);
  755. draw_cpu_usage();
  756. draw_process_bars();
  757. draw_c_p_states();
  758. draw_wakeups();
  759. svg_close();
  760. }
  761. static struct perf_event_ops event_ops = {
  762. .comm = process_comm_event,
  763. .fork = process_fork_event,
  764. .exit = process_exit_event,
  765. .sample = process_sample_event,
  766. .ordered_samples = true,
  767. };
  768. static int __cmd_timechart(void)
  769. {
  770. struct perf_session *session = perf_session__new(input_name, O_RDONLY,
  771. 0, false, &event_ops);
  772. int ret = -EINVAL;
  773. if (session == NULL)
  774. return -ENOMEM;
  775. if (!perf_session__has_traces(session, "timechart record"))
  776. goto out_delete;
  777. ret = perf_session__process_events(session, &event_ops);
  778. if (ret)
  779. goto out_delete;
  780. end_sample_processing();
  781. sort_pids();
  782. write_svg_file(output_name);
  783. pr_info("Written %2.1f seconds of trace to %s.\n",
  784. (last_time - first_time) / 1000000000.0, output_name);
  785. out_delete:
  786. perf_session__delete(session);
  787. return ret;
  788. }
  789. static const char * const timechart_usage[] = {
  790. "perf timechart [<options>] {record}",
  791. NULL
  792. };
  793. static const char *record_args[] = {
  794. "record",
  795. "-a",
  796. "-R",
  797. "-f",
  798. "-c", "1",
  799. "-e", "power:power_start",
  800. "-e", "power:power_end",
  801. "-e", "power:power_frequency",
  802. "-e", "sched:sched_wakeup",
  803. "-e", "sched:sched_switch",
  804. };
  805. static int __cmd_record(int argc, const char **argv)
  806. {
  807. unsigned int rec_argc, i, j;
  808. const char **rec_argv;
  809. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  810. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  811. if (rec_argv == NULL)
  812. return -ENOMEM;
  813. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  814. rec_argv[i] = strdup(record_args[i]);
  815. for (j = 1; j < (unsigned int)argc; j++, i++)
  816. rec_argv[i] = argv[j];
  817. return cmd_record(i, rec_argv, NULL);
  818. }
  819. static int
  820. parse_process(const struct option *opt __used, const char *arg, int __used unset)
  821. {
  822. if (arg)
  823. add_process_filter(arg);
  824. return 0;
  825. }
  826. static const struct option options[] = {
  827. OPT_STRING('i', "input", &input_name, "file",
  828. "input file name"),
  829. OPT_STRING('o', "output", &output_name, "file",
  830. "output file name"),
  831. OPT_INTEGER('w', "width", &svg_page_width,
  832. "page width"),
  833. OPT_BOOLEAN('P', "power-only", &power_only,
  834. "output power data only"),
  835. OPT_CALLBACK('p', "process", NULL, "process",
  836. "process selector. Pass a pid or process name.",
  837. parse_process),
  838. OPT_END()
  839. };
  840. int cmd_timechart(int argc, const char **argv, const char *prefix __used)
  841. {
  842. argc = parse_options(argc, argv, options, timechart_usage,
  843. PARSE_OPT_STOP_AT_NON_OPTION);
  844. symbol__init();
  845. if (argc && !strncmp(argv[0], "rec", 3))
  846. return __cmd_record(argc, argv);
  847. else if (argc)
  848. usage_with_options(timechart_usage, options);
  849. setup_pager();
  850. return __cmd_timechart();
  851. }