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 static unsigned int numcpus;
46 static u64 min_freq; /* Lowest CPU frequency seen */
47 static u64 max_freq; /* Highest CPU frequency seen */
48 static u64 turbo_frequency;
50 static u64 first_time, last_time;
52 static bool power_only;
62 struct sample_wrapper;
65 * Datastructure layout:
66 * We keep an list of "pid"s, matching the kernels notion of a task struct.
67 * Each "pid" entry, has a list of "comm"s.
68 * this is because we want to track different programs different, while
69 * exec will reuse the original pid (by design).
70 * Each comm has a list of samples that will be used to draw
85 struct per_pidcomm *all;
86 struct per_pidcomm *current;
91 struct per_pidcomm *next;
105 struct cpu_sample *samples;
108 struct sample_wrapper {
109 struct sample_wrapper *next;
112 unsigned char data[0];
116 #define TYPE_RUNNING 1
117 #define TYPE_WAITING 2
118 #define TYPE_BLOCKED 3
121 struct cpu_sample *next;
129 static struct per_pid *all_data;
135 struct power_event *next;
144 struct wake_event *next;
150 static struct power_event *power_events;
151 static struct wake_event *wake_events;
153 struct process_filter;
154 struct process_filter {
157 struct process_filter *next;
160 static struct process_filter *process_filter;
163 static struct per_pid *find_create_pid(int pid)
165 struct per_pid *cursor = all_data;
168 if (cursor->pid == pid)
170 cursor = cursor->next;
172 cursor = zalloc(sizeof(*cursor));
173 assert(cursor != NULL);
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 = zalloc(sizeof(*c));
200 c->comm = strdup(comm);
206 static void pid_fork(int pid, int ppid, u64 timestamp)
208 struct per_pid *p, *pp;
209 p = find_create_pid(pid);
210 pp = find_create_pid(ppid);
212 if (pp->current && pp->current->comm && !p->current)
213 pid_set_comm(pid, pp->current->comm);
215 p->start_time = timestamp;
217 p->current->start_time = timestamp;
218 p->current->state_since = timestamp;
222 static void pid_exit(int pid, u64 timestamp)
225 p = find_create_pid(pid);
226 p->end_time = timestamp;
228 p->current->end_time = timestamp;
232 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
235 struct per_pidcomm *c;
236 struct cpu_sample *sample;
238 p = find_create_pid(pid);
241 c = zalloc(sizeof(*c));
248 sample = zalloc(sizeof(*sample));
249 assert(sample != NULL);
250 sample->start_time = start;
251 sample->end_time = end;
253 sample->next = c->samples;
257 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
258 c->total_time += (end-start);
259 p->total_time += (end-start);
262 if (c->start_time == 0 || c->start_time > start)
263 c->start_time = start;
264 if (p->start_time == 0 || p->start_time > start)
265 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(struct perf_tool *tool __maybe_unused,
276 union perf_event *event,
277 struct perf_sample *sample __maybe_unused,
278 struct machine *machine __maybe_unused)
280 pid_set_comm(event->comm.tid, event->comm.comm);
284 static int process_fork_event(struct perf_tool *tool __maybe_unused,
285 union perf_event *event,
286 struct perf_sample *sample __maybe_unused,
287 struct machine *machine __maybe_unused)
289 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
293 static int process_exit_event(struct perf_tool *tool __maybe_unused,
294 union perf_event *event,
295 struct perf_sample *sample __maybe_unused,
296 struct machine *machine __maybe_unused)
298 pid_exit(event->fork.pid, event->fork.time);
305 unsigned char preempt_count;
310 #ifdef SUPPORT_OLD_POWER_EVENTS
311 static int use_old_power_events;
312 struct power_entry_old {
313 struct trace_entry te;
320 struct power_processor_entry {
321 struct trace_entry te;
326 #define TASK_COMM_LEN 16
327 struct wakeup_entry {
328 struct trace_entry te;
329 char comm[TASK_COMM_LEN];
335 struct sched_switch {
336 struct trace_entry te;
337 char prev_comm[TASK_COMM_LEN];
340 long prev_state; /* Arjan weeps. */
341 char next_comm[TASK_COMM_LEN];
346 static void c_state_start(int cpu, u64 timestamp, int state)
348 cpus_cstate_start_times[cpu] = timestamp;
349 cpus_cstate_state[cpu] = state;
352 static void c_state_end(int cpu, u64 timestamp)
354 struct power_event *pwr = zalloc(sizeof(*pwr));
359 pwr->state = cpus_cstate_state[cpu];
360 pwr->start_time = cpus_cstate_start_times[cpu];
361 pwr->end_time = timestamp;
364 pwr->next = power_events;
369 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
371 struct power_event *pwr;
373 if (new_freq > 8000000) /* detect invalid data */
376 pwr = zalloc(sizeof(*pwr));
380 pwr->state = cpus_pstate_state[cpu];
381 pwr->start_time = cpus_pstate_start_times[cpu];
382 pwr->end_time = timestamp;
385 pwr->next = power_events;
387 if (!pwr->start_time)
388 pwr->start_time = first_time;
392 cpus_pstate_state[cpu] = new_freq;
393 cpus_pstate_start_times[cpu] = timestamp;
395 if ((u64)new_freq > max_freq)
398 if (new_freq < min_freq || min_freq == 0)
401 if (new_freq == max_freq - 1000)
402 turbo_frequency = max_freq;
406 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
409 struct wakeup_entry *wake = (void *)te;
410 struct wake_event *we = zalloc(sizeof(*we));
415 we->time = timestamp;
418 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
421 we->wakee = wake->pid;
422 we->next = wake_events;
424 p = find_create_pid(we->wakee);
426 if (p && p->current && p->current->state == TYPE_NONE) {
427 p->current->state_since = timestamp;
428 p->current->state = TYPE_WAITING;
430 if (p && p->current && p->current->state == TYPE_BLOCKED) {
431 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
432 p->current->state_since = timestamp;
433 p->current->state = TYPE_WAITING;
437 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
439 struct per_pid *p = NULL, *prev_p;
440 struct sched_switch *sw = (void *)te;
443 prev_p = find_create_pid(sw->prev_pid);
445 p = find_create_pid(sw->next_pid);
447 if (prev_p->current && prev_p->current->state != TYPE_NONE)
448 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
449 if (p && p->current) {
450 if (p->current->state != TYPE_NONE)
451 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
453 p->current->state_since = timestamp;
454 p->current->state = TYPE_RUNNING;
457 if (prev_p->current) {
458 prev_p->current->state = TYPE_NONE;
459 prev_p->current->state_since = timestamp;
460 if (sw->prev_state & 2)
461 prev_p->current->state = TYPE_BLOCKED;
462 if (sw->prev_state == 0)
463 prev_p->current->state = TYPE_WAITING;
467 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
468 struct perf_sample *sample);
470 static int process_sample_event(struct perf_tool *tool __maybe_unused,
471 union perf_event *event __maybe_unused,
472 struct perf_sample *sample,
473 struct perf_evsel *evsel,
474 struct machine *machine __maybe_unused)
476 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
477 if (!first_time || first_time > sample->time)
478 first_time = sample->time;
479 if (last_time < sample->time)
480 last_time = sample->time;
483 if (sample->cpu > numcpus)
484 numcpus = sample->cpu;
486 if (evsel->handler != NULL) {
487 tracepoint_handler f = evsel->handler;
488 return f(evsel, sample);
495 process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
496 struct perf_sample *sample)
498 struct power_processor_entry *ppe = sample->raw_data;
500 if (ppe->state == (u32) PWR_EVENT_EXIT)
501 c_state_end(ppe->cpu_id, sample->time);
503 c_state_start(ppe->cpu_id, sample->time, ppe->state);
508 process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
509 struct perf_sample *sample)
511 struct power_processor_entry *ppe = sample->raw_data;
513 p_state_change(ppe->cpu_id, sample->time, ppe->state);
518 process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
519 struct perf_sample *sample)
521 struct trace_entry *te = sample->raw_data;
523 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
528 process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
529 struct perf_sample *sample)
531 struct trace_entry *te = sample->raw_data;
533 sched_switch(sample->cpu, sample->time, te);
537 #ifdef SUPPORT_OLD_POWER_EVENTS
539 process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
540 struct perf_sample *sample)
542 struct power_entry_old *peo = sample->raw_data;
544 c_state_start(peo->cpu_id, sample->time, peo->value);
549 process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
550 struct perf_sample *sample)
552 c_state_end(sample->cpu, sample->time);
557 process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
558 struct perf_sample *sample)
560 struct power_entry_old *peo = sample->raw_data;
562 p_state_change(peo->cpu_id, sample->time, peo->value);
565 #endif /* SUPPORT_OLD_POWER_EVENTS */
568 * After the last sample we need to wrap up the current C/P state
569 * and close out each CPU for these.
571 static void end_sample_processing(void)
574 struct power_event *pwr;
576 for (cpu = 0; cpu <= numcpus; cpu++) {
579 pwr = zalloc(sizeof(*pwr));
583 pwr->state = cpus_cstate_state[cpu];
584 pwr->start_time = cpus_cstate_start_times[cpu];
585 pwr->end_time = last_time;
588 pwr->next = power_events;
594 pwr = zalloc(sizeof(*pwr));
598 pwr->state = cpus_pstate_state[cpu];
599 pwr->start_time = cpus_pstate_start_times[cpu];
600 pwr->end_time = last_time;
603 pwr->next = power_events;
605 if (!pwr->start_time)
606 pwr->start_time = first_time;
608 pwr->state = min_freq;
614 * Sort the pid datastructure
616 static void sort_pids(void)
618 struct per_pid *new_list, *p, *cursor, *prev;
619 /* sort by ppid first, then by pid, lowest to highest */
628 if (new_list == NULL) {
636 if (cursor->ppid > p->ppid ||
637 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
638 /* must insert before */
640 p->next = prev->next;
653 cursor = cursor->next;
662 static void draw_c_p_states(void)
664 struct power_event *pwr;
668 * two pass drawing so that the P state bars are on top of the C state blocks
671 if (pwr->type == CSTATE)
672 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
678 if (pwr->type == PSTATE) {
680 pwr->state = min_freq;
681 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
687 static void draw_wakeups(void)
689 struct wake_event *we;
691 struct per_pidcomm *c;
695 int from = 0, to = 0;
696 char *task_from = NULL, *task_to = NULL;
698 /* locate the column of the waker and wakee */
701 if (p->pid == we->waker || p->pid == we->wakee) {
704 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
705 if (p->pid == we->waker && !from) {
707 task_from = strdup(c->comm);
709 if (p->pid == we->wakee && !to) {
711 task_to = strdup(c->comm);
718 if (p->pid == we->waker && !from) {
720 task_from = strdup(c->comm);
722 if (p->pid == we->wakee && !to) {
724 task_to = strdup(c->comm);
733 task_from = malloc(40);
734 sprintf(task_from, "[%i]", we->waker);
737 task_to = malloc(40);
738 sprintf(task_to, "[%i]", we->wakee);
742 svg_interrupt(we->time, to);
743 else if (from && to && abs(from - to) == 1)
744 svg_wakeline(we->time, from, to);
746 svg_partial_wakeline(we->time, from, task_from, to, task_to);
754 static void draw_cpu_usage(void)
757 struct per_pidcomm *c;
758 struct cpu_sample *sample;
765 if (sample->type == TYPE_RUNNING)
766 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
768 sample = sample->next;
776 static void draw_process_bars(void)
779 struct per_pidcomm *c;
780 struct cpu_sample *sample;
795 svg_box(Y, c->start_time, c->end_time, "process");
798 if (sample->type == TYPE_RUNNING)
799 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
800 if (sample->type == TYPE_BLOCKED)
801 svg_box(Y, sample->start_time, sample->end_time, "blocked");
802 if (sample->type == TYPE_WAITING)
803 svg_waiting(Y, sample->start_time, sample->end_time);
804 sample = sample->next;
809 if (c->total_time > 5000000000) /* 5 seconds */
810 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
812 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
814 svg_text(Y, c->start_time, comm);
824 static void add_process_filter(const char *string)
826 int pid = strtoull(string, NULL, 10);
827 struct process_filter *filt = malloc(sizeof(*filt));
832 filt->name = strdup(string);
834 filt->next = process_filter;
836 process_filter = filt;
839 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
841 struct process_filter *filt;
845 filt = process_filter;
847 if (filt->pid && p->pid == filt->pid)
849 if (strcmp(filt->name, c->comm) == 0)
856 static int determine_display_tasks_filtered(void)
859 struct per_pidcomm *c;
865 if (p->start_time == 1)
866 p->start_time = first_time;
868 /* no exit marker, task kept running to the end */
869 if (p->end_time == 0)
870 p->end_time = last_time;
877 if (c->start_time == 1)
878 c->start_time = first_time;
880 if (passes_filter(p, c)) {
886 if (c->end_time == 0)
887 c->end_time = last_time;
896 static int determine_display_tasks(u64 threshold)
899 struct per_pidcomm *c;
903 return determine_display_tasks_filtered();
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;
914 if (p->total_time >= threshold && !power_only)
922 if (c->start_time == 1)
923 c->start_time = first_time;
925 if (c->total_time >= threshold && !power_only) {
930 if (c->end_time == 0)
931 c->end_time = last_time;
942 #define TIME_THRESH 10000000
944 static void write_svg_file(const char *filename)
952 count = determine_display_tasks(TIME_THRESH);
954 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
956 count = determine_display_tasks(TIME_THRESH / 10);
958 open_svg(filename, numcpus, count, first_time, last_time);
963 for (i = 0; i < numcpus; i++)
964 svg_cpu_box(i, max_freq, turbo_frequency);
974 static int __cmd_timechart(const char *output_name)
976 struct perf_tool perf_timechart = {
977 .comm = process_comm_event,
978 .fork = process_fork_event,
979 .exit = process_exit_event,
980 .sample = process_sample_event,
981 .ordered_samples = true,
983 const struct perf_evsel_str_handler power_tracepoints[] = {
984 { "power:cpu_idle", process_sample_cpu_idle },
985 { "power:cpu_frequency", process_sample_cpu_frequency },
986 { "sched:sched_wakeup", process_sample_sched_wakeup },
987 { "sched:sched_switch", process_sample_sched_switch },
988 #ifdef SUPPORT_OLD_POWER_EVENTS
989 { "power:power_start", process_sample_power_start },
990 { "power:power_end", process_sample_power_end },
991 { "power:power_frequency", process_sample_power_frequency },
994 struct perf_data_file file = {
996 .mode = PERF_DATA_MODE_READ,
999 struct perf_session *session = perf_session__new(&file, false,
1003 if (session == NULL)
1006 if (!perf_session__has_traces(session, "timechart record"))
1009 if (perf_session__set_tracepoints_handlers(session,
1010 power_tracepoints)) {
1011 pr_err("Initializing session tracepoint handlers failed\n");
1015 ret = perf_session__process_events(session, &perf_timechart);
1019 end_sample_processing();
1023 write_svg_file(output_name);
1025 pr_info("Written %2.1f seconds of trace to %s.\n",
1026 (last_time - first_time) / 1000000000.0, output_name);
1028 perf_session__delete(session);
1032 static int __cmd_record(int argc, const char **argv)
1034 #ifdef SUPPORT_OLD_POWER_EVENTS
1035 const char * const record_old_args[] = {
1036 "record", "-a", "-R", "-c", "1",
1037 "-e", "power:power_start",
1038 "-e", "power:power_end",
1039 "-e", "power:power_frequency",
1040 "-e", "sched:sched_wakeup",
1041 "-e", "sched:sched_switch",
1044 const char * const record_new_args[] = {
1045 "record", "-a", "-R", "-c", "1",
1046 "-e", "power:cpu_frequency",
1047 "-e", "power:cpu_idle",
1048 "-e", "sched:sched_wakeup",
1049 "-e", "sched:sched_switch",
1051 unsigned int rec_argc, i, j;
1052 const char **rec_argv;
1053 const char * const *record_args = record_new_args;
1054 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1056 #ifdef SUPPORT_OLD_POWER_EVENTS
1057 if (!is_valid_tracepoint("power:cpu_idle") &&
1058 is_valid_tracepoint("power:power_start")) {
1059 use_old_power_events = 1;
1060 record_args = record_old_args;
1061 record_elems = ARRAY_SIZE(record_old_args);
1065 rec_argc = record_elems + argc - 1;
1066 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1068 if (rec_argv == NULL)
1071 for (i = 0; i < record_elems; i++)
1072 rec_argv[i] = strdup(record_args[i]);
1074 for (j = 1; j < (unsigned int)argc; j++, i++)
1075 rec_argv[i] = argv[j];
1077 return cmd_record(i, rec_argv, NULL);
1081 parse_process(const struct option *opt __maybe_unused, const char *arg,
1082 int __maybe_unused unset)
1085 add_process_filter(arg);
1089 int cmd_timechart(int argc, const char **argv,
1090 const char *prefix __maybe_unused)
1092 const char *output_name = "output.svg";
1093 const struct option options[] = {
1094 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1095 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1096 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1097 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
1098 OPT_CALLBACK('p', "process", NULL, "process",
1099 "process selector. Pass a pid or process name.",
1101 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1102 "Look for files with symbols relative to this directory"),
1105 const char * const timechart_usage[] = {
1106 "perf timechart [<options>] {record}",
1110 argc = parse_options(argc, argv, options, timechart_usage,
1111 PARSE_OPT_STOP_AT_NON_OPTION);
1115 if (argc && !strncmp(argv[0], "rec", 3))
1116 return __cmd_record(argc, argv);
1118 usage_with_options(timechart_usage, options);
1122 return __cmd_timechart(output_name);