builtin-timechart.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. int cursor = 0;
  393. u64 addr = 0;
  394. u64 stamp = 0;
  395. u32 cpu = 0;
  396. u32 pid = 0;
  397. u32 size, *size_ptr;
  398. struct trace_entry *te;
  399. if (sample_type & PERF_SAMPLE_IP)
  400. cursor++;
  401. if (sample_type & PERF_SAMPLE_TID) {
  402. pid = event->sample.array[cursor]>>32;
  403. cursor++;
  404. }
  405. if (sample_type & PERF_SAMPLE_TIME) {
  406. stamp = event->sample.array[cursor++];
  407. if (!first_time || first_time > stamp)
  408. first_time = stamp;
  409. if (last_time < stamp)
  410. last_time = stamp;
  411. }
  412. if (sample_type & PERF_SAMPLE_ADDR)
  413. addr = event->sample.array[cursor++];
  414. if (sample_type & PERF_SAMPLE_ID)
  415. cursor++;
  416. if (sample_type & PERF_SAMPLE_STREAM_ID)
  417. cursor++;
  418. if (sample_type & PERF_SAMPLE_CPU)
  419. cpu = event->sample.array[cursor++] & 0xFFFFFFFF;
  420. if (sample_type & PERF_SAMPLE_PERIOD)
  421. cursor++;
  422. size_ptr = (void *)&event->sample.array[cursor];
  423. size = *size_ptr;
  424. size_ptr++;
  425. te = (void *)size_ptr;
  426. if (sample_type & PERF_SAMPLE_RAW && size > 0) {
  427. char *event_str;
  428. struct power_entry *pe;
  429. pe = (void *)te;
  430. event_str = perf_header__find_event(te->type);
  431. if (!event_str)
  432. return 0;
  433. if (strcmp(event_str, "power:power_start") == 0)
  434. c_state_start(cpu, stamp, pe->value);
  435. if (strcmp(event_str, "power:power_end") == 0)
  436. c_state_end(cpu, stamp);
  437. if (strcmp(event_str, "power:power_frequency") == 0)
  438. p_state_change(cpu, stamp, pe->value);
  439. if (strcmp(event_str, "sched:sched_wakeup") == 0)
  440. sched_wakeup(cpu, stamp, pid, te);
  441. if (strcmp(event_str, "sched:sched_switch") == 0)
  442. sched_switch(cpu, stamp, te);
  443. }
  444. return 0;
  445. }
  446. /*
  447. * After the last sample we need to wrap up the current C/P state
  448. * and close out each CPU for these.
  449. */
  450. static void end_sample_processing(void)
  451. {
  452. u64 cpu;
  453. struct power_event *pwr;
  454. for (cpu = 0; cpu <= numcpus; cpu++) {
  455. pwr = malloc(sizeof(struct power_event));
  456. if (!pwr)
  457. return;
  458. memset(pwr, 0, sizeof(struct power_event));
  459. /* C state */
  460. #if 0
  461. pwr->state = cpus_cstate_state[cpu];
  462. pwr->start_time = cpus_cstate_start_times[cpu];
  463. pwr->end_time = last_time;
  464. pwr->cpu = cpu;
  465. pwr->type = CSTATE;
  466. pwr->next = power_events;
  467. power_events = pwr;
  468. #endif
  469. /* P state */
  470. pwr = malloc(sizeof(struct power_event));
  471. if (!pwr)
  472. return;
  473. memset(pwr, 0, sizeof(struct power_event));
  474. pwr->state = cpus_pstate_state[cpu];
  475. pwr->start_time = cpus_pstate_start_times[cpu];
  476. pwr->end_time = last_time;
  477. pwr->cpu = cpu;
  478. pwr->type = PSTATE;
  479. pwr->next = power_events;
  480. if (!pwr->start_time)
  481. pwr->start_time = first_time;
  482. if (!pwr->state)
  483. pwr->state = min_freq;
  484. power_events = pwr;
  485. }
  486. }
  487. static u64 sample_time(event_t *event)
  488. {
  489. int cursor;
  490. cursor = 0;
  491. if (sample_type & PERF_SAMPLE_IP)
  492. cursor++;
  493. if (sample_type & PERF_SAMPLE_TID)
  494. cursor++;
  495. if (sample_type & PERF_SAMPLE_TIME)
  496. return event->sample.array[cursor];
  497. return 0;
  498. }
  499. /*
  500. * We first queue all events, sorted backwards by insertion.
  501. * The order will get flipped later.
  502. */
  503. static int
  504. queue_sample_event(event_t *event)
  505. {
  506. struct sample_wrapper *copy, *prev;
  507. int size;
  508. size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
  509. copy = malloc(size);
  510. if (!copy)
  511. return 1;
  512. memset(copy, 0, size);
  513. copy->next = NULL;
  514. copy->timestamp = sample_time(event);
  515. memcpy(&copy->data, event, event->sample.header.size);
  516. /* insert in the right place in the list */
  517. if (!all_samples) {
  518. /* first sample ever */
  519. all_samples = copy;
  520. return 0;
  521. }
  522. if (all_samples->timestamp < copy->timestamp) {
  523. /* insert at the head of the list */
  524. copy->next = all_samples;
  525. all_samples = copy;
  526. return 0;
  527. }
  528. prev = all_samples;
  529. while (prev->next) {
  530. if (prev->next->timestamp < copy->timestamp) {
  531. copy->next = prev->next;
  532. prev->next = copy;
  533. return 0;
  534. }
  535. prev = prev->next;
  536. }
  537. /* insert at the end of the list */
  538. prev->next = copy;
  539. return 0;
  540. }
  541. static void sort_queued_samples(void)
  542. {
  543. struct sample_wrapper *cursor, *next;
  544. cursor = all_samples;
  545. all_samples = NULL;
  546. while (cursor) {
  547. next = cursor->next;
  548. cursor->next = all_samples;
  549. all_samples = cursor;
  550. cursor = next;
  551. }
  552. }
  553. /*
  554. * Sort the pid datastructure
  555. */
  556. static void sort_pids(void)
  557. {
  558. struct per_pid *new_list, *p, *cursor, *prev;
  559. /* sort by ppid first, then by pid, lowest to highest */
  560. new_list = NULL;
  561. while (all_data) {
  562. p = all_data;
  563. all_data = p->next;
  564. p->next = NULL;
  565. if (new_list == NULL) {
  566. new_list = p;
  567. p->next = NULL;
  568. continue;
  569. }
  570. prev = NULL;
  571. cursor = new_list;
  572. while (cursor) {
  573. if (cursor->ppid > p->ppid ||
  574. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  575. /* must insert before */
  576. if (prev) {
  577. p->next = prev->next;
  578. prev->next = p;
  579. cursor = NULL;
  580. continue;
  581. } else {
  582. p->next = new_list;
  583. new_list = p;
  584. cursor = NULL;
  585. continue;
  586. }
  587. }
  588. prev = cursor;
  589. cursor = cursor->next;
  590. if (!cursor)
  591. prev->next = p;
  592. }
  593. }
  594. all_data = new_list;
  595. }
  596. static void draw_c_p_states(void)
  597. {
  598. struct power_event *pwr;
  599. pwr = power_events;
  600. /*
  601. * two pass drawing so that the P state bars are on top of the C state blocks
  602. */
  603. while (pwr) {
  604. if (pwr->type == CSTATE)
  605. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  606. pwr = pwr->next;
  607. }
  608. pwr = power_events;
  609. while (pwr) {
  610. if (pwr->type == PSTATE) {
  611. if (!pwr->state)
  612. pwr->state = min_freq;
  613. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  614. }
  615. pwr = pwr->next;
  616. }
  617. }
  618. static void draw_wakeups(void)
  619. {
  620. struct wake_event *we;
  621. struct per_pid *p;
  622. struct per_pidcomm *c;
  623. we = wake_events;
  624. while (we) {
  625. int from = 0, to = 0;
  626. char *task_from = NULL, *task_to = NULL;
  627. /* locate the column of the waker and wakee */
  628. p = all_data;
  629. while (p) {
  630. if (p->pid == we->waker || p->pid == we->wakee) {
  631. c = p->all;
  632. while (c) {
  633. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  634. if (p->pid == we->waker && !from) {
  635. from = c->Y;
  636. task_from = strdup(c->comm);
  637. }
  638. if (p->pid == we->wakee && !to) {
  639. to = c->Y;
  640. task_to = strdup(c->comm);
  641. }
  642. }
  643. c = c->next;
  644. }
  645. c = p->all;
  646. while (c) {
  647. if (p->pid == we->waker && !from) {
  648. from = c->Y;
  649. task_from = strdup(c->comm);
  650. }
  651. if (p->pid == we->wakee && !to) {
  652. to = c->Y;
  653. task_to = strdup(c->comm);
  654. }
  655. c = c->next;
  656. }
  657. }
  658. p = p->next;
  659. }
  660. if (!task_from) {
  661. task_from = malloc(40);
  662. sprintf(task_from, "[%i]", we->waker);
  663. }
  664. if (!task_to) {
  665. task_to = malloc(40);
  666. sprintf(task_to, "[%i]", we->wakee);
  667. }
  668. if (we->waker == -1)
  669. svg_interrupt(we->time, to);
  670. else if (from && to && abs(from - to) == 1)
  671. svg_wakeline(we->time, from, to);
  672. else
  673. svg_partial_wakeline(we->time, from, task_from, to, task_to);
  674. we = we->next;
  675. free(task_from);
  676. free(task_to);
  677. }
  678. }
  679. static void draw_cpu_usage(void)
  680. {
  681. struct per_pid *p;
  682. struct per_pidcomm *c;
  683. struct cpu_sample *sample;
  684. p = all_data;
  685. while (p) {
  686. c = p->all;
  687. while (c) {
  688. sample = c->samples;
  689. while (sample) {
  690. if (sample->type == TYPE_RUNNING)
  691. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  692. sample = sample->next;
  693. }
  694. c = c->next;
  695. }
  696. p = p->next;
  697. }
  698. }
  699. static void draw_process_bars(void)
  700. {
  701. struct per_pid *p;
  702. struct per_pidcomm *c;
  703. struct cpu_sample *sample;
  704. int Y = 0;
  705. Y = 2 * numcpus + 2;
  706. p = all_data;
  707. while (p) {
  708. c = p->all;
  709. while (c) {
  710. if (!c->display) {
  711. c->Y = 0;
  712. c = c->next;
  713. continue;
  714. }
  715. svg_box(Y, c->start_time, c->end_time, "process");
  716. sample = c->samples;
  717. while (sample) {
  718. if (sample->type == TYPE_RUNNING)
  719. svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
  720. if (sample->type == TYPE_BLOCKED)
  721. svg_box(Y, sample->start_time, sample->end_time, "blocked");
  722. if (sample->type == TYPE_WAITING)
  723. svg_waiting(Y, sample->start_time, sample->end_time);
  724. sample = sample->next;
  725. }
  726. if (c->comm) {
  727. char comm[256];
  728. if (c->total_time > 5000000000) /* 5 seconds */
  729. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  730. else
  731. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  732. svg_text(Y, c->start_time, comm);
  733. }
  734. c->Y = Y;
  735. Y++;
  736. c = c->next;
  737. }
  738. p = p->next;
  739. }
  740. }
  741. static void add_process_filter(const char *string)
  742. {
  743. struct process_filter *filt;
  744. int pid;
  745. pid = strtoull(string, NULL, 10);
  746. filt = malloc(sizeof(struct process_filter));
  747. if (!filt)
  748. return;
  749. filt->name = strdup(string);
  750. filt->pid = pid;
  751. filt->next = process_filter;
  752. process_filter = filt;
  753. }
  754. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  755. {
  756. struct process_filter *filt;
  757. if (!process_filter)
  758. return 1;
  759. filt = process_filter;
  760. while (filt) {
  761. if (filt->pid && p->pid == filt->pid)
  762. return 1;
  763. if (strcmp(filt->name, c->comm) == 0)
  764. return 1;
  765. filt = filt->next;
  766. }
  767. return 0;
  768. }
  769. static int determine_display_tasks_filtered(void)
  770. {
  771. struct per_pid *p;
  772. struct per_pidcomm *c;
  773. int count = 0;
  774. p = all_data;
  775. while (p) {
  776. p->display = 0;
  777. if (p->start_time == 1)
  778. p->start_time = first_time;
  779. /* no exit marker, task kept running to the end */
  780. if (p->end_time == 0)
  781. p->end_time = last_time;
  782. c = p->all;
  783. while (c) {
  784. c->display = 0;
  785. if (c->start_time == 1)
  786. c->start_time = first_time;
  787. if (passes_filter(p, c)) {
  788. c->display = 1;
  789. p->display = 1;
  790. count++;
  791. }
  792. if (c->end_time == 0)
  793. c->end_time = last_time;
  794. c = c->next;
  795. }
  796. p = p->next;
  797. }
  798. return count;
  799. }
  800. static int determine_display_tasks(u64 threshold)
  801. {
  802. struct per_pid *p;
  803. struct per_pidcomm *c;
  804. int count = 0;
  805. if (process_filter)
  806. return determine_display_tasks_filtered();
  807. p = all_data;
  808. while (p) {
  809. p->display = 0;
  810. if (p->start_time == 1)
  811. p->start_time = first_time;
  812. /* no exit marker, task kept running to the end */
  813. if (p->end_time == 0)
  814. p->end_time = last_time;
  815. if (p->total_time >= threshold && !power_only)
  816. p->display = 1;
  817. c = p->all;
  818. while (c) {
  819. c->display = 0;
  820. if (c->start_time == 1)
  821. c->start_time = first_time;
  822. if (c->total_time >= threshold && !power_only) {
  823. c->display = 1;
  824. count++;
  825. }
  826. if (c->end_time == 0)
  827. c->end_time = last_time;
  828. c = c->next;
  829. }
  830. p = p->next;
  831. }
  832. return count;
  833. }
  834. #define TIME_THRESH 10000000
  835. static void write_svg_file(const char *filename)
  836. {
  837. u64 i;
  838. int count;
  839. numcpus++;
  840. count = determine_display_tasks(TIME_THRESH);
  841. /* We'd like to show at least 15 tasks; be less picky if we have fewer */
  842. if (count < 15)
  843. count = determine_display_tasks(TIME_THRESH / 10);
  844. open_svg(filename, numcpus, count, first_time, last_time);
  845. svg_time_grid();
  846. svg_legenda();
  847. for (i = 0; i < numcpus; i++)
  848. svg_cpu_box(i, max_freq, turbo_frequency);
  849. draw_cpu_usage();
  850. draw_process_bars();
  851. draw_c_p_states();
  852. draw_wakeups();
  853. svg_close();
  854. }
  855. static void process_samples(void)
  856. {
  857. struct sample_wrapper *cursor;
  858. event_t *event;
  859. sort_queued_samples();
  860. cursor = all_samples;
  861. while (cursor) {
  862. event = (void *)&cursor->data;
  863. cursor = cursor->next;
  864. process_sample_event(event);
  865. }
  866. }
  867. static int sample_type_check(u64 type)
  868. {
  869. sample_type = type;
  870. if (!(sample_type & PERF_SAMPLE_RAW)) {
  871. fprintf(stderr, "No trace samples found in the file.\n"
  872. "Have you used 'perf timechart record' to record it?\n");
  873. return -1;
  874. }
  875. return 0;
  876. }
  877. static struct perf_file_handler file_handler = {
  878. .process_comm_event = process_comm_event,
  879. .process_fork_event = process_fork_event,
  880. .process_exit_event = process_exit_event,
  881. .process_sample_event = queue_sample_event,
  882. .sample_type_check = sample_type_check,
  883. };
  884. static int __cmd_timechart(void)
  885. {
  886. struct perf_header *header;
  887. int ret;
  888. register_perf_file_handler(&file_handler);
  889. ret = mmap_dispatch_perf_file(&header, input_name, 0, 0,
  890. &event__cwdlen, &event__cwd);
  891. if (ret)
  892. return EXIT_FAILURE;
  893. process_samples();
  894. end_sample_processing();
  895. sort_pids();
  896. write_svg_file(output_name);
  897. pr_info("Written %2.1f seconds of trace to %s.\n",
  898. (last_time - first_time) / 1000000000.0, output_name);
  899. return EXIT_SUCCESS;
  900. }
  901. static const char * const timechart_usage[] = {
  902. "perf timechart [<options>] {record}",
  903. NULL
  904. };
  905. static const char *record_args[] = {
  906. "record",
  907. "-a",
  908. "-R",
  909. "-M",
  910. "-f",
  911. "-c", "1",
  912. "-e", "power:power_start",
  913. "-e", "power:power_end",
  914. "-e", "power:power_frequency",
  915. "-e", "sched:sched_wakeup",
  916. "-e", "sched:sched_switch",
  917. };
  918. static int __cmd_record(int argc, const char **argv)
  919. {
  920. unsigned int rec_argc, i, j;
  921. const char **rec_argv;
  922. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  923. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  924. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  925. rec_argv[i] = strdup(record_args[i]);
  926. for (j = 1; j < (unsigned int)argc; j++, i++)
  927. rec_argv[i] = argv[j];
  928. return cmd_record(i, rec_argv, NULL);
  929. }
  930. static int
  931. parse_process(const struct option *opt __used, const char *arg, int __used unset)
  932. {
  933. if (arg)
  934. add_process_filter(arg);
  935. return 0;
  936. }
  937. static const struct option options[] = {
  938. OPT_STRING('i', "input", &input_name, "file",
  939. "input file name"),
  940. OPT_STRING('o', "output", &output_name, "file",
  941. "output file name"),
  942. OPT_INTEGER('w', "width", &svg_page_width,
  943. "page width"),
  944. OPT_BOOLEAN('P', "power-only", &power_only,
  945. "output power data only"),
  946. OPT_CALLBACK('p', "process", NULL, "process",
  947. "process selector. Pass a pid or process name.",
  948. parse_process),
  949. OPT_END()
  950. };
  951. int cmd_timechart(int argc, const char **argv, const char *prefix __used)
  952. {
  953. symbol__init(0);
  954. argc = parse_options(argc, argv, options, timechart_usage,
  955. PARSE_OPT_STOP_AT_NON_OPTION);
  956. if (argc && !strncmp(argv[0], "rec", 3))
  957. return __cmd_record(argc, argv);
  958. else if (argc)
  959. usage_with_options(timechart_usage, options);
  960. setup_pager();
  961. return __cmd_timechart();
  962. }