blob: e2d62f1a96e4ddd56a225f14e07193b27bba5db3 [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
Jiri Olsac85cffa2013-07-11 17:28:29 +020015#include <traceevent/event-parse.h>
16
Arjan van de Ven10274982009-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"
Jiri Olsa59366782013-07-11 17:28:30 +020024#include "util/evlist.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -020025#include "util/evsel.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020026#include <linux/rbtree.h>
27#include "util/symbol.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020028#include "util/callchain.h"
29#include "util/strlist.h"
30
31#include "perf.h"
32#include "util/header.h"
33#include "util/parse-options.h"
34#include "util/parse-events.h"
Li Zefan5cbd0802009-12-01 14:05:16 +080035#include "util/event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020036#include "util/session.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020037#include "util/svghelper.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020038#include "util/tool.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020039#include "util/data.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020040
Thomas Renninger20c457b2011-01-03 17:50:45 +010041#define SUPPORT_OLD_POWER_EVENTS 1
42#define PWR_EVENT_EXIT -1
43
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030044struct timechart {
45 struct perf_tool tool;
46 int proc_num;
47 unsigned int numcpus;
48 u64 min_freq, /* Lowest CPU frequency seen */
49 max_freq, /* Highest CPU frequency seen */
50 turbo_frequency,
51 first_time, last_time;
52 bool power_only,
53 tasks_only,
54 with_backtrace;
55};
Arjan van de Ven10274982009-09-12 07:53:05 +020056
Arjan van de Ven10274982009-09-12 07:53:05 +020057struct per_pidcomm;
Arjan van de Ven10274982009-09-12 07:53:05 +020058struct cpu_sample;
Arjan van de Ven10274982009-09-12 07:53:05 +020059
60/*
61 * Datastructure layout:
62 * We keep an list of "pid"s, matching the kernels notion of a task struct.
63 * Each "pid" entry, has a list of "comm"s.
64 * this is because we want to track different programs different, while
65 * exec will reuse the original pid (by design).
66 * Each comm has a list of samples that will be used to draw
67 * final graph.
68 */
69
70struct per_pid {
71 struct per_pid *next;
72
73 int pid;
74 int ppid;
75
76 u64 start_time;
77 u64 end_time;
78 u64 total_time;
79 int display;
80
81 struct per_pidcomm *all;
82 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +020083};
84
85
86struct per_pidcomm {
87 struct per_pidcomm *next;
88
89 u64 start_time;
90 u64 end_time;
91 u64 total_time;
92
93 int Y;
94 int display;
95
96 long state;
97 u64 state_since;
98
99 char *comm;
100
101 struct cpu_sample *samples;
102};
103
104struct sample_wrapper {
105 struct sample_wrapper *next;
106
107 u64 timestamp;
108 unsigned char data[0];
109};
110
111#define TYPE_NONE 0
112#define TYPE_RUNNING 1
113#define TYPE_WAITING 2
114#define TYPE_BLOCKED 3
115
116struct cpu_sample {
117 struct cpu_sample *next;
118
119 u64 start_time;
120 u64 end_time;
121 int type;
122 int cpu;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400123 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200124};
125
126static struct per_pid *all_data;
127
128#define CSTATE 1
129#define PSTATE 2
130
131struct power_event {
132 struct power_event *next;
133 int type;
134 int state;
135 u64 start_time;
136 u64 end_time;
137 int cpu;
138};
139
140struct wake_event {
141 struct wake_event *next;
142 int waker;
143 int wakee;
144 u64 time;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400145 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200146};
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 {
Li Zefan5cbd0802009-12-01 14:05:16 +0800152 char *name;
153 int pid;
154 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900155};
156
157static struct process_filter *process_filter;
158
159
Arjan van de Ven10274982009-09-12 07:53:05 +0200160static struct per_pid *find_create_pid(int pid)
161{
162 struct per_pid *cursor = all_data;
163
164 while (cursor) {
165 if (cursor->pid == pid)
166 return cursor;
167 cursor = cursor->next;
168 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300169 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200170 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200171 cursor->pid = pid;
172 cursor->next = all_data;
173 all_data = cursor;
174 return cursor;
175}
176
177static void pid_set_comm(int pid, char *comm)
178{
179 struct per_pid *p;
180 struct per_pidcomm *c;
181 p = find_create_pid(pid);
182 c = p->all;
183 while (c) {
184 if (c->comm && strcmp(c->comm, comm) == 0) {
185 p->current = c;
186 return;
187 }
188 if (!c->comm) {
189 c->comm = strdup(comm);
190 p->current = c;
191 return;
192 }
193 c = c->next;
194 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300195 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200196 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200197 c->comm = strdup(comm);
198 p->current = c;
199 c->next = p->all;
200 p->all = c;
201}
202
203static void pid_fork(int pid, int ppid, u64 timestamp)
204{
205 struct per_pid *p, *pp;
206 p = find_create_pid(pid);
207 pp = find_create_pid(ppid);
208 p->ppid = ppid;
209 if (pp->current && pp->current->comm && !p->current)
210 pid_set_comm(pid, pp->current->comm);
211
212 p->start_time = timestamp;
213 if (p->current) {
214 p->current->start_time = timestamp;
215 p->current->state_since = timestamp;
216 }
217}
218
219static void pid_exit(int pid, u64 timestamp)
220{
221 struct per_pid *p;
222 p = find_create_pid(pid);
223 p->end_time = timestamp;
224 if (p->current)
225 p->current->end_time = timestamp;
226}
227
228static void
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400229pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end,
230 const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200231{
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 Ven10274982009-09-12 07:53:05 +0200240 assert(c != NULL);
Arjan van de Ven10274982009-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 Ven10274982009-09-12 07:53:05 +0200247 assert(sample != NULL);
Arjan van de Ven10274982009-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;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400253 sample->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200254 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
Thomas Renninger20c457b2011-01-03 17:50:45 +0100301#ifdef SUPPORT_OLD_POWER_EVENTS
302static int use_old_power_events;
Thomas Renninger20c457b2011-01-03 17:50:45 +0100303#endif
304
Arjan van de Ven10274982009-09-12 07:53:05 +0200305static void c_state_start(int cpu, u64 timestamp, int state)
306{
307 cpus_cstate_start_times[cpu] = timestamp;
308 cpus_cstate_state[cpu] = state;
309}
310
311static void c_state_end(int cpu, u64 timestamp)
312{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300313 struct power_event *pwr = zalloc(sizeof(*pwr));
314
Arjan van de Ven10274982009-09-12 07:53:05 +0200315 if (!pwr)
316 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200317
318 pwr->state = cpus_cstate_state[cpu];
319 pwr->start_time = cpus_cstate_start_times[cpu];
320 pwr->end_time = timestamp;
321 pwr->cpu = cpu;
322 pwr->type = CSTATE;
323 pwr->next = power_events;
324
325 power_events = pwr;
326}
327
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300328static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
Arjan van de Ven10274982009-09-12 07:53:05 +0200329{
330 struct power_event *pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200331
332 if (new_freq > 8000000) /* detect invalid data */
333 return;
334
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300335 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200336 if (!pwr)
337 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200338
339 pwr->state = cpus_pstate_state[cpu];
340 pwr->start_time = cpus_pstate_start_times[cpu];
341 pwr->end_time = timestamp;
342 pwr->cpu = cpu;
343 pwr->type = PSTATE;
344 pwr->next = power_events;
345
346 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300347 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200348
349 power_events = pwr;
350
351 cpus_pstate_state[cpu] = new_freq;
352 cpus_pstate_start_times[cpu] = timestamp;
353
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300354 if ((u64)new_freq > tchart->max_freq)
355 tchart->max_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200356
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300357 if (new_freq < tchart->min_freq || tchart->min_freq == 0)
358 tchart->min_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200359
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300360 if (new_freq == tchart->max_freq - 1000)
361 tchart->turbo_frequency = tchart->max_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200362}
363
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400364static void sched_wakeup(int cpu, u64 timestamp, int waker, int wakee,
365 u8 flags, const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200366{
Arjan van de Ven10274982009-09-12 07:53:05 +0200367 struct per_pid *p;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300368 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200369
Arjan van de Ven10274982009-09-12 07:53:05 +0200370 if (!we)
371 return;
372
Arjan van de Ven10274982009-09-12 07:53:05 +0200373 we->time = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400374 we->waker = waker;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400375 we->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200376
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400377 if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
Arjan van de Ven10274982009-09-12 07:53:05 +0200378 we->waker = -1;
379
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400380 we->wakee = wakee;
Arjan van de Ven10274982009-09-12 07:53:05 +0200381 we->next = wake_events;
382 wake_events = we;
383 p = find_create_pid(we->wakee);
384
385 if (p && p->current && p->current->state == TYPE_NONE) {
386 p->current->state_since = timestamp;
387 p->current->state = TYPE_WAITING;
388 }
389 if (p && p->current && p->current->state == TYPE_BLOCKED) {
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400390 pid_put_sample(p->pid, p->current->state, cpu,
391 p->current->state_since, timestamp, NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200392 p->current->state_since = timestamp;
393 p->current->state = TYPE_WAITING;
394 }
395}
396
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400397static void sched_switch(int cpu, u64 timestamp, int prev_pid, int next_pid,
398 u64 prev_state, const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200399{
400 struct per_pid *p = NULL, *prev_p;
Arjan van de Ven10274982009-09-12 07:53:05 +0200401
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400402 prev_p = find_create_pid(prev_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200403
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400404 p = find_create_pid(next_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200405
406 if (prev_p->current && prev_p->current->state != TYPE_NONE)
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400407 pid_put_sample(prev_pid, TYPE_RUNNING, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400408 prev_p->current->state_since, timestamp,
409 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200410 if (p && p->current) {
411 if (p->current->state != TYPE_NONE)
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400412 pid_put_sample(next_pid, p->current->state, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400413 p->current->state_since, timestamp,
414 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200415
Julia Lawall33e26a12010-08-05 22:27:51 +0200416 p->current->state_since = timestamp;
417 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200418 }
419
420 if (prev_p->current) {
421 prev_p->current->state = TYPE_NONE;
422 prev_p->current->state_since = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400423 if (prev_state & 2)
Arjan van de Ven10274982009-09-12 07:53:05 +0200424 prev_p->current->state = TYPE_BLOCKED;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400425 if (prev_state == 0)
Arjan van de Ven10274982009-09-12 07:53:05 +0200426 prev_p->current->state = TYPE_WAITING;
427 }
428}
429
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400430static const char *cat_backtrace(union perf_event *event,
431 struct perf_sample *sample,
432 struct machine *machine)
433{
434 struct addr_location al;
435 unsigned int i;
436 char *p = NULL;
437 size_t p_len;
438 u8 cpumode = PERF_RECORD_MISC_USER;
439 struct addr_location tal;
440 struct ip_callchain *chain = sample->callchain;
441 FILE *f = open_memstream(&p, &p_len);
442
443 if (!f) {
444 perror("open_memstream error");
445 return NULL;
446 }
447
448 if (!chain)
449 goto exit;
450
451 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
452 fprintf(stderr, "problem processing %d event, skipping it.\n",
453 event->header.type);
454 goto exit;
455 }
456
457 for (i = 0; i < chain->nr; i++) {
458 u64 ip;
459
460 if (callchain_param.order == ORDER_CALLEE)
461 ip = chain->ips[i];
462 else
463 ip = chain->ips[chain->nr - i - 1];
464
465 if (ip >= PERF_CONTEXT_MAX) {
466 switch (ip) {
467 case PERF_CONTEXT_HV:
468 cpumode = PERF_RECORD_MISC_HYPERVISOR;
469 break;
470 case PERF_CONTEXT_KERNEL:
471 cpumode = PERF_RECORD_MISC_KERNEL;
472 break;
473 case PERF_CONTEXT_USER:
474 cpumode = PERF_RECORD_MISC_USER;
475 break;
476 default:
477 pr_debug("invalid callchain context: "
478 "%"PRId64"\n", (s64) ip);
479
480 /*
481 * It seems the callchain is corrupted.
482 * Discard all.
483 */
484 free(p);
485 p = NULL;
486 goto exit;
487 }
488 continue;
489 }
490
491 tal.filtered = false;
492 thread__find_addr_location(al.thread, machine, cpumode,
493 MAP__FUNCTION, ip, &tal);
494
495 if (tal.sym)
496 fprintf(f, "..... %016" PRIx64 " %s\n", ip,
497 tal.sym->name);
498 else
499 fprintf(f, "..... %016" PRIx64 "\n", ip);
500 }
501
502exit:
503 fclose(f);
504
505 return p;
506}
507
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300508typedef int (*tracepoint_handler)(struct timechart *tchart,
509 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400510 struct perf_sample *sample,
511 const char *backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200512
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300513static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo972ec652013-11-27 16:32:56 -0300514 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200515 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200516 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300517 struct machine *machine)
Arjan van de Ven10274982009-09-12 07:53:05 +0200518{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300519 struct timechart *tchart = container_of(tool, struct timechart, tool);
520
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200521 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300522 if (!tchart->first_time || tchart->first_time > sample->time)
523 tchart->first_time = sample->time;
524 if (tchart->last_time < sample->time)
525 tchart->last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200526 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200527
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300528 if (sample->cpu > tchart->numcpus)
529 tchart->numcpus = sample->cpu;
Arjan van de Ven10274982009-09-12 07:53:05 +0200530
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300531 if (evsel->handler != NULL) {
532 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300533 return f(tchart, evsel, sample, cat_backtrace(event, sample, machine));
Arjan van de Ven10274982009-09-12 07:53:05 +0200534 }
Jiri Olsa59366782013-07-11 17:28:30 +0200535
Arjan van de Ven10274982009-09-12 07:53:05 +0200536 return 0;
537}
538
Jiri Olsa59366782013-07-11 17:28:30 +0200539static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300540process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
541 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400542 struct perf_sample *sample,
543 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200544{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400545 u32 state = perf_evsel__intval(evsel, sample, "state");
546 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200547
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400548 if (state == (u32)PWR_EVENT_EXIT)
549 c_state_end(cpu_id, sample->time);
Jiri Olsa59366782013-07-11 17:28:30 +0200550 else
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400551 c_state_start(cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200552 return 0;
553}
554
555static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300556process_sample_cpu_frequency(struct timechart *tchart,
557 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400558 struct perf_sample *sample,
559 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200560{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400561 u32 state = perf_evsel__intval(evsel, sample, "state");
562 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200563
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300564 p_state_change(tchart, cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200565 return 0;
566}
567
568static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300569process_sample_sched_wakeup(struct timechart *tchart __maybe_unused,
570 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400571 struct perf_sample *sample,
572 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200573{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400574 u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
575 int waker = perf_evsel__intval(evsel, sample, "common_pid");
576 int wakee = perf_evsel__intval(evsel, sample, "pid");
Jiri Olsa59366782013-07-11 17:28:30 +0200577
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400578 sched_wakeup(sample->cpu, sample->time, waker, wakee, flags, backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200579 return 0;
580}
581
582static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300583process_sample_sched_switch(struct timechart *tchart __maybe_unused,
584 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400585 struct perf_sample *sample,
586 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200587{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400588 int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
589 int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
590 u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Jiri Olsa59366782013-07-11 17:28:30 +0200591
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400592 sched_switch(sample->cpu, sample->time, prev_pid, next_pid, prev_state,
593 backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200594 return 0;
595}
596
597#ifdef SUPPORT_OLD_POWER_EVENTS
598static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300599process_sample_power_start(struct timechart *tchart __maybe_unused,
600 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400601 struct perf_sample *sample,
602 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200603{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400604 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
605 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200606
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400607 c_state_start(cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200608 return 0;
609}
610
611static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300612process_sample_power_end(struct timechart *tchart __maybe_unused,
613 struct perf_evsel *evsel __maybe_unused,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400614 struct perf_sample *sample,
615 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200616{
617 c_state_end(sample->cpu, sample->time);
618 return 0;
619}
620
621static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300622process_sample_power_frequency(struct timechart *tchart,
623 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400624 struct perf_sample *sample,
625 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200626{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400627 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
628 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200629
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300630 p_state_change(tchart, cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200631 return 0;
632}
633#endif /* SUPPORT_OLD_POWER_EVENTS */
634
Arjan van de Ven10274982009-09-12 07:53:05 +0200635/*
636 * After the last sample we need to wrap up the current C/P state
637 * and close out each CPU for these.
638 */
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300639static void end_sample_processing(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200640{
641 u64 cpu;
642 struct power_event *pwr;
643
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300644 for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200645 /* C state */
646#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300647 pwr = zalloc(sizeof(*pwr));
648 if (!pwr)
649 return;
650
Arjan van de Ven10274982009-09-12 07:53:05 +0200651 pwr->state = cpus_cstate_state[cpu];
652 pwr->start_time = cpus_cstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300653 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200654 pwr->cpu = cpu;
655 pwr->type = CSTATE;
656 pwr->next = power_events;
657
658 power_events = pwr;
659#endif
660 /* P state */
661
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300662 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200663 if (!pwr)
664 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200665
666 pwr->state = cpus_pstate_state[cpu];
667 pwr->start_time = cpus_pstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300668 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200669 pwr->cpu = cpu;
670 pwr->type = PSTATE;
671 pwr->next = power_events;
672
673 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300674 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200675 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300676 pwr->state = tchart->min_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200677 power_events = pwr;
678 }
679}
680
Arjan van de Ven10274982009-09-12 07:53:05 +0200681/*
682 * Sort the pid datastructure
683 */
684static void sort_pids(void)
685{
686 struct per_pid *new_list, *p, *cursor, *prev;
687 /* sort by ppid first, then by pid, lowest to highest */
688
689 new_list = NULL;
690
691 while (all_data) {
692 p = all_data;
693 all_data = p->next;
694 p->next = NULL;
695
696 if (new_list == NULL) {
697 new_list = p;
698 p->next = NULL;
699 continue;
700 }
701 prev = NULL;
702 cursor = new_list;
703 while (cursor) {
704 if (cursor->ppid > p->ppid ||
705 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
706 /* must insert before */
707 if (prev) {
708 p->next = prev->next;
709 prev->next = p;
710 cursor = NULL;
711 continue;
712 } else {
713 p->next = new_list;
714 new_list = p;
715 cursor = NULL;
716 continue;
717 }
718 }
719
720 prev = cursor;
721 cursor = cursor->next;
722 if (!cursor)
723 prev->next = p;
724 }
725 }
726 all_data = new_list;
727}
728
729
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300730static void draw_c_p_states(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200731{
732 struct power_event *pwr;
733 pwr = power_events;
734
735 /*
736 * two pass drawing so that the P state bars are on top of the C state blocks
737 */
738 while (pwr) {
739 if (pwr->type == CSTATE)
740 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
741 pwr = pwr->next;
742 }
743
744 pwr = power_events;
745 while (pwr) {
746 if (pwr->type == PSTATE) {
747 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300748 pwr->state = tchart->min_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200749 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
750 }
751 pwr = pwr->next;
752 }
753}
754
755static void draw_wakeups(void)
756{
757 struct wake_event *we;
758 struct per_pid *p;
759 struct per_pidcomm *c;
760
761 we = wake_events;
762 while (we) {
763 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200764 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200765
766 /* locate the column of the waker and wakee */
767 p = all_data;
768 while (p) {
769 if (p->pid == we->waker || p->pid == we->wakee) {
770 c = p->all;
771 while (c) {
772 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900773 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200774 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900775 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200776 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900777 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200778 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900779 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200780 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200781 }
782 c = c->next;
783 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900784 c = p->all;
785 while (c) {
786 if (p->pid == we->waker && !from) {
787 from = c->Y;
788 task_from = strdup(c->comm);
789 }
790 if (p->pid == we->wakee && !to) {
791 to = c->Y;
792 task_to = strdup(c->comm);
793 }
794 c = c->next;
795 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200796 }
797 p = p->next;
798 }
799
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900800 if (!task_from) {
801 task_from = malloc(40);
802 sprintf(task_from, "[%i]", we->waker);
803 }
804 if (!task_to) {
805 task_to = malloc(40);
806 sprintf(task_to, "[%i]", we->wakee);
807 }
808
Arjan van de Ven10274982009-09-12 07:53:05 +0200809 if (we->waker == -1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400810 svg_interrupt(we->time, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200811 else if (from && to && abs(from - to) == 1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400812 svg_wakeline(we->time, from, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200813 else
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400814 svg_partial_wakeline(we->time, from, task_from, to,
815 task_to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200816 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900817
818 free(task_from);
819 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200820 }
821}
822
823static void draw_cpu_usage(void)
824{
825 struct per_pid *p;
826 struct per_pidcomm *c;
827 struct cpu_sample *sample;
828 p = all_data;
829 while (p) {
830 c = p->all;
831 while (c) {
832 sample = c->samples;
833 while (sample) {
834 if (sample->type == TYPE_RUNNING)
835 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
836
837 sample = sample->next;
838 }
839 c = c->next;
840 }
841 p = p->next;
842 }
843}
844
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300845static void draw_process_bars(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200846{
847 struct per_pid *p;
848 struct per_pidcomm *c;
849 struct cpu_sample *sample;
850 int Y = 0;
851
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300852 Y = 2 * tchart->numcpus + 2;
Arjan van de Ven10274982009-09-12 07:53:05 +0200853
854 p = all_data;
855 while (p) {
856 c = p->all;
857 while (c) {
858 if (!c->display) {
859 c->Y = 0;
860 c = c->next;
861 continue;
862 }
863
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200864 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +0200865 sample = c->samples;
866 while (sample) {
867 if (sample->type == TYPE_RUNNING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400868 svg_running(Y, sample->cpu,
869 sample->start_time,
870 sample->end_time,
871 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200872 if (sample->type == TYPE_BLOCKED)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400873 svg_blocked(Y, sample->cpu,
874 sample->start_time,
875 sample->end_time,
876 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200877 if (sample->type == TYPE_WAITING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400878 svg_waiting(Y, sample->cpu,
879 sample->start_time,
880 sample->end_time,
881 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200882 sample = sample->next;
883 }
884
885 if (c->comm) {
886 char comm[256];
887 if (c->total_time > 5000000000) /* 5 seconds */
888 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
889 else
890 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
891
892 svg_text(Y, c->start_time, comm);
893 }
894 c->Y = Y;
895 Y++;
896 c = c->next;
897 }
898 p = p->next;
899 }
900}
901
Arjan van de Venbbe29872009-10-20 07:09:39 +0900902static void add_process_filter(const char *string)
903{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300904 int pid = strtoull(string, NULL, 10);
905 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +0900906
Arjan van de Venbbe29872009-10-20 07:09:39 +0900907 if (!filt)
908 return;
909
910 filt->name = strdup(string);
911 filt->pid = pid;
912 filt->next = process_filter;
913
914 process_filter = filt;
915}
916
917static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
918{
919 struct process_filter *filt;
920 if (!process_filter)
921 return 1;
922
923 filt = process_filter;
924 while (filt) {
925 if (filt->pid && p->pid == filt->pid)
926 return 1;
927 if (strcmp(filt->name, c->comm) == 0)
928 return 1;
929 filt = filt->next;
930 }
931 return 0;
932}
933
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300934static int determine_display_tasks_filtered(struct timechart *tchart)
Arjan van de Venbbe29872009-10-20 07:09:39 +0900935{
936 struct per_pid *p;
937 struct per_pidcomm *c;
938 int count = 0;
939
940 p = all_data;
941 while (p) {
942 p->display = 0;
943 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300944 p->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900945
946 /* no exit marker, task kept running to the end */
947 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300948 p->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900949
950 c = p->all;
951
952 while (c) {
953 c->display = 0;
954
955 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300956 c->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900957
958 if (passes_filter(p, c)) {
959 c->display = 1;
960 p->display = 1;
961 count++;
962 }
963
964 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300965 c->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900966
967 c = c->next;
968 }
969 p = p->next;
970 }
971 return count;
972}
973
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300974static int determine_display_tasks(struct timechart *tchart, u64 threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +0200975{
976 struct per_pid *p;
977 struct per_pidcomm *c;
978 int count = 0;
979
Arjan van de Venbbe29872009-10-20 07:09:39 +0900980 if (process_filter)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300981 return determine_display_tasks_filtered(tchart);
Arjan van de Venbbe29872009-10-20 07:09:39 +0900982
Arjan van de Ven10274982009-09-12 07:53:05 +0200983 p = all_data;
984 while (p) {
985 p->display = 0;
986 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300987 p->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200988
989 /* no exit marker, task kept running to the end */
990 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300991 p->end_time = tchart->last_time;
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400992 if (p->total_time >= threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +0200993 p->display = 1;
994
995 c = p->all;
996
997 while (c) {
998 c->display = 0;
999
1000 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001001 c->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001002
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001003 if (c->total_time >= threshold) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001004 c->display = 1;
1005 count++;
1006 }
1007
1008 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001009 c->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001010
1011 c = c->next;
1012 }
1013 p = p->next;
1014 }
1015 return count;
1016}
1017
1018
1019
1020#define TIME_THRESH 10000000
1021
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001022static void write_svg_file(struct timechart *tchart, const char *filename)
Arjan van de Ven10274982009-09-12 07:53:05 +02001023{
1024 u64 i;
1025 int count;
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001026 int thresh = TIME_THRESH;
Arjan van de Ven10274982009-09-12 07:53:05 +02001027
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001028 tchart->numcpus++;
Arjan van de Ven10274982009-09-12 07:53:05 +02001029
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001030 if (tchart->power_only)
1031 tchart->proc_num = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +02001032
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001033 /* We'd like to show at least proc_num tasks;
1034 * be less picky if we have fewer */
1035 do {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001036 count = determine_display_tasks(tchart, thresh);
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001037 thresh /= 10;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001038 } while (!process_filter && thresh && count < tchart->proc_num);
Arjan van de Ven10274982009-09-12 07:53:05 +02001039
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001040 open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001041
Arjan van de Ven5094b652009-09-20 18:14:16 +02001042 svg_time_grid();
Arjan van de Ven10274982009-09-12 07:53:05 +02001043 svg_legenda();
1044
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001045 for (i = 0; i < tchart->numcpus; i++)
1046 svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
Arjan van de Ven10274982009-09-12 07:53:05 +02001047
1048 draw_cpu_usage();
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001049 if (tchart->proc_num)
1050 draw_process_bars(tchart);
1051 if (!tchart->tasks_only)
1052 draw_c_p_states(tchart);
1053 if (tchart->proc_num)
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001054 draw_wakeups();
Arjan van de Ven10274982009-09-12 07:53:05 +02001055
1056 svg_close();
1057}
1058
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001059static int __cmd_timechart(struct timechart *tchart, const char *output_name)
Arjan van de Ven10274982009-09-12 07:53:05 +02001060{
Jiri Olsa59366782013-07-11 17:28:30 +02001061 const struct perf_evsel_str_handler power_tracepoints[] = {
1062 { "power:cpu_idle", process_sample_cpu_idle },
1063 { "power:cpu_frequency", process_sample_cpu_frequency },
1064 { "sched:sched_wakeup", process_sample_sched_wakeup },
1065 { "sched:sched_switch", process_sample_sched_switch },
1066#ifdef SUPPORT_OLD_POWER_EVENTS
1067 { "power:power_start", process_sample_power_start },
1068 { "power:power_end", process_sample_power_end },
1069 { "power:power_frequency", process_sample_power_frequency },
1070#endif
1071 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001072 struct perf_data_file file = {
1073 .path = input_name,
1074 .mode = PERF_DATA_MODE_READ,
1075 };
1076
1077 struct perf_session *session = perf_session__new(&file, false,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001078 &tchart->tool);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001079 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001080
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001081 if (session == NULL)
1082 return -ENOMEM;
1083
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001084 if (!perf_session__has_traces(session, "timechart record"))
1085 goto out_delete;
1086
Jiri Olsa59366782013-07-11 17:28:30 +02001087 if (perf_session__set_tracepoints_handlers(session,
1088 power_tracepoints)) {
1089 pr_err("Initializing session tracepoint handlers failed\n");
1090 goto out_delete;
1091 }
1092
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001093 ret = perf_session__process_events(session, &tchart->tool);
Li Zefan5cbd0802009-12-01 14:05:16 +08001094 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001095 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001096
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001097 end_sample_processing(tchart);
Arjan van de Ven10274982009-09-12 07:53:05 +02001098
1099 sort_pids();
1100
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001101 write_svg_file(tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001102
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001103 pr_info("Written %2.1f seconds of trace to %s.\n",
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001104 (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001105out_delete:
1106 perf_session__delete(session);
1107 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001108}
1109
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001110static int timechart__record(struct timechart *tchart, int argc, const char **argv)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001111{
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001112 unsigned int rec_argc, i, j;
1113 const char **rec_argv;
1114 const char **p;
1115 unsigned int record_elems;
1116
1117 const char * const common_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001118 "record", "-a", "-R", "-c", "1",
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001119 };
1120 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1121
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001122 const char * const backtrace_args[] = {
1123 "-g",
1124 };
1125 unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
1126
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001127 const char * const power_args[] = {
1128 "-e", "power:cpu_frequency",
1129 "-e", "power:cpu_idle",
1130 };
1131 unsigned int power_args_nr = ARRAY_SIZE(power_args);
1132
1133 const char * const old_power_args[] = {
1134#ifdef SUPPORT_OLD_POWER_EVENTS
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001135 "-e", "power:power_start",
1136 "-e", "power:power_end",
1137 "-e", "power:power_frequency",
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001138#endif
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001139 };
1140 unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
1141
1142 const char * const tasks_args[] = {
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001143 "-e", "sched:sched_wakeup",
1144 "-e", "sched:sched_switch",
1145 };
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001146 unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001147
Thomas Renninger20c457b2011-01-03 17:50:45 +01001148#ifdef SUPPORT_OLD_POWER_EVENTS
1149 if (!is_valid_tracepoint("power:cpu_idle") &&
1150 is_valid_tracepoint("power:power_start")) {
1151 use_old_power_events = 1;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001152 power_args_nr = 0;
1153 } else {
1154 old_power_args_nr = 0;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001155 }
1156#endif
1157
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001158 if (tchart->power_only)
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001159 tasks_args_nr = 0;
1160
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001161 if (tchart->tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001162 power_args_nr = 0;
1163 old_power_args_nr = 0;
1164 }
1165
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001166 if (!tchart->with_backtrace)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001167 backtrace_args_no = 0;
1168
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001169 record_elems = common_args_nr + tasks_args_nr +
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001170 power_args_nr + old_power_args_nr + backtrace_args_no;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001171
1172 rec_argc = record_elems + argc;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001173 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1174
Chris Samuelce47dc52010-11-13 13:35:06 +11001175 if (rec_argv == NULL)
1176 return -ENOMEM;
1177
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001178 p = rec_argv;
1179 for (i = 0; i < common_args_nr; i++)
1180 *p++ = strdup(common_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001181
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001182 for (i = 0; i < backtrace_args_no; i++)
1183 *p++ = strdup(backtrace_args[i]);
1184
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001185 for (i = 0; i < tasks_args_nr; i++)
1186 *p++ = strdup(tasks_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001187
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001188 for (i = 0; i < power_args_nr; i++)
1189 *p++ = strdup(power_args[i]);
1190
1191 for (i = 0; i < old_power_args_nr; i++)
1192 *p++ = strdup(old_power_args[i]);
1193
1194 for (j = 1; j < (unsigned int)argc; j++)
1195 *p++ = argv[j];
1196
1197 return cmd_record(rec_argc, rec_argv, NULL);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001198}
1199
Arjan van de Venbbe29872009-10-20 07:09:39 +09001200static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001201parse_process(const struct option *opt __maybe_unused, const char *arg,
1202 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001203{
1204 if (arg)
1205 add_process_filter(arg);
1206 return 0;
1207}
1208
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001209int cmd_timechart(int argc, const char **argv,
1210 const char *prefix __maybe_unused)
1211{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001212 struct timechart tchart = {
1213 .tool = {
1214 .comm = process_comm_event,
1215 .fork = process_fork_event,
1216 .exit = process_exit_event,
1217 .sample = process_sample_event,
1218 .ordered_samples = true,
1219 },
1220 .proc_num = 15,
1221 };
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001222 const char *output_name = "output.svg";
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001223 const struct option timechart_options[] = {
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001224 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1225 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1226 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001227 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1228 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001229 "output processes data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001230 OPT_CALLBACK('p', "process", NULL, "process",
1231 "process selector. Pass a pid or process name.",
1232 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001233 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1234 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001235 OPT_INTEGER('n', "proc-num", &tchart.proc_num,
Stanislav Fomichev54874e32013-11-01 20:25:46 +04001236 "min. number of tasks to print"),
Arjan van de Ven10274982009-09-12 07:53:05 +02001237 OPT_END()
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001238 };
1239 const char * const timechart_usage[] = {
1240 "perf timechart [<options>] {record}",
1241 NULL
1242 };
Arjan van de Ven10274982009-09-12 07:53:05 +02001243
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001244 const struct option record_options[] = {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001245 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1246 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001247 "output processes data only"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001248 OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001249 OPT_END()
1250 };
1251 const char * const record_usage[] = {
1252 "perf timechart record [<options>]",
1253 NULL
1254 };
1255 argc = parse_options(argc, argv, timechart_options, timechart_usage,
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001256 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001257
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001258 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001259 pr_err("-P and -T options cannot be used at the same time.\n");
1260 return -1;
1261 }
1262
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001263 symbol__init();
1264
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001265 if (argc && !strncmp(argv[0], "rec", 3)) {
1266 argc = parse_options(argc, argv, record_options, record_usage,
1267 PARSE_OPT_STOP_AT_NON_OPTION);
1268
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001269 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001270 pr_err("-P and -T options cannot be used at the same time.\n");
1271 return -1;
1272 }
1273
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001274 return timechart__record(&tchart, argc, argv);
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001275 } else if (argc)
1276 usage_with_options(timechart_usage, timechart_options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001277
1278 setup_pager();
1279
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001280 return __cmd_timechart(&tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001281}