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 <linux/rbtree.h>
23 #include "util/symbol.h"
24 #include "util/string.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 static char const *input_name = "perf.data";
37 static char const *output_name = "output.svg";
40 static u64 sample_type;
42 static unsigned int numcpus;
43 static u64 min_freq; /* Lowest CPU frequency seen */
44 static u64 max_freq; /* Highest CPU frequency seen */
45 static u64 turbo_frequency;
47 static u64 first_time, last_time;
49 static int power_only;
59 struct sample_wrapper;
62 * Datastructure layout:
63 * We keep an list of "pid"s, matching the kernels notion of a task struct.
64 * Each "pid" entry, has a list of "comm"s.
65 * this is because we want to track different programs different, while
66 * exec will reuse the original pid (by design).
67 * Each comm has a list of samples that will be used to draw
82 struct per_pidcomm *all;
83 struct per_pidcomm *current;
90 struct per_pidcomm *next;
104 struct cpu_sample *samples;
107 struct sample_wrapper {
108 struct sample_wrapper *next;
111 unsigned char data[0];
115 #define TYPE_RUNNING 1
116 #define TYPE_WAITING 2
117 #define TYPE_BLOCKED 3
120 struct cpu_sample *next;
128 static struct per_pid *all_data;
134 struct power_event *next;
143 struct wake_event *next;
149 static struct power_event *power_events;
150 static struct wake_event *wake_events;
152 struct sample_wrapper *all_samples;
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 = malloc(sizeof(struct per_pid));
175 assert(cursor != NULL);
176 memset(cursor, 0, sizeof(struct per_pid));
178 cursor->next = all_data;
183 static void pid_set_comm(int pid, char *comm)
186 struct per_pidcomm *c;
187 p = find_create_pid(pid);
190 if (c->comm && strcmp(c->comm, comm) == 0) {
195 c->comm = strdup(comm);
201 c = malloc(sizeof(struct per_pidcomm));
203 memset(c, 0, sizeof(struct per_pidcomm));
204 c->comm = strdup(comm);
210 static void pid_fork(int pid, int ppid, u64 timestamp)
212 struct per_pid *p, *pp;
213 p = find_create_pid(pid);
214 pp = find_create_pid(ppid);
216 if (pp->current && pp->current->comm && !p->current)
217 pid_set_comm(pid, pp->current->comm);
219 p->start_time = timestamp;
221 p->current->start_time = timestamp;
222 p->current->state_since = timestamp;
226 static void pid_exit(int pid, u64 timestamp)
229 p = find_create_pid(pid);
230 p->end_time = timestamp;
232 p->current->end_time = timestamp;
236 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
239 struct per_pidcomm *c;
240 struct cpu_sample *sample;
242 p = find_create_pid(pid);
245 c = malloc(sizeof(struct per_pidcomm));
247 memset(c, 0, sizeof(struct per_pidcomm));
253 sample = malloc(sizeof(struct cpu_sample));
254 assert(sample != NULL);
255 memset(sample, 0, sizeof(struct cpu_sample));
256 sample->start_time = start;
257 sample->end_time = end;
259 sample->next = c->samples;
263 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
264 c->total_time += (end-start);
265 p->total_time += (end-start);
268 if (c->start_time == 0 || c->start_time > start)
269 c->start_time = start;
270 if (p->start_time == 0 || p->start_time > start)
271 p->start_time = start;
277 #define MAX_CPUS 4096
279 static u64 cpus_cstate_start_times[MAX_CPUS];
280 static int cpus_cstate_state[MAX_CPUS];
281 static u64 cpus_pstate_start_times[MAX_CPUS];
282 static u64 cpus_pstate_state[MAX_CPUS];
284 static int process_comm_event(event_t *event, struct perf_session *session __used)
286 pid_set_comm(event->comm.pid, event->comm.comm);
290 static int process_fork_event(event_t *event, struct perf_session *session __used)
292 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
296 static int process_exit_event(event_t *event, struct perf_session *session __used)
298 pid_exit(event->fork.pid, event->fork.time);
305 unsigned char preempt_count;
311 struct trace_entry te;
316 #define TASK_COMM_LEN 16
317 struct wakeup_entry {
318 struct trace_entry te;
319 char comm[TASK_COMM_LEN];
326 * trace_flag_type is an enumeration that holds different
327 * states when a trace occurs. These are:
328 * IRQS_OFF - interrupts were disabled
329 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
330 * NEED_RESCED - reschedule is requested
331 * HARDIRQ - inside an interrupt handler
332 * SOFTIRQ - inside a softirq handler
334 enum trace_flag_type {
335 TRACE_FLAG_IRQS_OFF = 0x01,
336 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
337 TRACE_FLAG_NEED_RESCHED = 0x04,
338 TRACE_FLAG_HARDIRQ = 0x08,
339 TRACE_FLAG_SOFTIRQ = 0x10,
344 struct sched_switch {
345 struct trace_entry te;
346 char prev_comm[TASK_COMM_LEN];
349 long prev_state; /* Arjan weeps. */
350 char next_comm[TASK_COMM_LEN];
355 static void c_state_start(int cpu, u64 timestamp, int state)
357 cpus_cstate_start_times[cpu] = timestamp;
358 cpus_cstate_state[cpu] = state;
361 static void c_state_end(int cpu, u64 timestamp)
363 struct power_event *pwr;
364 pwr = malloc(sizeof(struct power_event));
367 memset(pwr, 0, sizeof(struct power_event));
369 pwr->state = cpus_cstate_state[cpu];
370 pwr->start_time = cpus_cstate_start_times[cpu];
371 pwr->end_time = timestamp;
374 pwr->next = power_events;
379 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
381 struct power_event *pwr;
382 pwr = malloc(sizeof(struct power_event));
384 if (new_freq > 8000000) /* detect invalid data */
389 memset(pwr, 0, sizeof(struct power_event));
391 pwr->state = cpus_pstate_state[cpu];
392 pwr->start_time = cpus_pstate_start_times[cpu];
393 pwr->end_time = timestamp;
396 pwr->next = power_events;
398 if (!pwr->start_time)
399 pwr->start_time = first_time;
403 cpus_pstate_state[cpu] = new_freq;
404 cpus_pstate_start_times[cpu] = timestamp;
406 if ((u64)new_freq > max_freq)
409 if (new_freq < min_freq || min_freq == 0)
412 if (new_freq == max_freq - 1000)
413 turbo_frequency = max_freq;
417 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
419 struct wake_event *we;
421 struct wakeup_entry *wake = (void *)te;
423 we = malloc(sizeof(struct wake_event));
427 memset(we, 0, sizeof(struct wake_event));
428 we->time = timestamp;
431 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
434 we->wakee = wake->pid;
435 we->next = wake_events;
437 p = find_create_pid(we->wakee);
439 if (p && p->current && p->current->state == TYPE_NONE) {
440 p->current->state_since = timestamp;
441 p->current->state = TYPE_WAITING;
443 if (p && p->current && p->current->state == TYPE_BLOCKED) {
444 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
445 p->current->state_since = timestamp;
446 p->current->state = TYPE_WAITING;
450 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
452 struct per_pid *p = NULL, *prev_p;
453 struct sched_switch *sw = (void *)te;
456 prev_p = find_create_pid(sw->prev_pid);
458 p = find_create_pid(sw->next_pid);
460 if (prev_p->current && prev_p->current->state != TYPE_NONE)
461 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
462 if (p && p->current) {
463 if (p->current->state != TYPE_NONE)
464 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
466 p->current->state_since = timestamp;
467 p->current->state = TYPE_RUNNING;
470 if (prev_p->current) {
471 prev_p->current->state = TYPE_NONE;
472 prev_p->current->state_since = timestamp;
473 if (sw->prev_state & 2)
474 prev_p->current->state = TYPE_BLOCKED;
475 if (sw->prev_state == 0)
476 prev_p->current->state = TYPE_WAITING;
482 process_sample_event(event_t *event)
484 struct sample_data data;
485 struct trace_entry *te;
487 memset(&data, 0, sizeof(data));
489 event__parse_sample(event, sample_type, &data);
491 if (sample_type & PERF_SAMPLE_TIME) {
492 if (!first_time || first_time > data.time)
493 first_time = data.time;
494 if (last_time < data.time)
495 last_time = data.time;
498 te = (void *)data.raw_data;
499 if (sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
501 struct power_entry *pe;
505 event_str = perf_header__find_event(te->type);
510 if (strcmp(event_str, "power:power_start") == 0)
511 c_state_start(data.cpu, data.time, pe->value);
513 if (strcmp(event_str, "power:power_end") == 0)
514 c_state_end(data.cpu, data.time);
516 if (strcmp(event_str, "power:power_frequency") == 0)
517 p_state_change(data.cpu, data.time, pe->value);
519 if (strcmp(event_str, "sched:sched_wakeup") == 0)
520 sched_wakeup(data.cpu, data.time, data.pid, te);
522 if (strcmp(event_str, "sched:sched_switch") == 0)
523 sched_switch(data.cpu, data.time, te);
529 * After the last sample we need to wrap up the current C/P state
530 * and close out each CPU for these.
532 static void end_sample_processing(void)
535 struct power_event *pwr;
537 for (cpu = 0; cpu <= numcpus; cpu++) {
538 pwr = malloc(sizeof(struct power_event));
541 memset(pwr, 0, sizeof(struct power_event));
545 pwr->state = cpus_cstate_state[cpu];
546 pwr->start_time = cpus_cstate_start_times[cpu];
547 pwr->end_time = last_time;
550 pwr->next = power_events;
556 pwr = malloc(sizeof(struct power_event));
559 memset(pwr, 0, sizeof(struct power_event));
561 pwr->state = cpus_pstate_state[cpu];
562 pwr->start_time = cpus_pstate_start_times[cpu];
563 pwr->end_time = last_time;
566 pwr->next = power_events;
568 if (!pwr->start_time)
569 pwr->start_time = first_time;
571 pwr->state = min_freq;
576 static u64 sample_time(event_t *event)
581 if (sample_type & PERF_SAMPLE_IP)
583 if (sample_type & PERF_SAMPLE_TID)
585 if (sample_type & PERF_SAMPLE_TIME)
586 return event->sample.array[cursor];
592 * We first queue all events, sorted backwards by insertion.
593 * The order will get flipped later.
595 static int queue_sample_event(event_t *event, struct perf_session *session __used)
597 struct sample_wrapper *copy, *prev;
600 size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
606 memset(copy, 0, size);
609 copy->timestamp = sample_time(event);
611 memcpy(©->data, event, event->sample.header.size);
613 /* insert in the right place in the list */
616 /* first sample ever */
621 if (all_samples->timestamp < copy->timestamp) {
622 /* insert at the head of the list */
623 copy->next = all_samples;
630 if (prev->next->timestamp < copy->timestamp) {
631 copy->next = prev->next;
637 /* insert at the end of the list */
643 static void sort_queued_samples(void)
645 struct sample_wrapper *cursor, *next;
647 cursor = all_samples;
652 cursor->next = all_samples;
653 all_samples = cursor;
659 * Sort the pid datastructure
661 static void sort_pids(void)
663 struct per_pid *new_list, *p, *cursor, *prev;
664 /* sort by ppid first, then by pid, lowest to highest */
673 if (new_list == NULL) {
681 if (cursor->ppid > p->ppid ||
682 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
683 /* must insert before */
685 p->next = prev->next;
698 cursor = cursor->next;
707 static void draw_c_p_states(void)
709 struct power_event *pwr;
713 * two pass drawing so that the P state bars are on top of the C state blocks
716 if (pwr->type == CSTATE)
717 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
723 if (pwr->type == PSTATE) {
725 pwr->state = min_freq;
726 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
732 static void draw_wakeups(void)
734 struct wake_event *we;
736 struct per_pidcomm *c;
740 int from = 0, to = 0;
741 char *task_from = NULL, *task_to = NULL;
743 /* locate the column of the waker and wakee */
746 if (p->pid == we->waker || p->pid == we->wakee) {
749 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
750 if (p->pid == we->waker && !from) {
752 task_from = strdup(c->comm);
754 if (p->pid == we->wakee && !to) {
756 task_to = strdup(c->comm);
763 if (p->pid == we->waker && !from) {
765 task_from = strdup(c->comm);
767 if (p->pid == we->wakee && !to) {
769 task_to = strdup(c->comm);
778 task_from = malloc(40);
779 sprintf(task_from, "[%i]", we->waker);
782 task_to = malloc(40);
783 sprintf(task_to, "[%i]", we->wakee);
787 svg_interrupt(we->time, to);
788 else if (from && to && abs(from - to) == 1)
789 svg_wakeline(we->time, from, to);
791 svg_partial_wakeline(we->time, from, task_from, to, task_to);
799 static void draw_cpu_usage(void)
802 struct per_pidcomm *c;
803 struct cpu_sample *sample;
810 if (sample->type == TYPE_RUNNING)
811 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
813 sample = sample->next;
821 static void draw_process_bars(void)
824 struct per_pidcomm *c;
825 struct cpu_sample *sample;
840 svg_box(Y, c->start_time, c->end_time, "process");
843 if (sample->type == TYPE_RUNNING)
844 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
845 if (sample->type == TYPE_BLOCKED)
846 svg_box(Y, sample->start_time, sample->end_time, "blocked");
847 if (sample->type == TYPE_WAITING)
848 svg_waiting(Y, sample->start_time, sample->end_time);
849 sample = sample->next;
854 if (c->total_time > 5000000000) /* 5 seconds */
855 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
857 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
859 svg_text(Y, c->start_time, comm);
869 static void add_process_filter(const char *string)
871 struct process_filter *filt;
874 pid = strtoull(string, NULL, 10);
875 filt = malloc(sizeof(struct process_filter));
879 filt->name = strdup(string);
881 filt->next = process_filter;
883 process_filter = filt;
886 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
888 struct process_filter *filt;
892 filt = process_filter;
894 if (filt->pid && p->pid == filt->pid)
896 if (strcmp(filt->name, c->comm) == 0)
903 static int determine_display_tasks_filtered(void)
906 struct per_pidcomm *c;
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;
924 if (c->start_time == 1)
925 c->start_time = first_time;
927 if (passes_filter(p, c)) {
933 if (c->end_time == 0)
934 c->end_time = last_time;
943 static int determine_display_tasks(u64 threshold)
946 struct per_pidcomm *c;
950 return determine_display_tasks_filtered();
955 if (p->start_time == 1)
956 p->start_time = first_time;
958 /* no exit marker, task kept running to the end */
959 if (p->end_time == 0)
960 p->end_time = last_time;
961 if (p->total_time >= threshold && !power_only)
969 if (c->start_time == 1)
970 c->start_time = first_time;
972 if (c->total_time >= threshold && !power_only) {
977 if (c->end_time == 0)
978 c->end_time = last_time;
989 #define TIME_THRESH 10000000
991 static void write_svg_file(const char *filename)
999 count = determine_display_tasks(TIME_THRESH);
1001 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
1003 count = determine_display_tasks(TIME_THRESH / 10);
1005 open_svg(filename, numcpus, count, first_time, last_time);
1010 for (i = 0; i < numcpus; i++)
1011 svg_cpu_box(i, max_freq, turbo_frequency);
1014 draw_process_bars();
1021 static void process_samples(void)
1023 struct sample_wrapper *cursor;
1026 sort_queued_samples();
1028 cursor = all_samples;
1030 event = (void *)&cursor->data;
1031 cursor = cursor->next;
1032 process_sample_event(event);
1036 static int sample_type_check(u64 type, struct perf_session *session __used)
1040 if (!(sample_type & PERF_SAMPLE_RAW)) {
1041 fprintf(stderr, "No trace samples found in the file.\n"
1042 "Have you used 'perf timechart record' to record it?\n");
1049 static struct perf_event_ops event_ops = {
1050 .process_comm_event = process_comm_event,
1051 .process_fork_event = process_fork_event,
1052 .process_exit_event = process_exit_event,
1053 .process_sample_event = queue_sample_event,
1054 .sample_type_check = sample_type_check,
1057 static int __cmd_timechart(void)
1059 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
1063 if (session == NULL)
1066 ret = perf_session__process_events(session, &event_ops);
1072 end_sample_processing();
1076 write_svg_file(output_name);
1078 pr_info("Written %2.1f seconds of trace to %s.\n",
1079 (last_time - first_time) / 1000000000.0, output_name);
1081 perf_session__delete(session);
1085 static const char * const timechart_usage[] = {
1086 "perf timechart [<options>] {record}",
1090 static const char *record_args[] = {
1097 "-e", "power:power_start",
1098 "-e", "power:power_end",
1099 "-e", "power:power_frequency",
1100 "-e", "sched:sched_wakeup",
1101 "-e", "sched:sched_switch",
1104 static int __cmd_record(int argc, const char **argv)
1106 unsigned int rec_argc, i, j;
1107 const char **rec_argv;
1109 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1110 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1112 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1113 rec_argv[i] = strdup(record_args[i]);
1115 for (j = 1; j < (unsigned int)argc; j++, i++)
1116 rec_argv[i] = argv[j];
1118 return cmd_record(i, rec_argv, NULL);
1122 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1125 add_process_filter(arg);
1129 static const struct option options[] = {
1130 OPT_STRING('i', "input", &input_name, "file",
1132 OPT_STRING('o', "output", &output_name, "file",
1133 "output file name"),
1134 OPT_INTEGER('w', "width", &svg_page_width,
1136 OPT_BOOLEAN('P', "power-only", &power_only,
1137 "output power data only"),
1138 OPT_CALLBACK('p', "process", NULL, "process",
1139 "process selector. Pass a pid or process name.",
1145 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1149 argc = parse_options(argc, argv, options, timechart_usage,
1150 PARSE_OPT_STOP_AT_NON_OPTION);
1152 if (argc && !strncmp(argv[0], "rec", 3))
1153 return __cmd_record(argc, argv);
1155 usage_with_options(timechart_usage, options);
1159 return __cmd_timechart();