builtin-timechart.c 23 KB

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