blob: 27018531404b68761adef79aa9a3bd43a3950087 [file] [log] [blame]
Arjan van de Ven10274982009-09-12 07:53:05 +02001/*
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"
Li Zefan5cbd0802009-12-01 14:05:16 +080032#include "util/event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020033#include "util/session.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020034#include "util/svghelper.h"
35
36static char const *input_name = "perf.data";
37static char const *output_name = "output.svg";
38
39
Arjan van de Ven10274982009-09-12 07:53:05 +020040static u64 sample_type;
41
42static unsigned int numcpus;
43static u64 min_freq; /* Lowest CPU frequency seen */
44static u64 max_freq; /* Highest CPU frequency seen */
45static u64 turbo_frequency;
46
47static u64 first_time, last_time;
48
Arjan van de Ven39a90a82009-09-24 15:40:13 +020049static int power_only;
50
Arjan van de Ven10274982009-09-12 07:53:05 +020051
Arjan van de Ven10274982009-09-12 07:53:05 +020052struct per_pid;
53struct per_pidcomm;
54
55struct cpu_sample;
56struct power_event;
57struct wake_event;
58
59struct 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
71struct 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
89struct 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
107struct 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
119struct cpu_sample {
120 struct cpu_sample *next;
121
122 u64 start_time;
123 u64 end_time;
124 int type;
125 int cpu;
126};
127
128static struct per_pid *all_data;
129
130#define CSTATE 1
131#define PSTATE 2
132
133struct 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
142struct wake_event {
143 struct wake_event *next;
144 int waker;
145 int wakee;
146 u64 time;
147};
148
149static struct power_event *power_events;
150static struct wake_event *wake_events;
151
152struct sample_wrapper *all_samples;
153
Arjan van de Venbbe29872009-10-20 07:09:39 +0900154
155struct process_filter;
156struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800157 char *name;
158 int pid;
159 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900160};
161
162static struct process_filter *process_filter;
163
164
Arjan van de Ven10274982009-09-12 07:53:05 +0200165static 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
183static 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
210static 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
226static 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
235static void
236pid_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
279static u64 cpus_cstate_start_times[MAX_CPUS];
280static int cpus_cstate_state[MAX_CPUS];
281static u64 cpus_pstate_start_times[MAX_CPUS];
282static u64 cpus_pstate_state[MAX_CPUS];
283
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200284static int process_comm_event(event_t *event, struct perf_session *session __used)
Arjan van de Ven10274982009-09-12 07:53:05 +0200285{
286 pid_set_comm(event->comm.pid, event->comm.comm);
287 return 0;
288}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200289
290static int process_fork_event(event_t *event, struct perf_session *session __used)
Arjan van de Ven10274982009-09-12 07:53:05 +0200291{
292 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
293 return 0;
294}
295
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200296static int process_exit_event(event_t *event, struct perf_session *session __used)
Arjan van de Ven10274982009-09-12 07:53:05 +0200297{
298 pid_exit(event->fork.pid, event->fork.time);
299 return 0;
300}
301
302struct trace_entry {
Arjan van de Ven10274982009-09-12 07:53:05 +0200303 unsigned short type;
304 unsigned char flags;
305 unsigned char preempt_count;
306 int pid;
OGAWA Hirofumi028c5152009-12-06 20:07:29 +0900307 int lock_depth;
Arjan van de Ven10274982009-09-12 07:53:05 +0200308};
309
310struct power_entry {
311 struct trace_entry te;
312 s64 type;
313 s64 value;
314};
315
316#define TASK_COMM_LEN 16
317struct wakeup_entry {
318 struct trace_entry te;
319 char comm[TASK_COMM_LEN];
320 int pid;
321 int prio;
322 int success;
323};
324
325/*
326 * trace_flag_type is an enumeration that holds different
327 * states when a trace occurs. These are:
328 * IRQS_OFF - interrupts were disabled
329 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
330 * NEED_RESCED - reschedule is requested
331 * HARDIRQ - inside an interrupt handler
332 * SOFTIRQ - inside a softirq handler
333 */
334enum trace_flag_type {
335 TRACE_FLAG_IRQS_OFF = 0x01,
336 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
337 TRACE_FLAG_NEED_RESCHED = 0x04,
338 TRACE_FLAG_HARDIRQ = 0x08,
339 TRACE_FLAG_SOFTIRQ = 0x10,
340};
341
342
343
344struct sched_switch {
345 struct trace_entry te;
346 char prev_comm[TASK_COMM_LEN];
347 int prev_pid;
348 int prev_prio;
349 long prev_state; /* Arjan weeps. */
350 char next_comm[TASK_COMM_LEN];
351 int next_pid;
352 int next_prio;
353};
354
355static void c_state_start(int cpu, u64 timestamp, int state)
356{
357 cpus_cstate_start_times[cpu] = timestamp;
358 cpus_cstate_state[cpu] = state;
359}
360
361static void c_state_end(int cpu, u64 timestamp)
362{
363 struct power_event *pwr;
364 pwr = malloc(sizeof(struct power_event));
365 if (!pwr)
366 return;
367 memset(pwr, 0, sizeof(struct power_event));
368
369 pwr->state = cpus_cstate_state[cpu];
370 pwr->start_time = cpus_cstate_start_times[cpu];
371 pwr->end_time = timestamp;
372 pwr->cpu = cpu;
373 pwr->type = CSTATE;
374 pwr->next = power_events;
375
376 power_events = pwr;
377}
378
379static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
380{
381 struct power_event *pwr;
382 pwr = malloc(sizeof(struct power_event));
383
384 if (new_freq > 8000000) /* detect invalid data */
385 return;
386
387 if (!pwr)
388 return;
389 memset(pwr, 0, sizeof(struct power_event));
390
391 pwr->state = cpus_pstate_state[cpu];
392 pwr->start_time = cpus_pstate_start_times[cpu];
393 pwr->end_time = timestamp;
394 pwr->cpu = cpu;
395 pwr->type = PSTATE;
396 pwr->next = power_events;
397
398 if (!pwr->start_time)
399 pwr->start_time = first_time;
400
401 power_events = pwr;
402
403 cpus_pstate_state[cpu] = new_freq;
404 cpus_pstate_start_times[cpu] = timestamp;
405
406 if ((u64)new_freq > max_freq)
407 max_freq = new_freq;
408
409 if (new_freq < min_freq || min_freq == 0)
410 min_freq = new_freq;
411
412 if (new_freq == max_freq - 1000)
413 turbo_frequency = max_freq;
414}
415
416static void
417sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
418{
419 struct wake_event *we;
420 struct per_pid *p;
421 struct wakeup_entry *wake = (void *)te;
422
423 we = malloc(sizeof(struct wake_event));
424 if (!we)
425 return;
426
427 memset(we, 0, sizeof(struct wake_event));
428 we->time = timestamp;
429 we->waker = pid;
430
431 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
432 we->waker = -1;
433
434 we->wakee = wake->pid;
435 we->next = wake_events;
436 wake_events = we;
437 p = find_create_pid(we->wakee);
438
439 if (p && p->current && p->current->state == TYPE_NONE) {
440 p->current->state_since = timestamp;
441 p->current->state = TYPE_WAITING;
442 }
443 if (p && p->current && p->current->state == TYPE_BLOCKED) {
444 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
445 p->current->state_since = timestamp;
446 p->current->state = TYPE_WAITING;
447 }
448}
449
450static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
451{
452 struct per_pid *p = NULL, *prev_p;
453 struct sched_switch *sw = (void *)te;
454
455
456 prev_p = find_create_pid(sw->prev_pid);
457
458 p = find_create_pid(sw->next_pid);
459
460 if (prev_p->current && prev_p->current->state != TYPE_NONE)
461 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
462 if (p && p->current) {
463 if (p->current->state != TYPE_NONE)
464 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
465
466 p->current->state_since = timestamp;
467 p->current->state = TYPE_RUNNING;
468 }
469
470 if (prev_p->current) {
471 prev_p->current->state = TYPE_NONE;
472 prev_p->current->state_since = timestamp;
473 if (sw->prev_state & 2)
474 prev_p->current->state = TYPE_BLOCKED;
475 if (sw->prev_state == 0)
476 prev_p->current->state = TYPE_WAITING;
477 }
478}
479
480
481static int
482process_sample_event(event_t *event)
483{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900484 struct sample_data data;
Arjan van de Ven10274982009-09-12 07:53:05 +0200485 struct trace_entry *te;
486
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900487 memset(&data, 0, sizeof(data));
Arjan van de Ven10274982009-09-12 07:53:05 +0200488
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900489 event__parse_sample(event, sample_type, &data);
490
Arjan van de Ven10274982009-09-12 07:53:05 +0200491 if (sample_type & PERF_SAMPLE_TIME) {
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900492 if (!first_time || first_time > data.time)
493 first_time = data.time;
494 if (last_time < data.time)
495 last_time = data.time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200496 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200497
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900498 te = (void *)data.raw_data;
499 if (sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200500 char *event_str;
501 struct power_entry *pe;
502
503 pe = (void *)te;
504
505 event_str = perf_header__find_event(te->type);
506
507 if (!event_str)
508 return 0;
509
510 if (strcmp(event_str, "power:power_start") == 0)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900511 c_state_start(data.cpu, data.time, pe->value);
Arjan van de Ven10274982009-09-12 07:53:05 +0200512
513 if (strcmp(event_str, "power:power_end") == 0)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900514 c_state_end(data.cpu, data.time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200515
516 if (strcmp(event_str, "power:power_frequency") == 0)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900517 p_state_change(data.cpu, data.time, pe->value);
Arjan van de Ven10274982009-09-12 07:53:05 +0200518
519 if (strcmp(event_str, "sched:sched_wakeup") == 0)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900520 sched_wakeup(data.cpu, data.time, data.pid, te);
Arjan van de Ven10274982009-09-12 07:53:05 +0200521
522 if (strcmp(event_str, "sched:sched_switch") == 0)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900523 sched_switch(data.cpu, data.time, te);
Arjan van de Ven10274982009-09-12 07:53:05 +0200524 }
525 return 0;
526}
527
528/*
529 * After the last sample we need to wrap up the current C/P state
530 * and close out each CPU for these.
531 */
532static void end_sample_processing(void)
533{
534 u64 cpu;
535 struct power_event *pwr;
536
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200537 for (cpu = 0; cpu <= numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200538 pwr = malloc(sizeof(struct power_event));
539 if (!pwr)
540 return;
541 memset(pwr, 0, sizeof(struct power_event));
542
543 /* C state */
544#if 0
545 pwr->state = cpus_cstate_state[cpu];
546 pwr->start_time = cpus_cstate_start_times[cpu];
547 pwr->end_time = last_time;
548 pwr->cpu = cpu;
549 pwr->type = CSTATE;
550 pwr->next = power_events;
551
552 power_events = pwr;
553#endif
554 /* P state */
555
556 pwr = malloc(sizeof(struct power_event));
557 if (!pwr)
558 return;
559 memset(pwr, 0, sizeof(struct power_event));
560
561 pwr->state = cpus_pstate_state[cpu];
562 pwr->start_time = cpus_pstate_start_times[cpu];
563 pwr->end_time = last_time;
564 pwr->cpu = cpu;
565 pwr->type = PSTATE;
566 pwr->next = power_events;
567
568 if (!pwr->start_time)
569 pwr->start_time = first_time;
570 if (!pwr->state)
571 pwr->state = min_freq;
572 power_events = pwr;
573 }
574}
575
576static u64 sample_time(event_t *event)
577{
578 int cursor;
579
580 cursor = 0;
581 if (sample_type & PERF_SAMPLE_IP)
582 cursor++;
583 if (sample_type & PERF_SAMPLE_TID)
584 cursor++;
585 if (sample_type & PERF_SAMPLE_TIME)
586 return event->sample.array[cursor];
587 return 0;
588}
589
590
591/*
592 * We first queue all events, sorted backwards by insertion.
593 * The order will get flipped later.
594 */
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200595static int queue_sample_event(event_t *event, struct perf_session *session __used)
Arjan van de Ven10274982009-09-12 07:53:05 +0200596{
597 struct sample_wrapper *copy, *prev;
598 int size;
599
600 size = event->sample.header.size + sizeof(struct sample_wrapper) + 8;
601
602 copy = malloc(size);
603 if (!copy)
604 return 1;
605
606 memset(copy, 0, size);
607
608 copy->next = NULL;
609 copy->timestamp = sample_time(event);
610
611 memcpy(&copy->data, event, event->sample.header.size);
612
613 /* insert in the right place in the list */
614
615 if (!all_samples) {
616 /* first sample ever */
617 all_samples = copy;
618 return 0;
619 }
620
621 if (all_samples->timestamp < copy->timestamp) {
622 /* insert at the head of the list */
623 copy->next = all_samples;
624 all_samples = copy;
625 return 0;
626 }
627
628 prev = all_samples;
629 while (prev->next) {
630 if (prev->next->timestamp < copy->timestamp) {
631 copy->next = prev->next;
632 prev->next = copy;
633 return 0;
634 }
635 prev = prev->next;
636 }
637 /* insert at the end of the list */
638 prev->next = copy;
639
640 return 0;
641}
642
643static void sort_queued_samples(void)
644{
645 struct sample_wrapper *cursor, *next;
646
647 cursor = all_samples;
648 all_samples = NULL;
649
650 while (cursor) {
651 next = cursor->next;
652 cursor->next = all_samples;
653 all_samples = cursor;
654 cursor = next;
655 }
656}
657
658/*
659 * Sort the pid datastructure
660 */
661static void sort_pids(void)
662{
663 struct per_pid *new_list, *p, *cursor, *prev;
664 /* sort by ppid first, then by pid, lowest to highest */
665
666 new_list = NULL;
667
668 while (all_data) {
669 p = all_data;
670 all_data = p->next;
671 p->next = NULL;
672
673 if (new_list == NULL) {
674 new_list = p;
675 p->next = NULL;
676 continue;
677 }
678 prev = NULL;
679 cursor = new_list;
680 while (cursor) {
681 if (cursor->ppid > p->ppid ||
682 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
683 /* must insert before */
684 if (prev) {
685 p->next = prev->next;
686 prev->next = p;
687 cursor = NULL;
688 continue;
689 } else {
690 p->next = new_list;
691 new_list = p;
692 cursor = NULL;
693 continue;
694 }
695 }
696
697 prev = cursor;
698 cursor = cursor->next;
699 if (!cursor)
700 prev->next = p;
701 }
702 }
703 all_data = new_list;
704}
705
706
707static void draw_c_p_states(void)
708{
709 struct power_event *pwr;
710 pwr = power_events;
711
712 /*
713 * two pass drawing so that the P state bars are on top of the C state blocks
714 */
715 while (pwr) {
716 if (pwr->type == CSTATE)
717 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
718 pwr = pwr->next;
719 }
720
721 pwr = power_events;
722 while (pwr) {
723 if (pwr->type == PSTATE) {
724 if (!pwr->state)
725 pwr->state = min_freq;
726 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
727 }
728 pwr = pwr->next;
729 }
730}
731
732static void draw_wakeups(void)
733{
734 struct wake_event *we;
735 struct per_pid *p;
736 struct per_pidcomm *c;
737
738 we = wake_events;
739 while (we) {
740 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200741 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200742
743 /* locate the column of the waker and wakee */
744 p = all_data;
745 while (p) {
746 if (p->pid == we->waker || p->pid == we->wakee) {
747 c = p->all;
748 while (c) {
749 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900750 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200751 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900752 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200753 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900754 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200755 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900756 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200757 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200758 }
759 c = c->next;
760 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900761 c = p->all;
762 while (c) {
763 if (p->pid == we->waker && !from) {
764 from = c->Y;
765 task_from = strdup(c->comm);
766 }
767 if (p->pid == we->wakee && !to) {
768 to = c->Y;
769 task_to = strdup(c->comm);
770 }
771 c = c->next;
772 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200773 }
774 p = p->next;
775 }
776
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900777 if (!task_from) {
778 task_from = malloc(40);
779 sprintf(task_from, "[%i]", we->waker);
780 }
781 if (!task_to) {
782 task_to = malloc(40);
783 sprintf(task_to, "[%i]", we->wakee);
784 }
785
Arjan van de Ven10274982009-09-12 07:53:05 +0200786 if (we->waker == -1)
787 svg_interrupt(we->time, to);
788 else if (from && to && abs(from - to) == 1)
789 svg_wakeline(we->time, from, to);
790 else
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200791 svg_partial_wakeline(we->time, from, task_from, to, task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200792 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900793
794 free(task_from);
795 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200796 }
797}
798
799static void draw_cpu_usage(void)
800{
801 struct per_pid *p;
802 struct per_pidcomm *c;
803 struct cpu_sample *sample;
804 p = all_data;
805 while (p) {
806 c = p->all;
807 while (c) {
808 sample = c->samples;
809 while (sample) {
810 if (sample->type == TYPE_RUNNING)
811 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
812
813 sample = sample->next;
814 }
815 c = c->next;
816 }
817 p = p->next;
818 }
819}
820
821static void draw_process_bars(void)
822{
823 struct per_pid *p;
824 struct per_pidcomm *c;
825 struct cpu_sample *sample;
826 int Y = 0;
827
828 Y = 2 * numcpus + 2;
829
830 p = all_data;
831 while (p) {
832 c = p->all;
833 while (c) {
834 if (!c->display) {
835 c->Y = 0;
836 c = c->next;
837 continue;
838 }
839
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200840 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +0200841 sample = c->samples;
842 while (sample) {
843 if (sample->type == TYPE_RUNNING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200844 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200845 if (sample->type == TYPE_BLOCKED)
846 svg_box(Y, sample->start_time, sample->end_time, "blocked");
847 if (sample->type == TYPE_WAITING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200848 svg_waiting(Y, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200849 sample = sample->next;
850 }
851
852 if (c->comm) {
853 char comm[256];
854 if (c->total_time > 5000000000) /* 5 seconds */
855 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
856 else
857 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
858
859 svg_text(Y, c->start_time, comm);
860 }
861 c->Y = Y;
862 Y++;
863 c = c->next;
864 }
865 p = p->next;
866 }
867}
868
Arjan van de Venbbe29872009-10-20 07:09:39 +0900869static void add_process_filter(const char *string)
870{
871 struct process_filter *filt;
872 int pid;
873
874 pid = strtoull(string, NULL, 10);
875 filt = malloc(sizeof(struct process_filter));
876 if (!filt)
877 return;
878
879 filt->name = strdup(string);
880 filt->pid = pid;
881 filt->next = process_filter;
882
883 process_filter = filt;
884}
885
886static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
887{
888 struct process_filter *filt;
889 if (!process_filter)
890 return 1;
891
892 filt = process_filter;
893 while (filt) {
894 if (filt->pid && p->pid == filt->pid)
895 return 1;
896 if (strcmp(filt->name, c->comm) == 0)
897 return 1;
898 filt = filt->next;
899 }
900 return 0;
901}
902
903static int determine_display_tasks_filtered(void)
904{
905 struct per_pid *p;
906 struct per_pidcomm *c;
907 int count = 0;
908
909 p = all_data;
910 while (p) {
911 p->display = 0;
912 if (p->start_time == 1)
913 p->start_time = first_time;
914
915 /* no exit marker, task kept running to the end */
916 if (p->end_time == 0)
917 p->end_time = last_time;
918
919 c = p->all;
920
921 while (c) {
922 c->display = 0;
923
924 if (c->start_time == 1)
925 c->start_time = first_time;
926
927 if (passes_filter(p, c)) {
928 c->display = 1;
929 p->display = 1;
930 count++;
931 }
932
933 if (c->end_time == 0)
934 c->end_time = last_time;
935
936 c = c->next;
937 }
938 p = p->next;
939 }
940 return count;
941}
942
Arjan van de Ven10274982009-09-12 07:53:05 +0200943static int determine_display_tasks(u64 threshold)
944{
945 struct per_pid *p;
946 struct per_pidcomm *c;
947 int count = 0;
948
Arjan van de Venbbe29872009-10-20 07:09:39 +0900949 if (process_filter)
950 return determine_display_tasks_filtered();
951
Arjan van de Ven10274982009-09-12 07:53:05 +0200952 p = all_data;
953 while (p) {
954 p->display = 0;
955 if (p->start_time == 1)
956 p->start_time = first_time;
957
958 /* no exit marker, task kept running to the end */
959 if (p->end_time == 0)
960 p->end_time = last_time;
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200961 if (p->total_time >= threshold && !power_only)
Arjan van de Ven10274982009-09-12 07:53:05 +0200962 p->display = 1;
963
964 c = p->all;
965
966 while (c) {
967 c->display = 0;
968
969 if (c->start_time == 1)
970 c->start_time = first_time;
971
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200972 if (c->total_time >= threshold && !power_only) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200973 c->display = 1;
974 count++;
975 }
976
977 if (c->end_time == 0)
978 c->end_time = last_time;
979
980 c = c->next;
981 }
982 p = p->next;
983 }
984 return count;
985}
986
987
988
989#define TIME_THRESH 10000000
990
991static void write_svg_file(const char *filename)
992{
993 u64 i;
994 int count;
995
996 numcpus++;
997
998
999 count = determine_display_tasks(TIME_THRESH);
1000
1001 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
1002 if (count < 15)
1003 count = determine_display_tasks(TIME_THRESH / 10);
1004
Arjan van de Ven5094b652009-09-20 18:14:16 +02001005 open_svg(filename, numcpus, count, first_time, last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001006
Arjan van de Ven5094b652009-09-20 18:14:16 +02001007 svg_time_grid();
Arjan van de Ven10274982009-09-12 07:53:05 +02001008 svg_legenda();
1009
1010 for (i = 0; i < numcpus; i++)
1011 svg_cpu_box(i, max_freq, turbo_frequency);
1012
1013 draw_cpu_usage();
1014 draw_process_bars();
1015 draw_c_p_states();
1016 draw_wakeups();
1017
1018 svg_close();
1019}
1020
Arjan van de Ven10274982009-09-12 07:53:05 +02001021static void process_samples(void)
1022{
1023 struct sample_wrapper *cursor;
1024 event_t *event;
1025
1026 sort_queued_samples();
1027
1028 cursor = all_samples;
1029 while (cursor) {
1030 event = (void *)&cursor->data;
1031 cursor = cursor->next;
1032 process_sample_event(event);
1033 }
1034}
1035
Li Zefan5cbd0802009-12-01 14:05:16 +08001036static int sample_type_check(u64 type)
1037{
1038 sample_type = type;
1039
1040 if (!(sample_type & PERF_SAMPLE_RAW)) {
1041 fprintf(stderr, "No trace samples found in the file.\n"
1042 "Have you used 'perf timechart record' to record it?\n");
1043 return -1;
1044 }
1045
1046 return 0;
1047}
1048
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -02001049static struct perf_event_ops event_ops = {
Li Zefan5cbd0802009-12-01 14:05:16 +08001050 .process_comm_event = process_comm_event,
1051 .process_fork_event = process_fork_event,
1052 .process_exit_event = process_exit_event,
1053 .process_sample_event = queue_sample_event,
1054 .sample_type_check = sample_type_check,
1055};
Arjan van de Ven10274982009-09-12 07:53:05 +02001056
1057static int __cmd_timechart(void)
1058{
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001059 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
1060 0, NULL);
Li Zefan5cbd0802009-12-01 14:05:16 +08001061 int ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001062
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001063 if (session == NULL)
1064 return -ENOMEM;
1065
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -02001066 ret = perf_session__process_events(session, &event_ops);
Li Zefan5cbd0802009-12-01 14:05:16 +08001067 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001068 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001069
1070 process_samples();
1071
1072 end_sample_processing();
1073
1074 sort_pids();
1075
1076 write_svg_file(output_name);
1077
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001078 pr_info("Written %2.1f seconds of trace to %s.\n",
1079 (last_time - first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001080out_delete:
1081 perf_session__delete(session);
1082 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001083}
1084
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001085static const char * const timechart_usage[] = {
1086 "perf timechart [<options>] {record}",
Arjan van de Ven10274982009-09-12 07:53:05 +02001087 NULL
1088};
1089
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001090static const char *record_args[] = {
1091 "record",
1092 "-a",
1093 "-R",
1094 "-M",
1095 "-f",
1096 "-c", "1",
1097 "-e", "power:power_start",
1098 "-e", "power:power_end",
1099 "-e", "power:power_frequency",
1100 "-e", "sched:sched_wakeup",
1101 "-e", "sched:sched_switch",
1102};
1103
1104static int __cmd_record(int argc, const char **argv)
1105{
1106 unsigned int rec_argc, i, j;
1107 const char **rec_argv;
1108
1109 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1110 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1111
1112 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1113 rec_argv[i] = strdup(record_args[i]);
1114
1115 for (j = 1; j < (unsigned int)argc; j++, i++)
1116 rec_argv[i] = argv[j];
1117
1118 return cmd_record(i, rec_argv, NULL);
1119}
1120
Arjan van de Venbbe29872009-10-20 07:09:39 +09001121static int
1122parse_process(const struct option *opt __used, const char *arg, int __used unset)
1123{
1124 if (arg)
1125 add_process_filter(arg);
1126 return 0;
1127}
1128
Arjan van de Ven10274982009-09-12 07:53:05 +02001129static const struct option options[] = {
1130 OPT_STRING('i', "input", &input_name, "file",
1131 "input file name"),
1132 OPT_STRING('o', "output", &output_name, "file",
1133 "output file name"),
Arjan van de Ven5094b652009-09-20 18:14:16 +02001134 OPT_INTEGER('w', "width", &svg_page_width,
1135 "page width"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001136 OPT_BOOLEAN('P', "power-only", &power_only,
Arjan van de Ven39a90a82009-09-24 15:40:13 +02001137 "output power data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001138 OPT_CALLBACK('p', "process", NULL, "process",
1139 "process selector. Pass a pid or process name.",
1140 parse_process),
Arjan van de Ven10274982009-09-12 07:53:05 +02001141 OPT_END()
1142};
1143
1144
1145int cmd_timechart(int argc, const char **argv, const char *prefix __used)
1146{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001147 symbol__init(0);
Arjan van de Ven10274982009-09-12 07:53:05 +02001148
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001149 argc = parse_options(argc, argv, options, timechart_usage,
1150 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001151
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001152 if (argc && !strncmp(argv[0], "rec", 3))
1153 return __cmd_record(argc, argv);
1154 else if (argc)
1155 usage_with_options(timechart_usage, options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001156
1157 setup_pager();
1158
1159 return __cmd_timechart();
1160}