perf timechart: Fix header handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-timechart.c
blobc0f29ed0996628f5fcb85ba432e43862f45e9248
1 /*
2 * builtin-timechart.c - make an svg timechart of system activity
4 * (C) Copyright 2009 Intel Corporation
6 * Authors:
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
12 * of the License.
15 #include "builtin.h"
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"
28 #include "perf.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/data_map.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;
52 struct per_pid;
53 struct per_pidcomm;
55 struct cpu_sample;
56 struct power_event;
57 struct wake_event;
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
68 * final graph.
71 struct per_pid {
72 struct per_pid *next;
74 int pid;
75 int ppid;
77 u64 start_time;
78 u64 end_time;
79 u64 total_time;
80 int display;
82 struct per_pidcomm *all;
83 struct per_pidcomm *current;
85 int painted;
89 struct per_pidcomm {
90 struct per_pidcomm *next;
92 u64 start_time;
93 u64 end_time;
94 u64 total_time;
96 int Y;
97 int display;
99 long state;
100 u64 state_since;
102 char *comm;
104 struct cpu_sample *samples;
107 struct sample_wrapper {
108 struct sample_wrapper *next;
110 u64 timestamp;
111 unsigned char data[0];
114 #define TYPE_NONE 0
115 #define TYPE_RUNNING 1
116 #define TYPE_WAITING 2
117 #define TYPE_BLOCKED 3
119 struct cpu_sample {
120 struct cpu_sample *next;
122 u64 start_time;
123 u64 end_time;
124 int type;
125 int cpu;
128 static struct per_pid *all_data;
130 #define CSTATE 1
131 #define PSTATE 2
133 struct power_event {
134 struct power_event *next;
135 int type;
136 int state;
137 u64 start_time;
138 u64 end_time;
139 int cpu;
142 struct wake_event {
143 struct wake_event *next;
144 int waker;
145 int wakee;
146 u64 time;
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 {
157 char *name;
158 int pid;
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;
169 while (cursor) {
170 if (cursor->pid == pid)
171 return cursor;
172 cursor = cursor->next;
174 cursor = malloc(sizeof(struct per_pid));
175 assert(cursor != NULL);
176 memset(cursor, 0, sizeof(struct per_pid));
177 cursor->pid = pid;
178 cursor->next = all_data;
179 all_data = cursor;
180 return cursor;
183 static void pid_set_comm(int pid, char *comm)
185 struct per_pid *p;
186 struct per_pidcomm *c;
187 p = find_create_pid(pid);
188 c = p->all;
189 while (c) {
190 if (c->comm && strcmp(c->comm, comm) == 0) {
191 p->current = c;
192 return;
194 if (!c->comm) {
195 c->comm = strdup(comm);
196 p->current = c;
197 return;
199 c = c->next;
201 c = malloc(sizeof(struct per_pidcomm));
202 assert(c != NULL);
203 memset(c, 0, sizeof(struct per_pidcomm));
204 c->comm = strdup(comm);
205 p->current = c;
206 c->next = p->all;
207 p->all = c;
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);
215 p->ppid = ppid;
216 if (pp->current && pp->current->comm && !p->current)
217 pid_set_comm(pid, pp->current->comm);
219 p->start_time = timestamp;
220 if (p->current) {
221 p->current->start_time = timestamp;
222 p->current->state_since = timestamp;
226 static void pid_exit(int pid, u64 timestamp)
228 struct per_pid *p;
229 p = find_create_pid(pid);
230 p->end_time = timestamp;
231 if (p->current)
232 p->current->end_time = timestamp;
235 static void
236 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
238 struct per_pid *p;
239 struct per_pidcomm *c;
240 struct cpu_sample *sample;
242 p = find_create_pid(pid);
243 c = p->current;
244 if (!c) {
245 c = malloc(sizeof(struct per_pidcomm));
246 assert(c != NULL);
247 memset(c, 0, sizeof(struct per_pidcomm));
248 p->current = c;
249 c->next = p->all;
250 p->all = c;
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;
258 sample->type = type;
259 sample->next = c->samples;
260 sample->cpu = cpu;
261 c->samples = sample;
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;
273 if (cpu > numcpus)
274 numcpus = cpu;
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
285 process_comm_event(event_t *event)
287 pid_set_comm(event->comm.pid, event->comm.comm);
288 return 0;
290 static int
291 process_fork_event(event_t *event)
293 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
294 return 0;
297 static int
298 process_exit_event(event_t *event)
300 pid_exit(event->fork.pid, event->fork.time);
301 return 0;
304 struct trace_entry {
305 unsigned short type;
306 unsigned char flags;
307 unsigned char preempt_count;
308 int pid;
309 int lock_depth;
312 struct power_entry {
313 struct trace_entry te;
314 s64 type;
315 s64 value;
318 #define TASK_COMM_LEN 16
319 struct wakeup_entry {
320 struct trace_entry te;
321 char comm[TASK_COMM_LEN];
322 int pid;
323 int prio;
324 int success;
328 * trace_flag_type is an enumeration that holds different
329 * states when a trace occurs. These are:
330 * IRQS_OFF - interrupts were disabled
331 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
332 * NEED_RESCED - reschedule is requested
333 * HARDIRQ - inside an interrupt handler
334 * SOFTIRQ - inside a softirq handler
336 enum trace_flag_type {
337 TRACE_FLAG_IRQS_OFF = 0x01,
338 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
339 TRACE_FLAG_NEED_RESCHED = 0x04,
340 TRACE_FLAG_HARDIRQ = 0x08,
341 TRACE_FLAG_SOFTIRQ = 0x10,
346 struct sched_switch {
347 struct trace_entry te;
348 char prev_comm[TASK_COMM_LEN];
349 int prev_pid;
350 int prev_prio;
351 long prev_state; /* Arjan weeps. */
352 char next_comm[TASK_COMM_LEN];
353 int next_pid;
354 int next_prio;
357 static void c_state_start(int cpu, u64 timestamp, int state)
359 cpus_cstate_start_times[cpu] = timestamp;
360 cpus_cstate_state[cpu] = state;
363 static void c_state_end(int cpu, u64 timestamp)
365 struct power_event *pwr;
366 pwr = malloc(sizeof(struct power_event));
367 if (!pwr)
368 return;
369 memset(pwr, 0, sizeof(struct power_event));
371 pwr->state = cpus_cstate_state[cpu];
372 pwr->start_time = cpus_cstate_start_times[cpu];
373 pwr->end_time = timestamp;
374 pwr->cpu = cpu;
375 pwr->type = CSTATE;
376 pwr->next = power_events;
378 power_events = pwr;
381 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
383 struct power_event *pwr;
384 pwr = malloc(sizeof(struct power_event));
386 if (new_freq > 8000000) /* detect invalid data */
387 return;
389 if (!pwr)
390 return;
391 memset(pwr, 0, sizeof(struct power_event));
393 pwr->state = cpus_pstate_state[cpu];
394 pwr->start_time = cpus_pstate_start_times[cpu];
395 pwr->end_time = timestamp;
396 pwr->cpu = cpu;
397 pwr->type = PSTATE;
398 pwr->next = power_events;
400 if (!pwr->start_time)
401 pwr->start_time = first_time;
403 power_events = pwr;
405 cpus_pstate_state[cpu] = new_freq;
406 cpus_pstate_start_times[cpu] = timestamp;
408 if ((u64)new_freq > max_freq)
409 max_freq = new_freq;
411 if (new_freq < min_freq || min_freq == 0)
412 min_freq = new_freq;
414 if (new_freq == max_freq - 1000)
415 turbo_frequency = max_freq;
418 static void
419 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
421 struct wake_event *we;
422 struct per_pid *p;
423 struct wakeup_entry *wake = (void *)te;
425 we = malloc(sizeof(struct wake_event));
426 if (!we)
427 return;
429 memset(we, 0, sizeof(struct wake_event));
430 we->time = timestamp;
431 we->waker = pid;
433 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
434 we->waker = -1;
436 we->wakee = wake->pid;
437 we->next = wake_events;
438 wake_events = we;
439 p = find_create_pid(we->wakee);
441 if (p && p->current && p->current->state == TYPE_NONE) {
442 p->current->state_since = timestamp;
443 p->current->state = TYPE_WAITING;
445 if (p && p->current && p->current->state == TYPE_BLOCKED) {
446 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
447 p->current->state_since = timestamp;
448 p->current->state = TYPE_WAITING;
452 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
454 struct per_pid *p = NULL, *prev_p;
455 struct sched_switch *sw = (void *)te;
458 prev_p = find_create_pid(sw->prev_pid);
460 p = find_create_pid(sw->next_pid);
462 if (prev_p->current && prev_p->current->state != TYPE_NONE)
463 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
464 if (p && p->current) {
465 if (p->current->state != TYPE_NONE)
466 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
468 p->current->state_since = timestamp;
469 p->current->state = TYPE_RUNNING;
472 if (prev_p->current) {
473 prev_p->current->state = TYPE_NONE;
474 prev_p->current->state_since = timestamp;
475 if (sw->prev_state & 2)
476 prev_p->current->state = TYPE_BLOCKED;
477 if (sw->prev_state == 0)
478 prev_p->current->state = TYPE_WAITING;
483 static int
484 process_sample_event(event_t *event)
486 int cursor = 0;
487 u64 addr = 0;
488 u64 stamp = 0;
489 u32 cpu = 0;
490 u32 pid = 0;
491 u32 size, *size_ptr;
492 struct trace_entry *te;
494 if (sample_type & PERF_SAMPLE_IP)
495 cursor++;
497 if (sample_type & PERF_SAMPLE_TID) {
498 pid = event->sample.array[cursor]>>32;
499 cursor++;
501 if (sample_type & PERF_SAMPLE_TIME) {
502 stamp = event->sample.array[cursor++];
504 if (!first_time || first_time > stamp)
505 first_time = stamp;
506 if (last_time < stamp)
507 last_time = stamp;
510 if (sample_type & PERF_SAMPLE_ADDR)
511 addr = event->sample.array[cursor++];
512 if (sample_type & PERF_SAMPLE_ID)
513 cursor++;
514 if (sample_type & PERF_SAMPLE_STREAM_ID)
515 cursor++;
516 if (sample_type & PERF_SAMPLE_CPU)
517 cpu = event->sample.array[cursor++] & 0xFFFFFFFF;
518 if (sample_type & PERF_SAMPLE_PERIOD)
519 cursor++;
521 size_ptr = (void *)&event->sample.array[cursor];
523 size = *size_ptr;
524 size_ptr++;
526 te = (void *)size_ptr;
527 if (sample_type & PERF_SAMPLE_RAW && size > 0) {
528 char *event_str;
529 struct power_entry *pe;
531 pe = (void *)te;
533 event_str = perf_header__find_event(te->type);
535 if (!event_str)
536 return 0;
538 if (strcmp(event_str, "power:power_start") == 0)
539 c_state_start(cpu, stamp, pe->value);
541 if (strcmp(event_str, "power:power_end") == 0)
542 c_state_end(cpu, stamp);
544 if (strcmp(event_str, "power:power_frequency") == 0)
545 p_state_change(cpu, stamp, pe->value);
547 if (strcmp(event_str, "sched:sched_wakeup") == 0)
548 sched_wakeup(cpu, stamp, pid, te);
550 if (strcmp(event_str, "sched:sched_switch") == 0)
551 sched_switch(cpu, stamp, te);
553 return 0;
557 * After the last sample we need to wrap up the current C/P state
558 * and close out each CPU for these.
560 static void end_sample_processing(void)
562 u64 cpu;
563 struct power_event *pwr;
565 for (cpu = 0; cpu <= numcpus; cpu++) {
566 pwr = malloc(sizeof(struct power_event));
567 if (!pwr)
568 return;
569 memset(pwr, 0, sizeof(struct power_event));
571 /* C state */
572 #if 0
573 pwr->state = cpus_cstate_state[cpu];
574 pwr->start_time = cpus_cstate_start_times[cpu];
575 pwr->end_time = last_time;
576 pwr->cpu = cpu;
577 pwr->type = CSTATE;
578 pwr->next = power_events;
580 power_events = pwr;
581 #endif
582 /* P state */
584 pwr = malloc(sizeof(struct power_event));
585 if (!pwr)
586 return;
587 memset(pwr, 0, sizeof(struct power_event));
589 pwr->state = cpus_pstate_state[cpu];
590 pwr->start_time = cpus_pstate_start_times[cpu];
591 pwr->end_time = last_time;
592 pwr->cpu = cpu;
593 pwr->type = PSTATE;
594 pwr->next = power_events;
596 if (!pwr->start_time)
597 pwr->start_time = first_time;
598 if (!pwr->state)
599 pwr->state = min_freq;
600 power_events = pwr;
604 static u64 sample_time(event_t *event)
606 int cursor;
608 cursor = 0;
609 if (sample_type & PERF_SAMPLE_IP)
610 cursor++;
611 if (sample_type & PERF_SAMPLE_TID)
612 cursor++;
613 if (sample_type & PERF_SAMPLE_TIME)
614 return event->sample.array[cursor];
615 return 0;
620 * We first queue all events, sorted backwards by insertion.
621 * The order will get flipped later.
623 static int
624 queue_sample_event(event_t *event)
626 struct sample_wrapper *copy, *prev;
627 int size;
629 size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
631 copy = malloc(size);
632 if (!copy)
633 return 1;
635 memset(copy, 0, size);
637 copy->next = NULL;
638 copy->timestamp = sample_time(event);
640 memcpy(&copy->data, event, event->sample.header.size);
642 /* insert in the right place in the list */
644 if (!all_samples) {
645 /* first sample ever */
646 all_samples = copy;
647 return 0;
650 if (all_samples->timestamp < copy->timestamp) {
651 /* insert at the head of the list */
652 copy->next = all_samples;
653 all_samples = copy;
654 return 0;
657 prev = all_samples;
658 while (prev->next) {
659 if (prev->next->timestamp < copy->timestamp) {
660 copy->next = prev->next;
661 prev->next = copy;
662 return 0;
664 prev = prev->next;
666 /* insert at the end of the list */
667 prev->next = copy;
669 return 0;
672 static void sort_queued_samples(void)
674 struct sample_wrapper *cursor, *next;
676 cursor = all_samples;
677 all_samples = NULL;
679 while (cursor) {
680 next = cursor->next;
681 cursor->next = all_samples;
682 all_samples = cursor;
683 cursor = next;
688 * Sort the pid datastructure
690 static void sort_pids(void)
692 struct per_pid *new_list, *p, *cursor, *prev;
693 /* sort by ppid first, then by pid, lowest to highest */
695 new_list = NULL;
697 while (all_data) {
698 p = all_data;
699 all_data = p->next;
700 p->next = NULL;
702 if (new_list == NULL) {
703 new_list = p;
704 p->next = NULL;
705 continue;
707 prev = NULL;
708 cursor = new_list;
709 while (cursor) {
710 if (cursor->ppid > p->ppid ||
711 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
712 /* must insert before */
713 if (prev) {
714 p->next = prev->next;
715 prev->next = p;
716 cursor = NULL;
717 continue;
718 } else {
719 p->next = new_list;
720 new_list = p;
721 cursor = NULL;
722 continue;
726 prev = cursor;
727 cursor = cursor->next;
728 if (!cursor)
729 prev->next = p;
732 all_data = new_list;
736 static void draw_c_p_states(void)
738 struct power_event *pwr;
739 pwr = power_events;
742 * two pass drawing so that the P state bars are on top of the C state blocks
744 while (pwr) {
745 if (pwr->type == CSTATE)
746 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
747 pwr = pwr->next;
750 pwr = power_events;
751 while (pwr) {
752 if (pwr->type == PSTATE) {
753 if (!pwr->state)
754 pwr->state = min_freq;
755 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
757 pwr = pwr->next;
761 static void draw_wakeups(void)
763 struct wake_event *we;
764 struct per_pid *p;
765 struct per_pidcomm *c;
767 we = wake_events;
768 while (we) {
769 int from = 0, to = 0;
770 char *task_from = NULL, *task_to = NULL;
772 /* locate the column of the waker and wakee */
773 p = all_data;
774 while (p) {
775 if (p->pid == we->waker || p->pid == we->wakee) {
776 c = p->all;
777 while (c) {
778 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
779 if (p->pid == we->waker && !from) {
780 from = c->Y;
781 task_from = strdup(c->comm);
783 if (p->pid == we->wakee && !to) {
784 to = c->Y;
785 task_to = strdup(c->comm);
788 c = c->next;
790 c = p->all;
791 while (c) {
792 if (p->pid == we->waker && !from) {
793 from = c->Y;
794 task_from = strdup(c->comm);
796 if (p->pid == we->wakee && !to) {
797 to = c->Y;
798 task_to = strdup(c->comm);
800 c = c->next;
803 p = p->next;
806 if (!task_from) {
807 task_from = malloc(40);
808 sprintf(task_from, "[%i]", we->waker);
810 if (!task_to) {
811 task_to = malloc(40);
812 sprintf(task_to, "[%i]", we->wakee);
815 if (we->waker == -1)
816 svg_interrupt(we->time, to);
817 else if (from && to && abs(from - to) == 1)
818 svg_wakeline(we->time, from, to);
819 else
820 svg_partial_wakeline(we->time, from, task_from, to, task_to);
821 we = we->next;
823 free(task_from);
824 free(task_to);
828 static void draw_cpu_usage(void)
830 struct per_pid *p;
831 struct per_pidcomm *c;
832 struct cpu_sample *sample;
833 p = all_data;
834 while (p) {
835 c = p->all;
836 while (c) {
837 sample = c->samples;
838 while (sample) {
839 if (sample->type == TYPE_RUNNING)
840 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
842 sample = sample->next;
844 c = c->next;
846 p = p->next;
850 static void draw_process_bars(void)
852 struct per_pid *p;
853 struct per_pidcomm *c;
854 struct cpu_sample *sample;
855 int Y = 0;
857 Y = 2 * numcpus + 2;
859 p = all_data;
860 while (p) {
861 c = p->all;
862 while (c) {
863 if (!c->display) {
864 c->Y = 0;
865 c = c->next;
866 continue;
869 svg_box(Y, c->start_time, c->end_time, "process");
870 sample = c->samples;
871 while (sample) {
872 if (sample->type == TYPE_RUNNING)
873 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
874 if (sample->type == TYPE_BLOCKED)
875 svg_box(Y, sample->start_time, sample->end_time, "blocked");
876 if (sample->type == TYPE_WAITING)
877 svg_waiting(Y, sample->start_time, sample->end_time);
878 sample = sample->next;
881 if (c->comm) {
882 char comm[256];
883 if (c->total_time > 5000000000) /* 5 seconds */
884 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
885 else
886 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
888 svg_text(Y, c->start_time, comm);
890 c->Y = Y;
891 Y++;
892 c = c->next;
894 p = p->next;
898 static void add_process_filter(const char *string)
900 struct process_filter *filt;
901 int pid;
903 pid = strtoull(string, NULL, 10);
904 filt = malloc(sizeof(struct process_filter));
905 if (!filt)
906 return;
908 filt->name = strdup(string);
909 filt->pid = pid;
910 filt->next = process_filter;
912 process_filter = filt;
915 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
917 struct process_filter *filt;
918 if (!process_filter)
919 return 1;
921 filt = process_filter;
922 while (filt) {
923 if (filt->pid && p->pid == filt->pid)
924 return 1;
925 if (strcmp(filt->name, c->comm) == 0)
926 return 1;
927 filt = filt->next;
929 return 0;
932 static int determine_display_tasks_filtered(void)
934 struct per_pid *p;
935 struct per_pidcomm *c;
936 int count = 0;
938 p = all_data;
939 while (p) {
940 p->display = 0;
941 if (p->start_time == 1)
942 p->start_time = first_time;
944 /* no exit marker, task kept running to the end */
945 if (p->end_time == 0)
946 p->end_time = last_time;
948 c = p->all;
950 while (c) {
951 c->display = 0;
953 if (c->start_time == 1)
954 c->start_time = first_time;
956 if (passes_filter(p, c)) {
957 c->display = 1;
958 p->display = 1;
959 count++;
962 if (c->end_time == 0)
963 c->end_time = last_time;
965 c = c->next;
967 p = p->next;
969 return count;
972 static int determine_display_tasks(u64 threshold)
974 struct per_pid *p;
975 struct per_pidcomm *c;
976 int count = 0;
978 if (process_filter)
979 return determine_display_tasks_filtered();
981 p = all_data;
982 while (p) {
983 p->display = 0;
984 if (p->start_time == 1)
985 p->start_time = first_time;
987 /* no exit marker, task kept running to the end */
988 if (p->end_time == 0)
989 p->end_time = last_time;
990 if (p->total_time >= threshold && !power_only)
991 p->display = 1;
993 c = p->all;
995 while (c) {
996 c->display = 0;
998 if (c->start_time == 1)
999 c->start_time = first_time;
1001 if (c->total_time >= threshold && !power_only) {
1002 c->display = 1;
1003 count++;
1006 if (c->end_time == 0)
1007 c->end_time = last_time;
1009 c = c->next;
1011 p = p->next;
1013 return count;
1018 #define TIME_THRESH 10000000
1020 static void write_svg_file(const char *filename)
1022 u64 i;
1023 int count;
1025 numcpus++;
1028 count = determine_display_tasks(TIME_THRESH);
1030 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
1031 if (count < 15)
1032 count = determine_display_tasks(TIME_THRESH / 10);
1034 open_svg(filename, numcpus, count, first_time, last_time);
1036 svg_time_grid();
1037 svg_legenda();
1039 for (i = 0; i < numcpus; i++)
1040 svg_cpu_box(i, max_freq, turbo_frequency);
1042 draw_cpu_usage();
1043 draw_process_bars();
1044 draw_c_p_states();
1045 draw_wakeups();
1047 svg_close();
1050 static void process_samples(void)
1052 struct sample_wrapper *cursor;
1053 event_t *event;
1055 sort_queued_samples();
1057 cursor = all_samples;
1058 while (cursor) {
1059 event = (void *)&cursor->data;
1060 cursor = cursor->next;
1061 process_sample_event(event);
1065 static int sample_type_check(u64 type)
1067 sample_type = type;
1069 if (!(sample_type & PERF_SAMPLE_RAW)) {
1070 fprintf(stderr, "No trace samples found in the file.\n"
1071 "Have you used 'perf timechart record' to record it?\n");
1072 return -1;
1075 return 0;
1078 static struct perf_file_handler file_handler = {
1079 .process_comm_event = process_comm_event,
1080 .process_fork_event = process_fork_event,
1081 .process_exit_event = process_exit_event,
1082 .process_sample_event = queue_sample_event,
1083 .sample_type_check = sample_type_check,
1086 static int __cmd_timechart(void)
1088 struct perf_header *header;
1089 int ret;
1091 register_perf_file_handler(&file_handler);
1093 ret = mmap_dispatch_perf_file(&header, input_name, 0, 0,
1094 &event__cwdlen, &event__cwd);
1095 if (ret)
1096 return EXIT_FAILURE;
1098 process_samples();
1100 end_sample_processing();
1102 sort_pids();
1104 write_svg_file(output_name);
1106 pr_info("Written %2.1f seconds of trace to %s.\n",
1107 (last_time - first_time) / 1000000000.0, output_name);
1109 return EXIT_SUCCESS;
1112 static const char * const timechart_usage[] = {
1113 "perf timechart [<options>] {record}",
1114 NULL
1117 static const char *record_args[] = {
1118 "record",
1119 "-a",
1120 "-R",
1121 "-M",
1122 "-f",
1123 "-c", "1",
1124 "-e", "power:power_start",
1125 "-e", "power:power_end",
1126 "-e", "power:power_frequency",
1127 "-e", "sched:sched_wakeup",
1128 "-e", "sched:sched_switch",
1131 static int __cmd_record(int argc, const char **argv)
1133 unsigned int rec_argc, i, j;
1134 const char **rec_argv;
1136 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1137 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1139 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1140 rec_argv[i] = strdup(record_args[i]);
1142 for (j = 1; j < (unsigned int)argc; j++, i++)
1143 rec_argv[i] = argv[j];
1145 return cmd_record(i, rec_argv, NULL);
1148 static int
1149 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1151 if (arg)
1152 add_process_filter(arg);
1153 return 0;
1156 static const struct option options[] = {
1157 OPT_STRING('i', "input", &input_name, "file",
1158 "input file name"),
1159 OPT_STRING('o', "output", &output_name, "file",
1160 "output file name"),
1161 OPT_INTEGER('w', "width", &svg_page_width,
1162 "page width"),
1163 OPT_BOOLEAN('P', "power-only", &power_only,
1164 "output power data only"),
1165 OPT_CALLBACK('p', "process", NULL, "process",
1166 "process selector. Pass a pid or process name.",
1167 parse_process),
1168 OPT_END()
1172 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1174 symbol__init(0);
1176 argc = parse_options(argc, argv, options, timechart_usage,
1177 PARSE_OPT_STOP_AT_NON_OPTION);
1179 if (argc && !strncmp(argv[0], "rec", 3))
1180 return __cmd_record(argc, argv);
1181 else if (argc)
1182 usage_with_options(timechart_usage, options);
1184 setup_pager();
1186 return __cmd_timechart();