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
44 static int proc_num = 15;
46 static unsigned int numcpus;
47 static u64 min_freq; /* Lowest CPU frequency seen */
48 static u64 max_freq; /* Highest CPU frequency seen */
49 static u64 turbo_frequency;
51 static u64 first_time, last_time;
53 static bool power_only;
63 struct sample_wrapper;
66 * Datastructure layout:
67 * We keep an list of "pid"s, matching the kernels notion of a task struct.
68 * Each "pid" entry, has a list of "comm"s.
69 * this is because we want to track different programs different, while
70 * exec will reuse the original pid (by design).
71 * Each comm has a list of samples that will be used to draw
86 struct per_pidcomm *all;
87 struct per_pidcomm *current;
92 struct per_pidcomm *next;
106 struct cpu_sample *samples;
109 struct sample_wrapper {
110 struct sample_wrapper *next;
113 unsigned char data[0];
117 #define TYPE_RUNNING 1
118 #define TYPE_WAITING 2
119 #define TYPE_BLOCKED 3
122 struct cpu_sample *next;
130 static struct per_pid *all_data;
136 struct power_event *next;
145 struct wake_event *next;
151 static struct power_event *power_events;
152 static struct wake_event *wake_events;
154 struct process_filter;
155 struct process_filter {
158 struct process_filter *next;
161 static struct process_filter *process_filter;
164 static struct per_pid *find_create_pid(int pid)
166 struct per_pid *cursor = all_data;
169 if (cursor->pid == pid)
171 cursor = cursor->next;
173 cursor = zalloc(sizeof(*cursor));
174 assert(cursor != NULL);
176 cursor->next = all_data;
181 static void pid_set_comm(int pid, char *comm)
184 struct per_pidcomm *c;
185 p = find_create_pid(pid);
188 if (c->comm && strcmp(c->comm, comm) == 0) {
193 c->comm = strdup(comm);
199 c = zalloc(sizeof(*c));
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 = zalloc(sizeof(*c));
249 sample = zalloc(sizeof(*sample));
250 assert(sample != NULL);
251 sample->start_time = start;
252 sample->end_time = end;
254 sample->next = c->samples;
258 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
259 c->total_time += (end-start);
260 p->total_time += (end-start);
263 if (c->start_time == 0 || c->start_time > start)
264 c->start_time = start;
265 if (p->start_time == 0 || p->start_time > start)
266 p->start_time = start;
269 #define MAX_CPUS 4096
271 static u64 cpus_cstate_start_times[MAX_CPUS];
272 static int cpus_cstate_state[MAX_CPUS];
273 static u64 cpus_pstate_start_times[MAX_CPUS];
274 static u64 cpus_pstate_state[MAX_CPUS];
276 static int process_comm_event(struct perf_tool *tool __maybe_unused,
277 union perf_event *event,
278 struct perf_sample *sample __maybe_unused,
279 struct machine *machine __maybe_unused)
281 pid_set_comm(event->comm.tid, event->comm.comm);
285 static int process_fork_event(struct perf_tool *tool __maybe_unused,
286 union perf_event *event,
287 struct perf_sample *sample __maybe_unused,
288 struct machine *machine __maybe_unused)
290 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
294 static int process_exit_event(struct perf_tool *tool __maybe_unused,
295 union perf_event *event,
296 struct perf_sample *sample __maybe_unused,
297 struct machine *machine __maybe_unused)
299 pid_exit(event->fork.pid, event->fork.time);
306 unsigned char preempt_count;
311 #ifdef SUPPORT_OLD_POWER_EVENTS
312 static int use_old_power_events;
313 struct power_entry_old {
314 struct trace_entry te;
321 struct power_processor_entry {
322 struct trace_entry te;
327 #define TASK_COMM_LEN 16
328 struct wakeup_entry {
329 struct trace_entry te;
330 char comm[TASK_COMM_LEN];
336 struct sched_switch {
337 struct trace_entry te;
338 char prev_comm[TASK_COMM_LEN];
341 long prev_state; /* Arjan weeps. */
342 char next_comm[TASK_COMM_LEN];
347 static void c_state_start(int cpu, u64 timestamp, int state)
349 cpus_cstate_start_times[cpu] = timestamp;
350 cpus_cstate_state[cpu] = state;
353 static void c_state_end(int cpu, u64 timestamp)
355 struct power_event *pwr = zalloc(sizeof(*pwr));
360 pwr->state = cpus_cstate_state[cpu];
361 pwr->start_time = cpus_cstate_start_times[cpu];
362 pwr->end_time = timestamp;
365 pwr->next = power_events;
370 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
372 struct power_event *pwr;
374 if (new_freq > 8000000) /* detect invalid data */
377 pwr = zalloc(sizeof(*pwr));
381 pwr->state = cpus_pstate_state[cpu];
382 pwr->start_time = cpus_pstate_start_times[cpu];
383 pwr->end_time = timestamp;
386 pwr->next = power_events;
388 if (!pwr->start_time)
389 pwr->start_time = first_time;
393 cpus_pstate_state[cpu] = new_freq;
394 cpus_pstate_start_times[cpu] = timestamp;
396 if ((u64)new_freq > max_freq)
399 if (new_freq < min_freq || min_freq == 0)
402 if (new_freq == max_freq - 1000)
403 turbo_frequency = max_freq;
407 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
410 struct wakeup_entry *wake = (void *)te;
411 struct wake_event *we = zalloc(sizeof(*we));
416 we->time = timestamp;
419 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
422 we->wakee = wake->pid;
423 we->next = wake_events;
425 p = find_create_pid(we->wakee);
427 if (p && p->current && p->current->state == TYPE_NONE) {
428 p->current->state_since = timestamp;
429 p->current->state = TYPE_WAITING;
431 if (p && p->current && p->current->state == TYPE_BLOCKED) {
432 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
433 p->current->state_since = timestamp;
434 p->current->state = TYPE_WAITING;
438 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
440 struct per_pid *p = NULL, *prev_p;
441 struct sched_switch *sw = (void *)te;
444 prev_p = find_create_pid(sw->prev_pid);
446 p = find_create_pid(sw->next_pid);
448 if (prev_p->current && prev_p->current->state != TYPE_NONE)
449 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
450 if (p && p->current) {
451 if (p->current->state != TYPE_NONE)
452 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
454 p->current->state_since = timestamp;
455 p->current->state = TYPE_RUNNING;
458 if (prev_p->current) {
459 prev_p->current->state = TYPE_NONE;
460 prev_p->current->state_since = timestamp;
461 if (sw->prev_state & 2)
462 prev_p->current->state = TYPE_BLOCKED;
463 if (sw->prev_state == 0)
464 prev_p->current->state = TYPE_WAITING;
468 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
469 struct perf_sample *sample);
471 static int process_sample_event(struct perf_tool *tool __maybe_unused,
472 union perf_event *event __maybe_unused,
473 struct perf_sample *sample,
474 struct perf_evsel *evsel,
475 struct machine *machine __maybe_unused)
477 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
478 if (!first_time || first_time > sample->time)
479 first_time = sample->time;
480 if (last_time < sample->time)
481 last_time = sample->time;
484 if (sample->cpu > numcpus)
485 numcpus = sample->cpu;
487 if (evsel->handler != NULL) {
488 tracepoint_handler f = evsel->handler;
489 return f(evsel, sample);
496 process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
497 struct perf_sample *sample)
499 struct power_processor_entry *ppe = sample->raw_data;
501 if (ppe->state == (u32) PWR_EVENT_EXIT)
502 c_state_end(ppe->cpu_id, sample->time);
504 c_state_start(ppe->cpu_id, sample->time, ppe->state);
509 process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
510 struct perf_sample *sample)
512 struct power_processor_entry *ppe = sample->raw_data;
514 p_state_change(ppe->cpu_id, sample->time, ppe->state);
519 process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
520 struct perf_sample *sample)
522 struct trace_entry *te = sample->raw_data;
524 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
529 process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
530 struct perf_sample *sample)
532 struct trace_entry *te = sample->raw_data;
534 sched_switch(sample->cpu, sample->time, te);
538 #ifdef SUPPORT_OLD_POWER_EVENTS
540 process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
541 struct perf_sample *sample)
543 struct power_entry_old *peo = sample->raw_data;
545 c_state_start(peo->cpu_id, sample->time, peo->value);
550 process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
551 struct perf_sample *sample)
553 c_state_end(sample->cpu, sample->time);
558 process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
559 struct perf_sample *sample)
561 struct power_entry_old *peo = sample->raw_data;
563 p_state_change(peo->cpu_id, sample->time, peo->value);
566 #endif /* SUPPORT_OLD_POWER_EVENTS */
569 * After the last sample we need to wrap up the current C/P state
570 * and close out each CPU for these.
572 static void end_sample_processing(void)
575 struct power_event *pwr;
577 for (cpu = 0; cpu <= numcpus; cpu++) {
580 pwr = zalloc(sizeof(*pwr));
584 pwr->state = cpus_cstate_state[cpu];
585 pwr->start_time = cpus_cstate_start_times[cpu];
586 pwr->end_time = last_time;
589 pwr->next = power_events;
595 pwr = zalloc(sizeof(*pwr));
599 pwr->state = cpus_pstate_state[cpu];
600 pwr->start_time = cpus_pstate_start_times[cpu];
601 pwr->end_time = last_time;
604 pwr->next = power_events;
606 if (!pwr->start_time)
607 pwr->start_time = first_time;
609 pwr->state = min_freq;
615 * Sort the pid datastructure
617 static void sort_pids(void)
619 struct per_pid *new_list, *p, *cursor, *prev;
620 /* sort by ppid first, then by pid, lowest to highest */
629 if (new_list == NULL) {
637 if (cursor->ppid > p->ppid ||
638 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
639 /* must insert before */
641 p->next = prev->next;
654 cursor = cursor->next;
663 static void draw_c_p_states(void)
665 struct power_event *pwr;
669 * two pass drawing so that the P state bars are on top of the C state blocks
672 if (pwr->type == CSTATE)
673 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
679 if (pwr->type == PSTATE) {
681 pwr->state = min_freq;
682 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
688 static void draw_wakeups(void)
690 struct wake_event *we;
692 struct per_pidcomm *c;
696 int from = 0, to = 0;
697 char *task_from = NULL, *task_to = NULL;
699 /* locate the column of the waker and wakee */
702 if (p->pid == we->waker || p->pid == we->wakee) {
705 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
706 if (p->pid == we->waker && !from) {
708 task_from = strdup(c->comm);
710 if (p->pid == we->wakee && !to) {
712 task_to = strdup(c->comm);
719 if (p->pid == we->waker && !from) {
721 task_from = strdup(c->comm);
723 if (p->pid == we->wakee && !to) {
725 task_to = strdup(c->comm);
734 task_from = malloc(40);
735 sprintf(task_from, "[%i]", we->waker);
738 task_to = malloc(40);
739 sprintf(task_to, "[%i]", we->wakee);
743 svg_interrupt(we->time, to);
744 else if (from && to && abs(from - to) == 1)
745 svg_wakeline(we->time, from, to);
747 svg_partial_wakeline(we->time, from, task_from, to, task_to);
755 static void draw_cpu_usage(void)
758 struct per_pidcomm *c;
759 struct cpu_sample *sample;
766 if (sample->type == TYPE_RUNNING)
767 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
769 sample = sample->next;
777 static void draw_process_bars(void)
780 struct per_pidcomm *c;
781 struct cpu_sample *sample;
796 svg_box(Y, c->start_time, c->end_time, "process");
799 if (sample->type == TYPE_RUNNING)
800 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
801 if (sample->type == TYPE_BLOCKED)
802 svg_box(Y, sample->start_time, sample->end_time, "blocked");
803 if (sample->type == TYPE_WAITING)
804 svg_waiting(Y, sample->start_time, sample->end_time);
805 sample = sample->next;
810 if (c->total_time > 5000000000) /* 5 seconds */
811 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
813 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
815 svg_text(Y, c->start_time, comm);
825 static void add_process_filter(const char *string)
827 int pid = strtoull(string, NULL, 10);
828 struct process_filter *filt = malloc(sizeof(*filt));
833 filt->name = strdup(string);
835 filt->next = process_filter;
837 process_filter = filt;
840 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
842 struct process_filter *filt;
846 filt = process_filter;
848 if (filt->pid && p->pid == filt->pid)
850 if (strcmp(filt->name, c->comm) == 0)
857 static int determine_display_tasks_filtered(void)
860 struct per_pidcomm *c;
866 if (p->start_time == 1)
867 p->start_time = first_time;
869 /* no exit marker, task kept running to the end */
870 if (p->end_time == 0)
871 p->end_time = last_time;
878 if (c->start_time == 1)
879 c->start_time = first_time;
881 if (passes_filter(p, c)) {
887 if (c->end_time == 0)
888 c->end_time = last_time;
897 static int determine_display_tasks(u64 threshold)
900 struct per_pidcomm *c;
904 return determine_display_tasks_filtered();
909 if (p->start_time == 1)
910 p->start_time = first_time;
912 /* no exit marker, task kept running to the end */
913 if (p->end_time == 0)
914 p->end_time = last_time;
915 if (p->total_time >= threshold && !power_only)
923 if (c->start_time == 1)
924 c->start_time = first_time;
926 if (c->total_time >= threshold && !power_only) {
931 if (c->end_time == 0)
932 c->end_time = last_time;
943 #define TIME_THRESH 10000000
945 static void write_svg_file(const char *filename)
949 int thresh = TIME_THRESH;
954 /* We'd like to show at least proc_num tasks;
955 * be less picky if we have fewer */
957 count = determine_display_tasks(thresh);
959 } while (!process_filter && thresh && count < proc_num);
961 open_svg(filename, numcpus, count, first_time, last_time);
966 for (i = 0; i < numcpus; i++)
967 svg_cpu_box(i, max_freq, turbo_frequency);
977 static int __cmd_timechart(const char *output_name)
979 struct perf_tool perf_timechart = {
980 .comm = process_comm_event,
981 .fork = process_fork_event,
982 .exit = process_exit_event,
983 .sample = process_sample_event,
984 .ordered_samples = true,
986 const struct perf_evsel_str_handler power_tracepoints[] = {
987 { "power:cpu_idle", process_sample_cpu_idle },
988 { "power:cpu_frequency", process_sample_cpu_frequency },
989 { "sched:sched_wakeup", process_sample_sched_wakeup },
990 { "sched:sched_switch", process_sample_sched_switch },
991 #ifdef SUPPORT_OLD_POWER_EVENTS
992 { "power:power_start", process_sample_power_start },
993 { "power:power_end", process_sample_power_end },
994 { "power:power_frequency", process_sample_power_frequency },
997 struct perf_data_file file = {
999 .mode = PERF_DATA_MODE_READ,
1002 struct perf_session *session = perf_session__new(&file, false,
1006 if (session == NULL)
1009 if (!perf_session__has_traces(session, "timechart record"))
1012 if (perf_session__set_tracepoints_handlers(session,
1013 power_tracepoints)) {
1014 pr_err("Initializing session tracepoint handlers failed\n");
1018 ret = perf_session__process_events(session, &perf_timechart);
1022 end_sample_processing();
1026 write_svg_file(output_name);
1028 pr_info("Written %2.1f seconds of trace to %s.\n",
1029 (last_time - first_time) / 1000000000.0, output_name);
1031 perf_session__delete(session);
1035 static int __cmd_record(int argc, const char **argv)
1037 #ifdef SUPPORT_OLD_POWER_EVENTS
1038 const char * const record_old_args[] = {
1039 "record", "-a", "-R", "-c", "1",
1040 "-e", "power:power_start",
1041 "-e", "power:power_end",
1042 "-e", "power:power_frequency",
1043 "-e", "sched:sched_wakeup",
1044 "-e", "sched:sched_switch",
1047 const char * const record_new_args[] = {
1048 "record", "-a", "-R", "-c", "1",
1049 "-e", "power:cpu_frequency",
1050 "-e", "power:cpu_idle",
1051 "-e", "sched:sched_wakeup",
1052 "-e", "sched:sched_switch",
1054 unsigned int rec_argc, i, j;
1055 const char **rec_argv;
1056 const char * const *record_args = record_new_args;
1057 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1059 #ifdef SUPPORT_OLD_POWER_EVENTS
1060 if (!is_valid_tracepoint("power:cpu_idle") &&
1061 is_valid_tracepoint("power:power_start")) {
1062 use_old_power_events = 1;
1063 record_args = record_old_args;
1064 record_elems = ARRAY_SIZE(record_old_args);
1068 rec_argc = record_elems + argc - 1;
1069 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1071 if (rec_argv == NULL)
1074 for (i = 0; i < record_elems; i++)
1075 rec_argv[i] = strdup(record_args[i]);
1077 for (j = 1; j < (unsigned int)argc; j++, i++)
1078 rec_argv[i] = argv[j];
1080 return cmd_record(i, rec_argv, NULL);
1084 parse_process(const struct option *opt __maybe_unused, const char *arg,
1085 int __maybe_unused unset)
1088 add_process_filter(arg);
1092 int cmd_timechart(int argc, const char **argv,
1093 const char *prefix __maybe_unused)
1095 const char *output_name = "output.svg";
1096 const struct option options[] = {
1097 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1098 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1099 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1100 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
1101 OPT_CALLBACK('p', "process", NULL, "process",
1102 "process selector. Pass a pid or process name.",
1104 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1105 "Look for files with symbols relative to this directory"),
1106 OPT_INTEGER('n', "proc-num", &proc_num,
1107 "min. number of tasks to print"),
1110 const char * const timechart_usage[] = {
1111 "perf timechart [<options>] {record}",
1115 argc = parse_options(argc, argv, options, timechart_usage,
1116 PARSE_OPT_STOP_AT_NON_OPTION);
1120 if (argc && !strncmp(argv[0], "rec", 3))
1121 return __cmd_record(argc, argv);
1123 usage_with_options(timechart_usage, options);
1127 return __cmd_timechart(output_name);