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/callchain.h"
25 #include "util/strlist.h"
28 #include "util/header.h"
29 #include "util/parse-options.h"
30 #include "util/parse-events.h"
31 #include "util/event.h"
32 #include "util/session.h"
33 #include "util/svghelper.h"
35 static char const *input_name = "perf.data";
36 static char const *output_name = "output.svg";
38 static unsigned int numcpus;
39 static u64 min_freq; /* Lowest CPU frequency seen */
40 static u64 max_freq; /* Highest CPU frequency seen */
41 static u64 turbo_frequency;
43 static u64 first_time, last_time;
45 static bool power_only;
55 struct sample_wrapper;
58 * Datastructure layout:
59 * We keep an list of "pid"s, matching the kernels notion of a task struct.
60 * Each "pid" entry, has a list of "comm"s.
61 * this is because we want to track different programs different, while
62 * exec will reuse the original pid (by design).
63 * Each comm has a list of samples that will be used to draw
78 struct per_pidcomm *all;
79 struct per_pidcomm *current;
84 struct per_pidcomm *next;
98 struct cpu_sample *samples;
101 struct sample_wrapper {
102 struct sample_wrapper *next;
105 unsigned char data[0];
109 #define TYPE_RUNNING 1
110 #define TYPE_WAITING 2
111 #define TYPE_BLOCKED 3
114 struct cpu_sample *next;
122 static struct per_pid *all_data;
128 struct power_event *next;
137 struct wake_event *next;
143 static struct power_event *power_events;
144 static struct wake_event *wake_events;
146 struct sample_wrapper *all_samples;
149 struct process_filter;
150 struct process_filter {
153 struct process_filter *next;
156 static struct process_filter *process_filter;
159 static struct per_pid *find_create_pid(int pid)
161 struct per_pid *cursor = all_data;
164 if (cursor->pid == pid)
166 cursor = cursor->next;
168 cursor = malloc(sizeof(struct per_pid));
169 assert(cursor != NULL);
170 memset(cursor, 0, sizeof(struct per_pid));
172 cursor->next = all_data;
177 static void pid_set_comm(int pid, char *comm)
180 struct per_pidcomm *c;
181 p = find_create_pid(pid);
184 if (c->comm && strcmp(c->comm, comm) == 0) {
189 c->comm = strdup(comm);
195 c = malloc(sizeof(struct per_pidcomm));
197 memset(c, 0, sizeof(struct per_pidcomm));
198 c->comm = strdup(comm);
204 static void pid_fork(int pid, int ppid, u64 timestamp)
206 struct per_pid *p, *pp;
207 p = find_create_pid(pid);
208 pp = find_create_pid(ppid);
210 if (pp->current && pp->current->comm && !p->current)
211 pid_set_comm(pid, pp->current->comm);
213 p->start_time = timestamp;
215 p->current->start_time = timestamp;
216 p->current->state_since = timestamp;
220 static void pid_exit(int pid, u64 timestamp)
223 p = find_create_pid(pid);
224 p->end_time = timestamp;
226 p->current->end_time = timestamp;
230 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
233 struct per_pidcomm *c;
234 struct cpu_sample *sample;
236 p = find_create_pid(pid);
239 c = malloc(sizeof(struct per_pidcomm));
241 memset(c, 0, sizeof(struct per_pidcomm));
247 sample = malloc(sizeof(struct cpu_sample));
248 assert(sample != NULL);
249 memset(sample, 0, sizeof(struct cpu_sample));
250 sample->start_time = start;
251 sample->end_time = end;
253 sample->next = c->samples;
257 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
258 c->total_time += (end-start);
259 p->total_time += (end-start);
262 if (c->start_time == 0 || c->start_time > start)
263 c->start_time = start;
264 if (p->start_time == 0 || p->start_time > start)
265 p->start_time = start;
271 #define MAX_CPUS 4096
273 static u64 cpus_cstate_start_times[MAX_CPUS];
274 static int cpus_cstate_state[MAX_CPUS];
275 static u64 cpus_pstate_start_times[MAX_CPUS];
276 static u64 cpus_pstate_state[MAX_CPUS];
278 static int process_comm_event(event_t *event, struct perf_session *session __used)
280 pid_set_comm(event->comm.tid, event->comm.comm);
284 static int process_fork_event(event_t *event, struct perf_session *session __used)
286 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
290 static int process_exit_event(event_t *event, struct perf_session *session __used)
292 pid_exit(event->fork.pid, event->fork.time);
299 unsigned char preempt_count;
305 struct trace_entry te;
310 #define TASK_COMM_LEN 16
311 struct wakeup_entry {
312 struct trace_entry te;
313 char comm[TASK_COMM_LEN];
320 * trace_flag_type is an enumeration that holds different
321 * states when a trace occurs. These are:
322 * IRQS_OFF - interrupts were disabled
323 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
324 * NEED_RESCED - reschedule is requested
325 * HARDIRQ - inside an interrupt handler
326 * SOFTIRQ - inside a softirq handler
328 enum trace_flag_type {
329 TRACE_FLAG_IRQS_OFF = 0x01,
330 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
331 TRACE_FLAG_NEED_RESCHED = 0x04,
332 TRACE_FLAG_HARDIRQ = 0x08,
333 TRACE_FLAG_SOFTIRQ = 0x10,
338 struct sched_switch {
339 struct trace_entry te;
340 char prev_comm[TASK_COMM_LEN];
343 long prev_state; /* Arjan weeps. */
344 char next_comm[TASK_COMM_LEN];
349 static void c_state_start(int cpu, u64 timestamp, int state)
351 cpus_cstate_start_times[cpu] = timestamp;
352 cpus_cstate_state[cpu] = state;
355 static void c_state_end(int cpu, u64 timestamp)
357 struct power_event *pwr;
358 pwr = malloc(sizeof(struct power_event));
361 memset(pwr, 0, sizeof(struct power_event));
363 pwr->state = cpus_cstate_state[cpu];
364 pwr->start_time = cpus_cstate_start_times[cpu];
365 pwr->end_time = timestamp;
368 pwr->next = power_events;
373 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
375 struct power_event *pwr;
376 pwr = malloc(sizeof(struct power_event));
378 if (new_freq > 8000000) /* detect invalid data */
383 memset(pwr, 0, sizeof(struct power_event));
385 pwr->state = cpus_pstate_state[cpu];
386 pwr->start_time = cpus_pstate_start_times[cpu];
387 pwr->end_time = timestamp;
390 pwr->next = power_events;
392 if (!pwr->start_time)
393 pwr->start_time = first_time;
397 cpus_pstate_state[cpu] = new_freq;
398 cpus_pstate_start_times[cpu] = timestamp;
400 if ((u64)new_freq > max_freq)
403 if (new_freq < min_freq || min_freq == 0)
406 if (new_freq == max_freq - 1000)
407 turbo_frequency = max_freq;
411 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
413 struct wake_event *we;
415 struct wakeup_entry *wake = (void *)te;
417 we = malloc(sizeof(struct wake_event));
421 memset(we, 0, sizeof(struct wake_event));
422 we->time = timestamp;
425 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
428 we->wakee = wake->pid;
429 we->next = wake_events;
431 p = find_create_pid(we->wakee);
433 if (p && p->current && p->current->state == TYPE_NONE) {
434 p->current->state_since = timestamp;
435 p->current->state = TYPE_WAITING;
437 if (p && p->current && p->current->state == TYPE_BLOCKED) {
438 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
439 p->current->state_since = timestamp;
440 p->current->state = TYPE_WAITING;
444 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
446 struct per_pid *p = NULL, *prev_p;
447 struct sched_switch *sw = (void *)te;
450 prev_p = find_create_pid(sw->prev_pid);
452 p = find_create_pid(sw->next_pid);
454 if (prev_p->current && prev_p->current->state != TYPE_NONE)
455 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
456 if (p && p->current) {
457 if (p->current->state != TYPE_NONE)
458 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
460 p->current->state_since = timestamp;
461 p->current->state = TYPE_RUNNING;
464 if (prev_p->current) {
465 prev_p->current->state = TYPE_NONE;
466 prev_p->current->state_since = timestamp;
467 if (sw->prev_state & 2)
468 prev_p->current->state = TYPE_BLOCKED;
469 if (sw->prev_state == 0)
470 prev_p->current->state = TYPE_WAITING;
475 static int process_sample_event(event_t *event, struct perf_session *session)
477 struct sample_data data;
478 struct trace_entry *te;
480 memset(&data, 0, sizeof(data));
482 event__parse_sample(event, session->sample_type, &data);
484 if (session->sample_type & PERF_SAMPLE_TIME) {
485 if (!first_time || first_time > data.time)
486 first_time = data.time;
487 if (last_time < data.time)
488 last_time = data.time;
491 te = (void *)data.raw_data;
492 if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
494 struct power_entry *pe;
498 event_str = perf_header__find_event(te->type);
503 if (strcmp(event_str, "power:power_start") == 0)
504 c_state_start(data.cpu, data.time, pe->value);
506 if (strcmp(event_str, "power:power_end") == 0)
507 c_state_end(data.cpu, data.time);
509 if (strcmp(event_str, "power:power_frequency") == 0)
510 p_state_change(data.cpu, data.time, pe->value);
512 if (strcmp(event_str, "sched:sched_wakeup") == 0)
513 sched_wakeup(data.cpu, data.time, data.pid, te);
515 if (strcmp(event_str, "sched:sched_switch") == 0)
516 sched_switch(data.cpu, data.time, te);
522 * After the last sample we need to wrap up the current C/P state
523 * and close out each CPU for these.
525 static void end_sample_processing(void)
528 struct power_event *pwr;
530 for (cpu = 0; cpu <= numcpus; cpu++) {
531 pwr = malloc(sizeof(struct power_event));
534 memset(pwr, 0, sizeof(struct power_event));
538 pwr->state = cpus_cstate_state[cpu];
539 pwr->start_time = cpus_cstate_start_times[cpu];
540 pwr->end_time = last_time;
543 pwr->next = power_events;
549 pwr = malloc(sizeof(struct power_event));
552 memset(pwr, 0, sizeof(struct power_event));
554 pwr->state = cpus_pstate_state[cpu];
555 pwr->start_time = cpus_pstate_start_times[cpu];
556 pwr->end_time = last_time;
559 pwr->next = power_events;
561 if (!pwr->start_time)
562 pwr->start_time = first_time;
564 pwr->state = min_freq;
569 static u64 sample_time(event_t *event, const struct perf_session *session)
574 if (session->sample_type & PERF_SAMPLE_IP)
576 if (session->sample_type & PERF_SAMPLE_TID)
578 if (session->sample_type & PERF_SAMPLE_TIME)
579 return event->sample.array[cursor];
585 * We first queue all events, sorted backwards by insertion.
586 * The order will get flipped later.
588 static int queue_sample_event(event_t *event, struct perf_session *session)
590 struct sample_wrapper *copy, *prev;
593 size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
599 memset(copy, 0, size);
602 copy->timestamp = sample_time(event, session);
604 memcpy(©->data, event, event->sample.header.size);
606 /* insert in the right place in the list */
609 /* first sample ever */
614 if (all_samples->timestamp < copy->timestamp) {
615 /* insert at the head of the list */
616 copy->next = all_samples;
623 if (prev->next->timestamp < copy->timestamp) {
624 copy->next = prev->next;
630 /* insert at the end of the list */
636 static void sort_queued_samples(void)
638 struct sample_wrapper *cursor, *next;
640 cursor = all_samples;
645 cursor->next = all_samples;
646 all_samples = cursor;
652 * Sort the pid datastructure
654 static void sort_pids(void)
656 struct per_pid *new_list, *p, *cursor, *prev;
657 /* sort by ppid first, then by pid, lowest to highest */
666 if (new_list == NULL) {
674 if (cursor->ppid > p->ppid ||
675 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
676 /* must insert before */
678 p->next = prev->next;
691 cursor = cursor->next;
700 static void draw_c_p_states(void)
702 struct power_event *pwr;
706 * two pass drawing so that the P state bars are on top of the C state blocks
709 if (pwr->type == CSTATE)
710 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
716 if (pwr->type == PSTATE) {
718 pwr->state = min_freq;
719 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
725 static void draw_wakeups(void)
727 struct wake_event *we;
729 struct per_pidcomm *c;
733 int from = 0, to = 0;
734 char *task_from = NULL, *task_to = NULL;
736 /* locate the column of the waker and wakee */
739 if (p->pid == we->waker || p->pid == we->wakee) {
742 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
743 if (p->pid == we->waker && !from) {
745 task_from = strdup(c->comm);
747 if (p->pid == we->wakee && !to) {
749 task_to = strdup(c->comm);
756 if (p->pid == we->waker && !from) {
758 task_from = strdup(c->comm);
760 if (p->pid == we->wakee && !to) {
762 task_to = strdup(c->comm);
771 task_from = malloc(40);
772 sprintf(task_from, "[%i]", we->waker);
775 task_to = malloc(40);
776 sprintf(task_to, "[%i]", we->wakee);
780 svg_interrupt(we->time, to);
781 else if (from && to && abs(from - to) == 1)
782 svg_wakeline(we->time, from, to);
784 svg_partial_wakeline(we->time, from, task_from, to, task_to);
792 static void draw_cpu_usage(void)
795 struct per_pidcomm *c;
796 struct cpu_sample *sample;
803 if (sample->type == TYPE_RUNNING)
804 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
806 sample = sample->next;
814 static void draw_process_bars(void)
817 struct per_pidcomm *c;
818 struct cpu_sample *sample;
833 svg_box(Y, c->start_time, c->end_time, "process");
836 if (sample->type == TYPE_RUNNING)
837 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
838 if (sample->type == TYPE_BLOCKED)
839 svg_box(Y, sample->start_time, sample->end_time, "blocked");
840 if (sample->type == TYPE_WAITING)
841 svg_waiting(Y, sample->start_time, sample->end_time);
842 sample = sample->next;
847 if (c->total_time > 5000000000) /* 5 seconds */
848 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
850 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
852 svg_text(Y, c->start_time, comm);
862 static void add_process_filter(const char *string)
864 struct process_filter *filt;
867 pid = strtoull(string, NULL, 10);
868 filt = malloc(sizeof(struct process_filter));
872 filt->name = strdup(string);
874 filt->next = process_filter;
876 process_filter = filt;
879 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
881 struct process_filter *filt;
885 filt = process_filter;
887 if (filt->pid && p->pid == filt->pid)
889 if (strcmp(filt->name, c->comm) == 0)
896 static int determine_display_tasks_filtered(void)
899 struct per_pidcomm *c;
905 if (p->start_time == 1)
906 p->start_time = first_time;
908 /* no exit marker, task kept running to the end */
909 if (p->end_time == 0)
910 p->end_time = last_time;
917 if (c->start_time == 1)
918 c->start_time = first_time;
920 if (passes_filter(p, c)) {
926 if (c->end_time == 0)
927 c->end_time = last_time;
936 static int determine_display_tasks(u64 threshold)
939 struct per_pidcomm *c;
943 return determine_display_tasks_filtered();
948 if (p->start_time == 1)
949 p->start_time = first_time;
951 /* no exit marker, task kept running to the end */
952 if (p->end_time == 0)
953 p->end_time = last_time;
954 if (p->total_time >= threshold && !power_only)
962 if (c->start_time == 1)
963 c->start_time = first_time;
965 if (c->total_time >= threshold && !power_only) {
970 if (c->end_time == 0)
971 c->end_time = last_time;
982 #define TIME_THRESH 10000000
984 static void write_svg_file(const char *filename)
992 count = determine_display_tasks(TIME_THRESH);
994 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
996 count = determine_display_tasks(TIME_THRESH / 10);
998 open_svg(filename, numcpus, count, first_time, last_time);
1003 for (i = 0; i < numcpus; i++)
1004 svg_cpu_box(i, max_freq, turbo_frequency);
1007 draw_process_bars();
1014 static void process_samples(struct perf_session *session)
1016 struct sample_wrapper *cursor;
1019 sort_queued_samples();
1021 cursor = all_samples;
1023 event = (void *)&cursor->data;
1024 cursor = cursor->next;
1025 process_sample_event(event, session);
1029 static struct perf_event_ops event_ops = {
1030 .comm = process_comm_event,
1031 .fork = process_fork_event,
1032 .exit = process_exit_event,
1033 .sample = queue_sample_event,
1036 static int __cmd_timechart(void)
1038 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
1041 if (session == NULL)
1044 if (!perf_session__has_traces(session, "timechart record"))
1047 ret = perf_session__process_events(session, &event_ops);
1051 process_samples(session);
1053 end_sample_processing();
1057 write_svg_file(output_name);
1059 pr_info("Written %2.1f seconds of trace to %s.\n",
1060 (last_time - first_time) / 1000000000.0, output_name);
1062 perf_session__delete(session);
1066 static const char * const timechart_usage[] = {
1067 "perf timechart [<options>] {record}",
1071 static const char *record_args[] = {
1078 "-e", "power:power_start",
1079 "-e", "power:power_end",
1080 "-e", "power:power_frequency",
1081 "-e", "sched:sched_wakeup",
1082 "-e", "sched:sched_switch",
1085 static int __cmd_record(int argc, const char **argv)
1087 unsigned int rec_argc, i, j;
1088 const char **rec_argv;
1090 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1091 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1093 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1094 rec_argv[i] = strdup(record_args[i]);
1096 for (j = 1; j < (unsigned int)argc; j++, i++)
1097 rec_argv[i] = argv[j];
1099 return cmd_record(i, rec_argv, NULL);
1103 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1106 add_process_filter(arg);
1110 static const struct option options[] = {
1111 OPT_STRING('i', "input", &input_name, "file",
1113 OPT_STRING('o', "output", &output_name, "file",
1114 "output file name"),
1115 OPT_INTEGER('w', "width", &svg_page_width,
1117 OPT_BOOLEAN('P', "power-only", &power_only,
1118 "output power data only"),
1119 OPT_CALLBACK('p', "process", NULL, "process",
1120 "process selector. Pass a pid or process name.",
1126 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1128 argc = parse_options(argc, argv, options, timechart_usage,
1129 PARSE_OPT_STOP_AT_NON_OPTION);
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();