builtin-timechart.c 23 KB

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