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;
54 static bool tasks_only;
64 struct sample_wrapper;
67 * Datastructure layout:
68 * We keep an list of "pid"s, matching the kernels notion of a task struct.
69 * Each "pid" entry, has a list of "comm"s.
70 * this is because we want to track different programs different, while
71 * exec will reuse the original pid (by design).
72 * Each comm has a list of samples that will be used to draw
87 struct per_pidcomm *all;
88 struct per_pidcomm *current;
93 struct per_pidcomm *next;
107 struct cpu_sample *samples;
110 struct sample_wrapper {
111 struct sample_wrapper *next;
114 unsigned char data[0];
118 #define TYPE_RUNNING 1
119 #define TYPE_WAITING 2
120 #define TYPE_BLOCKED 3
123 struct cpu_sample *next;
131 static struct per_pid *all_data;
137 struct power_event *next;
146 struct wake_event *next;
152 static struct power_event *power_events;
153 static struct wake_event *wake_events;
155 struct process_filter;
156 struct process_filter {
159 struct process_filter *next;
162 static struct process_filter *process_filter;
165 static struct per_pid *find_create_pid(int pid)
167 struct per_pid *cursor = all_data;
170 if (cursor->pid == pid)
172 cursor = cursor->next;
174 cursor = zalloc(sizeof(*cursor));
175 assert(cursor != NULL);
177 cursor->next = all_data;
182 static void pid_set_comm(int pid, char *comm)
185 struct per_pidcomm *c;
186 p = find_create_pid(pid);
189 if (c->comm && strcmp(c->comm, comm) == 0) {
194 c->comm = strdup(comm);
200 c = zalloc(sizeof(*c));
202 c->comm = strdup(comm);
208 static void pid_fork(int pid, int ppid, u64 timestamp)
210 struct per_pid *p, *pp;
211 p = find_create_pid(pid);
212 pp = find_create_pid(ppid);
214 if (pp->current && pp->current->comm && !p->current)
215 pid_set_comm(pid, pp->current->comm);
217 p->start_time = timestamp;
219 p->current->start_time = timestamp;
220 p->current->state_since = timestamp;
224 static void pid_exit(int pid, u64 timestamp)
227 p = find_create_pid(pid);
228 p->end_time = timestamp;
230 p->current->end_time = timestamp;
234 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
237 struct per_pidcomm *c;
238 struct cpu_sample *sample;
240 p = find_create_pid(pid);
243 c = zalloc(sizeof(*c));
250 sample = zalloc(sizeof(*sample));
251 assert(sample != NULL);
252 sample->start_time = start;
253 sample->end_time = end;
255 sample->next = c->samples;
259 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
260 c->total_time += (end-start);
261 p->total_time += (end-start);
264 if (c->start_time == 0 || c->start_time > start)
265 c->start_time = start;
266 if (p->start_time == 0 || p->start_time > start)
267 p->start_time = start;
270 #define MAX_CPUS 4096
272 static u64 cpus_cstate_start_times[MAX_CPUS];
273 static int cpus_cstate_state[MAX_CPUS];
274 static u64 cpus_pstate_start_times[MAX_CPUS];
275 static u64 cpus_pstate_state[MAX_CPUS];
277 static int process_comm_event(struct perf_tool *tool __maybe_unused,
278 union perf_event *event,
279 struct perf_sample *sample __maybe_unused,
280 struct machine *machine __maybe_unused)
282 pid_set_comm(event->comm.tid, event->comm.comm);
286 static int process_fork_event(struct perf_tool *tool __maybe_unused,
287 union perf_event *event,
288 struct perf_sample *sample __maybe_unused,
289 struct machine *machine __maybe_unused)
291 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
295 static int process_exit_event(struct perf_tool *tool __maybe_unused,
296 union perf_event *event,
297 struct perf_sample *sample __maybe_unused,
298 struct machine *machine __maybe_unused)
300 pid_exit(event->fork.pid, event->fork.time);
307 unsigned char preempt_count;
312 #ifdef SUPPORT_OLD_POWER_EVENTS
313 static int use_old_power_events;
314 struct power_entry_old {
315 struct trace_entry te;
322 struct power_processor_entry {
323 struct trace_entry te;
328 #define TASK_COMM_LEN 16
329 struct wakeup_entry {
330 struct trace_entry te;
331 char comm[TASK_COMM_LEN];
337 struct sched_switch {
338 struct trace_entry te;
339 char prev_comm[TASK_COMM_LEN];
342 long prev_state; /* Arjan weeps. */
343 char next_comm[TASK_COMM_LEN];
348 static void c_state_start(int cpu, u64 timestamp, int state)
350 cpus_cstate_start_times[cpu] = timestamp;
351 cpus_cstate_state[cpu] = state;
354 static void c_state_end(int cpu, u64 timestamp)
356 struct power_event *pwr = zalloc(sizeof(*pwr));
361 pwr->state = cpus_cstate_state[cpu];
362 pwr->start_time = cpus_cstate_start_times[cpu];
363 pwr->end_time = timestamp;
366 pwr->next = power_events;
371 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
373 struct power_event *pwr;
375 if (new_freq > 8000000) /* detect invalid data */
378 pwr = zalloc(sizeof(*pwr));
382 pwr->state = cpus_pstate_state[cpu];
383 pwr->start_time = cpus_pstate_start_times[cpu];
384 pwr->end_time = timestamp;
387 pwr->next = power_events;
389 if (!pwr->start_time)
390 pwr->start_time = first_time;
394 cpus_pstate_state[cpu] = new_freq;
395 cpus_pstate_start_times[cpu] = timestamp;
397 if ((u64)new_freq > max_freq)
400 if (new_freq < min_freq || min_freq == 0)
403 if (new_freq == max_freq - 1000)
404 turbo_frequency = max_freq;
408 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
411 struct wakeup_entry *wake = (void *)te;
412 struct wake_event *we = zalloc(sizeof(*we));
417 we->time = timestamp;
420 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
423 we->wakee = wake->pid;
424 we->next = wake_events;
426 p = find_create_pid(we->wakee);
428 if (p && p->current && p->current->state == TYPE_NONE) {
429 p->current->state_since = timestamp;
430 p->current->state = TYPE_WAITING;
432 if (p && p->current && p->current->state == TYPE_BLOCKED) {
433 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
434 p->current->state_since = timestamp;
435 p->current->state = TYPE_WAITING;
439 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
441 struct per_pid *p = NULL, *prev_p;
442 struct sched_switch *sw = (void *)te;
445 prev_p = find_create_pid(sw->prev_pid);
447 p = find_create_pid(sw->next_pid);
449 if (prev_p->current && prev_p->current->state != TYPE_NONE)
450 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
451 if (p && p->current) {
452 if (p->current->state != TYPE_NONE)
453 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
455 p->current->state_since = timestamp;
456 p->current->state = TYPE_RUNNING;
459 if (prev_p->current) {
460 prev_p->current->state = TYPE_NONE;
461 prev_p->current->state_since = timestamp;
462 if (sw->prev_state & 2)
463 prev_p->current->state = TYPE_BLOCKED;
464 if (sw->prev_state == 0)
465 prev_p->current->state = TYPE_WAITING;
469 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
470 struct perf_sample *sample);
472 static int process_sample_event(struct perf_tool *tool __maybe_unused,
473 union perf_event *event __maybe_unused,
474 struct perf_sample *sample,
475 struct perf_evsel *evsel,
476 struct machine *machine __maybe_unused)
478 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
479 if (!first_time || first_time > sample->time)
480 first_time = sample->time;
481 if (last_time < sample->time)
482 last_time = sample->time;
485 if (sample->cpu > numcpus)
486 numcpus = sample->cpu;
488 if (evsel->handler != NULL) {
489 tracepoint_handler f = evsel->handler;
490 return f(evsel, sample);
497 process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
498 struct perf_sample *sample)
500 struct power_processor_entry *ppe = sample->raw_data;
502 if (ppe->state == (u32) PWR_EVENT_EXIT)
503 c_state_end(ppe->cpu_id, sample->time);
505 c_state_start(ppe->cpu_id, sample->time, ppe->state);
510 process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
511 struct perf_sample *sample)
513 struct power_processor_entry *ppe = sample->raw_data;
515 p_state_change(ppe->cpu_id, sample->time, ppe->state);
520 process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
521 struct perf_sample *sample)
523 struct trace_entry *te = sample->raw_data;
525 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
530 process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
531 struct perf_sample *sample)
533 struct trace_entry *te = sample->raw_data;
535 sched_switch(sample->cpu, sample->time, te);
539 #ifdef SUPPORT_OLD_POWER_EVENTS
541 process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
542 struct perf_sample *sample)
544 struct power_entry_old *peo = sample->raw_data;
546 c_state_start(peo->cpu_id, sample->time, peo->value);
551 process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
552 struct perf_sample *sample)
554 c_state_end(sample->cpu, sample->time);
559 process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
560 struct perf_sample *sample)
562 struct power_entry_old *peo = sample->raw_data;
564 p_state_change(peo->cpu_id, sample->time, peo->value);
567 #endif /* SUPPORT_OLD_POWER_EVENTS */
570 * After the last sample we need to wrap up the current C/P state
571 * and close out each CPU for these.
573 static void end_sample_processing(void)
576 struct power_event *pwr;
578 for (cpu = 0; cpu <= numcpus; cpu++) {
581 pwr = zalloc(sizeof(*pwr));
585 pwr->state = cpus_cstate_state[cpu];
586 pwr->start_time = cpus_cstate_start_times[cpu];
587 pwr->end_time = last_time;
590 pwr->next = power_events;
596 pwr = zalloc(sizeof(*pwr));
600 pwr->state = cpus_pstate_state[cpu];
601 pwr->start_time = cpus_pstate_start_times[cpu];
602 pwr->end_time = last_time;
605 pwr->next = power_events;
607 if (!pwr->start_time)
608 pwr->start_time = first_time;
610 pwr->state = min_freq;
616 * Sort the pid datastructure
618 static void sort_pids(void)
620 struct per_pid *new_list, *p, *cursor, *prev;
621 /* sort by ppid first, then by pid, lowest to highest */
630 if (new_list == NULL) {
638 if (cursor->ppid > p->ppid ||
639 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
640 /* must insert before */
642 p->next = prev->next;
655 cursor = cursor->next;
664 static void draw_c_p_states(void)
666 struct power_event *pwr;
670 * two pass drawing so that the P state bars are on top of the C state blocks
673 if (pwr->type == CSTATE)
674 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
680 if (pwr->type == PSTATE) {
682 pwr->state = min_freq;
683 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
689 static void draw_wakeups(void)
691 struct wake_event *we;
693 struct per_pidcomm *c;
697 int from = 0, to = 0;
698 char *task_from = NULL, *task_to = NULL;
700 /* locate the column of the waker and wakee */
703 if (p->pid == we->waker || p->pid == we->wakee) {
706 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
707 if (p->pid == we->waker && !from) {
709 task_from = strdup(c->comm);
711 if (p->pid == we->wakee && !to) {
713 task_to = strdup(c->comm);
720 if (p->pid == we->waker && !from) {
722 task_from = strdup(c->comm);
724 if (p->pid == we->wakee && !to) {
726 task_to = strdup(c->comm);
735 task_from = malloc(40);
736 sprintf(task_from, "[%i]", we->waker);
739 task_to = malloc(40);
740 sprintf(task_to, "[%i]", we->wakee);
744 svg_interrupt(we->time, to);
745 else if (from && to && abs(from - to) == 1)
746 svg_wakeline(we->time, from, to);
748 svg_partial_wakeline(we->time, from, task_from, to, task_to);
756 static void draw_cpu_usage(void)
759 struct per_pidcomm *c;
760 struct cpu_sample *sample;
767 if (sample->type == TYPE_RUNNING)
768 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
770 sample = sample->next;
778 static void draw_process_bars(void)
781 struct per_pidcomm *c;
782 struct cpu_sample *sample;
797 svg_box(Y, c->start_time, c->end_time, "process");
800 if (sample->type == TYPE_RUNNING)
801 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
802 if (sample->type == TYPE_BLOCKED)
803 svg_box(Y, sample->start_time, sample->end_time, "blocked");
804 if (sample->type == TYPE_WAITING)
805 svg_waiting(Y, sample->start_time, sample->end_time);
806 sample = sample->next;
811 if (c->total_time > 5000000000) /* 5 seconds */
812 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
814 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
816 svg_text(Y, c->start_time, comm);
826 static void add_process_filter(const char *string)
828 int pid = strtoull(string, NULL, 10);
829 struct process_filter *filt = malloc(sizeof(*filt));
834 filt->name = strdup(string);
836 filt->next = process_filter;
838 process_filter = filt;
841 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
843 struct process_filter *filt;
847 filt = process_filter;
849 if (filt->pid && p->pid == filt->pid)
851 if (strcmp(filt->name, c->comm) == 0)
858 static int determine_display_tasks_filtered(void)
861 struct per_pidcomm *c;
867 if (p->start_time == 1)
868 p->start_time = first_time;
870 /* no exit marker, task kept running to the end */
871 if (p->end_time == 0)
872 p->end_time = last_time;
879 if (c->start_time == 1)
880 c->start_time = first_time;
882 if (passes_filter(p, c)) {
888 if (c->end_time == 0)
889 c->end_time = last_time;
898 static int determine_display_tasks(u64 threshold)
901 struct per_pidcomm *c;
905 return determine_display_tasks_filtered();
910 if (p->start_time == 1)
911 p->start_time = first_time;
913 /* no exit marker, task kept running to the end */
914 if (p->end_time == 0)
915 p->end_time = last_time;
916 if (p->total_time >= threshold)
924 if (c->start_time == 1)
925 c->start_time = first_time;
927 if (c->total_time >= threshold) {
932 if (c->end_time == 0)
933 c->end_time = last_time;
944 #define TIME_THRESH 10000000
946 static void write_svg_file(const char *filename)
950 int thresh = TIME_THRESH;
957 /* We'd like to show at least proc_num tasks;
958 * be less picky if we have fewer */
960 count = determine_display_tasks(thresh);
962 } while (!process_filter && thresh && count < proc_num);
964 open_svg(filename, numcpus, count, first_time, last_time);
969 for (i = 0; i < numcpus; i++)
970 svg_cpu_box(i, max_freq, turbo_frequency);
983 static int __cmd_timechart(const char *output_name)
985 struct perf_tool perf_timechart = {
986 .comm = process_comm_event,
987 .fork = process_fork_event,
988 .exit = process_exit_event,
989 .sample = process_sample_event,
990 .ordered_samples = true,
992 const struct perf_evsel_str_handler power_tracepoints[] = {
993 { "power:cpu_idle", process_sample_cpu_idle },
994 { "power:cpu_frequency", process_sample_cpu_frequency },
995 { "sched:sched_wakeup", process_sample_sched_wakeup },
996 { "sched:sched_switch", process_sample_sched_switch },
997 #ifdef SUPPORT_OLD_POWER_EVENTS
998 { "power:power_start", process_sample_power_start },
999 { "power:power_end", process_sample_power_end },
1000 { "power:power_frequency", process_sample_power_frequency },
1003 struct perf_data_file file = {
1005 .mode = PERF_DATA_MODE_READ,
1008 struct perf_session *session = perf_session__new(&file, false,
1012 if (session == NULL)
1015 if (!perf_session__has_traces(session, "timechart record"))
1018 if (perf_session__set_tracepoints_handlers(session,
1019 power_tracepoints)) {
1020 pr_err("Initializing session tracepoint handlers failed\n");
1024 ret = perf_session__process_events(session, &perf_timechart);
1028 end_sample_processing();
1032 write_svg_file(output_name);
1034 pr_info("Written %2.1f seconds of trace to %s.\n",
1035 (last_time - first_time) / 1000000000.0, output_name);
1037 perf_session__delete(session);
1041 static int __cmd_record(int argc, const char **argv)
1043 #ifdef SUPPORT_OLD_POWER_EVENTS
1044 const char * const record_old_args[] = {
1045 "record", "-a", "-R", "-c", "1",
1046 "-e", "power:power_start",
1047 "-e", "power:power_end",
1048 "-e", "power:power_frequency",
1049 "-e", "sched:sched_wakeup",
1050 "-e", "sched:sched_switch",
1053 const char * const record_new_args[] = {
1054 "record", "-a", "-R", "-c", "1",
1055 "-e", "power:cpu_frequency",
1056 "-e", "power:cpu_idle",
1057 "-e", "sched:sched_wakeup",
1058 "-e", "sched:sched_switch",
1060 unsigned int rec_argc, i, j;
1061 const char **rec_argv;
1062 const char * const *record_args = record_new_args;
1063 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1065 #ifdef SUPPORT_OLD_POWER_EVENTS
1066 if (!is_valid_tracepoint("power:cpu_idle") &&
1067 is_valid_tracepoint("power:power_start")) {
1068 use_old_power_events = 1;
1069 record_args = record_old_args;
1070 record_elems = ARRAY_SIZE(record_old_args);
1074 rec_argc = record_elems + argc - 1;
1075 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1077 if (rec_argv == NULL)
1080 for (i = 0; i < record_elems; i++)
1081 rec_argv[i] = strdup(record_args[i]);
1083 for (j = 1; j < (unsigned int)argc; j++, i++)
1084 rec_argv[i] = argv[j];
1086 return cmd_record(i, rec_argv, NULL);
1090 parse_process(const struct option *opt __maybe_unused, const char *arg,
1091 int __maybe_unused unset)
1094 add_process_filter(arg);
1098 int cmd_timechart(int argc, const char **argv,
1099 const char *prefix __maybe_unused)
1101 const char *output_name = "output.svg";
1102 const struct option options[] = {
1103 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1104 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1105 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1106 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
1107 OPT_BOOLEAN('T', "tasks-only", &tasks_only,
1108 "output processes data only"),
1109 OPT_CALLBACK('p', "process", NULL, "process",
1110 "process selector. Pass a pid or process name.",
1112 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1113 "Look for files with symbols relative to this directory"),
1114 OPT_INTEGER('n', "proc-num", &proc_num,
1115 "min. number of tasks to print"),
1118 const char * const timechart_usage[] = {
1119 "perf timechart [<options>] {record}",
1123 argc = parse_options(argc, argv, options, timechart_usage,
1124 PARSE_OPT_STOP_AT_NON_OPTION);
1126 if (power_only && tasks_only) {
1127 pr_err("-P and -T options cannot be used at the same time.\n");
1133 if (argc && !strncmp(argv[0], "rec", 3))
1134 return __cmd_record(argc, argv);
1136 usage_with_options(timechart_usage, options);
1140 return __cmd_timechart(output_name);