builtin-timechart.c 23 KB

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