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/string.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"
36 static char const *input_name
= "perf.data";
37 static char const *output_name
= "output.svg";
39 static unsigned int numcpus
;
40 static u64 min_freq
; /* Lowest CPU frequency seen */
41 static u64 max_freq
; /* Highest CPU frequency seen */
42 static u64 turbo_frequency
;
44 static u64 first_time
, last_time
;
46 static int power_only
;
56 struct sample_wrapper
;
59 * Datastructure layout:
60 * We keep an list of "pid"s, matching the kernels notion of a task struct.
61 * Each "pid" entry, has a list of "comm"s.
62 * this is because we want to track different programs different, while
63 * exec will reuse the original pid (by design).
64 * Each comm has a list of samples that will be used to draw
79 struct per_pidcomm
*all
;
80 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 sample_wrapper
*all_samples
;
152 struct process_filter
;
153 struct process_filter
{
156 struct process_filter
*next
;
159 static struct process_filter
*process_filter
;
162 static struct per_pid
*find_create_pid(int pid
)
164 struct per_pid
*cursor
= all_data
;
167 if (cursor
->pid
== pid
)
169 cursor
= cursor
->next
;
171 cursor
= malloc(sizeof(struct per_pid
));
172 assert(cursor
!= NULL
);
173 memset(cursor
, 0, sizeof(struct per_pid
));
175 cursor
->next
= all_data
;
180 static void pid_set_comm(int pid
, char *comm
)
183 struct per_pidcomm
*c
;
184 p
= find_create_pid(pid
);
187 if (c
->comm
&& strcmp(c
->comm
, comm
) == 0) {
192 c
->comm
= strdup(comm
);
198 c
= malloc(sizeof(struct per_pidcomm
));
200 memset(c
, 0, sizeof(struct per_pidcomm
));
201 c
->comm
= strdup(comm
);
207 static void pid_fork(int pid
, int ppid
, u64 timestamp
)
209 struct per_pid
*p
, *pp
;
210 p
= find_create_pid(pid
);
211 pp
= find_create_pid(ppid
);
213 if (pp
->current
&& pp
->current
->comm
&& !p
->current
)
214 pid_set_comm(pid
, pp
->current
->comm
);
216 p
->start_time
= timestamp
;
218 p
->current
->start_time
= timestamp
;
219 p
->current
->state_since
= timestamp
;
223 static void pid_exit(int pid
, u64 timestamp
)
226 p
= find_create_pid(pid
);
227 p
->end_time
= timestamp
;
229 p
->current
->end_time
= timestamp
;
233 pid_put_sample(int pid
, int type
, unsigned int cpu
, u64 start
, u64 end
)
236 struct per_pidcomm
*c
;
237 struct cpu_sample
*sample
;
239 p
= find_create_pid(pid
);
242 c
= malloc(sizeof(struct per_pidcomm
));
244 memset(c
, 0, sizeof(struct per_pidcomm
));
250 sample
= malloc(sizeof(struct cpu_sample
));
251 assert(sample
!= NULL
);
252 memset(sample
, 0, sizeof(struct cpu_sample
));
253 sample
->start_time
= start
;
254 sample
->end_time
= end
;
256 sample
->next
= c
->samples
;
260 if (sample
->type
== TYPE_RUNNING
&& end
> start
&& start
> 0) {
261 c
->total_time
+= (end
-start
);
262 p
->total_time
+= (end
-start
);
265 if (c
->start_time
== 0 || c
->start_time
> start
)
266 c
->start_time
= start
;
267 if (p
->start_time
== 0 || p
->start_time
> start
)
268 p
->start_time
= start
;
274 #define MAX_CPUS 4096
276 static u64 cpus_cstate_start_times
[MAX_CPUS
];
277 static int cpus_cstate_state
[MAX_CPUS
];
278 static u64 cpus_pstate_start_times
[MAX_CPUS
];
279 static u64 cpus_pstate_state
[MAX_CPUS
];
281 static int process_comm_event(event_t
*event
, struct perf_session
*session __used
)
283 pid_set_comm(event
->comm
.tid
, event
->comm
.comm
);
287 static int process_fork_event(event_t
*event
, struct perf_session
*session __used
)
289 pid_fork(event
->fork
.pid
, event
->fork
.ppid
, event
->fork
.time
);
293 static int process_exit_event(event_t
*event
, struct perf_session
*session __used
)
295 pid_exit(event
->fork
.pid
, event
->fork
.time
);
302 unsigned char preempt_count
;
308 struct trace_entry te
;
313 #define TASK_COMM_LEN 16
314 struct wakeup_entry
{
315 struct trace_entry te
;
316 char comm
[TASK_COMM_LEN
];
323 * trace_flag_type is an enumeration that holds different
324 * states when a trace occurs. These are:
325 * IRQS_OFF - interrupts were disabled
326 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
327 * NEED_RESCED - reschedule is requested
328 * HARDIRQ - inside an interrupt handler
329 * SOFTIRQ - inside a softirq handler
331 enum trace_flag_type
{
332 TRACE_FLAG_IRQS_OFF
= 0x01,
333 TRACE_FLAG_IRQS_NOSUPPORT
= 0x02,
334 TRACE_FLAG_NEED_RESCHED
= 0x04,
335 TRACE_FLAG_HARDIRQ
= 0x08,
336 TRACE_FLAG_SOFTIRQ
= 0x10,
341 struct sched_switch
{
342 struct trace_entry te
;
343 char prev_comm
[TASK_COMM_LEN
];
346 long prev_state
; /* Arjan weeps. */
347 char next_comm
[TASK_COMM_LEN
];
352 static void c_state_start(int cpu
, u64 timestamp
, int state
)
354 cpus_cstate_start_times
[cpu
] = timestamp
;
355 cpus_cstate_state
[cpu
] = state
;
358 static void c_state_end(int cpu
, u64 timestamp
)
360 struct power_event
*pwr
;
361 pwr
= malloc(sizeof(struct power_event
));
364 memset(pwr
, 0, sizeof(struct power_event
));
366 pwr
->state
= cpus_cstate_state
[cpu
];
367 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
368 pwr
->end_time
= timestamp
;
371 pwr
->next
= power_events
;
376 static void p_state_change(int cpu
, u64 timestamp
, u64 new_freq
)
378 struct power_event
*pwr
;
379 pwr
= malloc(sizeof(struct power_event
));
381 if (new_freq
> 8000000) /* detect invalid data */
386 memset(pwr
, 0, sizeof(struct power_event
));
388 pwr
->state
= cpus_pstate_state
[cpu
];
389 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
390 pwr
->end_time
= timestamp
;
393 pwr
->next
= power_events
;
395 if (!pwr
->start_time
)
396 pwr
->start_time
= first_time
;
400 cpus_pstate_state
[cpu
] = new_freq
;
401 cpus_pstate_start_times
[cpu
] = timestamp
;
403 if ((u64
)new_freq
> max_freq
)
406 if (new_freq
< min_freq
|| min_freq
== 0)
409 if (new_freq
== max_freq
- 1000)
410 turbo_frequency
= max_freq
;
414 sched_wakeup(int cpu
, u64 timestamp
, int pid
, struct trace_entry
*te
)
416 struct wake_event
*we
;
418 struct wakeup_entry
*wake
= (void *)te
;
420 we
= malloc(sizeof(struct wake_event
));
424 memset(we
, 0, sizeof(struct wake_event
));
425 we
->time
= timestamp
;
428 if ((te
->flags
& TRACE_FLAG_HARDIRQ
) || (te
->flags
& TRACE_FLAG_SOFTIRQ
))
431 we
->wakee
= wake
->pid
;
432 we
->next
= wake_events
;
434 p
= find_create_pid(we
->wakee
);
436 if (p
&& p
->current
&& p
->current
->state
== TYPE_NONE
) {
437 p
->current
->state_since
= timestamp
;
438 p
->current
->state
= TYPE_WAITING
;
440 if (p
&& p
->current
&& p
->current
->state
== TYPE_BLOCKED
) {
441 pid_put_sample(p
->pid
, p
->current
->state
, cpu
, p
->current
->state_since
, timestamp
);
442 p
->current
->state_since
= timestamp
;
443 p
->current
->state
= TYPE_WAITING
;
447 static void sched_switch(int cpu
, u64 timestamp
, struct trace_entry
*te
)
449 struct per_pid
*p
= NULL
, *prev_p
;
450 struct sched_switch
*sw
= (void *)te
;
453 prev_p
= find_create_pid(sw
->prev_pid
);
455 p
= find_create_pid(sw
->next_pid
);
457 if (prev_p
->current
&& prev_p
->current
->state
!= TYPE_NONE
)
458 pid_put_sample(sw
->prev_pid
, TYPE_RUNNING
, cpu
, prev_p
->current
->state_since
, timestamp
);
459 if (p
&& p
->current
) {
460 if (p
->current
->state
!= TYPE_NONE
)
461 pid_put_sample(sw
->next_pid
, p
->current
->state
, cpu
, p
->current
->state_since
, timestamp
);
463 p
->current
->state_since
= timestamp
;
464 p
->current
->state
= TYPE_RUNNING
;
467 if (prev_p
->current
) {
468 prev_p
->current
->state
= TYPE_NONE
;
469 prev_p
->current
->state_since
= timestamp
;
470 if (sw
->prev_state
& 2)
471 prev_p
->current
->state
= TYPE_BLOCKED
;
472 if (sw
->prev_state
== 0)
473 prev_p
->current
->state
= TYPE_WAITING
;
478 static int process_sample_event(event_t
*event
, struct perf_session
*session
)
480 struct sample_data data
;
481 struct trace_entry
*te
;
483 memset(&data
, 0, sizeof(data
));
485 event__parse_sample(event
, session
->sample_type
, &data
);
487 if (session
->sample_type
& PERF_SAMPLE_TIME
) {
488 if (!first_time
|| first_time
> data
.time
)
489 first_time
= data
.time
;
490 if (last_time
< data
.time
)
491 last_time
= data
.time
;
494 te
= (void *)data
.raw_data
;
495 if (session
->sample_type
& PERF_SAMPLE_RAW
&& data
.raw_size
> 0) {
497 struct power_entry
*pe
;
501 event_str
= perf_header__find_event(te
->type
);
506 if (strcmp(event_str
, "power:power_start") == 0)
507 c_state_start(data
.cpu
, data
.time
, pe
->value
);
509 if (strcmp(event_str
, "power:power_end") == 0)
510 c_state_end(data
.cpu
, data
.time
);
512 if (strcmp(event_str
, "power:power_frequency") == 0)
513 p_state_change(data
.cpu
, data
.time
, pe
->value
);
515 if (strcmp(event_str
, "sched:sched_wakeup") == 0)
516 sched_wakeup(data
.cpu
, data
.time
, data
.pid
, te
);
518 if (strcmp(event_str
, "sched:sched_switch") == 0)
519 sched_switch(data
.cpu
, data
.time
, te
);
525 * After the last sample we need to wrap up the current C/P state
526 * and close out each CPU for these.
528 static void end_sample_processing(void)
531 struct power_event
*pwr
;
533 for (cpu
= 0; cpu
<= numcpus
; cpu
++) {
534 pwr
= malloc(sizeof(struct power_event
));
537 memset(pwr
, 0, sizeof(struct power_event
));
541 pwr
->state
= cpus_cstate_state
[cpu
];
542 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
543 pwr
->end_time
= last_time
;
546 pwr
->next
= power_events
;
552 pwr
= malloc(sizeof(struct power_event
));
555 memset(pwr
, 0, sizeof(struct power_event
));
557 pwr
->state
= cpus_pstate_state
[cpu
];
558 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
559 pwr
->end_time
= last_time
;
562 pwr
->next
= power_events
;
564 if (!pwr
->start_time
)
565 pwr
->start_time
= first_time
;
567 pwr
->state
= min_freq
;
572 static u64
sample_time(event_t
*event
, const struct perf_session
*session
)
577 if (session
->sample_type
& PERF_SAMPLE_IP
)
579 if (session
->sample_type
& PERF_SAMPLE_TID
)
581 if (session
->sample_type
& PERF_SAMPLE_TIME
)
582 return event
->sample
.array
[cursor
];
588 * We first queue all events, sorted backwards by insertion.
589 * The order will get flipped later.
591 static int queue_sample_event(event_t
*event
, struct perf_session
*session
)
593 struct sample_wrapper
*copy
, *prev
;
596 size
= event
->sample
.header
.size
+ sizeof(struct sample_wrapper
) + 8;
602 memset(copy
, 0, size
);
605 copy
->timestamp
= sample_time(event
, session
);
607 memcpy(©
->data
, event
, event
->sample
.header
.size
);
609 /* insert in the right place in the list */
612 /* first sample ever */
617 if (all_samples
->timestamp
< copy
->timestamp
) {
618 /* insert at the head of the list */
619 copy
->next
= all_samples
;
626 if (prev
->next
->timestamp
< copy
->timestamp
) {
627 copy
->next
= prev
->next
;
633 /* insert at the end of the list */
639 static void sort_queued_samples(void)
641 struct sample_wrapper
*cursor
, *next
;
643 cursor
= all_samples
;
648 cursor
->next
= all_samples
;
649 all_samples
= cursor
;
655 * Sort the pid datastructure
657 static void sort_pids(void)
659 struct per_pid
*new_list
, *p
, *cursor
, *prev
;
660 /* sort by ppid first, then by pid, lowest to highest */
669 if (new_list
== NULL
) {
677 if (cursor
->ppid
> p
->ppid
||
678 (cursor
->ppid
== p
->ppid
&& cursor
->pid
> p
->pid
)) {
679 /* must insert before */
681 p
->next
= prev
->next
;
694 cursor
= cursor
->next
;
703 static void draw_c_p_states(void)
705 struct power_event
*pwr
;
709 * two pass drawing so that the P state bars are on top of the C state blocks
712 if (pwr
->type
== CSTATE
)
713 svg_cstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
719 if (pwr
->type
== PSTATE
) {
721 pwr
->state
= min_freq
;
722 svg_pstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
728 static void draw_wakeups(void)
730 struct wake_event
*we
;
732 struct per_pidcomm
*c
;
736 int from
= 0, to
= 0;
737 char *task_from
= NULL
, *task_to
= NULL
;
739 /* locate the column of the waker and wakee */
742 if (p
->pid
== we
->waker
|| p
->pid
== we
->wakee
) {
745 if (c
->Y
&& c
->start_time
<= we
->time
&& c
->end_time
>= we
->time
) {
746 if (p
->pid
== we
->waker
&& !from
) {
748 task_from
= strdup(c
->comm
);
750 if (p
->pid
== we
->wakee
&& !to
) {
752 task_to
= strdup(c
->comm
);
759 if (p
->pid
== we
->waker
&& !from
) {
761 task_from
= strdup(c
->comm
);
763 if (p
->pid
== we
->wakee
&& !to
) {
765 task_to
= strdup(c
->comm
);
774 task_from
= malloc(40);
775 sprintf(task_from
, "[%i]", we
->waker
);
778 task_to
= malloc(40);
779 sprintf(task_to
, "[%i]", we
->wakee
);
783 svg_interrupt(we
->time
, to
);
784 else if (from
&& to
&& abs(from
- to
) == 1)
785 svg_wakeline(we
->time
, from
, to
);
787 svg_partial_wakeline(we
->time
, from
, task_from
, to
, task_to
);
795 static void draw_cpu_usage(void)
798 struct per_pidcomm
*c
;
799 struct cpu_sample
*sample
;
806 if (sample
->type
== TYPE_RUNNING
)
807 svg_process(sample
->cpu
, sample
->start_time
, sample
->end_time
, "sample", c
->comm
);
809 sample
= sample
->next
;
817 static void draw_process_bars(void)
820 struct per_pidcomm
*c
;
821 struct cpu_sample
*sample
;
836 svg_box(Y
, c
->start_time
, c
->end_time
, "process");
839 if (sample
->type
== TYPE_RUNNING
)
840 svg_sample(Y
, sample
->cpu
, sample
->start_time
, sample
->end_time
);
841 if (sample
->type
== TYPE_BLOCKED
)
842 svg_box(Y
, sample
->start_time
, sample
->end_time
, "blocked");
843 if (sample
->type
== TYPE_WAITING
)
844 svg_waiting(Y
, sample
->start_time
, sample
->end_time
);
845 sample
= sample
->next
;
850 if (c
->total_time
> 5000000000) /* 5 seconds */
851 sprintf(comm
, "%s:%i (%2.2fs)", c
->comm
, p
->pid
, c
->total_time
/ 1000000000.0);
853 sprintf(comm
, "%s:%i (%3.1fms)", c
->comm
, p
->pid
, c
->total_time
/ 1000000.0);
855 svg_text(Y
, c
->start_time
, comm
);
865 static void add_process_filter(const char *string
)
867 struct process_filter
*filt
;
870 pid
= strtoull(string
, NULL
, 10);
871 filt
= malloc(sizeof(struct process_filter
));
875 filt
->name
= strdup(string
);
877 filt
->next
= process_filter
;
879 process_filter
= filt
;
882 static int passes_filter(struct per_pid
*p
, struct per_pidcomm
*c
)
884 struct process_filter
*filt
;
888 filt
= process_filter
;
890 if (filt
->pid
&& p
->pid
== filt
->pid
)
892 if (strcmp(filt
->name
, c
->comm
) == 0)
899 static int determine_display_tasks_filtered(void)
902 struct per_pidcomm
*c
;
908 if (p
->start_time
== 1)
909 p
->start_time
= first_time
;
911 /* no exit marker, task kept running to the end */
912 if (p
->end_time
== 0)
913 p
->end_time
= last_time
;
920 if (c
->start_time
== 1)
921 c
->start_time
= first_time
;
923 if (passes_filter(p
, c
)) {
929 if (c
->end_time
== 0)
930 c
->end_time
= last_time
;
939 static int determine_display_tasks(u64 threshold
)
942 struct per_pidcomm
*c
;
946 return determine_display_tasks_filtered();
951 if (p
->start_time
== 1)
952 p
->start_time
= first_time
;
954 /* no exit marker, task kept running to the end */
955 if (p
->end_time
== 0)
956 p
->end_time
= last_time
;
957 if (p
->total_time
>= threshold
&& !power_only
)
965 if (c
->start_time
== 1)
966 c
->start_time
= first_time
;
968 if (c
->total_time
>= threshold
&& !power_only
) {
973 if (c
->end_time
== 0)
974 c
->end_time
= last_time
;
985 #define TIME_THRESH 10000000
987 static void write_svg_file(const char *filename
)
995 count
= determine_display_tasks(TIME_THRESH
);
997 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
999 count
= determine_display_tasks(TIME_THRESH
/ 10);
1001 open_svg(filename
, numcpus
, count
, first_time
, last_time
);
1006 for (i
= 0; i
< numcpus
; i
++)
1007 svg_cpu_box(i
, max_freq
, turbo_frequency
);
1010 draw_process_bars();
1017 static void process_samples(struct perf_session
*session
)
1019 struct sample_wrapper
*cursor
;
1022 sort_queued_samples();
1024 cursor
= all_samples
;
1026 event
= (void *)&cursor
->data
;
1027 cursor
= cursor
->next
;
1028 process_sample_event(event
, session
);
1032 static int sample_type_check(struct perf_session
*session
)
1034 if (!(session
->sample_type
& PERF_SAMPLE_RAW
)) {
1035 fprintf(stderr
, "No trace samples found in the file.\n"
1036 "Have you used 'perf timechart record' to record it?\n");
1043 static struct perf_event_ops event_ops
= {
1044 .process_comm_event
= process_comm_event
,
1045 .process_fork_event
= process_fork_event
,
1046 .process_exit_event
= process_exit_event
,
1047 .process_sample_event
= queue_sample_event
,
1048 .sample_type_check
= sample_type_check
,
1051 static int __cmd_timechart(void)
1053 struct perf_session
*session
= perf_session__new(input_name
, O_RDONLY
, 0);
1056 if (session
== NULL
)
1059 ret
= perf_session__process_events(session
, &event_ops
);
1063 process_samples(session
);
1065 end_sample_processing();
1069 write_svg_file(output_name
);
1071 pr_info("Written %2.1f seconds of trace to %s.\n",
1072 (last_time
- first_time
) / 1000000000.0, output_name
);
1074 perf_session__delete(session
);
1078 static const char * const timechart_usage
[] = {
1079 "perf timechart [<options>] {record}",
1083 static const char *record_args
[] = {
1090 "-e", "power:power_start",
1091 "-e", "power:power_end",
1092 "-e", "power:power_frequency",
1093 "-e", "sched:sched_wakeup",
1094 "-e", "sched:sched_switch",
1097 static int __cmd_record(int argc
, const char **argv
)
1099 unsigned int rec_argc
, i
, j
;
1100 const char **rec_argv
;
1102 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
1103 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1105 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
1106 rec_argv
[i
] = strdup(record_args
[i
]);
1108 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
1109 rec_argv
[i
] = argv
[j
];
1111 return cmd_record(i
, rec_argv
, NULL
);
1115 parse_process(const struct option
*opt __used
, const char *arg
, int __used unset
)
1118 add_process_filter(arg
);
1122 static const struct option options
[] = {
1123 OPT_STRING('i', "input", &input_name
, "file",
1125 OPT_STRING('o', "output", &output_name
, "file",
1126 "output file name"),
1127 OPT_INTEGER('w', "width", &svg_page_width
,
1129 OPT_BOOLEAN('P', "power-only", &power_only
,
1130 "output power data only"),
1131 OPT_CALLBACK('p', "process", NULL
, "process",
1132 "process selector. Pass a pid or process name.",
1138 int cmd_timechart(int argc
, const char **argv
, const char *prefix __used
)
1140 argc
= parse_options(argc
, argv
, options
, timechart_usage
,
1141 PARSE_OPT_STOP_AT_NON_OPTION
);
1145 if (argc
&& !strncmp(argv
[0], "rec", 3))
1146 return __cmd_record(argc
, argv
);
1148 usage_with_options(timechart_usage
, options
);
1152 return __cmd_timechart();