2 * builtin-timechart.c - make an svg timechart of system activity
4 * (C) Copyright 2009 Intel Corporation
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"
40 #include "util/debug.h"
42 #define SUPPORT_OLD_POWER_EVENTS 1
43 #define PWR_EVENT_EXIT -1
50 struct perf_tool tool;
51 struct per_pid *all_data;
52 struct power_event *power_events;
53 struct wake_event *wake_events;
56 u64 min_freq, /* Lowest CPU frequency seen */
57 max_freq, /* Highest CPU frequency seen */
59 first_time, last_time;
64 /* IO related settings */
77 * Datastructure layout:
78 * We keep an list of "pid"s, matching the kernels notion of a task struct.
79 * Each "pid" entry, has a list of "comm"s.
80 * this is because we want to track different programs different, while
81 * exec will reuse the original pid (by design).
82 * Each comm has a list of samples that will be used to draw
98 struct per_pidcomm *all;
99 struct per_pidcomm *current;
104 struct per_pidcomm *next;
120 struct cpu_sample *samples;
121 struct io_sample *io_samples;
124 struct sample_wrapper {
125 struct sample_wrapper *next;
128 unsigned char data[0];
132 #define TYPE_RUNNING 1
133 #define TYPE_WAITING 2
134 #define TYPE_BLOCKED 3
137 struct cpu_sample *next;
143 const char *backtrace;
156 struct io_sample *next;
171 struct power_event *next;
180 struct wake_event *next;
184 const char *backtrace;
187 struct process_filter {
190 struct process_filter *next;
193 static struct process_filter *process_filter;
196 static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
198 struct per_pid *cursor = tchart->all_data;
201 if (cursor->pid == pid)
203 cursor = cursor->next;
205 cursor = zalloc(sizeof(*cursor));
206 assert(cursor != NULL);
208 cursor->next = tchart->all_data;
209 tchart->all_data = cursor;
213 static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
216 struct per_pidcomm *c;
217 p = find_create_pid(tchart, pid);
220 if (c->comm && strcmp(c->comm, comm) == 0) {
225 c->comm = strdup(comm);
231 c = zalloc(sizeof(*c));
233 c->comm = strdup(comm);
239 static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
241 struct per_pid *p, *pp;
242 p = find_create_pid(tchart, pid);
243 pp = find_create_pid(tchart, ppid);
245 if (pp->current && pp->current->comm && !p->current)
246 pid_set_comm(tchart, pid, pp->current->comm);
248 p->start_time = timestamp;
249 if (p->current && !p->current->start_time) {
250 p->current->start_time = timestamp;
251 p->current->state_since = timestamp;
255 static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
258 p = find_create_pid(tchart, pid);
259 p->end_time = timestamp;
261 p->current->end_time = timestamp;
264 static void pid_put_sample(struct timechart *tchart, int pid, int type,
265 unsigned int cpu, u64 start, u64 end,
266 const char *backtrace)
269 struct per_pidcomm *c;
270 struct cpu_sample *sample;
272 p = find_create_pid(tchart, pid);
275 c = zalloc(sizeof(*c));
282 sample = zalloc(sizeof(*sample));
283 assert(sample != NULL);
284 sample->start_time = start;
285 sample->end_time = end;
287 sample->next = c->samples;
289 sample->backtrace = backtrace;
292 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
293 c->total_time += (end-start);
294 p->total_time += (end-start);
297 if (c->start_time == 0 || c->start_time > start)
298 c->start_time = start;
299 if (p->start_time == 0 || p->start_time > start)
300 p->start_time = start;
303 #define MAX_CPUS 4096
305 static u64 cpus_cstate_start_times[MAX_CPUS];
306 static int cpus_cstate_state[MAX_CPUS];
307 static u64 cpus_pstate_start_times[MAX_CPUS];
308 static u64 cpus_pstate_state[MAX_CPUS];
310 static int process_comm_event(struct perf_tool *tool,
311 union perf_event *event,
312 struct perf_sample *sample __maybe_unused,
313 struct machine *machine __maybe_unused)
315 struct timechart *tchart = container_of(tool, struct timechart, tool);
316 pid_set_comm(tchart, event->comm.tid, event->comm.comm);
320 static int process_fork_event(struct perf_tool *tool,
321 union perf_event *event,
322 struct perf_sample *sample __maybe_unused,
323 struct machine *machine __maybe_unused)
325 struct timechart *tchart = container_of(tool, struct timechart, tool);
326 pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
330 static int process_exit_event(struct perf_tool *tool,
331 union perf_event *event,
332 struct perf_sample *sample __maybe_unused,
333 struct machine *machine __maybe_unused)
335 struct timechart *tchart = container_of(tool, struct timechart, tool);
336 pid_exit(tchart, event->fork.pid, event->fork.time);
340 #ifdef SUPPORT_OLD_POWER_EVENTS
341 static int use_old_power_events;
344 static void c_state_start(int cpu, u64 timestamp, int state)
346 cpus_cstate_start_times[cpu] = timestamp;
347 cpus_cstate_state[cpu] = state;
350 static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
352 struct power_event *pwr = zalloc(sizeof(*pwr));
357 pwr->state = cpus_cstate_state[cpu];
358 pwr->start_time = cpus_cstate_start_times[cpu];
359 pwr->end_time = timestamp;
362 pwr->next = tchart->power_events;
364 tchart->power_events = pwr;
367 static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
369 struct power_event *pwr;
371 if (new_freq > 8000000) /* detect invalid data */
374 pwr = zalloc(sizeof(*pwr));
378 pwr->state = cpus_pstate_state[cpu];
379 pwr->start_time = cpus_pstate_start_times[cpu];
380 pwr->end_time = timestamp;
383 pwr->next = tchart->power_events;
385 if (!pwr->start_time)
386 pwr->start_time = tchart->first_time;
388 tchart->power_events = pwr;
390 cpus_pstate_state[cpu] = new_freq;
391 cpus_pstate_start_times[cpu] = timestamp;
393 if ((u64)new_freq > tchart->max_freq)
394 tchart->max_freq = new_freq;
396 if (new_freq < tchart->min_freq || tchart->min_freq == 0)
397 tchart->min_freq = new_freq;
399 if (new_freq == tchart->max_freq - 1000)
400 tchart->turbo_frequency = tchart->max_freq;
403 static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
404 int waker, int wakee, u8 flags, const char *backtrace)
407 struct wake_event *we = zalloc(sizeof(*we));
412 we->time = timestamp;
414 we->backtrace = backtrace;
416 if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
420 we->next = tchart->wake_events;
421 tchart->wake_events = we;
422 p = find_create_pid(tchart, we->wakee);
424 if (p && p->current && p->current->state == TYPE_NONE) {
425 p->current->state_since = timestamp;
426 p->current->state = TYPE_WAITING;
428 if (p && p->current && p->current->state == TYPE_BLOCKED) {
429 pid_put_sample(tchart, p->pid, p->current->state, cpu,
430 p->current->state_since, timestamp, NULL);
431 p->current->state_since = timestamp;
432 p->current->state = TYPE_WAITING;
436 static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
437 int prev_pid, int next_pid, u64 prev_state,
438 const char *backtrace)
440 struct per_pid *p = NULL, *prev_p;
442 prev_p = find_create_pid(tchart, prev_pid);
444 p = find_create_pid(tchart, next_pid);
446 if (prev_p->current && prev_p->current->state != TYPE_NONE)
447 pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
448 prev_p->current->state_since, timestamp,
450 if (p && p->current) {
451 if (p->current->state != TYPE_NONE)
452 pid_put_sample(tchart, next_pid, p->current->state, cpu,
453 p->current->state_since, timestamp,
456 p->current->state_since = timestamp;
457 p->current->state = TYPE_RUNNING;
460 if (prev_p->current) {
461 prev_p->current->state = TYPE_NONE;
462 prev_p->current->state_since = timestamp;
464 prev_p->current->state = TYPE_BLOCKED;
466 prev_p->current->state = TYPE_WAITING;
470 static const char *cat_backtrace(union perf_event *event,
471 struct perf_sample *sample,
472 struct machine *machine)
474 struct addr_location al;
478 u8 cpumode = PERF_RECORD_MISC_USER;
479 struct addr_location tal;
480 struct ip_callchain *chain = sample->callchain;
481 FILE *f = open_memstream(&p, &p_len);
484 perror("open_memstream error");
491 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
492 fprintf(stderr, "problem processing %d event, skipping it.\n",
497 for (i = 0; i < chain->nr; i++) {
500 if (callchain_param.order == ORDER_CALLEE)
503 ip = chain->ips[chain->nr - i - 1];
505 if (ip >= PERF_CONTEXT_MAX) {
507 case PERF_CONTEXT_HV:
508 cpumode = PERF_RECORD_MISC_HYPERVISOR;
510 case PERF_CONTEXT_KERNEL:
511 cpumode = PERF_RECORD_MISC_KERNEL;
513 case PERF_CONTEXT_USER:
514 cpumode = PERF_RECORD_MISC_USER;
517 pr_debug("invalid callchain context: "
518 "%"PRId64"\n", (s64) ip);
521 * It seems the callchain is corrupted.
531 thread__find_addr_location(al.thread, machine, cpumode,
532 MAP__FUNCTION, ip, &tal);
535 fprintf(f, "..... %016" PRIx64 " %s\n", ip,
538 fprintf(f, "..... %016" PRIx64 "\n", ip);
547 typedef int (*tracepoint_handler)(struct timechart *tchart,
548 struct perf_evsel *evsel,
549 struct perf_sample *sample,
550 const char *backtrace);
552 static int process_sample_event(struct perf_tool *tool,
553 union perf_event *event,
554 struct perf_sample *sample,
555 struct perf_evsel *evsel,
556 struct machine *machine)
558 struct timechart *tchart = container_of(tool, struct timechart, tool);
560 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
561 if (!tchart->first_time || tchart->first_time > sample->time)
562 tchart->first_time = sample->time;
563 if (tchart->last_time < sample->time)
564 tchart->last_time = sample->time;
567 if (evsel->handler != NULL) {
568 tracepoint_handler f = evsel->handler;
569 return f(tchart, evsel, sample,
570 cat_backtrace(event, sample, machine));
577 process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
578 struct perf_evsel *evsel,
579 struct perf_sample *sample,
580 const char *backtrace __maybe_unused)
582 u32 state = perf_evsel__intval(evsel, sample, "state");
583 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
585 if (state == (u32)PWR_EVENT_EXIT)
586 c_state_end(tchart, cpu_id, sample->time);
588 c_state_start(cpu_id, sample->time, state);
593 process_sample_cpu_frequency(struct timechart *tchart,
594 struct perf_evsel *evsel,
595 struct perf_sample *sample,
596 const char *backtrace __maybe_unused)
598 u32 state = perf_evsel__intval(evsel, sample, "state");
599 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
601 p_state_change(tchart, cpu_id, sample->time, state);
606 process_sample_sched_wakeup(struct timechart *tchart,
607 struct perf_evsel *evsel,
608 struct perf_sample *sample,
609 const char *backtrace)
611 u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
612 int waker = perf_evsel__intval(evsel, sample, "common_pid");
613 int wakee = perf_evsel__intval(evsel, sample, "pid");
615 sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
620 process_sample_sched_switch(struct timechart *tchart,
621 struct perf_evsel *evsel,
622 struct perf_sample *sample,
623 const char *backtrace)
625 int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
626 int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
627 u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
629 sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
630 prev_state, backtrace);
634 #ifdef SUPPORT_OLD_POWER_EVENTS
636 process_sample_power_start(struct timechart *tchart __maybe_unused,
637 struct perf_evsel *evsel,
638 struct perf_sample *sample,
639 const char *backtrace __maybe_unused)
641 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
642 u64 value = perf_evsel__intval(evsel, sample, "value");
644 c_state_start(cpu_id, sample->time, value);
649 process_sample_power_end(struct timechart *tchart,
650 struct perf_evsel *evsel __maybe_unused,
651 struct perf_sample *sample,
652 const char *backtrace __maybe_unused)
654 c_state_end(tchart, sample->cpu, sample->time);
659 process_sample_power_frequency(struct timechart *tchart,
660 struct perf_evsel *evsel,
661 struct perf_sample *sample,
662 const char *backtrace __maybe_unused)
664 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
665 u64 value = perf_evsel__intval(evsel, sample, "value");
667 p_state_change(tchart, cpu_id, sample->time, value);
670 #endif /* SUPPORT_OLD_POWER_EVENTS */
673 * After the last sample we need to wrap up the current C/P state
674 * and close out each CPU for these.
676 static void end_sample_processing(struct timechart *tchart)
679 struct power_event *pwr;
681 for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
684 pwr = zalloc(sizeof(*pwr));
688 pwr->state = cpus_cstate_state[cpu];
689 pwr->start_time = cpus_cstate_start_times[cpu];
690 pwr->end_time = tchart->last_time;
693 pwr->next = tchart->power_events;
695 tchart->power_events = pwr;
699 pwr = zalloc(sizeof(*pwr));
703 pwr->state = cpus_pstate_state[cpu];
704 pwr->start_time = cpus_pstate_start_times[cpu];
705 pwr->end_time = tchart->last_time;
708 pwr->next = tchart->power_events;
710 if (!pwr->start_time)
711 pwr->start_time = tchart->first_time;
713 pwr->state = tchart->min_freq;
714 tchart->power_events = pwr;
718 static int pid_begin_io_sample(struct timechart *tchart, int pid, int type,
721 struct per_pid *p = find_create_pid(tchart, pid);
722 struct per_pidcomm *c = p->current;
723 struct io_sample *sample;
724 struct io_sample *prev;
727 c = zalloc(sizeof(*c));
735 prev = c->io_samples;
737 if (prev && prev->start_time && !prev->end_time) {
738 pr_warning("Skip invalid start event: "
739 "previous event already started!\n");
741 /* remove previous event that has been started,
742 * we are not sure we will ever get an end for it */
743 c->io_samples = prev->next;
748 sample = zalloc(sizeof(*sample));
751 sample->start_time = start;
754 sample->next = c->io_samples;
755 c->io_samples = sample;
757 if (c->start_time == 0 || c->start_time > start)
758 c->start_time = start;
763 static int pid_end_io_sample(struct timechart *tchart, int pid, int type,
766 struct per_pid *p = find_create_pid(tchart, pid);
767 struct per_pidcomm *c = p->current;
768 struct io_sample *sample, *prev;
771 pr_warning("Invalid pidcomm!\n");
775 sample = c->io_samples;
777 if (!sample) /* skip partially captured events */
780 if (sample->end_time) {
781 pr_warning("Skip invalid end event: "
782 "previous event already ended!\n");
786 if (sample->type != type) {
787 pr_warning("Skip invalid end event: invalid event type!\n");
791 sample->end_time = end;
794 /* we want to be able to see small and fast transfers, so make them
795 * at least min_time long, but don't overlap them */
796 if (sample->end_time - sample->start_time < tchart->min_time)
797 sample->end_time = sample->start_time + tchart->min_time;
798 if (prev && sample->start_time < prev->end_time) {
799 if (prev->err) /* try to make errors more visible */
800 sample->start_time = prev->end_time;
802 prev->end_time = sample->start_time;
807 } else if (type == IOTYPE_READ || type == IOTYPE_WRITE ||
808 type == IOTYPE_TX || type == IOTYPE_RX) {
810 if ((u64)ret > c->max_bytes)
813 c->total_bytes += ret;
814 p->total_bytes += ret;
818 /* merge two requests to make svg smaller and render-friendly */
820 prev->type == sample->type &&
821 prev->err == sample->err &&
822 prev->fd == sample->fd &&
823 prev->end_time + tchart->merge_dist >= sample->start_time) {
825 sample->bytes += prev->bytes;
826 sample->merges += prev->merges + 1;
828 sample->start_time = prev->start_time;
829 sample->next = prev->next;
832 if (!sample->err && sample->bytes > c->max_bytes)
833 c->max_bytes = sample->bytes;
842 process_enter_read(struct timechart *tchart,
843 struct perf_evsel *evsel,
844 struct perf_sample *sample)
846 long fd = perf_evsel__intval(evsel, sample, "fd");
847 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_READ,
852 process_exit_read(struct timechart *tchart,
853 struct perf_evsel *evsel,
854 struct perf_sample *sample)
856 long ret = perf_evsel__intval(evsel, sample, "ret");
857 return pid_end_io_sample(tchart, sample->tid, IOTYPE_READ,
862 process_enter_write(struct timechart *tchart,
863 struct perf_evsel *evsel,
864 struct perf_sample *sample)
866 long fd = perf_evsel__intval(evsel, sample, "fd");
867 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_WRITE,
872 process_exit_write(struct timechart *tchart,
873 struct perf_evsel *evsel,
874 struct perf_sample *sample)
876 long ret = perf_evsel__intval(evsel, sample, "ret");
877 return pid_end_io_sample(tchart, sample->tid, IOTYPE_WRITE,
882 process_enter_sync(struct timechart *tchart,
883 struct perf_evsel *evsel,
884 struct perf_sample *sample)
886 long fd = perf_evsel__intval(evsel, sample, "fd");
887 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_SYNC,
892 process_exit_sync(struct timechart *tchart,
893 struct perf_evsel *evsel,
894 struct perf_sample *sample)
896 long ret = perf_evsel__intval(evsel, sample, "ret");
897 return pid_end_io_sample(tchart, sample->tid, IOTYPE_SYNC,
902 process_enter_tx(struct timechart *tchart,
903 struct perf_evsel *evsel,
904 struct perf_sample *sample)
906 long fd = perf_evsel__intval(evsel, sample, "fd");
907 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_TX,
912 process_exit_tx(struct timechart *tchart,
913 struct perf_evsel *evsel,
914 struct perf_sample *sample)
916 long ret = perf_evsel__intval(evsel, sample, "ret");
917 return pid_end_io_sample(tchart, sample->tid, IOTYPE_TX,
922 process_enter_rx(struct timechart *tchart,
923 struct perf_evsel *evsel,
924 struct perf_sample *sample)
926 long fd = perf_evsel__intval(evsel, sample, "fd");
927 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_RX,
932 process_exit_rx(struct timechart *tchart,
933 struct perf_evsel *evsel,
934 struct perf_sample *sample)
936 long ret = perf_evsel__intval(evsel, sample, "ret");
937 return pid_end_io_sample(tchart, sample->tid, IOTYPE_RX,
942 process_enter_poll(struct timechart *tchart,
943 struct perf_evsel *evsel,
944 struct perf_sample *sample)
946 long fd = perf_evsel__intval(evsel, sample, "fd");
947 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_POLL,
952 process_exit_poll(struct timechart *tchart,
953 struct perf_evsel *evsel,
954 struct perf_sample *sample)
956 long ret = perf_evsel__intval(evsel, sample, "ret");
957 return pid_end_io_sample(tchart, sample->tid, IOTYPE_POLL,
962 * Sort the pid datastructure
964 static void sort_pids(struct timechart *tchart)
966 struct per_pid *new_list, *p, *cursor, *prev;
967 /* sort by ppid first, then by pid, lowest to highest */
971 while (tchart->all_data) {
972 p = tchart->all_data;
973 tchart->all_data = p->next;
976 if (new_list == NULL) {
984 if (cursor->ppid > p->ppid ||
985 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
986 /* must insert before */
988 p->next = prev->next;
1001 cursor = cursor->next;
1006 tchart->all_data = new_list;
1010 static void draw_c_p_states(struct timechart *tchart)
1012 struct power_event *pwr;
1013 pwr = tchart->power_events;
1016 * two pass drawing so that the P state bars are on top of the C state blocks
1019 if (pwr->type == CSTATE)
1020 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
1024 pwr = tchart->power_events;
1026 if (pwr->type == PSTATE) {
1028 pwr->state = tchart->min_freq;
1029 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
1035 static void draw_wakeups(struct timechart *tchart)
1037 struct wake_event *we;
1039 struct per_pidcomm *c;
1041 we = tchart->wake_events;
1043 int from = 0, to = 0;
1044 char *task_from = NULL, *task_to = NULL;
1046 /* locate the column of the waker and wakee */
1047 p = tchart->all_data;
1049 if (p->pid == we->waker || p->pid == we->wakee) {
1052 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
1053 if (p->pid == we->waker && !from) {
1055 task_from = strdup(c->comm);
1057 if (p->pid == we->wakee && !to) {
1059 task_to = strdup(c->comm);
1066 if (p->pid == we->waker && !from) {
1068 task_from = strdup(c->comm);
1070 if (p->pid == we->wakee && !to) {
1072 task_to = strdup(c->comm);
1081 task_from = malloc(40);
1082 sprintf(task_from, "[%i]", we->waker);
1085 task_to = malloc(40);
1086 sprintf(task_to, "[%i]", we->wakee);
1089 if (we->waker == -1)
1090 svg_interrupt(we->time, to, we->backtrace);
1091 else if (from && to && abs(from - to) == 1)
1092 svg_wakeline(we->time, from, to, we->backtrace);
1094 svg_partial_wakeline(we->time, from, task_from, to,
1095 task_to, we->backtrace);
1103 static void draw_cpu_usage(struct timechart *tchart)
1106 struct per_pidcomm *c;
1107 struct cpu_sample *sample;
1108 p = tchart->all_data;
1112 sample = c->samples;
1114 if (sample->type == TYPE_RUNNING) {
1115 svg_process(sample->cpu,
1123 sample = sample->next;
1131 static void draw_io_bars(struct timechart *tchart)
1137 struct per_pidcomm *c;
1138 struct io_sample *sample;
1141 p = tchart->all_data;
1151 svg_box(Y, c->start_time, c->end_time, "process3");
1152 sample = c->io_samples;
1153 for (sample = c->io_samples; sample; sample = sample->next) {
1154 double h = (double)sample->bytes / c->max_bytes;
1156 if (tchart->skip_eagain &&
1157 sample->err == -EAGAIN)
1163 if (sample->type == IOTYPE_SYNC)
1168 sample->err ? "error" : "sync",
1172 else if (sample->type == IOTYPE_POLL)
1177 sample->err ? "error" : "poll",
1181 else if (sample->type == IOTYPE_READ)
1186 sample->err ? "error" : "disk",
1190 else if (sample->type == IOTYPE_WRITE)
1195 sample->err ? "error" : "disk",
1199 else if (sample->type == IOTYPE_RX)
1204 sample->err ? "error" : "net",
1208 else if (sample->type == IOTYPE_TX)
1213 sample->err ? "error" : "net",
1220 bytes = c->total_bytes;
1222 bytes = bytes / 1024;
1226 bytes = bytes / 1024;
1230 bytes = bytes / 1024;
1235 sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
1236 svg_text(Y, c->start_time, comm);
1246 static void draw_process_bars(struct timechart *tchart)
1249 struct per_pidcomm *c;
1250 struct cpu_sample *sample;
1253 Y = 2 * tchart->numcpus + 2;
1255 p = tchart->all_data;
1265 svg_box(Y, c->start_time, c->end_time, "process");
1266 sample = c->samples;
1268 if (sample->type == TYPE_RUNNING)
1269 svg_running(Y, sample->cpu,
1273 if (sample->type == TYPE_BLOCKED)
1274 svg_blocked(Y, sample->cpu,
1278 if (sample->type == TYPE_WAITING)
1279 svg_waiting(Y, sample->cpu,
1283 sample = sample->next;
1288 if (c->total_time > 5000000000) /* 5 seconds */
1289 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
1291 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
1293 svg_text(Y, c->start_time, comm);
1303 static void add_process_filter(const char *string)
1305 int pid = strtoull(string, NULL, 10);
1306 struct process_filter *filt = malloc(sizeof(*filt));
1311 filt->name = strdup(string);
1313 filt->next = process_filter;
1315 process_filter = filt;
1318 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
1320 struct process_filter *filt;
1321 if (!process_filter)
1324 filt = process_filter;
1326 if (filt->pid && p->pid == filt->pid)
1328 if (strcmp(filt->name, c->comm) == 0)
1335 static int determine_display_tasks_filtered(struct timechart *tchart)
1338 struct per_pidcomm *c;
1341 p = tchart->all_data;
1344 if (p->start_time == 1)
1345 p->start_time = tchart->first_time;
1347 /* no exit marker, task kept running to the end */
1348 if (p->end_time == 0)
1349 p->end_time = tchart->last_time;
1356 if (c->start_time == 1)
1357 c->start_time = tchart->first_time;
1359 if (passes_filter(p, c)) {
1365 if (c->end_time == 0)
1366 c->end_time = tchart->last_time;
1375 static int determine_display_tasks(struct timechart *tchart, u64 threshold)
1378 struct per_pidcomm *c;
1381 p = tchart->all_data;
1384 if (p->start_time == 1)
1385 p->start_time = tchart->first_time;
1387 /* no exit marker, task kept running to the end */
1388 if (p->end_time == 0)
1389 p->end_time = tchart->last_time;
1390 if (p->total_time >= threshold)
1398 if (c->start_time == 1)
1399 c->start_time = tchart->first_time;
1401 if (c->total_time >= threshold) {
1406 if (c->end_time == 0)
1407 c->end_time = tchart->last_time;
1416 static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
1419 struct per_pidcomm *c;
1422 p = timechart->all_data;
1424 /* no exit marker, task kept running to the end */
1425 if (p->end_time == 0)
1426 p->end_time = timechart->last_time;
1433 if (c->total_bytes >= threshold) {
1438 if (c->end_time == 0)
1439 c->end_time = timechart->last_time;
1448 #define BYTES_THRESH (1 * 1024 * 1024)
1449 #define TIME_THRESH 10000000
1451 static void write_svg_file(struct timechart *tchart, const char *filename)
1455 int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
1457 if (tchart->power_only)
1458 tchart->proc_num = 0;
1460 /* We'd like to show at least proc_num tasks;
1461 * be less picky if we have fewer */
1464 count = determine_display_tasks_filtered(tchart);
1465 else if (tchart->io_events)
1466 count = determine_display_io_tasks(tchart, thresh);
1468 count = determine_display_tasks(tchart, thresh);
1470 } while (!process_filter && thresh && count < tchart->proc_num);
1472 if (!tchart->proc_num)
1475 if (tchart->io_events) {
1476 open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
1481 draw_io_bars(tchart);
1483 open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
1489 for (i = 0; i < tchart->numcpus; i++)
1490 svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
1492 draw_cpu_usage(tchart);
1493 if (tchart->proc_num)
1494 draw_process_bars(tchart);
1495 if (!tchart->tasks_only)
1496 draw_c_p_states(tchart);
1497 if (tchart->proc_num)
1498 draw_wakeups(tchart);
1504 static int process_header(struct perf_file_section *section __maybe_unused,
1505 struct perf_header *ph,
1507 int fd __maybe_unused,
1510 struct timechart *tchart = data;
1514 tchart->numcpus = ph->env.nr_cpus_avail;
1517 case HEADER_CPU_TOPOLOGY:
1518 if (!tchart->topology)
1521 if (svg_build_topology_map(ph->env.sibling_cores,
1522 ph->env.nr_sibling_cores,
1523 ph->env.sibling_threads,
1524 ph->env.nr_sibling_threads))
1525 fprintf(stderr, "problem building topology\n");
1535 static int __cmd_timechart(struct timechart *tchart, const char *output_name)
1537 const struct perf_evsel_str_handler power_tracepoints[] = {
1538 { "power:cpu_idle", process_sample_cpu_idle },
1539 { "power:cpu_frequency", process_sample_cpu_frequency },
1540 { "sched:sched_wakeup", process_sample_sched_wakeup },
1541 { "sched:sched_switch", process_sample_sched_switch },
1542 #ifdef SUPPORT_OLD_POWER_EVENTS
1543 { "power:power_start", process_sample_power_start },
1544 { "power:power_end", process_sample_power_end },
1545 { "power:power_frequency", process_sample_power_frequency },
1548 { "syscalls:sys_enter_read", process_enter_read },
1549 { "syscalls:sys_enter_pread64", process_enter_read },
1550 { "syscalls:sys_enter_readv", process_enter_read },
1551 { "syscalls:sys_enter_preadv", process_enter_read },
1552 { "syscalls:sys_enter_write", process_enter_write },
1553 { "syscalls:sys_enter_pwrite64", process_enter_write },
1554 { "syscalls:sys_enter_writev", process_enter_write },
1555 { "syscalls:sys_enter_pwritev", process_enter_write },
1556 { "syscalls:sys_enter_sync", process_enter_sync },
1557 { "syscalls:sys_enter_sync_file_range", process_enter_sync },
1558 { "syscalls:sys_enter_fsync", process_enter_sync },
1559 { "syscalls:sys_enter_msync", process_enter_sync },
1560 { "syscalls:sys_enter_recvfrom", process_enter_rx },
1561 { "syscalls:sys_enter_recvmmsg", process_enter_rx },
1562 { "syscalls:sys_enter_recvmsg", process_enter_rx },
1563 { "syscalls:sys_enter_sendto", process_enter_tx },
1564 { "syscalls:sys_enter_sendmsg", process_enter_tx },
1565 { "syscalls:sys_enter_sendmmsg", process_enter_tx },
1566 { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
1567 { "syscalls:sys_enter_epoll_wait", process_enter_poll },
1568 { "syscalls:sys_enter_poll", process_enter_poll },
1569 { "syscalls:sys_enter_ppoll", process_enter_poll },
1570 { "syscalls:sys_enter_pselect6", process_enter_poll },
1571 { "syscalls:sys_enter_select", process_enter_poll },
1573 { "syscalls:sys_exit_read", process_exit_read },
1574 { "syscalls:sys_exit_pread64", process_exit_read },
1575 { "syscalls:sys_exit_readv", process_exit_read },
1576 { "syscalls:sys_exit_preadv", process_exit_read },
1577 { "syscalls:sys_exit_write", process_exit_write },
1578 { "syscalls:sys_exit_pwrite64", process_exit_write },
1579 { "syscalls:sys_exit_writev", process_exit_write },
1580 { "syscalls:sys_exit_pwritev", process_exit_write },
1581 { "syscalls:sys_exit_sync", process_exit_sync },
1582 { "syscalls:sys_exit_sync_file_range", process_exit_sync },
1583 { "syscalls:sys_exit_fsync", process_exit_sync },
1584 { "syscalls:sys_exit_msync", process_exit_sync },
1585 { "syscalls:sys_exit_recvfrom", process_exit_rx },
1586 { "syscalls:sys_exit_recvmmsg", process_exit_rx },
1587 { "syscalls:sys_exit_recvmsg", process_exit_rx },
1588 { "syscalls:sys_exit_sendto", process_exit_tx },
1589 { "syscalls:sys_exit_sendmsg", process_exit_tx },
1590 { "syscalls:sys_exit_sendmmsg", process_exit_tx },
1591 { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
1592 { "syscalls:sys_exit_epoll_wait", process_exit_poll },
1593 { "syscalls:sys_exit_poll", process_exit_poll },
1594 { "syscalls:sys_exit_ppoll", process_exit_poll },
1595 { "syscalls:sys_exit_pselect6", process_exit_poll },
1596 { "syscalls:sys_exit_select", process_exit_poll },
1598 struct perf_data_file file = {
1600 .mode = PERF_DATA_MODE_READ,
1603 struct perf_session *session = perf_session__new(&file, false,
1607 if (session == NULL)
1610 (void)perf_header__process_sections(&session->header,
1611 perf_data_file__fd(session->file),
1615 if (!perf_session__has_traces(session, "timechart record"))
1618 if (perf_session__set_tracepoints_handlers(session,
1619 power_tracepoints)) {
1620 pr_err("Initializing session tracepoint handlers failed\n");
1624 ret = perf_session__process_events(session, &tchart->tool);
1628 end_sample_processing(tchart);
1632 write_svg_file(tchart, output_name);
1634 pr_info("Written %2.1f seconds of trace to %s.\n",
1635 (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
1637 perf_session__delete(session);
1641 static int timechart__io_record(int argc, const char **argv)
1643 unsigned int rec_argc, i;
1644 const char **rec_argv;
1646 char *filter = NULL;
1648 const char * const common_args[] = {
1649 "record", "-a", "-R", "-c", "1",
1651 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1653 const char * const disk_events[] = {
1654 "syscalls:sys_enter_read",
1655 "syscalls:sys_enter_pread64",
1656 "syscalls:sys_enter_readv",
1657 "syscalls:sys_enter_preadv",
1658 "syscalls:sys_enter_write",
1659 "syscalls:sys_enter_pwrite64",
1660 "syscalls:sys_enter_writev",
1661 "syscalls:sys_enter_pwritev",
1662 "syscalls:sys_enter_sync",
1663 "syscalls:sys_enter_sync_file_range",
1664 "syscalls:sys_enter_fsync",
1665 "syscalls:sys_enter_msync",
1667 "syscalls:sys_exit_read",
1668 "syscalls:sys_exit_pread64",
1669 "syscalls:sys_exit_readv",
1670 "syscalls:sys_exit_preadv",
1671 "syscalls:sys_exit_write",
1672 "syscalls:sys_exit_pwrite64",
1673 "syscalls:sys_exit_writev",
1674 "syscalls:sys_exit_pwritev",
1675 "syscalls:sys_exit_sync",
1676 "syscalls:sys_exit_sync_file_range",
1677 "syscalls:sys_exit_fsync",
1678 "syscalls:sys_exit_msync",
1680 unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
1682 const char * const net_events[] = {
1683 "syscalls:sys_enter_recvfrom",
1684 "syscalls:sys_enter_recvmmsg",
1685 "syscalls:sys_enter_recvmsg",
1686 "syscalls:sys_enter_sendto",
1687 "syscalls:sys_enter_sendmsg",
1688 "syscalls:sys_enter_sendmmsg",
1690 "syscalls:sys_exit_recvfrom",
1691 "syscalls:sys_exit_recvmmsg",
1692 "syscalls:sys_exit_recvmsg",
1693 "syscalls:sys_exit_sendto",
1694 "syscalls:sys_exit_sendmsg",
1695 "syscalls:sys_exit_sendmmsg",
1697 unsigned int net_events_nr = ARRAY_SIZE(net_events);
1699 const char * const poll_events[] = {
1700 "syscalls:sys_enter_epoll_pwait",
1701 "syscalls:sys_enter_epoll_wait",
1702 "syscalls:sys_enter_poll",
1703 "syscalls:sys_enter_ppoll",
1704 "syscalls:sys_enter_pselect6",
1705 "syscalls:sys_enter_select",
1707 "syscalls:sys_exit_epoll_pwait",
1708 "syscalls:sys_exit_epoll_wait",
1709 "syscalls:sys_exit_poll",
1710 "syscalls:sys_exit_ppoll",
1711 "syscalls:sys_exit_pselect6",
1712 "syscalls:sys_exit_select",
1714 unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
1716 rec_argc = common_args_nr +
1717 disk_events_nr * 4 +
1719 poll_events_nr * 4 +
1721 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1723 if (rec_argv == NULL)
1726 if (asprintf(&filter, "common_pid != %d", getpid()) < 0)
1730 for (i = 0; i < common_args_nr; i++)
1731 *p++ = strdup(common_args[i]);
1733 for (i = 0; i < disk_events_nr; i++) {
1734 if (!is_valid_tracepoint(disk_events[i])) {
1740 *p++ = strdup(disk_events[i]);
1744 for (i = 0; i < net_events_nr; i++) {
1745 if (!is_valid_tracepoint(net_events[i])) {
1751 *p++ = strdup(net_events[i]);
1755 for (i = 0; i < poll_events_nr; i++) {
1756 if (!is_valid_tracepoint(poll_events[i])) {
1762 *p++ = strdup(poll_events[i]);
1767 for (i = 0; i < (unsigned int)argc; i++)
1770 return cmd_record(rec_argc, rec_argv, NULL);
1774 static int timechart__record(struct timechart *tchart, int argc, const char **argv)
1776 unsigned int rec_argc, i, j;
1777 const char **rec_argv;
1779 unsigned int record_elems;
1781 const char * const common_args[] = {
1782 "record", "-a", "-R", "-c", "1",
1784 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1786 const char * const backtrace_args[] = {
1789 unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
1791 const char * const power_args[] = {
1792 "-e", "power:cpu_frequency",
1793 "-e", "power:cpu_idle",
1795 unsigned int power_args_nr = ARRAY_SIZE(power_args);
1797 const char * const old_power_args[] = {
1798 #ifdef SUPPORT_OLD_POWER_EVENTS
1799 "-e", "power:power_start",
1800 "-e", "power:power_end",
1801 "-e", "power:power_frequency",
1804 unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
1806 const char * const tasks_args[] = {
1807 "-e", "sched:sched_wakeup",
1808 "-e", "sched:sched_switch",
1810 unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
1812 #ifdef SUPPORT_OLD_POWER_EVENTS
1813 if (!is_valid_tracepoint("power:cpu_idle") &&
1814 is_valid_tracepoint("power:power_start")) {
1815 use_old_power_events = 1;
1818 old_power_args_nr = 0;
1822 if (tchart->power_only)
1825 if (tchart->tasks_only) {
1827 old_power_args_nr = 0;
1830 if (!tchart->with_backtrace)
1831 backtrace_args_no = 0;
1833 record_elems = common_args_nr + tasks_args_nr +
1834 power_args_nr + old_power_args_nr + backtrace_args_no;
1836 rec_argc = record_elems + argc;
1837 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1839 if (rec_argv == NULL)
1843 for (i = 0; i < common_args_nr; i++)
1844 *p++ = strdup(common_args[i]);
1846 for (i = 0; i < backtrace_args_no; i++)
1847 *p++ = strdup(backtrace_args[i]);
1849 for (i = 0; i < tasks_args_nr; i++)
1850 *p++ = strdup(tasks_args[i]);
1852 for (i = 0; i < power_args_nr; i++)
1853 *p++ = strdup(power_args[i]);
1855 for (i = 0; i < old_power_args_nr; i++)
1856 *p++ = strdup(old_power_args[i]);
1858 for (j = 0; j < (unsigned int)argc; j++)
1861 return cmd_record(rec_argc, rec_argv, NULL);
1865 parse_process(const struct option *opt __maybe_unused, const char *arg,
1866 int __maybe_unused unset)
1869 add_process_filter(arg);
1874 parse_highlight(const struct option *opt __maybe_unused, const char *arg,
1875 int __maybe_unused unset)
1877 unsigned long duration = strtoul(arg, NULL, 0);
1879 if (svg_highlight || svg_highlight_name)
1883 svg_highlight = duration;
1885 svg_highlight_name = strdup(arg);
1891 parse_time(const struct option *opt, const char *arg, int __maybe_unused unset)
1894 u64 *value = opt->value;
1896 if (sscanf(arg, "%" PRIu64 "%cs", value, &unit) > 0) {
1914 int cmd_timechart(int argc, const char **argv,
1915 const char *prefix __maybe_unused)
1917 struct timechart tchart = {
1919 .comm = process_comm_event,
1920 .fork = process_fork_event,
1921 .exit = process_exit_event,
1922 .sample = process_sample_event,
1923 .ordered_samples = true,
1926 .min_time = 1000000,
1929 const char *output_name = "output.svg";
1930 const struct option timechart_options[] = {
1931 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1932 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1933 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1934 OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
1935 "highlight tasks. Pass duration in ns or process name.",
1937 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1938 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
1939 "output processes data only"),
1940 OPT_CALLBACK('p', "process", NULL, "process",
1941 "process selector. Pass a pid or process name.",
1943 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1944 "Look for files with symbols relative to this directory"),
1945 OPT_INTEGER('n', "proc-num", &tchart.proc_num,
1946 "min. number of tasks to print"),
1947 OPT_BOOLEAN('t', "topology", &tchart.topology,
1948 "sort CPUs according to topology"),
1949 OPT_BOOLEAN(0, "io-skip-eagain", &tchart.skip_eagain,
1950 "skip EAGAIN errors"),
1951 OPT_CALLBACK(0, "io-min-time", &tchart.min_time, "time",
1952 "all IO faster than min-time will visually appear longer",
1954 OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
1955 "merge events that are merge-dist us apart",
1959 const char * const timechart_usage[] = {
1960 "perf timechart [<options>] {record}",
1964 const struct option record_options[] = {
1965 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1966 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
1967 "output processes data only"),
1968 OPT_BOOLEAN('I', "io-only", &tchart.io_only,
1969 "record only IO data"),
1970 OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
1973 const char * const record_usage[] = {
1974 "perf timechart record [<options>]",
1977 argc = parse_options(argc, argv, timechart_options, timechart_usage,
1978 PARSE_OPT_STOP_AT_NON_OPTION);
1980 if (tchart.power_only && tchart.tasks_only) {
1981 pr_err("-P and -T options cannot be used at the same time.\n");
1987 if (argc && !strncmp(argv[0], "rec", 3)) {
1988 argc = parse_options(argc, argv, record_options, record_usage,
1989 PARSE_OPT_STOP_AT_NON_OPTION);
1991 if (tchart.power_only && tchart.tasks_only) {
1992 pr_err("-P and -T options cannot be used at the same time.\n");
1997 return timechart__io_record(argc, argv);
1999 return timechart__record(&tchart, argc, argv);
2001 usage_with_options(timechart_usage, timechart_options);
2005 return __cmd_timechart(&tchart, output_name);