blob: a24b229a785f7bd28e291a888f6494f5eb151e68 [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
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030015#include <inttypes.h>
Jiri Olsac85cffa2013-07-11 17:28:29 +020016#include <traceevent/event-parse.h>
17
Arjan van de Ven10274982009-09-12 07:53:05 +020018#include "builtin.h"
19
20#include "util/util.h"
21
22#include "util/color.h"
23#include <linux/list.h>
24#include "util/cache.h"
Jiri Olsa59366782013-07-11 17:28:30 +020025#include "util/evlist.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -020026#include "util/evsel.h"
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030027#include <linux/kernel.h>
Arjan van de Ven10274982009-09-12 07:53:05 +020028#include <linux/rbtree.h>
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -030029#include <linux/time64.h>
Arjan van de Ven10274982009-09-12 07:53:05 +020030#include "util/symbol.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020031#include "util/callchain.h"
32#include "util/strlist.h"
33
34#include "perf.h"
35#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060036#include <subcmd/parse-options.h>
Arjan van de Ven10274982009-09-12 07:53:05 +020037#include "util/parse-events.h"
Li Zefan5cbd0802009-12-01 14:05:16 +080038#include "util/event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020039#include "util/session.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020040#include "util/svghelper.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020041#include "util/tool.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020042#include "util/data.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020043#include "util/debug.h"
Arjan van de Ven10274982009-09-12 07:53:05 +020044
Thomas Renninger20c457b2011-01-03 17:50:45 +010045#define SUPPORT_OLD_POWER_EVENTS 1
46#define PWR_EVENT_EXIT -1
47
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -030048struct per_pid;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -030049struct power_event;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -030050struct wake_event;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -030051
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030052struct timechart {
53 struct perf_tool tool;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -030054 struct per_pid *all_data;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -030055 struct power_event *power_events;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -030056 struct wake_event *wake_events;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030057 int proc_num;
58 unsigned int numcpus;
59 u64 min_freq, /* Lowest CPU frequency seen */
60 max_freq, /* Highest CPU frequency seen */
61 turbo_frequency,
62 first_time, last_time;
63 bool power_only,
64 tasks_only,
Stanislav Fomichevc5079992013-12-02 18:37:36 +040065 with_backtrace,
66 topology;
Arnaldo Carvalho de Melo86066062015-05-15 17:29:56 -030067 bool force;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040068 /* IO related settings */
Stanislav Fomichevd2431442014-07-08 20:03:43 +040069 bool io_only,
70 skip_eagain;
Arnaldo Carvalho de Melo86066062015-05-15 17:29:56 -030071 u64 io_events;
Stanislav Fomichevd2431442014-07-08 20:03:43 +040072 u64 min_time,
73 merge_dist;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -030074};
Arjan van de Ven10274982009-09-12 07:53:05 +020075
Arjan van de Ven10274982009-09-12 07:53:05 +020076struct per_pidcomm;
Arjan van de Ven10274982009-09-12 07:53:05 +020077struct cpu_sample;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040078struct io_sample;
Arjan van de Ven10274982009-09-12 07:53:05 +020079
80/*
81 * Datastructure layout:
82 * We keep an list of "pid"s, matching the kernels notion of a task struct.
83 * Each "pid" entry, has a list of "comm"s.
84 * this is because we want to track different programs different, while
85 * exec will reuse the original pid (by design).
86 * Each comm has a list of samples that will be used to draw
87 * final graph.
88 */
89
90struct per_pid {
91 struct per_pid *next;
92
93 int pid;
94 int ppid;
95
96 u64 start_time;
97 u64 end_time;
98 u64 total_time;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +040099 u64 total_bytes;
Arjan van de Ven10274982009-09-12 07:53:05 +0200100 int display;
101
102 struct per_pidcomm *all;
103 struct per_pidcomm *current;
Arjan van de Ven10274982009-09-12 07:53:05 +0200104};
105
106
107struct per_pidcomm {
108 struct per_pidcomm *next;
109
110 u64 start_time;
111 u64 end_time;
112 u64 total_time;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400113 u64 max_bytes;
114 u64 total_bytes;
Arjan van de Ven10274982009-09-12 07:53:05 +0200115
116 int Y;
117 int display;
118
119 long state;
120 u64 state_since;
121
122 char *comm;
123
124 struct cpu_sample *samples;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400125 struct io_sample *io_samples;
Arjan van de Ven10274982009-09-12 07:53:05 +0200126};
127
128struct sample_wrapper {
129 struct sample_wrapper *next;
130
131 u64 timestamp;
132 unsigned char data[0];
133};
134
135#define TYPE_NONE 0
136#define TYPE_RUNNING 1
137#define TYPE_WAITING 2
138#define TYPE_BLOCKED 3
139
140struct cpu_sample {
141 struct cpu_sample *next;
142
143 u64 start_time;
144 u64 end_time;
145 int type;
146 int cpu;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400147 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200148};
149
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400150enum {
151 IOTYPE_READ,
152 IOTYPE_WRITE,
153 IOTYPE_SYNC,
154 IOTYPE_TX,
155 IOTYPE_RX,
156 IOTYPE_POLL,
157};
158
159struct io_sample {
160 struct io_sample *next;
161
162 u64 start_time;
163 u64 end_time;
164 u64 bytes;
165 int type;
166 int fd;
167 int err;
168 int merges;
169};
170
Arjan van de Ven10274982009-09-12 07:53:05 +0200171#define CSTATE 1
172#define PSTATE 2
173
174struct power_event {
175 struct power_event *next;
176 int type;
177 int state;
178 u64 start_time;
179 u64 end_time;
180 int cpu;
181};
182
183struct wake_event {
184 struct wake_event *next;
185 int waker;
186 int wakee;
187 u64 time;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400188 const char *backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200189};
190
Arjan van de Venbbe29872009-10-20 07:09:39 +0900191struct process_filter {
Li Zefan5cbd0802009-12-01 14:05:16 +0800192 char *name;
193 int pid;
194 struct process_filter *next;
Arjan van de Venbbe29872009-10-20 07:09:39 +0900195};
196
197static struct process_filter *process_filter;
198
199
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300200static struct per_pid *find_create_pid(struct timechart *tchart, int pid)
Arjan van de Ven10274982009-09-12 07:53:05 +0200201{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300202 struct per_pid *cursor = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +0200203
204 while (cursor) {
205 if (cursor->pid == pid)
206 return cursor;
207 cursor = cursor->next;
208 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300209 cursor = zalloc(sizeof(*cursor));
Arjan van de Ven10274982009-09-12 07:53:05 +0200210 assert(cursor != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200211 cursor->pid = pid;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300212 cursor->next = tchart->all_data;
213 tchart->all_data = cursor;
Arjan van de Ven10274982009-09-12 07:53:05 +0200214 return cursor;
215}
216
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300217static void pid_set_comm(struct timechart *tchart, int pid, char *comm)
Arjan van de Ven10274982009-09-12 07:53:05 +0200218{
219 struct per_pid *p;
220 struct per_pidcomm *c;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300221 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200222 c = p->all;
223 while (c) {
224 if (c->comm && strcmp(c->comm, comm) == 0) {
225 p->current = c;
226 return;
227 }
228 if (!c->comm) {
229 c->comm = strdup(comm);
230 p->current = c;
231 return;
232 }
233 c = c->next;
234 }
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300235 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200236 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200237 c->comm = strdup(comm);
238 p->current = c;
239 c->next = p->all;
240 p->all = c;
241}
242
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300243static void pid_fork(struct timechart *tchart, int pid, int ppid, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200244{
245 struct per_pid *p, *pp;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300246 p = find_create_pid(tchart, pid);
247 pp = find_create_pid(tchart, ppid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200248 p->ppid = ppid;
249 if (pp->current && pp->current->comm && !p->current)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300250 pid_set_comm(tchart, pid, pp->current->comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200251
252 p->start_time = timestamp;
Stanislav Fomichev962e3102014-07-08 20:03:42 +0400253 if (p->current && !p->current->start_time) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200254 p->current->start_time = timestamp;
255 p->current->state_since = timestamp;
256 }
257}
258
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300259static void pid_exit(struct timechart *tchart, int pid, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200260{
261 struct per_pid *p;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300262 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200263 p->end_time = timestamp;
264 if (p->current)
265 p->current->end_time = timestamp;
266}
267
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300268static void pid_put_sample(struct timechart *tchart, int pid, int type,
269 unsigned int cpu, u64 start, u64 end,
270 const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200271{
272 struct per_pid *p;
273 struct per_pidcomm *c;
274 struct cpu_sample *sample;
275
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300276 p = find_create_pid(tchart, pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200277 c = p->current;
278 if (!c) {
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300279 c = zalloc(sizeof(*c));
Arjan van de Ven10274982009-09-12 07:53:05 +0200280 assert(c != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200281 p->current = c;
282 c->next = p->all;
283 p->all = c;
284 }
285
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300286 sample = zalloc(sizeof(*sample));
Arjan van de Ven10274982009-09-12 07:53:05 +0200287 assert(sample != NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200288 sample->start_time = start;
289 sample->end_time = end;
290 sample->type = type;
291 sample->next = c->samples;
292 sample->cpu = cpu;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400293 sample->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200294 c->samples = sample;
295
296 if (sample->type == TYPE_RUNNING && end > start && start > 0) {
297 c->total_time += (end-start);
298 p->total_time += (end-start);
299 }
300
301 if (c->start_time == 0 || c->start_time > start)
302 c->start_time = start;
303 if (p->start_time == 0 || p->start_time > start)
304 p->start_time = start;
Arjan van de Ven10274982009-09-12 07:53:05 +0200305}
306
307#define MAX_CPUS 4096
308
309static u64 cpus_cstate_start_times[MAX_CPUS];
310static int cpus_cstate_state[MAX_CPUS];
311static u64 cpus_pstate_start_times[MAX_CPUS];
312static u64 cpus_pstate_state[MAX_CPUS];
313
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300314static int process_comm_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200315 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300316 struct perf_sample *sample __maybe_unused,
317 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200318{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300319 struct timechart *tchart = container_of(tool, struct timechart, tool);
320 pid_set_comm(tchart, event->comm.tid, event->comm.comm);
Arjan van de Ven10274982009-09-12 07:53:05 +0200321 return 0;
322}
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200323
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300324static int process_fork_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200325 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300326 struct perf_sample *sample __maybe_unused,
327 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200328{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300329 struct timechart *tchart = container_of(tool, struct timechart, tool);
330 pid_fork(tchart, event->fork.pid, event->fork.ppid, event->fork.time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200331 return 0;
332}
333
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300334static int process_exit_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200335 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300336 struct perf_sample *sample __maybe_unused,
337 struct machine *machine __maybe_unused)
Arjan van de Ven10274982009-09-12 07:53:05 +0200338{
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300339 struct timechart *tchart = container_of(tool, struct timechart, tool);
340 pid_exit(tchart, event->fork.pid, event->fork.time);
Arjan van de Ven10274982009-09-12 07:53:05 +0200341 return 0;
342}
343
Thomas Renninger20c457b2011-01-03 17:50:45 +0100344#ifdef SUPPORT_OLD_POWER_EVENTS
345static int use_old_power_events;
Thomas Renninger20c457b2011-01-03 17:50:45 +0100346#endif
347
Arjan van de Ven10274982009-09-12 07:53:05 +0200348static 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
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300354static void c_state_end(struct timechart *tchart, int cpu, u64 timestamp)
Arjan van de Ven10274982009-09-12 07:53:05 +0200355{
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;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300366 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200367
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300368 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200369}
370
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300371static void p_state_change(struct timechart *tchart, int cpu, u64 timestamp, u64 new_freq)
Arjan van de Ven10274982009-09-12 07:53:05 +0200372{
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;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300387 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200388
389 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300390 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200391
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300392 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200393
394 cpus_pstate_state[cpu] = new_freq;
395 cpus_pstate_start_times[cpu] = timestamp;
396
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300397 if ((u64)new_freq > tchart->max_freq)
398 tchart->max_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200399
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300400 if (new_freq < tchart->min_freq || tchart->min_freq == 0)
401 tchart->min_freq = new_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200402
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300403 if (new_freq == tchart->max_freq - 1000)
404 tchart->turbo_frequency = tchart->max_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +0200405}
406
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300407static void sched_wakeup(struct timechart *tchart, int cpu, u64 timestamp,
408 int waker, int wakee, u8 flags, const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200409{
Arjan van de Ven10274982009-09-12 07:53:05 +0200410 struct per_pid *p;
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300411 struct wake_event *we = zalloc(sizeof(*we));
Arjan van de Ven10274982009-09-12 07:53:05 +0200412
Arjan van de Ven10274982009-09-12 07:53:05 +0200413 if (!we)
414 return;
415
Arjan van de Ven10274982009-09-12 07:53:05 +0200416 we->time = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400417 we->waker = waker;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400418 we->backtrace = backtrace;
Arjan van de Ven10274982009-09-12 07:53:05 +0200419
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400420 if ((flags & TRACE_FLAG_HARDIRQ) || (flags & TRACE_FLAG_SOFTIRQ))
Arjan van de Ven10274982009-09-12 07:53:05 +0200421 we->waker = -1;
422
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400423 we->wakee = wakee;
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -0300424 we->next = tchart->wake_events;
425 tchart->wake_events = we;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300426 p = find_create_pid(tchart, we->wakee);
Arjan van de Ven10274982009-09-12 07:53:05 +0200427
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) {
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300433 pid_put_sample(tchart, p->pid, p->current->state, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400434 p->current->state_since, timestamp, NULL);
Arjan van de Ven10274982009-09-12 07:53:05 +0200435 p->current->state_since = timestamp;
436 p->current->state = TYPE_WAITING;
437 }
438}
439
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300440static void sched_switch(struct timechart *tchart, int cpu, u64 timestamp,
441 int prev_pid, int next_pid, u64 prev_state,
442 const char *backtrace)
Arjan van de Ven10274982009-09-12 07:53:05 +0200443{
444 struct per_pid *p = NULL, *prev_p;
Arjan van de Ven10274982009-09-12 07:53:05 +0200445
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300446 prev_p = find_create_pid(tchart, prev_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200447
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300448 p = find_create_pid(tchart, next_pid);
Arjan van de Ven10274982009-09-12 07:53:05 +0200449
450 if (prev_p->current && prev_p->current->state != TYPE_NONE)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300451 pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400452 prev_p->current->state_since, timestamp,
453 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200454 if (p && p->current) {
455 if (p->current->state != TYPE_NONE)
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300456 pid_put_sample(tchart, next_pid, p->current->state, cpu,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400457 p->current->state_since, timestamp,
458 backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200459
Julia Lawall33e26a12010-08-05 22:27:51 +0200460 p->current->state_since = timestamp;
461 p->current->state = TYPE_RUNNING;
Arjan van de Ven10274982009-09-12 07:53:05 +0200462 }
463
464 if (prev_p->current) {
465 prev_p->current->state = TYPE_NONE;
466 prev_p->current->state_since = timestamp;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400467 if (prev_state & 2)
Arjan van de Ven10274982009-09-12 07:53:05 +0200468 prev_p->current->state = TYPE_BLOCKED;
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400469 if (prev_state == 0)
Arjan van de Ven10274982009-09-12 07:53:05 +0200470 prev_p->current->state = TYPE_WAITING;
471 }
472}
473
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400474static const char *cat_backtrace(union perf_event *event,
475 struct perf_sample *sample,
476 struct machine *machine)
477{
478 struct addr_location al;
479 unsigned int i;
480 char *p = NULL;
481 size_t p_len;
482 u8 cpumode = PERF_RECORD_MISC_USER;
483 struct addr_location tal;
484 struct ip_callchain *chain = sample->callchain;
485 FILE *f = open_memstream(&p, &p_len);
486
487 if (!f) {
488 perror("open_memstream error");
489 return NULL;
490 }
491
492 if (!chain)
493 goto exit;
494
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300495 if (machine__resolve(machine, &al, sample) < 0) {
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400496 fprintf(stderr, "problem processing %d event, skipping it.\n",
497 event->header.type);
498 goto exit;
499 }
500
501 for (i = 0; i < chain->nr; i++) {
502 u64 ip;
503
504 if (callchain_param.order == ORDER_CALLEE)
505 ip = chain->ips[i];
506 else
507 ip = chain->ips[chain->nr - i - 1];
508
509 if (ip >= PERF_CONTEXT_MAX) {
510 switch (ip) {
511 case PERF_CONTEXT_HV:
512 cpumode = PERF_RECORD_MISC_HYPERVISOR;
513 break;
514 case PERF_CONTEXT_KERNEL:
515 cpumode = PERF_RECORD_MISC_KERNEL;
516 break;
517 case PERF_CONTEXT_USER:
518 cpumode = PERF_RECORD_MISC_USER;
519 break;
520 default:
521 pr_debug("invalid callchain context: "
522 "%"PRId64"\n", (s64) ip);
523
524 /*
525 * It seems the callchain is corrupted.
526 * Discard all.
527 */
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300528 zfree(&p);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300529 goto exit_put;
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400530 }
531 continue;
532 }
533
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300534 tal.filtered = 0;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300535 thread__find_addr_location(al.thread, cpumode,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400536 MAP__FUNCTION, ip, &tal);
537
538 if (tal.sym)
539 fprintf(f, "..... %016" PRIx64 " %s\n", ip,
540 tal.sym->name);
541 else
542 fprintf(f, "..... %016" PRIx64 "\n", ip);
543 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300544exit_put:
545 addr_location__put(&al);
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400546exit:
547 fclose(f);
548
549 return p;
550}
551
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300552typedef int (*tracepoint_handler)(struct timechart *tchart,
553 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400554 struct perf_sample *sample,
555 const char *backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +0200556
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300557static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo972ec652013-11-27 16:32:56 -0300558 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200559 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200560 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300561 struct machine *machine)
Arjan van de Ven10274982009-09-12 07:53:05 +0200562{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300563 struct timechart *tchart = container_of(tool, struct timechart, tool);
564
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -0200565 if (evsel->attr.sample_type & PERF_SAMPLE_TIME) {
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300566 if (!tchart->first_time || tchart->first_time > sample->time)
567 tchart->first_time = sample->time;
568 if (tchart->last_time < sample->time)
569 tchart->last_time = sample->time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200570 }
Arjan van de Ven10274982009-09-12 07:53:05 +0200571
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300572 if (evsel->handler != NULL) {
573 tracepoint_handler f = evsel->handler;
Stanislav Fomichev58b9a182013-12-02 18:37:35 +0400574 return f(tchart, evsel, sample,
575 cat_backtrace(event, sample, machine));
Arjan van de Ven10274982009-09-12 07:53:05 +0200576 }
Jiri Olsa59366782013-07-11 17:28:30 +0200577
Arjan van de Ven10274982009-09-12 07:53:05 +0200578 return 0;
579}
580
Jiri Olsa59366782013-07-11 17:28:30 +0200581static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300582process_sample_cpu_idle(struct timechart *tchart __maybe_unused,
583 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400584 struct perf_sample *sample,
585 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200586{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400587 u32 state = perf_evsel__intval(evsel, sample, "state");
588 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200589
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400590 if (state == (u32)PWR_EVENT_EXIT)
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300591 c_state_end(tchart, cpu_id, sample->time);
Jiri Olsa59366782013-07-11 17:28:30 +0200592 else
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400593 c_state_start(cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200594 return 0;
595}
596
597static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300598process_sample_cpu_frequency(struct timechart *tchart,
599 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400600 struct perf_sample *sample,
601 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200602{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400603 u32 state = perf_evsel__intval(evsel, sample, "state");
604 u32 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
Jiri Olsa59366782013-07-11 17:28:30 +0200605
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300606 p_state_change(tchart, cpu_id, sample->time, state);
Jiri Olsa59366782013-07-11 17:28:30 +0200607 return 0;
608}
609
610static int
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300611process_sample_sched_wakeup(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300612 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400613 struct perf_sample *sample,
614 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200615{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400616 u8 flags = perf_evsel__intval(evsel, sample, "common_flags");
617 int waker = perf_evsel__intval(evsel, sample, "common_pid");
618 int wakee = perf_evsel__intval(evsel, sample, "pid");
Jiri Olsa59366782013-07-11 17:28:30 +0200619
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300620 sched_wakeup(tchart, sample->cpu, sample->time, waker, wakee, flags, backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200621 return 0;
622}
623
624static int
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300625process_sample_sched_switch(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300626 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400627 struct perf_sample *sample,
628 const char *backtrace)
Jiri Olsa59366782013-07-11 17:28:30 +0200629{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400630 int prev_pid = perf_evsel__intval(evsel, sample, "prev_pid");
631 int next_pid = perf_evsel__intval(evsel, sample, "next_pid");
632 u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Jiri Olsa59366782013-07-11 17:28:30 +0200633
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300634 sched_switch(tchart, sample->cpu, sample->time, prev_pid, next_pid,
635 prev_state, backtrace);
Jiri Olsa59366782013-07-11 17:28:30 +0200636 return 0;
637}
638
639#ifdef SUPPORT_OLD_POWER_EVENTS
640static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300641process_sample_power_start(struct timechart *tchart __maybe_unused,
642 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400643 struct perf_sample *sample,
644 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200645{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400646 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
647 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200648
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400649 c_state_start(cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200650 return 0;
651}
652
653static int
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300654process_sample_power_end(struct timechart *tchart,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300655 struct perf_evsel *evsel __maybe_unused,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400656 struct perf_sample *sample,
657 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200658{
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300659 c_state_end(tchart, sample->cpu, sample->time);
Jiri Olsa59366782013-07-11 17:28:30 +0200660 return 0;
661}
662
663static int
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300664process_sample_power_frequency(struct timechart *tchart,
665 struct perf_evsel *evsel,
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +0400666 struct perf_sample *sample,
667 const char *backtrace __maybe_unused)
Jiri Olsa59366782013-07-11 17:28:30 +0200668{
Stanislav Fomichev3ed0d212013-11-27 14:45:00 +0400669 u64 cpu_id = perf_evsel__intval(evsel, sample, "cpu_id");
670 u64 value = perf_evsel__intval(evsel, sample, "value");
Jiri Olsa59366782013-07-11 17:28:30 +0200671
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300672 p_state_change(tchart, cpu_id, sample->time, value);
Jiri Olsa59366782013-07-11 17:28:30 +0200673 return 0;
674}
675#endif /* SUPPORT_OLD_POWER_EVENTS */
676
Arjan van de Ven10274982009-09-12 07:53:05 +0200677/*
678 * After the last sample we need to wrap up the current C/P state
679 * and close out each CPU for these.
680 */
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300681static void end_sample_processing(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200682{
683 u64 cpu;
684 struct power_event *pwr;
685
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300686 for (cpu = 0; cpu <= tchart->numcpus; cpu++) {
Arjan van de Ven10274982009-09-12 07:53:05 +0200687 /* C state */
688#if 0
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300689 pwr = zalloc(sizeof(*pwr));
690 if (!pwr)
691 return;
692
Arjan van de Ven10274982009-09-12 07:53:05 +0200693 pwr->state = cpus_cstate_state[cpu];
694 pwr->start_time = cpus_cstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300695 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200696 pwr->cpu = cpu;
697 pwr->type = CSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300698 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200699
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300700 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200701#endif
702 /* P state */
703
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -0300704 pwr = zalloc(sizeof(*pwr));
Arjan van de Ven10274982009-09-12 07:53:05 +0200705 if (!pwr)
706 return;
Arjan van de Ven10274982009-09-12 07:53:05 +0200707
708 pwr->state = cpus_pstate_state[cpu];
709 pwr->start_time = cpus_pstate_start_times[cpu];
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300710 pwr->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200711 pwr->cpu = cpu;
712 pwr->type = PSTATE;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300713 pwr->next = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +0200714
715 if (!pwr->start_time)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300716 pwr->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +0200717 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -0300718 pwr->state = tchart->min_freq;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -0300719 tchart->power_events = pwr;
Arjan van de Ven10274982009-09-12 07:53:05 +0200720 }
721}
722
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400723static int pid_begin_io_sample(struct timechart *tchart, int pid, int type,
724 u64 start, int fd)
725{
726 struct per_pid *p = find_create_pid(tchart, pid);
727 struct per_pidcomm *c = p->current;
728 struct io_sample *sample;
729 struct io_sample *prev;
730
731 if (!c) {
732 c = zalloc(sizeof(*c));
733 if (!c)
734 return -ENOMEM;
735 p->current = c;
736 c->next = p->all;
737 p->all = c;
738 }
739
740 prev = c->io_samples;
741
742 if (prev && prev->start_time && !prev->end_time) {
743 pr_warning("Skip invalid start event: "
744 "previous event already started!\n");
745
746 /* remove previous event that has been started,
747 * we are not sure we will ever get an end for it */
748 c->io_samples = prev->next;
749 free(prev);
750 return 0;
751 }
752
753 sample = zalloc(sizeof(*sample));
754 if (!sample)
755 return -ENOMEM;
756 sample->start_time = start;
757 sample->type = type;
758 sample->fd = fd;
759 sample->next = c->io_samples;
760 c->io_samples = sample;
761
762 if (c->start_time == 0 || c->start_time > start)
763 c->start_time = start;
764
765 return 0;
766}
767
768static int pid_end_io_sample(struct timechart *tchart, int pid, int type,
769 u64 end, long ret)
770{
771 struct per_pid *p = find_create_pid(tchart, pid);
772 struct per_pidcomm *c = p->current;
Stanislav Fomichevd2431442014-07-08 20:03:43 +0400773 struct io_sample *sample, *prev;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400774
775 if (!c) {
776 pr_warning("Invalid pidcomm!\n");
777 return -1;
778 }
779
780 sample = c->io_samples;
781
782 if (!sample) /* skip partially captured events */
783 return 0;
784
785 if (sample->end_time) {
786 pr_warning("Skip invalid end event: "
787 "previous event already ended!\n");
788 return 0;
789 }
790
791 if (sample->type != type) {
792 pr_warning("Skip invalid end event: invalid event type!\n");
793 return 0;
794 }
795
796 sample->end_time = end;
Stanislav Fomichevd2431442014-07-08 20:03:43 +0400797 prev = sample->next;
798
799 /* we want to be able to see small and fast transfers, so make them
800 * at least min_time long, but don't overlap them */
801 if (sample->end_time - sample->start_time < tchart->min_time)
802 sample->end_time = sample->start_time + tchart->min_time;
803 if (prev && sample->start_time < prev->end_time) {
804 if (prev->err) /* try to make errors more visible */
805 sample->start_time = prev->end_time;
806 else
807 prev->end_time = sample->start_time;
808 }
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400809
810 if (ret < 0) {
811 sample->err = ret;
812 } else if (type == IOTYPE_READ || type == IOTYPE_WRITE ||
813 type == IOTYPE_TX || type == IOTYPE_RX) {
814
815 if ((u64)ret > c->max_bytes)
816 c->max_bytes = ret;
817
818 c->total_bytes += ret;
819 p->total_bytes += ret;
820 sample->bytes = ret;
821 }
822
Stanislav Fomichevd2431442014-07-08 20:03:43 +0400823 /* merge two requests to make svg smaller and render-friendly */
824 if (prev &&
825 prev->type == sample->type &&
826 prev->err == sample->err &&
827 prev->fd == sample->fd &&
828 prev->end_time + tchart->merge_dist >= sample->start_time) {
829
830 sample->bytes += prev->bytes;
831 sample->merges += prev->merges + 1;
832
833 sample->start_time = prev->start_time;
834 sample->next = prev->next;
835 free(prev);
836
837 if (!sample->err && sample->bytes > c->max_bytes)
838 c->max_bytes = sample->bytes;
839 }
840
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +0400841 tchart->io_events++;
842
843 return 0;
844}
845
846static int
847process_enter_read(struct timechart *tchart,
848 struct perf_evsel *evsel,
849 struct perf_sample *sample)
850{
851 long fd = perf_evsel__intval(evsel, sample, "fd");
852 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_READ,
853 sample->time, fd);
854}
855
856static int
857process_exit_read(struct timechart *tchart,
858 struct perf_evsel *evsel,
859 struct perf_sample *sample)
860{
861 long ret = perf_evsel__intval(evsel, sample, "ret");
862 return pid_end_io_sample(tchart, sample->tid, IOTYPE_READ,
863 sample->time, ret);
864}
865
866static int
867process_enter_write(struct timechart *tchart,
868 struct perf_evsel *evsel,
869 struct perf_sample *sample)
870{
871 long fd = perf_evsel__intval(evsel, sample, "fd");
872 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_WRITE,
873 sample->time, fd);
874}
875
876static int
877process_exit_write(struct timechart *tchart,
878 struct perf_evsel *evsel,
879 struct perf_sample *sample)
880{
881 long ret = perf_evsel__intval(evsel, sample, "ret");
882 return pid_end_io_sample(tchart, sample->tid, IOTYPE_WRITE,
883 sample->time, ret);
884}
885
886static int
887process_enter_sync(struct timechart *tchart,
888 struct perf_evsel *evsel,
889 struct perf_sample *sample)
890{
891 long fd = perf_evsel__intval(evsel, sample, "fd");
892 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_SYNC,
893 sample->time, fd);
894}
895
896static int
897process_exit_sync(struct timechart *tchart,
898 struct perf_evsel *evsel,
899 struct perf_sample *sample)
900{
901 long ret = perf_evsel__intval(evsel, sample, "ret");
902 return pid_end_io_sample(tchart, sample->tid, IOTYPE_SYNC,
903 sample->time, ret);
904}
905
906static int
907process_enter_tx(struct timechart *tchart,
908 struct perf_evsel *evsel,
909 struct perf_sample *sample)
910{
911 long fd = perf_evsel__intval(evsel, sample, "fd");
912 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_TX,
913 sample->time, fd);
914}
915
916static int
917process_exit_tx(struct timechart *tchart,
918 struct perf_evsel *evsel,
919 struct perf_sample *sample)
920{
921 long ret = perf_evsel__intval(evsel, sample, "ret");
922 return pid_end_io_sample(tchart, sample->tid, IOTYPE_TX,
923 sample->time, ret);
924}
925
926static int
927process_enter_rx(struct timechart *tchart,
928 struct perf_evsel *evsel,
929 struct perf_sample *sample)
930{
931 long fd = perf_evsel__intval(evsel, sample, "fd");
932 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_RX,
933 sample->time, fd);
934}
935
936static int
937process_exit_rx(struct timechart *tchart,
938 struct perf_evsel *evsel,
939 struct perf_sample *sample)
940{
941 long ret = perf_evsel__intval(evsel, sample, "ret");
942 return pid_end_io_sample(tchart, sample->tid, IOTYPE_RX,
943 sample->time, ret);
944}
945
946static int
947process_enter_poll(struct timechart *tchart,
948 struct perf_evsel *evsel,
949 struct perf_sample *sample)
950{
951 long fd = perf_evsel__intval(evsel, sample, "fd");
952 return pid_begin_io_sample(tchart, sample->tid, IOTYPE_POLL,
953 sample->time, fd);
954}
955
956static int
957process_exit_poll(struct timechart *tchart,
958 struct perf_evsel *evsel,
959 struct perf_sample *sample)
960{
961 long ret = perf_evsel__intval(evsel, sample, "ret");
962 return pid_end_io_sample(tchart, sample->tid, IOTYPE_POLL,
963 sample->time, ret);
964}
965
Arjan van de Ven10274982009-09-12 07:53:05 +0200966/*
967 * Sort the pid datastructure
968 */
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300969static void sort_pids(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +0200970{
971 struct per_pid *new_list, *p, *cursor, *prev;
972 /* sort by ppid first, then by pid, lowest to highest */
973
974 new_list = NULL;
975
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -0300976 while (tchart->all_data) {
977 p = tchart->all_data;
978 tchart->all_data = p->next;
Arjan van de Ven10274982009-09-12 07:53:05 +0200979 p->next = NULL;
980
981 if (new_list == NULL) {
982 new_list = p;
983 p->next = NULL;
984 continue;
985 }
986 prev = NULL;
987 cursor = new_list;
988 while (cursor) {
989 if (cursor->ppid > p->ppid ||
990 (cursor->ppid == p->ppid && cursor->pid > p->pid)) {
991 /* must insert before */
992 if (prev) {
993 p->next = prev->next;
994 prev->next = p;
995 cursor = NULL;
996 continue;
997 } else {
998 p->next = new_list;
999 new_list = p;
1000 cursor = NULL;
1001 continue;
1002 }
1003 }
1004
1005 prev = cursor;
1006 cursor = cursor->next;
1007 if (!cursor)
1008 prev->next = p;
1009 }
1010 }
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001011 tchart->all_data = new_list;
Arjan van de Ven10274982009-09-12 07:53:05 +02001012}
1013
1014
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001015static void draw_c_p_states(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001016{
1017 struct power_event *pwr;
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -03001018 pwr = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +02001019
1020 /*
1021 * two pass drawing so that the P state bars are on top of the C state blocks
1022 */
1023 while (pwr) {
1024 if (pwr->type == CSTATE)
1025 svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
1026 pwr = pwr->next;
1027 }
1028
Arnaldo Carvalho de Melo66cc3ad2013-11-28 13:23:05 -03001029 pwr = tchart->power_events;
Arjan van de Ven10274982009-09-12 07:53:05 +02001030 while (pwr) {
1031 if (pwr->type == PSTATE) {
1032 if (!pwr->state)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001033 pwr->state = tchart->min_freq;
Arjan van de Ven10274982009-09-12 07:53:05 +02001034 svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state);
1035 }
1036 pwr = pwr->next;
1037 }
1038}
1039
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001040static void draw_wakeups(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001041{
1042 struct wake_event *we;
1043 struct per_pid *p;
1044 struct per_pidcomm *c;
1045
Arnaldo Carvalho de Melo436b0da2013-11-28 13:26:33 -03001046 we = tchart->wake_events;
Arjan van de Ven10274982009-09-12 07:53:05 +02001047 while (we) {
1048 int from = 0, to = 0;
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001049 char *task_from = NULL, *task_to = NULL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001050
1051 /* locate the column of the waker and wakee */
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001052 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001053 while (p) {
1054 if (p->pid == we->waker || p->pid == we->wakee) {
1055 c = p->all;
1056 while (c) {
1057 if (c->Y && c->start_time <= we->time && c->end_time >= we->time) {
Arjan van de Venbbe29872009-10-20 07:09:39 +09001058 if (p->pid == we->waker && !from) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001059 from = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001060 task_from = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001061 }
Arjan van de Venbbe29872009-10-20 07:09:39 +09001062 if (p->pid == we->wakee && !to) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001063 to = c->Y;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001064 task_to = strdup(c->comm);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +02001065 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001066 }
1067 c = c->next;
1068 }
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001069 c = p->all;
1070 while (c) {
1071 if (p->pid == we->waker && !from) {
1072 from = c->Y;
1073 task_from = strdup(c->comm);
1074 }
1075 if (p->pid == we->wakee && !to) {
1076 to = c->Y;
1077 task_to = strdup(c->comm);
1078 }
1079 c = c->next;
1080 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001081 }
1082 p = p->next;
1083 }
1084
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001085 if (!task_from) {
1086 task_from = malloc(40);
1087 sprintf(task_from, "[%i]", we->waker);
1088 }
1089 if (!task_to) {
1090 task_to = malloc(40);
1091 sprintf(task_to, "[%i]", we->wakee);
1092 }
1093
Arjan van de Ven10274982009-09-12 07:53:05 +02001094 if (we->waker == -1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001095 svg_interrupt(we->time, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001096 else if (from && to && abs(from - to) == 1)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001097 svg_wakeline(we->time, from, to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001098 else
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001099 svg_partial_wakeline(we->time, from, task_from, to,
1100 task_to, we->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001101 we = we->next;
Arjan van de Ven3bc2a392009-10-20 06:46:49 +09001102
1103 free(task_from);
1104 free(task_to);
Arjan van de Ven10274982009-09-12 07:53:05 +02001105 }
1106}
1107
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001108static void draw_cpu_usage(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001109{
1110 struct per_pid *p;
1111 struct per_pidcomm *c;
1112 struct cpu_sample *sample;
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001113 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001114 while (p) {
1115 c = p->all;
1116 while (c) {
1117 sample = c->samples;
1118 while (sample) {
Stanislav Fomichev8b6dcca2013-12-02 18:37:33 +04001119 if (sample->type == TYPE_RUNNING) {
1120 svg_process(sample->cpu,
1121 sample->start_time,
1122 sample->end_time,
Stanislav Fomichevde996222013-12-02 18:37:34 +04001123 p->pid,
Stanislav Fomichev8b6dcca2013-12-02 18:37:33 +04001124 c->comm,
1125 sample->backtrace);
1126 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001127
1128 sample = sample->next;
1129 }
1130 c = c->next;
1131 }
1132 p = p->next;
1133 }
1134}
1135
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001136static void draw_io_bars(struct timechart *tchart)
1137{
1138 const char *suf;
1139 double bytes;
1140 char comm[256];
1141 struct per_pid *p;
1142 struct per_pidcomm *c;
1143 struct io_sample *sample;
1144 int Y = 1;
1145
1146 p = tchart->all_data;
1147 while (p) {
1148 c = p->all;
1149 while (c) {
1150 if (!c->display) {
1151 c->Y = 0;
1152 c = c->next;
1153 continue;
1154 }
1155
1156 svg_box(Y, c->start_time, c->end_time, "process3");
1157 sample = c->io_samples;
1158 for (sample = c->io_samples; sample; sample = sample->next) {
1159 double h = (double)sample->bytes / c->max_bytes;
1160
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001161 if (tchart->skip_eagain &&
1162 sample->err == -EAGAIN)
1163 continue;
1164
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001165 if (sample->err)
1166 h = 1;
1167
1168 if (sample->type == IOTYPE_SYNC)
1169 svg_fbox(Y,
1170 sample->start_time,
1171 sample->end_time,
1172 1,
1173 sample->err ? "error" : "sync",
1174 sample->fd,
1175 sample->err,
1176 sample->merges);
1177 else if (sample->type == IOTYPE_POLL)
1178 svg_fbox(Y,
1179 sample->start_time,
1180 sample->end_time,
1181 1,
1182 sample->err ? "error" : "poll",
1183 sample->fd,
1184 sample->err,
1185 sample->merges);
1186 else if (sample->type == IOTYPE_READ)
1187 svg_ubox(Y,
1188 sample->start_time,
1189 sample->end_time,
1190 h,
1191 sample->err ? "error" : "disk",
1192 sample->fd,
1193 sample->err,
1194 sample->merges);
1195 else if (sample->type == IOTYPE_WRITE)
1196 svg_lbox(Y,
1197 sample->start_time,
1198 sample->end_time,
1199 h,
1200 sample->err ? "error" : "disk",
1201 sample->fd,
1202 sample->err,
1203 sample->merges);
1204 else if (sample->type == IOTYPE_RX)
1205 svg_ubox(Y,
1206 sample->start_time,
1207 sample->end_time,
1208 h,
1209 sample->err ? "error" : "net",
1210 sample->fd,
1211 sample->err,
1212 sample->merges);
1213 else if (sample->type == IOTYPE_TX)
1214 svg_lbox(Y,
1215 sample->start_time,
1216 sample->end_time,
1217 h,
1218 sample->err ? "error" : "net",
1219 sample->fd,
1220 sample->err,
1221 sample->merges);
1222 }
1223
1224 suf = "";
1225 bytes = c->total_bytes;
1226 if (bytes > 1024) {
1227 bytes = bytes / 1024;
1228 suf = "K";
1229 }
1230 if (bytes > 1024) {
1231 bytes = bytes / 1024;
1232 suf = "M";
1233 }
1234 if (bytes > 1024) {
1235 bytes = bytes / 1024;
1236 suf = "G";
1237 }
1238
1239
1240 sprintf(comm, "%s:%i (%3.1f %sbytes)", c->comm ?: "", p->pid, bytes, suf);
1241 svg_text(Y, c->start_time, comm);
1242
1243 c->Y = Y;
1244 Y++;
1245 c = c->next;
1246 }
1247 p = p->next;
1248 }
1249}
1250
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001251static void draw_process_bars(struct timechart *tchart)
Arjan van de Ven10274982009-09-12 07:53:05 +02001252{
1253 struct per_pid *p;
1254 struct per_pidcomm *c;
1255 struct cpu_sample *sample;
1256 int Y = 0;
1257
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001258 Y = 2 * tchart->numcpus + 2;
Arjan van de Ven10274982009-09-12 07:53:05 +02001259
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001260 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001261 while (p) {
1262 c = p->all;
1263 while (c) {
1264 if (!c->display) {
1265 c->Y = 0;
1266 c = c->next;
1267 continue;
1268 }
1269
Arjan van de Vena92fe7b2009-09-20 18:13:53 +02001270 svg_box(Y, c->start_time, c->end_time, "process");
Arjan van de Ven10274982009-09-12 07:53:05 +02001271 sample = c->samples;
1272 while (sample) {
1273 if (sample->type == TYPE_RUNNING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001274 svg_running(Y, sample->cpu,
1275 sample->start_time,
1276 sample->end_time,
1277 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001278 if (sample->type == TYPE_BLOCKED)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001279 svg_blocked(Y, sample->cpu,
1280 sample->start_time,
1281 sample->end_time,
1282 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001283 if (sample->type == TYPE_WAITING)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001284 svg_waiting(Y, sample->cpu,
1285 sample->start_time,
1286 sample->end_time,
1287 sample->backtrace);
Arjan van de Ven10274982009-09-12 07:53:05 +02001288 sample = sample->next;
1289 }
1290
1291 if (c->comm) {
1292 char comm[256];
1293 if (c->total_time > 5000000000) /* 5 seconds */
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001294 sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / (double)NSEC_PER_SEC);
Arjan van de Ven10274982009-09-12 07:53:05 +02001295 else
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001296 sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / (double)NSEC_PER_MSEC);
Arjan van de Ven10274982009-09-12 07:53:05 +02001297
1298 svg_text(Y, c->start_time, comm);
1299 }
1300 c->Y = Y;
1301 Y++;
1302 c = c->next;
1303 }
1304 p = p->next;
1305 }
1306}
1307
Arjan van de Venbbe29872009-10-20 07:09:39 +09001308static void add_process_filter(const char *string)
1309{
Arnaldo Carvalho de Meloe0dcd6f2012-09-24 11:16:40 -03001310 int pid = strtoull(string, NULL, 10);
1311 struct process_filter *filt = malloc(sizeof(*filt));
Arjan van de Venbbe29872009-10-20 07:09:39 +09001312
Arjan van de Venbbe29872009-10-20 07:09:39 +09001313 if (!filt)
1314 return;
1315
1316 filt->name = strdup(string);
1317 filt->pid = pid;
1318 filt->next = process_filter;
1319
1320 process_filter = filt;
1321}
1322
1323static int passes_filter(struct per_pid *p, struct per_pidcomm *c)
1324{
1325 struct process_filter *filt;
1326 if (!process_filter)
1327 return 1;
1328
1329 filt = process_filter;
1330 while (filt) {
1331 if (filt->pid && p->pid == filt->pid)
1332 return 1;
1333 if (strcmp(filt->name, c->comm) == 0)
1334 return 1;
1335 filt = filt->next;
1336 }
1337 return 0;
1338}
1339
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001340static int determine_display_tasks_filtered(struct timechart *tchart)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001341{
1342 struct per_pid *p;
1343 struct per_pidcomm *c;
1344 int count = 0;
1345
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001346 p = tchart->all_data;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001347 while (p) {
1348 p->display = 0;
1349 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001350 p->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001351
1352 /* no exit marker, task kept running to the end */
1353 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001354 p->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001355
1356 c = p->all;
1357
1358 while (c) {
1359 c->display = 0;
1360
1361 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001362 c->start_time = tchart->first_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001363
1364 if (passes_filter(p, c)) {
1365 c->display = 1;
1366 p->display = 1;
1367 count++;
1368 }
1369
1370 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001371 c->end_time = tchart->last_time;
Arjan van de Venbbe29872009-10-20 07:09:39 +09001372
1373 c = c->next;
1374 }
1375 p = p->next;
1376 }
1377 return count;
1378}
1379
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001380static int determine_display_tasks(struct timechart *tchart, u64 threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +02001381{
1382 struct per_pid *p;
1383 struct per_pidcomm *c;
1384 int count = 0;
1385
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001386 p = tchart->all_data;
Arjan van de Ven10274982009-09-12 07:53:05 +02001387 while (p) {
1388 p->display = 0;
1389 if (p->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001390 p->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001391
1392 /* no exit marker, task kept running to the end */
1393 if (p->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001394 p->end_time = tchart->last_time;
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001395 if (p->total_time >= threshold)
Arjan van de Ven10274982009-09-12 07:53:05 +02001396 p->display = 1;
1397
1398 c = p->all;
1399
1400 while (c) {
1401 c->display = 0;
1402
1403 if (c->start_time == 1)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001404 c->start_time = tchart->first_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001405
Stanislav Fomichev753c5052013-11-01 20:25:47 +04001406 if (c->total_time >= threshold) {
Arjan van de Ven10274982009-09-12 07:53:05 +02001407 c->display = 1;
1408 count++;
1409 }
1410
1411 if (c->end_time == 0)
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001412 c->end_time = tchart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001413
1414 c = c->next;
1415 }
1416 p = p->next;
1417 }
1418 return count;
1419}
1420
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001421static int determine_display_io_tasks(struct timechart *timechart, u64 threshold)
1422{
1423 struct per_pid *p;
1424 struct per_pidcomm *c;
1425 int count = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +02001426
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001427 p = timechart->all_data;
1428 while (p) {
1429 /* no exit marker, task kept running to the end */
1430 if (p->end_time == 0)
1431 p->end_time = timechart->last_time;
Arjan van de Ven10274982009-09-12 07:53:05 +02001432
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001433 c = p->all;
1434
1435 while (c) {
1436 c->display = 0;
1437
1438 if (c->total_bytes >= threshold) {
1439 c->display = 1;
1440 count++;
1441 }
1442
1443 if (c->end_time == 0)
1444 c->end_time = timechart->last_time;
1445
1446 c = c->next;
1447 }
1448 p = p->next;
1449 }
1450 return count;
1451}
1452
1453#define BYTES_THRESH (1 * 1024 * 1024)
Arjan van de Ven10274982009-09-12 07:53:05 +02001454#define TIME_THRESH 10000000
1455
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001456static void write_svg_file(struct timechart *tchart, const char *filename)
Arjan van de Ven10274982009-09-12 07:53:05 +02001457{
1458 u64 i;
1459 int count;
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001460 int thresh = tchart->io_events ? BYTES_THRESH : TIME_THRESH;
Arjan van de Ven10274982009-09-12 07:53:05 +02001461
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001462 if (tchart->power_only)
1463 tchart->proc_num = 0;
Arjan van de Ven10274982009-09-12 07:53:05 +02001464
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001465 /* We'd like to show at least proc_num tasks;
1466 * be less picky if we have fewer */
1467 do {
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001468 if (process_filter)
1469 count = determine_display_tasks_filtered(tchart);
1470 else if (tchart->io_events)
1471 count = determine_display_io_tasks(tchart, thresh);
1472 else
1473 count = determine_display_tasks(tchart, thresh);
Stanislav Fomichev0a8eb272013-11-01 20:25:45 +04001474 thresh /= 10;
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001475 } while (!process_filter && thresh && count < tchart->proc_num);
Arjan van de Ven10274982009-09-12 07:53:05 +02001476
Stanislav Fomichev3415d8b2014-01-20 15:39:38 +04001477 if (!tchart->proc_num)
1478 count = 0;
1479
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001480 if (tchart->io_events) {
1481 open_svg(filename, 0, count, tchart->first_time, tchart->last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001482
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001483 svg_time_grid(0.5);
1484 svg_io_legenda();
Arjan van de Ven10274982009-09-12 07:53:05 +02001485
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001486 draw_io_bars(tchart);
1487 } else {
1488 open_svg(filename, tchart->numcpus, count, tchart->first_time, tchart->last_time);
Arjan van de Ven10274982009-09-12 07:53:05 +02001489
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001490 svg_time_grid(0);
1491
1492 svg_legenda();
1493
1494 for (i = 0; i < tchart->numcpus; i++)
1495 svg_cpu_box(i, tchart->max_freq, tchart->turbo_frequency);
1496
1497 draw_cpu_usage(tchart);
1498 if (tchart->proc_num)
1499 draw_process_bars(tchart);
1500 if (!tchart->tasks_only)
1501 draw_c_p_states(tchart);
1502 if (tchart->proc_num)
1503 draw_wakeups(tchart);
1504 }
Arjan van de Ven10274982009-09-12 07:53:05 +02001505
1506 svg_close();
1507}
1508
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001509static int process_header(struct perf_file_section *section __maybe_unused,
1510 struct perf_header *ph,
1511 int feat,
1512 int fd __maybe_unused,
1513 void *data)
1514{
1515 struct timechart *tchart = data;
1516
1517 switch (feat) {
1518 case HEADER_NRCPUS:
1519 tchart->numcpus = ph->env.nr_cpus_avail;
1520 break;
Stanislav Fomichevc5079992013-12-02 18:37:36 +04001521
1522 case HEADER_CPU_TOPOLOGY:
1523 if (!tchart->topology)
1524 break;
1525
1526 if (svg_build_topology_map(ph->env.sibling_cores,
1527 ph->env.nr_sibling_cores,
1528 ph->env.sibling_threads,
1529 ph->env.nr_sibling_threads))
1530 fprintf(stderr, "problem building topology\n");
1531 break;
1532
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001533 default:
1534 break;
1535 }
1536
1537 return 0;
1538}
1539
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001540static int __cmd_timechart(struct timechart *tchart, const char *output_name)
Arjan van de Ven10274982009-09-12 07:53:05 +02001541{
Jiri Olsa59366782013-07-11 17:28:30 +02001542 const struct perf_evsel_str_handler power_tracepoints[] = {
1543 { "power:cpu_idle", process_sample_cpu_idle },
1544 { "power:cpu_frequency", process_sample_cpu_frequency },
1545 { "sched:sched_wakeup", process_sample_sched_wakeup },
1546 { "sched:sched_switch", process_sample_sched_switch },
1547#ifdef SUPPORT_OLD_POWER_EVENTS
1548 { "power:power_start", process_sample_power_start },
1549 { "power:power_end", process_sample_power_end },
1550 { "power:power_frequency", process_sample_power_frequency },
1551#endif
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001552
1553 { "syscalls:sys_enter_read", process_enter_read },
1554 { "syscalls:sys_enter_pread64", process_enter_read },
1555 { "syscalls:sys_enter_readv", process_enter_read },
1556 { "syscalls:sys_enter_preadv", process_enter_read },
1557 { "syscalls:sys_enter_write", process_enter_write },
1558 { "syscalls:sys_enter_pwrite64", process_enter_write },
1559 { "syscalls:sys_enter_writev", process_enter_write },
1560 { "syscalls:sys_enter_pwritev", process_enter_write },
1561 { "syscalls:sys_enter_sync", process_enter_sync },
1562 { "syscalls:sys_enter_sync_file_range", process_enter_sync },
1563 { "syscalls:sys_enter_fsync", process_enter_sync },
1564 { "syscalls:sys_enter_msync", process_enter_sync },
1565 { "syscalls:sys_enter_recvfrom", process_enter_rx },
1566 { "syscalls:sys_enter_recvmmsg", process_enter_rx },
1567 { "syscalls:sys_enter_recvmsg", process_enter_rx },
1568 { "syscalls:sys_enter_sendto", process_enter_tx },
1569 { "syscalls:sys_enter_sendmsg", process_enter_tx },
1570 { "syscalls:sys_enter_sendmmsg", process_enter_tx },
1571 { "syscalls:sys_enter_epoll_pwait", process_enter_poll },
1572 { "syscalls:sys_enter_epoll_wait", process_enter_poll },
1573 { "syscalls:sys_enter_poll", process_enter_poll },
1574 { "syscalls:sys_enter_ppoll", process_enter_poll },
1575 { "syscalls:sys_enter_pselect6", process_enter_poll },
1576 { "syscalls:sys_enter_select", process_enter_poll },
1577
1578 { "syscalls:sys_exit_read", process_exit_read },
1579 { "syscalls:sys_exit_pread64", process_exit_read },
1580 { "syscalls:sys_exit_readv", process_exit_read },
1581 { "syscalls:sys_exit_preadv", process_exit_read },
1582 { "syscalls:sys_exit_write", process_exit_write },
1583 { "syscalls:sys_exit_pwrite64", process_exit_write },
1584 { "syscalls:sys_exit_writev", process_exit_write },
1585 { "syscalls:sys_exit_pwritev", process_exit_write },
1586 { "syscalls:sys_exit_sync", process_exit_sync },
1587 { "syscalls:sys_exit_sync_file_range", process_exit_sync },
1588 { "syscalls:sys_exit_fsync", process_exit_sync },
1589 { "syscalls:sys_exit_msync", process_exit_sync },
1590 { "syscalls:sys_exit_recvfrom", process_exit_rx },
1591 { "syscalls:sys_exit_recvmmsg", process_exit_rx },
1592 { "syscalls:sys_exit_recvmsg", process_exit_rx },
1593 { "syscalls:sys_exit_sendto", process_exit_tx },
1594 { "syscalls:sys_exit_sendmsg", process_exit_tx },
1595 { "syscalls:sys_exit_sendmmsg", process_exit_tx },
1596 { "syscalls:sys_exit_epoll_pwait", process_exit_poll },
1597 { "syscalls:sys_exit_epoll_wait", process_exit_poll },
1598 { "syscalls:sys_exit_poll", process_exit_poll },
1599 { "syscalls:sys_exit_ppoll", process_exit_poll },
1600 { "syscalls:sys_exit_pselect6", process_exit_poll },
1601 { "syscalls:sys_exit_select", process_exit_poll },
Jiri Olsa59366782013-07-11 17:28:30 +02001602 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001603 struct perf_data_file file = {
1604 .path = input_name,
1605 .mode = PERF_DATA_MODE_READ,
Yunlong Song44f7e432015-04-02 21:47:17 +08001606 .force = tchart->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001607 };
1608
1609 struct perf_session *session = perf_session__new(&file, false,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001610 &tchart->tool);
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001611 int ret = -EINVAL;
Arjan van de Ven10274982009-09-12 07:53:05 +02001612
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001613 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001614 return -1;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001615
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001616 symbol__init(&session->header.env);
Namhyung Kimdc5c8192014-08-12 15:40:43 +09001617
Stanislav Fomichev58b9a182013-12-02 18:37:35 +04001618 (void)perf_header__process_sections(&session->header,
1619 perf_data_file__fd(session->file),
1620 tchart,
1621 process_header);
1622
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001623 if (!perf_session__has_traces(session, "timechart record"))
1624 goto out_delete;
1625
Jiri Olsa59366782013-07-11 17:28:30 +02001626 if (perf_session__set_tracepoints_handlers(session,
1627 power_tracepoints)) {
1628 pr_err("Initializing session tracepoint handlers failed\n");
1629 goto out_delete;
1630 }
1631
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001632 ret = perf_session__process_events(session);
Li Zefan5cbd0802009-12-01 14:05:16 +08001633 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001634 goto out_delete;
Arjan van de Ven10274982009-09-12 07:53:05 +02001635
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001636 end_sample_processing(tchart);
Arjan van de Ven10274982009-09-12 07:53:05 +02001637
Arnaldo Carvalho de Melo5e22f6d2013-11-28 11:50:41 -03001638 sort_pids(tchart);
Arjan van de Ven10274982009-09-12 07:53:05 +02001639
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001640 write_svg_file(tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02001641
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001642 pr_info("Written %2.1f seconds of trace to %s.\n",
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001643 (tchart->last_time - tchart->first_time) / (double)NSEC_PER_SEC, output_name);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001644out_delete:
1645 perf_session__delete(session);
1646 return ret;
Arjan van de Ven10274982009-09-12 07:53:05 +02001647}
1648
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001649static int timechart__io_record(int argc, const char **argv)
1650{
1651 unsigned int rec_argc, i;
1652 const char **rec_argv;
1653 const char **p;
1654 char *filter = NULL;
1655
1656 const char * const common_args[] = {
1657 "record", "-a", "-R", "-c", "1",
1658 };
1659 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1660
1661 const char * const disk_events[] = {
1662 "syscalls:sys_enter_read",
1663 "syscalls:sys_enter_pread64",
1664 "syscalls:sys_enter_readv",
1665 "syscalls:sys_enter_preadv",
1666 "syscalls:sys_enter_write",
1667 "syscalls:sys_enter_pwrite64",
1668 "syscalls:sys_enter_writev",
1669 "syscalls:sys_enter_pwritev",
1670 "syscalls:sys_enter_sync",
1671 "syscalls:sys_enter_sync_file_range",
1672 "syscalls:sys_enter_fsync",
1673 "syscalls:sys_enter_msync",
1674
1675 "syscalls:sys_exit_read",
1676 "syscalls:sys_exit_pread64",
1677 "syscalls:sys_exit_readv",
1678 "syscalls:sys_exit_preadv",
1679 "syscalls:sys_exit_write",
1680 "syscalls:sys_exit_pwrite64",
1681 "syscalls:sys_exit_writev",
1682 "syscalls:sys_exit_pwritev",
1683 "syscalls:sys_exit_sync",
1684 "syscalls:sys_exit_sync_file_range",
1685 "syscalls:sys_exit_fsync",
1686 "syscalls:sys_exit_msync",
1687 };
1688 unsigned int disk_events_nr = ARRAY_SIZE(disk_events);
1689
1690 const char * const net_events[] = {
1691 "syscalls:sys_enter_recvfrom",
1692 "syscalls:sys_enter_recvmmsg",
1693 "syscalls:sys_enter_recvmsg",
1694 "syscalls:sys_enter_sendto",
1695 "syscalls:sys_enter_sendmsg",
1696 "syscalls:sys_enter_sendmmsg",
1697
1698 "syscalls:sys_exit_recvfrom",
1699 "syscalls:sys_exit_recvmmsg",
1700 "syscalls:sys_exit_recvmsg",
1701 "syscalls:sys_exit_sendto",
1702 "syscalls:sys_exit_sendmsg",
1703 "syscalls:sys_exit_sendmmsg",
1704 };
1705 unsigned int net_events_nr = ARRAY_SIZE(net_events);
1706
1707 const char * const poll_events[] = {
1708 "syscalls:sys_enter_epoll_pwait",
1709 "syscalls:sys_enter_epoll_wait",
1710 "syscalls:sys_enter_poll",
1711 "syscalls:sys_enter_ppoll",
1712 "syscalls:sys_enter_pselect6",
1713 "syscalls:sys_enter_select",
1714
1715 "syscalls:sys_exit_epoll_pwait",
1716 "syscalls:sys_exit_epoll_wait",
1717 "syscalls:sys_exit_poll",
1718 "syscalls:sys_exit_ppoll",
1719 "syscalls:sys_exit_pselect6",
1720 "syscalls:sys_exit_select",
1721 };
1722 unsigned int poll_events_nr = ARRAY_SIZE(poll_events);
1723
1724 rec_argc = common_args_nr +
1725 disk_events_nr * 4 +
1726 net_events_nr * 4 +
1727 poll_events_nr * 4 +
1728 argc;
1729 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1730
1731 if (rec_argv == NULL)
1732 return -ENOMEM;
1733
1734 if (asprintf(&filter, "common_pid != %d", getpid()) < 0)
1735 return -ENOMEM;
1736
1737 p = rec_argv;
1738 for (i = 0; i < common_args_nr; i++)
1739 *p++ = strdup(common_args[i]);
1740
1741 for (i = 0; i < disk_events_nr; i++) {
1742 if (!is_valid_tracepoint(disk_events[i])) {
1743 rec_argc -= 4;
1744 continue;
1745 }
1746
1747 *p++ = "-e";
1748 *p++ = strdup(disk_events[i]);
1749 *p++ = "--filter";
1750 *p++ = filter;
1751 }
1752 for (i = 0; i < net_events_nr; i++) {
1753 if (!is_valid_tracepoint(net_events[i])) {
1754 rec_argc -= 4;
1755 continue;
1756 }
1757
1758 *p++ = "-e";
1759 *p++ = strdup(net_events[i]);
1760 *p++ = "--filter";
1761 *p++ = filter;
1762 }
1763 for (i = 0; i < poll_events_nr; i++) {
1764 if (!is_valid_tracepoint(poll_events[i])) {
1765 rec_argc -= 4;
1766 continue;
1767 }
1768
1769 *p++ = "-e";
1770 *p++ = strdup(poll_events[i]);
1771 *p++ = "--filter";
1772 *p++ = filter;
1773 }
1774
1775 for (i = 0; i < (unsigned int)argc; i++)
1776 *p++ = argv[i];
1777
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001778 return cmd_record(rec_argc, rec_argv);
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001779}
1780
1781
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001782static int timechart__record(struct timechart *tchart, int argc, const char **argv)
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001783{
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001784 unsigned int rec_argc, i, j;
1785 const char **rec_argv;
1786 const char **p;
1787 unsigned int record_elems;
1788
1789 const char * const common_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +02001790 "record", "-a", "-R", "-c", "1",
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001791 };
1792 unsigned int common_args_nr = ARRAY_SIZE(common_args);
1793
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001794 const char * const backtrace_args[] = {
1795 "-g",
1796 };
1797 unsigned int backtrace_args_no = ARRAY_SIZE(backtrace_args);
1798
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001799 const char * const power_args[] = {
1800 "-e", "power:cpu_frequency",
1801 "-e", "power:cpu_idle",
1802 };
1803 unsigned int power_args_nr = ARRAY_SIZE(power_args);
1804
1805 const char * const old_power_args[] = {
1806#ifdef SUPPORT_OLD_POWER_EVENTS
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001807 "-e", "power:power_start",
1808 "-e", "power:power_end",
1809 "-e", "power:power_frequency",
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001810#endif
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001811 };
1812 unsigned int old_power_args_nr = ARRAY_SIZE(old_power_args);
1813
1814 const char * const tasks_args[] = {
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001815 "-e", "sched:sched_wakeup",
1816 "-e", "sched:sched_switch",
1817 };
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001818 unsigned int tasks_args_nr = ARRAY_SIZE(tasks_args);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001819
Thomas Renninger20c457b2011-01-03 17:50:45 +01001820#ifdef SUPPORT_OLD_POWER_EVENTS
1821 if (!is_valid_tracepoint("power:cpu_idle") &&
1822 is_valid_tracepoint("power:power_start")) {
1823 use_old_power_events = 1;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001824 power_args_nr = 0;
1825 } else {
1826 old_power_args_nr = 0;
Thomas Renninger20c457b2011-01-03 17:50:45 +01001827 }
1828#endif
1829
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001830 if (tchart->power_only)
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001831 tasks_args_nr = 0;
1832
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001833 if (tchart->tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001834 power_args_nr = 0;
1835 old_power_args_nr = 0;
1836 }
1837
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001838 if (!tchart->with_backtrace)
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001839 backtrace_args_no = 0;
1840
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001841 record_elems = common_args_nr + tasks_args_nr +
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001842 power_args_nr + old_power_args_nr + backtrace_args_no;
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001843
1844 rec_argc = record_elems + argc;
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001845 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1846
Chris Samuelce47dc52010-11-13 13:35:06 +11001847 if (rec_argv == NULL)
1848 return -ENOMEM;
1849
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001850 p = rec_argv;
1851 for (i = 0; i < common_args_nr; i++)
1852 *p++ = strdup(common_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001853
Stanislav Fomichev6f8d67f2013-11-01 20:25:51 +04001854 for (i = 0; i < backtrace_args_no; i++)
1855 *p++ = strdup(backtrace_args[i]);
1856
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001857 for (i = 0; i < tasks_args_nr; i++)
1858 *p++ = strdup(tasks_args[i]);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001859
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001860 for (i = 0; i < power_args_nr; i++)
1861 *p++ = strdup(power_args[i]);
1862
1863 for (i = 0; i < old_power_args_nr; i++)
1864 *p++ = strdup(old_power_args[i]);
1865
Ramkumar Ramachandra263f89b2014-03-16 12:06:05 -04001866 for (j = 0; j < (unsigned int)argc; j++)
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001867 *p++ = argv[j];
1868
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001869 return cmd_record(rec_argc, rec_argv);
Arjan van de Ven3c09eeb2009-09-19 13:34:42 +02001870}
1871
Arjan van de Venbbe29872009-10-20 07:09:39 +09001872static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001873parse_process(const struct option *opt __maybe_unused, const char *arg,
1874 int __maybe_unused unset)
Arjan van de Venbbe29872009-10-20 07:09:39 +09001875{
1876 if (arg)
1877 add_process_filter(arg);
1878 return 0;
1879}
1880
Stanislav Fomicheve57a2df2013-12-17 19:53:49 +04001881static int
1882parse_highlight(const struct option *opt __maybe_unused, const char *arg,
1883 int __maybe_unused unset)
1884{
1885 unsigned long duration = strtoul(arg, NULL, 0);
1886
1887 if (svg_highlight || svg_highlight_name)
1888 return -1;
1889
1890 if (duration)
1891 svg_highlight = duration;
1892 else
1893 svg_highlight_name = strdup(arg);
1894
1895 return 0;
1896}
1897
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001898static int
1899parse_time(const struct option *opt, const char *arg, int __maybe_unused unset)
1900{
1901 char unit = 'n';
1902 u64 *value = opt->value;
1903
1904 if (sscanf(arg, "%" PRIu64 "%cs", value, &unit) > 0) {
1905 switch (unit) {
1906 case 'm':
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001907 *value *= NSEC_PER_MSEC;
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001908 break;
1909 case 'u':
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001910 *value *= NSEC_PER_USEC;
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001911 break;
1912 case 'n':
1913 break;
1914 default:
1915 return -1;
1916 }
1917 }
1918
1919 return 0;
1920}
1921
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001922int cmd_timechart(int argc, const char **argv)
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001923{
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001924 struct timechart tchart = {
1925 .tool = {
1926 .comm = process_comm_event,
1927 .fork = process_fork_event,
1928 .exit = process_exit_event,
1929 .sample = process_sample_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001930 .ordered_events = true,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001931 },
1932 .proc_num = 15,
Arnaldo Carvalho de Meloaf4b2c92016-08-08 12:45:58 -03001933 .min_time = NSEC_PER_MSEC,
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001934 .merge_dist = 1000,
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001935 };
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001936 const char *output_name = "output.svg";
Arnaldo Carvalho de Melof3715942017-03-17 11:23:21 -03001937 const struct option timechart_common_options[] = {
1938 OPT_BOOLEAN('P', "power-only", &tchart.power_only, "output power data only"),
1939 OPT_BOOLEAN('T', "tasks-only", &tchart.tasks_only, "output processes data only"),
1940 OPT_END()
1941 };
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001942 const struct option timechart_options[] = {
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001943 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1944 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1945 OPT_INTEGER('w', "width", &svg_page_width, "page width"),
Stanislav Fomicheve57a2df2013-12-17 19:53:49 +04001946 OPT_CALLBACK(0, "highlight", NULL, "duration or task name",
1947 "highlight tasks. Pass duration in ns or process name.",
1948 parse_highlight),
Arjan van de Venbbe29872009-10-20 07:09:39 +09001949 OPT_CALLBACK('p', "process", NULL, "process",
1950 "process selector. Pass a pid or process name.",
1951 parse_process),
He Kuanga7066702016-05-19 11:47:37 +00001952 OPT_CALLBACK(0, "symfs", NULL, "directory",
1953 "Look for files with symbols relative to this directory",
1954 symbol__config_symfs),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001955 OPT_INTEGER('n', "proc-num", &tchart.proc_num,
Stanislav Fomichev54874e32013-11-01 20:25:46 +04001956 "min. number of tasks to print"),
Stanislav Fomichevc5079992013-12-02 18:37:36 +04001957 OPT_BOOLEAN('t', "topology", &tchart.topology,
1958 "sort CPUs according to topology"),
Stanislav Fomichevd2431442014-07-08 20:03:43 +04001959 OPT_BOOLEAN(0, "io-skip-eagain", &tchart.skip_eagain,
1960 "skip EAGAIN errors"),
1961 OPT_CALLBACK(0, "io-min-time", &tchart.min_time, "time",
1962 "all IO faster than min-time will visually appear longer",
1963 parse_time),
1964 OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
1965 "merge events that are merge-dist us apart",
1966 parse_time),
Yunlong Song44f7e432015-04-02 21:47:17 +08001967 OPT_BOOLEAN('f', "force", &tchart.force, "don't complain, do it"),
Arnaldo Carvalho de Melof3715942017-03-17 11:23:21 -03001968 OPT_PARENT(timechart_common_options),
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001969 };
Yunlong Song33ec0ca2015-03-18 21:35:56 +08001970 const char * const timechart_subcommands[] = { "record", NULL };
1971 const char *timechart_usage[] = {
Arnaldo Carvalho de Melo73bdc712012-10-01 15:20:58 -03001972 "perf timechart [<options>] {record}",
1973 NULL
1974 };
Namhyung Kime5b2c202014-10-23 00:15:46 +09001975 const struct option timechart_record_options[] = {
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04001976 OPT_BOOLEAN('I', "io-only", &tchart.io_only,
1977 "record only IO data"),
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001978 OPT_BOOLEAN('g', "callchain", &tchart.with_backtrace, "record callchain"),
Arnaldo Carvalho de Melof3715942017-03-17 11:23:21 -03001979 OPT_PARENT(timechart_common_options),
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001980 };
Namhyung Kime5b2c202014-10-23 00:15:46 +09001981 const char * const timechart_record_usage[] = {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001982 "perf timechart record [<options>]",
1983 NULL
1984 };
Yunlong Song33ec0ca2015-03-18 21:35:56 +08001985 argc = parse_options_subcommand(argc, argv, timechart_options, timechart_subcommands,
1986 timechart_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Arjan van de Ven10274982009-09-12 07:53:05 +02001987
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001988 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichevc87097d2013-11-01 20:25:48 +04001989 pr_err("-P and -T options cannot be used at the same time.\n");
1990 return -1;
1991 }
1992
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001993 if (argc && !strncmp(argv[0], "rec", 3)) {
Namhyung Kime5b2c202014-10-23 00:15:46 +09001994 argc = parse_options(argc, argv, timechart_record_options,
1995 timechart_record_usage,
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001996 PARSE_OPT_STOP_AT_NON_OPTION);
1997
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03001998 if (tchart.power_only && tchart.tasks_only) {
Stanislav Fomichev367b3152013-11-01 20:25:50 +04001999 pr_err("-P and -T options cannot be used at the same time.\n");
2000 return -1;
2001 }
2002
Stanislav Fomichevb97b59b2014-07-08 20:03:41 +04002003 if (tchart.io_only)
2004 return timechart__io_record(argc, argv);
2005 else
2006 return timechart__record(&tchart, argc, argv);
Stanislav Fomichev367b3152013-11-01 20:25:50 +04002007 } else if (argc)
2008 usage_with_options(timechart_usage, timechart_options);
Arjan van de Ven10274982009-09-12 07:53:05 +02002009
2010 setup_pager();
2011
Arnaldo Carvalho de Melo985b12e2013-11-28 11:25:19 -03002012 return __cmd_timechart(&tchart, output_name);
Arjan van de Ven10274982009-09-12 07:53:05 +02002013}