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