Merge tag 'soc-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[linux-2.6.git] / tools / perf / builtin-timechart.c
blobab4cf232b8522db25dfaeaf399c05790053d39c9
1 /*
2 * builtin-timechart.c - make an svg timechart of system activity
4 * (C) Copyright 2009 Intel Corporation
6 * Authors:
7 * Arjan van de Ven <arjan@linux.intel.com>
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.
15 #include "builtin.h"
17 #include "util/util.h"
19 #include "util/color.h"
20 #include <linux/list.h>
21 #include "util/cache.h"
22 #include "util/evsel.h"
23 #include <linux/rbtree.h>
24 #include "util/symbol.h"
25 #include "util/callchain.h"
26 #include "util/strlist.h"
28 #include "perf.h"
29 #include "util/header.h"
30 #include "util/parse-options.h"
31 #include "util/parse-events.h"
32 #include "util/event.h"
33 #include "util/session.h"
34 #include "util/svghelper.h"
35 #include "util/tool.h"
37 #define SUPPORT_OLD_POWER_EVENTS 1
38 #define PWR_EVENT_EXIT -1
41 static unsigned int numcpus;
42 static u64 min_freq; /* Lowest CPU frequency seen */
43 static u64 max_freq; /* Highest CPU frequency seen */
44 static u64 turbo_frequency;
46 static u64 first_time, last_time;
48 static bool power_only;
51 struct per_pid;
52 struct per_pidcomm;
54 struct cpu_sample;
55 struct power_event;
56 struct wake_event;
58 struct sample_wrapper;
61 * Datastructure layout:
62 * We keep an list of "pid"s, matching the kernels notion of a task struct.
63 * Each "pid" entry, has a list of "comm"s.
64 * this is because we want to track different programs different, while
65 * exec will reuse the original pid (by design).
66 * Each comm has a list of samples that will be used to draw
67 * final graph.
70 struct per_pid {
71 struct per_pid *next;
73 int pid;
74 int ppid;
76 u64 start_time;
77 u64 end_time;
78 u64 total_time;
79 int display;
81 struct per_pidcomm *all;
82 struct per_pidcomm *current;
86 struct per_pidcomm {
87 struct per_pidcomm *next;
89 u64 start_time;
90 u64 end_time;
91 u64 total_time;
93 int Y;
94 int display;
96 long state;
97 u64 state_since;
99 char *comm;
101 struct cpu_sample *samples;
104 struct sample_wrapper {
105 struct sample_wrapper *next;
107 u64 timestamp;
108 unsigned char data[0];
111 #define TYPE_NONE 0
112 #define TYPE_RUNNING 1
113 #define TYPE_WAITING 2
114 #define TYPE_BLOCKED 3
116 struct cpu_sample {
117 struct cpu_sample *next;
119 u64 start_time;
120 u64 end_time;
121 int type;
122 int cpu;
125 static struct per_pid *all_data;
127 #define CSTATE 1
128 #define PSTATE 2
130 struct power_event {
131 struct power_event *next;
132 int type;
133 int state;
134 u64 start_time;
135 u64 end_time;
136 int cpu;
139 struct wake_event {
140 struct wake_event *next;
141 int waker;
142 int wakee;
143 u64 time;
146 static struct power_event *power_events;
147 static struct wake_event *wake_events;
149 struct process_filter;
150 struct process_filter {
151 char *name;
152 int pid;
153 struct process_filter *next;
156 static struct process_filter *process_filter;
159 static struct per_pid *find_create_pid(int pid)
161 struct per_pid *cursor = all_data;
163 while (cursor) {
164 if (cursor->pid == pid)
165 return cursor;
166 cursor = cursor->next;
168 cursor = zalloc(sizeof(*cursor));
169 assert(cursor != NULL);
170 cursor->pid = pid;
171 cursor->next = all_data;
172 all_data = cursor;
173 return cursor;
176 static void pid_set_comm(int pid, char *comm)
178 struct per_pid *p;
179 struct per_pidcomm *c;
180 p = find_create_pid(pid);
181 c = p->all;
182 while (c) {
183 if (c->comm && strcmp(c->comm, comm) == 0) {
184 p->current = c;
185 return;
187 if (!c->comm) {
188 c->comm = strdup(comm);
189 p->current = c;
190 return;
192 c = c->next;
194 c = zalloc(sizeof(*c));
195 assert(c != NULL);
196 c->comm = strdup(comm);
197 p->current = c;
198 c->next = p->all;
199 p->all = c;
202 static void pid_fork(int pid, int ppid, u64 timestamp)
204 struct per_pid *p, *pp;
205 p = find_create_pid(pid);
206 pp = find_create_pid(ppid);
207 p->ppid = ppid;
208 if (pp->current && pp->current->comm && !p->current)
209 pid_set_comm(pid, pp->current->comm);
211 p->start_time = timestamp;
212 if (p->current) {
213 p->current->start_time = timestamp;
214 p->current->state_since = timestamp;
218 static void pid_exit(int pid, u64 timestamp)
220 struct per_pid *p;
221 p = find_create_pid(pid);
222 p->end_time = timestamp;
223 if (p->current)
224 p->current->end_time = timestamp;
227 static void
228 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
230 struct per_pid *p;
231 struct per_pidcomm *c;
232 struct cpu_sample *sample;
234 p = find_create_pid(pid);
235 c = p->current;
236 if (!c) {
237 c = zalloc(sizeof(*c));
238 assert(c != NULL);
239 p->current = c;
240 c->next = p->all;
241 p->all = c;
244 sample = zalloc(sizeof(*sample));
245 assert(sample != NULL);
246 sample->start_time = start;
247 sample->end_time = end;
248 sample->type = type;
249 sample->next = c->samples;
250 sample->cpu = cpu;
251 c->samples = sample;
253 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
254 c->total_time += (end-start);
255 p->total_time += (end-start);
258 if (c->start_time == 0 || c->start_time > start)
259 c->start_time = start;
260 if (p->start_time == 0 || p->start_time > start)
261 p->start_time = start;
264 #define MAX_CPUS 4096
266 static u64 cpus_cstate_start_times[MAX_CPUS];
267 static int cpus_cstate_state[MAX_CPUS];
268 static u64 cpus_pstate_start_times[MAX_CPUS];
269 static u64 cpus_pstate_state[MAX_CPUS];
271 static int process_comm_event(struct perf_tool *tool __maybe_unused,
272 union perf_event *event,
273 struct perf_sample *sample __maybe_unused,
274 struct machine *machine __maybe_unused)
276 pid_set_comm(event->comm.tid, event->comm.comm);
277 return 0;
280 static int process_fork_event(struct perf_tool *tool __maybe_unused,
281 union perf_event *event,
282 struct perf_sample *sample __maybe_unused,
283 struct machine *machine __maybe_unused)
285 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
286 return 0;
289 static int process_exit_event(struct perf_tool *tool __maybe_unused,
290 union perf_event *event,
291 struct perf_sample *sample __maybe_unused,
292 struct machine *machine __maybe_unused)
294 pid_exit(event->fork.pid, event->fork.time);
295 return 0;
298 struct trace_entry {
299 unsigned short type;
300 unsigned char flags;
301 unsigned char preempt_count;
302 int pid;
303 int lock_depth;
306 #ifdef SUPPORT_OLD_POWER_EVENTS
307 static int use_old_power_events;
308 struct power_entry_old {
309 struct trace_entry te;
310 u64 type;
311 u64 value;
312 u64 cpu_id;
314 #endif
316 struct power_processor_entry {
317 struct trace_entry te;
318 u32 state;
319 u32 cpu_id;
322 #define TASK_COMM_LEN 16
323 struct wakeup_entry {
324 struct trace_entry te;
325 char comm[TASK_COMM_LEN];
326 int pid;
327 int prio;
328 int success;
332 * trace_flag_type is an enumeration that holds different
333 * states when a trace occurs. These are:
334 * IRQS_OFF - interrupts were disabled
335 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
336 * NEED_RESCED - reschedule is requested
337 * HARDIRQ - inside an interrupt handler
338 * SOFTIRQ - inside a softirq handler
340 enum trace_flag_type {
341 TRACE_FLAG_IRQS_OFF = 0x01,
342 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
343 TRACE_FLAG_NEED_RESCHED = 0x04,
344 TRACE_FLAG_HARDIRQ = 0x08,
345 TRACE_FLAG_SOFTIRQ = 0x10,
350 struct sched_switch {
351 struct trace_entry te;
352 char prev_comm[TASK_COMM_LEN];
353 int prev_pid;
354 int prev_prio;
355 long prev_state; /* Arjan weeps. */
356 char next_comm[TASK_COMM_LEN];
357 int next_pid;
358 int next_prio;
361 static void c_state_start(int cpu, u64 timestamp, int state)
363 cpus_cstate_start_times[cpu] = timestamp;
364 cpus_cstate_state[cpu] = state;
367 static void c_state_end(int cpu, u64 timestamp)
369 struct power_event *pwr = zalloc(sizeof(*pwr));
371 if (!pwr)
372 return;
374 pwr->state = cpus_cstate_state[cpu];
375 pwr->start_time = cpus_cstate_start_times[cpu];
376 pwr->end_time = timestamp;
377 pwr->cpu = cpu;
378 pwr->type = CSTATE;
379 pwr->next = power_events;
381 power_events = pwr;
384 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
386 struct power_event *pwr;
388 if (new_freq > 8000000) /* detect invalid data */
389 return;
391 pwr = zalloc(sizeof(*pwr));
392 if (!pwr)
393 return;
395 pwr->state = cpus_pstate_state[cpu];
396 pwr->start_time = cpus_pstate_start_times[cpu];
397 pwr->end_time = timestamp;
398 pwr->cpu = cpu;
399 pwr->type = PSTATE;
400 pwr->next = power_events;
402 if (!pwr->start_time)
403 pwr->start_time = first_time;
405 power_events = pwr;
407 cpus_pstate_state[cpu] = new_freq;
408 cpus_pstate_start_times[cpu] = timestamp;
410 if ((u64)new_freq > max_freq)
411 max_freq = new_freq;
413 if (new_freq < min_freq || min_freq == 0)
414 min_freq = new_freq;
416 if (new_freq == max_freq - 1000)
417 turbo_frequency = max_freq;
420 static void
421 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
423 struct per_pid *p;
424 struct wakeup_entry *wake = (void *)te;
425 struct wake_event *we = zalloc(sizeof(*we));
427 if (!we)
428 return;
430 we->time = timestamp;
431 we->waker = pid;
433 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
434 we->waker = -1;
436 we->wakee = wake->pid;
437 we->next = wake_events;
438 wake_events = we;
439 p = find_create_pid(we->wakee);
441 if (p && p->current && p->current->state == TYPE_NONE) {
442 p->current->state_since = timestamp;
443 p->current->state = TYPE_WAITING;
445 if (p && p->current && p->current->state == TYPE_BLOCKED) {
446 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
447 p->current->state_since = timestamp;
448 p->current->state = TYPE_WAITING;
452 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
454 struct per_pid *p = NULL, *prev_p;
455 struct sched_switch *sw = (void *)te;
458 prev_p = find_create_pid(sw->prev_pid);
460 p = find_create_pid(sw->next_pid);
462 if (prev_p->current && prev_p->current->state != TYPE_NONE)
463 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
464 if (p && p->current) {
465 if (p->current->state != TYPE_NONE)
466 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
468 p->current->state_since = timestamp;
469 p->current->state = TYPE_RUNNING;
472 if (prev_p->current) {
473 prev_p->current->state = TYPE_NONE;
474 prev_p->current->state_since = timestamp;
475 if (sw->prev_state & 2)
476 prev_p->current->state = TYPE_BLOCKED;
477 if (sw->prev_state == 0)
478 prev_p->current->state = TYPE_WAITING;
483 static int process_sample_event(struct perf_tool *tool __maybe_unused,
484 union perf_event *event __maybe_unused,
485 struct perf_sample *sample,
486 struct perf_evsel *evsel,
487 struct machine *machine __maybe_unused)
489 struct trace_entry *te;
491 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
492 if (!first_time || first_time > sample->time)
493 first_time = sample->time;
494 if (last_time < sample->time)
495 last_time = sample->time;
498 te = (void *)sample->raw_data;
499 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
500 char *event_str;
501 #ifdef SUPPORT_OLD_POWER_EVENTS
502 struct power_entry_old *peo;
503 peo = (void *)te;
504 #endif
506 * FIXME: use evsel, its already mapped from id to perf_evsel,
507 * remove perf_header__find_event infrastructure bits.
508 * Mapping all these "power:cpu_idle" strings to the tracepoint
509 * ID and then just comparing against evsel->attr.config.
511 * e.g.:
513 * if (evsel->attr.config == power_cpu_idle_id)
515 event_str = perf_header__find_event(te->type);
517 if (!event_str)
518 return 0;
520 if (sample->cpu > numcpus)
521 numcpus = sample->cpu;
523 if (strcmp(event_str, "power:cpu_idle") == 0) {
524 struct power_processor_entry *ppe = (void *)te;
525 if (ppe->state == (u32)PWR_EVENT_EXIT)
526 c_state_end(ppe->cpu_id, sample->time);
527 else
528 c_state_start(ppe->cpu_id, sample->time,
529 ppe->state);
531 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
532 struct power_processor_entry *ppe = (void *)te;
533 p_state_change(ppe->cpu_id, sample->time, ppe->state);
536 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
537 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
539 else if (strcmp(event_str, "sched:sched_switch") == 0)
540 sched_switch(sample->cpu, sample->time, te);
542 #ifdef SUPPORT_OLD_POWER_EVENTS
543 if (use_old_power_events) {
544 if (strcmp(event_str, "power:power_start") == 0)
545 c_state_start(peo->cpu_id, sample->time,
546 peo->value);
548 else if (strcmp(event_str, "power:power_end") == 0)
549 c_state_end(sample->cpu, sample->time);
551 else if (strcmp(event_str,
552 "power:power_frequency") == 0)
553 p_state_change(peo->cpu_id, sample->time,
554 peo->value);
556 #endif
558 return 0;
562 * After the last sample we need to wrap up the current C/P state
563 * and close out each CPU for these.
565 static void end_sample_processing(void)
567 u64 cpu;
568 struct power_event *pwr;
570 for (cpu = 0; cpu <= numcpus; cpu++) {
571 /* C state */
572 #if 0
573 pwr = zalloc(sizeof(*pwr));
574 if (!pwr)
575 return;
577 pwr->state = cpus_cstate_state[cpu];
578 pwr->start_time = cpus_cstate_start_times[cpu];
579 pwr->end_time = last_time;
580 pwr->cpu = cpu;
581 pwr->type = CSTATE;
582 pwr->next = power_events;
584 power_events = pwr;
585 #endif
586 /* P state */
588 pwr = zalloc(sizeof(*pwr));
589 if (!pwr)
590 return;
592 pwr->state = cpus_pstate_state[cpu];
593 pwr->start_time = cpus_pstate_start_times[cpu];
594 pwr->end_time = last_time;
595 pwr->cpu = cpu;
596 pwr->type = PSTATE;
597 pwr->next = power_events;
599 if (!pwr->start_time)
600 pwr->start_time = first_time;
601 if (!pwr->state)
602 pwr->state = min_freq;
603 power_events = pwr;
608 * Sort the pid datastructure
610 static void sort_pids(void)
612 struct per_pid *new_list, *p, *cursor, *prev;
613 /* sort by ppid first, then by pid, lowest to highest */
615 new_list = NULL;
617 while (all_data) {
618 p = all_data;
619 all_data = p->next;
620 p->next = NULL;
622 if (new_list == NULL) {
623 new_list = p;
624 p->next = NULL;
625 continue;
627 prev = NULL;
628 cursor = new_list;
629 while (cursor) {
630 if (cursor->ppid > p->ppid ||
631 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
632 /* must insert before */
633 if (prev) {
634 p->next = prev->next;
635 prev->next = p;
636 cursor = NULL;
637 continue;
638 } else {
639 p->next = new_list;
640 new_list = p;
641 cursor = NULL;
642 continue;
646 prev = cursor;
647 cursor = cursor->next;
648 if (!cursor)
649 prev->next = p;
652 all_data = new_list;
656 static void draw_c_p_states(void)
658 struct power_event *pwr;
659 pwr = power_events;
662 * two pass drawing so that the P state bars are on top of the C state blocks
664 while (pwr) {
665 if (pwr->type == CSTATE)
666 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
667 pwr = pwr->next;
670 pwr = power_events;
671 while (pwr) {
672 if (pwr->type == PSTATE) {
673 if (!pwr->state)
674 pwr->state = min_freq;
675 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
677 pwr = pwr->next;
681 static void draw_wakeups(void)
683 struct wake_event *we;
684 struct per_pid *p;
685 struct per_pidcomm *c;
687 we = wake_events;
688 while (we) {
689 int from = 0, to = 0;
690 char *task_from = NULL, *task_to = NULL;
692 /* locate the column of the waker and wakee */
693 p = all_data;
694 while (p) {
695 if (p->pid == we->waker || p->pid == we->wakee) {
696 c = p->all;
697 while (c) {
698 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
699 if (p->pid == we->waker && !from) {
700 from = c->Y;
701 task_from = strdup(c->comm);
703 if (p->pid == we->wakee && !to) {
704 to = c->Y;
705 task_to = strdup(c->comm);
708 c = c->next;
710 c = p->all;
711 while (c) {
712 if (p->pid == we->waker && !from) {
713 from = c->Y;
714 task_from = strdup(c->comm);
716 if (p->pid == we->wakee && !to) {
717 to = c->Y;
718 task_to = strdup(c->comm);
720 c = c->next;
723 p = p->next;
726 if (!task_from) {
727 task_from = malloc(40);
728 sprintf(task_from, "[%i]", we->waker);
730 if (!task_to) {
731 task_to = malloc(40);
732 sprintf(task_to, "[%i]", we->wakee);
735 if (we->waker == -1)
736 svg_interrupt(we->time, to);
737 else if (from && to && abs(from - to) == 1)
738 svg_wakeline(we->time, from, to);
739 else
740 svg_partial_wakeline(we->time, from, task_from, to, task_to);
741 we = we->next;
743 free(task_from);
744 free(task_to);
748 static void draw_cpu_usage(void)
750 struct per_pid *p;
751 struct per_pidcomm *c;
752 struct cpu_sample *sample;
753 p = all_data;
754 while (p) {
755 c = p->all;
756 while (c) {
757 sample = c->samples;
758 while (sample) {
759 if (sample->type == TYPE_RUNNING)
760 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
762 sample = sample->next;
764 c = c->next;
766 p = p->next;
770 static void draw_process_bars(void)
772 struct per_pid *p;
773 struct per_pidcomm *c;
774 struct cpu_sample *sample;
775 int Y = 0;
777 Y = 2 * numcpus + 2;
779 p = all_data;
780 while (p) {
781 c = p->all;
782 while (c) {
783 if (!c->display) {
784 c->Y = 0;
785 c = c->next;
786 continue;
789 svg_box(Y, c->start_time, c->end_time, "process");
790 sample = c->samples;
791 while (sample) {
792 if (sample->type == TYPE_RUNNING)
793 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
794 if (sample->type == TYPE_BLOCKED)
795 svg_box(Y, sample->start_time, sample->end_time, "blocked");
796 if (sample->type == TYPE_WAITING)
797 svg_waiting(Y, sample->start_time, sample->end_time);
798 sample = sample->next;
801 if (c->comm) {
802 char comm[256];
803 if (c->total_time > 5000000000) /* 5 seconds */
804 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
805 else
806 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
808 svg_text(Y, c->start_time, comm);
810 c->Y = Y;
811 Y++;
812 c = c->next;
814 p = p->next;
818 static void add_process_filter(const char *string)
820 int pid = strtoull(string, NULL, 10);
821 struct process_filter *filt = malloc(sizeof(*filt));
823 if (!filt)
824 return;
826 filt->name = strdup(string);
827 filt->pid = pid;
828 filt->next = process_filter;
830 process_filter = filt;
833 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
835 struct process_filter *filt;
836 if (!process_filter)
837 return 1;
839 filt = process_filter;
840 while (filt) {
841 if (filt->pid && p->pid == filt->pid)
842 return 1;
843 if (strcmp(filt->name, c->comm) == 0)
844 return 1;
845 filt = filt->next;
847 return 0;
850 static int determine_display_tasks_filtered(void)
852 struct per_pid *p;
853 struct per_pidcomm *c;
854 int count = 0;
856 p = all_data;
857 while (p) {
858 p->display = 0;
859 if (p->start_time == 1)
860 p->start_time = first_time;
862 /* no exit marker, task kept running to the end */
863 if (p->end_time == 0)
864 p->end_time = last_time;
866 c = p->all;
868 while (c) {
869 c->display = 0;
871 if (c->start_time == 1)
872 c->start_time = first_time;
874 if (passes_filter(p, c)) {
875 c->display = 1;
876 p->display = 1;
877 count++;
880 if (c->end_time == 0)
881 c->end_time = last_time;
883 c = c->next;
885 p = p->next;
887 return count;
890 static int determine_display_tasks(u64 threshold)
892 struct per_pid *p;
893 struct per_pidcomm *c;
894 int count = 0;
896 if (process_filter)
897 return determine_display_tasks_filtered();
899 p = all_data;
900 while (p) {
901 p->display = 0;
902 if (p->start_time == 1)
903 p->start_time = first_time;
905 /* no exit marker, task kept running to the end */
906 if (p->end_time == 0)
907 p->end_time = last_time;
908 if (p->total_time >= threshold && !power_only)
909 p->display = 1;
911 c = p->all;
913 while (c) {
914 c->display = 0;
916 if (c->start_time == 1)
917 c->start_time = first_time;
919 if (c->total_time >= threshold && !power_only) {
920 c->display = 1;
921 count++;
924 if (c->end_time == 0)
925 c->end_time = last_time;
927 c = c->next;
929 p = p->next;
931 return count;
936 #define TIME_THRESH 10000000
938 static void write_svg_file(const char *filename)
940 u64 i;
941 int count;
943 numcpus++;
946 count = determine_display_tasks(TIME_THRESH);
948 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
949 if (count < 15)
950 count = determine_display_tasks(TIME_THRESH / 10);
952 open_svg(filename, numcpus, count, first_time, last_time);
954 svg_time_grid();
955 svg_legenda();
957 for (i = 0; i < numcpus; i++)
958 svg_cpu_box(i, max_freq, turbo_frequency);
960 draw_cpu_usage();
961 draw_process_bars();
962 draw_c_p_states();
963 draw_wakeups();
965 svg_close();
968 static int __cmd_timechart(const char *output_name)
970 struct perf_tool perf_timechart = {
971 .comm = process_comm_event,
972 .fork = process_fork_event,
973 .exit = process_exit_event,
974 .sample = process_sample_event,
975 .ordered_samples = true,
977 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
978 0, false, &perf_timechart);
979 int ret = -EINVAL;
981 if (session == NULL)
982 return -ENOMEM;
984 if (!perf_session__has_traces(session, "timechart record"))
985 goto out_delete;
987 ret = perf_session__process_events(session, &perf_timechart);
988 if (ret)
989 goto out_delete;
991 end_sample_processing();
993 sort_pids();
995 write_svg_file(output_name);
997 pr_info("Written %2.1f seconds of trace to %s.\n",
998 (last_time - first_time) / 1000000000.0, output_name);
999 out_delete:
1000 perf_session__delete(session);
1001 return ret;
1004 static int __cmd_record(int argc, const char **argv)
1006 #ifdef SUPPORT_OLD_POWER_EVENTS
1007 const char * const record_old_args[] = {
1008 "record", "-a", "-R", "-f", "-c", "1",
1009 "-e", "power:power_start",
1010 "-e", "power:power_end",
1011 "-e", "power:power_frequency",
1012 "-e", "sched:sched_wakeup",
1013 "-e", "sched:sched_switch",
1015 #endif
1016 const char * const record_new_args[] = {
1017 "record", "-a", "-R", "-f", "-c", "1",
1018 "-e", "power:cpu_frequency",
1019 "-e", "power:cpu_idle",
1020 "-e", "sched:sched_wakeup",
1021 "-e", "sched:sched_switch",
1023 unsigned int rec_argc, i, j;
1024 const char **rec_argv;
1025 const char * const *record_args = record_new_args;
1026 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1028 #ifdef SUPPORT_OLD_POWER_EVENTS
1029 if (!is_valid_tracepoint("power:cpu_idle") &&
1030 is_valid_tracepoint("power:power_start")) {
1031 use_old_power_events = 1;
1032 record_args = record_old_args;
1033 record_elems = ARRAY_SIZE(record_old_args);
1035 #endif
1037 rec_argc = record_elems + argc - 1;
1038 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1040 if (rec_argv == NULL)
1041 return -ENOMEM;
1043 for (i = 0; i < record_elems; i++)
1044 rec_argv[i] = strdup(record_args[i]);
1046 for (j = 1; j < (unsigned int)argc; j++, i++)
1047 rec_argv[i] = argv[j];
1049 return cmd_record(i, rec_argv, NULL);
1052 static int
1053 parse_process(const struct option *opt __maybe_unused, const char *arg,
1054 int __maybe_unused unset)
1056 if (arg)
1057 add_process_filter(arg);
1058 return 0;
1061 int cmd_timechart(int argc, const char **argv,
1062 const char *prefix __maybe_unused)
1064 const char *output_name = "output.svg";
1065 const struct option options[] = {
1066 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1067 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1068 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1069 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
1070 OPT_CALLBACK('p', "process", NULL, "process",
1071 "process selector. Pass a pid or process name.",
1072 parse_process),
1073 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1074 "Look for files with symbols relative to this directory"),
1075 OPT_END()
1077 const char * const timechart_usage[] = {
1078 "perf timechart [<options>] {record}",
1079 NULL
1082 argc = parse_options(argc, argv, options, timechart_usage,
1083 PARSE_OPT_STOP_AT_NON_OPTION);
1085 symbol__init();
1087 if (argc && !strncmp(argv[0], "rec", 3))
1088 return __cmd_record(argc, argv);
1089 else if (argc)
1090 usage_with_options(timechart_usage, options);
1092 setup_pager();
1094 return __cmd_timechart(output_name);