perf timechart: Remove open-coded event parsing code
[safe/jmp/linux-2.6] / tools / perf / builtin-timechart.c
1 /*
2  * builtin-timechart.c - make an svg timechart of system activity
3  *
4  * (C) Copyright 2009 Intel Corporation
5  *
6  * Authors:
7  *     Arjan van de Ven <arjan@linux.intel.com>
8  *
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.
13  */
14
15 #include "builtin.h"
16
17 #include "util/util.h"
18
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"
27
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"
35
36 static char             const *input_name = "perf.data";
37 static char             const *output_name = "output.svg";
38
39
40 static u64              sample_type;
41
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;
46
47 static u64              first_time, last_time;
48
49 static int              power_only;
50
51
52 struct per_pid;
53 struct per_pidcomm;
54
55 struct cpu_sample;
56 struct power_event;
57 struct wake_event;
58
59 struct sample_wrapper;
60
61 /*
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.
69  */
70
71 struct per_pid {
72         struct per_pid *next;
73
74         int             pid;
75         int             ppid;
76
77         u64             start_time;
78         u64             end_time;
79         u64             total_time;
80         int             display;
81
82         struct per_pidcomm *all;
83         struct per_pidcomm *current;
84
85         int painted;
86 };
87
88
89 struct per_pidcomm {
90         struct per_pidcomm *next;
91
92         u64             start_time;
93         u64             end_time;
94         u64             total_time;
95
96         int             Y;
97         int             display;
98
99         long            state;
100         u64             state_since;
101
102         char            *comm;
103
104         struct cpu_sample *samples;
105 };
106
107 struct sample_wrapper {
108         struct sample_wrapper *next;
109
110         u64             timestamp;
111         unsigned char   data[0];
112 };
113
114 #define TYPE_NONE       0
115 #define TYPE_RUNNING    1
116 #define TYPE_WAITING    2
117 #define TYPE_BLOCKED    3
118
119 struct cpu_sample {
120         struct cpu_sample *next;
121
122         u64 start_time;
123         u64 end_time;
124         int type;
125         int cpu;
126 };
127
128 static struct per_pid *all_data;
129
130 #define CSTATE 1
131 #define PSTATE 2
132
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;
140 };
141
142 struct wake_event {
143         struct wake_event *next;
144         int waker;
145         int wakee;
146         u64 time;
147 };
148
149 static struct power_event    *power_events;
150 static struct wake_event     *wake_events;
151
152 struct sample_wrapper *all_samples;
153
154
155 struct process_filter;
156 struct process_filter {
157         char                    *name;
158         int                     pid;
159         struct process_filter   *next;
160 };
161
162 static struct process_filter *process_filter;
163
164
165 static struct per_pid *find_create_pid(int pid)
166 {
167         struct per_pid *cursor = all_data;
168
169         while (cursor) {
170                 if (cursor->pid == pid)
171                         return cursor;
172                 cursor = cursor->next;
173         }
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;
181 }
182
183 static void pid_set_comm(int pid, char *comm)
184 {
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;
193                 }
194                 if (!c->comm) {
195                         c->comm = strdup(comm);
196                         p->current = c;
197                         return;
198                 }
199                 c = c->next;
200         }
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;
208 }
209
210 static void pid_fork(int pid, int ppid, u64 timestamp)
211 {
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);
218
219         p->start_time = timestamp;
220         if (p->current) {
221                 p->current->start_time = timestamp;
222                 p->current->state_since = timestamp;
223         }
224 }
225
226 static void pid_exit(int pid, u64 timestamp)
227 {
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;
233 }
234
235 static void
236 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
237 {
238         struct per_pid *p;
239         struct per_pidcomm *c;
240         struct cpu_sample *sample;
241
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;
251         }
252
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;
262
263         if (sample->type == TYPE_RUNNING && end > start && start > 0) {
264                 c->total_time += (end-start);
265                 p->total_time += (end-start);
266         }
267
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;
272
273         if (cpu > numcpus)
274                 numcpus = cpu;
275 }
276
277 #define MAX_CPUS 4096
278
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];
283
284 static int
285 process_comm_event(event_t *event)
286 {
287         pid_set_comm(event->comm.pid, event->comm.comm);
288         return 0;
289 }
290 static int
291 process_fork_event(event_t *event)
292 {
293         pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
294         return 0;
295 }
296
297 static int
298 process_exit_event(event_t *event)
299 {
300         pid_exit(event->fork.pid, event->fork.time);
301         return 0;
302 }
303
304 struct trace_entry {
305         u32                     size;
306         unsigned short          type;
307         unsigned char           flags;
308         unsigned char           preempt_count;
309         int                     pid;
310         int                     tgid;
311 };
312
313 struct power_entry {
314         struct trace_entry te;
315         s64     type;
316         s64     value;
317 };
318
319 #define TASK_COMM_LEN 16
320 struct wakeup_entry {
321         struct trace_entry te;
322         char comm[TASK_COMM_LEN];
323         int   pid;
324         int   prio;
325         int   success;
326 };
327
328 /*
329  * trace_flag_type is an enumeration that holds different
330  * states when a trace occurs. These are:
331  *  IRQS_OFF            - interrupts were disabled
332  *  IRQS_NOSUPPORT      - arch does not support irqs_disabled_flags
333  *  NEED_RESCED         - reschedule is requested
334  *  HARDIRQ             - inside an interrupt handler
335  *  SOFTIRQ             - inside a softirq handler
336  */
337 enum trace_flag_type {
338         TRACE_FLAG_IRQS_OFF             = 0x01,
339         TRACE_FLAG_IRQS_NOSUPPORT       = 0x02,
340         TRACE_FLAG_NEED_RESCHED         = 0x04,
341         TRACE_FLAG_HARDIRQ              = 0x08,
342         TRACE_FLAG_SOFTIRQ              = 0x10,
343 };
344
345
346
347 struct sched_switch {
348         struct trace_entry te;
349         char prev_comm[TASK_COMM_LEN];
350         int  prev_pid;
351         int  prev_prio;
352         long prev_state; /* Arjan weeps. */
353         char next_comm[TASK_COMM_LEN];
354         int  next_pid;
355         int  next_prio;
356 };
357
358 static void c_state_start(int cpu, u64 timestamp, int state)
359 {
360         cpus_cstate_start_times[cpu] = timestamp;
361         cpus_cstate_state[cpu] = state;
362 }
363
364 static void c_state_end(int cpu, u64 timestamp)
365 {
366         struct power_event *pwr;
367         pwr = malloc(sizeof(struct power_event));
368         if (!pwr)
369                 return;
370         memset(pwr, 0, sizeof(struct power_event));
371
372         pwr->state = cpus_cstate_state[cpu];
373         pwr->start_time = cpus_cstate_start_times[cpu];
374         pwr->end_time = timestamp;
375         pwr->cpu = cpu;
376         pwr->type = CSTATE;
377         pwr->next = power_events;
378
379         power_events = pwr;
380 }
381
382 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
383 {
384         struct power_event *pwr;
385         pwr = malloc(sizeof(struct power_event));
386
387         if (new_freq > 8000000) /* detect invalid data */
388                 return;
389
390         if (!pwr)
391                 return;
392         memset(pwr, 0, sizeof(struct power_event));
393
394         pwr->state = cpus_pstate_state[cpu];
395         pwr->start_time = cpus_pstate_start_times[cpu];
396         pwr->end_time = timestamp;
397         pwr->cpu = cpu;
398         pwr->type = PSTATE;
399         pwr->next = power_events;
400
401         if (!pwr->start_time)
402                 pwr->start_time = first_time;
403
404         power_events = pwr;
405
406         cpus_pstate_state[cpu] = new_freq;
407         cpus_pstate_start_times[cpu] = timestamp;
408
409         if ((u64)new_freq > max_freq)
410                 max_freq = new_freq;
411
412         if (new_freq < min_freq || min_freq == 0)
413                 min_freq = new_freq;
414
415         if (new_freq == max_freq - 1000)
416                         turbo_frequency = max_freq;
417 }
418
419 static void
420 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
421 {
422         struct wake_event *we;
423         struct per_pid *p;
424         struct wakeup_entry *wake = (void *)te;
425
426         we = malloc(sizeof(struct wake_event));
427         if (!we)
428                 return;
429
430         memset(we, 0, sizeof(struct wake_event));
431         we->time = timestamp;
432         we->waker = pid;
433
434         if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
435                 we->waker = -1;
436
437         we->wakee = wake->pid;
438         we->next = wake_events;
439         wake_events = we;
440         p = find_create_pid(we->wakee);
441
442         if (p && p->current && p->current->state == TYPE_NONE) {
443                 p->current->state_since = timestamp;
444                 p->current->state = TYPE_WAITING;
445         }
446         if (p && p->current && p->current->state == TYPE_BLOCKED) {
447                 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
448                 p->current->state_since = timestamp;
449                 p->current->state = TYPE_WAITING;
450         }
451 }
452
453 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
454 {
455         struct per_pid *p = NULL, *prev_p;
456         struct sched_switch *sw = (void *)te;
457
458
459         prev_p = find_create_pid(sw->prev_pid);
460
461         p = find_create_pid(sw->next_pid);
462
463         if (prev_p->current && prev_p->current->state != TYPE_NONE)
464                 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
465         if (p && p->current) {
466                 if (p->current->state != TYPE_NONE)
467                         pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
468
469                         p->current->state_since = timestamp;
470                         p->current->state = TYPE_RUNNING;
471         }
472
473         if (prev_p->current) {
474                 prev_p->current->state = TYPE_NONE;
475                 prev_p->current->state_since = timestamp;
476                 if (sw->prev_state & 2)
477                         prev_p->current->state = TYPE_BLOCKED;
478                 if (sw->prev_state == 0)
479                         prev_p->current->state = TYPE_WAITING;
480         }
481 }
482
483
484 static int
485 process_sample_event(event_t *event)
486 {
487         int cursor = 0;
488         u64 addr = 0;
489         u64 stamp = 0;
490         u32 cpu = 0;
491         u32 pid = 0;
492         struct trace_entry *te;
493
494         if (sample_type & PERF_SAMPLE_IP)
495                 cursor++;
496
497         if (sample_type & PERF_SAMPLE_TID) {
498                 pid = event->sample.array[cursor]>>32;
499                 cursor++;
500         }
501         if (sample_type & PERF_SAMPLE_TIME) {
502                 stamp = event->sample.array[cursor++];
503
504                 if (!first_time || first_time > stamp)
505                         first_time = stamp;
506                 if (last_time < stamp)
507                         last_time = stamp;
508
509         }
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++;
520
521         te = (void *)&event->sample.array[cursor];
522
523         if (sample_type & PERF_SAMPLE_RAW && te->size > 0) {
524                 char *event_str;
525                 struct power_entry *pe;
526
527                 pe = (void *)te;
528
529                 event_str = perf_header__find_event(te->type);
530
531                 if (!event_str)
532                         return 0;
533
534                 if (strcmp(event_str, "power:power_start") == 0)
535                         c_state_start(cpu, stamp, pe->value);
536
537                 if (strcmp(event_str, "power:power_end") == 0)
538                         c_state_end(cpu, stamp);
539
540                 if (strcmp(event_str, "power:power_frequency") == 0)
541                         p_state_change(cpu, stamp, pe->value);
542
543                 if (strcmp(event_str, "sched:sched_wakeup") == 0)
544                         sched_wakeup(cpu, stamp, pid, te);
545
546                 if (strcmp(event_str, "sched:sched_switch") == 0)
547                         sched_switch(cpu, stamp, te);
548         }
549         return 0;
550 }
551
552 /*
553  * After the last sample we need to wrap up the current C/P state
554  * and close out each CPU for these.
555  */
556 static void end_sample_processing(void)
557 {
558         u64 cpu;
559         struct power_event *pwr;
560
561         for (cpu = 0; cpu <= numcpus; cpu++) {
562                 pwr = malloc(sizeof(struct power_event));
563                 if (!pwr)
564                         return;
565                 memset(pwr, 0, sizeof(struct power_event));
566
567                 /* C state */
568 #if 0
569                 pwr->state = cpus_cstate_state[cpu];
570                 pwr->start_time = cpus_cstate_start_times[cpu];
571                 pwr->end_time = last_time;
572                 pwr->cpu = cpu;
573                 pwr->type = CSTATE;
574                 pwr->next = power_events;
575
576                 power_events = pwr;
577 #endif
578                 /* P state */
579
580                 pwr = malloc(sizeof(struct power_event));
581                 if (!pwr)
582                         return;
583                 memset(pwr, 0, sizeof(struct power_event));
584
585                 pwr->state = cpus_pstate_state[cpu];
586                 pwr->start_time = cpus_pstate_start_times[cpu];
587                 pwr->end_time = last_time;
588                 pwr->cpu = cpu;
589                 pwr->type = PSTATE;
590                 pwr->next = power_events;
591
592                 if (!pwr->start_time)
593                         pwr->start_time = first_time;
594                 if (!pwr->state)
595                         pwr->state = min_freq;
596                 power_events = pwr;
597         }
598 }
599
600 static u64 sample_time(event_t *event)
601 {
602         int cursor;
603
604         cursor = 0;
605         if (sample_type & PERF_SAMPLE_IP)
606                 cursor++;
607         if (sample_type & PERF_SAMPLE_TID)
608                 cursor++;
609         if (sample_type & PERF_SAMPLE_TIME)
610                 return event->sample.array[cursor];
611         return 0;
612 }
613
614
615 /*
616  * We first queue all events, sorted backwards by insertion.
617  * The order will get flipped later.
618  */
619 static int
620 queue_sample_event(event_t *event)
621 {
622         struct sample_wrapper *copy, *prev;
623         int size;
624
625         size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
626
627         copy = malloc(size);
628         if (!copy)
629                 return 1;
630
631         memset(copy, 0, size);
632
633         copy->next = NULL;
634         copy->timestamp = sample_time(event);
635
636         memcpy(&copy->data, event, event->sample.header.size);
637
638         /* insert in the right place in the list */
639
640         if (!all_samples) {
641                 /* first sample ever */
642                 all_samples = copy;
643                 return 0;
644         }
645
646         if (all_samples->timestamp < copy->timestamp) {
647                 /* insert at the head of the list */
648                 copy->next = all_samples;
649                 all_samples = copy;
650                 return 0;
651         }
652
653         prev = all_samples;
654         while (prev->next) {
655                 if (prev->next->timestamp < copy->timestamp) {
656                         copy->next = prev->next;
657                         prev->next = copy;
658                         return 0;
659                 }
660                 prev = prev->next;
661         }
662         /* insert at the end of the list */
663         prev->next = copy;
664
665         return 0;
666 }
667
668 static void sort_queued_samples(void)
669 {
670         struct sample_wrapper *cursor, *next;
671
672         cursor = all_samples;
673         all_samples = NULL;
674
675         while (cursor) {
676                 next = cursor->next;
677                 cursor->next = all_samples;
678                 all_samples = cursor;
679                 cursor = next;
680         }
681 }
682
683 /*
684  * Sort the pid datastructure
685  */
686 static void sort_pids(void)
687 {
688         struct per_pid *new_list, *p, *cursor, *prev;
689         /* sort by ppid first, then by pid, lowest to highest */
690
691         new_list = NULL;
692
693         while (all_data) {
694                 p = all_data;
695                 all_data = p->next;
696                 p->next = NULL;
697
698                 if (new_list == NULL) {
699                         new_list = p;
700                         p->next = NULL;
701                         continue;
702                 }
703                 prev = NULL;
704                 cursor = new_list;
705                 while (cursor) {
706                         if (cursor->ppid > p->ppid ||
707                                 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
708                                 /* must insert before */
709                                 if (prev) {
710                                         p->next = prev->next;
711                                         prev->next = p;
712                                         cursor = NULL;
713                                         continue;
714                                 } else {
715                                         p->next = new_list;
716                                         new_list = p;
717                                         cursor = NULL;
718                                         continue;
719                                 }
720                         }
721
722                         prev = cursor;
723                         cursor = cursor->next;
724                         if (!cursor)
725                                 prev->next = p;
726                 }
727         }
728         all_data = new_list;
729 }
730
731
732 static void draw_c_p_states(void)
733 {
734         struct power_event *pwr;
735         pwr = power_events;
736
737         /*
738          * two pass drawing so that the P state bars are on top of the C state blocks
739          */
740         while (pwr) {
741                 if (pwr->type == CSTATE)
742                         svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
743                 pwr = pwr->next;
744         }
745
746         pwr = power_events;
747         while (pwr) {
748                 if (pwr->type == PSTATE) {
749                         if (!pwr->state)
750                                 pwr->state = min_freq;
751                         svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
752                 }
753                 pwr = pwr->next;
754         }
755 }
756
757 static void draw_wakeups(void)
758 {
759         struct wake_event *we;
760         struct per_pid *p;
761         struct per_pidcomm *c;
762
763         we = wake_events;
764         while (we) {
765                 int from = 0, to = 0;
766                 char *task_from = NULL, *task_to = NULL;
767
768                 /* locate the column of the waker and wakee */
769                 p = all_data;
770                 while (p) {
771                         if (p->pid == we->waker || p->pid == we->wakee) {
772                                 c = p->all;
773                                 while (c) {
774                                         if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
775                                                 if (p->pid == we->waker && !from) {
776                                                         from = c->Y;
777                                                         task_from = strdup(c->comm);
778                                                 }
779                                                 if (p->pid == we->wakee && !to) {
780                                                         to = c->Y;
781                                                         task_to = strdup(c->comm);
782                                                 }
783                                         }
784                                         c = c->next;
785                                 }
786                                 c = p->all;
787                                 while (c) {
788                                         if (p->pid == we->waker && !from) {
789                                                 from = c->Y;
790                                                 task_from = strdup(c->comm);
791                                         }
792                                         if (p->pid == we->wakee && !to) {
793                                                 to = c->Y;
794                                                 task_to = strdup(c->comm);
795                                         }
796                                         c = c->next;
797                                 }
798                         }
799                         p = p->next;
800                 }
801
802                 if (!task_from) {
803                         task_from = malloc(40);
804                         sprintf(task_from, "[%i]", we->waker);
805                 }
806                 if (!task_to) {
807                         task_to = malloc(40);
808                         sprintf(task_to, "[%i]", we->wakee);
809                 }
810
811                 if (we->waker == -1)
812                         svg_interrupt(we->time, to);
813                 else if (from && to && abs(from - to) == 1)
814                         svg_wakeline(we->time, from, to);
815                 else
816                         svg_partial_wakeline(we->time, from, task_from, to, task_to);
817                 we = we->next;
818
819                 free(task_from);
820                 free(task_to);
821         }
822 }
823
824 static void draw_cpu_usage(void)
825 {
826         struct per_pid *p;
827         struct per_pidcomm *c;
828         struct cpu_sample *sample;
829         p = all_data;
830         while (p) {
831                 c = p->all;
832                 while (c) {
833                         sample = c->samples;
834                         while (sample) {
835                                 if (sample->type == TYPE_RUNNING)
836                                         svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
837
838                                 sample = sample->next;
839                         }
840                         c = c->next;
841                 }
842                 p = p->next;
843         }
844 }
845
846 static void draw_process_bars(void)
847 {
848         struct per_pid *p;
849         struct per_pidcomm *c;
850         struct cpu_sample *sample;
851         int Y = 0;
852
853         Y = 2 * numcpus + 2;
854
855         p = all_data;
856         while (p) {
857                 c = p->all;
858                 while (c) {
859                         if (!c->display) {
860                                 c->Y = 0;
861                                 c = c->next;
862                                 continue;
863                         }
864
865                         svg_box(Y, c->start_time, c->end_time, "process");
866                         sample = c->samples;
867                         while (sample) {
868                                 if (sample->type == TYPE_RUNNING)
869                                         svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
870                                 if (sample->type == TYPE_BLOCKED)
871                                         svg_box(Y, sample->start_time, sample->end_time, "blocked");
872                                 if (sample->type == TYPE_WAITING)
873                                         svg_waiting(Y, sample->start_time, sample->end_time);
874                                 sample = sample->next;
875                         }
876
877                         if (c->comm) {
878                                 char comm[256];
879                                 if (c->total_time > 5000000000) /* 5 seconds */
880                                         sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
881                                 else
882                                         sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
883
884                                 svg_text(Y, c->start_time, comm);
885                         }
886                         c->Y = Y;
887                         Y++;
888                         c = c->next;
889                 }
890                 p = p->next;
891         }
892 }
893
894 static void add_process_filter(const char *string)
895 {
896         struct process_filter *filt;
897         int pid;
898
899         pid = strtoull(string, NULL, 10);
900         filt = malloc(sizeof(struct process_filter));
901         if (!filt)
902                 return;
903
904         filt->name = strdup(string);
905         filt->pid  = pid;
906         filt->next = process_filter;
907
908         process_filter = filt;
909 }
910
911 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
912 {
913         struct process_filter *filt;
914         if (!process_filter)
915                 return 1;
916
917         filt = process_filter;
918         while (filt) {
919                 if (filt->pid && p->pid == filt->pid)
920                         return 1;
921                 if (strcmp(filt->name, c->comm) == 0)
922                         return 1;
923                 filt = filt->next;
924         }
925         return 0;
926 }
927
928 static int determine_display_tasks_filtered(void)
929 {
930         struct per_pid *p;
931         struct per_pidcomm *c;
932         int count = 0;
933
934         p = all_data;
935         while (p) {
936                 p->display = 0;
937                 if (p->start_time == 1)
938                         p->start_time = first_time;
939
940                 /* no exit marker, task kept running to the end */
941                 if (p->end_time == 0)
942                         p->end_time = last_time;
943
944                 c = p->all;
945
946                 while (c) {
947                         c->display = 0;
948
949                         if (c->start_time == 1)
950                                 c->start_time = first_time;
951
952                         if (passes_filter(p, c)) {
953                                 c->display = 1;
954                                 p->display = 1;
955                                 count++;
956                         }
957
958                         if (c->end_time == 0)
959                                 c->end_time = last_time;
960
961                         c = c->next;
962                 }
963                 p = p->next;
964         }
965         return count;
966 }
967
968 static int determine_display_tasks(u64 threshold)
969 {
970         struct per_pid *p;
971         struct per_pidcomm *c;
972         int count = 0;
973
974         if (process_filter)
975                 return determine_display_tasks_filtered();
976
977         p = all_data;
978         while (p) {
979                 p->display = 0;
980                 if (p->start_time == 1)
981                         p->start_time = first_time;
982
983                 /* no exit marker, task kept running to the end */
984                 if (p->end_time == 0)
985                         p->end_time = last_time;
986                 if (p->total_time >= threshold && !power_only)
987                         p->display = 1;
988
989                 c = p->all;
990
991                 while (c) {
992                         c->display = 0;
993
994                         if (c->start_time == 1)
995                                 c->start_time = first_time;
996
997                         if (c->total_time >= threshold && !power_only) {
998                                 c->display = 1;
999                                 count++;
1000                         }
1001
1002                         if (c->end_time == 0)
1003                                 c->end_time = last_time;
1004
1005                         c = c->next;
1006                 }
1007                 p = p->next;
1008         }
1009         return count;
1010 }
1011
1012
1013
1014 #define TIME_THRESH 10000000
1015
1016 static void write_svg_file(const char *filename)
1017 {
1018         u64 i;
1019         int count;
1020
1021         numcpus++;
1022
1023
1024         count = determine_display_tasks(TIME_THRESH);
1025
1026         /* We'd like to show at least 15 tasks; be less picky if we have fewer */
1027         if (count < 15)
1028                 count = determine_display_tasks(TIME_THRESH / 10);
1029
1030         open_svg(filename, numcpus, count, first_time, last_time);
1031
1032         svg_time_grid();
1033         svg_legenda();
1034
1035         for (i = 0; i < numcpus; i++)
1036                 svg_cpu_box(i, max_freq, turbo_frequency);
1037
1038         draw_cpu_usage();
1039         draw_process_bars();
1040         draw_c_p_states();
1041         draw_wakeups();
1042
1043         svg_close();
1044 }
1045
1046 static void process_samples(void)
1047 {
1048         struct sample_wrapper *cursor;
1049         event_t *event;
1050
1051         sort_queued_samples();
1052
1053         cursor = all_samples;
1054         while (cursor) {
1055                 event = (void *)&cursor->data;
1056                 cursor = cursor->next;
1057                 process_sample_event(event);
1058         }
1059 }
1060
1061 static int sample_type_check(u64 type)
1062 {
1063         sample_type = type;
1064
1065         if (!(sample_type & PERF_SAMPLE_RAW)) {
1066                 fprintf(stderr, "No trace samples found in the file.\n"
1067                                 "Have you used 'perf timechart record' to record it?\n");
1068                 return -1;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static struct perf_file_handler file_handler = {
1075         .process_comm_event     = process_comm_event,
1076         .process_fork_event     = process_fork_event,
1077         .process_exit_event     = process_exit_event,
1078         .process_sample_event   = queue_sample_event,
1079         .sample_type_check      = sample_type_check,
1080 };
1081
1082 static int __cmd_timechart(void)
1083 {
1084         struct perf_header *header;
1085         int ret;
1086
1087         register_perf_file_handler(&file_handler);
1088
1089         ret = mmap_dispatch_perf_file(&header, input_name, 0, 0,
1090                                       &event__cwdlen, &event__cwd);
1091         if (ret)
1092                 return EXIT_FAILURE;
1093
1094         process_samples();
1095
1096         end_sample_processing();
1097
1098         sort_pids();
1099
1100         write_svg_file(output_name);
1101
1102         pr_info("Written %2.1f seconds of trace to %s.\n",
1103                 (last_time - first_time) / 1000000000.0, output_name);
1104
1105         return EXIT_SUCCESS;
1106 }
1107
1108 static const char * const timechart_usage[] = {
1109         "perf timechart [<options>] {record}",
1110         NULL
1111 };
1112
1113 static const char *record_args[] = {
1114         "record",
1115         "-a",
1116         "-R",
1117         "-M",
1118         "-f",
1119         "-c", "1",
1120         "-e", "power:power_start",
1121         "-e", "power:power_end",
1122         "-e", "power:power_frequency",
1123         "-e", "sched:sched_wakeup",
1124         "-e", "sched:sched_switch",
1125 };
1126
1127 static int __cmd_record(int argc, const char **argv)
1128 {
1129         unsigned int rec_argc, i, j;
1130         const char **rec_argv;
1131
1132         rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1133         rec_argv = calloc(rec_argc + 1, sizeof(char *));
1134
1135         for (i = 0; i < ARRAY_SIZE(record_args); i++)
1136                 rec_argv[i] = strdup(record_args[i]);
1137
1138         for (j = 1; j < (unsigned int)argc; j++, i++)
1139                 rec_argv[i] = argv[j];
1140
1141         return cmd_record(i, rec_argv, NULL);
1142 }
1143
1144 static int
1145 parse_process(const struct option *opt __used, const char *arg, int __used unset)
1146 {
1147         if (arg)
1148                 add_process_filter(arg);
1149         return 0;
1150 }
1151
1152 static const struct option options[] = {
1153         OPT_STRING('i', "input", &input_name, "file",
1154                     "input file name"),
1155         OPT_STRING('o', "output", &output_name, "file",
1156                     "output file name"),
1157         OPT_INTEGER('w', "width", &svg_page_width,
1158                     "page width"),
1159         OPT_BOOLEAN('P', "power-only", &power_only,
1160                     "output power data only"),
1161         OPT_CALLBACK('p', "process", NULL, "process",
1162                       "process selector. Pass a pid or process name.",
1163                        parse_process),
1164         OPT_END()
1165 };
1166
1167
1168 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1169 {
1170         symbol__init(0);
1171
1172         argc = parse_options(argc, argv, options, timechart_usage,
1173                         PARSE_OPT_STOP_AT_NON_OPTION);
1174
1175         if (argc && !strncmp(argv[0], "rec", 3))
1176                 return __cmd_record(argc, argv);
1177         else if (argc)
1178                 usage_with_options(timechart_usage, options);
1179
1180         setup_pager();
1181
1182         return __cmd_timechart();
1183 }