blob: b3f175a30d94d2dcf5f73ffce2219b61ce75c078 [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
Stanislav Fomichev54874e32013-11-01 20:25:46 +040044static int proc_num = 15;
Thomas Renninger20c457b2011-01-03 17:50:45 +010045
Arjan van de Ven10274982009-09-12 07:53:05 +020046static unsigned int numcpus;
47static u64 min_freq; /* Lowest CPU frequency seen */
48static u64 max_freq; /* Highest CPU frequency seen */
49static u64 turbo_frequency;
50
51static u64 first_time, last_time;
52
Ian Munsiec0555642010-04-13 18:37:33 +100053static bool power_only;
Stanislav Fomichevc87097d2013-11-01 20:25:48 +040054static bool tasks_only;
Arjan van de Ven39a90a82009-09-24 15:40:13 +020055
Arjan van de Ven10274982009-09-12 07:53:05 +020056
Arjan van de Ven10274982009-09-12 07:53:05 +020057struct per_pid;
58struct per_pidcomm;
59
60struct cpu_sample;
61struct power_event;
62struct wake_event;
63
64struct sample_wrapper;
65
66/*
67 * Datastructure layout:
68 * We keep an list of "pid"s, matching the kernels notion of a task struct.
69 * Each "pid" entry, has a list of "comm"s.
70 * this is because we want to track different programs different, while
71 * exec will reuse the original pid (by design).
72 * Each comm has a list of samples that will be used to draw
73 * final graph.
74 */
75
76struct per_pid {
77 struct per_pid *next;
78
79 int pid;
80 int ppid;
81
82 u64 start_time;
83 u64 end_time;
84 u64 total_time;
85 int display;
86
87 struct per_pidcomm *all;
88 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +020089};
90
91
92struct per_pidcomm {
93 struct per_pidcomm *next;
94
95 u64 start_time;
96 u64 end_time;
97 u64 total_time;
98
99 int Y;
100 int display;
101
102 long state;
103 u64 state_since;
104
105 char *comm;
106
107 struct cpu_sample *samples;
108};
109
110struct sample_wrapper {
111 struct sample_wrapper *next;
112
113 u64 timestamp;
114 unsigned char data[0];
115};
116
117#define TYPE_NONE 0
118#define TYPE_RUNNING 1
119#define TYPE_WAITING 2
120#define TYPE_BLOCKED 3
121
122struct cpu_sample {
123 struct cpu_sample *next;
124
125 u64 start_time;
126 u64 end_time;
127 int type;
128 int cpu;
129};
130
131static struct per_pid *all_data;
132
133#define CSTATE 1
134#define PSTATE 2
135
136struct power_event {
137 struct power_event *next;
138 int type;
139 int state;
140 u64 start_time;
141 u64 end_time;
142 int cpu;
143};
144
145struct wake_event {
146 struct wake_event *next;
147 int waker;
148 int wakee;
149 u64 time;
150};
151
152static struct power_event *power_events;
153static struct wake_event *wake_events;
154
Arjan van de Venbbe29872009-10-20 07:09:39 +0900155struct process_filter;
156struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800157 char *name;
158 int pid;
159 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900160};
161
162static struct process_filter *process_filter;
163
164
Arjan van de Ven10274982009-09-12 07:53:05 +0200165static struct per_pid *find_create_pid(int pid)
166{
167 struct per_pid *cursor = all_data;
168
169 while (cursor) {
170 if (cursor->pid == pid)
171 return cursor;
172 cursor = cursor->next;
173 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300174 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200175 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200176 cursor->pid = pid;
177 cursor->next = all_data;
178 all_data = cursor;
179 return cursor;
180}
181
182static void pid_set_comm(int pid, char *comm)
183{
184 struct per_pid *p;
185 struct per_pidcomm *c;
186 p = find_create_pid(pid);
187 c = p->all;
188 while (c) {
189 if (c->comm && strcmp(c->comm, comm) == 0) {
190 p->current = c;
191 return;
192 }
193 if (!c->comm) {
194 c->comm = strdup(comm);
195 p->current = c;
196 return;
197 }
198 c = c->next;
199 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300200 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200201 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200202 c->comm = strdup(comm);
203 p->current = c;
204 c->next = p->all;
205 p->all = c;
206}
207
208static void pid_fork(int pid, int ppid, u64 timestamp)
209{
210 struct per_pid *p, *pp;
211 p = find_create_pid(pid);
212 pp = find_create_pid(ppid);
213 p->ppid = ppid;
214 if (pp->current && pp->current->comm && !p->current)
215 pid_set_comm(pid, pp->current->comm);
216
217 p->start_time = timestamp;
218 if (p->current) {
219 p->current->start_time = timestamp;
220 p->current->state_since = timestamp;
221 }
222}
223
224static void pid_exit(int pid, u64 timestamp)
225{
226 struct per_pid *p;
227 p = find_create_pid(pid);
228 p->end_time = timestamp;
229 if (p->current)
230 p->current->end_time = timestamp;
231}
232
233static void
234pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end)
235{
236 struct per_pid *p;
237 struct per_pidcomm *c;
238 struct cpu_sample *sample;
239
240 p = find_create_pid(pid);
241 c = p->current;
242 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300243 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200244 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200245 p->current = c;
246 c->next = p->all;
247 p->all = c;
248 }
249
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300250 sample = zalloc(sizeof(*sample));
Arjan van de Ven10274982009-09-12 07:53:05 +0200251 assert(sample != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200252 sample->start_time = start;
253 sample->end_time = end;
254 sample->type = type;
255 sample->next = c->samples;
256 sample->cpu = cpu;
257 c->samples = sample;
258
259 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
260 c->total_time += (end-start);
261 p->total_time += (end-start);
262 }
263
264 if (c->start_time == 0 || c->start_time > start)
265 c->start_time = start;
266 if (p->start_time == 0 || p->start_time > start)
267 p->start_time = start;
Arjan van de Ven10274982009-09-12 07:53:05 +0200268}
269
270#define MAX_CPUS 4096
271
272static u64 cpus_cstate_start_times[MAX_CPUS];
273static int cpus_cstate_state[MAX_CPUS];
274static u64 cpus_pstate_start_times[MAX_CPUS];
275static u64 cpus_pstate_state[MAX_CPUS];
276
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300277static int process_comm_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200278 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300279 struct perf_sample *sample __maybe_unused,
280 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200281{
Arjan van de Ven8f06d7e2010-01-16 12:53:19 -0800282 pid_set_comm(event->comm.tid, event->comm.comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200283 return 0;
284}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200285
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300286static int process_fork_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200287 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300288 struct perf_sample *sample __maybe_unused,
289 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200290{
291 pid_fork(event->fork.pid, event->fork.ppid, event->fork.time);
292 return 0;
293}
294
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300295static int process_exit_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200296 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300297 struct perf_sample *sample __maybe_unused,
298 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200299{
300 pid_exit(event->fork.pid, event->fork.time);
301 return 0;
302}
303
304struct trace_entry {
Arjan van de Ven10274982009-09-12 07:53:05 +0200305 unsigned short type;
306 unsigned char flags;
307 unsigned char preempt_count;
308 int pid;
OGAWA Hirofumi028c5152009-12-06 20:07:29 +0900309 int lock_depth;
Arjan van de Ven10274982009-09-12 07:53:05 +0200310};
311
Thomas Renninger20c457b2011-01-03 17:50:45 +0100312#ifdef SUPPORT_OLD_POWER_EVENTS
313static int use_old_power_events;
314struct power_entry_old {
Arjan van de Ven10274982009-09-12 07:53:05 +0200315 struct trace_entry te;
Thomas Renninger4c21adf2010-07-20 16:59:34 -0700316 u64 type;
317 u64 value;
318 u64 cpu_id;
Arjan van de Ven10274982009-09-12 07:53:05 +0200319};
Thomas Renninger20c457b2011-01-03 17:50:45 +0100320#endif
321
322struct power_processor_entry {
323 struct trace_entry te;
324 u32 state;
325 u32 cpu_id;
326};
Arjan van de Ven10274982009-09-12 07:53:05 +0200327
328#define TASK_COMM_LEN 16
329struct wakeup_entry {
330 struct trace_entry te;
331 char comm[TASK_COMM_LEN];
332 int pid;
333 int prio;
334 int success;
335};
336
Arjan van de Ven10274982009-09-12 07:53:05 +0200337struct sched_switch {
338 struct trace_entry te;
339 char prev_comm[TASK_COMM_LEN];
340 int prev_pid;
341 int prev_prio;
342 long prev_state; /* Arjan weeps. */
343 char next_comm[TASK_COMM_LEN];
344 int next_pid;
345 int next_prio;
346};
347
348static void c_state_start(int cpu, u64 timestamp, int state)
349{
350 cpus_cstate_start_times[cpu] = timestamp;
351 cpus_cstate_state[cpu] = state;
352}
353
354static void c_state_end(int cpu, u64 timestamp)
355{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300356 struct power_event *pwr = zalloc(sizeof(*pwr));
357
Arjan van de Ven10274982009-09-12 07:53:05 +0200358 if (!pwr)
359 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200360
361 pwr->state = cpus_cstate_state[cpu];
362 pwr->start_time = cpus_cstate_start_times[cpu];
363 pwr->end_time = timestamp;
364 pwr->cpu = cpu;
365 pwr->type = CSTATE;
366 pwr->next = power_events;
367
368 power_events = pwr;
369}
370
371static void p_state_change(int cpu, u64 timestamp, u64 new_freq)
372{
373 struct power_event *pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200374
375 if (new_freq > 8000000) /* detect invalid data */
376 return;
377
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300378 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200379 if (!pwr)
380 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200381
382 pwr->state = cpus_pstate_state[cpu];
383 pwr->start_time = cpus_pstate_start_times[cpu];
384 pwr->end_time = timestamp;
385 pwr->cpu = cpu;
386 pwr->type = PSTATE;
387 pwr->next = power_events;
388
389 if (!pwr->start_time)
390 pwr->start_time = first_time;
391
392 power_events = pwr;
393
394 cpus_pstate_state[cpu] = new_freq;
395 cpus_pstate_start_times[cpu] = timestamp;
396
397 if ((u64)new_freq > max_freq)
398 max_freq = new_freq;
399
400 if (new_freq < min_freq || min_freq == 0)
401 min_freq = new_freq;
402
403 if (new_freq == max_freq - 1000)
404 turbo_frequency = max_freq;
405}
406
407static void
408sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te)
409{
Arjan van de Ven10274982009-09-12 07:53:05 +0200410 struct per_pid *p;
411 struct wakeup_entry *wake = (void *)te;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300412 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200413
Arjan van de Ven10274982009-09-12 07:53:05 +0200414 if (!we)
415 return;
416
Arjan van de Ven10274982009-09-12 07:53:05 +0200417 we->time = timestamp;
418 we->waker = pid;
419
420 if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ))
421 we->waker = -1;
422
423 we->wakee = wake->pid;
424 we->next = wake_events;
425 wake_events = we;
426 p = find_create_pid(we->wakee);
427
428 if (p && p->current && p->current->state == TYPE_NONE) {
429 p->current->state_since = timestamp;
430 p->current->state = TYPE_WAITING;
431 }
432 if (p && p->current && p->current->state == TYPE_BLOCKED) {
433 pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp);
434 p->current->state_since = timestamp;
435 p->current->state = TYPE_WAITING;
436 }
437}
438
439static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te)
440{
441 struct per_pid *p = NULL, *prev_p;
442 struct sched_switch *sw = (void *)te;
443
444
445 prev_p = find_create_pid(sw->prev_pid);
446
447 p = find_create_pid(sw->next_pid);
448
449 if (prev_p->current && prev_p->current->state != TYPE_NONE)
450 pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp);
451 if (p && p->current) {
452 if (p->current->state != TYPE_NONE)
453 pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp);
454
Julia Lawall33e26a12010-08-05 22:27:51 +0200455 p->current->state_since = timestamp;
456 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200457 }
458
459 if (prev_p->current) {
460 prev_p->current->state = TYPE_NONE;
461 prev_p->current->state_since = timestamp;
462 if (sw->prev_state & 2)
463 prev_p->current->state = TYPE_BLOCKED;
464 if (sw->prev_state == 0)
465 prev_p->current->state = TYPE_WAITING;
466 }
467}
468
Jiri Olsa59366782013-07-11 17:28:30 +0200469typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
470 struct perf_sample *sample);
Arjan van de Ven10274982009-09-12 07:53:05 +0200471
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300472static int process_sample_event(struct perf_tool *tool __maybe_unused,
473 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200474 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200475 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300476 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200477{
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200478 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200479 if (!first_time || first_time > sample->time)
480 first_time = sample->time;
481 if (last_time < sample->time)
482 last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200483 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200484
Jiri Olsa59366782013-07-11 17:28:30 +0200485 if (sample->cpu > numcpus)
486 numcpus = sample->cpu;
Arjan van de Ven10274982009-09-12 07:53:05 +0200487
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300488 if (evsel->handler != NULL) {
489 tracepoint_handler f = evsel->handler;
Jiri Olsa59366782013-07-11 17:28:30 +0200490 return f(evsel, sample);
Arjan van de Ven10274982009-09-12 07:53:05 +0200491 }
Jiri Olsa59366782013-07-11 17:28:30 +0200492
Arjan van de Ven10274982009-09-12 07:53:05 +0200493 return 0;
494}
495
Jiri Olsa59366782013-07-11 17:28:30 +0200496static int
497process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused,
498 struct perf_sample *sample)
499{
500 struct power_processor_entry *ppe = sample->raw_data;
501
502 if (ppe->state == (u32) PWR_EVENT_EXIT)
503 c_state_end(ppe->cpu_id, sample->time);
504 else
505 c_state_start(ppe->cpu_id, sample->time, ppe->state);
506 return 0;
507}
508
509static int
510process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused,
511 struct perf_sample *sample)
512{
513 struct power_processor_entry *ppe = sample->raw_data;
514
515 p_state_change(ppe->cpu_id, sample->time, ppe->state);
516 return 0;
517}
518
519static int
520process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused,
521 struct perf_sample *sample)
522{
523 struct trace_entry *te = sample->raw_data;
524
525 sched_wakeup(sample->cpu, sample->time, sample->pid, te);
526 return 0;
527}
528
529static int
530process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused,
531 struct perf_sample *sample)
532{
533 struct trace_entry *te = sample->raw_data;
534
535 sched_switch(sample->cpu, sample->time, te);
536 return 0;
537}
538
539#ifdef SUPPORT_OLD_POWER_EVENTS
540static int
541process_sample_power_start(struct perf_evsel *evsel __maybe_unused,
542 struct perf_sample *sample)
543{
544 struct power_entry_old *peo = sample->raw_data;
545
546 c_state_start(peo->cpu_id, sample->time, peo->value);
547 return 0;
548}
549
550static int
551process_sample_power_end(struct perf_evsel *evsel __maybe_unused,
552 struct perf_sample *sample)
553{
554 c_state_end(sample->cpu, sample->time);
555 return 0;
556}
557
558static int
559process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused,
560 struct perf_sample *sample)
561{
562 struct power_entry_old *peo = sample->raw_data;
563
564 p_state_change(peo->cpu_id, sample->time, peo->value);
565 return 0;
566}
567#endif /* SUPPORT_OLD_POWER_EVENTS */
568
Arjan van de Ven10274982009-09-12 07:53:05 +0200569/*
570 * After the last sample we need to wrap up the current C/P state
571 * and close out each CPU for these.
572 */
573static void end_sample_processing(void)
574{
575 u64 cpu;
576 struct power_event *pwr;
577
Arjan van de Ven39a90a82009-09-24 15:40:13 +0200578 for (cpu = 0; cpu <= numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200579 /* C state */
580#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300581 pwr = zalloc(sizeof(*pwr));
582 if (!pwr)
583 return;
584
Arjan van de Ven10274982009-09-12 07:53:05 +0200585 pwr->state = cpus_cstate_state[cpu];
586 pwr->start_time = cpus_cstate_start_times[cpu];
587 pwr->end_time = last_time;
588 pwr->cpu = cpu;
589 pwr->type = CSTATE;
590 pwr->next = power_events;
591
592 power_events = pwr;
593#endif
594 /* P state */
595
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300596 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200597 if (!pwr)
598 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200599
600 pwr->state = cpus_pstate_state[cpu];
601 pwr->start_time = cpus_pstate_start_times[cpu];
602 pwr->end_time = last_time;
603 pwr->cpu = cpu;
604 pwr->type = PSTATE;
605 pwr->next = power_events;
606
607 if (!pwr->start_time)
608 pwr->start_time = first_time;
609 if (!pwr->state)
610 pwr->state = min_freq;
611 power_events = pwr;
612 }
613}
614
Arjan van de Ven10274982009-09-12 07:53:05 +0200615/*
616 * Sort the pid datastructure
617 */
618static void sort_pids(void)
619{
620 struct per_pid *new_list, *p, *cursor, *prev;
621 /* sort by ppid first, then by pid, lowest to highest */
622
623 new_list = NULL;
624
625 while (all_data) {
626 p = all_data;
627 all_data = p->next;
628 p->next = NULL;
629
630 if (new_list == NULL) {
631 new_list = p;
632 p->next = NULL;
633 continue;
634 }
635 prev = NULL;
636 cursor = new_list;
637 while (cursor) {
638 if (cursor->ppid > p->ppid ||
639 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
640 /* must insert before */
641 if (prev) {
642 p->next = prev->next;
643 prev->next = p;
644 cursor = NULL;
645 continue;
646 } else {
647 p->next = new_list;
648 new_list = p;
649 cursor = NULL;
650 continue;
651 }
652 }
653
654 prev = cursor;
655 cursor = cursor->next;
656 if (!cursor)
657 prev->next = p;
658 }
659 }
660 all_data = new_list;
661}
662
663
664static void draw_c_p_states(void)
665{
666 struct power_event *pwr;
667 pwr = power_events;
668
669 /*
670 * two pass drawing so that the P state bars are on top of the C state blocks
671 */
672 while (pwr) {
673 if (pwr->type == CSTATE)
674 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
675 pwr = pwr->next;
676 }
677
678 pwr = power_events;
679 while (pwr) {
680 if (pwr->type == PSTATE) {
681 if (!pwr->state)
682 pwr->state = min_freq;
683 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
684 }
685 pwr = pwr->next;
686 }
687}
688
689static void draw_wakeups(void)
690{
691 struct wake_event *we;
692 struct per_pid *p;
693 struct per_pidcomm *c;
694
695 we = wake_events;
696 while (we) {
697 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200698 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +0200699
700 /* locate the column of the waker and wakee */
701 p = all_data;
702 while (p) {
703 if (p->pid == we->waker || p->pid == we->wakee) {
704 c = p->all;
705 while (c) {
706 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +0900707 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200708 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900709 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200710 }
Arjan van de Venbbe29872009-10-20 07:09:39 +0900711 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200712 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900713 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200714 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200715 }
716 c = c->next;
717 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900718 c = p->all;
719 while (c) {
720 if (p->pid == we->waker && !from) {
721 from = c->Y;
722 task_from = strdup(c->comm);
723 }
724 if (p->pid == we->wakee && !to) {
725 to = c->Y;
726 task_to = strdup(c->comm);
727 }
728 c = c->next;
729 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200730 }
731 p = p->next;
732 }
733
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900734 if (!task_from) {
735 task_from = malloc(40);
736 sprintf(task_from, "[%i]", we->waker);
737 }
738 if (!task_to) {
739 task_to = malloc(40);
740 sprintf(task_to, "[%i]", we->wakee);
741 }
742
Arjan van de Ven10274982009-09-12 07:53:05 +0200743 if (we->waker == -1)
744 svg_interrupt(we->time, to);
745 else if (from && to && abs(from - to) == 1)
746 svg_wakeline(we->time, from, to);
747 else
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200748 svg_partial_wakeline(we->time, from, task_from, to, task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200749 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +0900750
751 free(task_from);
752 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +0200753 }
754}
755
756static void draw_cpu_usage(void)
757{
758 struct per_pid *p;
759 struct per_pidcomm *c;
760 struct cpu_sample *sample;
761 p = all_data;
762 while (p) {
763 c = p->all;
764 while (c) {
765 sample = c->samples;
766 while (sample) {
767 if (sample->type == TYPE_RUNNING)
768 svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm);
769
770 sample = sample->next;
771 }
772 c = c->next;
773 }
774 p = p->next;
775 }
776}
777
778static void draw_process_bars(void)
779{
780 struct per_pid *p;
781 struct per_pidcomm *c;
782 struct cpu_sample *sample;
783 int Y = 0;
784
785 Y = 2 * numcpus + 2;
786
787 p = all_data;
788 while (p) {
789 c = p->all;
790 while (c) {
791 if (!c->display) {
792 c->Y = 0;
793 c = c->next;
794 continue;
795 }
796
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200797 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +0200798 sample = c->samples;
799 while (sample) {
800 if (sample->type == TYPE_RUNNING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200801 svg_sample(Y, sample->cpu, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200802 if (sample->type == TYPE_BLOCKED)
803 svg_box(Y, sample->start_time, sample->end_time, "blocked");
804 if (sample->type == TYPE_WAITING)
Arjan van de Vena92fe7b2009-09-20 18:13:53 +0200805 svg_waiting(Y, sample->start_time, sample->end_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200806 sample = sample->next;
807 }
808
809 if (c->comm) {
810 char comm[256];
811 if (c->total_time > 5000000000) /* 5 seconds */
812 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
813 else
814 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
815
816 svg_text(Y, c->start_time, comm);
817 }
818 c->Y = Y;
819 Y++;
820 c = c->next;
821 }
822 p = p->next;
823 }
824}
825
Arjan van de Venbbe29872009-10-20 07:09:39 +0900826static void add_process_filter(const char *string)
827{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300828 int pid = strtoull(string, NULL, 10);
829 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +0900830
Arjan van de Venbbe29872009-10-20 07:09:39 +0900831 if (!filt)
832 return;
833
834 filt->name = strdup(string);
835 filt->pid = pid;
836 filt->next = process_filter;
837
838 process_filter = filt;
839}
840
841static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
842{
843 struct process_filter *filt;
844 if (!process_filter)
845 return 1;
846
847 filt = process_filter;
848 while (filt) {
849 if (filt->pid && p->pid == filt->pid)
850 return 1;
851 if (strcmp(filt->name, c->comm) == 0)
852 return 1;
853 filt = filt->next;
854 }
855 return 0;
856}
857
858static int determine_display_tasks_filtered(void)
859{
860 struct per_pid *p;
861 struct per_pidcomm *c;
862 int count = 0;
863
864 p = all_data;
865 while (p) {
866 p->display = 0;
867 if (p->start_time == 1)
868 p->start_time = first_time;
869
870 /* no exit marker, task kept running to the end */
871 if (p->end_time == 0)
872 p->end_time = last_time;
873
874 c = p->all;
875
876 while (c) {
877 c->display = 0;
878
879 if (c->start_time == 1)
880 c->start_time = first_time;
881
882 if (passes_filter(p, c)) {
883 c->display = 1;
884 p->display = 1;
885 count++;
886 }
887
888 if (c->end_time == 0)
889 c->end_time = last_time;
890
891 c = c->next;
892 }
893 p = p->next;
894 }
895 return count;
896}
897
Arjan van de Ven10274982009-09-12 07:53:05 +0200898static int determine_display_tasks(u64 threshold)
899{
900 struct per_pid *p;
901 struct per_pidcomm *c;
902 int count = 0;
903
Arjan van de Venbbe29872009-10-20 07:09:39 +0900904 if (process_filter)
905 return determine_display_tasks_filtered();
906
Arjan van de Ven10274982009-09-12 07:53:05 +0200907 p = all_data;
908 while (p) {
909 p->display = 0;
910 if (p->start_time == 1)
911 p->start_time = first_time;
912
913 /* no exit marker, task kept running to the end */
914 if (p->end_time == 0)
915 p->end_time = last_time;
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400916 if (p->total_time >= threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +0200917 p->display = 1;
918
919 c = p->all;
920
921 while (c) {
922 c->display = 0;
923
924 if (c->start_time == 1)
925 c->start_time = first_time;
926
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400927 if (c->total_time >= threshold) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200928 c->display = 1;
929 count++;
930 }
931
932 if (c->end_time == 0)
933 c->end_time = last_time;
934
935 c = c->next;
936 }
937 p = p->next;
938 }
939 return count;
940}
941
942
943
944#define TIME_THRESH 10000000
945
946static void write_svg_file(const char *filename)
947{
948 u64 i;
949 int count;
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +0400950 int thresh = TIME_THRESH;
Arjan van de Ven10274982009-09-12 07:53:05 +0200951
952 numcpus++;
953
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400954 if (power_only)
955 proc_num = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +0200956
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +0400957 /* We'd like to show at least proc_num tasks;
958 * be less picky if we have fewer */
959 do {
960 count = determine_display_tasks(thresh);
961 thresh /= 10;
Stanislav Fomichev54874e32013-11-01 20:25:46 +0400962 } while (!process_filter && thresh && count < proc_num);
Arjan van de Ven10274982009-09-12 07:53:05 +0200963
Arjan van de Ven5094b652009-09-20 18:14:16 +0200964 open_svg(filename, numcpus, count, first_time, last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200965
Arjan van de Ven5094b652009-09-20 18:14:16 +0200966 svg_time_grid();
Arjan van de Ven10274982009-09-12 07:53:05 +0200967 svg_legenda();
968
969 for (i = 0; i < numcpus; i++)
970 svg_cpu_box(i, max_freq, turbo_frequency);
971
972 draw_cpu_usage();
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400973 if (proc_num)
974 draw_process_bars();
Stanislav Fomichevc87097d2013-11-01 20:25:48 +0400975 if (!tasks_only)
976 draw_c_p_states();
Stanislav Fomichev753c5052013-11-01 20:25:47 +0400977 if (proc_num)
978 draw_wakeups();
Arjan van de Ven10274982009-09-12 07:53:05 +0200979
980 svg_close();
981}
982
Feng Tang70cb4e92012-10-30 11:56:02 +0800983static int __cmd_timechart(const char *output_name)
Arjan van de Ven10274982009-09-12 07:53:05 +0200984{
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -0300985 struct perf_tool perf_timechart = {
986 .comm = process_comm_event,
987 .fork = process_fork_event,
988 .exit = process_exit_event,
989 .sample = process_sample_event,
990 .ordered_samples = true,
991 };
Jiri Olsa59366782013-07-11 17:28:30 +0200992 const struct perf_evsel_str_handler power_tracepoints[] = {
993 { "power:cpu_idle", process_sample_cpu_idle },
994 { "power:cpu_frequency", process_sample_cpu_frequency },
995 { "sched:sched_wakeup", process_sample_sched_wakeup },
996 { "sched:sched_switch", process_sample_sched_switch },
997#ifdef SUPPORT_OLD_POWER_EVENTS
998 { "power:power_start", process_sample_power_start },
999 { "power:power_end", process_sample_power_end },
1000 { "power:power_frequency", process_sample_power_frequency },
1001#endif
1002 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001003 struct perf_data_file file = {
1004 .path = input_name,
1005 .mode = PERF_DATA_MODE_READ,
1006 };
1007
1008 struct perf_session *session = perf_session__new(&file, false,
1009 &perf_timechart);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001010 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001011
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001012 if (session == NULL)
1013 return -ENOMEM;
1014
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001015 if (!perf_session__has_traces(session, "timechart record"))
1016 goto out_delete;
1017
Jiri Olsa59366782013-07-11 17:28:30 +02001018 if (perf_session__set_tracepoints_handlers(session,
1019 power_tracepoints)) {
1020 pr_err("Initializing session tracepoint handlers failed\n");
1021 goto out_delete;
1022 }
1023
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001024 ret = perf_session__process_events(session, &perf_timechart);
Li Zefan5cbd0802009-12-01 14:05:16 +08001025 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001026 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001027
Arjan van de Ven10274982009-09-12 07:53:05 +02001028 end_sample_processing();
1029
1030 sort_pids();
1031
1032 write_svg_file(output_name);
1033
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001034 pr_info("Written %2.1f seconds of trace to %s.\n",
1035 (last_time - first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001036out_delete:
1037 perf_session__delete(session);
1038 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001039}
1040
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001041static int __cmd_record(int argc, const char **argv)
1042{
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001043#ifdef SUPPORT_OLD_POWER_EVENTS
1044 const char * const record_old_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001045 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001046 "-e", "power:power_start",
1047 "-e", "power:power_end",
1048 "-e", "power:power_frequency",
1049 "-e", "sched:sched_wakeup",
1050 "-e", "sched:sched_switch",
1051 };
1052#endif
1053 const char * const record_new_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001054 "record", "-a", "-R", "-c", "1",
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001055 "-e", "power:cpu_frequency",
1056 "-e", "power:cpu_idle",
1057 "-e", "sched:sched_wakeup",
1058 "-e", "sched:sched_switch",
1059 };
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001060 unsigned int rec_argc, i, j;
1061 const char **rec_argv;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001062 const char * const *record_args = record_new_args;
1063 unsigned int record_elems = ARRAY_SIZE(record_new_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001064
Thomas Renninger20c457b2011-01-03 17:50:45 +01001065#ifdef SUPPORT_OLD_POWER_EVENTS
1066 if (!is_valid_tracepoint("power:cpu_idle") &&
1067 is_valid_tracepoint("power:power_start")) {
1068 use_old_power_events = 1;
1069 record_args = record_old_args;
1070 record_elems = ARRAY_SIZE(record_old_args);
1071 }
1072#endif
1073
1074 rec_argc = record_elems + argc - 1;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001075 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1076
Chris Samuelce47dc52010-11-13 13:35:06 +11001077 if (rec_argv == NULL)
1078 return -ENOMEM;
1079
Thomas Renninger20c457b2011-01-03 17:50:45 +01001080 for (i = 0; i < record_elems; i++)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001081 rec_argv[i] = strdup(record_args[i]);
1082
1083 for (j = 1; j < (unsigned int)argc; j++, i++)
1084 rec_argv[i] = argv[j];
1085
1086 return cmd_record(i, rec_argv, NULL);
1087}
1088
Arjan van de Venbbe29872009-10-20 07:09:39 +09001089static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001090parse_process(const struct option *opt __maybe_unused, const char *arg,
1091 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001092{
1093 if (arg)
1094 add_process_filter(arg);
1095 return 0;
1096}
1097
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001098int cmd_timechart(int argc, const char **argv,
1099 const char *prefix __maybe_unused)
1100{
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001101 const char *output_name = "output.svg";
1102 const struct option options[] = {
1103 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1104 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1105 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
1106 OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"),
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001107 OPT_BOOLEAN('T', "tasks-only", &tasks_only,
1108 "output processes data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001109 OPT_CALLBACK('p', "process", NULL, "process",
1110 "process selector. Pass a pid or process name.",
1111 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001112 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1113 "Look for files with symbols relative to this directory"),
Stanislav Fomichev54874e32013-11-01 20:25:46 +04001114 OPT_INTEGER('n', "proc-num", &proc_num,
1115 "min. number of tasks to print"),
Arjan van de Ven10274982009-09-12 07:53:05 +02001116 OPT_END()
Arnaldo Carvalho de Melo73bdc7152012-10-01 15:20:58 -03001117 };
1118 const char * const timechart_usage[] = {
1119 "perf timechart [<options>] {record}",
1120 NULL
1121 };
Arjan van de Ven10274982009-09-12 07:53:05 +02001122
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001123 argc = parse_options(argc, argv, options, timechart_usage,
1124 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001125
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001126 if (power_only && tasks_only) {
1127 pr_err("-P and -T options cannot be used at the same time.\n");
1128 return -1;
1129 }
1130
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001131 symbol__init();
1132
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001133 if (argc && !strncmp(argv[0], "rec", 3))
1134 return __cmd_record(argc, argv);
1135 else if (argc)
1136 usage_with_options(timechart_usage, options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001137
1138 setup_pager();
1139
Feng Tang70cb4e92012-10-30 11:56:02 +08001140 return __cmd_timechart(output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001141}