builtin-timechart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 err, 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. err = fstat(input, &statbuf);
  908. if (err < 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__new();
  917. if (header == NULL)
  918. return -ENOMEM;
  919. err = perf_header__read(header, input);
  920. if (err < 0) {
  921. perf_header__delete(header);
  922. return err;
  923. }
  924. head = header->data_offset;
  925. sample_type = perf_header__sample_type(header);
  926. shift = page_size * (head / page_size);
  927. offset += shift;
  928. head -= shift;
  929. remap:
  930. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  931. MAP_SHARED, input, offset);
  932. if (buf == MAP_FAILED) {
  933. perror("failed to mmap file");
  934. exit(-1);
  935. }
  936. more:
  937. event = (event_t *)(buf + head);
  938. size = event->header.size;
  939. if (!size)
  940. size = 8;
  941. if (head + event->header.size >= page_size * mmap_window) {
  942. int ret2;
  943. shift = page_size * (head / page_size);
  944. ret2 = munmap(buf, page_size * mmap_window);
  945. assert(ret2 == 0);
  946. offset += shift;
  947. head -= shift;
  948. goto remap;
  949. }
  950. size = event->header.size;
  951. if (!size || process_event(event) < 0) {
  952. pr_warning("%p [%p]: skipping unknown header type: %d\n",
  953. (void *)(offset + head),
  954. (void *)(long)(event->header.size),
  955. event->header.type);
  956. /*
  957. * assume we lost track of the stream, check alignment, and
  958. * increment a single u64 in the hope to catch on again 'soon'.
  959. */
  960. if (unlikely(head & 7))
  961. head &= ~7ULL;
  962. size = 8;
  963. }
  964. head += size;
  965. if (offset + head >= header->data_offset + header->data_size)
  966. goto done;
  967. if (offset + head < (unsigned long)statbuf.st_size)
  968. goto more;
  969. done:
  970. rc = EXIT_SUCCESS;
  971. close(input);
  972. process_samples();
  973. end_sample_processing();
  974. sort_pids();
  975. write_svg_file(output_name);
  976. pr_info("Written %2.1f seconds of trace to %s.\n",
  977. (last_time - first_time) / 1000000000.0, output_name);
  978. return rc;
  979. }
  980. static const char * const timechart_usage[] = {
  981. "perf timechart [<options>] {record}",
  982. NULL
  983. };
  984. static const char *record_args[] = {
  985. "record",
  986. "-a",
  987. "-R",
  988. "-M",
  989. "-f",
  990. "-c", "1",
  991. "-e", "power:power_start",
  992. "-e", "power:power_end",
  993. "-e", "power:power_frequency",
  994. "-e", "sched:sched_wakeup",
  995. "-e", "sched:sched_switch",
  996. };
  997. static int __cmd_record(int argc, const char **argv)
  998. {
  999. unsigned int rec_argc, i, j;
  1000. const char **rec_argv;
  1001. rec_argc = ARRAY_SIZE(record_args) + argc - 1;
  1002. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1003. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1004. rec_argv[i] = strdup(record_args[i]);
  1005. for (j = 1; j < (unsigned int)argc; j++, i++)
  1006. rec_argv[i] = argv[j];
  1007. return cmd_record(i, rec_argv, NULL);
  1008. }
  1009. static int
  1010. parse_process(const struct option *opt __used, const char *arg, int __used unset)
  1011. {
  1012. if (arg)
  1013. add_process_filter(arg);
  1014. return 0;
  1015. }
  1016. static const struct option options[] = {
  1017. OPT_STRING('i', "input", &input_name, "file",
  1018. "input file name"),
  1019. OPT_STRING('o', "output", &output_name, "file",
  1020. "output file name"),
  1021. OPT_INTEGER('w', "width", &svg_page_width,
  1022. "page width"),
  1023. OPT_BOOLEAN('P', "power-only", &power_only,
  1024. "output power data only"),
  1025. OPT_CALLBACK('p', "process", NULL, "process",
  1026. "process selector. Pass a pid or process name.",
  1027. parse_process),
  1028. OPT_END()
  1029. };
  1030. int cmd_timechart(int argc, const char **argv, const char *prefix __used)
  1031. {
  1032. symbol__init(0);
  1033. page_size = getpagesize();
  1034. argc = parse_options(argc, argv, options, timechart_usage,
  1035. PARSE_OPT_STOP_AT_NON_OPTION);
  1036. if (argc && !strncmp(argv[0], "rec", 3))
  1037. return __cmd_record(argc, argv);
  1038. else if (argc)
  1039. usage_with_options(timechart_usage, options);
  1040. setup_pager();
  1041. return __cmd_timechart();
  1042. }