blob: a5120095978e10a81a6f2b4e347d35abe6e79126 [file] [log] [blame]
Arjan van de Ven102749892009-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
Jiri Olsac85cffa2013-07-11 17:28:29 +020015#include <traceevent/event-parse.h>
16
Arjan van de Ven102749892009-09-12 07:53:05 +020017#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"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -020024#include "util/evsel.h"
Arjan van de Ven102749892009-09-12 07:53:05 +020025#include <linux/rbtree.h>
26#include "util/symbol.h"
Arjan van de Ven102749892009-09-12 07:53:05 +020027#include "util/callchain.h"
28#include "util/strlist.h"
29
30#include "perf.h"
31#include "util/header.h"
32#include "util/parse-options.h"
33#include "util/parse-events.h"
Li Zefan5cbd0802009-12-01 14:05:16 +080034#include "util/event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020035#include "util/session.h"
Arjan van de Ven102749892009-09-12 07:53:05 +020036#include "util/svghelper.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020037#include "util/tool.h"
Arjan van de Ven102749892009-09-12 07:53:05 +020038
Thomas Renninger20c457b2011-01-03 17:50:45 +010039#define SUPPORT_OLD_POWER_EVENTS 1
40#define PWR_EVENT_EXIT -1
41
42
Arjan van de Ven102749892009-09-12 07:53:05 +020043static unsigned int numcpus;
44static u64 min_freq; /* Lowest CPU frequency seen */
45static u64 max_freq; /* Highest CPU frequency seen */
46static u64 turbo_frequency;
47
48static u64 first_time, last_time;
49
Ian Munsiec0555642010-04-13 18:37:33 +100050static bool power_only;
Arjan van de Ven39a90a82009-09-24 15:40:13 +020051
Arjan van de Ven102749892009-09-12 07:53:05 +020052
Arjan van de Ven102749892009-09-12 07:53:05 +020053struct per_pid;
54struct per_pidcomm;
55
56struct cpu_sample;
57struct power_event;
58struct wake_event;
59
60struct sample_wrapper;
61
62/*
63 * Datastructure layout:
64 * We keep an list of "pid"s, matching the kernels notion of a task struct.
65 * Each "pid" entry, has a list of "comm"s.
66 * this is because we want to track different programs different, while
67 * exec will reuse the original pid (by design).
68 * Each comm has a list of samples that will be used to draw
69 * final graph.
70 */
71
72struct per_pid {
73 struct per_pid *next;
74
75 int pid;
76 int ppid;
77
78 u64 start_time;
79 u64 end_time;
80 u64 total_time;
81 int display;
82
83 struct per_pidcomm *all;
84 struct per_pidcomm *current;
Arjan van de Ven102749892009-09-12 07:53:05 +020085};
86
87
88struct per_pidcomm {
89 struct per_pidcomm *next;
90
91 u64 start_time;
92 u64 end_time;
93 u64 total_time;
94
95 int Y;
96 int display;
97
98 long state;
99 u64 state_since;
100
101 char *comm;
102
103 struct cpu_sample *samples;
104};
105
106struct sample_wrapper {
107 struct sample_wrapper *next;
108
109 u64 timestamp;
110 unsigned char data[0];
111};
112
113#define TYPE_NONE 0
114#define TYPE_RUNNING 1
115#define TYPE_WAITING 2
116#define TYPE_BLOCKED 3
117
118struct cpu_sample {
119 struct cpu_sample *next;
120
121 u64 start_time;
122 u64 end_time;
123 int type;
124 int cpu;
125};
126
127static struct per_pid *all_data;
128
129#define CSTATE 1
130#define PSTATE 2
131
132struct power_event {
133 struct power_event *next;
134 int type;
135 int state;
136 u64 start_time;
137 u64 end_time;
138 int cpu;
139};
140
141struct wake_event {
142 struct wake_event *next;
143 int waker;
144 int wakee;
145 u64 time;
146};
147
148static struct power_event *power_events;
149static struct wake_event *wake_events;
150
Arjan van de Venbbe29872009-10-20 07:09:39 +0900151struct process_filter;
152struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800153 char *name;
154 int pid;
155 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900156};
157
158static struct process_filter *process_filter;
159
160
Arjan van de Ven102749892009-09-12 07:53:05 +0200161static struct per_pid *find_create_pid(int pid)
162{
163 struct per_pid *cursor = all_data;
164
165 while (cursor) {
166 if (cursor->pid == pid)
167 return cursor;
168 cursor = cursor->next;
169 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300170 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven102749892009-09-12 07:53:05 +0200171 assert(cursor != NULL);
Arjan van de Ven102749892009-09-12 07:53:05 +0200172 cursor->pid = pid;
173 cursor->next = all_data;
174 all_data = cursor;
175 return cursor;
176}
177
178static void pid_set_comm(int pid, char *comm)
179{
180 struct per_pid *p;
181 struct per_pidcomm *c;
182 p = find_create_pid(pid);
183 c = p->all;
184 while (c) {
185 if (c->comm && strcmp(c->comm, comm) == 0) {
186 p->current = c;
187 return;
188 }
189 if (!c->comm) {
190 c->comm = strdup(comm);
191 p->current = c;
192 return;
193 }
194 c = c->next;
195 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300196 c = zalloc(sizeof(*c));
Arjan van de Ven102749892009-09-12 07:53:05 +0200197 assert(c != NULL);
Arjan van de Ven102749892009-09-12 07:53:05 +0200198 c->comm = strdup(comm);
199 p->current = c;
200 c->next = p->all;
201 p->all = c;
202}
203
204static void pid_fork(int pid, int ppid, u64 timestamp)
205{
206 struct per_pid *p, *pp;
207 p = find_create_pid(pid);
208 pp = find_create_pid(ppid);
209 p->ppid = ppid;
210 if (pp->current && pp->current->comm && !p->current)
211 pid_set_comm(pid, pp->current->comm);
212
213 p->start_time = timestamp;
214 if (p->current) {
215 p->current->start_time = timestamp;
216 p->current->state_since = timestamp;
217 }
218}
219
220static void pid_exit(int pid, u64 timestamp)
221{
222 struct per_pid *p;
223 p = find_create_pid(pid);
224 p->end_time = timestamp;
225 if (p->current)
226 p->current->end_time = timestamp;
227}
228
229static void
230pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
231{
232 struct per_pid *p;
233 struct per_pidcomm *c;
234 struct cpu_sample *sample;
235
236 p = find_create_pid(pid);
237 c = p->current;
238 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300239 c = zalloc(sizeof(*c));
Arjan van de Ven102749892009-09-12 07:53:05 +0200240 assert(c != NULL);
Arjan van de Ven102749892009-09-12 07:53:05 +0200241 p->current = c;
242 c->next = p->all;
243 p->all = c;
244 }
245
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300246 sample = zalloc(sizeof(*sample));
Arjan van de Ven102749892009-09-12 07:53:05 +0200247 assert(sample != NULL);
Arjan van de Ven102749892009-09-12 07:53:05 +0200248 sample->start_time = start;
249 sample->end_time = end;
250 sample->type = type;
251 sample->next = c->samples;
252 sample->cpu = cpu;
253 c->samples = sample;
254
255 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
256 c->total_time += (end-start);
257 p->total_time += (end-start);
258 }
259
260 if (c->start_time == 0 || c->start_time > start)
261 c->start_time = start;
262 if (p->start_time == 0 || p->start_time > start)
263 p->start_time = start;
Arjan van de Ven102749892009-09-12 07:53:05 +0200264}
265
266#define MAX_CPUS 4096
267
268static u64 cpus_cstate_start_times[MAX_CPUS];
269static int cpus_cstate_state[MAX_CPUS];
270static u64 cpus_pstate_start_times[MAX_CPUS];
271static u64 cpus_pstate_state[MAX_CPUS];
272
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300273static int process_comm_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200274 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300275 struct perf_sample *sample __maybe_unused,
276 struct machine *machine __maybe_unused)
Arjan van de Ven102749892009-09-12 07:53:05 +0200277{
Arjan van de Ven8f06d7e2010-01-16 12:53:19 -0800278 pid_set_comm(event->comm.tid, event->comm.comm);
Arjan van de Ven102749892009-09-12 07:53:05 +0200279 return 0;
280}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200281
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300282static int process_fork_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200283 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300284 struct perf_sample *sample __maybe_unused,
285 struct machine *machine __maybe_unused)
Arjan van de Ven102749892009-09-12 07:53:05 +0200286{
287 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
288 return 0;
289}
290
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300291static int process_exit_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200292 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300293 struct perf_sample *sample __maybe_unused,
294 struct machine *machine __maybe_unused)
Arjan van de Ven102749892009-09-12 07:53:05 +0200295{
296 pid_exit(event->fork.pid, event->fork.time);
297 return 0;
298}
299
300struct trace_entry {
Arjan van de Ven102749892009-09-12 07:53:05 +0200301 unsigned short type;
302 unsigned char flags;
303 unsigned char preempt_count;
304 int pid;
OGAWA Hirofumi028c5152009-12-06 20:07:29 +0900305 int lock_depth;
Arjan van de Ven102749892009-09-12 07:53:05 +0200306};
307
Thomas Renninger20c457b2011-01-03 17:50:45 +0100308#ifdef SUPPORT_OLD_POWER_EVENTS
309static int use_old_power_events;
310struct power_entry_old {
Arjan van de Ven102749892009-09-12 07:53:05 +0200311 struct trace_entry te;
Thomas Renninger4c21adf2010-07-20 16:59:34 -0700312 u64 type;
313 u64 value;
314 u64 cpu_id;
Arjan van de Ven102749892009-09-12 07:53:05 +0200315};
Thomas Renninger20c457b2011-01-03 17:50:45 +0100316#endif
317
318struct power_processor_entry {
319 struct trace_entry te;
320 u32 state;
321 u32 cpu_id;
322};
Arjan van de Ven102749892009-09-12 07:53:05 +0200323
324#define TASK_COMM_LEN 16
325struct wakeup_entry {
326 struct trace_entry te;
327 char comm[TASK_COMM_LEN];
328 int pid;
329 int prio;
330 int success;
331};
332
Arjan van de Ven102749892009-09-12 07:53:05 +0200333struct sched_switch {
334 struct trace_entry te;
335 char prev_comm[TASK_COMM_LEN];
336 int prev_pid;
337 int prev_prio;
338 long prev_state; /* Arjan weeps. */
339 char next_comm[TASK_COMM_LEN];
340 int next_pid;
341 int next_prio;
342};
343
344static void c_state_start(int cpu, u64 timestamp, int state)
345{
346 cpus_cstate_start_times[cpu] = timestamp;
347 cpus_cstate_state[cpu] = state;
348}
349
350static void c_state_end(int cpu, u64 timestamp)
351{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300352 struct power_event *pwr = zalloc(sizeof(*pwr));
353
Arjan van de Ven102749892009-09-12 07:53:05 +0200354 if (!pwr)
355 return;
Arjan van de Ven102749892009-09-12 07:53:05 +0200356
357 pwr->state = cpus_cstate_state[cpu];
358 pwr->start_time = cpus_cstate_start_times[cpu];
359 pwr->end_time = timestamp;
360 pwr->cpu = cpu;
361 pwr->type = CSTATE;
362 pwr->next = power_events;
363
364 power_events = pwr;
365}
366
367static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
368{
369 struct power_event *pwr;
Arjan van de Ven102749892009-09-12 07:53:05 +0200370
371 if (new_freq > 8000000) /* detect invalid data */
372 return;
373
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300374 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven102749892009-09-12 07:53:05 +0200375 if (!pwr)
376 return;
Arjan van de Ven102749892009-09-12 07:53:05 +0200377
378 pwr->state = cpus_pstate_state[cpu];
379 pwr->start_time = cpus_pstate_start_times[cpu];
380 pwr->end_time = timestamp;
381 pwr->cpu = cpu;
382 pwr->type = PSTATE;
383 pwr->next = power_events;
384
385 if (!pwr->start_time)
386 pwr->start_time = first_time;
387
388 power_events = pwr;
389
390 cpus_pstate_state[cpu] = new_freq;
391 cpus_pstate_start_times[cpu] = timestamp;
392
393 if ((u64)new_freq > max_freq)
394 max_freq = new_freq;
395
396 if (new_freq < min_freq || min_freq == 0)
397 min_freq = new_freq;
398
399 if (new_freq == max_freq - 1000)
400 turbo_frequency = max_freq;
401}
402
403static void
404sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
405{
Arjan van de Ven102749892009-09-12 07:53:05 +0200406 struct per_pid *p;
407 struct wakeup_entry *wake = (void *)te;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300408 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven102749892009-09-12 07:53:05 +0200409
Arjan van de Ven102749892009-09-12 07:53:05 +0200410 if (!we)
411 return;
412
Arjan van de Ven102749892009-09-12 07:53:05 +0200413 we->time = timestamp;
414 we->waker = pid;
415
416 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
417 we->waker = -1;
418
419 we->wakee = wake->pid;
420 we->next = wake_events;
421 wake_events = we;
422 p = find_create_pid(we->wakee);
423
424 if (p && p->current && p->current->state == TYPE_NONE) {
425 p->current->state_since = timestamp;
426 p->current->state = TYPE_WAITING;
427 }
428 if (p && p->current && p->current->state == TYPE_BLOCKED) {
429 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
430 p->current->state_since = timestamp;
431 p->current->state = TYPE_WAITING;
432 }
433}
434
435static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
436{
437 struct per_pid *p = NULL, *prev_p;
438 struct sched_switch *sw = (void *)te;
439
440
441 prev_p = find_create_pid(sw->prev_pid);
442
443 p = find_create_pid(sw->next_pid);
444
445 if (prev_p->current && prev_p->current->state != TYPE_NONE)
446 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
447 if (p && p->current) {
448 if (p->current->state != TYPE_NONE)
449 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
450
Julia Lawall33e26a12010-08-05 22:27:51 +0200451 p->current->state_since = timestamp;
452 p->current->state = TYPE_RUNNING;
Arjan van de Ven102749892009-09-12 07:53:05 +0200453 }
454
455 if (prev_p->current) {
456 prev_p->current->state = TYPE_NONE;
457 prev_p->current->state_since = timestamp;
458 if (sw->prev_state & 2)
459 prev_p->current->state = TYPE_BLOCKED;
460 if (sw->prev_state == 0)
461 prev_p->current->state = TYPE_WAITING;
462 }
463}
464
465
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300466static int process_sample_event(struct perf_tool *tool __maybe_unused,
467 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200468 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200469 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300470 struct machine *machine __maybe_unused)
Arjan van de Ven102749892009-09-12 07:53:05 +0200471{
Arjan van de Ven102749892009-09-12 07:53:05 +0200472 struct trace_entry *te;
473
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200474 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200475 if (!first_time || first_time > sample->time)
476 first_time = sample->time;
477 if (last_time < sample->time)
478 last_time = sample->time;
Arjan van de Ven102749892009-09-12 07:53:05 +0200479 }
Arjan van de Ven102749892009-09-12 07:53:05 +0200480
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200481 te = (void *)sample->raw_data;
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200482 if ((evsel->attr.sample_type & PERF_SAMPLE_RAW) && sample->raw_size > 0) {
Arjan van de Ven102749892009-09-12 07:53:05 +0200483 char *event_str;
Thomas Renninger20c457b2011-01-03 17:50:45 +0100484#ifdef SUPPORT_OLD_POWER_EVENTS
485 struct power_entry_old *peo;
486 peo = (void *)te;
487#endif
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300488 /*
489 * FIXME: use evsel, its already mapped from id to perf_evsel,
490 * remove perf_header__find_event infrastructure bits.
491 * Mapping all these "power:cpu_idle" strings to the tracepoint
492 * ID and then just comparing against evsel->attr.config.
493 *
494 * e.g.:
495 *
496 * if (evsel->attr.config == power_cpu_idle_id)
497 */
Arjan van de Ven102749892009-09-12 07:53:05 +0200498 event_str = perf_header__find_event(te->type);
499
500 if (!event_str)
501 return 0;
502
Thomas Renninger54b08f52011-02-27 22:36:46 +0100503 if (sample->cpu > numcpus)
504 numcpus = sample->cpu;
505
Thomas Renninger20c457b2011-01-03 17:50:45 +0100506 if (strcmp(event_str, "power:cpu_idle") == 0) {
507 struct power_processor_entry *ppe = (void *)te;
508 if (ppe->state == (u32)PWR_EVENT_EXIT)
509 c_state_end(ppe->cpu_id, sample->time);
510 else
511 c_state_start(ppe->cpu_id, sample->time,
512 ppe->state);
513 }
514 else if (strcmp(event_str, "power:cpu_frequency") == 0) {
515 struct power_processor_entry *ppe = (void *)te;
516 p_state_change(ppe->cpu_id, sample->time, ppe->state);
517 }
Arjan van de Ven102749892009-09-12 07:53:05 +0200518
Thomas Renninger20c457b2011-01-03 17:50:45 +0100519 else if (strcmp(event_str, "sched:sched_wakeup") == 0)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200520 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
Arjan van de Ven102749892009-09-12 07:53:05 +0200521
Thomas Renninger20c457b2011-01-03 17:50:45 +0100522 else if (strcmp(event_str, "sched:sched_switch") == 0)
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200523 sched_switch(sample->cpu, sample->time, te);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100524
525#ifdef SUPPORT_OLD_POWER_EVENTS
526 if (use_old_power_events) {
527 if (strcmp(event_str, "power:power_start") == 0)
528 c_state_start(peo->cpu_id, sample->time,
529 peo->value);
530
531 else if (strcmp(event_str, "power:power_end") == 0)
532 c_state_end(sample->cpu, sample->time);
533
534 else if (strcmp(event_str,
535 "power:power_frequency") == 0)
536 p_state_change(peo->cpu_id, sample->time,
537 peo->value);
538 }
539#endif
Arjan van de Ven102749892009-09-12 07:53:05 +0200540 }
541 return 0;
542}
543
544/*
545 * After the last sample we need to wrap up the current C/P state
546 * and close out each CPU for these.
547 */
548static void end_sample_processing(void)
549{
550 u64 cpu;
551 struct power_event *pwr;
552
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200553 for (cpu = 0; cpu <= numcpus; cpu++) {
Arjan van de Ven102749892009-09-12 07:53:05 +0200554 /* C state */
555#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300556 pwr = zalloc(sizeof(*pwr));
557 if (!pwr)
558 return;
559
Arjan van de Ven102749892009-09-12 07:53:05 +0200560 pwr->state = cpus_cstate_state[cpu];
561 pwr->start_time = cpus_cstate_start_times[cpu];
562 pwr->end_time = last_time;
563 pwr->cpu = cpu;
564 pwr->type = CSTATE;
565 pwr->next = power_events;
566
567 power_events = pwr;
568#endif
569 /* P state */
570
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300571 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven102749892009-09-12 07:53:05 +0200572 if (!pwr)
573 return;
Arjan van de Ven102749892009-09-12 07:53:05 +0200574
575 pwr->state = cpus_pstate_state[cpu];
576 pwr->start_time = cpus_pstate_start_times[cpu];
577 pwr->end_time = last_time;
578 pwr->cpu = cpu;
579 pwr->type = PSTATE;
580 pwr->next = power_events;
581
582 if (!pwr->start_time)
583 pwr->start_time = first_time;
584 if (!pwr->state)
585 pwr->state = min_freq;
586 power_events = pwr;
587 }
588}
589
Arjan van de Ven102749892009-09-12 07:53:05 +0200590/*
591 * Sort the pid datastructure
592 */
593static void sort_pids(void)
594{
595 struct per_pid *new_list, *p, *cursor, *prev;
596 /* sort by ppid first, then by pid, lowest to highest */
597
598 new_list = NULL;
599
600 while (all_data) {
601 p = all_data;
602 all_data = p->next;
603 p->next = NULL;
604
605 if (new_list == NULL) {
606 new_list = p;
607 p->next = NULL;
608 continue;
609 }
610 prev = NULL;
611 cursor = new_list;
612 while (cursor) {
613 if (cursor->ppid > p->ppid ||
614 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
615 /* must insert before */
616 if (prev) {
617 p->next = prev->next;
618 prev->next = p;
619 cursor = NULL;
620 continue;
621 } else {
622 p->next = new_list;
623 new_list = p;
624 cursor = NULL;
625 continue;
626 }
627 }
628
629 prev = cursor;
630 cursor = cursor->next;
631 if (!cursor)
632 prev->next = p;
633 }
634 }
635 all_data = new_list;
636}
637
638
639static void draw_c_p_states(void)
640{
641 struct power_event *pwr;
642 pwr = power_events;
643
644 /*
645 * two pass drawing so that the P state bars are on top of the C state blocks
646 */
647 while (pwr) {
648 if (pwr->type == CSTATE)
649 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
650 pwr = pwr->next;
651 }
652
653 pwr = power_events;
654 while (pwr) {
655 if (pwr->type == PSTATE) {
656 if (!pwr->state)
657 pwr->state = min_freq;
658 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
659 }
660 pwr = pwr->next;
661 }
662}
663
664static void draw_wakeups(void)
665{
666 struct wake_event *we;
667 struct per_pid *p;
668 struct per_pidcomm *c;
669
670 we = wake_events;
671 while (we) {
672 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200673 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven102749892009-09-12 07:53:05 +0200674
675 /* locate the column of the waker and wakee */
676 p = all_data;
677 while (p) {
678 if (p->pid == we->waker || p->pid == we->wakee) {
679 c = p->all;
680 while (c) {
681 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900682 if (p->pid == we->waker && !from) {
Arjan van de Ven102749892009-09-12 07:53:05 +0200683 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900684 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200685 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900686 if (p->pid == we->wakee && !to) {
Arjan van de Ven102749892009-09-12 07:53:05 +0200687 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900688 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200689 }
Arjan van de Ven102749892009-09-12 07:53:05 +0200690 }
691 c = c->next;
692 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900693 c = p->all;
694 while (c) {
695 if (p->pid == we->waker && !from) {
696 from = c->Y;
697 task_from = strdup(c->comm);
698 }
699 if (p->pid == we->wakee && !to) {
700 to = c->Y;
701 task_to = strdup(c->comm);
702 }
703 c = c->next;
704 }
Arjan van de Ven102749892009-09-12 07:53:05 +0200705 }
706 p = p->next;
707 }
708
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900709 if (!task_from) {
710 task_from = malloc(40);
711 sprintf(task_from, "[%i]", we->waker);
712 }
713 if (!task_to) {
714 task_to = malloc(40);
715 sprintf(task_to, "[%i]", we->wakee);
716 }
717
Arjan van de Ven102749892009-09-12 07:53:05 +0200718 if (we->waker == -1)
719 svg_interrupt(we->time, to);
720 else if (from && to && abs(from - to) == 1)
721 svg_wakeline(we->time, from, to);
722 else
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200723 svg_partial_wakeline(we->time, from, task_from, to, task_to);
Arjan van de Ven102749892009-09-12 07:53:05 +0200724 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900725
726 free(task_from);
727 free(task_to);
Arjan van de Ven102749892009-09-12 07:53:05 +0200728 }
729}
730
731static void draw_cpu_usage(void)
732{
733 struct per_pid *p;
734 struct per_pidcomm *c;
735 struct cpu_sample *sample;
736 p = all_data;
737 while (p) {
738 c = p->all;
739 while (c) {
740 sample = c->samples;
741 while (sample) {
742 if (sample->type == TYPE_RUNNING)
743 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
744
745 sample = sample->next;
746 }
747 c = c->next;
748 }
749 p = p->next;
750 }
751}
752
753static void draw_process_bars(void)
754{
755 struct per_pid *p;
756 struct per_pidcomm *c;
757 struct cpu_sample *sample;
758 int Y = 0;
759
760 Y = 2 * numcpus + 2;
761
762 p = all_data;
763 while (p) {
764 c = p->all;
765 while (c) {
766 if (!c->display) {
767 c->Y = 0;
768 c = c->next;
769 continue;
770 }
771
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200772 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven102749892009-09-12 07:53:05 +0200773 sample = c->samples;
774 while (sample) {
775 if (sample->type == TYPE_RUNNING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200776 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
Arjan van de Ven102749892009-09-12 07:53:05 +0200777 if (sample->type == TYPE_BLOCKED)
778 svg_box(Y, sample->start_time, sample->end_time, "blocked");
779 if (sample->type == TYPE_WAITING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200780 svg_waiting(Y, sample->start_time, sample->end_time);
Arjan van de Ven102749892009-09-12 07:53:05 +0200781 sample = sample->next;
782 }
783
784 if (c->comm) {
785 char comm[256];
786 if (c->total_time > 5000000000) /* 5 seconds */
787 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
788 else
789 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
790
791 svg_text(Y, c->start_time, comm);
792 }
793 c->Y = Y;
794 Y++;
795 c = c->next;
796 }
797 p = p->next;
798 }
799}
800
Arjan van de Venbbe29872009-10-20 07:09:39 +0900801static void add_process_filter(const char *string)
802{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300803 int pid = strtoull(string, NULL, 10);
804 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +0900805
Arjan van de Venbbe29872009-10-20 07:09:39 +0900806 if (!filt)
807 return;
808
809 filt->name = strdup(string);
810 filt->pid = pid;
811 filt->next = process_filter;
812
813 process_filter = filt;
814}
815
816static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
817{
818 struct process_filter *filt;
819 if (!process_filter)
820 return 1;
821
822 filt = process_filter;
823 while (filt) {
824 if (filt->pid && p->pid == filt->pid)
825 return 1;
826 if (strcmp(filt->name, c->comm) == 0)
827 return 1;
828 filt = filt->next;
829 }
830 return 0;
831}
832
833static int determine_display_tasks_filtered(void)
834{
835 struct per_pid *p;
836 struct per_pidcomm *c;
837 int count = 0;
838
839 p = all_data;
840 while (p) {
841 p->display = 0;
842 if (p->start_time == 1)
843 p->start_time = first_time;
844
845 /* no exit marker, task kept running to the end */
846 if (p->end_time == 0)
847 p->end_time = last_time;
848
849 c = p->all;
850
851 while (c) {
852 c->display = 0;
853
854 if (c->start_time == 1)
855 c->start_time = first_time;
856
857 if (passes_filter(p, c)) {
858 c->display = 1;
859 p->display = 1;
860 count++;
861 }
862
863 if (c->end_time == 0)
864 c->end_time = last_time;
865
866 c = c->next;
867 }
868 p = p->next;
869 }
870 return count;
871}
872
Arjan van de Ven102749892009-09-12 07:53:05 +0200873static int determine_display_tasks(u64 threshold)
874{
875 struct per_pid *p;
876 struct per_pidcomm *c;
877 int count = 0;
878
Arjan van de Venbbe29872009-10-20 07:09:39 +0900879 if (process_filter)
880 return determine_display_tasks_filtered();
881
Arjan van de Ven102749892009-09-12 07:53:05 +0200882 p = all_data;
883 while (p) {
884 p->display = 0;
885 if (p->start_time == 1)
886 p->start_time = first_time;
887
888 /* no exit marker, task kept running to the end */
889 if (p->end_time == 0)
890 p->end_time = last_time;
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200891 if (p->total_time >= threshold && !power_only)
Arjan van de Ven102749892009-09-12 07:53:05 +0200892 p->display = 1;
893
894 c = p->all;
895
896 while (c) {
897 c->display = 0;
898
899 if (c->start_time == 1)
900 c->start_time = first_time;
901
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200902 if (c->total_time >= threshold && !power_only) {
Arjan van de Ven102749892009-09-12 07:53:05 +0200903 c->display = 1;
904 count++;
905 }
906
907 if (c->end_time == 0)
908 c->end_time = last_time;
909
910 c = c->next;
911 }
912 p = p->next;
913 }
914 return count;
915}
916
917
918
919#define TIME_THRESH 10000000
920
921static void write_svg_file(const char *filename)
922{
923 u64 i;
924 int count;
925
926 numcpus++;
927
928
929 count = determine_display_tasks(TIME_THRESH);
930
931 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
932 if (count < 15)
933 count = determine_display_tasks(TIME_THRESH / 10);
934
Arjan van de Ven5094b652009-09-20 18:14:16 +0200935 open_svg(filename, numcpus, count, first_time, last_time);
Arjan van de Ven102749892009-09-12 07:53:05 +0200936
Arjan van de Ven5094b652009-09-20 18:14:16 +0200937 svg_time_grid();
Arjan van de Ven102749892009-09-12 07:53:05 +0200938 svg_legenda();
939
940 for (i = 0; i < numcpus; i++)
941 svg_cpu_box(i, max_freq, turbo_frequency);
942
943 draw_cpu_usage();
944 draw_process_bars();
945 draw_c_p_states();
946 draw_wakeups();
947
948 svg_close();
949}
950
Feng Tang70cb4e92012-10-30 11:56:02 +0800951static int __cmd_timechart(const char *output_name)
Arjan van de Ven102749892009-09-12 07:53:05 +0200952{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -0300953 struct perf_tool perf_timechart = {
954 .comm = process_comm_event,
955 .fork = process_fork_event,
956 .exit = process_exit_event,
957 .sample = process_sample_event,
958 .ordered_samples = true,
959 };
Ian Munsie21ef97f2010-12-10 14:09:16 +1100960 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200961 0, false, &perf_timechart);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200962 int ret = -EINVAL;
Arjan van de Ven102749892009-09-12 07:53:05 +0200963
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200964 if (session == NULL)
965 return -ENOMEM;
966
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200967 if (!perf_session__has_traces(session, "timechart record"))
968 goto out_delete;
969
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200970 ret = perf_session__process_events(session, &perf_timechart);
Li Zefan5cbd0802009-12-01 14:05:16 +0800971 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200972 goto out_delete;
Arjan van de Ven102749892009-09-12 07:53:05 +0200973
Arjan van de Ven102749892009-09-12 07:53:05 +0200974 end_sample_processing();
975
976 sort_pids();
977
978 write_svg_file(output_name);
979
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200980 pr_info("Written %2.1f seconds of trace to %s.\n",
981 (last_time - first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200982out_delete:
983 perf_session__delete(session);
984 return ret;
Arjan van de Ven102749892009-09-12 07:53:05 +0200985}
986
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +0200987static int __cmd_record(int argc, const char **argv)
988{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -0300989#ifdef SUPPORT_OLD_POWER_EVENTS
990 const char * const record_old_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200991 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -0300992 "-e", "power:power_start",
993 "-e", "power:power_end",
994 "-e", "power:power_frequency",
995 "-e", "sched:sched_wakeup",
996 "-e", "sched:sched_switch",
997 };
998#endif
999 const char * const record_new_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001000 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001001 "-e", "power:cpu_frequency",
1002 "-e", "power:cpu_idle",
1003 "-e", "sched:sched_wakeup",
1004 "-e", "sched:sched_switch",
1005 };
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001006 unsigned int rec_argc, i, j;
1007 const char **rec_argv;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001008 const char * const *record_args = record_new_args;
1009 unsigned int record_elems = ARRAY_SIZE(record_new_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001010
Thomas Renninger20c457b2011-01-03 17:50:45 +01001011#ifdef SUPPORT_OLD_POWER_EVENTS
1012 if (!is_valid_tracepoint("power:cpu_idle") &&
1013 is_valid_tracepoint("power:power_start")) {
1014 use_old_power_events = 1;
1015 record_args = record_old_args;
1016 record_elems = ARRAY_SIZE(record_old_args);
1017 }
1018#endif
1019
1020 rec_argc = record_elems + argc - 1;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001021 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1022
Chris Samuelce47dc52010-11-13 13:35:06 +11001023 if (rec_argv == NULL)
1024 return -ENOMEM;
1025
Thomas Renninger20c457b2011-01-03 17:50:45 +01001026 for (i = 0; i < record_elems; i++)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001027 rec_argv[i] = strdup(record_args[i]);
1028
1029 for (j = 1; j < (unsigned int)argc; j++, i++)
1030 rec_argv[i] = argv[j];
1031
1032 return cmd_record(i, rec_argv, NULL);
1033}
1034
Arjan van de Venbbe29872009-10-20 07:09:39 +09001035static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001036parse_process(const struct option *opt __maybe_unused, const char *arg,
1037 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001038{
1039 if (arg)
1040 add_process_filter(arg);
1041 return 0;
1042}
1043
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001044int cmd_timechart(int argc, const char **argv,
1045 const char *prefix __maybe_unused)
1046{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001047 const char *output_name = "output.svg";
1048 const struct option options[] = {
1049 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1050 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1051 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1052 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001053 OPT_CALLBACK('p', "process", NULL, "process",
1054 "process selector. Pass a pid or process name.",
1055 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001056 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1057 "Look for files with symbols relative to this directory"),
Arjan van de Ven102749892009-09-12 07:53:05 +02001058 OPT_END()
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001059 };
1060 const char * const timechart_usage[] = {
1061 "perf timechart [<options>] {record}",
1062 NULL
1063 };
Arjan van de Ven102749892009-09-12 07:53:05 +02001064
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001065 argc = parse_options(argc, argv, options, timechart_usage,
1066 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven102749892009-09-12 07:53:05 +02001067
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001068 symbol__init();
1069
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001070 if (argc && !strncmp(argv[0], "rec", 3))
1071 return __cmd_record(argc, argv);
1072 else if (argc)
1073 usage_with_options(timechart_usage, options);
Arjan van de Ven102749892009-09-12 07:53:05 +02001074
1075 setup_pager();
1076
Feng Tang70cb4e92012-10-30 11:56:02 +08001077 return __cmd_timechart(output_name);
Arjan van de Ven102749892009-09-12 07:53:05 +02001078}