kmemcheck: Fix build errors due to missing slab.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-timechart.c
blob0d4d8ff7914b029423dba4e742c7343925519043
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/session.h"
34 #include "util/svghelper.h"
36 static char const *input_name = "perf.data";
37 static char const *output_name = "output.svg";
39 static unsigned int numcpus;
40 static u64 min_freq; /* Lowest CPU frequency seen */
41 static u64 max_freq; /* Highest CPU frequency seen */
42 static u64 turbo_frequency;
44 static u64 first_time, last_time;
46 static int power_only;
49 struct per_pid;
50 struct per_pidcomm;
52 struct cpu_sample;
53 struct power_event;
54 struct wake_event;
56 struct sample_wrapper;
59 * Datastructure layout:
60 * We keep an list of "pid"s, matching the kernels notion of a task struct.
61 * Each "pid" entry, has a list of "comm"s.
62 * this is because we want to track different programs different, while
63 * exec will reuse the original pid (by design).
64 * Each comm has a list of samples that will be used to draw
65 * final graph.
68 struct per_pid {
69 struct per_pid *next;
71 int pid;
72 int ppid;
74 u64 start_time;
75 u64 end_time;
76 u64 total_time;
77 int display;
79 struct per_pidcomm *all;
80 struct per_pidcomm *current;
82 int painted;
86 struct per_pidcomm {
87 struct per_pidcomm *next;
89 u64 start_time;
90 u64 end_time;
91 u64 total_time;
93 int Y;
94 int display;
96 long state;
97 u64 state_since;
99 char *comm;
101 struct cpu_sample *samples;
104 struct sample_wrapper {
105 struct sample_wrapper *next;
107 u64 timestamp;
108 unsigned char data[0];
111 #define TYPE_NONE 0
112 #define TYPE_RUNNING 1
113 #define TYPE_WAITING 2
114 #define TYPE_BLOCKED 3
116 struct cpu_sample {
117 struct cpu_sample *next;
119 u64 start_time;
120 u64 end_time;
121 int type;
122 int cpu;
125 static struct per_pid *all_data;
127 #define CSTATE 1
128 #define PSTATE 2
130 struct power_event {
131 struct power_event *next;
132 int type;
133 int state;
134 u64 start_time;
135 u64 end_time;
136 int cpu;
139 struct wake_event {
140 struct wake_event *next;
141 int waker;
142 int wakee;
143 u64 time;
146 static struct power_event *power_events;
147 static struct wake_event *wake_events;
149 struct sample_wrapper *all_samples;
152 struct process_filter;
153 struct process_filter {
154 char *name;
155 int pid;
156 struct process_filter *next;
159 static struct process_filter *process_filter;
162 static struct per_pid *find_create_pid(int pid)
164 struct per_pid *cursor = all_data;
166 while (cursor) {
167 if (cursor->pid == pid)
168 return cursor;
169 cursor = cursor->next;
171 cursor = malloc(sizeof(struct per_pid));
172 assert(cursor != NULL);
173 memset(cursor, 0, sizeof(struct per_pid));
174 cursor->pid = pid;
175 cursor->next = all_data;
176 all_data = cursor;
177 return cursor;
180 static void pid_set_comm(int pid, char *comm)
182 struct per_pid *p;
183 struct per_pidcomm *c;
184 p = find_create_pid(pid);
185 c = p->all;
186 while (c) {
187 if (c->comm && strcmp(c->comm, comm) == 0) {
188 p->current = c;
189 return;
191 if (!c->comm) {
192 c->comm = strdup(comm);
193 p->current = c;
194 return;
196 c = c->next;
198 c = malloc(sizeof(struct per_pidcomm));
199 assert(c != NULL);
200 memset(c, 0, sizeof(struct per_pidcomm));
201 c->comm = strdup(comm);
202 p->current = c;
203 c->next = p->all;
204 p->all = c;
207 static void pid_fork(int pid, int ppid, u64 timestamp)
209 struct per_pid *p, *pp;
210 p = find_create_pid(pid);
211 pp = find_create_pid(ppid);
212 p->ppid = ppid;
213 if (pp->current && pp->current->comm && !p->current)
214 pid_set_comm(pid, pp->current->comm);
216 p->start_time = timestamp;
217 if (p->current) {
218 p->current->start_time = timestamp;
219 p->current->state_since = timestamp;
223 static void pid_exit(int pid, u64 timestamp)
225 struct per_pid *p;
226 p = find_create_pid(pid);
227 p->end_time = timestamp;
228 if (p->current)
229 p->current->end_time = timestamp;
232 static void
233 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
235 struct per_pid *p;
236 struct per_pidcomm *c;
237 struct cpu_sample *sample;
239 p = find_create_pid(pid);
240 c = p->current;
241 if (!c) {
242 c = malloc(sizeof(struct per_pidcomm));
243 assert(c != NULL);
244 memset(c, 0, sizeof(struct per_pidcomm));
245 p->current = c;
246 c->next = p->all;
247 p->all = c;
250 sample = malloc(sizeof(struct cpu_sample));
251 assert(sample != NULL);
252 memset(sample, 0, sizeof(struct cpu_sample));
253 sample->start_time = start;
254 sample->end_time = end;
255 sample->type = type;
256 sample->next = c->samples;
257 sample->cpu = cpu;
258 c->samples = sample;
260 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
261 c->total_time += (end-start);
262 p->total_time += (end-start);
265 if (c->start_time == 0 || c->start_time > start)
266 c->start_time = start;
267 if (p->start_time == 0 || p->start_time > start)
268 p->start_time = start;
270 if (cpu > numcpus)
271 numcpus = cpu;
274 #define MAX_CPUS 4096
276 static u64 cpus_cstate_start_times[MAX_CPUS];
277 static int cpus_cstate_state[MAX_CPUS];
278 static u64 cpus_pstate_start_times[MAX_CPUS];
279 static u64 cpus_pstate_state[MAX_CPUS];
281 static int process_comm_event(event_t *event, struct perf_session *session __used)
283 pid_set_comm(event->comm.tid, event->comm.comm);
284 return 0;
287 static int process_fork_event(event_t *event, struct perf_session *session __used)
289 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
290 return 0;
293 static int process_exit_event(event_t *event, struct perf_session *session __used)
295 pid_exit(event->fork.pid, event->fork.time);
296 return 0;
299 struct trace_entry {
300 unsigned short type;
301 unsigned char flags;
302 unsigned char preempt_count;
303 int pid;
304 int lock_depth;
307 struct power_entry {
308 struct trace_entry te;
309 s64 type;
310 s64 value;
313 #define TASK_COMM_LEN 16
314 struct wakeup_entry {
315 struct trace_entry te;
316 char comm[TASK_COMM_LEN];
317 int pid;
318 int prio;
319 int success;
323 * trace_flag_type is an enumeration that holds different
324 * states when a trace occurs. These are:
325 * IRQS_OFF - interrupts were disabled
326 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
327 * NEED_RESCED - reschedule is requested
328 * HARDIRQ - inside an interrupt handler
329 * SOFTIRQ - inside a softirq handler
331 enum trace_flag_type {
332 TRACE_FLAG_IRQS_OFF = 0x01,
333 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
334 TRACE_FLAG_NEED_RESCHED = 0x04,
335 TRACE_FLAG_HARDIRQ = 0x08,
336 TRACE_FLAG_SOFTIRQ = 0x10,
341 struct sched_switch {
342 struct trace_entry te;
343 char prev_comm[TASK_COMM_LEN];
344 int prev_pid;
345 int prev_prio;
346 long prev_state; /* Arjan weeps. */
347 char next_comm[TASK_COMM_LEN];
348 int next_pid;
349 int next_prio;
352 static void c_state_start(int cpu, u64 timestamp, int state)
354 cpus_cstate_start_times[cpu] = timestamp;
355 cpus_cstate_state[cpu] = state;
358 static void c_state_end(int cpu, u64 timestamp)
360 struct power_event *pwr;
361 pwr = malloc(sizeof(struct power_event));
362 if (!pwr)
363 return;
364 memset(pwr, 0, sizeof(struct power_event));
366 pwr->state = cpus_cstate_state[cpu];
367 pwr->start_time = cpus_cstate_start_times[cpu];
368 pwr->end_time = timestamp;
369 pwr->cpu = cpu;
370 pwr->type = CSTATE;
371 pwr->next = power_events;
373 power_events = pwr;
376 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
378 struct power_event *pwr;
379 pwr = malloc(sizeof(struct power_event));
381 if (new_freq > 8000000) /* detect invalid data */
382 return;
384 if (!pwr)
385 return;
386 memset(pwr, 0, sizeof(struct power_event));
388 pwr->state = cpus_pstate_state[cpu];
389 pwr->start_time = cpus_pstate_start_times[cpu];
390 pwr->end_time = timestamp;
391 pwr->cpu = cpu;
392 pwr->type = PSTATE;
393 pwr->next = power_events;
395 if (!pwr->start_time)
396 pwr->start_time = first_time;
398 power_events = pwr;
400 cpus_pstate_state[cpu] = new_freq;
401 cpus_pstate_start_times[cpu] = timestamp;
403 if ((u64)new_freq > max_freq)
404 max_freq = new_freq;
406 if (new_freq < min_freq || min_freq == 0)
407 min_freq = new_freq;
409 if (new_freq == max_freq - 1000)
410 turbo_frequency = max_freq;
413 static void
414 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
416 struct wake_event *we;
417 struct per_pid *p;
418 struct wakeup_entry *wake = (void *)te;
420 we = malloc(sizeof(struct wake_event));
421 if (!we)
422 return;
424 memset(we, 0, sizeof(struct wake_event));
425 we->time = timestamp;
426 we->waker = pid;
428 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
429 we->waker = -1;
431 we->wakee = wake->pid;
432 we->next = wake_events;
433 wake_events = we;
434 p = find_create_pid(we->wakee);
436 if (p && p->current && p->current->state == TYPE_NONE) {
437 p->current->state_since = timestamp;
438 p->current->state = TYPE_WAITING;
440 if (p && p->current && p->current->state == TYPE_BLOCKED) {
441 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
442 p->current->state_since = timestamp;
443 p->current->state = TYPE_WAITING;
447 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
449 struct per_pid *p = NULL, *prev_p;
450 struct sched_switch *sw = (void *)te;
453 prev_p = find_create_pid(sw->prev_pid);
455 p = find_create_pid(sw->next_pid);
457 if (prev_p->current && prev_p->current->state != TYPE_NONE)
458 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
459 if (p && p->current) {
460 if (p->current->state != TYPE_NONE)
461 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
463 p->current->state_since = timestamp;
464 p->current->state = TYPE_RUNNING;
467 if (prev_p->current) {
468 prev_p->current->state = TYPE_NONE;
469 prev_p->current->state_since = timestamp;
470 if (sw->prev_state & 2)
471 prev_p->current->state = TYPE_BLOCKED;
472 if (sw->prev_state == 0)
473 prev_p->current->state = TYPE_WAITING;
478 static int process_sample_event(event_t *event, struct perf_session *session)
480 struct sample_data data;
481 struct trace_entry *te;
483 memset(&data, 0, sizeof(data));
485 event__parse_sample(event, session->sample_type, &data);
487 if (session->sample_type & PERF_SAMPLE_TIME) {
488 if (!first_time || first_time > data.time)
489 first_time = data.time;
490 if (last_time < data.time)
491 last_time = data.time;
494 te = (void *)data.raw_data;
495 if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
496 char *event_str;
497 struct power_entry *pe;
499 pe = (void *)te;
501 event_str = perf_header__find_event(te->type);
503 if (!event_str)
504 return 0;
506 if (strcmp(event_str, "power:power_start") == 0)
507 c_state_start(data.cpu, data.time, pe->value);
509 if (strcmp(event_str, "power:power_end") == 0)
510 c_state_end(data.cpu, data.time);
512 if (strcmp(event_str, "power:power_frequency") == 0)
513 p_state_change(data.cpu, data.time, pe->value);
515 if (strcmp(event_str, "sched:sched_wakeup") == 0)
516 sched_wakeup(data.cpu, data.time, data.pid, te);
518 if (strcmp(event_str, "sched:sched_switch") == 0)
519 sched_switch(data.cpu, data.time, te);
521 return 0;
525 * After the last sample we need to wrap up the current C/P state
526 * and close out each CPU for these.
528 static void end_sample_processing(void)
530 u64 cpu;
531 struct power_event *pwr;
533 for (cpu = 0; cpu <= numcpus; cpu++) {
534 pwr = malloc(sizeof(struct power_event));
535 if (!pwr)
536 return;
537 memset(pwr, 0, sizeof(struct power_event));
539 /* C state */
540 #if 0
541 pwr->state = cpus_cstate_state[cpu];
542 pwr->start_time = cpus_cstate_start_times[cpu];
543 pwr->end_time = last_time;
544 pwr->cpu = cpu;
545 pwr->type = CSTATE;
546 pwr->next = power_events;
548 power_events = pwr;
549 #endif
550 /* P state */
552 pwr = malloc(sizeof(struct power_event));
553 if (!pwr)
554 return;
555 memset(pwr, 0, sizeof(struct power_event));
557 pwr->state = cpus_pstate_state[cpu];
558 pwr->start_time = cpus_pstate_start_times[cpu];
559 pwr->end_time = last_time;
560 pwr->cpu = cpu;
561 pwr->type = PSTATE;
562 pwr->next = power_events;
564 if (!pwr->start_time)
565 pwr->start_time = first_time;
566 if (!pwr->state)
567 pwr->state = min_freq;
568 power_events = pwr;
572 static u64 sample_time(event_t *event, const struct perf_session *session)
574 int cursor;
576 cursor = 0;
577 if (session->sample_type & PERF_SAMPLE_IP)
578 cursor++;
579 if (session->sample_type & PERF_SAMPLE_TID)
580 cursor++;
581 if (session->sample_type & PERF_SAMPLE_TIME)
582 return event->sample.array[cursor];
583 return 0;
588 * We first queue all events, sorted backwards by insertion.
589 * The order will get flipped later.
591 static int queue_sample_event(event_t *event, struct perf_session *session)
593 struct sample_wrapper *copy, *prev;
594 int size;
596 size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
598 copy = malloc(size);
599 if (!copy)
600 return 1;
602 memset(copy, 0, size);
604 copy->next = NULL;
605 copy->timestamp = sample_time(event, session);
607 memcpy(&copy->data, event, event->sample.header.size);
609 /* insert in the right place in the list */
611 if (!all_samples) {
612 /* first sample ever */
613 all_samples = copy;
614 return 0;
617 if (all_samples->timestamp < copy->timestamp) {
618 /* insert at the head of the list */
619 copy->next = all_samples;
620 all_samples = copy;
621 return 0;
624 prev = all_samples;
625 while (prev->next) {
626 if (prev->next->timestamp < copy->timestamp) {
627 copy->next = prev->next;
628 prev->next = copy;
629 return 0;
631 prev = prev->next;
633 /* insert at the end of the list */
634 prev->next = copy;
636 return 0;
639 static void sort_queued_samples(void)
641 struct sample_wrapper *cursor, *next;
643 cursor = all_samples;
644 all_samples = NULL;
646 while (cursor) {
647 next = cursor->next;
648 cursor->next = all_samples;
649 all_samples = cursor;
650 cursor = next;
655 * Sort the pid datastructure
657 static void sort_pids(void)
659 struct per_pid *new_list, *p, *cursor, *prev;
660 /* sort by ppid first, then by pid, lowest to highest */
662 new_list = NULL;
664 while (all_data) {
665 p = all_data;
666 all_data = p->next;
667 p->next = NULL;
669 if (new_list == NULL) {
670 new_list = p;
671 p->next = NULL;
672 continue;
674 prev = NULL;
675 cursor = new_list;
676 while (cursor) {
677 if (cursor->ppid > p->ppid ||
678 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
679 /* must insert before */
680 if (prev) {
681 p->next = prev->next;
682 prev->next = p;
683 cursor = NULL;
684 continue;
685 } else {
686 p->next = new_list;
687 new_list = p;
688 cursor = NULL;
689 continue;
693 prev = cursor;
694 cursor = cursor->next;
695 if (!cursor)
696 prev->next = p;
699 all_data = new_list;
703 static void draw_c_p_states(void)
705 struct power_event *pwr;
706 pwr = power_events;
709 * two pass drawing so that the P state bars are on top of the C state blocks
711 while (pwr) {
712 if (pwr->type == CSTATE)
713 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
714 pwr = pwr->next;
717 pwr = power_events;
718 while (pwr) {
719 if (pwr->type == PSTATE) {
720 if (!pwr->state)
721 pwr->state = min_freq;
722 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
724 pwr = pwr->next;
728 static void draw_wakeups(void)
730 struct wake_event *we;
731 struct per_pid *p;
732 struct per_pidcomm *c;
734 we = wake_events;
735 while (we) {
736 int from = 0, to = 0;
737 char *task_from = NULL, *task_to = NULL;
739 /* locate the column of the waker and wakee */
740 p = all_data;
741 while (p) {
742 if (p->pid == we->waker || p->pid == we->wakee) {
743 c = p->all;
744 while (c) {
745 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
746 if (p->pid == we->waker && !from) {
747 from = c->Y;
748 task_from = strdup(c->comm);
750 if (p->pid == we->wakee && !to) {
751 to = c->Y;
752 task_to = strdup(c->comm);
755 c = c->next;
757 c = p->all;
758 while (c) {
759 if (p->pid == we->waker && !from) {
760 from = c->Y;
761 task_from = strdup(c->comm);
763 if (p->pid == we->wakee && !to) {
764 to = c->Y;
765 task_to = strdup(c->comm);
767 c = c->next;
770 p = p->next;
773 if (!task_from) {
774 task_from = malloc(40);
775 sprintf(task_from, "[%i]", we->waker);
777 if (!task_to) {
778 task_to = malloc(40);
779 sprintf(task_to, "[%i]", we->wakee);
782 if (we->waker == -1)
783 svg_interrupt(we->time, to);
784 else if (from && to && abs(from - to) == 1)
785 svg_wakeline(we->time, from, to);
786 else
787 svg_partial_wakeline(we->time, from, task_from, to, task_to);
788 we = we->next;
790 free(task_from);
791 free(task_to);
795 static void draw_cpu_usage(void)
797 struct per_pid *p;
798 struct per_pidcomm *c;
799 struct cpu_sample *sample;
800 p = all_data;
801 while (p) {
802 c = p->all;
803 while (c) {
804 sample = c->samples;
805 while (sample) {
806 if (sample->type == TYPE_RUNNING)
807 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
809 sample = sample->next;
811 c = c->next;
813 p = p->next;
817 static void draw_process_bars(void)
819 struct per_pid *p;
820 struct per_pidcomm *c;
821 struct cpu_sample *sample;
822 int Y = 0;
824 Y = 2 * numcpus + 2;
826 p = all_data;
827 while (p) {
828 c = p->all;
829 while (c) {
830 if (!c->display) {
831 c->Y = 0;
832 c = c->next;
833 continue;
836 svg_box(Y, c->start_time, c->end_time, "process");
837 sample = c->samples;
838 while (sample) {
839 if (sample->type == TYPE_RUNNING)
840 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
841 if (sample->type == TYPE_BLOCKED)
842 svg_box(Y, sample->start_time, sample->end_time, "blocked");
843 if (sample->type == TYPE_WAITING)
844 svg_waiting(Y, sample->start_time, sample->end_time);
845 sample = sample->next;
848 if (c->comm) {
849 char comm[256];
850 if (c->total_time > 5000000000) /* 5 seconds */
851 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
852 else
853 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
855 svg_text(Y, c->start_time, comm);
857 c->Y = Y;
858 Y++;
859 c = c->next;
861 p = p->next;
865 static void add_process_filter(const char *string)
867 struct process_filter *filt;
868 int pid;
870 pid = strtoull(string, NULL, 10);
871 filt = malloc(sizeof(struct process_filter));
872 if (!filt)
873 return;
875 filt->name = strdup(string);
876 filt->pid = pid;
877 filt->next = process_filter;
879 process_filter = filt;
882 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
884 struct process_filter *filt;
885 if (!process_filter)
886 return 1;
888 filt = process_filter;
889 while (filt) {
890 if (filt->pid && p->pid == filt->pid)
891 return 1;
892 if (strcmp(filt->name, c->comm) == 0)
893 return 1;
894 filt = filt->next;
896 return 0;
899 static int determine_display_tasks_filtered(void)
901 struct per_pid *p;
902 struct per_pidcomm *c;
903 int count = 0;
905 p = all_data;
906 while (p) {
907 p->display = 0;
908 if (p->start_time == 1)
909 p->start_time = first_time;
911 /* no exit marker, task kept running to the end */
912 if (p->end_time == 0)
913 p->end_time = last_time;
915 c = p->all;
917 while (c) {
918 c->display = 0;
920 if (c->start_time == 1)
921 c->start_time = first_time;
923 if (passes_filter(p, c)) {
924 c->display = 1;
925 p->display = 1;
926 count++;
929 if (c->end_time == 0)
930 c->end_time = last_time;
932 c = c->next;
934 p = p->next;
936 return count;
939 static int determine_display_tasks(u64 threshold)
941 struct per_pid *p;
942 struct per_pidcomm *c;
943 int count = 0;
945 if (process_filter)
946 return determine_display_tasks_filtered();
948 p = all_data;
949 while (p) {
950 p->display = 0;
951 if (p->start_time == 1)
952 p->start_time = first_time;
954 /* no exit marker, task kept running to the end */
955 if (p->end_time == 0)
956 p->end_time = last_time;
957 if (p->total_time >= threshold && !power_only)
958 p->display = 1;
960 c = p->all;
962 while (c) {
963 c->display = 0;
965 if (c->start_time == 1)
966 c->start_time = first_time;
968 if (c->total_time >= threshold && !power_only) {
969 c->display = 1;
970 count++;
973 if (c->end_time == 0)
974 c->end_time = last_time;
976 c = c->next;
978 p = p->next;
980 return count;
985 #define TIME_THRESH 10000000
987 static void write_svg_file(const char *filename)
989 u64 i;
990 int count;
992 numcpus++;
995 count = determine_display_tasks(TIME_THRESH);
997 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
998 if (count < 15)
999 count = determine_display_tasks(TIME_THRESH / 10);
1001 open_svg(filename, numcpus, count, first_time, last_time);
1003 svg_time_grid();
1004 svg_legenda();
1006 for (i = 0; i < numcpus; i++)
1007 svg_cpu_box(i, max_freq, turbo_frequency);
1009 draw_cpu_usage();
1010 draw_process_bars();
1011 draw_c_p_states();
1012 draw_wakeups();
1014 svg_close();
1017 static void process_samples(struct perf_session *session)
1019 struct sample_wrapper *cursor;
1020 event_t *event;
1022 sort_queued_samples();
1024 cursor = all_samples;
1025 while (cursor) {
1026 event = (void *)&cursor->data;
1027 cursor = cursor->next;
1028 process_sample_event(event, session);
1032 static struct perf_event_ops event_ops = {
1033 .comm = process_comm_event,
1034 .fork = process_fork_event,
1035 .exit = process_exit_event,
1036 .sample = queue_sample_event,
1039 static int __cmd_timechart(void)
1041 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
1042 int ret = -EINVAL;
1044 if (session == NULL)
1045 return -ENOMEM;
1047 if (!perf_session__has_traces(session, "timechart record"))
1048 goto out_delete;
1050 ret = perf_session__process_events(session, &event_ops);
1051 if (ret)
1052 goto out_delete;
1054 process_samples(session);
1056 end_sample_processing();
1058 sort_pids();
1060 write_svg_file(output_name);
1062 pr_info("Written %2.1f seconds of trace to %s.\n",
1063 (last_time - first_time) / 1000000000.0, output_name);
1064 out_delete:
1065 perf_session__delete(session);
1066 return ret;
1069 static const char * const timechart_usage[] = {
1070 "perf timechart [<options>] {record}",
1071 NULL
1074 static const char *record_args[] = {
1075 "record",
1076 "-a",
1077 "-R",
1078 "-M",
1079 "-f",
1080 "-c", "1",
1081 "-e", "power:power_start",
1082 "-e", "power:power_end",
1083 "-e", "power:power_frequency",
1084 "-e", "sched:sched_wakeup",
1085 "-e", "sched:sched_switch",
1088 static int __cmd_record(int argc, const char **argv)
1090 unsigned int rec_argc, i, j;
1091 const char **rec_argv;
1093 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1094 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1096 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1097 rec_argv[i] = strdup(record_args[i]);
1099 for (j = 1; j < (unsigned int)argc; j++, i++)
1100 rec_argv[i] = argv[j];
1102 return cmd_record(i, rec_argv, NULL);
1105 static int
1106 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1108 if (arg)
1109 add_process_filter(arg);
1110 return 0;
1113 static const struct option options[] = {
1114 OPT_STRING('i', "input", &input_name, "file",
1115 "input file name"),
1116 OPT_STRING('o', "output", &output_name, "file",
1117 "output file name"),
1118 OPT_INTEGER('w', "width", &svg_page_width,
1119 "page width"),
1120 OPT_BOOLEAN('P', "power-only", &power_only,
1121 "output power data only"),
1122 OPT_CALLBACK('p', "process", NULL, "process",
1123 "process selector. Pass a pid or process name.",
1124 parse_process),
1125 OPT_END()
1129 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1131 argc = parse_options(argc, argv, options, timechart_usage,
1132 PARSE_OPT_STOP_AT_NON_OPTION);
1134 symbol__init();
1136 if (argc && !strncmp(argv[0], "rec", 3))
1137 return __cmd_record(argc, argv);
1138 else if (argc)
1139 usage_with_options(timechart_usage, options);
1141 setup_pager();
1143 return __cmd_timechart();