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 <linux/rbtree.h>
23 #include "util/symbol.h"
24 #include "util/callchain.h"
25 #include "util/strlist.h"
28 #include "util/header.h"
29 #include "util/parse-options.h"
30 #include "util/parse-events.h"
31 #include "util/event.h"
32 #include "util/session.h"
33 #include "util/svghelper.h"
35 static char const *input_name
= "perf.data";
36 static char const *output_name
= "output.svg";
38 static unsigned int numcpus
;
39 static u64 min_freq
; /* Lowest CPU frequency seen */
40 static u64 max_freq
; /* Highest CPU frequency seen */
41 static u64 turbo_frequency
;
43 static u64 first_time
, last_time
;
45 static bool power_only
;
55 struct sample_wrapper
;
58 * Datastructure layout:
59 * We keep an list of "pid"s, matching the kernels notion of a task struct.
60 * Each "pid" entry, has a list of "comm"s.
61 * this is because we want to track different programs different, while
62 * exec will reuse the original pid (by design).
63 * Each comm has a list of samples that will be used to draw
78 struct per_pidcomm
*all
;
79 struct per_pidcomm
*current
;
84 struct per_pidcomm
*next
;
98 struct cpu_sample
*samples
;
101 struct sample_wrapper
{
102 struct sample_wrapper
*next
;
105 unsigned char data
[0];
109 #define TYPE_RUNNING 1
110 #define TYPE_WAITING 2
111 #define TYPE_BLOCKED 3
114 struct cpu_sample
*next
;
122 static struct per_pid
*all_data
;
128 struct power_event
*next
;
137 struct wake_event
*next
;
143 static struct power_event
*power_events
;
144 static struct wake_event
*wake_events
;
146 struct process_filter
;
147 struct process_filter
{
150 struct process_filter
*next
;
153 static struct process_filter
*process_filter
;
156 static struct per_pid
*find_create_pid(int pid
)
158 struct per_pid
*cursor
= all_data
;
161 if (cursor
->pid
== pid
)
163 cursor
= cursor
->next
;
165 cursor
= malloc(sizeof(struct per_pid
));
166 assert(cursor
!= NULL
);
167 memset(cursor
, 0, sizeof(struct per_pid
));
169 cursor
->next
= all_data
;
174 static void pid_set_comm(int pid
, char *comm
)
177 struct per_pidcomm
*c
;
178 p
= find_create_pid(pid
);
181 if (c
->comm
&& strcmp(c
->comm
, comm
) == 0) {
186 c
->comm
= strdup(comm
);
192 c
= malloc(sizeof(struct per_pidcomm
));
194 memset(c
, 0, sizeof(struct per_pidcomm
));
195 c
->comm
= strdup(comm
);
201 static void pid_fork(int pid
, int ppid
, u64 timestamp
)
203 struct per_pid
*p
, *pp
;
204 p
= find_create_pid(pid
);
205 pp
= find_create_pid(ppid
);
207 if (pp
->current
&& pp
->current
->comm
&& !p
->current
)
208 pid_set_comm(pid
, pp
->current
->comm
);
210 p
->start_time
= timestamp
;
212 p
->current
->start_time
= timestamp
;
213 p
->current
->state_since
= timestamp
;
217 static void pid_exit(int pid
, u64 timestamp
)
220 p
= find_create_pid(pid
);
221 p
->end_time
= timestamp
;
223 p
->current
->end_time
= timestamp
;
227 pid_put_sample(int pid
, int type
, unsigned int cpu
, u64 start
, u64 end
)
230 struct per_pidcomm
*c
;
231 struct cpu_sample
*sample
;
233 p
= find_create_pid(pid
);
236 c
= malloc(sizeof(struct per_pidcomm
));
238 memset(c
, 0, sizeof(struct per_pidcomm
));
244 sample
= malloc(sizeof(struct cpu_sample
));
245 assert(sample
!= NULL
);
246 memset(sample
, 0, sizeof(struct cpu_sample
));
247 sample
->start_time
= start
;
248 sample
->end_time
= end
;
250 sample
->next
= c
->samples
;
254 if (sample
->type
== TYPE_RUNNING
&& end
> start
&& start
> 0) {
255 c
->total_time
+= (end
-start
);
256 p
->total_time
+= (end
-start
);
259 if (c
->start_time
== 0 || c
->start_time
> start
)
260 c
->start_time
= start
;
261 if (p
->start_time
== 0 || p
->start_time
> start
)
262 p
->start_time
= start
;
268 #define MAX_CPUS 4096
270 static u64 cpus_cstate_start_times
[MAX_CPUS
];
271 static int cpus_cstate_state
[MAX_CPUS
];
272 static u64 cpus_pstate_start_times
[MAX_CPUS
];
273 static u64 cpus_pstate_state
[MAX_CPUS
];
275 static int process_comm_event(event_t
*event
, struct sample_data
*sample __used
,
276 struct perf_session
*session __used
)
278 pid_set_comm(event
->comm
.tid
, event
->comm
.comm
);
282 static int process_fork_event(event_t
*event
, struct sample_data
*sample __used
,
283 struct perf_session
*session __used
)
285 pid_fork(event
->fork
.pid
, event
->fork
.ppid
, event
->fork
.time
);
289 static int process_exit_event(event_t
*event
, struct sample_data
*sample __used
,
290 struct perf_session
*session __used
)
292 pid_exit(event
->fork
.pid
, event
->fork
.time
);
299 unsigned char preempt_count
;
305 struct trace_entry te
;
311 #define TASK_COMM_LEN 16
312 struct wakeup_entry
{
313 struct trace_entry te
;
314 char comm
[TASK_COMM_LEN
];
321 * trace_flag_type is an enumeration that holds different
322 * states when a trace occurs. These are:
323 * IRQS_OFF - interrupts were disabled
324 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
325 * NEED_RESCED - reschedule is requested
326 * HARDIRQ - inside an interrupt handler
327 * SOFTIRQ - inside a softirq handler
329 enum trace_flag_type
{
330 TRACE_FLAG_IRQS_OFF
= 0x01,
331 TRACE_FLAG_IRQS_NOSUPPORT
= 0x02,
332 TRACE_FLAG_NEED_RESCHED
= 0x04,
333 TRACE_FLAG_HARDIRQ
= 0x08,
334 TRACE_FLAG_SOFTIRQ
= 0x10,
339 struct sched_switch
{
340 struct trace_entry te
;
341 char prev_comm
[TASK_COMM_LEN
];
344 long prev_state
; /* Arjan weeps. */
345 char next_comm
[TASK_COMM_LEN
];
350 static void c_state_start(int cpu
, u64 timestamp
, int state
)
352 cpus_cstate_start_times
[cpu
] = timestamp
;
353 cpus_cstate_state
[cpu
] = state
;
356 static void c_state_end(int cpu
, u64 timestamp
)
358 struct power_event
*pwr
;
359 pwr
= malloc(sizeof(struct power_event
));
362 memset(pwr
, 0, sizeof(struct power_event
));
364 pwr
->state
= cpus_cstate_state
[cpu
];
365 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
366 pwr
->end_time
= timestamp
;
369 pwr
->next
= power_events
;
374 static void p_state_change(int cpu
, u64 timestamp
, u64 new_freq
)
376 struct power_event
*pwr
;
377 pwr
= malloc(sizeof(struct power_event
));
379 if (new_freq
> 8000000) /* detect invalid data */
384 memset(pwr
, 0, sizeof(struct power_event
));
386 pwr
->state
= cpus_pstate_state
[cpu
];
387 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
388 pwr
->end_time
= timestamp
;
391 pwr
->next
= power_events
;
393 if (!pwr
->start_time
)
394 pwr
->start_time
= first_time
;
398 cpus_pstate_state
[cpu
] = new_freq
;
399 cpus_pstate_start_times
[cpu
] = timestamp
;
401 if ((u64
)new_freq
> max_freq
)
404 if (new_freq
< min_freq
|| min_freq
== 0)
407 if (new_freq
== max_freq
- 1000)
408 turbo_frequency
= max_freq
;
412 sched_wakeup(int cpu
, u64 timestamp
, int pid
, struct trace_entry
*te
)
414 struct wake_event
*we
;
416 struct wakeup_entry
*wake
= (void *)te
;
418 we
= malloc(sizeof(struct wake_event
));
422 memset(we
, 0, sizeof(struct wake_event
));
423 we
->time
= timestamp
;
426 if ((te
->flags
& TRACE_FLAG_HARDIRQ
) || (te
->flags
& TRACE_FLAG_SOFTIRQ
))
429 we
->wakee
= wake
->pid
;
430 we
->next
= wake_events
;
432 p
= find_create_pid(we
->wakee
);
434 if (p
&& p
->current
&& p
->current
->state
== TYPE_NONE
) {
435 p
->current
->state_since
= timestamp
;
436 p
->current
->state
= TYPE_WAITING
;
438 if (p
&& p
->current
&& p
->current
->state
== TYPE_BLOCKED
) {
439 pid_put_sample(p
->pid
, p
->current
->state
, cpu
, p
->current
->state_since
, timestamp
);
440 p
->current
->state_since
= timestamp
;
441 p
->current
->state
= TYPE_WAITING
;
445 static void sched_switch(int cpu
, u64 timestamp
, struct trace_entry
*te
)
447 struct per_pid
*p
= NULL
, *prev_p
;
448 struct sched_switch
*sw
= (void *)te
;
451 prev_p
= find_create_pid(sw
->prev_pid
);
453 p
= find_create_pid(sw
->next_pid
);
455 if (prev_p
->current
&& prev_p
->current
->state
!= TYPE_NONE
)
456 pid_put_sample(sw
->prev_pid
, TYPE_RUNNING
, cpu
, prev_p
->current
->state_since
, timestamp
);
457 if (p
&& p
->current
) {
458 if (p
->current
->state
!= TYPE_NONE
)
459 pid_put_sample(sw
->next_pid
, p
->current
->state
, cpu
, p
->current
->state_since
, timestamp
);
461 p
->current
->state_since
= timestamp
;
462 p
->current
->state
= TYPE_RUNNING
;
465 if (prev_p
->current
) {
466 prev_p
->current
->state
= TYPE_NONE
;
467 prev_p
->current
->state_since
= timestamp
;
468 if (sw
->prev_state
& 2)
469 prev_p
->current
->state
= TYPE_BLOCKED
;
470 if (sw
->prev_state
== 0)
471 prev_p
->current
->state
= TYPE_WAITING
;
476 static int process_sample_event(event_t
*event __used
,
477 struct sample_data
*sample
,
478 struct perf_session
*session
)
480 struct trace_entry
*te
;
482 if (session
->sample_type
& PERF_SAMPLE_TIME
) {
483 if (!first_time
|| first_time
> sample
->time
)
484 first_time
= sample
->time
;
485 if (last_time
< sample
->time
)
486 last_time
= sample
->time
;
489 te
= (void *)sample
->raw_data
;
490 if (session
->sample_type
& PERF_SAMPLE_RAW
&& sample
->raw_size
> 0) {
492 struct power_entry
*pe
;
496 event_str
= perf_header__find_event(te
->type
);
501 if (strcmp(event_str
, "power:power_start") == 0)
502 c_state_start(pe
->cpu_id
, sample
->time
, pe
->value
);
504 if (strcmp(event_str
, "power:power_end") == 0)
505 c_state_end(pe
->cpu_id
, sample
->time
);
507 if (strcmp(event_str
, "power:power_frequency") == 0)
508 p_state_change(pe
->cpu_id
, sample
->time
, pe
->value
);
510 if (strcmp(event_str
, "sched:sched_wakeup") == 0)
511 sched_wakeup(sample
->cpu
, sample
->time
, sample
->pid
, te
);
513 if (strcmp(event_str
, "sched:sched_switch") == 0)
514 sched_switch(sample
->cpu
, sample
->time
, te
);
520 * After the last sample we need to wrap up the current C/P state
521 * and close out each CPU for these.
523 static void end_sample_processing(void)
526 struct power_event
*pwr
;
528 for (cpu
= 0; cpu
<= numcpus
; cpu
++) {
529 pwr
= malloc(sizeof(struct power_event
));
532 memset(pwr
, 0, sizeof(struct power_event
));
536 pwr
->state
= cpus_cstate_state
[cpu
];
537 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
538 pwr
->end_time
= last_time
;
541 pwr
->next
= power_events
;
547 pwr
= malloc(sizeof(struct power_event
));
550 memset(pwr
, 0, sizeof(struct power_event
));
552 pwr
->state
= cpus_pstate_state
[cpu
];
553 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
554 pwr
->end_time
= last_time
;
557 pwr
->next
= power_events
;
559 if (!pwr
->start_time
)
560 pwr
->start_time
= first_time
;
562 pwr
->state
= min_freq
;
568 * Sort the pid datastructure
570 static void sort_pids(void)
572 struct per_pid
*new_list
, *p
, *cursor
, *prev
;
573 /* sort by ppid first, then by pid, lowest to highest */
582 if (new_list
== NULL
) {
590 if (cursor
->ppid
> p
->ppid
||
591 (cursor
->ppid
== p
->ppid
&& cursor
->pid
> p
->pid
)) {
592 /* must insert before */
594 p
->next
= prev
->next
;
607 cursor
= cursor
->next
;
616 static void draw_c_p_states(void)
618 struct power_event
*pwr
;
622 * two pass drawing so that the P state bars are on top of the C state blocks
625 if (pwr
->type
== CSTATE
)
626 svg_cstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
632 if (pwr
->type
== PSTATE
) {
634 pwr
->state
= min_freq
;
635 svg_pstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
641 static void draw_wakeups(void)
643 struct wake_event
*we
;
645 struct per_pidcomm
*c
;
649 int from
= 0, to
= 0;
650 char *task_from
= NULL
, *task_to
= NULL
;
652 /* locate the column of the waker and wakee */
655 if (p
->pid
== we
->waker
|| p
->pid
== we
->wakee
) {
658 if (c
->Y
&& c
->start_time
<= we
->time
&& c
->end_time
>= we
->time
) {
659 if (p
->pid
== we
->waker
&& !from
) {
661 task_from
= strdup(c
->comm
);
663 if (p
->pid
== we
->wakee
&& !to
) {
665 task_to
= strdup(c
->comm
);
672 if (p
->pid
== we
->waker
&& !from
) {
674 task_from
= strdup(c
->comm
);
676 if (p
->pid
== we
->wakee
&& !to
) {
678 task_to
= strdup(c
->comm
);
687 task_from
= malloc(40);
688 sprintf(task_from
, "[%i]", we
->waker
);
691 task_to
= malloc(40);
692 sprintf(task_to
, "[%i]", we
->wakee
);
696 svg_interrupt(we
->time
, to
);
697 else if (from
&& to
&& abs(from
- to
) == 1)
698 svg_wakeline(we
->time
, from
, to
);
700 svg_partial_wakeline(we
->time
, from
, task_from
, to
, task_to
);
708 static void draw_cpu_usage(void)
711 struct per_pidcomm
*c
;
712 struct cpu_sample
*sample
;
719 if (sample
->type
== TYPE_RUNNING
)
720 svg_process(sample
->cpu
, sample
->start_time
, sample
->end_time
, "sample", c
->comm
);
722 sample
= sample
->next
;
730 static void draw_process_bars(void)
733 struct per_pidcomm
*c
;
734 struct cpu_sample
*sample
;
749 svg_box(Y
, c
->start_time
, c
->end_time
, "process");
752 if (sample
->type
== TYPE_RUNNING
)
753 svg_sample(Y
, sample
->cpu
, sample
->start_time
, sample
->end_time
);
754 if (sample
->type
== TYPE_BLOCKED
)
755 svg_box(Y
, sample
->start_time
, sample
->end_time
, "blocked");
756 if (sample
->type
== TYPE_WAITING
)
757 svg_waiting(Y
, sample
->start_time
, sample
->end_time
);
758 sample
= sample
->next
;
763 if (c
->total_time
> 5000000000) /* 5 seconds */
764 sprintf(comm
, "%s:%i (%2.2fs)", c
->comm
, p
->pid
, c
->total_time
/ 1000000000.0);
766 sprintf(comm
, "%s:%i (%3.1fms)", c
->comm
, p
->pid
, c
->total_time
/ 1000000.0);
768 svg_text(Y
, c
->start_time
, comm
);
778 static void add_process_filter(const char *string
)
780 struct process_filter
*filt
;
783 pid
= strtoull(string
, NULL
, 10);
784 filt
= malloc(sizeof(struct process_filter
));
788 filt
->name
= strdup(string
);
790 filt
->next
= process_filter
;
792 process_filter
= filt
;
795 static int passes_filter(struct per_pid
*p
, struct per_pidcomm
*c
)
797 struct process_filter
*filt
;
801 filt
= process_filter
;
803 if (filt
->pid
&& p
->pid
== filt
->pid
)
805 if (strcmp(filt
->name
, c
->comm
) == 0)
812 static int determine_display_tasks_filtered(void)
815 struct per_pidcomm
*c
;
821 if (p
->start_time
== 1)
822 p
->start_time
= first_time
;
824 /* no exit marker, task kept running to the end */
825 if (p
->end_time
== 0)
826 p
->end_time
= last_time
;
833 if (c
->start_time
== 1)
834 c
->start_time
= first_time
;
836 if (passes_filter(p
, c
)) {
842 if (c
->end_time
== 0)
843 c
->end_time
= last_time
;
852 static int determine_display_tasks(u64 threshold
)
855 struct per_pidcomm
*c
;
859 return determine_display_tasks_filtered();
864 if (p
->start_time
== 1)
865 p
->start_time
= first_time
;
867 /* no exit marker, task kept running to the end */
868 if (p
->end_time
== 0)
869 p
->end_time
= last_time
;
870 if (p
->total_time
>= threshold
&& !power_only
)
878 if (c
->start_time
== 1)
879 c
->start_time
= first_time
;
881 if (c
->total_time
>= threshold
&& !power_only
) {
886 if (c
->end_time
== 0)
887 c
->end_time
= last_time
;
898 #define TIME_THRESH 10000000
900 static void write_svg_file(const char *filename
)
908 count
= determine_display_tasks(TIME_THRESH
);
910 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
912 count
= determine_display_tasks(TIME_THRESH
/ 10);
914 open_svg(filename
, numcpus
, count
, first_time
, last_time
);
919 for (i
= 0; i
< numcpus
; i
++)
920 svg_cpu_box(i
, max_freq
, turbo_frequency
);
930 static struct perf_event_ops event_ops
= {
931 .comm
= process_comm_event
,
932 .fork
= process_fork_event
,
933 .exit
= process_exit_event
,
934 .sample
= process_sample_event
,
935 .ordered_samples
= true,
938 static int __cmd_timechart(void)
940 struct perf_session
*session
= perf_session__new(input_name
, O_RDONLY
,
941 0, false, &event_ops
);
947 if (!perf_session__has_traces(session
, "timechart record"))
950 ret
= perf_session__process_events(session
, &event_ops
);
954 end_sample_processing();
958 write_svg_file(output_name
);
960 pr_info("Written %2.1f seconds of trace to %s.\n",
961 (last_time
- first_time
) / 1000000000.0, output_name
);
963 perf_session__delete(session
);
967 static const char * const timechart_usage
[] = {
968 "perf timechart [<options>] {record}",
972 static const char *record_args
[] = {
978 "-e", "power:power_start",
979 "-e", "power:power_end",
980 "-e", "power:power_frequency",
981 "-e", "sched:sched_wakeup",
982 "-e", "sched:sched_switch",
985 static int __cmd_record(int argc
, const char **argv
)
987 unsigned int rec_argc
, i
, j
;
988 const char **rec_argv
;
990 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
991 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
993 if (rec_argv
== NULL
)
996 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
997 rec_argv
[i
] = strdup(record_args
[i
]);
999 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
1000 rec_argv
[i
] = argv
[j
];
1002 return cmd_record(i
, rec_argv
, NULL
);
1006 parse_process(const struct option
*opt __used
, const char *arg
, int __used unset
)
1009 add_process_filter(arg
);
1013 static const struct option options
[] = {
1014 OPT_STRING('i', "input", &input_name
, "file",
1016 OPT_STRING('o', "output", &output_name
, "file",
1017 "output file name"),
1018 OPT_INTEGER('w', "width", &svg_page_width
,
1020 OPT_BOOLEAN('P', "power-only", &power_only
,
1021 "output power data only"),
1022 OPT_CALLBACK('p', "process", NULL
, "process",
1023 "process selector. Pass a pid or process name.",
1029 int cmd_timechart(int argc
, const char **argv
, const char *prefix __used
)
1031 argc
= parse_options(argc
, argv
, options
, timechart_usage
,
1032 PARSE_OPT_STOP_AT_NON_OPTION
);
1036 if (argc
&& !strncmp(argv
[0], "rec", 3))
1037 return __cmd_record(argc
, argv
);
1039 usage_with_options(timechart_usage
, options
);
1043 return __cmd_timechart();