blob: e11c61d9bda4dee188719f96b2fdbb00e5d6140f [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
44
Arjan van de Ven10274982009-09-12 07:53:05 +020045static unsigned int numcpus;
46static u64 min_freq; /* Lowest CPU frequency seen */
47static u64 max_freq; /* Highest CPU frequency seen */
48static u64 turbo_frequency;
49
50static u64 first_time, last_time;
51
Ian Munsiec0555642010-04-13 18:37:33 +100052static bool power_only;
Arjan van de Ven39a90a82009-09-24 15:40:13 +020053
Arjan van de Ven10274982009-09-12 07:53:05 +020054
Arjan van de Ven10274982009-09-12 07:53:05 +020055struct per_pid;
56struct per_pidcomm;
57
58struct cpu_sample;
59struct power_event;
60struct wake_event;
61
62struct sample_wrapper;
63
64/*
65 * Datastructure layout:
66 * We keep an list of "pid"s, matching the kernels notion of a task struct.
67 * Each "pid" entry, has a list of "comm"s.
68 * this is because we want to track different programs different, while
69 * exec will reuse the original pid (by design).
70 * Each comm has a list of samples that will be used to draw
71 * final graph.
72 */
73
74struct per_pid {
75 struct per_pid *next;
76
77 int pid;
78 int ppid;
79
80 u64 start_time;
81 u64 end_time;
82 u64 total_time;
83 int display;
84
85 struct per_pidcomm *all;
86 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +020087};
88
89
90struct per_pidcomm {
91 struct per_pidcomm *next;
92
93 u64 start_time;
94 u64 end_time;
95 u64 total_time;
96
97 int Y;
98 int display;
99
100 long state;
101 u64 state_since;
102
103 char *comm;
104
105 struct cpu_sample *samples;
106};
107
108struct sample_wrapper {
109 struct sample_wrapper *next;
110
111 u64 timestamp;
112 unsigned char data[0];
113};
114
115#define TYPE_NONE 0
116#define TYPE_RUNNING 1
117#define TYPE_WAITING 2
118#define TYPE_BLOCKED 3
119
120struct cpu_sample {
121 struct cpu_sample *next;
122
123 u64 start_time;
124 u64 end_time;
125 int type;
126 int cpu;
127};
128
129static struct per_pid *all_data;
130
131#define CSTATE 1
132#define PSTATE 2
133
134struct power_event {
135 struct power_event *next;
136 int type;
137 int state;
138 u64 start_time;
139 u64 end_time;
140 int cpu;
141};
142
143struct wake_event {
144 struct wake_event *next;
145 int waker;
146 int wakee;
147 u64 time;
148};
149
150static struct power_event *power_events;
151static struct wake_event *wake_events;
152
Arjan van de Venbbe29872009-10-20 07:09:39 +0900153struct process_filter;
154struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800155 char *name;
156 int pid;
157 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900158};
159
160static struct process_filter *process_filter;
161
162
Arjan van de Ven10274982009-09-12 07:53:05 +0200163static struct per_pid *find_create_pid(int pid)
164{
165 struct per_pid *cursor = all_data;
166
167 while (cursor) {
168 if (cursor->pid == pid)
169 return cursor;
170 cursor = cursor->next;
171 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300172 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200173 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200174 cursor->pid = pid;
175 cursor->next = all_data;
176 all_data = cursor;
177 return cursor;
178}
179
180static void pid_set_comm(int pid, char *comm)
181{
182 struct per_pid *p;
183 struct per_pidcomm *c;
184 p = find_create_pid(pid);
185 c = p->all;
186 while (c) {
187 if (c->comm && strcmp(c->comm, comm) == 0) {
188 p->current = c;
189 return;
190 }
191 if (!c->comm) {
192 c->comm = strdup(comm);
193 p->current = c;
194 return;
195 }
196 c = c->next;
197 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300198 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200199 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200200 c->comm = strdup(comm);
201 p->current = c;
202 c->next = p->all;
203 p->all = c;
204}
205
206static void pid_fork(int pid, int ppid, u64 timestamp)
207{
208 struct per_pid *p, *pp;
209 p = find_create_pid(pid);
210 pp = find_create_pid(ppid);
211 p->ppid = ppid;
212 if (pp->current && pp->current->comm && !p->current)
213 pid_set_comm(pid, pp->current->comm);
214
215 p->start_time = timestamp;
216 if (p->current) {
217 p->current->start_time = timestamp;
218 p->current->state_since = timestamp;
219 }
220}
221
222static void pid_exit(int pid, u64 timestamp)
223{
224 struct per_pid *p;
225 p = find_create_pid(pid);
226 p->end_time = timestamp;
227 if (p->current)
228 p->current->end_time = timestamp;
229}
230
231static void
232pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
233{
234 struct per_pid *p;
235 struct per_pidcomm *c;
236 struct cpu_sample *sample;
237
238 p = find_create_pid(pid);
239 c = p->current;
240 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300241 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200242 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200243 p->current = c;
244 c->next = p->all;
245 p->all = c;
246 }
247
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300248 sample = zalloc(sizeof(*sample));
Arjan van de Ven10274982009-09-12 07:53:05 +0200249 assert(sample != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200250 sample->start_time = start;
251 sample->end_time = end;
252 sample->type = type;
253 sample->next = c->samples;
254 sample->cpu = cpu;
255 c->samples = sample;
256
257 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
258 c->total_time += (end-start);
259 p->total_time += (end-start);
260 }
261
262 if (c->start_time == 0 || c->start_time > start)
263 c->start_time = start;
264 if (p->start_time == 0 || p->start_time > start)
265 p->start_time = start;
Arjan van de Ven10274982009-09-12 07:53:05 +0200266}
267
268#define MAX_CPUS 4096
269
270static u64 cpus_cstate_start_times[MAX_CPUS];
271static int cpus_cstate_state[MAX_CPUS];
272static u64 cpus_pstate_start_times[MAX_CPUS];
273static u64 cpus_pstate_state[MAX_CPUS];
274
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300275static int process_comm_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200276 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300277 struct perf_sample *sample __maybe_unused,
278 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200279{
Arjan van de Ven8f06d7e2010-01-16 12:53:19 -0800280 pid_set_comm(event->comm.tid, event->comm.comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200281 return 0;
282}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200283
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300284static int process_fork_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200285 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300286 struct perf_sample *sample __maybe_unused,
287 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200288{
289 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
290 return 0;
291}
292
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300293static int process_exit_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200294 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300295 struct perf_sample *sample __maybe_unused,
296 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200297{
298 pid_exit(event->fork.pid, event->fork.time);
299 return 0;
300}
301
302struct trace_entry {
Arjan van de Ven10274982009-09-12 07:53:05 +0200303 unsigned short type;
304 unsigned char flags;
305 unsigned char preempt_count;
306 int pid;
OGAWA Hirofumi028c5152009-12-06 20:07:29 +0900307 int lock_depth;
Arjan van de Ven10274982009-09-12 07:53:05 +0200308};
309
Thomas Renninger20c457b2011-01-03 17:50:45 +0100310#ifdef SUPPORT_OLD_POWER_EVENTS
311static int use_old_power_events;
312struct power_entry_old {
Arjan van de Ven10274982009-09-12 07:53:05 +0200313 struct trace_entry te;
Thomas Renninger4c21adf2010-07-20 16:59:34 -0700314 u64 type;
315 u64 value;
316 u64 cpu_id;
Arjan van de Ven10274982009-09-12 07:53:05 +0200317};
Thomas Renninger20c457b2011-01-03 17:50:45 +0100318#endif
319
320struct power_processor_entry {
321 struct trace_entry te;
322 u32 state;
323 u32 cpu_id;
324};
Arjan van de Ven10274982009-09-12 07:53:05 +0200325
326#define TASK_COMM_LEN 16
327struct wakeup_entry {
328 struct trace_entry te;
329 char comm[TASK_COMM_LEN];
330 int pid;
331 int prio;
332 int success;
333};
334
Arjan van de Ven10274982009-09-12 07:53:05 +0200335struct sched_switch {
336 struct trace_entry te;
337 char prev_comm[TASK_COMM_LEN];
338 int prev_pid;
339 int prev_prio;
340 long prev_state; /* Arjan weeps. */
341 char next_comm[TASK_COMM_LEN];
342 int next_pid;
343 int next_prio;
344};
345
346static void c_state_start(int cpu, u64 timestamp, int state)
347{
348 cpus_cstate_start_times[cpu] = timestamp;
349 cpus_cstate_state[cpu] = state;
350}
351
352static void c_state_end(int cpu, u64 timestamp)
353{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300354 struct power_event *pwr = zalloc(sizeof(*pwr));
355
Arjan van de Ven10274982009-09-12 07:53:05 +0200356 if (!pwr)
357 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200358
359 pwr->state = cpus_cstate_state[cpu];
360 pwr->start_time = cpus_cstate_start_times[cpu];
361 pwr->end_time = timestamp;
362 pwr->cpu = cpu;
363 pwr->type = CSTATE;
364 pwr->next = power_events;
365
366 power_events = pwr;
367}
368
369static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
370{
371 struct power_event *pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200372
373 if (new_freq > 8000000) /* detect invalid data */
374 return;
375
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300376 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200377 if (!pwr)
378 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200379
380 pwr->state = cpus_pstate_state[cpu];
381 pwr->start_time = cpus_pstate_start_times[cpu];
382 pwr->end_time = timestamp;
383 pwr->cpu = cpu;
384 pwr->type = PSTATE;
385 pwr->next = power_events;
386
387 if (!pwr->start_time)
388 pwr->start_time = first_time;
389
390 power_events = pwr;
391
392 cpus_pstate_state[cpu] = new_freq;
393 cpus_pstate_start_times[cpu] = timestamp;
394
395 if ((u64)new_freq > max_freq)
396 max_freq = new_freq;
397
398 if (new_freq < min_freq || min_freq == 0)
399 min_freq = new_freq;
400
401 if (new_freq == max_freq - 1000)
402 turbo_frequency = max_freq;
403}
404
405static void
406sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
407{
Arjan van de Ven10274982009-09-12 07:53:05 +0200408 struct per_pid *p;
409 struct wakeup_entry *wake = (void *)te;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300410 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200411
Arjan van de Ven10274982009-09-12 07:53:05 +0200412 if (!we)
413 return;
414
Arjan van de Ven10274982009-09-12 07:53:05 +0200415 we->time = timestamp;
416 we->waker = pid;
417
418 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
419 we->waker = -1;
420
421 we->wakee = wake->pid;
422 we->next = wake_events;
423 wake_events = we;
424 p = find_create_pid(we->wakee);
425
426 if (p && p->current && p->current->state == TYPE_NONE) {
427 p->current->state_since = timestamp;
428 p->current->state = TYPE_WAITING;
429 }
430 if (p && p->current && p->current->state == TYPE_BLOCKED) {
431 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
432 p->current->state_since = timestamp;
433 p->current->state = TYPE_WAITING;
434 }
435}
436
437static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
438{
439 struct per_pid *p = NULL, *prev_p;
440 struct sched_switch *sw = (void *)te;
441
442
443 prev_p = find_create_pid(sw->prev_pid);
444
445 p = find_create_pid(sw->next_pid);
446
447 if (prev_p->current && prev_p->current->state != TYPE_NONE)
448 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
449 if (p && p->current) {
450 if (p->current->state != TYPE_NONE)
451 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
452
Julia Lawall33e26a12010-08-05 22:27:51 +0200453 p->current->state_since = timestamp;
454 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200455 }
456
457 if (prev_p->current) {
458 prev_p->current->state = TYPE_NONE;
459 prev_p->current->state_since = timestamp;
460 if (sw->prev_state & 2)
461 prev_p->current->state = TYPE_BLOCKED;
462 if (sw->prev_state == 0)
463 prev_p->current->state = TYPE_WAITING;
464 }
465}
466
Jiri Olsa59366782013-07-11 17:28:30 +0200467typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
468 struct perf_sample *sample);
Arjan van de Ven10274982009-09-12 07:53:05 +0200469
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300470static int process_sample_event(struct perf_tool *tool __maybe_unused,
471 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200472 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200473 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300474 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200475{
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200476 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200477 if (!first_time || first_time > sample->time)
478 first_time = sample->time;
479 if (last_time < sample->time)
480 last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200481 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200482
Jiri Olsa59366782013-07-11 17:28:30 +0200483 if (sample->cpu > numcpus)
484 numcpus = sample->cpu;
Arjan van de Ven10274982009-09-12 07:53:05 +0200485
Jiri Olsa59366782013-07-11 17:28:30 +0200486 if (evsel->handler.func != NULL) {
487 tracepoint_handler f = evsel->handler.func;
488 return f(evsel, sample);
Arjan van de Ven10274982009-09-12 07:53:05 +0200489 }
Jiri Olsa59366782013-07-11 17:28:30 +0200490
Arjan van de Ven10274982009-09-12 07:53:05 +0200491 return 0;
492}
493
Jiri Olsa59366782013-07-11 17:28:30 +0200494static int
495process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
496 struct perf_sample *sample)
497{
498 struct power_processor_entry *ppe = sample->raw_data;
499
500 if (ppe->state == (u32) PWR_EVENT_EXIT)
501 c_state_end(ppe->cpu_id, sample->time);
502 else
503 c_state_start(ppe->cpu_id, sample->time, ppe->state);
504 return 0;
505}
506
507static int
508process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
509 struct perf_sample *sample)
510{
511 struct power_processor_entry *ppe = sample->raw_data;
512
513 p_state_change(ppe->cpu_id, sample->time, ppe->state);
514 return 0;
515}
516
517static int
518process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
519 struct perf_sample *sample)
520{
521 struct trace_entry *te = sample->raw_data;
522
523 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
524 return 0;
525}
526
527static int
528process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
529 struct perf_sample *sample)
530{
531 struct trace_entry *te = sample->raw_data;
532
533 sched_switch(sample->cpu, sample->time, te);
534 return 0;
535}
536
537#ifdef SUPPORT_OLD_POWER_EVENTS
538static int
539process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
540 struct perf_sample *sample)
541{
542 struct power_entry_old *peo = sample->raw_data;
543
544 c_state_start(peo->cpu_id, sample->time, peo->value);
545 return 0;
546}
547
548static int
549process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
550 struct perf_sample *sample)
551{
552 c_state_end(sample->cpu, sample->time);
553 return 0;
554}
555
556static int
557process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
558 struct perf_sample *sample)
559{
560 struct power_entry_old *peo = sample->raw_data;
561
562 p_state_change(peo->cpu_id, sample->time, peo->value);
563 return 0;
564}
565#endif /* SUPPORT_OLD_POWER_EVENTS */
566
Arjan van de Ven10274982009-09-12 07:53:05 +0200567/*
568 * After the last sample we need to wrap up the current C/P state
569 * and close out each CPU for these.
570 */
571static void end_sample_processing(void)
572{
573 u64 cpu;
574 struct power_event *pwr;
575
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200576 for (cpu = 0; cpu <= numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200577 /* C state */
578#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300579 pwr = zalloc(sizeof(*pwr));
580 if (!pwr)
581 return;
582
Arjan van de Ven10274982009-09-12 07:53:05 +0200583 pwr->state = cpus_cstate_state[cpu];
584 pwr->start_time = cpus_cstate_start_times[cpu];
585 pwr->end_time = last_time;
586 pwr->cpu = cpu;
587 pwr->type = CSTATE;
588 pwr->next = power_events;
589
590 power_events = pwr;
591#endif
592 /* P state */
593
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300594 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200595 if (!pwr)
596 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200597
598 pwr->state = cpus_pstate_state[cpu];
599 pwr->start_time = cpus_pstate_start_times[cpu];
600 pwr->end_time = last_time;
601 pwr->cpu = cpu;
602 pwr->type = PSTATE;
603 pwr->next = power_events;
604
605 if (!pwr->start_time)
606 pwr->start_time = first_time;
607 if (!pwr->state)
608 pwr->state = min_freq;
609 power_events = pwr;
610 }
611}
612
Arjan van de Ven10274982009-09-12 07:53:05 +0200613/*
614 * Sort the pid datastructure
615 */
616static void sort_pids(void)
617{
618 struct per_pid *new_list, *p, *cursor, *prev;
619 /* sort by ppid first, then by pid, lowest to highest */
620
621 new_list = NULL;
622
623 while (all_data) {
624 p = all_data;
625 all_data = p->next;
626 p->next = NULL;
627
628 if (new_list == NULL) {
629 new_list = p;
630 p->next = NULL;
631 continue;
632 }
633 prev = NULL;
634 cursor = new_list;
635 while (cursor) {
636 if (cursor->ppid > p->ppid ||
637 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
638 /* must insert before */
639 if (prev) {
640 p->next = prev->next;
641 prev->next = p;
642 cursor = NULL;
643 continue;
644 } else {
645 p->next = new_list;
646 new_list = p;
647 cursor = NULL;
648 continue;
649 }
650 }
651
652 prev = cursor;
653 cursor = cursor->next;
654 if (!cursor)
655 prev->next = p;
656 }
657 }
658 all_data = new_list;
659}
660
661
662static void draw_c_p_states(void)
663{
664 struct power_event *pwr;
665 pwr = power_events;
666
667 /*
668 * two pass drawing so that the P state bars are on top of the C state blocks
669 */
670 while (pwr) {
671 if (pwr->type == CSTATE)
672 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
673 pwr = pwr->next;
674 }
675
676 pwr = power_events;
677 while (pwr) {
678 if (pwr->type == PSTATE) {
679 if (!pwr->state)
680 pwr->state = min_freq;
681 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
682 }
683 pwr = pwr->next;
684 }
685}
686
687static void draw_wakeups(void)
688{
689 struct wake_event *we;
690 struct per_pid *p;
691 struct per_pidcomm *c;
692
693 we = wake_events;
694 while (we) {
695 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200696 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200697
698 /* locate the column of the waker and wakee */
699 p = all_data;
700 while (p) {
701 if (p->pid == we->waker || p->pid == we->wakee) {
702 c = p->all;
703 while (c) {
704 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900705 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200706 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900707 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200708 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900709 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200710 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900711 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200712 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200713 }
714 c = c->next;
715 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900716 c = p->all;
717 while (c) {
718 if (p->pid == we->waker && !from) {
719 from = c->Y;
720 task_from = strdup(c->comm);
721 }
722 if (p->pid == we->wakee && !to) {
723 to = c->Y;
724 task_to = strdup(c->comm);
725 }
726 c = c->next;
727 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200728 }
729 p = p->next;
730 }
731
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900732 if (!task_from) {
733 task_from = malloc(40);
734 sprintf(task_from, "[%i]", we->waker);
735 }
736 if (!task_to) {
737 task_to = malloc(40);
738 sprintf(task_to, "[%i]", we->wakee);
739 }
740
Arjan van de Ven10274982009-09-12 07:53:05 +0200741 if (we->waker == -1)
742 svg_interrupt(we->time, to);
743 else if (from && to && abs(from - to) == 1)
744 svg_wakeline(we->time, from, to);
745 else
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200746 svg_partial_wakeline(we->time, from, task_from, to, task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200747 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900748
749 free(task_from);
750 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200751 }
752}
753
754static void draw_cpu_usage(void)
755{
756 struct per_pid *p;
757 struct per_pidcomm *c;
758 struct cpu_sample *sample;
759 p = all_data;
760 while (p) {
761 c = p->all;
762 while (c) {
763 sample = c->samples;
764 while (sample) {
765 if (sample->type == TYPE_RUNNING)
766 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
767
768 sample = sample->next;
769 }
770 c = c->next;
771 }
772 p = p->next;
773 }
774}
775
776static void draw_process_bars(void)
777{
778 struct per_pid *p;
779 struct per_pidcomm *c;
780 struct cpu_sample *sample;
781 int Y = 0;
782
783 Y = 2 * numcpus + 2;
784
785 p = all_data;
786 while (p) {
787 c = p->all;
788 while (c) {
789 if (!c->display) {
790 c->Y = 0;
791 c = c->next;
792 continue;
793 }
794
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200795 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +0200796 sample = c->samples;
797 while (sample) {
798 if (sample->type == TYPE_RUNNING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200799 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200800 if (sample->type == TYPE_BLOCKED)
801 svg_box(Y, sample->start_time, sample->end_time, "blocked");
802 if (sample->type == TYPE_WAITING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200803 svg_waiting(Y, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200804 sample = sample->next;
805 }
806
807 if (c->comm) {
808 char comm[256];
809 if (c->total_time > 5000000000) /* 5 seconds */
810 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
811 else
812 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
813
814 svg_text(Y, c->start_time, comm);
815 }
816 c->Y = Y;
817 Y++;
818 c = c->next;
819 }
820 p = p->next;
821 }
822}
823
Arjan van de Venbbe29872009-10-20 07:09:39 +0900824static void add_process_filter(const char *string)
825{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300826 int pid = strtoull(string, NULL, 10);
827 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +0900828
Arjan van de Venbbe29872009-10-20 07:09:39 +0900829 if (!filt)
830 return;
831
832 filt->name = strdup(string);
833 filt->pid = pid;
834 filt->next = process_filter;
835
836 process_filter = filt;
837}
838
839static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
840{
841 struct process_filter *filt;
842 if (!process_filter)
843 return 1;
844
845 filt = process_filter;
846 while (filt) {
847 if (filt->pid && p->pid == filt->pid)
848 return 1;
849 if (strcmp(filt->name, c->comm) == 0)
850 return 1;
851 filt = filt->next;
852 }
853 return 0;
854}
855
856static int determine_display_tasks_filtered(void)
857{
858 struct per_pid *p;
859 struct per_pidcomm *c;
860 int count = 0;
861
862 p = all_data;
863 while (p) {
864 p->display = 0;
865 if (p->start_time == 1)
866 p->start_time = first_time;
867
868 /* no exit marker, task kept running to the end */
869 if (p->end_time == 0)
870 p->end_time = last_time;
871
872 c = p->all;
873
874 while (c) {
875 c->display = 0;
876
877 if (c->start_time == 1)
878 c->start_time = first_time;
879
880 if (passes_filter(p, c)) {
881 c->display = 1;
882 p->display = 1;
883 count++;
884 }
885
886 if (c->end_time == 0)
887 c->end_time = last_time;
888
889 c = c->next;
890 }
891 p = p->next;
892 }
893 return count;
894}
895
Arjan van de Ven10274982009-09-12 07:53:05 +0200896static int determine_display_tasks(u64 threshold)
897{
898 struct per_pid *p;
899 struct per_pidcomm *c;
900 int count = 0;
901
Arjan van de Venbbe29872009-10-20 07:09:39 +0900902 if (process_filter)
903 return determine_display_tasks_filtered();
904
Arjan van de Ven10274982009-09-12 07:53:05 +0200905 p = all_data;
906 while (p) {
907 p->display = 0;
908 if (p->start_time == 1)
909 p->start_time = first_time;
910
911 /* no exit marker, task kept running to the end */
912 if (p->end_time == 0)
913 p->end_time = last_time;
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200914 if (p->total_time >= threshold && !power_only)
Arjan van de Ven10274982009-09-12 07:53:05 +0200915 p->display = 1;
916
917 c = p->all;
918
919 while (c) {
920 c->display = 0;
921
922 if (c->start_time == 1)
923 c->start_time = first_time;
924
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200925 if (c->total_time >= threshold && !power_only) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200926 c->display = 1;
927 count++;
928 }
929
930 if (c->end_time == 0)
931 c->end_time = last_time;
932
933 c = c->next;
934 }
935 p = p->next;
936 }
937 return count;
938}
939
940
941
942#define TIME_THRESH 10000000
943
944static void write_svg_file(const char *filename)
945{
946 u64 i;
947 int count;
948
949 numcpus++;
950
951
952 count = determine_display_tasks(TIME_THRESH);
953
954 /* We'd like to show at least 15 tasks; be less picky if we have fewer */
955 if (count < 15)
956 count = determine_display_tasks(TIME_THRESH / 10);
957
Arjan van de Ven5094b652009-09-20 18:14:16 +0200958 open_svg(filename, numcpus, count, first_time, last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200959
Arjan van de Ven5094b652009-09-20 18:14:16 +0200960 svg_time_grid();
Arjan van de Ven10274982009-09-12 07:53:05 +0200961 svg_legenda();
962
963 for (i = 0; i < numcpus; i++)
964 svg_cpu_box(i, max_freq, turbo_frequency);
965
966 draw_cpu_usage();
967 draw_process_bars();
968 draw_c_p_states();
969 draw_wakeups();
970
971 svg_close();
972}
973
Feng Tang70cb4e92012-10-30 11:56:02 +0800974static int __cmd_timechart(const char *output_name)
Arjan van de Ven10274982009-09-12 07:53:05 +0200975{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -0300976 struct perf_tool perf_timechart = {
977 .comm = process_comm_event,
978 .fork = process_fork_event,
979 .exit = process_exit_event,
980 .sample = process_sample_event,
981 .ordered_samples = true,
982 };
Jiri Olsa59366782013-07-11 17:28:30 +0200983 const struct perf_evsel_str_handler power_tracepoints[] = {
984 { "power:cpu_idle", process_sample_cpu_idle },
985 { "power:cpu_frequency", process_sample_cpu_frequency },
986 { "sched:sched_wakeup", process_sample_sched_wakeup },
987 { "sched:sched_switch", process_sample_sched_switch },
988#ifdef SUPPORT_OLD_POWER_EVENTS
989 { "power:power_start", process_sample_power_start },
990 { "power:power_end", process_sample_power_end },
991 { "power:power_frequency", process_sample_power_frequency },
992#endif
993 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200994 struct perf_data_file file = {
995 .path = input_name,
996 .mode = PERF_DATA_MODE_READ,
997 };
998
999 struct perf_session *session = perf_session__new(&file, false,
1000 &perf_timechart);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001001 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001002
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001003 if (session == NULL)
1004 return -ENOMEM;
1005
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001006 if (!perf_session__has_traces(session, "timechart record"))
1007 goto out_delete;
1008
Jiri Olsa59366782013-07-11 17:28:30 +02001009 if (perf_session__set_tracepoints_handlers(session,
1010 power_tracepoints)) {
1011 pr_err("Initializing session tracepoint handlers failed\n");
1012 goto out_delete;
1013 }
1014
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001015 ret = perf_session__process_events(session, &perf_timechart);
Li Zefan5cbd0802009-12-01 14:05:16 +08001016 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001017 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001018
Arjan van de Ven10274982009-09-12 07:53:05 +02001019 end_sample_processing();
1020
1021 sort_pids();
1022
1023 write_svg_file(output_name);
1024
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001025 pr_info("Written %2.1f seconds of trace to %s.\n",
1026 (last_time - first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001027out_delete:
1028 perf_session__delete(session);
1029 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001030}
1031
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001032static int __cmd_record(int argc, const char **argv)
1033{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001034#ifdef SUPPORT_OLD_POWER_EVENTS
1035 const char * const record_old_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001036 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001037 "-e", "power:power_start",
1038 "-e", "power:power_end",
1039 "-e", "power:power_frequency",
1040 "-e", "sched:sched_wakeup",
1041 "-e", "sched:sched_switch",
1042 };
1043#endif
1044 const char * const record_new_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001045 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001046 "-e", "power:cpu_frequency",
1047 "-e", "power:cpu_idle",
1048 "-e", "sched:sched_wakeup",
1049 "-e", "sched:sched_switch",
1050 };
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001051 unsigned int rec_argc, i, j;
1052 const char **rec_argv;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001053 const char * const *record_args = record_new_args;
1054 unsigned int record_elems = ARRAY_SIZE(record_new_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001055
Thomas Renninger20c457b2011-01-03 17:50:45 +01001056#ifdef SUPPORT_OLD_POWER_EVENTS
1057 if (!is_valid_tracepoint("power:cpu_idle") &&
1058 is_valid_tracepoint("power:power_start")) {
1059 use_old_power_events = 1;
1060 record_args = record_old_args;
1061 record_elems = ARRAY_SIZE(record_old_args);
1062 }
1063#endif
1064
1065 rec_argc = record_elems + argc - 1;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001066 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1067
Chris Samuelce47dc52010-11-13 13:35:06 +11001068 if (rec_argv == NULL)
1069 return -ENOMEM;
1070
Thomas Renninger20c457b2011-01-03 17:50:45 +01001071 for (i = 0; i < record_elems; i++)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001072 rec_argv[i] = strdup(record_args[i]);
1073
1074 for (j = 1; j < (unsigned int)argc; j++, i++)
1075 rec_argv[i] = argv[j];
1076
1077 return cmd_record(i, rec_argv, NULL);
1078}
1079
Arjan van de Venbbe29872009-10-20 07:09:39 +09001080static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001081parse_process(const struct option *opt __maybe_unused, const char *arg,
1082 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001083{
1084 if (arg)
1085 add_process_filter(arg);
1086 return 0;
1087}
1088
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001089int cmd_timechart(int argc, const char **argv,
1090 const char *prefix __maybe_unused)
1091{
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001092 const char *output_name = "output.svg";
1093 const struct option options[] = {
1094 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1095 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1096 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1097 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001098 OPT_CALLBACK('p', "process", NULL, "process",
1099 "process selector. Pass a pid or process name.",
1100 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001101 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1102 "Look for files with symbols relative to this directory"),
Arjan van de Ven10274982009-09-12 07:53:05 +02001103 OPT_END()
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001104 };
1105 const char * const timechart_usage[] = {
1106 "perf timechart [<options>] {record}",
1107 NULL
1108 };
Arjan van de Ven10274982009-09-12 07:53:05 +02001109
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001110 argc = parse_options(argc, argv, options, timechart_usage,
1111 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001112
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001113 symbol__init();
1114
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001115 if (argc && !strncmp(argv[0], "rec", 3))
1116 return __cmd_record(argc, argv);
1117 else if (argc)
1118 usage_with_options(timechart_usage, options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001119
1120 setup_pager();
1121
Feng Tang70cb4e92012-10-30 11:56:02 +08001122 return __cmd_timechart(output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001123}