builtin-timechart.c 23 KB

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