builtin-timechart.c 23 KB

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