builtin-timechart.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. /* locate the column of the waker and wakee */
  617. p = all_data;
  618. while (p) {
  619. if (p->pid == we->waker || p->pid == we->wakee) {
  620. c = p->all;
  621. while (c) {
  622. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  623. if (p->pid == we->waker)
  624. from = c->Y;
  625. if (p->pid == we->wakee)
  626. to = c->Y;
  627. }
  628. c = c->next;
  629. }
  630. }
  631. p = p->next;
  632. }
  633. if (we->waker == -1)
  634. svg_interrupt(we->time, to);
  635. else if (from && to && abs(from - to) == 1)
  636. svg_wakeline(we->time, from, to);
  637. else
  638. svg_partial_wakeline(we->time, from, to);
  639. we = we->next;
  640. }
  641. }
  642. static void draw_cpu_usage(void)
  643. {
  644. struct per_pid *p;
  645. struct per_pidcomm *c;
  646. struct cpu_sample *sample;
  647. p = all_data;
  648. while (p) {
  649. c = p->all;
  650. while (c) {
  651. sample = c->samples;
  652. while (sample) {
  653. if (sample->type == TYPE_RUNNING)
  654. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  655. sample = sample->next;
  656. }
  657. c = c->next;
  658. }
  659. p = p->next;
  660. }
  661. }
  662. static void draw_process_bars(void)
  663. {
  664. struct per_pid *p;
  665. struct per_pidcomm *c;
  666. struct cpu_sample *sample;
  667. int Y = 0;
  668. Y = 2 * numcpus + 2;
  669. p = all_data;
  670. while (p) {
  671. c = p->all;
  672. while (c) {
  673. if (!c->display) {
  674. c->Y = 0;
  675. c = c->next;
  676. continue;
  677. }
  678. svg_box(Y, p->start_time, p->end_time, "process");
  679. sample = c->samples;
  680. while (sample) {
  681. if (sample->type == TYPE_RUNNING)
  682. svg_sample(Y, sample->cpu, sample->start_time, sample->end_time, "sample");
  683. if (sample->type == TYPE_BLOCKED)
  684. svg_box(Y, sample->start_time, sample->end_time, "blocked");
  685. if (sample->type == TYPE_WAITING)
  686. svg_box(Y, sample->start_time, sample->end_time, "waiting");
  687. sample = sample->next;
  688. }
  689. if (c->comm) {
  690. char comm[256];
  691. if (c->total_time > 5000000000) /* 5 seconds */
  692. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  693. else
  694. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  695. svg_text(Y, c->start_time, comm);
  696. }
  697. c->Y = Y;
  698. Y++;
  699. c = c->next;
  700. }
  701. p = p->next;
  702. }
  703. }
  704. static int determine_display_tasks(u64 threshold)
  705. {
  706. struct per_pid *p;
  707. struct per_pidcomm *c;
  708. int count = 0;
  709. p = all_data;
  710. while (p) {
  711. p->display = 0;
  712. if (p->start_time == 1)
  713. p->start_time = first_time;
  714. /* no exit marker, task kept running to the end */
  715. if (p->end_time == 0)
  716. p->end_time = last_time;
  717. if (p->total_time >= threshold)
  718. p->display = 1;
  719. c = p->all;
  720. while (c) {
  721. c->display = 0;
  722. if (c->start_time == 1)
  723. c->start_time = first_time;
  724. if (c->total_time >= threshold) {
  725. c->display = 1;
  726. count++;
  727. }
  728. if (c->end_time == 0)
  729. c->end_time = last_time;
  730. c = c->next;
  731. }
  732. p = p->next;
  733. }
  734. return count;
  735. }
  736. #define TIME_THRESH 10000000
  737. static void write_svg_file(const char *filename)
  738. {
  739. u64 i;
  740. int count;
  741. numcpus++;
  742. count = determine_display_tasks(TIME_THRESH);
  743. /* We'd like to show at least 15 tasks; be less picky if we have fewer */
  744. if (count < 15)
  745. count = determine_display_tasks(TIME_THRESH / 10);
  746. open_svg(filename, numcpus, count);
  747. svg_time_grid(first_time, last_time);
  748. svg_legenda();
  749. for (i = 0; i < numcpus; i++)
  750. svg_cpu_box(i, max_freq, turbo_frequency);
  751. draw_cpu_usage();
  752. draw_process_bars();
  753. draw_c_p_states();
  754. draw_wakeups();
  755. svg_close();
  756. }
  757. static int
  758. process_event(event_t *event)
  759. {
  760. switch (event->header.type) {
  761. case PERF_EVENT_COMM:
  762. return process_comm_event(event);
  763. case PERF_EVENT_FORK:
  764. return process_fork_event(event);
  765. case PERF_EVENT_EXIT:
  766. return process_exit_event(event);
  767. case PERF_EVENT_SAMPLE:
  768. return queue_sample_event(event);
  769. /*
  770. * We dont process them right now but they are fine:
  771. */
  772. case PERF_EVENT_MMAP:
  773. case PERF_EVENT_THROTTLE:
  774. case PERF_EVENT_UNTHROTTLE:
  775. return 0;
  776. default:
  777. return -1;
  778. }
  779. return 0;
  780. }
  781. static void process_samples(void)
  782. {
  783. struct sample_wrapper *cursor;
  784. event_t *event;
  785. sort_queued_samples();
  786. cursor = all_samples;
  787. while (cursor) {
  788. event = (void *)&cursor->data;
  789. cursor = cursor->next;
  790. process_sample_event(event);
  791. }
  792. }
  793. static int __cmd_timechart(void)
  794. {
  795. int ret, rc = EXIT_FAILURE;
  796. unsigned long offset = 0;
  797. unsigned long head, shift;
  798. struct stat statbuf;
  799. event_t *event;
  800. uint32_t size;
  801. char *buf;
  802. int input;
  803. input = open(input_name, O_RDONLY);
  804. if (input < 0) {
  805. fprintf(stderr, " failed to open file: %s", input_name);
  806. if (!strcmp(input_name, "perf.data"))
  807. fprintf(stderr, " (try 'perf record' first)");
  808. fprintf(stderr, "\n");
  809. exit(-1);
  810. }
  811. ret = fstat(input, &statbuf);
  812. if (ret < 0) {
  813. perror("failed to stat file");
  814. exit(-1);
  815. }
  816. if (!statbuf.st_size) {
  817. fprintf(stderr, "zero-sized file, nothing to do!\n");
  818. exit(0);
  819. }
  820. header = perf_header__read(input);
  821. head = header->data_offset;
  822. sample_type = perf_header__sample_type(header);
  823. shift = page_size * (head / page_size);
  824. offset += shift;
  825. head -= shift;
  826. remap:
  827. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  828. MAP_SHARED, input, offset);
  829. if (buf == MAP_FAILED) {
  830. perror("failed to mmap file");
  831. exit(-1);
  832. }
  833. more:
  834. event = (event_t *)(buf + head);
  835. size = event->header.size;
  836. if (!size)
  837. size = 8;
  838. if (head + event->header.size >= page_size * mmap_window) {
  839. int ret2;
  840. shift = page_size * (head / page_size);
  841. ret2 = munmap(buf, page_size * mmap_window);
  842. assert(ret2 == 0);
  843. offset += shift;
  844. head -= shift;
  845. goto remap;
  846. }
  847. size = event->header.size;
  848. if (!size || process_event(event) < 0) {
  849. printf("%p [%p]: skipping unknown header type: %d\n",
  850. (void *)(offset + head),
  851. (void *)(long)(event->header.size),
  852. event->header.type);
  853. /*
  854. * assume we lost track of the stream, check alignment, and
  855. * increment a single u64 in the hope to catch on again 'soon'.
  856. */
  857. if (unlikely(head & 7))
  858. head &= ~7ULL;
  859. size = 8;
  860. }
  861. head += size;
  862. if (offset + head >= header->data_offset + header->data_size)
  863. goto done;
  864. if (offset + head < (unsigned long)statbuf.st_size)
  865. goto more;
  866. done:
  867. rc = EXIT_SUCCESS;
  868. close(input);
  869. process_samples();
  870. end_sample_processing();
  871. sort_pids();
  872. write_svg_file(output_name);
  873. printf("Written %2.1f seconds of trace to %s.\n", (last_time - first_time) / 1000000000.0, output_name);
  874. return rc;
  875. }
  876. static const char * const timechart_usage[] = {
  877. "perf timechart [<options>] {record}",
  878. NULL
  879. };
  880. static const char *record_args[] = {
  881. "record",
  882. "-a",
  883. "-R",
  884. "-M",
  885. "-f",
  886. "-c", "1",
  887. "-e", "power:power_start",
  888. "-e", "power:power_end",
  889. "-e", "power:power_frequency",
  890. "-e", "sched:sched_wakeup",
  891. "-e", "sched:sched_switch",
  892. };
  893. static int __cmd_record(int argc, const char **argv)
  894. {
  895. unsigned int rec_argc, i, j;
  896. const char **rec_argv;
  897. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  898. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  899. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  900. rec_argv[i] = strdup(record_args[i]);
  901. for (j = 1; j < (unsigned int)argc; j++, i++)
  902. rec_argv[i] = argv[j];
  903. return cmd_record(i, rec_argv, NULL);
  904. }
  905. static const struct option options[] = {
  906. OPT_STRING('i', "input", &input_name, "file",
  907. "input file name"),
  908. OPT_STRING('o', "output", &output_name, "file",
  909. "output file name"),
  910. OPT_END()
  911. };
  912. int cmd_timechart(int argc, const char **argv, const char *prefix __used)
  913. {
  914. symbol__init();
  915. page_size = getpagesize();
  916. argc = parse_options(argc, argv, options, timechart_usage,
  917. PARSE_OPT_STOP_AT_NON_OPTION);
  918. if (argc && !strncmp(argv[0], "rec", 3))
  919. return __cmd_record(argc, argv);
  920. else if (argc)
  921. usage_with_options(timechart_usage, options);
  922. setup_pager();
  923. return __cmd_timechart();
  924. }