builtin-timechart.c 23 KB

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