builtin-timechart.c 23 KB

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