builtin-timechart.c 23 KB

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