builtin-timechart.c 23 KB

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