builtin-timechart.c 23 KB

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