builtin-timechart.c 23 KB

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