blob: b1a8a3b841ccfbb7bfb895eec914533a07f75563 [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"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -020022#include "util/evsel.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020023#include <linux/rbtree.h>
24#include "util/symbol.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020025#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"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020035#include "util/tool.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020036
Thomas Renninger20c457b2011-01-03 17:50:45 +010037#define SUPPORT_OLD_POWER_EVENTS 1
38#define PWR_EVENT_EXIT -1
39
40
Robert Richterefad1412011-12-07 10:02:54 +010041static const char *input_name;
42static const char *output_name = "output.svg";
Arjan van de Ven10274982009-09-12 07:53:05 +020043
Arjan van de Ven10274982009-09-12 07:53:05 +020044static unsigned int numcpus;
45static u64 min_freq; /* Lowest CPU frequency seen */
46static u64 max_freq; /* Highest CPU frequency seen */
47static u64 turbo_frequency;
48
49static u64 first_time, last_time;
50
Ian Munsiec0555642010-04-13 18:37:33 +100051static bool power_only;
Arjan van de Ven39a90a82009-09-24 15:40:13 +020052
Arjan van de Ven10274982009-09-12 07:53:05 +020053
Arjan van de Ven10274982009-09-12 07:53:05 +020054struct per_pid;
55struct per_pidcomm;
56
57struct cpu_sample;
58struct power_event;
59struct wake_event;
60
61struct sample_wrapper;
62
63/*
64 * Datastructure layout:
65 * We keep an list of "pid"s, matching the kernels notion of a task struct.
66 * Each "pid" entry, has a list of "comm"s.
67 * this is because we want to track different programs different, while
68 * exec will reuse the original pid (by design).
69 * Each comm has a list of samples that will be used to draw
70 * final graph.
71 */
72
73struct per_pid {
74 struct per_pid *next;
75
76 int pid;
77 int ppid;
78
79 u64 start_time;
80 u64 end_time;
81 u64 total_time;
82 int display;
83
84 struct per_pidcomm *all;
85 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +020086};
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
Arjan van de Venbbe29872009-10-20 07:09:39 +0900152struct process_filter;
153struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800154 char *name;
155 int pid;
156 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900157};
158
159static struct process_filter *process_filter;
160
161
Arjan van de Ven10274982009-09-12 07:53:05 +0200162static struct per_pid *find_create_pid(int pid)
163{
164 struct per_pid *cursor = all_data;
165
166 while (cursor) {
167 if (cursor->pid == pid)
168 return cursor;
169 cursor = cursor->next;
170 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300171 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200172 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200173 cursor->pid = pid;
174 cursor->next = all_data;
175 all_data = cursor;
176 return cursor;
177}
178
179static void pid_set_comm(int pid, char *comm)
180{
181 struct per_pid *p;
182 struct per_pidcomm *c;
183 p = find_create_pid(pid);
184 c = p->all;
185 while (c) {
186 if (c->comm && strcmp(c->comm, comm) == 0) {
187 p->current = c;
188 return;
189 }
190 if (!c->comm) {
191 c->comm = strdup(comm);
192 p->current = c;
193 return;
194 }
195 c = c->next;
196 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300197 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200198 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200199 c->comm = strdup(comm);
200 p->current = c;
201 c->next = p->all;
202 p->all = c;
203}
204
205static void pid_fork(int pid, int ppid, u64 timestamp)
206{
207 struct per_pid *p, *pp;
208 p = find_create_pid(pid);
209 pp = find_create_pid(ppid);
210 p->ppid = ppid;
211 if (pp->current && pp->current->comm && !p->current)
212 pid_set_comm(pid, pp->current->comm);
213
214 p->start_time = timestamp;
215 if (p->current) {
216 p->current->start_time = timestamp;
217 p->current->state_since = timestamp;
218 }
219}
220
221static void pid_exit(int pid, u64 timestamp)
222{
223 struct per_pid *p;
224 p = find_create_pid(pid);
225 p->end_time = timestamp;
226 if (p->current)
227 p->current->end_time = timestamp;
228}
229
230static void
231pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
232{
233 struct per_pid *p;
234 struct per_pidcomm *c;
235 struct cpu_sample *sample;
236
237 p = find_create_pid(pid);
238 c = p->current;
239 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300240 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200241 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200242 p->current = c;
243 c->next = p->all;
244 p->all = c;
245 }
246
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300247 sample = zalloc(sizeof(*sample));
Arjan van de Ven10274982009-09-12 07:53:05 +0200248 assert(sample != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200249 sample->start_time = start;
250 sample->end_time = end;
251 sample->type = type;
252 sample->next = c->samples;
253 sample->cpu = cpu;
254 c->samples = sample;
255
256 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
257 c->total_time += (end-start);
258 p->total_time += (end-start);
259 }
260
261 if (c->start_time == 0 || c->start_time > start)
262 c->start_time = start;
263 if (p->start_time == 0 || p->start_time > start)
264 p->start_time = start;
Arjan van de Ven10274982009-09-12 07:53:05 +0200265}
266
267#define MAX_CPUS 4096
268
269static u64 cpus_cstate_start_times[MAX_CPUS];
270static int cpus_cstate_state[MAX_CPUS];
271static u64 cpus_pstate_start_times[MAX_CPUS];
272static u64 cpus_pstate_state[MAX_CPUS];
273
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300274static int process_comm_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200275 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300276 struct perf_sample *sample __maybe_unused,
277 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200278{
Arjan van de Ven8f06d7e2010-01-16 12:53:19 -0800279 pid_set_comm(event->comm.tid, event->comm.comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200280 return 0;
281}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200282
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300283static int process_fork_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200284 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300285 struct perf_sample *sample __maybe_unused,
286 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200287{
288 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
289 return 0;
290}
291
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300292static int process_exit_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200293 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300294 struct perf_sample *sample __maybe_unused,
295 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200296{
297 pid_exit(event->fork.pid, event->fork.time);
298 return 0;
299}
300
301struct trace_entry {
Arjan van de Ven10274982009-09-12 07:53:05 +0200302 unsigned short type;
303 unsigned char flags;
304 unsigned char preempt_count;
305 int pid;
OGAWA Hirofumi028c5152009-12-06 20:07:29 +0900306 int lock_depth;
Arjan van de Ven10274982009-09-12 07:53:05 +0200307};
308
Thomas Renninger20c457b2011-01-03 17:50:45 +0100309#ifdef SUPPORT_OLD_POWER_EVENTS
310static int use_old_power_events;
311struct power_entry_old {
Arjan van de Ven10274982009-09-12 07:53:05 +0200312 struct trace_entry te;
Thomas Renninger4c21adf2010-07-20 16:59:34 -0700313 u64 type;
314 u64 value;
315 u64 cpu_id;
Arjan van de Ven10274982009-09-12 07:53:05 +0200316};
Thomas Renninger20c457b2011-01-03 17:50:45 +0100317#endif
318
319struct power_processor_entry {
320 struct trace_entry te;
321 u32 state;
322 u32 cpu_id;
323};
Arjan van de Ven10274982009-09-12 07:53:05 +0200324
325#define TASK_COMM_LEN 16
326struct wakeup_entry {
327 struct trace_entry te;
328 char comm[TASK_COMM_LEN];
329 int pid;
330 int prio;
331 int success;
332};
333
334/*
335 * trace_flag_type is an enumeration that holds different
336 * states when a trace occurs. These are:
337 * IRQS_OFF - interrupts were disabled
338 * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
339 * NEED_RESCED - reschedule is requested
340 * HARDIRQ - inside an interrupt handler
341 * SOFTIRQ - inside a softirq handler
342 */
343enum trace_flag_type {
344 TRACE_FLAG_IRQS_OFF = 0x01,
345 TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
346 TRACE_FLAG_NEED_RESCHED = 0x04,
347 TRACE_FLAG_HARDIRQ = 0x08,
348 TRACE_FLAG_SOFTIRQ = 0x10,
349};
350
351
352
353struct sched_switch {
354 struct trace_entry te;
355 char prev_comm[TASK_COMM_LEN];
356 int prev_pid;
357 int prev_prio;
358 long prev_state; /* Arjan weeps. */
359 char next_comm[TASK_COMM_LEN];
360 int next_pid;
361 int next_prio;
362};
363
364static void c_state_start(int cpu, u64 timestamp, int state)
365{
366 cpus_cstate_start_times[cpu] = timestamp;
367 cpus_cstate_state[cpu] = state;
368}
369
370static void c_state_end(int cpu, u64 timestamp)
371{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300372 struct power_event *pwr = zalloc(sizeof(*pwr));
373
Arjan van de Ven10274982009-09-12 07:53:05 +0200374 if (!pwr)
375 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200376
377 pwr->state = cpus_cstate_state[cpu];
378 pwr->start_time = cpus_cstate_start_times[cpu];
379 pwr->end_time = timestamp;
380 pwr->cpu = cpu;
381 pwr->type = CSTATE;
382 pwr->next = power_events;
383
384 power_events = pwr;
385}
386
387static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
388{
389 struct power_event *pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200390
391 if (new_freq > 8000000) /* detect invalid data */
392 return;
393
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300394 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200395 if (!pwr)
396 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200397
398 pwr->state = cpus_pstate_state[cpu];
399 pwr->start_time = cpus_pstate_start_times[cpu];
400 pwr->end_time = timestamp;
401 pwr->cpu = cpu;
402 pwr->type = PSTATE;
403 pwr->next = power_events;
404
405 if (!pwr->start_time)
406 pwr->start_time = first_time;
407
408 power_events = pwr;
409
410 cpus_pstate_state[cpu] = new_freq;
411 cpus_pstate_start_times[cpu] = timestamp;
412
413 if ((u64)new_freq > max_freq)
414 max_freq = new_freq;
415
416 if (new_freq < min_freq || min_freq == 0)
417 min_freq = new_freq;
418
419 if (new_freq == max_freq - 1000)
420 turbo_frequency = max_freq;
421}
422
423static void
424sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
425{
Arjan van de Ven10274982009-09-12 07:53:05 +0200426 struct per_pid *p;
427 struct wakeup_entry *wake = (void *)te;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300428 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200429
Arjan van de Ven10274982009-09-12 07:53:05 +0200430 if (!we)
431 return;
432
Arjan van de Ven10274982009-09-12 07:53:05 +0200433 we->time = timestamp;
434 we->waker = pid;
435
436 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
437 we->waker = -1;
438
439 we->wakee = wake->pid;
440 we->next = wake_events;
441 wake_events = we;
442 p = find_create_pid(we->wakee);
443
444 if (p && p->current && p->current->state == TYPE_NONE) {
445 p->current->state_since = timestamp;
446 p->current->state = TYPE_WAITING;
447 }
448 if (p && p->current && p->current->state == TYPE_BLOCKED) {
449 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
450 p->current->state_since = timestamp;
451 p->current->state = TYPE_WAITING;
452 }
453}
454
455static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
456{
457 struct per_pid *p = NULL, *prev_p;
458 struct sched_switch *sw = (void *)te;
459
460
461 prev_p = find_create_pid(sw->prev_pid);
462
463 p = find_create_pid(sw->next_pid);
464
465 if (prev_p->current && prev_p->current->state != TYPE_NONE)
466 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
467 if (p && p->current) {
468 if (p->current->state != TYPE_NONE)
469 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
470
Julia Lawall33e26a12010-08-05 22:27:51 +0200471 p->current->state_since = timestamp;
472 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200473 }
474
475 if (prev_p->current) {
476 prev_p->current->state = TYPE_NONE;
477 prev_p->current->state_since = timestamp;
478 if (sw->prev_state & 2)
479 prev_p->current->state = TYPE_BLOCKED;
480 if (sw->prev_state == 0)
481 prev_p->current->state = TYPE_WAITING;
482 }
483}
484
485
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300486static int process_sample_event(struct perf_tool *tool __maybe_unused,
487 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200488 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200489 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300490 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200491{
Arjan van de Ven10274982009-09-12 07:53:05 +0200492 struct trace_entry *te;
493
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200494 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200495 if (!first_time || first_time > sample->time)
496 first_time = sample->time;
497 if (last_time < sample->time)
498 last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200499 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200500
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200501 te = (void *)sample->raw_data;
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200502 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200503 char *event_str;
Thomas Renninger20c457b2011-01-03 17:50:45 +0100504#ifdef SUPPORT_OLD_POWER_EVENTS
505 struct power_entry_old *peo;
506 peo = (void *)te;
507#endif
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300508 /*
509 * FIXME: use evsel, its already mapped from id to perf_evsel,
510 * remove perf_header__find_event infrastructure bits.
511 * Mapping all these "power:cpu_idle" strings to the tracepoint
512 * ID and then just comparing against evsel->attr.config.
513 *
514 * e.g.:
515 *
516 * if (evsel->attr.config == power_cpu_idle_id)
517 */
Arjan van de Ven10274982009-09-12 07:53:05 +0200518 event_str = perf_header__find_event(te->type);
519
520 if (!event_str)
521 return 0;
522
Thomas Renninger54b08f52011-02-27 22:36:46 +0100523 if (sample->cpu > numcpus)
524 numcpus = sample->cpu;
525
Thomas Renninger20c457b2011-01-03 17:50:45 +0100526 if (strcmp(event_str, "power:cpu_idle") == 0) {
527 struct power_processor_entry *ppe = (void *)te;
528 if (ppe->state == (u32)PWR_EVENT_EXIT)
529 c_state_end(ppe->cpu_id, sample->time);
530 else
531 c_state_start(ppe->cpu_id, sample->time,
532 ppe->state);
533 }
534 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
535 struct power_processor_entry *ppe = (void *)te;
536 p_state_change(ppe->cpu_id, sample->time, ppe->state);
537 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200538
Thomas Renninger20c457b2011-01-03 17:50:45 +0100539 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200540 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
Arjan van de Ven10274982009-09-12 07:53:05 +0200541
Thomas Renninger20c457b2011-01-03 17:50:45 +0100542 else if (strcmp(event_str, "sched:sched_switch") == 0)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200543 sched_switch(sample->cpu, sample->time, te);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100544
545#ifdef SUPPORT_OLD_POWER_EVENTS
546 if (use_old_power_events) {
547 if (strcmp(event_str, "power:power_start") == 0)
548 c_state_start(peo->cpu_id, sample->time,
549 peo->value);
550
551 else if (strcmp(event_str, "power:power_end") == 0)
552 c_state_end(sample->cpu, sample->time);
553
554 else if (strcmp(event_str,
555 "power:power_frequency") == 0)
556 p_state_change(peo->cpu_id, sample->time,
557 peo->value);
558 }
559#endif
Arjan van de Ven10274982009-09-12 07:53:05 +0200560 }
561 return 0;
562}
563
564/*
565 * After the last sample we need to wrap up the current C/P state
566 * and close out each CPU for these.
567 */
568static void end_sample_processing(void)
569{
570 u64 cpu;
571 struct power_event *pwr;
572
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200573 for (cpu = 0; cpu <= numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200574 /* C state */
575#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300576 pwr = zalloc(sizeof(*pwr));
577 if (!pwr)
578 return;
579
Arjan van de Ven10274982009-09-12 07:53:05 +0200580 pwr->state = cpus_cstate_state[cpu];
581 pwr->start_time = cpus_cstate_start_times[cpu];
582 pwr->end_time = last_time;
583 pwr->cpu = cpu;
584 pwr->type = CSTATE;
585 pwr->next = power_events;
586
587 power_events = pwr;
588#endif
589 /* P state */
590
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300591 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200592 if (!pwr)
593 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200594
595 pwr->state = cpus_pstate_state[cpu];
596 pwr->start_time = cpus_pstate_start_times[cpu];
597 pwr->end_time = last_time;
598 pwr->cpu = cpu;
599 pwr->type = PSTATE;
600 pwr->next = power_events;
601
602 if (!pwr->start_time)
603 pwr->start_time = first_time;
604 if (!pwr->state)
605 pwr->state = min_freq;
606 power_events = pwr;
607 }
608}
609
Arjan van de Ven10274982009-09-12 07:53:05 +0200610/*
611 * Sort the pid datastructure
612 */
613static void sort_pids(void)
614{
615 struct per_pid *new_list, *p, *cursor, *prev;
616 /* sort by ppid first, then by pid, lowest to highest */
617
618 new_list = NULL;
619
620 while (all_data) {
621 p = all_data;
622 all_data = p->next;
623 p->next = NULL;
624
625 if (new_list == NULL) {
626 new_list = p;
627 p->next = NULL;
628 continue;
629 }
630 prev = NULL;
631 cursor = new_list;
632 while (cursor) {
633 if (cursor->ppid > p->ppid ||
634 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
635 /* must insert before */
636 if (prev) {
637 p->next = prev->next;
638 prev->next = p;
639 cursor = NULL;
640 continue;
641 } else {
642 p->next = new_list;
643 new_list = p;
644 cursor = NULL;
645 continue;
646 }
647 }
648
649 prev = cursor;
650 cursor = cursor->next;
651 if (!cursor)
652 prev->next = p;
653 }
654 }
655 all_data = new_list;
656}
657
658
659static void draw_c_p_states(void)
660{
661 struct power_event *pwr;
662 pwr = power_events;
663
664 /*
665 * two pass drawing so that the P state bars are on top of the C state blocks
666 */
667 while (pwr) {
668 if (pwr->type == CSTATE)
669 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
670 pwr = pwr->next;
671 }
672
673 pwr = power_events;
674 while (pwr) {
675 if (pwr->type == PSTATE) {
676 if (!pwr->state)
677 pwr->state = min_freq;
678 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
679 }
680 pwr = pwr->next;
681 }
682}
683
684static void draw_wakeups(void)
685{
686 struct wake_event *we;
687 struct per_pid *p;
688 struct per_pidcomm *c;
689
690 we = wake_events;
691 while (we) {
692 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200693 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200694
695 /* locate the column of the waker and wakee */
696 p = all_data;
697 while (p) {
698 if (p->pid == we->waker || p->pid == we->wakee) {
699 c = p->all;
700 while (c) {
701 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900702 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200703 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900704 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200705 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900706 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200707 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900708 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200709 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200710 }
711 c = c->next;
712 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900713 c = p->all;
714 while (c) {
715 if (p->pid == we->waker && !from) {
716 from = c->Y;
717 task_from = strdup(c->comm);
718 }
719 if (p->pid == we->wakee && !to) {
720 to = c->Y;
721 task_to = strdup(c->comm);
722 }
723 c = c->next;
724 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200725 }
726 p = p->next;
727 }
728
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900729 if (!task_from) {
730 task_from = malloc(40);
731 sprintf(task_from, "[%i]", we->waker);
732 }
733 if (!task_to) {
734 task_to = malloc(40);
735 sprintf(task_to, "[%i]", we->wakee);
736 }
737
Arjan van de Ven10274982009-09-12 07:53:05 +0200738 if (we->waker == -1)
739 svg_interrupt(we->time, to);
740 else if (from && to && abs(from - to) == 1)
741 svg_wakeline(we->time, from, to);
742 else
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200743 svg_partial_wakeline(we->time, from, task_from, to, task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200744 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900745
746 free(task_from);
747 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200748 }
749}
750
751static void draw_cpu_usage(void)
752{
753 struct per_pid *p;
754 struct per_pidcomm *c;
755 struct cpu_sample *sample;
756 p = all_data;
757 while (p) {
758 c = p->all;
759 while (c) {
760 sample = c->samples;
761 while (sample) {
762 if (sample->type == TYPE_RUNNING)
763 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
764
765 sample = sample->next;
766 }
767 c = c->next;
768 }
769 p = p->next;
770 }
771}
772
773static void draw_process_bars(void)
774{
775 struct per_pid *p;
776 struct per_pidcomm *c;
777 struct cpu_sample *sample;
778 int Y = 0;
779
780 Y = 2 * numcpus + 2;
781
782 p = all_data;
783 while (p) {
784 c = p->all;
785 while (c) {
786 if (!c->display) {
787 c->Y = 0;
788 c = c->next;
789 continue;
790 }
791
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200792 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +0200793 sample = c->samples;
794 while (sample) {
795 if (sample->type == TYPE_RUNNING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200796 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200797 if (sample->type == TYPE_BLOCKED)
798 svg_box(Y, sample->start_time, sample->end_time, "blocked");
799 if (sample->type == TYPE_WAITING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200800 svg_waiting(Y, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200801 sample = sample->next;
802 }
803
804 if (c->comm) {
805 char comm[256];
806 if (c->total_time > 5000000000) /* 5 seconds */
807 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
808 else
809 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
810
811 svg_text(Y, c->start_time, comm);
812 }
813 c->Y = Y;
814 Y++;
815 c = c->next;
816 }
817 p = p->next;
818 }
819}
820
Arjan van de Venbbe29872009-10-20 07:09:39 +0900821static void add_process_filter(const char *string)
822{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300823 int pid = strtoull(string, NULL, 10);
824 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +0900825
Arjan van de Venbbe29872009-10-20 07:09:39 +0900826 if (!filt)
827 return;
828
829 filt->name = strdup(string);
830 filt->pid = pid;
831 filt->next = process_filter;
832
833 process_filter = filt;
834}
835
836static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
837{
838 struct process_filter *filt;
839 if (!process_filter)
840 return 1;
841
842 filt = process_filter;
843 while (filt) {
844 if (filt->pid && p->pid == filt->pid)
845 return 1;
846 if (strcmp(filt->name, c->comm) == 0)
847 return 1;
848 filt = filt->next;
849 }
850 return 0;
851}
852
853static int determine_display_tasks_filtered(void)
854{
855 struct per_pid *p;
856 struct per_pidcomm *c;
857 int count = 0;
858
859 p = all_data;
860 while (p) {
861 p->display = 0;
862 if (p->start_time == 1)
863 p->start_time = first_time;
864
865 /* no exit marker, task kept running to the end */
866 if (p->end_time == 0)
867 p->end_time = last_time;
868
869 c = p->all;
870
871 while (c) {
872 c->display = 0;
873
874 if (c->start_time == 1)
875 c->start_time = first_time;
876
877 if (passes_filter(p, c)) {
878 c->display = 1;
879 p->display = 1;
880 count++;
881 }
882
883 if (c->end_time == 0)
884 c->end_time = last_time;
885
886 c = c->next;
887 }
888 p = p->next;
889 }
890 return count;
891}
892
Arjan van de Ven10274982009-09-12 07:53:05 +0200893static int determine_display_tasks(u64 threshold)
894{
895 struct per_pid *p;
896 struct per_pidcomm *c;
897 int count = 0;
898
Arjan van de Venbbe29872009-10-20 07:09:39 +0900899 if (process_filter)
900 return determine_display_tasks_filtered();
901
Arjan van de Ven10274982009-09-12 07:53:05 +0200902 p = all_data;
903 while (p) {
904 p->display = 0;
905 if (p->start_time == 1)
906 p->start_time = first_time;
907
908 /* no exit marker, task kept running to the end */
909 if (p->end_time == 0)
910 p->end_time = last_time;
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200911 if (p->total_time >= threshold && !power_only)
Arjan van de Ven10274982009-09-12 07:53:05 +0200912 p->display = 1;
913
914 c = p->all;
915
916 while (c) {
917 c->display = 0;
918
919 if (c->start_time == 1)
920 c->start_time = first_time;
921
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200922 if (c->total_time >= threshold && !power_only) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200923 c->display = 1;
924 count++;
925 }
926
927 if (c->end_time == 0)
928 c->end_time = last_time;
929
930 c = c->next;
931 }
932 p = p->next;
933 }
934 return count;
935}
936
937
938
939#define TIME_THRESH 10000000
940
941static void write_svg_file(const char *filename)
942{
943 u64 i;
944 int count;
945
946 numcpus++;
947
948
949 count = determine_display_tasks(TIME_THRESH);
950
951 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
952 if (count < 15)
953 count = determine_display_tasks(TIME_THRESH / 10);
954
Arjan van de Ven5094b652009-09-20 18:14:16 +0200955 open_svg(filename, numcpus, count, first_time, last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200956
Arjan van de Ven5094b652009-09-20 18:14:16 +0200957 svg_time_grid();
Arjan van de Ven10274982009-09-12 07:53:05 +0200958 svg_legenda();
959
960 for (i = 0; i < numcpus; i++)
961 svg_cpu_box(i, max_freq, turbo_frequency);
962
963 draw_cpu_usage();
964 draw_process_bars();
965 draw_c_p_states();
966 draw_wakeups();
967
968 svg_close();
969}
970
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200971static struct perf_tool perf_timechart = {
Frederic Weisbecker9df9bbb2010-04-24 01:18:48 +0200972 .comm = process_comm_event,
973 .fork = process_fork_event,
974 .exit = process_exit_event,
975 .sample = process_sample_event,
976 .ordered_samples = true,
Li Zefan5cbd0802009-12-01 14:05:16 +0800977};
Arjan van de Ven10274982009-09-12 07:53:05 +0200978
979static int __cmd_timechart(void)
980{
Ian Munsie21ef97f2010-12-10 14:09:16 +1100981 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200982 0, false, &perf_timechart);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200983 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200984
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200985 if (session == NULL)
986 return -ENOMEM;
987
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200988 if (!perf_session__has_traces(session, "timechart record"))
989 goto out_delete;
990
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200991 ret = perf_session__process_events(session, &perf_timechart);
Li Zefan5cbd0802009-12-01 14:05:16 +0800992 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200993 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +0200994
Arjan van de Ven10274982009-09-12 07:53:05 +0200995 end_sample_processing();
996
997 sort_pids();
998
999 write_svg_file(output_name);
1000
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001001 pr_info("Written %2.1f seconds of trace to %s.\n",
1002 (last_time - first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001003out_delete:
1004 perf_session__delete(session);
1005 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001006}
1007
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001008static const char * const timechart_usage[] = {
1009 "perf timechart [<options>] {record}",
Arjan van de Ven10274982009-09-12 07:53:05 +02001010 NULL
1011};
1012
Thomas Renninger20c457b2011-01-03 17:50:45 +01001013#ifdef SUPPORT_OLD_POWER_EVENTS
1014static const char * const record_old_args[] = {
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001015 "record",
1016 "-a",
1017 "-R",
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001018 "-f",
1019 "-c", "1",
1020 "-e", "power:power_start",
1021 "-e", "power:power_end",
1022 "-e", "power:power_frequency",
1023 "-e", "sched:sched_wakeup",
1024 "-e", "sched:sched_switch",
1025};
Thomas Renninger20c457b2011-01-03 17:50:45 +01001026#endif
1027
1028static const char * const record_new_args[] = {
1029 "record",
1030 "-a",
1031 "-R",
1032 "-f",
1033 "-c", "1",
1034 "-e", "power:cpu_frequency",
1035 "-e", "power:cpu_idle",
1036 "-e", "sched:sched_wakeup",
1037 "-e", "sched:sched_switch",
1038};
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001039
1040static int __cmd_record(int argc, const char **argv)
1041{
1042 unsigned int rec_argc, i, j;
1043 const char **rec_argv;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001044 const char * const *record_args = record_new_args;
1045 unsigned int record_elems = ARRAY_SIZE(record_new_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001046
Thomas Renninger20c457b2011-01-03 17:50:45 +01001047#ifdef SUPPORT_OLD_POWER_EVENTS
1048 if (!is_valid_tracepoint("power:cpu_idle") &&
1049 is_valid_tracepoint("power:power_start")) {
1050 use_old_power_events = 1;
1051 record_args = record_old_args;
1052 record_elems = ARRAY_SIZE(record_old_args);
1053 }
1054#endif
1055
1056 rec_argc = record_elems + argc - 1;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001057 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1058
Chris Samuelce47dc52010-11-13 13:35:06 +11001059 if (rec_argv == NULL)
1060 return -ENOMEM;
1061
Thomas Renninger20c457b2011-01-03 17:50:45 +01001062 for (i = 0; i < record_elems; i++)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001063 rec_argv[i] = strdup(record_args[i]);
1064
1065 for (j = 1; j < (unsigned int)argc; j++, i++)
1066 rec_argv[i] = argv[j];
1067
1068 return cmd_record(i, rec_argv, NULL);
1069}
1070
Arjan van de Venbbe29872009-10-20 07:09:39 +09001071static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001072parse_process(const struct option *opt __maybe_unused, const char *arg,
1073 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001074{
1075 if (arg)
1076 add_process_filter(arg);
1077 return 0;
1078}
1079
Arjan van de Ven10274982009-09-12 07:53:05 +02001080static const struct option options[] = {
1081 OPT_STRING('i', "input", &input_name, "file",
1082 "input file name"),
1083 OPT_STRING('o', "output", &output_name, "file",
1084 "output file name"),
Arjan van de Ven5094b652009-09-20 18:14:16 +02001085 OPT_INTEGER('w', "width", &svg_page_width,
1086 "page width"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001087 OPT_BOOLEAN('P', "power-only", &power_only,
Arjan van de Ven39a90a82009-09-24 15:40:13 +02001088 "output power data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001089 OPT_CALLBACK('p', "process", NULL, "process",
1090 "process selector. Pass a pid or process name.",
1091 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001092 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1093 "Look for files with symbols relative to this directory"),
Arjan van de Ven10274982009-09-12 07:53:05 +02001094 OPT_END()
1095};
1096
1097
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001098int cmd_timechart(int argc, const char **argv,
1099 const char *prefix __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +02001100{
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001101 argc = parse_options(argc, argv, options, timechart_usage,
1102 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001103
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001104 symbol__init();
1105
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001106 if (argc && !strncmp(argv[0], "rec", 3))
1107 return __cmd_record(argc, argv);
1108 else if (argc)
1109 usage_with_options(timechart_usage, options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001110
1111 setup_pager();
1112
1113 return __cmd_timechart();
1114}