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
15 #include <traceevent/event-parse.h>
19 #include "util/util.h"
21 #include "util/color.h"
22 #include <linux/list.h>
23 #include "util/cache.h"
24 #include "util/evlist.h"
25 #include "util/evsel.h"
26 #include <linux/rbtree.h>
27 #include "util/symbol.h"
28 #include "util/callchain.h"
29 #include "util/strlist.h"
32 #include "util/header.h"
33 #include "util/parse-options.h"
34 #include "util/parse-events.h"
35 #include "util/event.h"
36 #include "util/session.h"
37 #include "util/svghelper.h"
38 #include "util/tool.h"
39 #include "util/data.h"
41 #define SUPPORT_OLD_POWER_EVENTS 1
42 #define PWR_EVENT_EXIT -1
45 struct perf_tool tool
;
48 u64 min_freq
, /* Lowest CPU frequency seen */
49 max_freq
, /* Highest CPU frequency seen */
51 first_time
, last_time
;
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
;
123 const char *backtrace
;
126 static struct per_pid
*all_data
;
132 struct power_event
*next
;
141 struct wake_event
*next
;
145 const char *backtrace
;
148 static struct power_event
*power_events
;
149 static struct wake_event
*wake_events
;
151 struct process_filter
{
154 struct process_filter
*next
;
157 static struct process_filter
*process_filter
;
160 static struct per_pid
*find_create_pid(int pid
)
162 struct per_pid
*cursor
= all_data
;
165 if (cursor
->pid
== pid
)
167 cursor
= cursor
->next
;
169 cursor
= zalloc(sizeof(*cursor
));
170 assert(cursor
!= NULL
);
172 cursor
->next
= all_data
;
177 static void pid_set_comm(int pid
, char *comm
)
180 struct per_pidcomm
*c
;
181 p
= find_create_pid(pid
);
184 if (c
->comm
&& strcmp(c
->comm
, comm
) == 0) {
189 c
->comm
= strdup(comm
);
195 c
= zalloc(sizeof(*c
));
197 c
->comm
= strdup(comm
);
203 static void pid_fork(int pid
, int ppid
, u64 timestamp
)
205 struct per_pid
*p
, *pp
;
206 p
= find_create_pid(pid
);
207 pp
= find_create_pid(ppid
);
209 if (pp
->current
&& pp
->current
->comm
&& !p
->current
)
210 pid_set_comm(pid
, pp
->current
->comm
);
212 p
->start_time
= timestamp
;
214 p
->current
->start_time
= timestamp
;
215 p
->current
->state_since
= timestamp
;
219 static void pid_exit(int pid
, u64 timestamp
)
222 p
= find_create_pid(pid
);
223 p
->end_time
= timestamp
;
225 p
->current
->end_time
= timestamp
;
229 pid_put_sample(int pid
, int type
, unsigned int cpu
, u64 start
, u64 end
,
230 const char *backtrace
)
233 struct per_pidcomm
*c
;
234 struct cpu_sample
*sample
;
236 p
= find_create_pid(pid
);
239 c
= zalloc(sizeof(*c
));
246 sample
= zalloc(sizeof(*sample
));
247 assert(sample
!= NULL
);
248 sample
->start_time
= start
;
249 sample
->end_time
= end
;
251 sample
->next
= c
->samples
;
253 sample
->backtrace
= backtrace
;
256 if (sample
->type
== TYPE_RUNNING
&& end
> start
&& start
> 0) {
257 c
->total_time
+= (end
-start
);
258 p
->total_time
+= (end
-start
);
261 if (c
->start_time
== 0 || c
->start_time
> start
)
262 c
->start_time
= start
;
263 if (p
->start_time
== 0 || p
->start_time
> start
)
264 p
->start_time
= start
;
267 #define MAX_CPUS 4096
269 static u64 cpus_cstate_start_times
[MAX_CPUS
];
270 static int cpus_cstate_state
[MAX_CPUS
];
271 static u64 cpus_pstate_start_times
[MAX_CPUS
];
272 static u64 cpus_pstate_state
[MAX_CPUS
];
274 static int process_comm_event(struct perf_tool
*tool __maybe_unused
,
275 union perf_event
*event
,
276 struct perf_sample
*sample __maybe_unused
,
277 struct machine
*machine __maybe_unused
)
279 pid_set_comm(event
->comm
.tid
, event
->comm
.comm
);
283 static int process_fork_event(struct perf_tool
*tool __maybe_unused
,
284 union perf_event
*event
,
285 struct perf_sample
*sample __maybe_unused
,
286 struct machine
*machine __maybe_unused
)
288 pid_fork(event
->fork
.pid
, event
->fork
.ppid
, event
->fork
.time
);
292 static int process_exit_event(struct perf_tool
*tool __maybe_unused
,
293 union perf_event
*event
,
294 struct perf_sample
*sample __maybe_unused
,
295 struct machine
*machine __maybe_unused
)
297 pid_exit(event
->fork
.pid
, event
->fork
.time
);
301 #ifdef SUPPORT_OLD_POWER_EVENTS
302 static int use_old_power_events
;
305 static void c_state_start(int cpu
, u64 timestamp
, int state
)
307 cpus_cstate_start_times
[cpu
] = timestamp
;
308 cpus_cstate_state
[cpu
] = state
;
311 static void c_state_end(int cpu
, u64 timestamp
)
313 struct power_event
*pwr
= zalloc(sizeof(*pwr
));
318 pwr
->state
= cpus_cstate_state
[cpu
];
319 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
320 pwr
->end_time
= timestamp
;
323 pwr
->next
= power_events
;
328 static void p_state_change(struct timechart
*tchart
, int cpu
, u64 timestamp
, u64 new_freq
)
330 struct power_event
*pwr
;
332 if (new_freq
> 8000000) /* detect invalid data */
335 pwr
= zalloc(sizeof(*pwr
));
339 pwr
->state
= cpus_pstate_state
[cpu
];
340 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
341 pwr
->end_time
= timestamp
;
344 pwr
->next
= power_events
;
346 if (!pwr
->start_time
)
347 pwr
->start_time
= tchart
->first_time
;
351 cpus_pstate_state
[cpu
] = new_freq
;
352 cpus_pstate_start_times
[cpu
] = timestamp
;
354 if ((u64
)new_freq
> tchart
->max_freq
)
355 tchart
->max_freq
= new_freq
;
357 if (new_freq
< tchart
->min_freq
|| tchart
->min_freq
== 0)
358 tchart
->min_freq
= new_freq
;
360 if (new_freq
== tchart
->max_freq
- 1000)
361 tchart
->turbo_frequency
= tchart
->max_freq
;
364 static void sched_wakeup(int cpu
, u64 timestamp
, int waker
, int wakee
,
365 u8 flags
, const char *backtrace
)
368 struct wake_event
*we
= zalloc(sizeof(*we
));
373 we
->time
= timestamp
;
375 we
->backtrace
= backtrace
;
377 if ((flags
& TRACE_FLAG_HARDIRQ
) || (flags
& TRACE_FLAG_SOFTIRQ
))
381 we
->next
= wake_events
;
383 p
= find_create_pid(we
->wakee
);
385 if (p
&& p
->current
&& p
->current
->state
== TYPE_NONE
) {
386 p
->current
->state_since
= timestamp
;
387 p
->current
->state
= TYPE_WAITING
;
389 if (p
&& p
->current
&& p
->current
->state
== TYPE_BLOCKED
) {
390 pid_put_sample(p
->pid
, p
->current
->state
, cpu
,
391 p
->current
->state_since
, timestamp
, NULL
);
392 p
->current
->state_since
= timestamp
;
393 p
->current
->state
= TYPE_WAITING
;
397 static void sched_switch(int cpu
, u64 timestamp
, int prev_pid
, int next_pid
,
398 u64 prev_state
, const char *backtrace
)
400 struct per_pid
*p
= NULL
, *prev_p
;
402 prev_p
= find_create_pid(prev_pid
);
404 p
= find_create_pid(next_pid
);
406 if (prev_p
->current
&& prev_p
->current
->state
!= TYPE_NONE
)
407 pid_put_sample(prev_pid
, TYPE_RUNNING
, cpu
,
408 prev_p
->current
->state_since
, timestamp
,
410 if (p
&& p
->current
) {
411 if (p
->current
->state
!= TYPE_NONE
)
412 pid_put_sample(next_pid
, p
->current
->state
, cpu
,
413 p
->current
->state_since
, timestamp
,
416 p
->current
->state_since
= timestamp
;
417 p
->current
->state
= TYPE_RUNNING
;
420 if (prev_p
->current
) {
421 prev_p
->current
->state
= TYPE_NONE
;
422 prev_p
->current
->state_since
= timestamp
;
424 prev_p
->current
->state
= TYPE_BLOCKED
;
426 prev_p
->current
->state
= TYPE_WAITING
;
430 static const char *cat_backtrace(union perf_event
*event
,
431 struct perf_sample
*sample
,
432 struct machine
*machine
)
434 struct addr_location al
;
438 u8 cpumode
= PERF_RECORD_MISC_USER
;
439 struct addr_location tal
;
440 struct ip_callchain
*chain
= sample
->callchain
;
441 FILE *f
= open_memstream(&p
, &p_len
);
444 perror("open_memstream error");
451 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
452 fprintf(stderr
, "problem processing %d event, skipping it.\n",
457 for (i
= 0; i
< chain
->nr
; i
++) {
460 if (callchain_param
.order
== ORDER_CALLEE
)
463 ip
= chain
->ips
[chain
->nr
- i
- 1];
465 if (ip
>= PERF_CONTEXT_MAX
) {
467 case PERF_CONTEXT_HV
:
468 cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
470 case PERF_CONTEXT_KERNEL
:
471 cpumode
= PERF_RECORD_MISC_KERNEL
;
473 case PERF_CONTEXT_USER
:
474 cpumode
= PERF_RECORD_MISC_USER
;
477 pr_debug("invalid callchain context: "
478 "%"PRId64
"\n", (s64
) ip
);
481 * It seems the callchain is corrupted.
491 tal
.filtered
= false;
492 thread__find_addr_location(al
.thread
, machine
, cpumode
,
493 MAP__FUNCTION
, ip
, &tal
);
496 fprintf(f
, "..... %016" PRIx64
" %s\n", ip
,
499 fprintf(f
, "..... %016" PRIx64
"\n", ip
);
508 typedef int (*tracepoint_handler
)(struct timechart
*tchart
,
509 struct perf_evsel
*evsel
,
510 struct perf_sample
*sample
,
511 const char *backtrace
);
513 static int process_sample_event(struct perf_tool
*tool
,
514 union perf_event
*event
,
515 struct perf_sample
*sample
,
516 struct perf_evsel
*evsel
,
517 struct machine
*machine
)
519 struct timechart
*tchart
= container_of(tool
, struct timechart
, tool
);
521 if (evsel
->attr
.sample_type
& PERF_SAMPLE_TIME
) {
522 if (!tchart
->first_time
|| tchart
->first_time
> sample
->time
)
523 tchart
->first_time
= sample
->time
;
524 if (tchart
->last_time
< sample
->time
)
525 tchart
->last_time
= sample
->time
;
528 if (sample
->cpu
> tchart
->numcpus
)
529 tchart
->numcpus
= sample
->cpu
;
531 if (evsel
->handler
!= NULL
) {
532 tracepoint_handler f
= evsel
->handler
;
533 return f(tchart
, evsel
, sample
, cat_backtrace(event
, sample
, machine
));
540 process_sample_cpu_idle(struct timechart
*tchart __maybe_unused
,
541 struct perf_evsel
*evsel
,
542 struct perf_sample
*sample
,
543 const char *backtrace __maybe_unused
)
545 u32 state
= perf_evsel__intval(evsel
, sample
, "state");
546 u32 cpu_id
= perf_evsel__intval(evsel
, sample
, "cpu_id");
548 if (state
== (u32
)PWR_EVENT_EXIT
)
549 c_state_end(cpu_id
, sample
->time
);
551 c_state_start(cpu_id
, sample
->time
, state
);
556 process_sample_cpu_frequency(struct timechart
*tchart
,
557 struct perf_evsel
*evsel
,
558 struct perf_sample
*sample
,
559 const char *backtrace __maybe_unused
)
561 u32 state
= perf_evsel__intval(evsel
, sample
, "state");
562 u32 cpu_id
= perf_evsel__intval(evsel
, sample
, "cpu_id");
564 p_state_change(tchart
, cpu_id
, sample
->time
, state
);
569 process_sample_sched_wakeup(struct timechart
*tchart __maybe_unused
,
570 struct perf_evsel
*evsel
,
571 struct perf_sample
*sample
,
572 const char *backtrace
)
574 u8 flags
= perf_evsel__intval(evsel
, sample
, "common_flags");
575 int waker
= perf_evsel__intval(evsel
, sample
, "common_pid");
576 int wakee
= perf_evsel__intval(evsel
, sample
, "pid");
578 sched_wakeup(sample
->cpu
, sample
->time
, waker
, wakee
, flags
, backtrace
);
583 process_sample_sched_switch(struct timechart
*tchart __maybe_unused
,
584 struct perf_evsel
*evsel
,
585 struct perf_sample
*sample
,
586 const char *backtrace
)
588 int prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid");
589 int next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
590 u64 prev_state
= perf_evsel__intval(evsel
, sample
, "prev_state");
592 sched_switch(sample
->cpu
, sample
->time
, prev_pid
, next_pid
, prev_state
,
597 #ifdef SUPPORT_OLD_POWER_EVENTS
599 process_sample_power_start(struct timechart
*tchart __maybe_unused
,
600 struct perf_evsel
*evsel
,
601 struct perf_sample
*sample
,
602 const char *backtrace __maybe_unused
)
604 u64 cpu_id
= perf_evsel__intval(evsel
, sample
, "cpu_id");
605 u64 value
= perf_evsel__intval(evsel
, sample
, "value");
607 c_state_start(cpu_id
, sample
->time
, value
);
612 process_sample_power_end(struct timechart
*tchart __maybe_unused
,
613 struct perf_evsel
*evsel __maybe_unused
,
614 struct perf_sample
*sample
,
615 const char *backtrace __maybe_unused
)
617 c_state_end(sample
->cpu
, sample
->time
);
622 process_sample_power_frequency(struct timechart
*tchart
,
623 struct perf_evsel
*evsel
,
624 struct perf_sample
*sample
,
625 const char *backtrace __maybe_unused
)
627 u64 cpu_id
= perf_evsel__intval(evsel
, sample
, "cpu_id");
628 u64 value
= perf_evsel__intval(evsel
, sample
, "value");
630 p_state_change(tchart
, cpu_id
, sample
->time
, value
);
633 #endif /* SUPPORT_OLD_POWER_EVENTS */
636 * After the last sample we need to wrap up the current C/P state
637 * and close out each CPU for these.
639 static void end_sample_processing(struct timechart
*tchart
)
642 struct power_event
*pwr
;
644 for (cpu
= 0; cpu
<= tchart
->numcpus
; cpu
++) {
647 pwr
= zalloc(sizeof(*pwr
));
651 pwr
->state
= cpus_cstate_state
[cpu
];
652 pwr
->start_time
= cpus_cstate_start_times
[cpu
];
653 pwr
->end_time
= tchart
->last_time
;
656 pwr
->next
= power_events
;
662 pwr
= zalloc(sizeof(*pwr
));
666 pwr
->state
= cpus_pstate_state
[cpu
];
667 pwr
->start_time
= cpus_pstate_start_times
[cpu
];
668 pwr
->end_time
= tchart
->last_time
;
671 pwr
->next
= power_events
;
673 if (!pwr
->start_time
)
674 pwr
->start_time
= tchart
->first_time
;
676 pwr
->state
= tchart
->min_freq
;
682 * Sort the pid datastructure
684 static void sort_pids(void)
686 struct per_pid
*new_list
, *p
, *cursor
, *prev
;
687 /* sort by ppid first, then by pid, lowest to highest */
696 if (new_list
== NULL
) {
704 if (cursor
->ppid
> p
->ppid
||
705 (cursor
->ppid
== p
->ppid
&& cursor
->pid
> p
->pid
)) {
706 /* must insert before */
708 p
->next
= prev
->next
;
721 cursor
= cursor
->next
;
730 static void draw_c_p_states(struct timechart
*tchart
)
732 struct power_event
*pwr
;
736 * two pass drawing so that the P state bars are on top of the C state blocks
739 if (pwr
->type
== CSTATE
)
740 svg_cstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
746 if (pwr
->type
== PSTATE
) {
748 pwr
->state
= tchart
->min_freq
;
749 svg_pstate(pwr
->cpu
, pwr
->start_time
, pwr
->end_time
, pwr
->state
);
755 static void draw_wakeups(void)
757 struct wake_event
*we
;
759 struct per_pidcomm
*c
;
763 int from
= 0, to
= 0;
764 char *task_from
= NULL
, *task_to
= NULL
;
766 /* locate the column of the waker and wakee */
769 if (p
->pid
== we
->waker
|| p
->pid
== we
->wakee
) {
772 if (c
->Y
&& c
->start_time
<= we
->time
&& c
->end_time
>= we
->time
) {
773 if (p
->pid
== we
->waker
&& !from
) {
775 task_from
= strdup(c
->comm
);
777 if (p
->pid
== we
->wakee
&& !to
) {
779 task_to
= strdup(c
->comm
);
786 if (p
->pid
== we
->waker
&& !from
) {
788 task_from
= strdup(c
->comm
);
790 if (p
->pid
== we
->wakee
&& !to
) {
792 task_to
= strdup(c
->comm
);
801 task_from
= malloc(40);
802 sprintf(task_from
, "[%i]", we
->waker
);
805 task_to
= malloc(40);
806 sprintf(task_to
, "[%i]", we
->wakee
);
810 svg_interrupt(we
->time
, to
, we
->backtrace
);
811 else if (from
&& to
&& abs(from
- to
) == 1)
812 svg_wakeline(we
->time
, from
, to
, we
->backtrace
);
814 svg_partial_wakeline(we
->time
, from
, task_from
, to
,
815 task_to
, we
->backtrace
);
823 static void draw_cpu_usage(void)
826 struct per_pidcomm
*c
;
827 struct cpu_sample
*sample
;
834 if (sample
->type
== TYPE_RUNNING
)
835 svg_process(sample
->cpu
, sample
->start_time
, sample
->end_time
, "sample", c
->comm
);
837 sample
= sample
->next
;
845 static void draw_process_bars(struct timechart
*tchart
)
848 struct per_pidcomm
*c
;
849 struct cpu_sample
*sample
;
852 Y
= 2 * tchart
->numcpus
+ 2;
864 svg_box(Y
, c
->start_time
, c
->end_time
, "process");
867 if (sample
->type
== TYPE_RUNNING
)
868 svg_running(Y
, sample
->cpu
,
872 if (sample
->type
== TYPE_BLOCKED
)
873 svg_blocked(Y
, sample
->cpu
,
877 if (sample
->type
== TYPE_WAITING
)
878 svg_waiting(Y
, sample
->cpu
,
882 sample
= sample
->next
;
887 if (c
->total_time
> 5000000000) /* 5 seconds */
888 sprintf(comm
, "%s:%i (%2.2fs)", c
->comm
, p
->pid
, c
->total_time
/ 1000000000.0);
890 sprintf(comm
, "%s:%i (%3.1fms)", c
->comm
, p
->pid
, c
->total_time
/ 1000000.0);
892 svg_text(Y
, c
->start_time
, comm
);
902 static void add_process_filter(const char *string
)
904 int pid
= strtoull(string
, NULL
, 10);
905 struct process_filter
*filt
= malloc(sizeof(*filt
));
910 filt
->name
= strdup(string
);
912 filt
->next
= process_filter
;
914 process_filter
= filt
;
917 static int passes_filter(struct per_pid
*p
, struct per_pidcomm
*c
)
919 struct process_filter
*filt
;
923 filt
= process_filter
;
925 if (filt
->pid
&& p
->pid
== filt
->pid
)
927 if (strcmp(filt
->name
, c
->comm
) == 0)
934 static int determine_display_tasks_filtered(struct timechart
*tchart
)
937 struct per_pidcomm
*c
;
943 if (p
->start_time
== 1)
944 p
->start_time
= tchart
->first_time
;
946 /* no exit marker, task kept running to the end */
947 if (p
->end_time
== 0)
948 p
->end_time
= tchart
->last_time
;
955 if (c
->start_time
== 1)
956 c
->start_time
= tchart
->first_time
;
958 if (passes_filter(p
, c
)) {
964 if (c
->end_time
== 0)
965 c
->end_time
= tchart
->last_time
;
974 static int determine_display_tasks(struct timechart
*tchart
, u64 threshold
)
977 struct per_pidcomm
*c
;
981 return determine_display_tasks_filtered(tchart
);
986 if (p
->start_time
== 1)
987 p
->start_time
= tchart
->first_time
;
989 /* no exit marker, task kept running to the end */
990 if (p
->end_time
== 0)
991 p
->end_time
= tchart
->last_time
;
992 if (p
->total_time
>= threshold
)
1000 if (c
->start_time
== 1)
1001 c
->start_time
= tchart
->first_time
;
1003 if (c
->total_time
>= threshold
) {
1008 if (c
->end_time
== 0)
1009 c
->end_time
= tchart
->last_time
;
1020 #define TIME_THRESH 10000000
1022 static void write_svg_file(struct timechart
*tchart
, const char *filename
)
1026 int thresh
= TIME_THRESH
;
1030 if (tchart
->power_only
)
1031 tchart
->proc_num
= 0;
1033 /* We'd like to show at least proc_num tasks;
1034 * be less picky if we have fewer */
1036 count
= determine_display_tasks(tchart
, thresh
);
1038 } while (!process_filter
&& thresh
&& count
< tchart
->proc_num
);
1040 open_svg(filename
, tchart
->numcpus
, count
, tchart
->first_time
, tchart
->last_time
);
1045 for (i
= 0; i
< tchart
->numcpus
; i
++)
1046 svg_cpu_box(i
, tchart
->max_freq
, tchart
->turbo_frequency
);
1049 if (tchart
->proc_num
)
1050 draw_process_bars(tchart
);
1051 if (!tchart
->tasks_only
)
1052 draw_c_p_states(tchart
);
1053 if (tchart
->proc_num
)
1059 static int __cmd_timechart(struct timechart
*tchart
, const char *output_name
)
1061 const struct perf_evsel_str_handler power_tracepoints
[] = {
1062 { "power:cpu_idle", process_sample_cpu_idle
},
1063 { "power:cpu_frequency", process_sample_cpu_frequency
},
1064 { "sched:sched_wakeup", process_sample_sched_wakeup
},
1065 { "sched:sched_switch", process_sample_sched_switch
},
1066 #ifdef SUPPORT_OLD_POWER_EVENTS
1067 { "power:power_start", process_sample_power_start
},
1068 { "power:power_end", process_sample_power_end
},
1069 { "power:power_frequency", process_sample_power_frequency
},
1072 struct perf_data_file file
= {
1074 .mode
= PERF_DATA_MODE_READ
,
1077 struct perf_session
*session
= perf_session__new(&file
, false,
1081 if (session
== NULL
)
1084 if (!perf_session__has_traces(session
, "timechart record"))
1087 if (perf_session__set_tracepoints_handlers(session
,
1088 power_tracepoints
)) {
1089 pr_err("Initializing session tracepoint handlers failed\n");
1093 ret
= perf_session__process_events(session
, &tchart
->tool
);
1097 end_sample_processing(tchart
);
1101 write_svg_file(tchart
, output_name
);
1103 pr_info("Written %2.1f seconds of trace to %s.\n",
1104 (tchart
->last_time
- tchart
->first_time
) / 1000000000.0, output_name
);
1106 perf_session__delete(session
);
1110 static int timechart__record(struct timechart
*tchart
, int argc
, const char **argv
)
1112 unsigned int rec_argc
, i
, j
;
1113 const char **rec_argv
;
1115 unsigned int record_elems
;
1117 const char * const common_args
[] = {
1118 "record", "-a", "-R", "-c", "1",
1120 unsigned int common_args_nr
= ARRAY_SIZE(common_args
);
1122 const char * const backtrace_args
[] = {
1125 unsigned int backtrace_args_no
= ARRAY_SIZE(backtrace_args
);
1127 const char * const power_args
[] = {
1128 "-e", "power:cpu_frequency",
1129 "-e", "power:cpu_idle",
1131 unsigned int power_args_nr
= ARRAY_SIZE(power_args
);
1133 const char * const old_power_args
[] = {
1134 #ifdef SUPPORT_OLD_POWER_EVENTS
1135 "-e", "power:power_start",
1136 "-e", "power:power_end",
1137 "-e", "power:power_frequency",
1140 unsigned int old_power_args_nr
= ARRAY_SIZE(old_power_args
);
1142 const char * const tasks_args
[] = {
1143 "-e", "sched:sched_wakeup",
1144 "-e", "sched:sched_switch",
1146 unsigned int tasks_args_nr
= ARRAY_SIZE(tasks_args
);
1148 #ifdef SUPPORT_OLD_POWER_EVENTS
1149 if (!is_valid_tracepoint("power:cpu_idle") &&
1150 is_valid_tracepoint("power:power_start")) {
1151 use_old_power_events
= 1;
1154 old_power_args_nr
= 0;
1158 if (tchart
->power_only
)
1161 if (tchart
->tasks_only
) {
1163 old_power_args_nr
= 0;
1166 if (!tchart
->with_backtrace
)
1167 backtrace_args_no
= 0;
1169 record_elems
= common_args_nr
+ tasks_args_nr
+
1170 power_args_nr
+ old_power_args_nr
+ backtrace_args_no
;
1172 rec_argc
= record_elems
+ argc
;
1173 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1175 if (rec_argv
== NULL
)
1179 for (i
= 0; i
< common_args_nr
; i
++)
1180 *p
++ = strdup(common_args
[i
]);
1182 for (i
= 0; i
< backtrace_args_no
; i
++)
1183 *p
++ = strdup(backtrace_args
[i
]);
1185 for (i
= 0; i
< tasks_args_nr
; i
++)
1186 *p
++ = strdup(tasks_args
[i
]);
1188 for (i
= 0; i
< power_args_nr
; i
++)
1189 *p
++ = strdup(power_args
[i
]);
1191 for (i
= 0; i
< old_power_args_nr
; i
++)
1192 *p
++ = strdup(old_power_args
[i
]);
1194 for (j
= 1; j
< (unsigned int)argc
; j
++)
1197 return cmd_record(rec_argc
, rec_argv
, NULL
);
1201 parse_process(const struct option
*opt __maybe_unused
, const char *arg
,
1202 int __maybe_unused unset
)
1205 add_process_filter(arg
);
1209 int cmd_timechart(int argc
, const char **argv
,
1210 const char *prefix __maybe_unused
)
1212 struct timechart tchart
= {
1214 .comm
= process_comm_event
,
1215 .fork
= process_fork_event
,
1216 .exit
= process_exit_event
,
1217 .sample
= process_sample_event
,
1218 .ordered_samples
= true,
1222 const char *output_name
= "output.svg";
1223 const struct option timechart_options
[] = {
1224 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
1225 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
1226 OPT_INTEGER('w', "width", &svg_page_width
, "page width"),
1227 OPT_BOOLEAN('P', "power-only", &tchart
.power_only
, "output power data only"),
1228 OPT_BOOLEAN('T', "tasks-only", &tchart
.tasks_only
,
1229 "output processes data only"),
1230 OPT_CALLBACK('p', "process", NULL
, "process",
1231 "process selector. Pass a pid or process name.",
1233 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1234 "Look for files with symbols relative to this directory"),
1235 OPT_INTEGER('n', "proc-num", &tchart
.proc_num
,
1236 "min. number of tasks to print"),
1239 const char * const timechart_usage
[] = {
1240 "perf timechart [<options>] {record}",
1244 const struct option record_options
[] = {
1245 OPT_BOOLEAN('P', "power-only", &tchart
.power_only
, "output power data only"),
1246 OPT_BOOLEAN('T', "tasks-only", &tchart
.tasks_only
,
1247 "output processes data only"),
1248 OPT_BOOLEAN('g', "callchain", &tchart
.with_backtrace
, "record callchain"),
1251 const char * const record_usage
[] = {
1252 "perf timechart record [<options>]",
1255 argc
= parse_options(argc
, argv
, timechart_options
, timechart_usage
,
1256 PARSE_OPT_STOP_AT_NON_OPTION
);
1258 if (tchart
.power_only
&& tchart
.tasks_only
) {
1259 pr_err("-P and -T options cannot be used at the same time.\n");
1265 if (argc
&& !strncmp(argv
[0], "rec", 3)) {
1266 argc
= parse_options(argc
, argv
, record_options
, record_usage
,
1267 PARSE_OPT_STOP_AT_NON_OPTION
);
1269 if (tchart
.power_only
&& tchart
.tasks_only
) {
1270 pr_err("-P and -T options cannot be used at the same time.\n");
1274 return timechart__record(&tchart
, argc
, argv
);
1276 usage_with_options(timechart_usage
, timechart_options
);
1280 return __cmd_timechart(&tchart
, output_name
);