blob: 4079062d25b0e3692f68a7442cb9e3ec1f9f9123 [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 Olsaf5fc14122013-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 Melo5e22f6d2013-11-28 11:50:41 -030044struct per_pid;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -030045struct power_event;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -030046struct wake_event;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -030047
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030048struct timechart {
49 struct perf_tool tool;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -030050 struct per_pid *all_data;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -030051 struct power_event *power_events;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -030052 struct wake_event *wake_events;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030053 int proc_num;
54 unsigned int numcpus;
55 u64 min_freq, /* Lowest CPU frequency seen */
56 max_freq, /* Highest CPU frequency seen */
57 turbo_frequency,
58 first_time, last_time;
59 bool power_only,
60 tasks_only,
Stanislav Fomichevc5079992013-12-02 18:37:36 +040061 with_backtrace,
62 topology;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040063 /* IO related settings */
64 u64 io_events;
65 bool io_only;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030066};
Arjan van de Ven10274982009-09-12 07:53:05 +020067
Arjan van de Ven10274982009-09-12 07:53:05 +020068struct per_pidcomm;
Arjan van de Ven10274982009-09-12 07:53:05 +020069struct cpu_sample;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040070struct io_sample;
Arjan van de Ven10274982009-09-12 07:53:05 +020071
72/*
73 * Datastructure layout:
74 * We keep an list of "pid"s, matching the kernels notion of a task struct.
75 * Each "pid" entry, has a list of "comm"s.
76 * this is because we want to track different programs different, while
77 * exec will reuse the original pid (by design).
78 * Each comm has a list of samples that will be used to draw
79 * final graph.
80 */
81
82struct per_pid {
83 struct per_pid *next;
84
85 int pid;
86 int ppid;
87
88 u64 start_time;
89 u64 end_time;
90 u64 total_time;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040091 u64 total_bytes;
Arjan van de Ven10274982009-09-12 07:53:05 +020092 int display;
93
94 struct per_pidcomm *all;
95 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +020096};
97
98
99struct per_pidcomm {
100 struct per_pidcomm *next;
101
102 u64 start_time;
103 u64 end_time;
104 u64 total_time;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400105 u64 max_bytes;
106 u64 total_bytes;
Arjan van de Ven10274982009-09-12 07:53:05 +0200107
108 int Y;
109 int display;
110
111 long state;
112 u64 state_since;
113
114 char *comm;
115
116 struct cpu_sample *samples;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400117 struct io_sample *io_samples;
Arjan van de Ven10274982009-09-12 07:53:05 +0200118};
119
120struct sample_wrapper {
121 struct sample_wrapper *next;
122
123 u64 timestamp;
124 unsigned char data[0];
125};
126
127#define TYPE_NONE 0
128#define TYPE_RUNNING 1
129#define TYPE_WAITING 2
130#define TYPE_BLOCKED 3
131
132struct cpu_sample {
133 struct cpu_sample *next;
134
135 u64 start_time;
136 u64 end_time;
137 int type;
138 int cpu;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400139 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200140};
141
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400142enum {
143 IOTYPE_READ,
144 IOTYPE_WRITE,
145 IOTYPE_SYNC,
146 IOTYPE_TX,
147 IOTYPE_RX,
148 IOTYPE_POLL,
149};
150
151struct io_sample {
152 struct io_sample *next;
153
154 u64 start_time;
155 u64 end_time;
156 u64 bytes;
157 int type;
158 int fd;
159 int err;
160 int merges;
161};
162
Arjan van de Ven10274982009-09-12 07:53:05 +0200163#define CSTATE 1
164#define PSTATE 2
165
166struct power_event {
167 struct power_event *next;
168 int type;
169 int state;
170 u64 start_time;
171 u64 end_time;
172 int cpu;
173};
174
175struct wake_event {
176 struct wake_event *next;
177 int waker;
178 int wakee;
179 u64 time;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400180 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200181};
182
Arjan van de Venbbe29872009-10-20 07:09:39 +0900183struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800184 char *name;
185 int pid;
186 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900187};
188
189static struct process_filter *process_filter;
190
191
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300192static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
Arjan van de Ven10274982009-09-12 07:53:05 +0200193{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300194 struct per_pid *cursor = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +0200195
196 while (cursor) {
197 if (cursor->pid == pid)
198 return cursor;
199 cursor = cursor->next;
200 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300201 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200202 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200203 cursor->pid = pid;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300204 cursor->next = tchart->all_data;
205 tchart->all_data = cursor;
Arjan van de Ven10274982009-09-12 07:53:05 +0200206 return cursor;
207}
208
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300209static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
Arjan van de Ven10274982009-09-12 07:53:05 +0200210{
211 struct per_pid *p;
212 struct per_pidcomm *c;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300213 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200214 c = p->all;
215 while (c) {
216 if (c->comm && strcmp(c->comm, comm) == 0) {
217 p->current = c;
218 return;
219 }
220 if (!c->comm) {
221 c->comm = strdup(comm);
222 p->current = c;
223 return;
224 }
225 c = c->next;
226 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300227 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200228 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200229 c->comm = strdup(comm);
230 p->current = c;
231 c->next = p->all;
232 p->all = c;
233}
234
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300235static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200236{
237 struct per_pid *p, *pp;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300238 p = find_create_pid(tchart, pid);
239 pp = find_create_pid(tchart, ppid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200240 p->ppid = ppid;
241 if (pp->current && pp->current->comm && !p->current)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300242 pid_set_comm(tchart, pid, pp->current->comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200243
244 p->start_time = timestamp;
245 if (p->current) {
246 p->current->start_time = timestamp;
247 p->current->state_since = timestamp;
248 }
249}
250
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300251static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200252{
253 struct per_pid *p;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300254 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200255 p->end_time = timestamp;
256 if (p->current)
257 p->current->end_time = timestamp;
258}
259
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300260static void pid_put_sample(struct timechart *tchart, int pid, int type,
261 unsigned int cpu, u64 start, u64 end,
262 const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200263{
264 struct per_pid *p;
265 struct per_pidcomm *c;
266 struct cpu_sample *sample;
267
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300268 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200269 c = p->current;
270 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300271 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200272 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200273 p->current = c;
274 c->next = p->all;
275 p->all = c;
276 }
277
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300278 sample = zalloc(sizeof(*sample));
Arjan van de Ven10274982009-09-12 07:53:05 +0200279 assert(sample != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200280 sample->start_time = start;
281 sample->end_time = end;
282 sample->type = type;
283 sample->next = c->samples;
284 sample->cpu = cpu;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400285 sample->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200286 c->samples = sample;
287
288 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
289 c->total_time += (end-start);
290 p->total_time += (end-start);
291 }
292
293 if (c->start_time == 0 || c->start_time > start)
294 c->start_time = start;
295 if (p->start_time == 0 || p->start_time > start)
296 p->start_time = start;
Arjan van de Ven10274982009-09-12 07:53:05 +0200297}
298
299#define MAX_CPUS 4096
300
301static u64 cpus_cstate_start_times[MAX_CPUS];
302static int cpus_cstate_state[MAX_CPUS];
303static u64 cpus_pstate_start_times[MAX_CPUS];
304static u64 cpus_pstate_state[MAX_CPUS];
305
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300306static int process_comm_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200307 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300308 struct perf_sample *sample __maybe_unused,
309 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200310{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300311 struct timechart *tchart = container_of(tool, struct timechart, tool);
312 pid_set_comm(tchart, event->comm.tid, event->comm.comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200313 return 0;
314}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200315
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300316static int process_fork_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200317 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300318 struct perf_sample *sample __maybe_unused,
319 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200320{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300321 struct timechart *tchart = container_of(tool, struct timechart, tool);
322 pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200323 return 0;
324}
325
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300326static int process_exit_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200327 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300328 struct perf_sample *sample __maybe_unused,
329 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200330{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300331 struct timechart *tchart = container_of(tool, struct timechart, tool);
332 pid_exit(tchart, event->fork.pid, event->fork.time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200333 return 0;
334}
335
Thomas Renninger20c457b2011-01-03 17:50:45 +0100336#ifdef SUPPORT_OLD_POWER_EVENTS
337static int use_old_power_events;
Thomas Renninger20c457b2011-01-03 17:50:45 +0100338#endif
339
Arjan van de Ven10274982009-09-12 07:53:05 +0200340static void c_state_start(int cpu, u64 timestamp, int state)
341{
342 cpus_cstate_start_times[cpu] = timestamp;
343 cpus_cstate_state[cpu] = state;
344}
345
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300346static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200347{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300348 struct power_event *pwr = zalloc(sizeof(*pwr));
349
Arjan van de Ven10274982009-09-12 07:53:05 +0200350 if (!pwr)
351 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200352
353 pwr->state = cpus_cstate_state[cpu];
354 pwr->start_time = cpus_cstate_start_times[cpu];
355 pwr->end_time = timestamp;
356 pwr->cpu = cpu;
357 pwr->type = CSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300358 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200359
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300360 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200361}
362
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300363static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
Arjan van de Ven10274982009-09-12 07:53:05 +0200364{
365 struct power_event *pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200366
367 if (new_freq > 8000000) /* detect invalid data */
368 return;
369
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300370 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200371 if (!pwr)
372 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200373
374 pwr->state = cpus_pstate_state[cpu];
375 pwr->start_time = cpus_pstate_start_times[cpu];
376 pwr->end_time = timestamp;
377 pwr->cpu = cpu;
378 pwr->type = PSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300379 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200380
381 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300382 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200383
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300384 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200385
386 cpus_pstate_state[cpu] = new_freq;
387 cpus_pstate_start_times[cpu] = timestamp;
388
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300389 if ((u64)new_freq > tchart->max_freq)
390 tchart->max_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200391
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300392 if (new_freq < tchart->min_freq || tchart->min_freq == 0)
393 tchart->min_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200394
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300395 if (new_freq == tchart->max_freq - 1000)
396 tchart->turbo_frequency = tchart->max_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200397}
398
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300399static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
400 int waker, int wakee, u8 flags, const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200401{
Arjan van de Ven10274982009-09-12 07:53:05 +0200402 struct per_pid *p;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300403 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200404
Arjan van de Ven10274982009-09-12 07:53:05 +0200405 if (!we)
406 return;
407
Arjan van de Ven10274982009-09-12 07:53:05 +0200408 we->time = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400409 we->waker = waker;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400410 we->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200411
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400412 if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
Arjan van de Ven10274982009-09-12 07:53:05 +0200413 we->waker = -1;
414
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400415 we->wakee = wakee;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -0300416 we->next = tchart->wake_events;
417 tchart->wake_events = we;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300418 p = find_create_pid(tchart, we->wakee);
Arjan van de Ven10274982009-09-12 07:53:05 +0200419
420 if (p && p->current && p->current->state == TYPE_NONE) {
421 p->current->state_since = timestamp;
422 p->current->state = TYPE_WAITING;
423 }
424 if (p && p->current && p->current->state == TYPE_BLOCKED) {
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300425 pid_put_sample(tchart, p->pid, p->current->state, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400426 p->current->state_since, timestamp, NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200427 p->current->state_since = timestamp;
428 p->current->state = TYPE_WAITING;
429 }
430}
431
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300432static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
433 int prev_pid, int next_pid, u64 prev_state,
434 const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200435{
436 struct per_pid *p = NULL, *prev_p;
Arjan van de Ven10274982009-09-12 07:53:05 +0200437
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300438 prev_p = find_create_pid(tchart, prev_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200439
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300440 p = find_create_pid(tchart, next_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200441
442 if (prev_p->current && prev_p->current->state != TYPE_NONE)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300443 pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400444 prev_p->current->state_since, timestamp,
445 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200446 if (p && p->current) {
447 if (p->current->state != TYPE_NONE)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300448 pid_put_sample(tchart, next_pid, p->current->state, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400449 p->current->state_since, timestamp,
450 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200451
Julia Lawall33e26a12010-08-05 22:27:51 +0200452 p->current->state_since = timestamp;
453 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200454 }
455
456 if (prev_p->current) {
457 prev_p->current->state = TYPE_NONE;
458 prev_p->current->state_since = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400459 if (prev_state & 2)
Arjan van de Ven10274982009-09-12 07:53:05 +0200460 prev_p->current->state = TYPE_BLOCKED;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400461 if (prev_state == 0)
Arjan van de Ven10274982009-09-12 07:53:05 +0200462 prev_p->current->state = TYPE_WAITING;
463 }
464}
465
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400466static const char *cat_backtrace(union perf_event *event,
467 struct perf_sample *sample,
468 struct machine *machine)
469{
470 struct addr_location al;
471 unsigned int i;
472 char *p = NULL;
473 size_t p_len;
474 u8 cpumode = PERF_RECORD_MISC_USER;
475 struct addr_location tal;
476 struct ip_callchain *chain = sample->callchain;
477 FILE *f = open_memstream(&p, &p_len);
478
479 if (!f) {
480 perror("open_memstream error");
481 return NULL;
482 }
483
484 if (!chain)
485 goto exit;
486
487 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
488 fprintf(stderr, "problem processing %d event, skipping it.\n",
489 event->header.type);
490 goto exit;
491 }
492
493 for (i = 0; i < chain->nr; i++) {
494 u64 ip;
495
496 if (callchain_param.order == ORDER_CALLEE)
497 ip = chain->ips[i];
498 else
499 ip = chain->ips[chain->nr - i - 1];
500
501 if (ip >= PERF_CONTEXT_MAX) {
502 switch (ip) {
503 case PERF_CONTEXT_HV:
504 cpumode = PERF_RECORD_MISC_HYPERVISOR;
505 break;
506 case PERF_CONTEXT_KERNEL:
507 cpumode = PERF_RECORD_MISC_KERNEL;
508 break;
509 case PERF_CONTEXT_USER:
510 cpumode = PERF_RECORD_MISC_USER;
511 break;
512 default:
513 pr_debug("invalid callchain context: "
514 "%"PRId64"\n", (s64) ip);
515
516 /*
517 * It seems the callchain is corrupted.
518 * Discard all.
519 */
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300520 zfree(&p);
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400521 goto exit;
522 }
523 continue;
524 }
525
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300526 tal.filtered = 0;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400527 thread__find_addr_location(al.thread, machine, cpumode,
528 MAP__FUNCTION, ip, &tal);
529
530 if (tal.sym)
531 fprintf(f, "..... %016" PRIx64 " %s\n", ip,
532 tal.sym->name);
533 else
534 fprintf(f, "..... %016" PRIx64 "\n", ip);
535 }
536
537exit:
538 fclose(f);
539
540 return p;
541}
542
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300543typedef int (*tracepoint_handler)(struct timechart *tchart,
544 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400545 struct perf_sample *sample,
546 const char *backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200547
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300548static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo972ec652013-11-27 16:32:56 -0300549 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200550 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200551 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300552 struct machine *machine)
Arjan van de Ven10274982009-09-12 07:53:05 +0200553{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300554 struct timechart *tchart = container_of(tool, struct timechart, tool);
555
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200556 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300557 if (!tchart->first_time || tchart->first_time > sample->time)
558 tchart->first_time = sample->time;
559 if (tchart->last_time < sample->time)
560 tchart->last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200561 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200562
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300563 if (evsel->handler != NULL) {
564 tracepoint_handler f = evsel->handler;
Stanislav Fomichev58b9a182013-12-02 18:37:35 +0400565 return f(tchart, evsel, sample,
566 cat_backtrace(event, sample, machine));
Arjan van de Ven10274982009-09-12 07:53:05 +0200567 }
Jiri Olsa59366782013-07-11 17:28:30 +0200568
Arjan van de Ven10274982009-09-12 07:53:05 +0200569 return 0;
570}
571
Jiri Olsa59366782013-07-11 17:28:30 +0200572static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300573process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
574 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400575 struct perf_sample *sample,
576 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200577{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400578 u32 state = perf_evsel__intval(evsel, sample, "state");
579 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200580
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400581 if (state == (u32)PWR_EVENT_EXIT)
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300582 c_state_end(tchart, cpu_id, sample->time);
Jiri Olsa59366782013-07-11 17:28:30 +0200583 else
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400584 c_state_start(cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200585 return 0;
586}
587
588static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300589process_sample_cpu_frequency(struct timechart *tchart,
590 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400591 struct perf_sample *sample,
592 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200593{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400594 u32 state = perf_evsel__intval(evsel, sample, "state");
595 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200596
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300597 p_state_change(tchart, cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200598 return 0;
599}
600
601static int
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300602process_sample_sched_wakeup(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300603 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400604 struct perf_sample *sample,
605 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200606{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400607 u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
608 int waker = perf_evsel__intval(evsel, sample, "common_pid");
609 int wakee = perf_evsel__intval(evsel, sample, "pid");
Jiri Olsa59366782013-07-11 17:28:30 +0200610
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300611 sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200612 return 0;
613}
614
615static int
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300616process_sample_sched_switch(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300617 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400618 struct perf_sample *sample,
619 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200620{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400621 int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
622 int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
623 u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Jiri Olsa59366782013-07-11 17:28:30 +0200624
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300625 sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
626 prev_state, backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200627 return 0;
628}
629
630#ifdef SUPPORT_OLD_POWER_EVENTS
631static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300632process_sample_power_start(struct timechart *tchart __maybe_unused,
633 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400634 struct perf_sample *sample,
635 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200636{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400637 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
638 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200639
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400640 c_state_start(cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200641 return 0;
642}
643
644static int
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300645process_sample_power_end(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300646 struct perf_evsel *evsel __maybe_unused,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400647 struct perf_sample *sample,
648 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200649{
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300650 c_state_end(tchart, sample->cpu, sample->time);
Jiri Olsa59366782013-07-11 17:28:30 +0200651 return 0;
652}
653
654static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300655process_sample_power_frequency(struct timechart *tchart,
656 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400657 struct perf_sample *sample,
658 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200659{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400660 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
661 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200662
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300663 p_state_change(tchart, cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200664 return 0;
665}
666#endif /* SUPPORT_OLD_POWER_EVENTS */
667
Arjan van de Ven10274982009-09-12 07:53:05 +0200668/*
669 * After the last sample we need to wrap up the current C/P state
670 * and close out each CPU for these.
671 */
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300672static void end_sample_processing(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200673{
674 u64 cpu;
675 struct power_event *pwr;
676
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300677 for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200678 /* C state */
679#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300680 pwr = zalloc(sizeof(*pwr));
681 if (!pwr)
682 return;
683
Arjan van de Ven10274982009-09-12 07:53:05 +0200684 pwr->state = cpus_cstate_state[cpu];
685 pwr->start_time = cpus_cstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300686 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200687 pwr->cpu = cpu;
688 pwr->type = CSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300689 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200690
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300691 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200692#endif
693 /* P state */
694
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300695 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200696 if (!pwr)
697 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200698
699 pwr->state = cpus_pstate_state[cpu];
700 pwr->start_time = cpus_pstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300701 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200702 pwr->cpu = cpu;
703 pwr->type = PSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300704 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200705
706 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300707 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200708 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300709 pwr->state = tchart->min_freq;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300710 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200711 }
712}
713
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400714static int pid_begin_io_sample(struct timechart *tchart, int pid, int type,
715 u64 start, int fd)
716{
717 struct per_pid *p = find_create_pid(tchart, pid);
718 struct per_pidcomm *c = p->current;
719 struct io_sample *sample;
720 struct io_sample *prev;
721
722 if (!c) {
723 c = zalloc(sizeof(*c));
724 if (!c)
725 return -ENOMEM;
726 p->current = c;
727 c->next = p->all;
728 p->all = c;
729 }
730
731 prev = c->io_samples;
732
733 if (prev && prev->start_time && !prev->end_time) {
734 pr_warning("Skip invalid start event: "
735 "previous event already started!\n");
736
737 /* remove previous event that has been started,
738 * we are not sure we will ever get an end for it */
739 c->io_samples = prev->next;
740 free(prev);
741 return 0;
742 }
743
744 sample = zalloc(sizeof(*sample));
745 if (!sample)
746 return -ENOMEM;
747 sample->start_time = start;
748 sample->type = type;
749 sample->fd = fd;
750 sample->next = c->io_samples;
751 c->io_samples = sample;
752
753 if (c->start_time == 0 || c->start_time > start)
754 c->start_time = start;
755
756 return 0;
757}
758
759static int pid_end_io_sample(struct timechart *tchart, int pid, int type,
760 u64 end, long ret)
761{
762 struct per_pid *p = find_create_pid(tchart, pid);
763 struct per_pidcomm *c = p->current;
764 struct io_sample *sample;
765
766 if (!c) {
767 pr_warning("Invalid pidcomm!\n");
768 return -1;
769 }
770
771 sample = c->io_samples;
772
773 if (!sample) /* skip partially captured events */
774 return 0;
775
776 if (sample->end_time) {
777 pr_warning("Skip invalid end event: "
778 "previous event already ended!\n");
779 return 0;
780 }
781
782 if (sample->type != type) {
783 pr_warning("Skip invalid end event: invalid event type!\n");
784 return 0;
785 }
786
787 sample->end_time = end;
788
789 if (ret < 0) {
790 sample->err = ret;
791 } else if (type == IOTYPE_READ || type == IOTYPE_WRITE ||
792 type == IOTYPE_TX || type == IOTYPE_RX) {
793
794 if ((u64)ret > c->max_bytes)
795 c->max_bytes = ret;
796
797 c->total_bytes += ret;
798 p->total_bytes += ret;
799 sample->bytes = ret;
800 }
801
802 tchart->io_events++;
803
804 return 0;
805}
806
807static int
808process_enter_read(struct timechart *tchart,
809 struct perf_evsel *evsel,
810 struct perf_sample *sample)
811{
812 long fd = perf_evsel__intval(evsel, sample, "fd");
813 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_READ,
814 sample->time, fd);
815}
816
817static int
818process_exit_read(struct timechart *tchart,
819 struct perf_evsel *evsel,
820 struct perf_sample *sample)
821{
822 long ret = perf_evsel__intval(evsel, sample, "ret");
823 return pid_end_io_sample(tchart, sample->tid, IOTYPE_READ,
824 sample->time, ret);
825}
826
827static int
828process_enter_write(struct timechart *tchart,
829 struct perf_evsel *evsel,
830 struct perf_sample *sample)
831{
832 long fd = perf_evsel__intval(evsel, sample, "fd");
833 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_WRITE,
834 sample->time, fd);
835}
836
837static int
838process_exit_write(struct timechart *tchart,
839 struct perf_evsel *evsel,
840 struct perf_sample *sample)
841{
842 long ret = perf_evsel__intval(evsel, sample, "ret");
843 return pid_end_io_sample(tchart, sample->tid, IOTYPE_WRITE,
844 sample->time, ret);
845}
846
847static int
848process_enter_sync(struct timechart *tchart,
849 struct perf_evsel *evsel,
850 struct perf_sample *sample)
851{
852 long fd = perf_evsel__intval(evsel, sample, "fd");
853 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_SYNC,
854 sample->time, fd);
855}
856
857static int
858process_exit_sync(struct timechart *tchart,
859 struct perf_evsel *evsel,
860 struct perf_sample *sample)
861{
862 long ret = perf_evsel__intval(evsel, sample, "ret");
863 return pid_end_io_sample(tchart, sample->tid, IOTYPE_SYNC,
864 sample->time, ret);
865}
866
867static int
868process_enter_tx(struct timechart *tchart,
869 struct perf_evsel *evsel,
870 struct perf_sample *sample)
871{
872 long fd = perf_evsel__intval(evsel, sample, "fd");
873 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_TX,
874 sample->time, fd);
875}
876
877static int
878process_exit_tx(struct timechart *tchart,
879 struct perf_evsel *evsel,
880 struct perf_sample *sample)
881{
882 long ret = perf_evsel__intval(evsel, sample, "ret");
883 return pid_end_io_sample(tchart, sample->tid, IOTYPE_TX,
884 sample->time, ret);
885}
886
887static int
888process_enter_rx(struct timechart *tchart,
889 struct perf_evsel *evsel,
890 struct perf_sample *sample)
891{
892 long fd = perf_evsel__intval(evsel, sample, "fd");
893 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_RX,
894 sample->time, fd);
895}
896
897static int
898process_exit_rx(struct timechart *tchart,
899 struct perf_evsel *evsel,
900 struct perf_sample *sample)
901{
902 long ret = perf_evsel__intval(evsel, sample, "ret");
903 return pid_end_io_sample(tchart, sample->tid, IOTYPE_RX,
904 sample->time, ret);
905}
906
907static int
908process_enter_poll(struct timechart *tchart,
909 struct perf_evsel *evsel,
910 struct perf_sample *sample)
911{
912 long fd = perf_evsel__intval(evsel, sample, "fd");
913 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_POLL,
914 sample->time, fd);
915}
916
917static int
918process_exit_poll(struct timechart *tchart,
919 struct perf_evsel *evsel,
920 struct perf_sample *sample)
921{
922 long ret = perf_evsel__intval(evsel, sample, "ret");
923 return pid_end_io_sample(tchart, sample->tid, IOTYPE_POLL,
924 sample->time, ret);
925}
926
Arjan van de Ven10274982009-09-12 07:53:05 +0200927/*
928 * Sort the pid datastructure
929 */
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300930static void sort_pids(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200931{
932 struct per_pid *new_list, *p, *cursor, *prev;
933 /* sort by ppid first, then by pid, lowest to highest */
934
935 new_list = NULL;
936
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300937 while (tchart->all_data) {
938 p = tchart->all_data;
939 tchart->all_data = p->next;
Arjan van de Ven10274982009-09-12 07:53:05 +0200940 p->next = NULL;
941
942 if (new_list == NULL) {
943 new_list = p;
944 p->next = NULL;
945 continue;
946 }
947 prev = NULL;
948 cursor = new_list;
949 while (cursor) {
950 if (cursor->ppid > p->ppid ||
951 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
952 /* must insert before */
953 if (prev) {
954 p->next = prev->next;
955 prev->next = p;
956 cursor = NULL;
957 continue;
958 } else {
959 p->next = new_list;
960 new_list = p;
961 cursor = NULL;
962 continue;
963 }
964 }
965
966 prev = cursor;
967 cursor = cursor->next;
968 if (!cursor)
969 prev->next = p;
970 }
971 }
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300972 tchart->all_data = new_list;
Arjan van de Ven10274982009-09-12 07:53:05 +0200973}
974
975
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300976static void draw_c_p_states(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200977{
978 struct power_event *pwr;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300979 pwr = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200980
981 /*
982 * two pass drawing so that the P state bars are on top of the C state blocks
983 */
984 while (pwr) {
985 if (pwr->type == CSTATE)
986 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
987 pwr = pwr->next;
988 }
989
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300990 pwr = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200991 while (pwr) {
992 if (pwr->type == PSTATE) {
993 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300994 pwr->state = tchart->min_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200995 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
996 }
997 pwr = pwr->next;
998 }
999}
1000
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001001static void draw_wakeups(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001002{
1003 struct wake_event *we;
1004 struct per_pid *p;
1005 struct per_pidcomm *c;
1006
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -03001007 we = tchart->wake_events;
Arjan van de Ven10274982009-09-12 07:53:05 +02001008 while (we) {
1009 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001010 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001011
1012 /* locate the column of the waker and wakee */
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001013 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001014 while (p) {
1015 if (p->pid == we->waker || p->pid == we->wakee) {
1016 c = p->all;
1017 while (c) {
1018 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +09001019 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001020 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001021 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001022 }
Arjan van de Venbbe29872009-10-20 07:09:39 +09001023 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001024 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001025 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001026 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001027 }
1028 c = c->next;
1029 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001030 c = p->all;
1031 while (c) {
1032 if (p->pid == we->waker && !from) {
1033 from = c->Y;
1034 task_from = strdup(c->comm);
1035 }
1036 if (p->pid == we->wakee && !to) {
1037 to = c->Y;
1038 task_to = strdup(c->comm);
1039 }
1040 c = c->next;
1041 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001042 }
1043 p = p->next;
1044 }
1045
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001046 if (!task_from) {
1047 task_from = malloc(40);
1048 sprintf(task_from, "[%i]", we->waker);
1049 }
1050 if (!task_to) {
1051 task_to = malloc(40);
1052 sprintf(task_to, "[%i]", we->wakee);
1053 }
1054
Arjan van de Ven10274982009-09-12 07:53:05 +02001055 if (we->waker == -1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001056 svg_interrupt(we->time, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001057 else if (from && to && abs(from - to) == 1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001058 svg_wakeline(we->time, from, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001059 else
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001060 svg_partial_wakeline(we->time, from, task_from, to,
1061 task_to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001062 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001063
1064 free(task_from);
1065 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +02001066 }
1067}
1068
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001069static void draw_cpu_usage(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001070{
1071 struct per_pid *p;
1072 struct per_pidcomm *c;
1073 struct cpu_sample *sample;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001074 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001075 while (p) {
1076 c = p->all;
1077 while (c) {
1078 sample = c->samples;
1079 while (sample) {
Stanislav Fomichev8b6dcca2013-12-02 18:37:33 +04001080 if (sample->type == TYPE_RUNNING) {
1081 svg_process(sample->cpu,
1082 sample->start_time,
1083 sample->end_time,
Stanislav Fomichevde996222013-12-02 18:37:34 +04001084 p->pid,
Stanislav Fomichev8b6dcca2013-12-02 18:37:33 +04001085 c->comm,
1086 sample->backtrace);
1087 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001088
1089 sample = sample->next;
1090 }
1091 c = c->next;
1092 }
1093 p = p->next;
1094 }
1095}
1096
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001097static void draw_io_bars(struct timechart *tchart)
1098{
1099 const char *suf;
1100 double bytes;
1101 char comm[256];
1102 struct per_pid *p;
1103 struct per_pidcomm *c;
1104 struct io_sample *sample;
1105 int Y = 1;
1106
1107 p = tchart->all_data;
1108 while (p) {
1109 c = p->all;
1110 while (c) {
1111 if (!c->display) {
1112 c->Y = 0;
1113 c = c->next;
1114 continue;
1115 }
1116
1117 svg_box(Y, c->start_time, c->end_time, "process3");
1118 sample = c->io_samples;
1119 for (sample = c->io_samples; sample; sample = sample->next) {
1120 double h = (double)sample->bytes / c->max_bytes;
1121
1122 if (sample->err)
1123 h = 1;
1124
1125 if (sample->type == IOTYPE_SYNC)
1126 svg_fbox(Y,
1127 sample->start_time,
1128 sample->end_time,
1129 1,
1130 sample->err ? "error" : "sync",
1131 sample->fd,
1132 sample->err,
1133 sample->merges);
1134 else if (sample->type == IOTYPE_POLL)
1135 svg_fbox(Y,
1136 sample->start_time,
1137 sample->end_time,
1138 1,
1139 sample->err ? "error" : "poll",
1140 sample->fd,
1141 sample->err,
1142 sample->merges);
1143 else if (sample->type == IOTYPE_READ)
1144 svg_ubox(Y,
1145 sample->start_time,
1146 sample->end_time,
1147 h,
1148 sample->err ? "error" : "disk",
1149 sample->fd,
1150 sample->err,
1151 sample->merges);
1152 else if (sample->type == IOTYPE_WRITE)
1153 svg_lbox(Y,
1154 sample->start_time,
1155 sample->end_time,
1156 h,
1157 sample->err ? "error" : "disk",
1158 sample->fd,
1159 sample->err,
1160 sample->merges);
1161 else if (sample->type == IOTYPE_RX)
1162 svg_ubox(Y,
1163 sample->start_time,
1164 sample->end_time,
1165 h,
1166 sample->err ? "error" : "net",
1167 sample->fd,
1168 sample->err,
1169 sample->merges);
1170 else if (sample->type == IOTYPE_TX)
1171 svg_lbox(Y,
1172 sample->start_time,
1173 sample->end_time,
1174 h,
1175 sample->err ? "error" : "net",
1176 sample->fd,
1177 sample->err,
1178 sample->merges);
1179 }
1180
1181 suf = "";
1182 bytes = c->total_bytes;
1183 if (bytes > 1024) {
1184 bytes = bytes / 1024;
1185 suf = "K";
1186 }
1187 if (bytes > 1024) {
1188 bytes = bytes / 1024;
1189 suf = "M";
1190 }
1191 if (bytes > 1024) {
1192 bytes = bytes / 1024;
1193 suf = "G";
1194 }
1195
1196
1197 sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
1198 svg_text(Y, c->start_time, comm);
1199
1200 c->Y = Y;
1201 Y++;
1202 c = c->next;
1203 }
1204 p = p->next;
1205 }
1206}
1207
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001208static void draw_process_bars(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001209{
1210 struct per_pid *p;
1211 struct per_pidcomm *c;
1212 struct cpu_sample *sample;
1213 int Y = 0;
1214
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001215 Y = 2 * tchart->numcpus + 2;
Arjan van de Ven10274982009-09-12 07:53:05 +02001216
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001217 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001218 while (p) {
1219 c = p->all;
1220 while (c) {
1221 if (!c->display) {
1222 c->Y = 0;
1223 c = c->next;
1224 continue;
1225 }
1226
Arjan van de Vena92fe7b2009-09-20 18:13:53 +02001227 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +02001228 sample = c->samples;
1229 while (sample) {
1230 if (sample->type == TYPE_RUNNING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001231 svg_running(Y, sample->cpu,
1232 sample->start_time,
1233 sample->end_time,
1234 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001235 if (sample->type == TYPE_BLOCKED)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001236 svg_blocked(Y, sample->cpu,
1237 sample->start_time,
1238 sample->end_time,
1239 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001240 if (sample->type == TYPE_WAITING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001241 svg_waiting(Y, sample->cpu,
1242 sample->start_time,
1243 sample->end_time,
1244 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001245 sample = sample->next;
1246 }
1247
1248 if (c->comm) {
1249 char comm[256];
1250 if (c->total_time > 5000000000) /* 5 seconds */
1251 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0);
1252 else
1253 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0);
1254
1255 svg_text(Y, c->start_time, comm);
1256 }
1257 c->Y = Y;
1258 Y++;
1259 c = c->next;
1260 }
1261 p = p->next;
1262 }
1263}
1264
Arjan van de Venbbe29872009-10-20 07:09:39 +09001265static void add_process_filter(const char *string)
1266{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -03001267 int pid = strtoull(string, NULL, 10);
1268 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +09001269
Arjan van de Venbbe29872009-10-20 07:09:39 +09001270 if (!filt)
1271 return;
1272
1273 filt->name = strdup(string);
1274 filt->pid = pid;
1275 filt->next = process_filter;
1276
1277 process_filter = filt;
1278}
1279
1280static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
1281{
1282 struct process_filter *filt;
1283 if (!process_filter)
1284 return 1;
1285
1286 filt = process_filter;
1287 while (filt) {
1288 if (filt->pid && p->pid == filt->pid)
1289 return 1;
1290 if (strcmp(filt->name, c->comm) == 0)
1291 return 1;
1292 filt = filt->next;
1293 }
1294 return 0;
1295}
1296
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001297static int determine_display_tasks_filtered(struct timechart *tchart)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001298{
1299 struct per_pid *p;
1300 struct per_pidcomm *c;
1301 int count = 0;
1302
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001303 p = tchart->all_data;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001304 while (p) {
1305 p->display = 0;
1306 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001307 p->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001308
1309 /* no exit marker, task kept running to the end */
1310 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001311 p->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001312
1313 c = p->all;
1314
1315 while (c) {
1316 c->display = 0;
1317
1318 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001319 c->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001320
1321 if (passes_filter(p, c)) {
1322 c->display = 1;
1323 p->display = 1;
1324 count++;
1325 }
1326
1327 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001328 c->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001329
1330 c = c->next;
1331 }
1332 p = p->next;
1333 }
1334 return count;
1335}
1336
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001337static int determine_display_tasks(struct timechart *tchart, u64 threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +02001338{
1339 struct per_pid *p;
1340 struct per_pidcomm *c;
1341 int count = 0;
1342
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001343 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001344 while (p) {
1345 p->display = 0;
1346 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001347 p->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001348
1349 /* no exit marker, task kept running to the end */
1350 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001351 p->end_time = tchart->last_time;
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001352 if (p->total_time >= threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +02001353 p->display = 1;
1354
1355 c = p->all;
1356
1357 while (c) {
1358 c->display = 0;
1359
1360 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001361 c->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001362
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001363 if (c->total_time >= threshold) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001364 c->display = 1;
1365 count++;
1366 }
1367
1368 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001369 c->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001370
1371 c = c->next;
1372 }
1373 p = p->next;
1374 }
1375 return count;
1376}
1377
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001378static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
1379{
1380 struct per_pid *p;
1381 struct per_pidcomm *c;
1382 int count = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +02001383
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001384 p = timechart->all_data;
1385 while (p) {
1386 /* no exit marker, task kept running to the end */
1387 if (p->end_time == 0)
1388 p->end_time = timechart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001389
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001390 c = p->all;
1391
1392 while (c) {
1393 c->display = 0;
1394
1395 if (c->total_bytes >= threshold) {
1396 c->display = 1;
1397 count++;
1398 }
1399
1400 if (c->end_time == 0)
1401 c->end_time = timechart->last_time;
1402
1403 c = c->next;
1404 }
1405 p = p->next;
1406 }
1407 return count;
1408}
1409
1410#define BYTES_THRESH (1 * 1024 * 1024)
Arjan van de Ven10274982009-09-12 07:53:05 +02001411#define TIME_THRESH 10000000
1412
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001413static void write_svg_file(struct timechart *tchart, const char *filename)
Arjan van de Ven10274982009-09-12 07:53:05 +02001414{
1415 u64 i;
1416 int count;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001417 int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
Arjan van de Ven10274982009-09-12 07:53:05 +02001418
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001419 if (tchart->power_only)
1420 tchart->proc_num = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +02001421
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001422 /* We'd like to show at least proc_num tasks;
1423 * be less picky if we have fewer */
1424 do {
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001425 if (process_filter)
1426 count = determine_display_tasks_filtered(tchart);
1427 else if (tchart->io_events)
1428 count = determine_display_io_tasks(tchart, thresh);
1429 else
1430 count = determine_display_tasks(tchart, thresh);
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001431 thresh /= 10;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001432 } while (!process_filter && thresh && count < tchart->proc_num);
Arjan van de Ven10274982009-09-12 07:53:05 +02001433
Stanislav Fomichev3415d8b2014-01-20 15:39:38 +04001434 if (!tchart->proc_num)
1435 count = 0;
1436
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001437 if (tchart->io_events) {
1438 open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001439
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001440 svg_time_grid(0.5);
1441 svg_io_legenda();
Arjan van de Ven10274982009-09-12 07:53:05 +02001442
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001443 draw_io_bars(tchart);
1444 } else {
1445 open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001446
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001447 svg_time_grid(0);
1448
1449 svg_legenda();
1450
1451 for (i = 0; i < tchart->numcpus; i++)
1452 svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
1453
1454 draw_cpu_usage(tchart);
1455 if (tchart->proc_num)
1456 draw_process_bars(tchart);
1457 if (!tchart->tasks_only)
1458 draw_c_p_states(tchart);
1459 if (tchart->proc_num)
1460 draw_wakeups(tchart);
1461 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001462
1463 svg_close();
1464}
1465
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001466static int process_header(struct perf_file_section *section __maybe_unused,
1467 struct perf_header *ph,
1468 int feat,
1469 int fd __maybe_unused,
1470 void *data)
1471{
1472 struct timechart *tchart = data;
1473
1474 switch (feat) {
1475 case HEADER_NRCPUS:
1476 tchart->numcpus = ph->env.nr_cpus_avail;
1477 break;
Stanislav Fomichevc5079992013-12-02 18:37:36 +04001478
1479 case HEADER_CPU_TOPOLOGY:
1480 if (!tchart->topology)
1481 break;
1482
1483 if (svg_build_topology_map(ph->env.sibling_cores,
1484 ph->env.nr_sibling_cores,
1485 ph->env.sibling_threads,
1486 ph->env.nr_sibling_threads))
1487 fprintf(stderr, "problem building topology\n");
1488 break;
1489
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001490 default:
1491 break;
1492 }
1493
1494 return 0;
1495}
1496
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001497static int __cmd_timechart(struct timechart *tchart, const char *output_name)
Arjan van de Ven10274982009-09-12 07:53:05 +02001498{
Jiri Olsa59366782013-07-11 17:28:30 +02001499 const struct perf_evsel_str_handler power_tracepoints[] = {
1500 { "power:cpu_idle", process_sample_cpu_idle },
1501 { "power:cpu_frequency", process_sample_cpu_frequency },
1502 { "sched:sched_wakeup", process_sample_sched_wakeup },
1503 { "sched:sched_switch", process_sample_sched_switch },
1504#ifdef SUPPORT_OLD_POWER_EVENTS
1505 { "power:power_start", process_sample_power_start },
1506 { "power:power_end", process_sample_power_end },
1507 { "power:power_frequency", process_sample_power_frequency },
1508#endif
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001509
1510 { "syscalls:sys_enter_read", process_enter_read },
1511 { "syscalls:sys_enter_pread64", process_enter_read },
1512 { "syscalls:sys_enter_readv", process_enter_read },
1513 { "syscalls:sys_enter_preadv", process_enter_read },
1514 { "syscalls:sys_enter_write", process_enter_write },
1515 { "syscalls:sys_enter_pwrite64", process_enter_write },
1516 { "syscalls:sys_enter_writev", process_enter_write },
1517 { "syscalls:sys_enter_pwritev", process_enter_write },
1518 { "syscalls:sys_enter_sync", process_enter_sync },
1519 { "syscalls:sys_enter_sync_file_range", process_enter_sync },
1520 { "syscalls:sys_enter_fsync", process_enter_sync },
1521 { "syscalls:sys_enter_msync", process_enter_sync },
1522 { "syscalls:sys_enter_recvfrom", process_enter_rx },
1523 { "syscalls:sys_enter_recvmmsg", process_enter_rx },
1524 { "syscalls:sys_enter_recvmsg", process_enter_rx },
1525 { "syscalls:sys_enter_sendto", process_enter_tx },
1526 { "syscalls:sys_enter_sendmsg", process_enter_tx },
1527 { "syscalls:sys_enter_sendmmsg", process_enter_tx },
1528 { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
1529 { "syscalls:sys_enter_epoll_wait", process_enter_poll },
1530 { "syscalls:sys_enter_poll", process_enter_poll },
1531 { "syscalls:sys_enter_ppoll", process_enter_poll },
1532 { "syscalls:sys_enter_pselect6", process_enter_poll },
1533 { "syscalls:sys_enter_select", process_enter_poll },
1534
1535 { "syscalls:sys_exit_read", process_exit_read },
1536 { "syscalls:sys_exit_pread64", process_exit_read },
1537 { "syscalls:sys_exit_readv", process_exit_read },
1538 { "syscalls:sys_exit_preadv", process_exit_read },
1539 { "syscalls:sys_exit_write", process_exit_write },
1540 { "syscalls:sys_exit_pwrite64", process_exit_write },
1541 { "syscalls:sys_exit_writev", process_exit_write },
1542 { "syscalls:sys_exit_pwritev", process_exit_write },
1543 { "syscalls:sys_exit_sync", process_exit_sync },
1544 { "syscalls:sys_exit_sync_file_range", process_exit_sync },
1545 { "syscalls:sys_exit_fsync", process_exit_sync },
1546 { "syscalls:sys_exit_msync", process_exit_sync },
1547 { "syscalls:sys_exit_recvfrom", process_exit_rx },
1548 { "syscalls:sys_exit_recvmmsg", process_exit_rx },
1549 { "syscalls:sys_exit_recvmsg", process_exit_rx },
1550 { "syscalls:sys_exit_sendto", process_exit_tx },
1551 { "syscalls:sys_exit_sendmsg", process_exit_tx },
1552 { "syscalls:sys_exit_sendmmsg", process_exit_tx },
1553 { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
1554 { "syscalls:sys_exit_epoll_wait", process_exit_poll },
1555 { "syscalls:sys_exit_poll", process_exit_poll },
1556 { "syscalls:sys_exit_ppoll", process_exit_poll },
1557 { "syscalls:sys_exit_pselect6", process_exit_poll },
1558 { "syscalls:sys_exit_select", process_exit_poll },
Jiri Olsa59366782013-07-11 17:28:30 +02001559 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001560 struct perf_data_file file = {
1561 .path = input_name,
1562 .mode = PERF_DATA_MODE_READ,
1563 };
1564
1565 struct perf_session *session = perf_session__new(&file, false,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001566 &tchart->tool);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001567 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001568
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001569 if (session == NULL)
1570 return -ENOMEM;
1571
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001572 (void)perf_header__process_sections(&session->header,
1573 perf_data_file__fd(session->file),
1574 tchart,
1575 process_header);
1576
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001577 if (!perf_session__has_traces(session, "timechart record"))
1578 goto out_delete;
1579
Jiri Olsa59366782013-07-11 17:28:30 +02001580 if (perf_session__set_tracepoints_handlers(session,
1581 power_tracepoints)) {
1582 pr_err("Initializing session tracepoint handlers failed\n");
1583 goto out_delete;
1584 }
1585
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001586 ret = perf_session__process_events(session, &tchart->tool);
Li Zefan5cbd0802009-12-01 14:05:16 +08001587 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001588 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001589
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001590 end_sample_processing(tchart);
Arjan van de Ven10274982009-09-12 07:53:05 +02001591
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001592 sort_pids(tchart);
Arjan van de Ven10274982009-09-12 07:53:05 +02001593
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001594 write_svg_file(tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001595
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001596 pr_info("Written %2.1f seconds of trace to %s.\n",
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001597 (tchart->last_time - tchart->first_time) / 1000000000.0, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001598out_delete:
1599 perf_session__delete(session);
1600 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001601}
1602
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001603static int timechart__io_record(int argc, const char **argv)
1604{
1605 unsigned int rec_argc, i;
1606 const char **rec_argv;
1607 const char **p;
1608 char *filter = NULL;
1609
1610 const char * const common_args[] = {
1611 "record", "-a", "-R", "-c", "1",
1612 };
1613 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1614
1615 const char * const disk_events[] = {
1616 "syscalls:sys_enter_read",
1617 "syscalls:sys_enter_pread64",
1618 "syscalls:sys_enter_readv",
1619 "syscalls:sys_enter_preadv",
1620 "syscalls:sys_enter_write",
1621 "syscalls:sys_enter_pwrite64",
1622 "syscalls:sys_enter_writev",
1623 "syscalls:sys_enter_pwritev",
1624 "syscalls:sys_enter_sync",
1625 "syscalls:sys_enter_sync_file_range",
1626 "syscalls:sys_enter_fsync",
1627 "syscalls:sys_enter_msync",
1628
1629 "syscalls:sys_exit_read",
1630 "syscalls:sys_exit_pread64",
1631 "syscalls:sys_exit_readv",
1632 "syscalls:sys_exit_preadv",
1633 "syscalls:sys_exit_write",
1634 "syscalls:sys_exit_pwrite64",
1635 "syscalls:sys_exit_writev",
1636 "syscalls:sys_exit_pwritev",
1637 "syscalls:sys_exit_sync",
1638 "syscalls:sys_exit_sync_file_range",
1639 "syscalls:sys_exit_fsync",
1640 "syscalls:sys_exit_msync",
1641 };
1642 unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
1643
1644 const char * const net_events[] = {
1645 "syscalls:sys_enter_recvfrom",
1646 "syscalls:sys_enter_recvmmsg",
1647 "syscalls:sys_enter_recvmsg",
1648 "syscalls:sys_enter_sendto",
1649 "syscalls:sys_enter_sendmsg",
1650 "syscalls:sys_enter_sendmmsg",
1651
1652 "syscalls:sys_exit_recvfrom",
1653 "syscalls:sys_exit_recvmmsg",
1654 "syscalls:sys_exit_recvmsg",
1655 "syscalls:sys_exit_sendto",
1656 "syscalls:sys_exit_sendmsg",
1657 "syscalls:sys_exit_sendmmsg",
1658 };
1659 unsigned int net_events_nr = ARRAY_SIZE(net_events);
1660
1661 const char * const poll_events[] = {
1662 "syscalls:sys_enter_epoll_pwait",
1663 "syscalls:sys_enter_epoll_wait",
1664 "syscalls:sys_enter_poll",
1665 "syscalls:sys_enter_ppoll",
1666 "syscalls:sys_enter_pselect6",
1667 "syscalls:sys_enter_select",
1668
1669 "syscalls:sys_exit_epoll_pwait",
1670 "syscalls:sys_exit_epoll_wait",
1671 "syscalls:sys_exit_poll",
1672 "syscalls:sys_exit_ppoll",
1673 "syscalls:sys_exit_pselect6",
1674 "syscalls:sys_exit_select",
1675 };
1676 unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
1677
1678 rec_argc = common_args_nr +
1679 disk_events_nr * 4 +
1680 net_events_nr * 4 +
1681 poll_events_nr * 4 +
1682 argc;
1683 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1684
1685 if (rec_argv == NULL)
1686 return -ENOMEM;
1687
1688 if (asprintf(&filter, "common_pid != %d", getpid()) < 0)
1689 return -ENOMEM;
1690
1691 p = rec_argv;
1692 for (i = 0; i < common_args_nr; i++)
1693 *p++ = strdup(common_args[i]);
1694
1695 for (i = 0; i < disk_events_nr; i++) {
1696 if (!is_valid_tracepoint(disk_events[i])) {
1697 rec_argc -= 4;
1698 continue;
1699 }
1700
1701 *p++ = "-e";
1702 *p++ = strdup(disk_events[i]);
1703 *p++ = "--filter";
1704 *p++ = filter;
1705 }
1706 for (i = 0; i < net_events_nr; i++) {
1707 if (!is_valid_tracepoint(net_events[i])) {
1708 rec_argc -= 4;
1709 continue;
1710 }
1711
1712 *p++ = "-e";
1713 *p++ = strdup(net_events[i]);
1714 *p++ = "--filter";
1715 *p++ = filter;
1716 }
1717 for (i = 0; i < poll_events_nr; i++) {
1718 if (!is_valid_tracepoint(poll_events[i])) {
1719 rec_argc -= 4;
1720 continue;
1721 }
1722
1723 *p++ = "-e";
1724 *p++ = strdup(poll_events[i]);
1725 *p++ = "--filter";
1726 *p++ = filter;
1727 }
1728
1729 for (i = 0; i < (unsigned int)argc; i++)
1730 *p++ = argv[i];
1731
1732 return cmd_record(rec_argc, rec_argv, NULL);
1733}
1734
1735
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001736static int timechart__record(struct timechart *tchart, int argc, const char **argv)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001737{
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001738 unsigned int rec_argc, i, j;
1739 const char **rec_argv;
1740 const char **p;
1741 unsigned int record_elems;
1742
1743 const char * const common_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001744 "record", "-a", "-R", "-c", "1",
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001745 };
1746 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1747
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001748 const char * const backtrace_args[] = {
1749 "-g",
1750 };
1751 unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
1752
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001753 const char * const power_args[] = {
1754 "-e", "power:cpu_frequency",
1755 "-e", "power:cpu_idle",
1756 };
1757 unsigned int power_args_nr = ARRAY_SIZE(power_args);
1758
1759 const char * const old_power_args[] = {
1760#ifdef SUPPORT_OLD_POWER_EVENTS
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001761 "-e", "power:power_start",
1762 "-e", "power:power_end",
1763 "-e", "power:power_frequency",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001764#endif
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001765 };
1766 unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
1767
1768 const char * const tasks_args[] = {
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001769 "-e", "sched:sched_wakeup",
1770 "-e", "sched:sched_switch",
1771 };
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001772 unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001773
Thomas Renninger20c457b2011-01-03 17:50:45 +01001774#ifdef SUPPORT_OLD_POWER_EVENTS
1775 if (!is_valid_tracepoint("power:cpu_idle") &&
1776 is_valid_tracepoint("power:power_start")) {
1777 use_old_power_events = 1;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001778 power_args_nr = 0;
1779 } else {
1780 old_power_args_nr = 0;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001781 }
1782#endif
1783
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001784 if (tchart->power_only)
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001785 tasks_args_nr = 0;
1786
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001787 if (tchart->tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001788 power_args_nr = 0;
1789 old_power_args_nr = 0;
1790 }
1791
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001792 if (!tchart->with_backtrace)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001793 backtrace_args_no = 0;
1794
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001795 record_elems = common_args_nr + tasks_args_nr +
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001796 power_args_nr + old_power_args_nr + backtrace_args_no;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001797
1798 rec_argc = record_elems + argc;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001799 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1800
Chris Samuelce47dc52010-11-13 13:35:06 +11001801 if (rec_argv == NULL)
1802 return -ENOMEM;
1803
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001804 p = rec_argv;
1805 for (i = 0; i < common_args_nr; i++)
1806 *p++ = strdup(common_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001807
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001808 for (i = 0; i < backtrace_args_no; i++)
1809 *p++ = strdup(backtrace_args[i]);
1810
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001811 for (i = 0; i < tasks_args_nr; i++)
1812 *p++ = strdup(tasks_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001813
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001814 for (i = 0; i < power_args_nr; i++)
1815 *p++ = strdup(power_args[i]);
1816
1817 for (i = 0; i < old_power_args_nr; i++)
1818 *p++ = strdup(old_power_args[i]);
1819
Ramkumar Ramachandra263f89b2014-03-16 12:06:05 -04001820 for (j = 0; j < (unsigned int)argc; j++)
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001821 *p++ = argv[j];
1822
1823 return cmd_record(rec_argc, rec_argv, NULL);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001824}
1825
Arjan van de Venbbe29872009-10-20 07:09:39 +09001826static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001827parse_process(const struct option *opt __maybe_unused, const char *arg,
1828 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001829{
1830 if (arg)
1831 add_process_filter(arg);
1832 return 0;
1833}
1834
Stanislav Fomicheve57a2df2013-12-17 19:53:49 +04001835static int
1836parse_highlight(const struct option *opt __maybe_unused, const char *arg,
1837 int __maybe_unused unset)
1838{
1839 unsigned long duration = strtoul(arg, NULL, 0);
1840
1841 if (svg_highlight || svg_highlight_name)
1842 return -1;
1843
1844 if (duration)
1845 svg_highlight = duration;
1846 else
1847 svg_highlight_name = strdup(arg);
1848
1849 return 0;
1850}
1851
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001852int cmd_timechart(int argc, const char **argv,
1853 const char *prefix __maybe_unused)
1854{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001855 struct timechart tchart = {
1856 .tool = {
1857 .comm = process_comm_event,
1858 .fork = process_fork_event,
1859 .exit = process_exit_event,
1860 .sample = process_sample_event,
1861 .ordered_samples = true,
1862 },
1863 .proc_num = 15,
1864 };
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001865 const char *output_name = "output.svg";
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001866 const struct option timechart_options[] = {
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001867 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1868 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1869 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
Stanislav Fomicheve57a2df2013-12-17 19:53:49 +04001870 OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
1871 "highlight tasks. Pass duration in ns or process name.",
1872 parse_highlight),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001873 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1874 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001875 "output processes data only"),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001876 OPT_CALLBACK('p', "process", NULL, "process",
1877 "process selector. Pass a pid or process name.",
1878 parse_process),
David Ahernec5761e2010-12-09 13:27:07 -07001879 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1880 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001881 OPT_INTEGER('n', "proc-num", &tchart.proc_num,
Stanislav Fomichev54874e32013-11-01 20:25:46 +04001882 "min. number of tasks to print"),
Stanislav Fomichevc5079992013-12-02 18:37:36 +04001883 OPT_BOOLEAN('t', "topology", &tchart.topology,
1884 "sort CPUs according to topology"),
Arjan van de Ven10274982009-09-12 07:53:05 +02001885 OPT_END()
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001886 };
1887 const char * const timechart_usage[] = {
1888 "perf timechart [<options>] {record}",
1889 NULL
1890 };
Arjan van de Ven10274982009-09-12 07:53:05 +02001891
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001892 const struct option record_options[] = {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001893 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1894 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only,
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001895 "output processes data only"),
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001896 OPT_BOOLEAN('I', "io-only", &tchart.io_only,
1897 "record only IO data"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001898 OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001899 OPT_END()
1900 };
1901 const char * const record_usage[] = {
1902 "perf timechart record [<options>]",
1903 NULL
1904 };
1905 argc = parse_options(argc, argv, timechart_options, timechart_usage,
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001906 PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001907
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001908 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001909 pr_err("-P and -T options cannot be used at the same time.\n");
1910 return -1;
1911 }
1912
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001913 symbol__init();
1914
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001915 if (argc && !strncmp(argv[0], "rec", 3)) {
1916 argc = parse_options(argc, argv, record_options, record_usage,
1917 PARSE_OPT_STOP_AT_NON_OPTION);
1918
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001919 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001920 pr_err("-P and -T options cannot be used at the same time.\n");
1921 return -1;
1922 }
1923
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001924 if (tchart.io_only)
1925 return timechart__io_record(argc, argv);
1926 else
1927 return timechart__record(&tchart, argc, argv);
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001928 } else if (argc)
1929 usage_with_options(timechart_usage, timechart_options);
Arjan van de Ven10274982009-09-12 07:53:05 +02001930
1931 setup_pager();
1932
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001933 return __cmd_timechart(&tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001934}