builtin-timechart.c 25 KB

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