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"
36 #define SUPPORT_OLD_POWER_EVENTS 1
37 #define PWR_EVENT_EXIT -1
40 static char const *input_name = "perf.data";
41 static char const *output_name = "output.svg";
43 static unsigned int numcpus;
44 static u64 min_freq; /* Lowest CPU frequency seen */
45 static u64 max_freq; /* Highest CPU frequency seen */
46 static u64 turbo_frequency;
48 static u64 first_time, last_time;
50 static bool power_only;
60 struct sample_wrapper;
63 * Datastructure layout:
64 * We keep an list of "pid"s, matching the kernels notion of a task struct.
65 * Each "pid" entry, has a list of "comm"s.
66 * this is because we want to track different programs different, while
67 * exec will reuse the original pid (by design).
68 * Each comm has a list of samples that will be used to draw
83 struct per_pidcomm *all;
84 struct per_pidcomm *current;
89 struct per_pidcomm *next;
103 struct cpu_sample *samples;
106 struct sample_wrapper {
107 struct sample_wrapper *next;
110 unsigned char data[0];
114 #define TYPE_RUNNING 1
115 #define TYPE_WAITING 2
116 #define TYPE_BLOCKED 3
119 struct cpu_sample *next;
127 static struct per_pid *all_data;
133 struct power_event *next;
142 struct wake_event *next;
148 static struct power_event *power_events;
149 static struct wake_event *wake_events;
151 struct process_filter;
152 struct process_filter {
155 struct process_filter *next;
158 static struct process_filter *process_filter;
161 static struct per_pid *find_create_pid(int pid)
163 struct per_pid *cursor = all_data;
166 if (cursor->pid == pid)
168 cursor = cursor->next;
170 cursor = malloc(sizeof(struct per_pid));
171 assert(cursor != NULL);
172 memset(cursor, 0, sizeof(struct per_pid));
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 = malloc(sizeof(struct per_pidcomm));
199 memset(c, 0, sizeof(struct per_pidcomm));
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 = malloc(sizeof(struct per_pidcomm));
243 memset(c, 0, sizeof(struct per_pidcomm));
249 sample = malloc(sizeof(struct cpu_sample));
250 assert(sample != NULL);
251 memset(sample, 0, sizeof(struct cpu_sample));
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_event_ops *ops __used,
278 union perf_event *event,
279 struct perf_sample *sample __used,
280 struct perf_session *session __used)
282 pid_set_comm(event->comm.tid, event->comm.comm);
286 static int process_fork_event(struct perf_event_ops *ops __used,
287 union perf_event *event,
288 struct perf_sample *sample __used,
289 struct perf_session *session __used)
291 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
295 static int process_exit_event(struct perf_event_ops *ops __used,
296 union perf_event *event,
297 struct perf_sample *sample __used,
298 struct perf_session *session __used)
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];
338 * trace_flag_type is an enumeration that holds different
339 * states when a trace occurs. These are:
340 * IRQS_OFF - interrupts were disabled
341 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
342 * NEED_RESCED - reschedule is requested
343 * HARDIRQ - inside an interrupt handler
344 * SOFTIRQ - inside a softirq handler
346 enum trace_flag_type {
347 TRACE_FLAG_IRQS_OFF = 0x01,
348 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
349 TRACE_FLAG_NEED_RESCHED = 0x04,
350 TRACE_FLAG_HARDIRQ = 0x08,
351 TRACE_FLAG_SOFTIRQ = 0x10,
356 struct sched_switch {
357 struct trace_entry te;
358 char prev_comm[TASK_COMM_LEN];
361 long prev_state; /* Arjan weeps. */
362 char next_comm[TASK_COMM_LEN];
367 static void c_state_start(int cpu, u64 timestamp, int state)
369 cpus_cstate_start_times[cpu] = timestamp;
370 cpus_cstate_state[cpu] = state;
373 static void c_state_end(int cpu, u64 timestamp)
375 struct power_event *pwr;
376 pwr = malloc(sizeof(struct power_event));
379 memset(pwr, 0, sizeof(struct power_event));
381 pwr->state = cpus_cstate_state[cpu];
382 pwr->start_time = cpus_cstate_start_times[cpu];
383 pwr->end_time = timestamp;
386 pwr->next = power_events;
391 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
393 struct power_event *pwr;
394 pwr = malloc(sizeof(struct power_event));
396 if (new_freq > 8000000) /* detect invalid data */
401 memset(pwr, 0, sizeof(struct power_event));
403 pwr->state = cpus_pstate_state[cpu];
404 pwr->start_time = cpus_pstate_start_times[cpu];
405 pwr->end_time = timestamp;
408 pwr->next = power_events;
410 if (!pwr->start_time)
411 pwr->start_time = first_time;
415 cpus_pstate_state[cpu] = new_freq;
416 cpus_pstate_start_times[cpu] = timestamp;
418 if ((u64)new_freq > max_freq)
421 if (new_freq < min_freq || min_freq == 0)
424 if (new_freq == max_freq - 1000)
425 turbo_frequency = max_freq;
429 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
431 struct wake_event *we;
433 struct wakeup_entry *wake = (void *)te;
435 we = malloc(sizeof(struct wake_event));
439 memset(we, 0, sizeof(struct wake_event));
440 we->time = timestamp;
443 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
446 we->wakee = wake->pid;
447 we->next = wake_events;
449 p = find_create_pid(we->wakee);
451 if (p && p->current && p->current->state == TYPE_NONE) {
452 p->current->state_since = timestamp;
453 p->current->state = TYPE_WAITING;
455 if (p && p->current && p->current->state == TYPE_BLOCKED) {
456 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
457 p->current->state_since = timestamp;
458 p->current->state = TYPE_WAITING;
462 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
464 struct per_pid *p = NULL, *prev_p;
465 struct sched_switch *sw = (void *)te;
468 prev_p = find_create_pid(sw->prev_pid);
470 p = find_create_pid(sw->next_pid);
472 if (prev_p->current && prev_p->current->state != TYPE_NONE)
473 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
474 if (p && p->current) {
475 if (p->current->state != TYPE_NONE)
476 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
478 p->current->state_since = timestamp;
479 p->current->state = TYPE_RUNNING;
482 if (prev_p->current) {
483 prev_p->current->state = TYPE_NONE;
484 prev_p->current->state_since = timestamp;
485 if (sw->prev_state & 2)
486 prev_p->current->state = TYPE_BLOCKED;
487 if (sw->prev_state == 0)
488 prev_p->current->state = TYPE_WAITING;
493 static int process_sample_event(struct perf_event_ops *ops __used,
494 union perf_event *event __used,
495 struct perf_sample *sample,
496 struct perf_evsel *evsel,
497 struct perf_session *session __used)
499 struct trace_entry *te;
501 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
502 if (!first_time || first_time > sample->time)
503 first_time = sample->time;
504 if (last_time < sample->time)
505 last_time = sample->time;
508 te = (void *)sample->raw_data;
509 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
511 #ifdef SUPPORT_OLD_POWER_EVENTS
512 struct power_entry_old *peo;
516 * FIXME: use evsel, its already mapped from id to perf_evsel,
517 * remove perf_header__find_event infrastructure bits.
518 * Mapping all these "power:cpu_idle" strings to the tracepoint
519 * ID and then just comparing against evsel->attr.config.
523 * if (evsel->attr.config == power_cpu_idle_id)
525 event_str = perf_header__find_event(te->type);
530 if (sample->cpu > numcpus)
531 numcpus = sample->cpu;
533 if (strcmp(event_str, "power:cpu_idle") == 0) {
534 struct power_processor_entry *ppe = (void *)te;
535 if (ppe->state == (u32)PWR_EVENT_EXIT)
536 c_state_end(ppe->cpu_id, sample->time);
538 c_state_start(ppe->cpu_id, sample->time,
541 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
542 struct power_processor_entry *ppe = (void *)te;
543 p_state_change(ppe->cpu_id, sample->time, ppe->state);
546 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
547 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
549 else if (strcmp(event_str, "sched:sched_switch") == 0)
550 sched_switch(sample->cpu, sample->time, te);
552 #ifdef SUPPORT_OLD_POWER_EVENTS
553 if (use_old_power_events) {
554 if (strcmp(event_str, "power:power_start") == 0)
555 c_state_start(peo->cpu_id, sample->time,
558 else if (strcmp(event_str, "power:power_end") == 0)
559 c_state_end(sample->cpu, sample->time);
561 else if (strcmp(event_str,
562 "power:power_frequency") == 0)
563 p_state_change(peo->cpu_id, sample->time,
572 * After the last sample we need to wrap up the current C/P state
573 * and close out each CPU for these.
575 static void end_sample_processing(void)
578 struct power_event *pwr;
580 for (cpu = 0; cpu <= numcpus; cpu++) {
581 pwr = malloc(sizeof(struct power_event));
584 memset(pwr, 0, sizeof(struct power_event));
588 pwr->state = cpus_cstate_state[cpu];
589 pwr->start_time = cpus_cstate_start_times[cpu];
590 pwr->end_time = last_time;
593 pwr->next = power_events;
599 pwr = malloc(sizeof(struct power_event));
602 memset(pwr, 0, sizeof(struct power_event));
604 pwr->state = cpus_pstate_state[cpu];
605 pwr->start_time = cpus_pstate_start_times[cpu];
606 pwr->end_time = last_time;
609 pwr->next = power_events;
611 if (!pwr->start_time)
612 pwr->start_time = first_time;
614 pwr->state = min_freq;
620 * Sort the pid datastructure
622 static void sort_pids(void)
624 struct per_pid *new_list, *p, *cursor, *prev;
625 /* sort by ppid first, then by pid, lowest to highest */
634 if (new_list == NULL) {
642 if (cursor->ppid > p->ppid ||
643 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
644 /* must insert before */
646 p->next = prev->next;
659 cursor = cursor->next;
668 static void draw_c_p_states(void)
670 struct power_event *pwr;
674 * two pass drawing so that the P state bars are on top of the C state blocks
677 if (pwr->type == CSTATE)
678 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
684 if (pwr->type == PSTATE) {
686 pwr->state = min_freq;
687 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
693 static void draw_wakeups(void)
695 struct wake_event *we;
697 struct per_pidcomm *c;
701 int from = 0, to = 0;
702 char *task_from = NULL, *task_to = NULL;
704 /* locate the column of the waker and wakee */
707 if (p->pid == we->waker || p->pid == we->wakee) {
710 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
711 if (p->pid == we->waker && !from) {
713 task_from = strdup(c->comm);
715 if (p->pid == we->wakee && !to) {
717 task_to = strdup(c->comm);
724 if (p->pid == we->waker && !from) {
726 task_from = strdup(c->comm);
728 if (p->pid == we->wakee && !to) {
730 task_to = strdup(c->comm);
739 task_from = malloc(40);
740 sprintf(task_from, "[%i]", we->waker);
743 task_to = malloc(40);
744 sprintf(task_to, "[%i]", we->wakee);
748 svg_interrupt(we->time, to);
749 else if (from && to && abs(from - to) == 1)
750 svg_wakeline(we->time, from, to);
752 svg_partial_wakeline(we->time, from, task_from, to, task_to);
760 static void draw_cpu_usage(void)
763 struct per_pidcomm *c;
764 struct cpu_sample *sample;
771 if (sample->type == TYPE_RUNNING)
772 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
774 sample = sample->next;
782 static void draw_process_bars(void)
785 struct per_pidcomm *c;
786 struct cpu_sample *sample;
801 svg_box(Y, c->start_time, c->end_time, "process");
804 if (sample->type == TYPE_RUNNING)
805 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
806 if (sample->type == TYPE_BLOCKED)
807 svg_box(Y, sample->start_time, sample->end_time, "blocked");
808 if (sample->type == TYPE_WAITING)
809 svg_waiting(Y, sample->start_time, sample->end_time);
810 sample = sample->next;
815 if (c->total_time > 5000000000) /* 5 seconds */
816 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
818 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
820 svg_text(Y, c->start_time, comm);
830 static void add_process_filter(const char *string)
832 struct process_filter *filt;
835 pid = strtoull(string, NULL, 10);
836 filt = malloc(sizeof(struct process_filter));
840 filt->name = strdup(string);
842 filt->next = process_filter;
844 process_filter = filt;
847 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
849 struct process_filter *filt;
853 filt = process_filter;
855 if (filt->pid && p->pid == filt->pid)
857 if (strcmp(filt->name, c->comm) == 0)
864 static int determine_display_tasks_filtered(void)
867 struct per_pidcomm *c;
873 if (p->start_time == 1)
874 p->start_time = first_time;
876 /* no exit marker, task kept running to the end */
877 if (p->end_time == 0)
878 p->end_time = last_time;
885 if (c->start_time == 1)
886 c->start_time = first_time;
888 if (passes_filter(p, c)) {
894 if (c->end_time == 0)
895 c->end_time = last_time;
904 static int determine_display_tasks(u64 threshold)
907 struct per_pidcomm *c;
911 return determine_display_tasks_filtered();
916 if (p->start_time == 1)
917 p->start_time = first_time;
919 /* no exit marker, task kept running to the end */
920 if (p->end_time == 0)
921 p->end_time = last_time;
922 if (p->total_time >= threshold && !power_only)
930 if (c->start_time == 1)
931 c->start_time = first_time;
933 if (c->total_time >= threshold && !power_only) {
938 if (c->end_time == 0)
939 c->end_time = last_time;
950 #define TIME_THRESH 10000000
952 static void write_svg_file(const char *filename)
960 count = determine_display_tasks(TIME_THRESH);
962 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
964 count = determine_display_tasks(TIME_THRESH / 10);
966 open_svg(filename, numcpus, count, first_time, last_time);
971 for (i = 0; i < numcpus; i++)
972 svg_cpu_box(i, max_freq, turbo_frequency);
982 static struct perf_event_ops event_ops = {
983 .comm = process_comm_event,
984 .fork = process_fork_event,
985 .exit = process_exit_event,
986 .sample = process_sample_event,
987 .ordered_samples = true,
990 static int __cmd_timechart(void)
992 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
993 0, false, &event_ops);
999 if (!perf_session__has_traces(session, "timechart record"))
1002 ret = perf_session__process_events(session, &event_ops);
1006 end_sample_processing();
1010 write_svg_file(output_name);
1012 pr_info("Written %2.1f seconds of trace to %s.\n",
1013 (last_time - first_time) / 1000000000.0, output_name);
1015 perf_session__delete(session);
1019 static const char * const timechart_usage[] = {
1020 "perf timechart [<options>] {record}",
1024 #ifdef SUPPORT_OLD_POWER_EVENTS
1025 static const char * const record_old_args[] = {
1031 "-e", "power:power_start",
1032 "-e", "power:power_end",
1033 "-e", "power:power_frequency",
1034 "-e", "sched:sched_wakeup",
1035 "-e", "sched:sched_switch",
1039 static const char * const record_new_args[] = {
1045 "-e", "power:cpu_frequency",
1046 "-e", "power:cpu_idle",
1047 "-e", "sched:sched_wakeup",
1048 "-e", "sched:sched_switch",
1051 static int __cmd_record(int argc, const char **argv)
1053 unsigned int rec_argc, i, j;
1054 const char **rec_argv;
1055 const char * const *record_args = record_new_args;
1056 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1058 #ifdef SUPPORT_OLD_POWER_EVENTS
1059 if (!is_valid_tracepoint("power:cpu_idle") &&
1060 is_valid_tracepoint("power:power_start")) {
1061 use_old_power_events = 1;
1062 record_args = record_old_args;
1063 record_elems = ARRAY_SIZE(record_old_args);
1067 rec_argc = record_elems + argc - 1;
1068 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1070 if (rec_argv == NULL)
1073 for (i = 0; i < record_elems; i++)
1074 rec_argv[i] = strdup(record_args[i]);
1076 for (j = 1; j < (unsigned int)argc; j++, i++)
1077 rec_argv[i] = argv[j];
1079 return cmd_record(i, rec_argv, NULL);
1083 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1086 add_process_filter(arg);
1090 static const struct option options[] = {
1091 OPT_STRING('i', "input", &input_name, "file",
1093 OPT_STRING('o', "output", &output_name, "file",
1094 "output file name"),
1095 OPT_INTEGER('w', "width", &svg_page_width,
1097 OPT_BOOLEAN('P', "power-only", &power_only,
1098 "output power data only"),
1099 OPT_CALLBACK('p', "process", NULL, "process",
1100 "process selector. Pass a pid or process name.",
1102 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1103 "Look for files with symbols relative to this directory"),
1108 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
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();