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(union perf_event *event,
278 struct perf_sample *sample __used,
279 struct perf_session *session __used)
281 pid_set_comm(event->comm.tid, event->comm.comm);
285 static int process_fork_event(union perf_event *event,
286 struct perf_sample *sample __used,
287 struct perf_session *session __used)
289 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
293 static int process_exit_event(union perf_event *event,
294 struct perf_sample *sample __used,
295 struct perf_session *session __used)
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;
373 pwr = malloc(sizeof(struct power_event));
376 memset(pwr, 0, sizeof(struct power_event));
378 pwr->state = cpus_cstate_state[cpu];
379 pwr->start_time = cpus_cstate_start_times[cpu];
380 pwr->end_time = timestamp;
383 pwr->next = power_events;
388 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
390 struct power_event *pwr;
391 pwr = malloc(sizeof(struct power_event));
393 if (new_freq > 8000000) /* detect invalid data */
398 memset(pwr, 0, sizeof(struct power_event));
400 pwr->state = cpus_pstate_state[cpu];
401 pwr->start_time = cpus_pstate_start_times[cpu];
402 pwr->end_time = timestamp;
405 pwr->next = power_events;
407 if (!pwr->start_time)
408 pwr->start_time = first_time;
412 cpus_pstate_state[cpu] = new_freq;
413 cpus_pstate_start_times[cpu] = timestamp;
415 if ((u64)new_freq > max_freq)
418 if (new_freq < min_freq || min_freq == 0)
421 if (new_freq == max_freq - 1000)
422 turbo_frequency = max_freq;
426 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
428 struct wake_event *we;
430 struct wakeup_entry *wake = (void *)te;
432 we = malloc(sizeof(struct wake_event));
436 memset(we, 0, sizeof(struct wake_event));
437 we->time = timestamp;
440 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
443 we->wakee = wake->pid;
444 we->next = wake_events;
446 p = find_create_pid(we->wakee);
448 if (p && p->current && p->current->state == TYPE_NONE) {
449 p->current->state_since = timestamp;
450 p->current->state = TYPE_WAITING;
452 if (p && p->current && p->current->state == TYPE_BLOCKED) {
453 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
454 p->current->state_since = timestamp;
455 p->current->state = TYPE_WAITING;
459 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
461 struct per_pid *p = NULL, *prev_p;
462 struct sched_switch *sw = (void *)te;
465 prev_p = find_create_pid(sw->prev_pid);
467 p = find_create_pid(sw->next_pid);
469 if (prev_p->current && prev_p->current->state != TYPE_NONE)
470 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
471 if (p && p->current) {
472 if (p->current->state != TYPE_NONE)
473 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
475 p->current->state_since = timestamp;
476 p->current->state = TYPE_RUNNING;
479 if (prev_p->current) {
480 prev_p->current->state = TYPE_NONE;
481 prev_p->current->state_since = timestamp;
482 if (sw->prev_state & 2)
483 prev_p->current->state = TYPE_BLOCKED;
484 if (sw->prev_state == 0)
485 prev_p->current->state = TYPE_WAITING;
490 static int process_sample_event(union perf_event *event __used,
491 struct perf_sample *sample,
492 struct perf_evsel *evsel,
493 struct perf_session *session __used)
495 struct trace_entry *te;
497 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
498 if (!first_time || first_time > sample->time)
499 first_time = sample->time;
500 if (last_time < sample->time)
501 last_time = sample->time;
504 te = (void *)sample->raw_data;
505 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
507 #ifdef SUPPORT_OLD_POWER_EVENTS
508 struct power_entry_old *peo;
512 * FIXME: use evsel, its already mapped from id to perf_evsel,
513 * remove perf_header__find_event infrastructure bits.
514 * Mapping all these "power:cpu_idle" strings to the tracepoint
515 * ID and then just comparing against evsel->attr.config.
519 * if (evsel->attr.config == power_cpu_idle_id)
521 event_str = perf_header__find_event(te->type);
526 if (sample->cpu > numcpus)
527 numcpus = sample->cpu;
529 if (strcmp(event_str, "power:cpu_idle") == 0) {
530 struct power_processor_entry *ppe = (void *)te;
531 if (ppe->state == (u32)PWR_EVENT_EXIT)
532 c_state_end(ppe->cpu_id, sample->time);
534 c_state_start(ppe->cpu_id, sample->time,
537 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
538 struct power_processor_entry *ppe = (void *)te;
539 p_state_change(ppe->cpu_id, sample->time, ppe->state);
542 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
543 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
545 else if (strcmp(event_str, "sched:sched_switch") == 0)
546 sched_switch(sample->cpu, sample->time, te);
548 #ifdef SUPPORT_OLD_POWER_EVENTS
549 if (use_old_power_events) {
550 if (strcmp(event_str, "power:power_start") == 0)
551 c_state_start(peo->cpu_id, sample->time,
554 else if (strcmp(event_str, "power:power_end") == 0)
555 c_state_end(sample->cpu, sample->time);
557 else if (strcmp(event_str,
558 "power:power_frequency") == 0)
559 p_state_change(peo->cpu_id, sample->time,
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++) {
577 pwr = malloc(sizeof(struct power_event));
580 memset(pwr, 0, sizeof(struct power_event));
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 = malloc(sizeof(struct power_event));
598 memset(pwr, 0, sizeof(struct power_event));
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 struct process_filter *filt;
831 pid = strtoull(string, NULL, 10);
832 filt = malloc(sizeof(struct process_filter));
836 filt->name = strdup(string);
838 filt->next = process_filter;
840 process_filter = filt;
843 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
845 struct process_filter *filt;
849 filt = process_filter;
851 if (filt->pid && p->pid == filt->pid)
853 if (strcmp(filt->name, c->comm) == 0)
860 static int determine_display_tasks_filtered(void)
863 struct per_pidcomm *c;
869 if (p->start_time == 1)
870 p->start_time = first_time;
872 /* no exit marker, task kept running to the end */
873 if (p->end_time == 0)
874 p->end_time = last_time;
881 if (c->start_time == 1)
882 c->start_time = first_time;
884 if (passes_filter(p, c)) {
890 if (c->end_time == 0)
891 c->end_time = last_time;
900 static int determine_display_tasks(u64 threshold)
903 struct per_pidcomm *c;
907 return determine_display_tasks_filtered();
912 if (p->start_time == 1)
913 p->start_time = first_time;
915 /* no exit marker, task kept running to the end */
916 if (p->end_time == 0)
917 p->end_time = last_time;
918 if (p->total_time >= threshold && !power_only)
926 if (c->start_time == 1)
927 c->start_time = first_time;
929 if (c->total_time >= threshold && !power_only) {
934 if (c->end_time == 0)
935 c->end_time = last_time;
946 #define TIME_THRESH 10000000
948 static void write_svg_file(const char *filename)
956 count = determine_display_tasks(TIME_THRESH);
958 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
960 count = determine_display_tasks(TIME_THRESH / 10);
962 open_svg(filename, numcpus, count, first_time, last_time);
967 for (i = 0; i < numcpus; i++)
968 svg_cpu_box(i, max_freq, turbo_frequency);
978 static struct perf_event_ops event_ops = {
979 .comm = process_comm_event,
980 .fork = process_fork_event,
981 .exit = process_exit_event,
982 .sample = process_sample_event,
983 .ordered_samples = true,
986 static int __cmd_timechart(void)
988 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
989 0, false, &event_ops);
995 if (!perf_session__has_traces(session, "timechart record"))
998 ret = perf_session__process_events(session, &event_ops);
1002 end_sample_processing();
1006 write_svg_file(output_name);
1008 pr_info("Written %2.1f seconds of trace to %s.\n",
1009 (last_time - first_time) / 1000000000.0, output_name);
1011 perf_session__delete(session);
1015 static const char * const timechart_usage[] = {
1016 "perf timechart [<options>] {record}",
1020 #ifdef SUPPORT_OLD_POWER_EVENTS
1021 static const char * const record_old_args[] = {
1027 "-e", "power:power_start",
1028 "-e", "power:power_end",
1029 "-e", "power:power_frequency",
1030 "-e", "sched:sched_wakeup",
1031 "-e", "sched:sched_switch",
1035 static const char * const record_new_args[] = {
1041 "-e", "power:cpu_frequency",
1042 "-e", "power:cpu_idle",
1043 "-e", "sched:sched_wakeup",
1044 "-e", "sched:sched_switch",
1047 static int __cmd_record(int argc, const char **argv)
1049 unsigned int rec_argc, i, j;
1050 const char **rec_argv;
1051 const char * const *record_args = record_new_args;
1052 unsigned int record_elems = ARRAY_SIZE(record_new_args);
1054 #ifdef SUPPORT_OLD_POWER_EVENTS
1055 if (!is_valid_tracepoint("power:cpu_idle") &&
1056 is_valid_tracepoint("power:power_start")) {
1057 use_old_power_events = 1;
1058 record_args = record_old_args;
1059 record_elems = ARRAY_SIZE(record_old_args);
1063 rec_argc = record_elems + argc - 1;
1064 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1066 if (rec_argv == NULL)
1069 for (i = 0; i < record_elems; i++)
1070 rec_argv[i] = strdup(record_args[i]);
1072 for (j = 1; j < (unsigned int)argc; j++, i++)
1073 rec_argv[i] = argv[j];
1075 return cmd_record(i, rec_argv, NULL);
1079 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1082 add_process_filter(arg);
1086 static const struct option options[] = {
1087 OPT_STRING('i', "input", &input_name, "file",
1089 OPT_STRING('o', "output", &output_name, "file",
1090 "output file name"),
1091 OPT_INTEGER('w', "width", &svg_page_width,
1093 OPT_BOOLEAN('P', "power-only", &power_only,
1094 "output power data only"),
1095 OPT_CALLBACK('p', "process", NULL, "process",
1096 "process selector. Pass a pid or process name.",
1098 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1099 "Look for files with symbols relative to this directory"),
1104 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1106 argc = parse_options(argc, argv, options, timechart_usage,
1107 PARSE_OPT_STOP_AT_NON_OPTION);
1111 if (argc && !strncmp(argv[0], "rec", 3))
1112 return __cmd_record(argc, argv);
1114 usage_with_options(timechart_usage, options);
1118 return __cmd_timechart();