builtin-timechart.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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/callchain.h"
  22. #include "util/strlist.h"
  23. #include "perf.h"
  24. #include "util/header.h"
  25. #include "util/parse-options.h"
  26. #include "util/parse-events.h"
  27. #include "util/event.h"
  28. #include "util/session.h"
  29. #include "util/svghelper.h"
  30. static char const *input_name = "perf.data";
  31. static char const *output_name = "output.svg";
  32. static unsigned int numcpus;
  33. static u64 min_freq; /* Lowest CPU frequency seen */
  34. static u64 max_freq; /* Highest CPU frequency seen */
  35. static u64 turbo_frequency;
  36. static u64 first_time, last_time;
  37. static bool power_only;
  38. struct per_pid;
  39. struct per_pidcomm;
  40. struct cpu_sample;
  41. struct power_event;
  42. struct wake_event;
  43. struct sample_wrapper;
  44. /*
  45. * Datastructure layout:
  46. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  47. * Each "pid" entry, has a list of "comm"s.
  48. * this is because we want to track different programs different, while
  49. * exec will reuse the original pid (by design).
  50. * Each comm has a list of samples that will be used to draw
  51. * final graph.
  52. */
  53. struct per_pid {
  54. struct per_pid *next;
  55. int pid;
  56. int ppid;
  57. u64 start_time;
  58. u64 end_time;
  59. u64 total_time;
  60. int display;
  61. struct per_pidcomm *all;
  62. struct per_pidcomm *current;
  63. };
  64. struct per_pidcomm {
  65. struct per_pidcomm *next;
  66. u64 start_time;
  67. u64 end_time;
  68. u64 total_time;
  69. int Y;
  70. int display;
  71. long state;
  72. u64 state_since;
  73. char *comm;
  74. struct cpu_sample *samples;
  75. };
  76. struct sample_wrapper {
  77. struct sample_wrapper *next;
  78. u64 timestamp;
  79. unsigned char data[0];
  80. };
  81. #define TYPE_NONE 0
  82. #define TYPE_RUNNING 1
  83. #define TYPE_WAITING 2
  84. #define TYPE_BLOCKED 3
  85. struct cpu_sample {
  86. struct cpu_sample *next;
  87. u64 start_time;
  88. u64 end_time;
  89. int type;
  90. int cpu;
  91. };
  92. static struct per_pid *all_data;
  93. #define CSTATE 1
  94. #define PSTATE 2
  95. struct power_event {
  96. struct power_event *next;
  97. int type;
  98. int state;
  99. u64 start_time;
  100. u64 end_time;
  101. int cpu;
  102. };
  103. struct wake_event {
  104. struct wake_event *next;
  105. int waker;
  106. int wakee;
  107. u64 time;
  108. };
  109. static struct power_event *power_events;
  110. static struct wake_event *wake_events;
  111. struct process_filter;
  112. struct process_filter {
  113. char *name;
  114. int pid;
  115. struct process_filter *next;
  116. };
  117. static struct process_filter *process_filter;
  118. static struct per_pid *find_create_pid(int pid)
  119. {
  120. struct per_pid *cursor = all_data;
  121. while (cursor) {
  122. if (cursor->pid == pid)
  123. return cursor;
  124. cursor = cursor->next;
  125. }
  126. cursor = malloc(sizeof(struct per_pid));
  127. assert(cursor != NULL);
  128. memset(cursor, 0, sizeof(struct per_pid));
  129. cursor->pid = pid;
  130. cursor->next = all_data;
  131. all_data = cursor;
  132. return cursor;
  133. }
  134. static void pid_set_comm(int pid, char *comm)
  135. {
  136. struct per_pid *p;
  137. struct per_pidcomm *c;
  138. p = find_create_pid(pid);
  139. c = p->all;
  140. while (c) {
  141. if (c->comm && strcmp(c->comm, comm) == 0) {
  142. p->current = c;
  143. return;
  144. }
  145. if (!c->comm) {
  146. c->comm = strdup(comm);
  147. p->current = c;
  148. return;
  149. }
  150. c = c->next;
  151. }
  152. c = malloc(sizeof(struct per_pidcomm));
  153. assert(c != NULL);
  154. memset(c, 0, sizeof(struct per_pidcomm));
  155. c->comm = strdup(comm);
  156. p->current = c;
  157. c->next = p->all;
  158. p->all = c;
  159. }
  160. static void pid_fork(int pid, int ppid, u64 timestamp)
  161. {
  162. struct per_pid *p, *pp;
  163. p = find_create_pid(pid);
  164. pp = find_create_pid(ppid);
  165. p->ppid = ppid;
  166. if (pp->current && pp->current->comm && !p->current)
  167. pid_set_comm(pid, pp->current->comm);
  168. p->start_time = timestamp;
  169. if (p->current) {
  170. p->current->start_time = timestamp;
  171. p->current->state_since = timestamp;
  172. }
  173. }
  174. static void pid_exit(int pid, u64 timestamp)
  175. {
  176. struct per_pid *p;
  177. p = find_create_pid(pid);
  178. p->end_time = timestamp;
  179. if (p->current)
  180. p->current->end_time = timestamp;
  181. }
  182. static void
  183. pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
  184. {
  185. struct per_pid *p;
  186. struct per_pidcomm *c;
  187. struct cpu_sample *sample;
  188. p = find_create_pid(pid);
  189. c = p->current;
  190. if (!c) {
  191. c = malloc(sizeof(struct per_pidcomm));
  192. assert(c != NULL);
  193. memset(c, 0, sizeof(struct per_pidcomm));
  194. p->current = c;
  195. c->next = p->all;
  196. p->all = c;
  197. }
  198. sample = malloc(sizeof(struct cpu_sample));
  199. assert(sample != NULL);
  200. memset(sample, 0, sizeof(struct cpu_sample));
  201. sample->start_time = start;
  202. sample->end_time = end;
  203. sample->type = type;
  204. sample->next = c->samples;
  205. sample->cpu = cpu;
  206. c->samples = sample;
  207. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  208. c->total_time += (end-start);
  209. p->total_time += (end-start);
  210. }
  211. if (c->start_time == 0 || c->start_time > start)
  212. c->start_time = start;
  213. if (p->start_time == 0 || p->start_time > start)
  214. p->start_time = start;
  215. if (cpu > numcpus)
  216. numcpus = cpu;
  217. }
  218. #define MAX_CPUS 4096
  219. static u64 cpus_cstate_start_times[MAX_CPUS];
  220. static int cpus_cstate_state[MAX_CPUS];
  221. static u64 cpus_pstate_start_times[MAX_CPUS];
  222. static u64 cpus_pstate_state[MAX_CPUS];
  223. static int process_comm_event(event_t *event, struct perf_session *session __used)
  224. {
  225. pid_set_comm(event->comm.tid, event->comm.comm);
  226. return 0;
  227. }
  228. static int process_fork_event(event_t *event, struct perf_session *session __used)
  229. {
  230. pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
  231. return 0;
  232. }
  233. static int process_exit_event(event_t *event, struct perf_session *session __used)
  234. {
  235. pid_exit(event->fork.pid, event->fork.time);
  236. return 0;
  237. }
  238. struct trace_entry {
  239. unsigned short type;
  240. unsigned char flags;
  241. unsigned char preempt_count;
  242. int pid;
  243. int lock_depth;
  244. };
  245. struct power_entry {
  246. struct trace_entry te;
  247. u64 type;
  248. u64 value;
  249. u64 cpu_id;
  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 process_sample_event(event_t *event, struct perf_session *session)
  384. {
  385. struct sample_data data;
  386. struct trace_entry *te;
  387. memset(&data, 0, sizeof(data));
  388. event__parse_sample(event, session->sample_type, &data);
  389. if (session->sample_type & PERF_SAMPLE_TIME) {
  390. if (!first_time || first_time > data.time)
  391. first_time = data.time;
  392. if (last_time < data.time)
  393. last_time = data.time;
  394. }
  395. te = (void *)data.raw_data;
  396. if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
  397. char *event_str;
  398. struct power_entry *pe;
  399. pe = (void *)te;
  400. event_str = perf_header__find_event(te->type);
  401. if (!event_str)
  402. return 0;
  403. if (strcmp(event_str, "power:power_start") == 0)
  404. c_state_start(pe->cpu_id, data.time, pe->value);
  405. if (strcmp(event_str, "power:power_end") == 0)
  406. c_state_end(pe->cpu_id, data.time);
  407. if (strcmp(event_str, "power:power_frequency") == 0)
  408. p_state_change(pe->cpu_id, data.time, pe->value);
  409. if (strcmp(event_str, "sched:sched_wakeup") == 0)
  410. sched_wakeup(data.cpu, data.time, data.pid, te);
  411. if (strcmp(event_str, "sched:sched_switch") == 0)
  412. sched_switch(data.cpu, data.time, te);
  413. }
  414. return 0;
  415. }
  416. /*
  417. * After the last sample we need to wrap up the current C/P state
  418. * and close out each CPU for these.
  419. */
  420. static void end_sample_processing(void)
  421. {
  422. u64 cpu;
  423. struct power_event *pwr;
  424. for (cpu = 0; cpu <= numcpus; cpu++) {
  425. pwr = malloc(sizeof(struct power_event));
  426. if (!pwr)
  427. return;
  428. memset(pwr, 0, sizeof(struct power_event));
  429. /* C state */
  430. #if 0
  431. pwr->state = cpus_cstate_state[cpu];
  432. pwr->start_time = cpus_cstate_start_times[cpu];
  433. pwr->end_time = last_time;
  434. pwr->cpu = cpu;
  435. pwr->type = CSTATE;
  436. pwr->next = power_events;
  437. power_events = pwr;
  438. #endif
  439. /* P state */
  440. pwr = malloc(sizeof(struct power_event));
  441. if (!pwr)
  442. return;
  443. memset(pwr, 0, sizeof(struct power_event));
  444. pwr->state = cpus_pstate_state[cpu];
  445. pwr->start_time = cpus_pstate_start_times[cpu];
  446. pwr->end_time = last_time;
  447. pwr->cpu = cpu;
  448. pwr->type = PSTATE;
  449. pwr->next = power_events;
  450. if (!pwr->start_time)
  451. pwr->start_time = first_time;
  452. if (!pwr->state)
  453. pwr->state = min_freq;
  454. power_events = pwr;
  455. }
  456. }
  457. /*
  458. * Sort the pid datastructure
  459. */
  460. static void sort_pids(void)
  461. {
  462. struct per_pid *new_list, *p, *cursor, *prev;
  463. /* sort by ppid first, then by pid, lowest to highest */
  464. new_list = NULL;
  465. while (all_data) {
  466. p = all_data;
  467. all_data = p->next;
  468. p->next = NULL;
  469. if (new_list == NULL) {
  470. new_list = p;
  471. p->next = NULL;
  472. continue;
  473. }
  474. prev = NULL;
  475. cursor = new_list;
  476. while (cursor) {
  477. if (cursor->ppid > p->ppid ||
  478. (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
  479. /* must insert before */
  480. if (prev) {
  481. p->next = prev->next;
  482. prev->next = p;
  483. cursor = NULL;
  484. continue;
  485. } else {
  486. p->next = new_list;
  487. new_list = p;
  488. cursor = NULL;
  489. continue;
  490. }
  491. }
  492. prev = cursor;
  493. cursor = cursor->next;
  494. if (!cursor)
  495. prev->next = p;
  496. }
  497. }
  498. all_data = new_list;
  499. }
  500. static void draw_c_p_states(void)
  501. {
  502. struct power_event *pwr;
  503. pwr = power_events;
  504. /*
  505. * two pass drawing so that the P state bars are on top of the C state blocks
  506. */
  507. while (pwr) {
  508. if (pwr->type == CSTATE)
  509. svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  510. pwr = pwr->next;
  511. }
  512. pwr = power_events;
  513. while (pwr) {
  514. if (pwr->type == PSTATE) {
  515. if (!pwr->state)
  516. pwr->state = min_freq;
  517. svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
  518. }
  519. pwr = pwr->next;
  520. }
  521. }
  522. static void draw_wakeups(void)
  523. {
  524. struct wake_event *we;
  525. struct per_pid *p;
  526. struct per_pidcomm *c;
  527. we = wake_events;
  528. while (we) {
  529. int from = 0, to = 0;
  530. char *task_from = NULL, *task_to = NULL;
  531. /* locate the column of the waker and wakee */
  532. p = all_data;
  533. while (p) {
  534. if (p->pid == we->waker || p->pid == we->wakee) {
  535. c = p->all;
  536. while (c) {
  537. if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
  538. if (p->pid == we->waker && !from) {
  539. from = c->Y;
  540. task_from = strdup(c->comm);
  541. }
  542. if (p->pid == we->wakee && !to) {
  543. to = c->Y;
  544. task_to = strdup(c->comm);
  545. }
  546. }
  547. c = c->next;
  548. }
  549. c = p->all;
  550. while (c) {
  551. if (p->pid == we->waker && !from) {
  552. from = c->Y;
  553. task_from = strdup(c->comm);
  554. }
  555. if (p->pid == we->wakee && !to) {
  556. to = c->Y;
  557. task_to = strdup(c->comm);
  558. }
  559. c = c->next;
  560. }
  561. }
  562. p = p->next;
  563. }
  564. if (!task_from) {
  565. task_from = malloc(40);
  566. sprintf(task_from, "[%i]", we->waker);
  567. }
  568. if (!task_to) {
  569. task_to = malloc(40);
  570. sprintf(task_to, "[%i]", we->wakee);
  571. }
  572. if (we->waker == -1)
  573. svg_interrupt(we->time, to);
  574. else if (from && to && abs(from - to) == 1)
  575. svg_wakeline(we->time, from, to);
  576. else
  577. svg_partial_wakeline(we->time, from, task_from, to, task_to);
  578. we = we->next;
  579. free(task_from);
  580. free(task_to);
  581. }
  582. }
  583. static void draw_cpu_usage(void)
  584. {
  585. struct per_pid *p;
  586. struct per_pidcomm *c;
  587. struct cpu_sample *sample;
  588. p = all_data;
  589. while (p) {
  590. c = p->all;
  591. while (c) {
  592. sample = c->samples;
  593. while (sample) {
  594. if (sample->type == TYPE_RUNNING)
  595. svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
  596. sample = sample->next;
  597. }
  598. c = c->next;
  599. }
  600. p = p->next;
  601. }
  602. }
  603. static void draw_process_bars(void)
  604. {
  605. struct per_pid *p;
  606. struct per_pidcomm *c;
  607. struct cpu_sample *sample;
  608. int Y = 0;
  609. Y = 2 * numcpus + 2;
  610. p = all_data;
  611. while (p) {
  612. c = p->all;
  613. while (c) {
  614. if (!c->display) {
  615. c->Y = 0;
  616. c = c->next;
  617. continue;
  618. }
  619. svg_box(Y, c->start_time, c->end_time, "process");
  620. sample = c->samples;
  621. while (sample) {
  622. if (sample->type == TYPE_RUNNING)
  623. svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
  624. if (sample->type == TYPE_BLOCKED)
  625. svg_box(Y, sample->start_time, sample->end_time, "blocked");
  626. if (sample->type == TYPE_WAITING)
  627. svg_waiting(Y, sample->start_time, sample->end_time);
  628. sample = sample->next;
  629. }
  630. if (c->comm) {
  631. char comm[256];
  632. if (c->total_time > 5000000000) /* 5 seconds */
  633. sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
  634. else
  635. sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
  636. svg_text(Y, c->start_time, comm);
  637. }
  638. c->Y = Y;
  639. Y++;
  640. c = c->next;
  641. }
  642. p = p->next;
  643. }
  644. }
  645. static void add_process_filter(const char *string)
  646. {
  647. struct process_filter *filt;
  648. int pid;
  649. pid = strtoull(string, NULL, 10);
  650. filt = malloc(sizeof(struct process_filter));
  651. if (!filt)
  652. return;
  653. filt->name = strdup(string);
  654. filt->pid = pid;
  655. filt->next = process_filter;
  656. process_filter = filt;
  657. }
  658. static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
  659. {
  660. struct process_filter *filt;
  661. if (!process_filter)
  662. return 1;
  663. filt = process_filter;
  664. while (filt) {
  665. if (filt->pid && p->pid == filt->pid)
  666. return 1;
  667. if (strcmp(filt->name, c->comm) == 0)
  668. return 1;
  669. filt = filt->next;
  670. }
  671. return 0;
  672. }
  673. static int determine_display_tasks_filtered(void)
  674. {
  675. struct per_pid *p;
  676. struct per_pidcomm *c;
  677. int count = 0;
  678. p = all_data;
  679. while (p) {
  680. p->display = 0;
  681. if (p->start_time == 1)
  682. p->start_time = first_time;
  683. /* no exit marker, task kept running to the end */
  684. if (p->end_time == 0)
  685. p->end_time = last_time;
  686. c = p->all;
  687. while (c) {
  688. c->display = 0;
  689. if (c->start_time == 1)
  690. c->start_time = first_time;
  691. if (passes_filter(p, c)) {
  692. c->display = 1;
  693. p->display = 1;
  694. count++;
  695. }
  696. if (c->end_time == 0)
  697. c->end_time = last_time;
  698. c = c->next;
  699. }
  700. p = p->next;
  701. }
  702. return count;
  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. if (process_filter)
  710. return determine_display_tasks_filtered();
  711. p = all_data;
  712. while (p) {
  713. p->display = 0;
  714. if (p->start_time == 1)
  715. p->start_time = first_time;
  716. /* no exit marker, task kept running to the end */
  717. if (p->end_time == 0)
  718. p->end_time = last_time;
  719. if (p->total_time >= threshold && !power_only)
  720. p->display = 1;
  721. c = p->all;
  722. while (c) {
  723. c->display = 0;
  724. if (c->start_time == 1)
  725. c->start_time = first_time;
  726. if (c->total_time >= threshold && !power_only) {
  727. c->display = 1;
  728. count++;
  729. }
  730. if (c->end_time == 0)
  731. c->end_time = last_time;
  732. c = c->next;
  733. }
  734. p = p->next;
  735. }
  736. return count;
  737. }
  738. #define TIME_THRESH 10000000
  739. static void write_svg_file(const char *filename)
  740. {
  741. u64 i;
  742. int count;
  743. numcpus++;
  744. count = determine_display_tasks(TIME_THRESH);
  745. /* We'd like to show at least 15 tasks; be less picky if we have fewer */
  746. if (count < 15)
  747. count = determine_display_tasks(TIME_THRESH / 10);
  748. open_svg(filename, numcpus, count, first_time, last_time);
  749. svg_time_grid();
  750. svg_legenda();
  751. for (i = 0; i < numcpus; i++)
  752. svg_cpu_box(i, max_freq, turbo_frequency);
  753. draw_cpu_usage();
  754. draw_process_bars();
  755. draw_c_p_states();
  756. draw_wakeups();
  757. svg_close();
  758. }
  759. static struct perf_event_ops event_ops = {
  760. .comm = process_comm_event,
  761. .fork = process_fork_event,
  762. .exit = process_exit_event,
  763. .sample = process_sample_event,
  764. .ordered_samples = true,
  765. };
  766. static int __cmd_timechart(void)
  767. {
  768. struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0, false);
  769. int ret = -EINVAL;
  770. if (session == NULL)
  771. return -ENOMEM;
  772. if (!perf_session__has_traces(session, "timechart record"))
  773. goto out_delete;
  774. ret = perf_session__process_events(session, &event_ops);
  775. if (ret)
  776. goto out_delete;
  777. end_sample_processing();
  778. sort_pids();
  779. write_svg_file(output_name);
  780. pr_info("Written %2.1f seconds of trace to %s.\n",
  781. (last_time - first_time) / 1000000000.0, output_name);
  782. out_delete:
  783. perf_session__delete(session);
  784. return ret;
  785. }
  786. static const char * const timechart_usage[] = {
  787. "perf timechart [<options>] {record}",
  788. NULL
  789. };
  790. static const char *record_args[] = {
  791. "record",
  792. "-a",
  793. "-R",
  794. "-f",
  795. "-c", "1",
  796. "-e", "power:power_start",
  797. "-e", "power:power_end",
  798. "-e", "power:power_frequency",
  799. "-e", "sched:sched_wakeup",
  800. "-e", "sched:sched_switch",
  801. };
  802. static int __cmd_record(int argc, const char **argv)
  803. {
  804. unsigned int rec_argc, i, j;
  805. const char **rec_argv;
  806. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  807. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  808. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  809. rec_argv[i] = strdup(record_args[i]);
  810. for (j = 1; j < (unsigned int)argc; j++, i++)
  811. rec_argv[i] = argv[j];
  812. return cmd_record(i, rec_argv, NULL);
  813. }
  814. static int
  815. parse_process(const struct option *opt __used, const char *arg, int __used unset)
  816. {
  817. if (arg)
  818. add_process_filter(arg);
  819. return 0;
  820. }
  821. static const struct option options[] = {
  822. OPT_STRING('i', "input", &input_name, "file",
  823. "input file name"),
  824. OPT_STRING('o', "output", &output_name, "file",
  825. "output file name"),
  826. OPT_INTEGER('w', "width", &svg_page_width,
  827. "page width"),
  828. OPT_BOOLEAN('P', "power-only", &power_only,
  829. "output power data only"),
  830. OPT_CALLBACK('p', "process", NULL, "process",
  831. "process selector. Pass a pid or process name.",
  832. parse_process),
  833. OPT_END()
  834. };
  835. int cmd_timechart(int argc, const char **argv, const char *prefix __used)
  836. {
  837. argc = parse_options(argc, argv, options, timechart_usage,
  838. PARSE_OPT_STOP_AT_NON_OPTION);
  839. symbol__init();
  840. if (argc && !strncmp(argv[0], "rec", 3))
  841. return __cmd_record(argc, argv);
  842. else if (argc)
  843. usage_with_options(timechart_usage, options);
  844. setup_pager();
  845. return __cmd_timechart();
  846. }