builtin-timechart.c 23 KB

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