perf timechart: Add option to limit number of tasks
[firefly-linux-kernel-4.4.55.git] / 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 <traceevent/event-parse.h>
16
17 #include "builtin.h"
18
19 #include "util/util.h"
20
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"
30
31 #include "perf.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
41 #define SUPPORT_OLD_POWER_EVENTS 1
42 #define PWR_EVENT_EXIT -1
43
44 static int proc_num = 15;
45
46 static unsigned int     numcpus;
47 static u64              min_freq;       /* Lowest CPU frequency seen */
48 static u64              max_freq;       /* Highest CPU frequency seen */
49 static u64              turbo_frequency;
50
51 static u64              first_time, last_time;
52
53 static bool             power_only;
54
55
56 struct per_pid;
57 struct per_pidcomm;
58
59 struct cpu_sample;
60 struct power_event;
61 struct wake_event;
62
63 struct sample_wrapper;
64
65 /*
66  * Datastructure layout:
67  * We keep an list of "pid"s, matching the kernels notion of a task struct.
68  * Each "pid" entry, has a list of "comm"s.
69  *      this is because we want to track different programs different, while
70  *      exec will reuse the original pid (by design).
71  * Each comm has a list of samples that will be used to draw
72  * final graph.
73  */
74
75 struct per_pid {
76         struct per_pid *next;
77
78         int             pid;
79         int             ppid;
80
81         u64             start_time;
82         u64             end_time;
83         u64             total_time;
84         int             display;
85
86         struct per_pidcomm *all;
87         struct per_pidcomm *current;
88 };
89
90
91 struct per_pidcomm {
92         struct per_pidcomm *next;
93
94         u64             start_time;
95         u64             end_time;
96         u64             total_time;
97
98         int             Y;
99         int             display;
100
101         long            state;
102         u64             state_since;
103
104         char            *comm;
105
106         struct cpu_sample *samples;
107 };
108
109 struct sample_wrapper {
110         struct sample_wrapper *next;
111
112         u64             timestamp;
113         unsigned char   data[0];
114 };
115
116 #define TYPE_NONE       0
117 #define TYPE_RUNNING    1
118 #define TYPE_WAITING    2
119 #define TYPE_BLOCKED    3
120
121 struct cpu_sample {
122         struct cpu_sample *next;
123
124         u64 start_time;
125         u64 end_time;
126         int type;
127         int cpu;
128 };
129
130 static struct per_pid *all_data;
131
132 #define CSTATE 1
133 #define PSTATE 2
134
135 struct power_event {
136         struct power_event *next;
137         int type;
138         int state;
139         u64 start_time;
140         u64 end_time;
141         int cpu;
142 };
143
144 struct wake_event {
145         struct wake_event *next;
146         int waker;
147         int wakee;
148         u64 time;
149 };
150
151 static struct power_event    *power_events;
152 static struct wake_event     *wake_events;
153
154 struct process_filter;
155 struct process_filter {
156         char                    *name;
157         int                     pid;
158         struct process_filter   *next;
159 };
160
161 static struct process_filter *process_filter;
162
163
164 static struct per_pid *find_create_pid(int pid)
165 {
166         struct per_pid *cursor = all_data;
167
168         while (cursor) {
169                 if (cursor->pid == pid)
170                         return cursor;
171                 cursor = cursor->next;
172         }
173         cursor = zalloc(sizeof(*cursor));
174         assert(cursor != NULL);
175         cursor->pid = pid;
176         cursor->next = all_data;
177         all_data = cursor;
178         return cursor;
179 }
180
181 static void pid_set_comm(int pid, char *comm)
182 {
183         struct per_pid *p;
184         struct per_pidcomm *c;
185         p = find_create_pid(pid);
186         c = p->all;
187         while (c) {
188                 if (c->comm && strcmp(c->comm, comm) == 0) {
189                         p->current = c;
190                         return;
191                 }
192                 if (!c->comm) {
193                         c->comm = strdup(comm);
194                         p->current = c;
195                         return;
196                 }
197                 c = c->next;
198         }
199         c = zalloc(sizeof(*c));
200         assert(c != NULL);
201         c->comm = strdup(comm);
202         p->current = c;
203         c->next = p->all;
204         p->all = c;
205 }
206
207 static void pid_fork(int pid, int ppid, u64 timestamp)
208 {
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);
215
216         p->start_time = timestamp;
217         if (p->current) {
218                 p->current->start_time = timestamp;
219                 p->current->state_since = timestamp;
220         }
221 }
222
223 static void pid_exit(int pid, u64 timestamp)
224 {
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;
230 }
231
232 static void
233 pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
234 {
235         struct per_pid *p;
236         struct per_pidcomm *c;
237         struct cpu_sample *sample;
238
239         p = find_create_pid(pid);
240         c = p->current;
241         if (!c) {
242                 c = zalloc(sizeof(*c));
243                 assert(c != NULL);
244                 p->current = c;
245                 c->next = p->all;
246                 p->all = c;
247         }
248
249         sample = zalloc(sizeof(*sample));
250         assert(sample != NULL);
251         sample->start_time = start;
252         sample->end_time = end;
253         sample->type = type;
254         sample->next = c->samples;
255         sample->cpu = cpu;
256         c->samples = sample;
257
258         if (sample->type == TYPE_RUNNING && end > start && start > 0) {
259                 c->total_time += (end-start);
260                 p->total_time += (end-start);
261         }
262
263         if (c->start_time == 0 || c->start_time > start)
264                 c->start_time = start;
265         if (p->start_time == 0 || p->start_time > start)
266                 p->start_time = start;
267 }
268
269 #define MAX_CPUS 4096
270
271 static u64 cpus_cstate_start_times[MAX_CPUS];
272 static int cpus_cstate_state[MAX_CPUS];
273 static u64 cpus_pstate_start_times[MAX_CPUS];
274 static u64 cpus_pstate_state[MAX_CPUS];
275
276 static int process_comm_event(struct perf_tool *tool __maybe_unused,
277                               union perf_event *event,
278                               struct perf_sample *sample __maybe_unused,
279                               struct machine *machine __maybe_unused)
280 {
281         pid_set_comm(event->comm.tid, event->comm.comm);
282         return 0;
283 }
284
285 static int process_fork_event(struct perf_tool *tool __maybe_unused,
286                               union perf_event *event,
287                               struct perf_sample *sample __maybe_unused,
288                               struct machine *machine __maybe_unused)
289 {
290         pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
291         return 0;
292 }
293
294 static int process_exit_event(struct perf_tool *tool __maybe_unused,
295                               union perf_event *event,
296                               struct perf_sample *sample __maybe_unused,
297                               struct machine *machine __maybe_unused)
298 {
299         pid_exit(event->fork.pid, event->fork.time);
300         return 0;
301 }
302
303 struct trace_entry {
304         unsigned short          type;
305         unsigned char           flags;
306         unsigned char           preempt_count;
307         int                     pid;
308         int                     lock_depth;
309 };
310
311 #ifdef SUPPORT_OLD_POWER_EVENTS
312 static int use_old_power_events;
313 struct power_entry_old {
314         struct trace_entry te;
315         u64     type;
316         u64     value;
317         u64     cpu_id;
318 };
319 #endif
320
321 struct power_processor_entry {
322         struct trace_entry te;
323         u32     state;
324         u32     cpu_id;
325 };
326
327 #define TASK_COMM_LEN 16
328 struct wakeup_entry {
329         struct trace_entry te;
330         char comm[TASK_COMM_LEN];
331         int   pid;
332         int   prio;
333         int   success;
334 };
335
336 struct sched_switch {
337         struct trace_entry te;
338         char prev_comm[TASK_COMM_LEN];
339         int  prev_pid;
340         int  prev_prio;
341         long prev_state; /* Arjan weeps. */
342         char next_comm[TASK_COMM_LEN];
343         int  next_pid;
344         int  next_prio;
345 };
346
347 static void c_state_start(int cpu, u64 timestamp, int state)
348 {
349         cpus_cstate_start_times[cpu] = timestamp;
350         cpus_cstate_state[cpu] = state;
351 }
352
353 static void c_state_end(int cpu, u64 timestamp)
354 {
355         struct power_event *pwr = zalloc(sizeof(*pwr));
356
357         if (!pwr)
358                 return;
359
360         pwr->state = cpus_cstate_state[cpu];
361         pwr->start_time = cpus_cstate_start_times[cpu];
362         pwr->end_time = timestamp;
363         pwr->cpu = cpu;
364         pwr->type = CSTATE;
365         pwr->next = power_events;
366
367         power_events = pwr;
368 }
369
370 static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
371 {
372         struct power_event *pwr;
373
374         if (new_freq > 8000000) /* detect invalid data */
375                 return;
376
377         pwr = zalloc(sizeof(*pwr));
378         if (!pwr)
379                 return;
380
381         pwr->state = cpus_pstate_state[cpu];
382         pwr->start_time = cpus_pstate_start_times[cpu];
383         pwr->end_time = timestamp;
384         pwr->cpu = cpu;
385         pwr->type = PSTATE;
386         pwr->next = power_events;
387
388         if (!pwr->start_time)
389                 pwr->start_time = first_time;
390
391         power_events = pwr;
392
393         cpus_pstate_state[cpu] = new_freq;
394         cpus_pstate_start_times[cpu] = timestamp;
395
396         if ((u64)new_freq > max_freq)
397                 max_freq = new_freq;
398
399         if (new_freq < min_freq || min_freq == 0)
400                 min_freq = new_freq;
401
402         if (new_freq == max_freq - 1000)
403                         turbo_frequency = max_freq;
404 }
405
406 static void
407 sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
408 {
409         struct per_pid *p;
410         struct wakeup_entry *wake = (void *)te;
411         struct wake_event *we = zalloc(sizeof(*we));
412
413         if (!we)
414                 return;
415
416         we->time = timestamp;
417         we->waker = pid;
418
419         if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
420                 we->waker = -1;
421
422         we->wakee = wake->pid;
423         we->next = wake_events;
424         wake_events = we;
425         p = find_create_pid(we->wakee);
426
427         if (p && p->current && p->current->state == TYPE_NONE) {
428                 p->current->state_since = timestamp;
429                 p->current->state = TYPE_WAITING;
430         }
431         if (p && p->current && p->current->state == TYPE_BLOCKED) {
432                 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
433                 p->current->state_since = timestamp;
434                 p->current->state = TYPE_WAITING;
435         }
436 }
437
438 static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
439 {
440         struct per_pid *p = NULL, *prev_p;
441         struct sched_switch *sw = (void *)te;
442
443
444         prev_p = find_create_pid(sw->prev_pid);
445
446         p = find_create_pid(sw->next_pid);
447
448         if (prev_p->current && prev_p->current->state != TYPE_NONE)
449                 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
450         if (p && p->current) {
451                 if (p->current->state != TYPE_NONE)
452                         pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
453
454                 p->current->state_since = timestamp;
455                 p->current->state = TYPE_RUNNING;
456         }
457
458         if (prev_p->current) {
459                 prev_p->current->state = TYPE_NONE;
460                 prev_p->current->state_since = timestamp;
461                 if (sw->prev_state & 2)
462                         prev_p->current->state = TYPE_BLOCKED;
463                 if (sw->prev_state == 0)
464                         prev_p->current->state = TYPE_WAITING;
465         }
466 }
467
468 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
469                                   struct perf_sample *sample);
470
471 static int process_sample_event(struct perf_tool *tool __maybe_unused,
472                                 union perf_event *event __maybe_unused,
473                                 struct perf_sample *sample,
474                                 struct perf_evsel *evsel,
475                                 struct machine *machine __maybe_unused)
476 {
477         if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
478                 if (!first_time || first_time > sample->time)
479                         first_time = sample->time;
480                 if (last_time < sample->time)
481                         last_time = sample->time;
482         }
483
484         if (sample->cpu > numcpus)
485                 numcpus = sample->cpu;
486
487         if (evsel->handler != NULL) {
488                 tracepoint_handler f = evsel->handler;
489                 return f(evsel, sample);
490         }
491
492         return 0;
493 }
494
495 static int
496 process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
497                         struct perf_sample *sample)
498 {
499         struct power_processor_entry *ppe = sample->raw_data;
500
501         if (ppe->state == (u32) PWR_EVENT_EXIT)
502                 c_state_end(ppe->cpu_id, sample->time);
503         else
504                 c_state_start(ppe->cpu_id, sample->time, ppe->state);
505         return 0;
506 }
507
508 static int
509 process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
510                              struct perf_sample *sample)
511 {
512         struct power_processor_entry *ppe = sample->raw_data;
513
514         p_state_change(ppe->cpu_id, sample->time, ppe->state);
515         return 0;
516 }
517
518 static int
519 process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
520                             struct perf_sample *sample)
521 {
522         struct trace_entry *te = sample->raw_data;
523
524         sched_wakeup(sample->cpu, sample->time, sample->pid, te);
525         return 0;
526 }
527
528 static int
529 process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
530                             struct perf_sample *sample)
531 {
532         struct trace_entry *te = sample->raw_data;
533
534         sched_switch(sample->cpu, sample->time, te);
535         return 0;
536 }
537
538 #ifdef SUPPORT_OLD_POWER_EVENTS
539 static int
540 process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
541                            struct perf_sample *sample)
542 {
543         struct power_entry_old *peo = sample->raw_data;
544
545         c_state_start(peo->cpu_id, sample->time, peo->value);
546         return 0;
547 }
548
549 static int
550 process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
551                          struct perf_sample *sample)
552 {
553         c_state_end(sample->cpu, sample->time);
554         return 0;
555 }
556
557 static int
558 process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
559                                struct perf_sample *sample)
560 {
561         struct power_entry_old *peo = sample->raw_data;
562
563         p_state_change(peo->cpu_id, sample->time, peo->value);
564         return 0;
565 }
566 #endif /* SUPPORT_OLD_POWER_EVENTS */
567
568 /*
569  * After the last sample we need to wrap up the current C/P state
570  * and close out each CPU for these.
571  */
572 static void end_sample_processing(void)
573 {
574         u64 cpu;
575         struct power_event *pwr;
576
577         for (cpu = 0; cpu <= numcpus; cpu++) {
578                 /* C state */
579 #if 0
580                 pwr = zalloc(sizeof(*pwr));
581                 if (!pwr)
582                         return;
583
584                 pwr->state = cpus_cstate_state[cpu];
585                 pwr->start_time = cpus_cstate_start_times[cpu];
586                 pwr->end_time = last_time;
587                 pwr->cpu = cpu;
588                 pwr->type = CSTATE;
589                 pwr->next = power_events;
590
591                 power_events = pwr;
592 #endif
593                 /* P state */
594
595                 pwr = zalloc(sizeof(*pwr));
596                 if (!pwr)
597                         return;
598
599                 pwr->state = cpus_pstate_state[cpu];
600                 pwr->start_time = cpus_pstate_start_times[cpu];
601                 pwr->end_time = last_time;
602                 pwr->cpu = cpu;
603                 pwr->type = PSTATE;
604                 pwr->next = power_events;
605
606                 if (!pwr->start_time)
607                         pwr->start_time = first_time;
608                 if (!pwr->state)
609                         pwr->state = min_freq;
610                 power_events = pwr;
611         }
612 }
613
614 /*
615  * Sort the pid datastructure
616  */
617 static void sort_pids(void)
618 {
619         struct per_pid *new_list, *p, *cursor, *prev;
620         /* sort by ppid first, then by pid, lowest to highest */
621
622         new_list = NULL;
623
624         while (all_data) {
625                 p = all_data;
626                 all_data = p->next;
627                 p->next = NULL;
628
629                 if (new_list == NULL) {
630                         new_list = p;
631                         p->next = NULL;
632                         continue;
633                 }
634                 prev = NULL;
635                 cursor = new_list;
636                 while (cursor) {
637                         if (cursor->ppid > p->ppid ||
638                                 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
639                                 /* must insert before */
640                                 if (prev) {
641                                         p->next = prev->next;
642                                         prev->next = p;
643                                         cursor = NULL;
644                                         continue;
645                                 } else {
646                                         p->next = new_list;
647                                         new_list = p;
648                                         cursor = NULL;
649                                         continue;
650                                 }
651                         }
652
653                         prev = cursor;
654                         cursor = cursor->next;
655                         if (!cursor)
656                                 prev->next = p;
657                 }
658         }
659         all_data = new_list;
660 }
661
662
663 static void draw_c_p_states(void)
664 {
665         struct power_event *pwr;
666         pwr = power_events;
667
668         /*
669          * two pass drawing so that the P state bars are on top of the C state blocks
670          */
671         while (pwr) {
672                 if (pwr->type == CSTATE)
673                         svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
674                 pwr = pwr->next;
675         }
676
677         pwr = power_events;
678         while (pwr) {
679                 if (pwr->type == PSTATE) {
680                         if (!pwr->state)
681                                 pwr->state = min_freq;
682                         svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
683                 }
684                 pwr = pwr->next;
685         }
686 }
687
688 static void draw_wakeups(void)
689 {
690         struct wake_event *we;
691         struct per_pid *p;
692         struct per_pidcomm *c;
693
694         we = wake_events;
695         while (we) {
696                 int from = 0, to = 0;
697                 char *task_from = NULL, *task_to = NULL;
698
699                 /* locate the column of the waker and wakee */
700                 p = all_data;
701                 while (p) {
702                         if (p->pid == we->waker || p->pid == we->wakee) {
703                                 c = p->all;
704                                 while (c) {
705                                         if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
706                                                 if (p->pid == we->waker && !from) {
707                                                         from = c->Y;
708                                                         task_from = strdup(c->comm);
709                                                 }
710                                                 if (p->pid == we->wakee && !to) {
711                                                         to = c->Y;
712                                                         task_to = strdup(c->comm);
713                                                 }
714                                         }
715                                         c = c->next;
716                                 }
717                                 c = p->all;
718                                 while (c) {
719                                         if (p->pid == we->waker && !from) {
720                                                 from = c->Y;
721                                                 task_from = strdup(c->comm);
722                                         }
723                                         if (p->pid == we->wakee && !to) {
724                                                 to = c->Y;
725                                                 task_to = strdup(c->comm);
726                                         }
727                                         c = c->next;
728                                 }
729                         }
730                         p = p->next;
731                 }
732
733                 if (!task_from) {
734                         task_from = malloc(40);
735                         sprintf(task_from, "[%i]", we->waker);
736                 }
737                 if (!task_to) {
738                         task_to = malloc(40);
739                         sprintf(task_to, "[%i]", we->wakee);
740                 }
741
742                 if (we->waker == -1)
743                         svg_interrupt(we->time, to);
744                 else if (from && to && abs(from - to) == 1)
745                         svg_wakeline(we->time, from, to);
746                 else
747                         svg_partial_wakeline(we->time, from, task_from, to, task_to);
748                 we = we->next;
749
750                 free(task_from);
751                 free(task_to);
752         }
753 }
754
755 static void draw_cpu_usage(void)
756 {
757         struct per_pid *p;
758         struct per_pidcomm *c;
759         struct cpu_sample *sample;
760         p = all_data;
761         while (p) {
762                 c = p->all;
763                 while (c) {
764                         sample = c->samples;
765                         while (sample) {
766                                 if (sample->type == TYPE_RUNNING)
767                                         svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
768
769                                 sample = sample->next;
770                         }
771                         c = c->next;
772                 }
773                 p = p->next;
774         }
775 }
776
777 static void draw_process_bars(void)
778 {
779         struct per_pid *p;
780         struct per_pidcomm *c;
781         struct cpu_sample *sample;
782         int Y = 0;
783
784         Y = 2 * numcpus + 2;
785
786         p = all_data;
787         while (p) {
788                 c = p->all;
789                 while (c) {
790                         if (!c->display) {
791                                 c->Y = 0;
792                                 c = c->next;
793                                 continue;
794                         }
795
796                         svg_box(Y, c->start_time, c->end_time, "process");
797                         sample = c->samples;
798                         while (sample) {
799                                 if (sample->type == TYPE_RUNNING)
800                                         svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
801                                 if (sample->type == TYPE_BLOCKED)
802                                         svg_box(Y, sample->start_time, sample->end_time, "blocked");
803                                 if (sample->type == TYPE_WAITING)
804                                         svg_waiting(Y, sample->start_time, sample->end_time);
805                                 sample = sample->next;
806                         }
807
808                         if (c->comm) {
809                                 char comm[256];
810                                 if (c->total_time > 5000000000) /* 5 seconds */
811                                         sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
812                                 else
813                                         sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
814
815                                 svg_text(Y, c->start_time, comm);
816                         }
817                         c->Y = Y;
818                         Y++;
819                         c = c->next;
820                 }
821                 p = p->next;
822         }
823 }
824
825 static void add_process_filter(const char *string)
826 {
827         int pid = strtoull(string, NULL, 10);
828         struct process_filter *filt = malloc(sizeof(*filt));
829
830         if (!filt)
831                 return;
832
833         filt->name = strdup(string);
834         filt->pid  = pid;
835         filt->next = process_filter;
836
837         process_filter = filt;
838 }
839
840 static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
841 {
842         struct process_filter *filt;
843         if (!process_filter)
844                 return 1;
845
846         filt = process_filter;
847         while (filt) {
848                 if (filt->pid && p->pid == filt->pid)
849                         return 1;
850                 if (strcmp(filt->name, c->comm) == 0)
851                         return 1;
852                 filt = filt->next;
853         }
854         return 0;
855 }
856
857 static int determine_display_tasks_filtered(void)
858 {
859         struct per_pid *p;
860         struct per_pidcomm *c;
861         int count = 0;
862
863         p = all_data;
864         while (p) {
865                 p->display = 0;
866                 if (p->start_time == 1)
867                         p->start_time = first_time;
868
869                 /* no exit marker, task kept running to the end */
870                 if (p->end_time == 0)
871                         p->end_time = last_time;
872
873                 c = p->all;
874
875                 while (c) {
876                         c->display = 0;
877
878                         if (c->start_time == 1)
879                                 c->start_time = first_time;
880
881                         if (passes_filter(p, c)) {
882                                 c->display = 1;
883                                 p->display = 1;
884                                 count++;
885                         }
886
887                         if (c->end_time == 0)
888                                 c->end_time = last_time;
889
890                         c = c->next;
891                 }
892                 p = p->next;
893         }
894         return count;
895 }
896
897 static int determine_display_tasks(u64 threshold)
898 {
899         struct per_pid *p;
900         struct per_pidcomm *c;
901         int count = 0;
902
903         if (process_filter)
904                 return determine_display_tasks_filtered();
905
906         p = all_data;
907         while (p) {
908                 p->display = 0;
909                 if (p->start_time == 1)
910                         p->start_time = first_time;
911
912                 /* no exit marker, task kept running to the end */
913                 if (p->end_time == 0)
914                         p->end_time = last_time;
915                 if (p->total_time >= threshold && !power_only)
916                         p->display = 1;
917
918                 c = p->all;
919
920                 while (c) {
921                         c->display = 0;
922
923                         if (c->start_time == 1)
924                                 c->start_time = first_time;
925
926                         if (c->total_time >= threshold && !power_only) {
927                                 c->display = 1;
928                                 count++;
929                         }
930
931                         if (c->end_time == 0)
932                                 c->end_time = last_time;
933
934                         c = c->next;
935                 }
936                 p = p->next;
937         }
938         return count;
939 }
940
941
942
943 #define TIME_THRESH 10000000
944
945 static void write_svg_file(const char *filename)
946 {
947         u64 i;
948         int count;
949         int thresh = TIME_THRESH;
950
951         numcpus++;
952
953
954         /* We'd like to show at least proc_num tasks;
955          * be less picky if we have fewer */
956         do {
957                 count = determine_display_tasks(thresh);
958                 thresh /= 10;
959         } while (!process_filter && thresh && count < proc_num);
960
961         open_svg(filename, numcpus, count, first_time, last_time);
962
963         svg_time_grid();
964         svg_legenda();
965
966         for (i = 0; i < numcpus; i++)
967                 svg_cpu_box(i, max_freq, turbo_frequency);
968
969         draw_cpu_usage();
970         draw_process_bars();
971         draw_c_p_states();
972         draw_wakeups();
973
974         svg_close();
975 }
976
977 static int __cmd_timechart(const char *output_name)
978 {
979         struct perf_tool perf_timechart = {
980                 .comm            = process_comm_event,
981                 .fork            = process_fork_event,
982                 .exit            = process_exit_event,
983                 .sample          = process_sample_event,
984                 .ordered_samples = true,
985         };
986         const struct perf_evsel_str_handler power_tracepoints[] = {
987                 { "power:cpu_idle",             process_sample_cpu_idle },
988                 { "power:cpu_frequency",        process_sample_cpu_frequency },
989                 { "sched:sched_wakeup",         process_sample_sched_wakeup },
990                 { "sched:sched_switch",         process_sample_sched_switch },
991 #ifdef SUPPORT_OLD_POWER_EVENTS
992                 { "power:power_start",          process_sample_power_start },
993                 { "power:power_end",            process_sample_power_end },
994                 { "power:power_frequency",      process_sample_power_frequency },
995 #endif
996         };
997         struct perf_data_file file = {
998                 .path = input_name,
999                 .mode = PERF_DATA_MODE_READ,
1000         };
1001
1002         struct perf_session *session = perf_session__new(&file, false,
1003                                                          &perf_timechart);
1004         int ret = -EINVAL;
1005
1006         if (session == NULL)
1007                 return -ENOMEM;
1008
1009         if (!perf_session__has_traces(session, "timechart record"))
1010                 goto out_delete;
1011
1012         if (perf_session__set_tracepoints_handlers(session,
1013                                                    power_tracepoints)) {
1014                 pr_err("Initializing session tracepoint handlers failed\n");
1015                 goto out_delete;
1016         }
1017
1018         ret = perf_session__process_events(session, &perf_timechart);
1019         if (ret)
1020                 goto out_delete;
1021
1022         end_sample_processing();
1023
1024         sort_pids();
1025
1026         write_svg_file(output_name);
1027
1028         pr_info("Written %2.1f seconds of trace to %s.\n",
1029                 (last_time - first_time) / 1000000000.0, output_name);
1030 out_delete:
1031         perf_session__delete(session);
1032         return ret;
1033 }
1034
1035 static int __cmd_record(int argc, const char **argv)
1036 {
1037 #ifdef SUPPORT_OLD_POWER_EVENTS
1038         const char * const record_old_args[] = {
1039                 "record", "-a", "-R", "-c", "1",
1040                 "-e", "power:power_start",
1041                 "-e", "power:power_end",
1042                 "-e", "power:power_frequency",
1043                 "-e", "sched:sched_wakeup",
1044                 "-e", "sched:sched_switch",
1045         };
1046 #endif
1047         const char * const record_new_args[] = {
1048                 "record", "-a", "-R", "-c", "1",
1049                 "-e", "power:cpu_frequency",
1050                 "-e", "power:cpu_idle",
1051                 "-e", "sched:sched_wakeup",
1052                 "-e", "sched:sched_switch",
1053         };
1054         unsigned int rec_argc, i, j;
1055         const char **rec_argv;
1056         const char * const *record_args = record_new_args;
1057         unsigned int record_elems = ARRAY_SIZE(record_new_args);
1058
1059 #ifdef SUPPORT_OLD_POWER_EVENTS
1060         if (!is_valid_tracepoint("power:cpu_idle") &&
1061             is_valid_tracepoint("power:power_start")) {
1062                 use_old_power_events = 1;
1063                 record_args = record_old_args;
1064                 record_elems = ARRAY_SIZE(record_old_args);
1065         }
1066 #endif
1067
1068         rec_argc = record_elems + argc - 1;
1069         rec_argv = calloc(rec_argc + 1, sizeof(char *));
1070
1071         if (rec_argv == NULL)
1072                 return -ENOMEM;
1073
1074         for (i = 0; i < record_elems; i++)
1075                 rec_argv[i] = strdup(record_args[i]);
1076
1077         for (j = 1; j < (unsigned int)argc; j++, i++)
1078                 rec_argv[i] = argv[j];
1079
1080         return cmd_record(i, rec_argv, NULL);
1081 }
1082
1083 static int
1084 parse_process(const struct option *opt __maybe_unused, const char *arg,
1085               int __maybe_unused unset)
1086 {
1087         if (arg)
1088                 add_process_filter(arg);
1089         return 0;
1090 }
1091
1092 int cmd_timechart(int argc, const char **argv,
1093                   const char *prefix __maybe_unused)
1094 {
1095         const char *output_name = "output.svg";
1096         const struct option options[] = {
1097         OPT_STRING('i', "input", &input_name, "file", "input file name"),
1098         OPT_STRING('o', "output", &output_name, "file", "output file name"),
1099         OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1100         OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
1101         OPT_CALLBACK('p', "process", NULL, "process",
1102                       "process selector. Pass a pid or process name.",
1103                        parse_process),
1104         OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1105                     "Look for files with symbols relative to this directory"),
1106         OPT_INTEGER('n', "proc-num", &proc_num,
1107                     "min. number of tasks to print"),
1108         OPT_END()
1109         };
1110         const char * const timechart_usage[] = {
1111                 "perf timechart [<options>] {record}",
1112                 NULL
1113         };
1114
1115         argc = parse_options(argc, argv, options, timechart_usage,
1116                         PARSE_OPT_STOP_AT_NON_OPTION);
1117
1118         symbol__init();
1119
1120         if (argc && !strncmp(argv[0], "rec", 3))
1121                 return __cmd_record(argc, argv);
1122         else if (argc)
1123                 usage_with_options(timechart_usage, options);
1124
1125         setup_pager();
1126
1127         return __cmd_timechart(output_name);
1128 }