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 const char *input_name;
42 static const char *output_name = "output.svg";
44 static unsigned int numcpus;
45 static u64 min_freq; /* Lowest CPU frequency seen */
46 static u64 max_freq; /* Highest CPU frequency seen */
47 static u64 turbo_frequency;
49 static u64 first_time, last_time;
51 static bool power_only;
61 struct sample_wrapper;
64 * Datastructure layout:
65 * We keep an list of "pid"s, matching the kernels notion of a task struct.
66 * Each "pid" entry, has a list of "comm"s.
67 * this is because we want to track different programs different, while
68 * exec will reuse the original pid (by design).
69 * Each comm has a list of samples that will be used to draw
84 struct per_pidcomm *all;
85 struct per_pidcomm *current;
90 struct per_pidcomm *next;
104 struct cpu_sample *samples;
107 struct sample_wrapper {
108 struct sample_wrapper *next;
111 unsigned char data[0];
115 #define TYPE_RUNNING 1
116 #define TYPE_WAITING 2
117 #define TYPE_BLOCKED 3
120 struct cpu_sample *next;
128 static struct per_pid *all_data;
134 struct power_event *next;
143 struct wake_event *next;
149 static struct power_event *power_events;
150 static struct wake_event *wake_events;
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 = zalloc(sizeof(*cursor));
172 assert(cursor != NULL);
174 cursor->next = all_data;
179 static void pid_set_comm(int pid, char *comm)
182 struct per_pidcomm *c;
183 p = find_create_pid(pid);
186 if (c->comm && strcmp(c->comm, comm) == 0) {
191 c->comm = strdup(comm);
197 c = zalloc(sizeof(*c));
199 c->comm = strdup(comm);
205 static void pid_fork(int pid, int ppid, u64 timestamp)
207 struct per_pid *p, *pp;
208 p = find_create_pid(pid);
209 pp = find_create_pid(ppid);
211 if (pp->current && pp->current->comm && !p->current)
212 pid_set_comm(pid, pp->current->comm);
214 p->start_time = timestamp;
216 p->current->start_time = timestamp;
217 p->current->state_since = timestamp;
221 static void pid_exit(int pid, u64 timestamp)
224 p = find_create_pid(pid);
225 p->end_time = timestamp;
227 p->current->end_time = timestamp;
231 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
234 struct per_pidcomm *c;
235 struct cpu_sample *sample;
237 p = find_create_pid(pid);
240 c = zalloc(sizeof(*c));
247 sample = zalloc(sizeof(*sample));
248 assert(sample != NULL);
249 sample->start_time = start;
250 sample->end_time = end;
252 sample->next = c->samples;
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);
304 unsigned char preempt_count;
309 #ifdef SUPPORT_OLD_POWER_EVENTS
310 static int use_old_power_events;
311 struct power_entry_old {
312 struct trace_entry te;
319 struct power_processor_entry {
320 struct trace_entry te;
325 #define TASK_COMM_LEN 16
326 struct wakeup_entry {
327 struct trace_entry te;
328 char comm[TASK_COMM_LEN];
335 * trace_flag_type is an enumeration that holds different
336 * states when a trace occurs. These are:
337 * IRQS_OFF - interrupts were disabled
338 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
339 * NEED_RESCED - reschedule is requested
340 * HARDIRQ - inside an interrupt handler
341 * SOFTIRQ - inside a softirq handler
343 enum trace_flag_type {
344 TRACE_FLAG_IRQS_OFF = 0x01,
345 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
346 TRACE_FLAG_NEED_RESCHED = 0x04,
347 TRACE_FLAG_HARDIRQ = 0x08,
348 TRACE_FLAG_SOFTIRQ = 0x10,
353 struct sched_switch {
354 struct trace_entry te;
355 char prev_comm[TASK_COMM_LEN];
358 long prev_state; /* Arjan weeps. */
359 char next_comm[TASK_COMM_LEN];
364 static void c_state_start(int cpu, u64 timestamp, int state)
366 cpus_cstate_start_times[cpu] = timestamp;
367 cpus_cstate_state[cpu] = state;
370 static void c_state_end(int cpu, u64 timestamp)
372 struct power_event *pwr = zalloc(sizeof(*pwr));
377 pwr->state = cpus_cstate_state[cpu];
378 pwr->start_time = cpus_cstate_start_times[cpu];
379 pwr->end_time = timestamp;
382 pwr->next = power_events;
387 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
389 struct power_event *pwr;
391 if (new_freq > 8000000) /* detect invalid data */
394 pwr = zalloc(sizeof(*pwr));
398 pwr->state = cpus_pstate_state[cpu];
399 pwr->start_time = cpus_pstate_start_times[cpu];
400 pwr->end_time = timestamp;
403 pwr->next = power_events;
405 if (!pwr->start_time)
406 pwr->start_time = first_time;
410 cpus_pstate_state[cpu] = new_freq;
411 cpus_pstate_start_times[cpu] = timestamp;
413 if ((u64)new_freq > max_freq)
416 if (new_freq < min_freq || min_freq == 0)
419 if (new_freq == max_freq - 1000)
420 turbo_frequency = max_freq;
424 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
427 struct wakeup_entry *wake = (void *)te;
428 struct wake_event *we = zalloc(sizeof(*we));
433 we->time = timestamp;
436 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
439 we->wakee = wake->pid;
440 we->next = wake_events;
442 p = find_create_pid(we->wakee);
444 if (p && p->current && p->current->state == TYPE_NONE) {
445 p->current->state_since = timestamp;
446 p->current->state = TYPE_WAITING;
448 if (p && p->current && p->current->state == TYPE_BLOCKED) {
449 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
450 p->current->state_since = timestamp;
451 p->current->state = TYPE_WAITING;
455 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
457 struct per_pid *p = NULL, *prev_p;
458 struct sched_switch *sw = (void *)te;
461 prev_p = find_create_pid(sw->prev_pid);
463 p = find_create_pid(sw->next_pid);
465 if (prev_p->current && prev_p->current->state != TYPE_NONE)
466 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
467 if (p && p->current) {
468 if (p->current->state != TYPE_NONE)
469 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
471 p->current->state_since = timestamp;
472 p->current->state = TYPE_RUNNING;
475 if (prev_p->current) {
476 prev_p->current->state = TYPE_NONE;
477 prev_p->current->state_since = timestamp;
478 if (sw->prev_state & 2)
479 prev_p->current->state = TYPE_BLOCKED;
480 if (sw->prev_state == 0)
481 prev_p->current->state = TYPE_WAITING;
486 static int process_sample_event(struct perf_tool *tool __maybe_unused,
487 union perf_event *event __maybe_unused,
488 struct perf_sample *sample,
489 struct perf_evsel *evsel,
490 struct machine *machine __maybe_unused)
492 struct trace_entry *te;
494 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
495 if (!first_time || first_time > sample->time)
496 first_time = sample->time;
497 if (last_time < sample->time)
498 last_time = sample->time;
501 te = (void *)sample->raw_data;
502 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
504 #ifdef SUPPORT_OLD_POWER_EVENTS
505 struct power_entry_old *peo;
509 * FIXME: use evsel, its already mapped from id to perf_evsel,
510 * remove perf_header__find_event infrastructure bits.
511 * Mapping all these "power:cpu_idle" strings to the tracepoint
512 * ID and then just comparing against evsel->attr.config.
516 * if (evsel->attr.config == power_cpu_idle_id)
518 event_str = perf_header__find_event(te->type);
523 if (sample->cpu > numcpus)
524 numcpus = sample->cpu;
526 if (strcmp(event_str, "power:cpu_idle") == 0) {
527 struct power_processor_entry *ppe = (void *)te;
528 if (ppe->state == (u32)PWR_EVENT_EXIT)
529 c_state_end(ppe->cpu_id, sample->time);
531 c_state_start(ppe->cpu_id, sample->time,
534 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
535 struct power_processor_entry *ppe = (void *)te;
536 p_state_change(ppe->cpu_id, sample->time, ppe->state);
539 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
540 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
542 else if (strcmp(event_str, "sched:sched_switch") == 0)
543 sched_switch(sample->cpu, sample->time, te);
545 #ifdef SUPPORT_OLD_POWER_EVENTS
546 if (use_old_power_events) {
547 if (strcmp(event_str, "power:power_start") == 0)
548 c_state_start(peo->cpu_id, sample->time,
551 else if (strcmp(event_str, "power:power_end") == 0)
552 c_state_end(sample->cpu, sample->time);
554 else if (strcmp(event_str,
555 "power:power_frequency") == 0)
556 p_state_change(peo->cpu_id, sample->time,
565 * After the last sample we need to wrap up the current C/P state
566 * and close out each CPU for these.
568 static void end_sample_processing(void)
571 struct power_event *pwr;
573 for (cpu = 0; cpu <= numcpus; cpu++) {
576 pwr = zalloc(sizeof(*pwr));
580 pwr->state = cpus_cstate_state[cpu];
581 pwr->start_time = cpus_cstate_start_times[cpu];
582 pwr->end_time = last_time;
585 pwr->next = power_events;
591 pwr = zalloc(sizeof(*pwr));
595 pwr->state = cpus_pstate_state[cpu];
596 pwr->start_time = cpus_pstate_start_times[cpu];
597 pwr->end_time = last_time;
600 pwr->next = power_events;
602 if (!pwr->start_time)
603 pwr->start_time = first_time;
605 pwr->state = min_freq;
611 * Sort the pid datastructure
613 static void sort_pids(void)
615 struct per_pid *new_list, *p, *cursor, *prev;
616 /* sort by ppid first, then by pid, lowest to highest */
625 if (new_list == NULL) {
633 if (cursor->ppid > p->ppid ||
634 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
635 /* must insert before */
637 p->next = prev->next;
650 cursor = cursor->next;
659 static void draw_c_p_states(void)
661 struct power_event *pwr;
665 * two pass drawing so that the P state bars are on top of the C state blocks
668 if (pwr->type == CSTATE)
669 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
675 if (pwr->type == PSTATE) {
677 pwr->state = min_freq;
678 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
684 static void draw_wakeups(void)
686 struct wake_event *we;
688 struct per_pidcomm *c;
692 int from = 0, to = 0;
693 char *task_from = NULL, *task_to = NULL;
695 /* locate the column of the waker and wakee */
698 if (p->pid == we->waker || p->pid == we->wakee) {
701 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
702 if (p->pid == we->waker && !from) {
704 task_from = strdup(c->comm);
706 if (p->pid == we->wakee && !to) {
708 task_to = strdup(c->comm);
715 if (p->pid == we->waker && !from) {
717 task_from = strdup(c->comm);
719 if (p->pid == we->wakee && !to) {
721 task_to = strdup(c->comm);
730 task_from = malloc(40);
731 sprintf(task_from, "[%i]", we->waker);
734 task_to = malloc(40);
735 sprintf(task_to, "[%i]", we->wakee);
739 svg_interrupt(we->time, to);
740 else if (from && to && abs(from - to) == 1)
741 svg_wakeline(we->time, from, to);
743 svg_partial_wakeline(we->time, from, task_from, to, task_to);
751 static void draw_cpu_usage(void)
754 struct per_pidcomm *c;
755 struct cpu_sample *sample;
762 if (sample->type == TYPE_RUNNING)
763 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
765 sample = sample->next;
773 static void draw_process_bars(void)
776 struct per_pidcomm *c;
777 struct cpu_sample *sample;
792 svg_box(Y, c->start_time, c->end_time, "process");
795 if (sample->type == TYPE_RUNNING)
796 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
797 if (sample->type == TYPE_BLOCKED)
798 svg_box(Y, sample->start_time, sample->end_time, "blocked");
799 if (sample->type == TYPE_WAITING)
800 svg_waiting(Y, sample->start_time, sample->end_time);
801 sample = sample->next;
806 if (c->total_time > 5000000000) /* 5 seconds */
807 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
809 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
811 svg_text(Y, c->start_time, comm);
821 static void add_process_filter(const char *string)
823 int pid = strtoull(string, NULL, 10);
824 struct process_filter *filt = malloc(sizeof(*filt));
829 filt->name = strdup(string);
831 filt->next = process_filter;
833 process_filter = filt;
836 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
838 struct process_filter *filt;
842 filt = process_filter;
844 if (filt->pid && p->pid == filt->pid)
846 if (strcmp(filt->name, c->comm) == 0)
853 static int determine_display_tasks_filtered(void)
856 struct per_pidcomm *c;
862 if (p->start_time == 1)
863 p->start_time = first_time;
865 /* no exit marker, task kept running to the end */
866 if (p->end_time == 0)
867 p->end_time = last_time;
874 if (c->start_time == 1)
875 c->start_time = first_time;
877 if (passes_filter(p, c)) {
883 if (c->end_time == 0)
884 c->end_time = last_time;
893 static int determine_display_tasks(u64 threshold)
896 struct per_pidcomm *c;
900 return determine_display_tasks_filtered();
905 if (p->start_time == 1)
906 p->start_time = first_time;
908 /* no exit marker, task kept running to the end */
909 if (p->end_time == 0)
910 p->end_time = last_time;
911 if (p->total_time >= threshold && !power_only)
919 if (c->start_time == 1)
920 c->start_time = first_time;
922 if (c->total_time >= threshold && !power_only) {
927 if (c->end_time == 0)
928 c->end_time = last_time;
939 #define TIME_THRESH 10000000
941 static void write_svg_file(const char *filename)
949 count = determine_display_tasks(TIME_THRESH);
951 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
953 count = determine_display_tasks(TIME_THRESH / 10);
955 open_svg(filename, numcpus, count, first_time, last_time);
960 for (i = 0; i < numcpus; i++)
961 svg_cpu_box(i, max_freq, turbo_frequency);
971 static struct perf_tool perf_timechart = {
972 .comm = process_comm_event,
973 .fork = process_fork_event,
974 .exit = process_exit_event,
975 .sample = process_sample_event,
976 .ordered_samples = true,
979 static int __cmd_timechart(void)
981 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
982 0, false, &perf_timechart);
988 if (!perf_session__has_traces(session, "timechart record"))
991 ret = perf_session__process_events(session, &perf_timechart);
995 end_sample_processing();
999 write_svg_file(output_name);
1001 pr_info("Written %2.1f seconds of trace to %s.\n",
1002 (last_time - first_time) / 1000000000.0, output_name);
1004 perf_session__delete(session);
1008 static const char * const timechart_usage[] = {
1009 "perf timechart [<options>] {record}",
1013 #ifdef SUPPORT_OLD_POWER_EVENTS
1014 static const char * const record_old_args[] = {
1020 "-e", "power:power_start",
1021 "-e", "power:power_end",
1022 "-e", "power:power_frequency",
1023 "-e", "sched:sched_wakeup",
1024 "-e", "sched:sched_switch",
1028 static const char * const record_new_args[] = {
1034 "-e", "power:cpu_frequency",
1035 "-e", "power:cpu_idle",
1036 "-e", "sched:sched_wakeup",
1037 "-e", "sched:sched_switch",
1040 static int __cmd_record(int argc, const char **argv)
1042 unsigned int rec_argc, i, j;
1043 const char **rec_argv;
1044 const char * const *record_args = record_new_args;
1045 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1047 #ifdef SUPPORT_OLD_POWER_EVENTS
1048 if (!is_valid_tracepoint("power:cpu_idle") &&
1049 is_valid_tracepoint("power:power_start")) {
1050 use_old_power_events = 1;
1051 record_args = record_old_args;
1052 record_elems = ARRAY_SIZE(record_old_args);
1056 rec_argc = record_elems + argc - 1;
1057 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1059 if (rec_argv == NULL)
1062 for (i = 0; i < record_elems; i++)
1063 rec_argv[i] = strdup(record_args[i]);
1065 for (j = 1; j < (unsigned int)argc; j++, i++)
1066 rec_argv[i] = argv[j];
1068 return cmd_record(i, rec_argv, NULL);
1072 parse_process(const struct option *opt __maybe_unused, const char *arg,
1073 int __maybe_unused unset)
1076 add_process_filter(arg);
1080 static const struct option options[] = {
1081 OPT_STRING('i', "input", &input_name, "file",
1083 OPT_STRING('o', "output", &output_name, "file",
1084 "output file name"),
1085 OPT_INTEGER('w', "width", &svg_page_width,
1087 OPT_BOOLEAN('P', "power-only", &power_only,
1088 "output power data only"),
1089 OPT_CALLBACK('p', "process", NULL, "process",
1090 "process selector. Pass a pid or process name.",
1092 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1093 "Look for files with symbols relative to this directory"),
1098 int cmd_timechart(int argc, const char **argv,
1099 const char *prefix __maybe_unused)
1101 argc = parse_options(argc, argv, options, timechart_usage,
1102 PARSE_OPT_STOP_AT_NON_OPTION);
1106 if (argc && !strncmp(argv[0], "rec", 3))
1107 return __cmd_record(argc, argv);
1109 usage_with_options(timechart_usage, options);
1113 return __cmd_timechart();