builtin-timechart.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 <traceevent/event-parse.h>
  15. #include "builtin.h"
  16. #include "util/util.h"
  17. #include "util/color.h"
  18. #include <linux/list.h>
  19. #include "util/cache.h"
  20. #include "util/evlist.h"
  21. #include "util/evsel.h"
  22. #include <linux/rbtree.h>
  23. #include "util/symbol.h"
  24. #include "util/callchain.h"
  25. #include "util/strlist.h"
  26. #include "perf.h"
  27. #include "util/header.h"
  28. #include "util/parse-options.h"
  29. #include "util/parse-events.h"
  30. #include "util/event.h"
  31. #include "util/session.h"
  32. #include "util/svghelper.h"
  33. #include "util/tool.h"
  34. #include "util/data.h"
  35. #define SUPPORT_OLD_POWER_EVENTS 1
  36. #define PWR_EVENT_EXIT -1
  37. static unsigned int numcpus;
  38. static u64 min_freq; /* Lowest CPU frequency seen */
  39. static u64 max_freq; /* Highest CPU frequency seen */
  40. static u64 turbo_frequency;
  41. static u64 first_time, last_time;
  42. static bool power_only;
  43. struct per_pid;
  44. struct per_pidcomm;
  45. struct cpu_sample;
  46. struct power_event;
  47. struct wake_event;
  48. struct sample_wrapper;
  49. /*
  50. * Datastructure layout:
  51. * We keep an list of "pid"s, matching the kernels notion of a task struct.
  52. * Each "pid" entry, has a list of "comm"s.
  53. * this is because we want to track different programs different, while
  54. * exec will reuse the original pid (by design).
  55. * Each comm has a list of samples that will be used to draw
  56. * final graph.
  57. */
  58. struct per_pid {
  59. struct per_pid *next;
  60. int pid;
  61. int ppid;
  62. u64 start_time;
  63. u64 end_time;
  64. u64 total_time;
  65. int display;
  66. struct per_pidcomm *all;
  67. struct per_pidcomm *current;
  68. };
  69. struct per_pidcomm {
  70. struct per_pidcomm *next;
  71. u64 start_time;
  72. u64 end_time;
  73. u64 total_time;
  74. int Y;
  75. int display;
  76. long state;
  77. u64 state_since;
  78. char *comm;
  79. struct cpu_sample *samples;
  80. };
  81. struct sample_wrapper {
  82. struct sample_wrapper *next;
  83. u64 timestamp;
  84. unsigned char data[0];
  85. };
  86. #define TYPE_NONE 0
  87. #define TYPE_RUNNING 1
  88. #define TYPE_WAITING 2
  89. #define TYPE_BLOCKED 3
  90. struct cpu_sample {
  91. struct cpu_sample *next;
  92. u64 start_time;
  93. u64 end_time;
  94. int type;
  95. int cpu;
  96. };
  97. static struct per_pid *all_data;
  98. #define CSTATE 1
  99. #define PSTATE 2
  100. struct power_event {
  101. struct power_event *next;
  102. int type;
  103. int state;
  104. u64 start_time;
  105. u64 end_time;
  106. int cpu;
  107. };
  108. struct wake_event {
  109. struct wake_event *next;
  110. int waker;
  111. int wakee;
  112. u64 time;
  113. };
  114. static struct power_event *power_events;
  115. static struct wake_event *wake_events;
  116. struct process_filter;
  117. struct process_filter {
  118. char *name;
  119. int pid;
  120. struct process_filter *next;
  121. };
  122. static struct process_filter *process_filter;
  123. static struct per_pid *find_create_pid(int pid)
  124. {
  125. struct per_pid *cursor = all_data;
  126. while (cursor) {
  127. if (cursor->pid == pid)
  128. return cursor;
  129. cursor = cursor->next;
  130. }
  131. cursor = zalloc(sizeof(*cursor));
  132. assert(cursor != NULL);
  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 = zalloc(sizeof(*c));
  157. assert(c != NULL);
  158. c->comm = strdup(comm);
  159. p->current = c;
  160. c->next = p->all;
  161. p->all = c;
  162. }
  163. static void pid_fork(int pid, int ppid, u64 timestamp)
  164. {
  165. struct per_pid *p, *pp;
  166. p = find_create_pid(pid);
  167. pp = find_create_pid(ppid);
  168. p->ppid = ppid;
  169. if (pp->current && pp->current->comm && !p->current)
  170. pid_set_comm(pid, pp->current->comm);
  171. p->start_time = timestamp;
  172. if (p->current) {
  173. p->current->start_time = timestamp;
  174. p->current->state_since = timestamp;
  175. }
  176. }
  177. static void pid_exit(int pid, u64 timestamp)
  178. {
  179. struct per_pid *p;
  180. p = find_create_pid(pid);
  181. p->end_time = timestamp;
  182. if (p->current)
  183. p->current->end_time = timestamp;
  184. }
  185. static void
  186. pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
  187. {
  188. struct per_pid *p;
  189. struct per_pidcomm *c;
  190. struct cpu_sample *sample;
  191. p = find_create_pid(pid);
  192. c = p->current;
  193. if (!c) {
  194. c = zalloc(sizeof(*c));
  195. assert(c != NULL);
  196. p->current = c;
  197. c->next = p->all;
  198. p->all = c;
  199. }
  200. sample = zalloc(sizeof(*sample));
  201. assert(sample != NULL);
  202. sample->start_time = start;
  203. sample->end_time = end;
  204. sample->type = type;
  205. sample->next = c->samples;
  206. sample->cpu = cpu;
  207. c->samples = sample;
  208. if (sample->type == TYPE_RUNNING && end > start && start > 0) {
  209. c->total_time += (end-start);
  210. p->total_time += (end-start);
  211. }
  212. if (c->start_time == 0 || c->start_time > start)
  213. c->start_time = start;
  214. if (p->start_time == 0 || p->start_time > start)
  215. p->start_time = start;
  216. }
  217. #define MAX_CPUS 4096
  218. static u64 cpus_cstate_start_times[MAX_CPUS];
  219. static int cpus_cstate_state[MAX_CPUS];
  220. static u64 cpus_pstate_start_times[MAX_CPUS];
  221. static u64 cpus_pstate_state[MAX_CPUS];
  222. static int process_comm_event(struct perf_tool *tool __maybe_unused,
  223. union perf_event *event,
  224. struct perf_sample *sample __maybe_unused,
  225. struct machine *machine __maybe_unused)
  226. {
  227. pid_set_comm(event->comm.tid, event->comm.comm);
  228. return 0;
  229. }
  230. static int process_fork_event(struct perf_tool *tool __maybe_unused,
  231. union perf_event *event,
  232. struct perf_sample *sample __maybe_unused,
  233. struct machine *machine __maybe_unused)
  234. {
  235. pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
  236. return 0;
  237. }
  238. static int process_exit_event(struct perf_tool *tool __maybe_unused,
  239. union perf_event *event,
  240. struct perf_sample *sample __maybe_unused,
  241. struct machine *machine __maybe_unused)
  242. {
  243. pid_exit(event->fork.pid, event->fork.time);
  244. return 0;
  245. }
  246. struct trace_entry {
  247. unsigned short type;
  248. unsigned char flags;
  249. unsigned char preempt_count;
  250. int pid;
  251. int lock_depth;
  252. };
  253. #ifdef SUPPORT_OLD_POWER_EVENTS
  254. static int use_old_power_events;
  255. struct power_entry_old {
  256. struct trace_entry te;
  257. u64 type;
  258. u64 value;
  259. u64 cpu_id;
  260. };
  261. #endif
  262. struct power_processor_entry {
  263. struct trace_entry te;
  264. u32 state;
  265. u32 cpu_id;
  266. };
  267. #define TASK_COMM_LEN 16
  268. struct wakeup_entry {
  269. struct trace_entry te;
  270. char comm[TASK_COMM_LEN];
  271. int pid;
  272. int prio;
  273. int success;
  274. };
  275. struct sched_switch {
  276. struct trace_entry te;
  277. char prev_comm[TASK_COMM_LEN];
  278. int prev_pid;
  279. int prev_prio;
  280. long prev_state; /* Arjan weeps. */
  281. char next_comm[TASK_COMM_LEN];
  282. int next_pid;
  283. int next_prio;
  284. };
  285. static void c_state_start(int cpu, u64 timestamp, int state)
  286. {
  287. cpus_cstate_start_times[cpu] = timestamp;
  288. cpus_cstate_state[cpu] = state;
  289. }
  290. static void c_state_end(int cpu, u64 timestamp)
  291. {
  292. struct power_event *pwr = zalloc(sizeof(*pwr));
  293. if (!pwr)
  294. return;
  295. pwr->state = cpus_cstate_state[cpu];
  296. pwr->start_time = cpus_cstate_start_times[cpu];
  297. pwr->end_time = timestamp;
  298. pwr->cpu = cpu;
  299. pwr->type = CSTATE;
  300. pwr->next = power_events;
  301. power_events = pwr;
  302. }
  303. static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
  304. {
  305. struct power_event *pwr;
  306. if (new_freq > 8000000) /* detect invalid data */
  307. return;
  308. pwr = zalloc(sizeof(*pwr));
  309. if (!pwr)
  310. return;
  311. pwr->state = cpus_pstate_state[cpu];
  312. pwr->start_time = cpus_pstate_start_times[cpu];
  313. pwr->end_time = timestamp;
  314. pwr->cpu = cpu;
  315. pwr->type = PSTATE;
  316. pwr->next = power_events;
  317. if (!pwr->start_time)
  318. pwr->start_time = first_time;
  319. power_events = pwr;
  320. cpus_pstate_state[cpu] = new_freq;
  321. cpus_pstate_start_times[cpu] = timestamp;
  322. if ((u64)new_freq > max_freq)
  323. max_freq = new_freq;
  324. if (new_freq < min_freq || min_freq == 0)
  325. min_freq = new_freq;
  326. if (new_freq == max_freq - 1000)
  327. turbo_frequency = max_freq;
  328. }
  329. static void
  330. sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
  331. {
  332. struct per_pid *p;
  333. struct wakeup_entry *wake = (void *)te;
  334. struct wake_event *we = zalloc(sizeof(*we));
  335. if (!we)
  336. return;
  337. we->time = timestamp;
  338. we->waker = pid;
  339. if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
  340. we->waker = -1;
  341. we->wakee = wake->pid;
  342. we->next = wake_events;
  343. wake_events = we;
  344. p = find_create_pid(we->wakee);
  345. if (p && p->current && p->current->state == TYPE_NONE) {
  346. p->current->state_since = timestamp;
  347. p->current->state = TYPE_WAITING;
  348. }
  349. if (p && p->current && p->current->state == TYPE_BLOCKED) {
  350. pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
  351. p->current->state_since = timestamp;
  352. p->current->state = TYPE_WAITING;
  353. }
  354. }
  355. static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
  356. {
  357. struct per_pid *p = NULL, *prev_p;
  358. struct sched_switch *sw = (void *)te;
  359. prev_p = find_create_pid(sw->prev_pid);
  360. p = find_create_pid(sw->next_pid);
  361. if (prev_p->current && prev_p->current->state != TYPE_NONE)
  362. pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
  363. if (p && p->current) {
  364. if (p->current->state != TYPE_NONE)
  365. pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
  366. p->current->state_since = timestamp;
  367. p->current->state = TYPE_RUNNING;
  368. }
  369. if (prev_p->current) {
  370. prev_p->current->state = TYPE_NONE;
  371. prev_p->current->state_since = timestamp;
  372. if (sw->prev_state & 2)
  373. prev_p->current->state = TYPE_BLOCKED;
  374. if (sw->prev_state == 0)
  375. prev_p->current->state = TYPE_WAITING;
  376. }
  377. }
  378. typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  379. struct perf_sample *sample);
  380. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  381. union perf_event *event __maybe_unused,
  382. struct perf_sample *sample,
  383. struct perf_evsel *evsel,
  384. struct machine *machine __maybe_unused)
  385. {
  386. if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
  387. if (!first_time || first_time > sample->time)
  388. first_time = sample->time;
  389. if (last_time < sample->time)
  390. last_time = sample->time;
  391. }
  392. if (sample->cpu > numcpus)
  393. numcpus = sample->cpu;
  394. if (evsel->handler != NULL) {
  395. tracepoint_handler f = evsel->handler;
  396. return f(evsel, sample);
  397. }
  398. return 0;
  399. }
  400. static int
  401. process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
  402. struct perf_sample *sample)
  403. {
  404. struct power_processor_entry *ppe = sample->raw_data;
  405. if (ppe->state == (u32) PWR_EVENT_EXIT)
  406. c_state_end(ppe->cpu_id, sample->time);
  407. else
  408. c_state_start(ppe->cpu_id, sample->time, ppe->state);
  409. return 0;
  410. }
  411. static int
  412. process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
  413. struct perf_sample *sample)
  414. {
  415. struct power_processor_entry *ppe = sample->raw_data;
  416. p_state_change(ppe->cpu_id, sample->time, ppe->state);
  417. return 0;
  418. }
  419. static int
  420. process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
  421. struct perf_sample *sample)
  422. {
  423. struct trace_entry *te = sample->raw_data;
  424. sched_wakeup(sample->cpu, sample->time, sample->pid, te);
  425. return 0;
  426. }
  427. static int
  428. process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
  429. struct perf_sample *sample)
  430. {
  431. struct trace_entry *te = sample->raw_data;
  432. sched_switch(sample->cpu, sample->time, te);
  433. return 0;
  434. }
  435. #ifdef SUPPORT_OLD_POWER_EVENTS
  436. static int
  437. process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
  438. struct perf_sample *sample)
  439. {
  440. struct power_entry_old *peo = sample->raw_data;
  441. c_state_start(peo->cpu_id, sample->time, peo->value);
  442. return 0;
  443. }
  444. static int
  445. process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
  446. struct perf_sample *sample)
  447. {
  448. c_state_end(sample->cpu, sample->time);
  449. return 0;
  450. }
  451. static int
  452. process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
  453. struct perf_sample *sample)
  454. {
  455. struct power_entry_old *peo = sample->raw_data;
  456. p_state_change(peo->cpu_id, sample->time, peo->value);
  457. return 0;
  458. }
  459. #endif /* SUPPORT_OLD_POWER_EVENTS */
  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 int __cmd_timechart(const char *output_name)
  800. {
  801. struct perf_tool perf_timechart = {
  802. .comm = process_comm_event,
  803. .fork = process_fork_event,
  804. .exit = process_exit_event,
  805. .sample = process_sample_event,
  806. .ordered_samples = true,
  807. };
  808. const struct perf_evsel_str_handler power_tracepoints[] = {
  809. { "power:cpu_idle", process_sample_cpu_idle },
  810. { "power:cpu_frequency", process_sample_cpu_frequency },
  811. { "sched:sched_wakeup", process_sample_sched_wakeup },
  812. { "sched:sched_switch", process_sample_sched_switch },
  813. #ifdef SUPPORT_OLD_POWER_EVENTS
  814. { "power:power_start", process_sample_power_start },
  815. { "power:power_end", process_sample_power_end },
  816. { "power:power_frequency", process_sample_power_frequency },
  817. #endif
  818. };
  819. struct perf_data_file file = {
  820. .path = input_name,
  821. .mode = PERF_DATA_MODE_READ,
  822. };
  823. struct perf_session *session = perf_session__new(&file, false,
  824. &perf_timechart);
  825. int ret = -EINVAL;
  826. if (session == NULL)
  827. return -ENOMEM;
  828. if (!perf_session__has_traces(session, "timechart record"))
  829. goto out_delete;
  830. if (perf_session__set_tracepoints_handlers(session,
  831. power_tracepoints)) {
  832. pr_err("Initializing session tracepoint handlers failed\n");
  833. goto out_delete;
  834. }
  835. ret = perf_session__process_events(session, &perf_timechart);
  836. if (ret)
  837. goto out_delete;
  838. end_sample_processing();
  839. sort_pids();
  840. write_svg_file(output_name);
  841. pr_info("Written %2.1f seconds of trace to %s.\n",
  842. (last_time - first_time) / 1000000000.0, output_name);
  843. out_delete:
  844. perf_session__delete(session);
  845. return ret;
  846. }
  847. static int __cmd_record(int argc, const char **argv)
  848. {
  849. #ifdef SUPPORT_OLD_POWER_EVENTS
  850. const char * const record_old_args[] = {
  851. "record", "-a", "-R", "-c", "1",
  852. "-e", "power:power_start",
  853. "-e", "power:power_end",
  854. "-e", "power:power_frequency",
  855. "-e", "sched:sched_wakeup",
  856. "-e", "sched:sched_switch",
  857. };
  858. #endif
  859. const char * const record_new_args[] = {
  860. "record", "-a", "-R", "-c", "1",
  861. "-e", "power:cpu_frequency",
  862. "-e", "power:cpu_idle",
  863. "-e", "sched:sched_wakeup",
  864. "-e", "sched:sched_switch",
  865. };
  866. unsigned int rec_argc, i, j;
  867. const char **rec_argv;
  868. const char * const *record_args = record_new_args;
  869. unsigned int record_elems = ARRAY_SIZE(record_new_args);
  870. #ifdef SUPPORT_OLD_POWER_EVENTS
  871. if (!is_valid_tracepoint("power:cpu_idle") &&
  872. is_valid_tracepoint("power:power_start")) {
  873. use_old_power_events = 1;
  874. record_args = record_old_args;
  875. record_elems = ARRAY_SIZE(record_old_args);
  876. }
  877. #endif
  878. rec_argc = record_elems + argc - 1;
  879. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  880. if (rec_argv == NULL)
  881. return -ENOMEM;
  882. for (i = 0; i < record_elems; i++)
  883. rec_argv[i] = strdup(record_args[i]);
  884. for (j = 1; j < (unsigned int)argc; j++, i++)
  885. rec_argv[i] = argv[j];
  886. return cmd_record(i, rec_argv, NULL);
  887. }
  888. static int
  889. parse_process(const struct option *opt __maybe_unused, const char *arg,
  890. int __maybe_unused unset)
  891. {
  892. if (arg)
  893. add_process_filter(arg);
  894. return 0;
  895. }
  896. int cmd_timechart(int argc, const char **argv,
  897. const char *prefix __maybe_unused)
  898. {
  899. const char *output_name = "output.svg";
  900. const struct option options[] = {
  901. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  902. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  903. OPT_INTEGER('w', "width", &svg_page_width, "page width"),
  904. OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
  905. OPT_CALLBACK('p', "process", NULL, "process",
  906. "process selector. Pass a pid or process name.",
  907. parse_process),
  908. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  909. "Look for files with symbols relative to this directory"),
  910. OPT_END()
  911. };
  912. const char * const timechart_usage[] = {
  913. "perf timechart [<options>] {record}",
  914. NULL
  915. };
  916. argc = parse_options(argc, argv, options, timechart_usage,
  917. PARSE_OPT_STOP_AT_NON_OPTION);
  918. symbol__init();
  919. if (argc && !strncmp(argv[0], "rec", 3))
  920. return __cmd_record(argc, argv);
  921. else if (argc)
  922. usage_with_options(timechart_usage, options);
  923. setup_pager();
  924. return __cmd_timechart(output_name);
  925. }