2 * builtin-timechart.c - make an svg timechart of system activity
4 * (C) Copyright 2009 Intel Corporation
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
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"
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
;
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
81 struct per_pidcomm
*all
;
82 struct per_pidcomm
*current
;
87 struct per_pidcomm
*next
;
101 struct cpu_sample
*samples
;
104 struct sample_wrapper
{
105 struct sample_wrapper
*next
;
108 unsigned char data
[0];
112 #define TYPE_RUNNING 1
113 #define TYPE_WAITING 2
114 #define TYPE_BLOCKED 3
117 struct cpu_sample
*next
;
125 static struct per_pid
*all_data
;
131 struct power_event
*next
;
140 struct wake_event
*next
;
146 static struct power_event
*power_events
;
147 static struct wake_event
*wake_events
;
149 struct process_filter
;
150 struct process_filter
{
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
;
164 if (cursor
->pid
== pid
)
166 cursor
= cursor
->next
;
168 cursor
= zalloc(sizeof(*cursor
));
169 assert(cursor
!= NULL
);
171 cursor
->next
= all_data
;
176 static void pid_set_comm(int pid
, char *comm
)
179 struct per_pidcomm
*c
;
180 p
= find_create_pid(pid
);
183 if (c
->comm
&& strcmp(c
->comm
, comm
) == 0) {
188 c
->comm
= strdup(comm
);
194 c
= zalloc(sizeof(*c
));
196 c
->comm
= strdup(comm
);
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
);
208 if (pp
->current
&& pp
->current
->comm
&& !p
->current
)
209 pid_set_comm(pid
, pp
->current
->comm
);
211 p
->start_time
= timestamp
;
213 p
->current
->start_time
= timestamp
;
214 p
->current
->state_since
= timestamp
;
218 static void pid_exit(int pid
, u64 timestamp
)
221 p
= find_create_pid(pid
);
222 p
->end_time
= timestamp
;
224 p
->current
->end_time
= timestamp
;
228 pid_put_sample(int pid
, int type
, unsigned int cpu
, u64 start
, u64 end
)
231 struct per_pidcomm
*c
;
232 struct cpu_sample
*sample
;
234 p
= find_create_pid(pid
);
237 c
= zalloc(sizeof(*c
));
244 sample
= zalloc(sizeof(*sample
));
245 assert(sample
!= NULL
);
246 sample
->start_time
= start
;
247 sample
->end_time
= end
;
249 sample
->next
= c
->samples
;
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
);
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
);
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
);
301 unsigned char preempt_count
;
306 #ifdef SUPPORT_OLD_POWER_EVENTS
307 static int use_old_power_events
;
308 struct power_entry_old
{
309 struct trace_entry te
;
316 struct power_processor_entry
{
317 struct trace_entry te
;
322 #define TASK_COMM_LEN 16
323 struct wakeup_entry
{
324 struct trace_entry te
;
325 char comm
[TASK_COMM_LEN
];
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
];
355 long prev_state
; /* Arjan weeps. */
356 char next_comm
[TASK_COMM_LEN
];
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
));
374 pwr
->state
= cpus_cstate_state
[cpu
];
375 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
376 pwr
->end_time
= timestamp
;
379 pwr
->next
= power_events
;
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 */
391 pwr
= zalloc(sizeof(*pwr
));
395 pwr
->state
= cpus_pstate_state
[cpu
];
396 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
397 pwr
->end_time
= timestamp
;
400 pwr
->next
= power_events
;
402 if (!pwr
->start_time
)
403 pwr
->start_time
= first_time
;
407 cpus_pstate_state
[cpu
] = new_freq
;
408 cpus_pstate_start_times
[cpu
] = timestamp
;
410 if ((u64
)new_freq
> max_freq
)
413 if (new_freq
< min_freq
|| min_freq
== 0)
416 if (new_freq
== max_freq
- 1000)
417 turbo_frequency
= max_freq
;
421 sched_wakeup(int cpu
, u64 timestamp
, int pid
, struct trace_entry
*te
)
424 struct wakeup_entry
*wake
= (void *)te
;
425 struct wake_event
*we
= zalloc(sizeof(*we
));
430 we
->time
= timestamp
;
433 if ((te
->flags
& TRACE_FLAG_HARDIRQ
) || (te
->flags
& TRACE_FLAG_SOFTIRQ
))
436 we
->wakee
= wake
->pid
;
437 we
->next
= wake_events
;
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) {
501 #ifdef SUPPORT_OLD_POWER_EVENTS
502 struct power_entry_old
*peo
;
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.
513 * if (evsel->attr.config == power_cpu_idle_id)
515 event_str
= perf_header__find_event(te
->type
);
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
);
528 c_state_start(ppe
->cpu_id
, sample
->time
,
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
,
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
,
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)
568 struct power_event
*pwr
;
570 for (cpu
= 0; cpu
<= numcpus
; cpu
++) {
573 pwr
= zalloc(sizeof(*pwr
));
577 pwr
->state
= cpus_cstate_state
[cpu
];
578 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
579 pwr
->end_time
= last_time
;
582 pwr
->next
= power_events
;
588 pwr
= zalloc(sizeof(*pwr
));
592 pwr
->state
= cpus_pstate_state
[cpu
];
593 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
594 pwr
->end_time
= last_time
;
597 pwr
->next
= power_events
;
599 if (!pwr
->start_time
)
600 pwr
->start_time
= first_time
;
602 pwr
->state
= min_freq
;
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 */
622 if (new_list
== NULL
) {
630 if (cursor
->ppid
> p
->ppid
||
631 (cursor
->ppid
== p
->ppid
&& cursor
->pid
> p
->pid
)) {
632 /* must insert before */
634 p
->next
= prev
->next
;
647 cursor
= cursor
->next
;
656 static void draw_c_p_states(void)
658 struct power_event
*pwr
;
662 * two pass drawing so that the P state bars are on top of the C state blocks
665 if (pwr
->type
== CSTATE
)
666 svg_cstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
672 if (pwr
->type
== PSTATE
) {
674 pwr
->state
= min_freq
;
675 svg_pstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
681 static void draw_wakeups(void)
683 struct wake_event
*we
;
685 struct per_pidcomm
*c
;
689 int from
= 0, to
= 0;
690 char *task_from
= NULL
, *task_to
= NULL
;
692 /* locate the column of the waker and wakee */
695 if (p
->pid
== we
->waker
|| p
->pid
== we
->wakee
) {
698 if (c
->Y
&& c
->start_time
<= we
->time
&& c
->end_time
>= we
->time
) {
699 if (p
->pid
== we
->waker
&& !from
) {
701 task_from
= strdup(c
->comm
);
703 if (p
->pid
== we
->wakee
&& !to
) {
705 task_to
= strdup(c
->comm
);
712 if (p
->pid
== we
->waker
&& !from
) {
714 task_from
= strdup(c
->comm
);
716 if (p
->pid
== we
->wakee
&& !to
) {
718 task_to
= strdup(c
->comm
);
727 task_from
= malloc(40);
728 sprintf(task_from
, "[%i]", we
->waker
);
731 task_to
= malloc(40);
732 sprintf(task_to
, "[%i]", we
->wakee
);
736 svg_interrupt(we
->time
, to
);
737 else if (from
&& to
&& abs(from
- to
) == 1)
738 svg_wakeline(we
->time
, from
, to
);
740 svg_partial_wakeline(we
->time
, from
, task_from
, to
, task_to
);
748 static void draw_cpu_usage(void)
751 struct per_pidcomm
*c
;
752 struct cpu_sample
*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
;
770 static void draw_process_bars(void)
773 struct per_pidcomm
*c
;
774 struct cpu_sample
*sample
;
789 svg_box(Y
, c
->start_time
, c
->end_time
, "process");
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
;
803 if (c
->total_time
> 5000000000) /* 5 seconds */
804 sprintf(comm
, "%s:%i (%2.2fs)", c
->comm
, p
->pid
, c
->total_time
/ 1000000000.0);
806 sprintf(comm
, "%s:%i (%3.1fms)", c
->comm
, p
->pid
, c
->total_time
/ 1000000.0);
808 svg_text(Y
, c
->start_time
, comm
);
818 static void add_process_filter(const char *string
)
820 int pid
= strtoull(string
, NULL
, 10);
821 struct process_filter
*filt
= malloc(sizeof(*filt
));
826 filt
->name
= strdup(string
);
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
;
839 filt
= process_filter
;
841 if (filt
->pid
&& p
->pid
== filt
->pid
)
843 if (strcmp(filt
->name
, c
->comm
) == 0)
850 static int determine_display_tasks_filtered(void)
853 struct per_pidcomm
*c
;
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
;
871 if (c
->start_time
== 1)
872 c
->start_time
= first_time
;
874 if (passes_filter(p
, c
)) {
880 if (c
->end_time
== 0)
881 c
->end_time
= last_time
;
890 static int determine_display_tasks(u64 threshold
)
893 struct per_pidcomm
*c
;
897 return determine_display_tasks_filtered();
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
)
916 if (c
->start_time
== 1)
917 c
->start_time
= first_time
;
919 if (c
->total_time
>= threshold
&& !power_only
) {
924 if (c
->end_time
== 0)
925 c
->end_time
= last_time
;
936 #define TIME_THRESH 10000000
938 static void write_svg_file(const char *filename
)
946 count
= determine_display_tasks(TIME_THRESH
);
948 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
950 count
= determine_display_tasks(TIME_THRESH
/ 10);
952 open_svg(filename
, numcpus
, count
, first_time
, last_time
);
957 for (i
= 0; i
< numcpus
; i
++)
958 svg_cpu_box(i
, max_freq
, turbo_frequency
);
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
);
984 if (!perf_session__has_traces(session
, "timechart record"))
987 ret
= perf_session__process_events(session
, &perf_timechart
);
991 end_sample_processing();
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
);
1000 perf_session__delete(session
);
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",
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
);
1037 rec_argc
= record_elems
+ argc
- 1;
1038 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1040 if (rec_argv
== NULL
)
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
);
1053 parse_process(const struct option
*opt __maybe_unused
, const char *arg
,
1054 int __maybe_unused unset
)
1057 add_process_filter(arg
);
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.",
1073 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1074 "Look for files with symbols relative to this directory"),
1077 const char * const timechart_usage
[] = {
1078 "perf timechart [<options>] {record}",
1082 argc
= parse_options(argc
, argv
, options
, timechart_usage
,
1083 PARSE_OPT_STOP_AT_NON_OPTION
);
1087 if (argc
&& !strncmp(argv
[0], "rec", 3))
1088 return __cmd_record(argc
, argv
);
1090 usage_with_options(timechart_usage
, options
);
1094 return __cmd_timechart(output_name
);