Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1 | /* |
| 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 Olsa | c85cffa | 2013-07-11 17:28:29 +0200 | [diff] [blame] | 15 | #include <traceevent/event-parse.h> |
| 16 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 17 | #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 Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 24 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | e3f4260 | 2011-11-16 17:02:54 -0200 | [diff] [blame] | 25 | #include "util/evsel.h" |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 26 | #include <linux/rbtree.h> |
| 27 | #include "util/symbol.h" |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 28 | #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 Zefan | 5cbd080 | 2009-12-01 14:05:16 +0800 | [diff] [blame] | 35 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 36 | #include "util/session.h" |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 37 | #include "util/svghelper.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 38 | #include "util/tool.h" |
Jiri Olsa | f5fc141 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 39 | #include "util/data.h" |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 40 | |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 41 | #define SUPPORT_OLD_POWER_EVENTS 1 |
| 42 | #define PWR_EVENT_EXIT -1 |
| 43 | |
Stanislav Fomichev | 54874e3 | 2013-11-01 20:25:46 +0400 | [diff] [blame] | 44 | static int proc_num = 15; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 45 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 46 | static unsigned int numcpus; |
| 47 | static u64 min_freq; /* Lowest CPU frequency seen */ |
| 48 | static u64 max_freq; /* Highest CPU frequency seen */ |
| 49 | static u64 turbo_frequency; |
| 50 | |
| 51 | static u64 first_time, last_time; |
| 52 | |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 53 | static bool power_only; |
Stanislav Fomichev | c87097d | 2013-11-01 20:25:48 +0400 | [diff] [blame^] | 54 | static bool tasks_only; |
Arjan van de Ven | 39a90a8 | 2009-09-24 15:40:13 +0200 | [diff] [blame] | 55 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 56 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 57 | struct per_pid; |
| 58 | struct per_pidcomm; |
| 59 | |
| 60 | struct cpu_sample; |
| 61 | struct power_event; |
| 62 | struct wake_event; |
| 63 | |
| 64 | struct sample_wrapper; |
| 65 | |
| 66 | /* |
| 67 | * Datastructure layout: |
| 68 | * We keep an list of "pid"s, matching the kernels notion of a task struct. |
| 69 | * Each "pid" entry, has a list of "comm"s. |
| 70 | * this is because we want to track different programs different, while |
| 71 | * exec will reuse the original pid (by design). |
| 72 | * Each comm has a list of samples that will be used to draw |
| 73 | * final graph. |
| 74 | */ |
| 75 | |
| 76 | struct per_pid { |
| 77 | struct per_pid *next; |
| 78 | |
| 79 | int pid; |
| 80 | int ppid; |
| 81 | |
| 82 | u64 start_time; |
| 83 | u64 end_time; |
| 84 | u64 total_time; |
| 85 | int display; |
| 86 | |
| 87 | struct per_pidcomm *all; |
| 88 | struct per_pidcomm *current; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | |
| 92 | struct per_pidcomm { |
| 93 | struct per_pidcomm *next; |
| 94 | |
| 95 | u64 start_time; |
| 96 | u64 end_time; |
| 97 | u64 total_time; |
| 98 | |
| 99 | int Y; |
| 100 | int display; |
| 101 | |
| 102 | long state; |
| 103 | u64 state_since; |
| 104 | |
| 105 | char *comm; |
| 106 | |
| 107 | struct cpu_sample *samples; |
| 108 | }; |
| 109 | |
| 110 | struct sample_wrapper { |
| 111 | struct sample_wrapper *next; |
| 112 | |
| 113 | u64 timestamp; |
| 114 | unsigned char data[0]; |
| 115 | }; |
| 116 | |
| 117 | #define TYPE_NONE 0 |
| 118 | #define TYPE_RUNNING 1 |
| 119 | #define TYPE_WAITING 2 |
| 120 | #define TYPE_BLOCKED 3 |
| 121 | |
| 122 | struct cpu_sample { |
| 123 | struct cpu_sample *next; |
| 124 | |
| 125 | u64 start_time; |
| 126 | u64 end_time; |
| 127 | int type; |
| 128 | int cpu; |
| 129 | }; |
| 130 | |
| 131 | static struct per_pid *all_data; |
| 132 | |
| 133 | #define CSTATE 1 |
| 134 | #define PSTATE 2 |
| 135 | |
| 136 | struct power_event { |
| 137 | struct power_event *next; |
| 138 | int type; |
| 139 | int state; |
| 140 | u64 start_time; |
| 141 | u64 end_time; |
| 142 | int cpu; |
| 143 | }; |
| 144 | |
| 145 | struct wake_event { |
| 146 | struct wake_event *next; |
| 147 | int waker; |
| 148 | int wakee; |
| 149 | u64 time; |
| 150 | }; |
| 151 | |
| 152 | static struct power_event *power_events; |
| 153 | static struct wake_event *wake_events; |
| 154 | |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 155 | struct process_filter; |
| 156 | struct process_filter { |
Li Zefan | 5cbd080 | 2009-12-01 14:05:16 +0800 | [diff] [blame] | 157 | char *name; |
| 158 | int pid; |
| 159 | struct process_filter *next; |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | static struct process_filter *process_filter; |
| 163 | |
| 164 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 165 | static struct per_pid *find_create_pid(int pid) |
| 166 | { |
| 167 | struct per_pid *cursor = all_data; |
| 168 | |
| 169 | while (cursor) { |
| 170 | if (cursor->pid == pid) |
| 171 | return cursor; |
| 172 | cursor = cursor->next; |
| 173 | } |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 174 | cursor = zalloc(sizeof(*cursor)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 175 | assert(cursor != NULL); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 176 | cursor->pid = pid; |
| 177 | cursor->next = all_data; |
| 178 | all_data = cursor; |
| 179 | return cursor; |
| 180 | } |
| 181 | |
| 182 | static void pid_set_comm(int pid, char *comm) |
| 183 | { |
| 184 | struct per_pid *p; |
| 185 | struct per_pidcomm *c; |
| 186 | p = find_create_pid(pid); |
| 187 | c = p->all; |
| 188 | while (c) { |
| 189 | if (c->comm && strcmp(c->comm, comm) == 0) { |
| 190 | p->current = c; |
| 191 | return; |
| 192 | } |
| 193 | if (!c->comm) { |
| 194 | c->comm = strdup(comm); |
| 195 | p->current = c; |
| 196 | return; |
| 197 | } |
| 198 | c = c->next; |
| 199 | } |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 200 | c = zalloc(sizeof(*c)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 201 | assert(c != NULL); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 202 | c->comm = strdup(comm); |
| 203 | p->current = c; |
| 204 | c->next = p->all; |
| 205 | p->all = c; |
| 206 | } |
| 207 | |
| 208 | static void pid_fork(int pid, int ppid, u64 timestamp) |
| 209 | { |
| 210 | struct per_pid *p, *pp; |
| 211 | p = find_create_pid(pid); |
| 212 | pp = find_create_pid(ppid); |
| 213 | p->ppid = ppid; |
| 214 | if (pp->current && pp->current->comm && !p->current) |
| 215 | pid_set_comm(pid, pp->current->comm); |
| 216 | |
| 217 | p->start_time = timestamp; |
| 218 | if (p->current) { |
| 219 | p->current->start_time = timestamp; |
| 220 | p->current->state_since = timestamp; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void pid_exit(int pid, u64 timestamp) |
| 225 | { |
| 226 | struct per_pid *p; |
| 227 | p = find_create_pid(pid); |
| 228 | p->end_time = timestamp; |
| 229 | if (p->current) |
| 230 | p->current->end_time = timestamp; |
| 231 | } |
| 232 | |
| 233 | static void |
| 234 | pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end) |
| 235 | { |
| 236 | struct per_pid *p; |
| 237 | struct per_pidcomm *c; |
| 238 | struct cpu_sample *sample; |
| 239 | |
| 240 | p = find_create_pid(pid); |
| 241 | c = p->current; |
| 242 | if (!c) { |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 243 | c = zalloc(sizeof(*c)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 244 | assert(c != NULL); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 245 | p->current = c; |
| 246 | c->next = p->all; |
| 247 | p->all = c; |
| 248 | } |
| 249 | |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 250 | sample = zalloc(sizeof(*sample)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 251 | assert(sample != NULL); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 252 | sample->start_time = start; |
| 253 | sample->end_time = end; |
| 254 | sample->type = type; |
| 255 | sample->next = c->samples; |
| 256 | sample->cpu = cpu; |
| 257 | c->samples = sample; |
| 258 | |
| 259 | if (sample->type == TYPE_RUNNING && end > start && start > 0) { |
| 260 | c->total_time += (end-start); |
| 261 | p->total_time += (end-start); |
| 262 | } |
| 263 | |
| 264 | if (c->start_time == 0 || c->start_time > start) |
| 265 | c->start_time = start; |
| 266 | if (p->start_time == 0 || p->start_time > start) |
| 267 | p->start_time = start; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | #define MAX_CPUS 4096 |
| 271 | |
| 272 | static u64 cpus_cstate_start_times[MAX_CPUS]; |
| 273 | static int cpus_cstate_state[MAX_CPUS]; |
| 274 | static u64 cpus_pstate_start_times[MAX_CPUS]; |
| 275 | static u64 cpus_pstate_state[MAX_CPUS]; |
| 276 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 277 | static int process_comm_event(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 278 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 279 | struct perf_sample *sample __maybe_unused, |
| 280 | struct machine *machine __maybe_unused) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 281 | { |
Arjan van de Ven | 8f06d7e | 2010-01-16 12:53:19 -0800 | [diff] [blame] | 282 | pid_set_comm(event->comm.tid, event->comm.comm); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 283 | return 0; |
| 284 | } |
Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 285 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 286 | static int process_fork_event(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 287 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 288 | struct perf_sample *sample __maybe_unused, |
| 289 | struct machine *machine __maybe_unused) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 290 | { |
| 291 | pid_fork(event->fork.pid, event->fork.ppid, event->fork.time); |
| 292 | return 0; |
| 293 | } |
| 294 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 295 | static int process_exit_event(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 296 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 297 | struct perf_sample *sample __maybe_unused, |
| 298 | struct machine *machine __maybe_unused) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 299 | { |
| 300 | pid_exit(event->fork.pid, event->fork.time); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | struct trace_entry { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 305 | unsigned short type; |
| 306 | unsigned char flags; |
| 307 | unsigned char preempt_count; |
| 308 | int pid; |
OGAWA Hirofumi | 028c515 | 2009-12-06 20:07:29 +0900 | [diff] [blame] | 309 | int lock_depth; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 310 | }; |
| 311 | |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 312 | #ifdef SUPPORT_OLD_POWER_EVENTS |
| 313 | static int use_old_power_events; |
| 314 | struct power_entry_old { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 315 | struct trace_entry te; |
Thomas Renninger | 4c21adf | 2010-07-20 16:59:34 -0700 | [diff] [blame] | 316 | u64 type; |
| 317 | u64 value; |
| 318 | u64 cpu_id; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 319 | }; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 320 | #endif |
| 321 | |
| 322 | struct power_processor_entry { |
| 323 | struct trace_entry te; |
| 324 | u32 state; |
| 325 | u32 cpu_id; |
| 326 | }; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 327 | |
| 328 | #define TASK_COMM_LEN 16 |
| 329 | struct wakeup_entry { |
| 330 | struct trace_entry te; |
| 331 | char comm[TASK_COMM_LEN]; |
| 332 | int pid; |
| 333 | int prio; |
| 334 | int success; |
| 335 | }; |
| 336 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 337 | struct sched_switch { |
| 338 | struct trace_entry te; |
| 339 | char prev_comm[TASK_COMM_LEN]; |
| 340 | int prev_pid; |
| 341 | int prev_prio; |
| 342 | long prev_state; /* Arjan weeps. */ |
| 343 | char next_comm[TASK_COMM_LEN]; |
| 344 | int next_pid; |
| 345 | int next_prio; |
| 346 | }; |
| 347 | |
| 348 | static 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 | |
| 354 | static void c_state_end(int cpu, u64 timestamp) |
| 355 | { |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 356 | struct power_event *pwr = zalloc(sizeof(*pwr)); |
| 357 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 358 | if (!pwr) |
| 359 | return; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 360 | |
| 361 | pwr->state = cpus_cstate_state[cpu]; |
| 362 | pwr->start_time = cpus_cstate_start_times[cpu]; |
| 363 | pwr->end_time = timestamp; |
| 364 | pwr->cpu = cpu; |
| 365 | pwr->type = CSTATE; |
| 366 | pwr->next = power_events; |
| 367 | |
| 368 | power_events = pwr; |
| 369 | } |
| 370 | |
| 371 | static void p_state_change(int cpu, u64 timestamp, u64 new_freq) |
| 372 | { |
| 373 | struct power_event *pwr; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 374 | |
| 375 | if (new_freq > 8000000) /* detect invalid data */ |
| 376 | return; |
| 377 | |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 378 | pwr = zalloc(sizeof(*pwr)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 379 | if (!pwr) |
| 380 | return; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 381 | |
| 382 | pwr->state = cpus_pstate_state[cpu]; |
| 383 | pwr->start_time = cpus_pstate_start_times[cpu]; |
| 384 | pwr->end_time = timestamp; |
| 385 | pwr->cpu = cpu; |
| 386 | pwr->type = PSTATE; |
| 387 | pwr->next = power_events; |
| 388 | |
| 389 | if (!pwr->start_time) |
| 390 | pwr->start_time = first_time; |
| 391 | |
| 392 | power_events = pwr; |
| 393 | |
| 394 | cpus_pstate_state[cpu] = new_freq; |
| 395 | cpus_pstate_start_times[cpu] = timestamp; |
| 396 | |
| 397 | if ((u64)new_freq > max_freq) |
| 398 | max_freq = new_freq; |
| 399 | |
| 400 | if (new_freq < min_freq || min_freq == 0) |
| 401 | min_freq = new_freq; |
| 402 | |
| 403 | if (new_freq == max_freq - 1000) |
| 404 | turbo_frequency = max_freq; |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te) |
| 409 | { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 410 | struct per_pid *p; |
| 411 | struct wakeup_entry *wake = (void *)te; |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 412 | struct wake_event *we = zalloc(sizeof(*we)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 413 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 414 | if (!we) |
| 415 | return; |
| 416 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 417 | we->time = timestamp; |
| 418 | we->waker = pid; |
| 419 | |
| 420 | if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ)) |
| 421 | we->waker = -1; |
| 422 | |
| 423 | we->wakee = wake->pid; |
| 424 | we->next = wake_events; |
| 425 | wake_events = we; |
| 426 | p = find_create_pid(we->wakee); |
| 427 | |
| 428 | if (p && p->current && p->current->state == TYPE_NONE) { |
| 429 | p->current->state_since = timestamp; |
| 430 | p->current->state = TYPE_WAITING; |
| 431 | } |
| 432 | if (p && p->current && p->current->state == TYPE_BLOCKED) { |
| 433 | pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp); |
| 434 | p->current->state_since = timestamp; |
| 435 | p->current->state = TYPE_WAITING; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te) |
| 440 | { |
| 441 | struct per_pid *p = NULL, *prev_p; |
| 442 | struct sched_switch *sw = (void *)te; |
| 443 | |
| 444 | |
| 445 | prev_p = find_create_pid(sw->prev_pid); |
| 446 | |
| 447 | p = find_create_pid(sw->next_pid); |
| 448 | |
| 449 | if (prev_p->current && prev_p->current->state != TYPE_NONE) |
| 450 | pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp); |
| 451 | if (p && p->current) { |
| 452 | if (p->current->state != TYPE_NONE) |
| 453 | pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp); |
| 454 | |
Julia Lawall | 33e26a1 | 2010-08-05 22:27:51 +0200 | [diff] [blame] | 455 | p->current->state_since = timestamp; |
| 456 | p->current->state = TYPE_RUNNING; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (prev_p->current) { |
| 460 | prev_p->current->state = TYPE_NONE; |
| 461 | prev_p->current->state_since = timestamp; |
| 462 | if (sw->prev_state & 2) |
| 463 | prev_p->current->state = TYPE_BLOCKED; |
| 464 | if (sw->prev_state == 0) |
| 465 | prev_p->current->state = TYPE_WAITING; |
| 466 | } |
| 467 | } |
| 468 | |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 469 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
| 470 | struct perf_sample *sample); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 471 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 472 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
| 473 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 474 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | e3f4260 | 2011-11-16 17:02:54 -0200 | [diff] [blame] | 475 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 476 | struct machine *machine __maybe_unused) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 477 | { |
Arnaldo Carvalho de Melo | e3f4260 | 2011-11-16 17:02:54 -0200 | [diff] [blame] | 478 | if (evsel->attr.sample_type & PERF_SAMPLE_TIME) { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 479 | if (!first_time || first_time > sample->time) |
| 480 | first_time = sample->time; |
| 481 | if (last_time < sample->time) |
| 482 | last_time = sample->time; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 483 | } |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 484 | |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 485 | if (sample->cpu > numcpus) |
| 486 | numcpus = sample->cpu; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 487 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 488 | if (evsel->handler != NULL) { |
| 489 | tracepoint_handler f = evsel->handler; |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 490 | return f(evsel, sample); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 491 | } |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 492 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 496 | static int |
| 497 | process_sample_cpu_idle(struct perf_evsel *evsel __maybe_unused, |
| 498 | struct perf_sample *sample) |
| 499 | { |
| 500 | struct power_processor_entry *ppe = sample->raw_data; |
| 501 | |
| 502 | if (ppe->state == (u32) PWR_EVENT_EXIT) |
| 503 | c_state_end(ppe->cpu_id, sample->time); |
| 504 | else |
| 505 | c_state_start(ppe->cpu_id, sample->time, ppe->state); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int |
| 510 | process_sample_cpu_frequency(struct perf_evsel *evsel __maybe_unused, |
| 511 | struct perf_sample *sample) |
| 512 | { |
| 513 | struct power_processor_entry *ppe = sample->raw_data; |
| 514 | |
| 515 | p_state_change(ppe->cpu_id, sample->time, ppe->state); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int |
| 520 | process_sample_sched_wakeup(struct perf_evsel *evsel __maybe_unused, |
| 521 | struct perf_sample *sample) |
| 522 | { |
| 523 | struct trace_entry *te = sample->raw_data; |
| 524 | |
| 525 | sched_wakeup(sample->cpu, sample->time, sample->pid, te); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int |
| 530 | process_sample_sched_switch(struct perf_evsel *evsel __maybe_unused, |
| 531 | struct perf_sample *sample) |
| 532 | { |
| 533 | struct trace_entry *te = sample->raw_data; |
| 534 | |
| 535 | sched_switch(sample->cpu, sample->time, te); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | #ifdef SUPPORT_OLD_POWER_EVENTS |
| 540 | static int |
| 541 | process_sample_power_start(struct perf_evsel *evsel __maybe_unused, |
| 542 | struct perf_sample *sample) |
| 543 | { |
| 544 | struct power_entry_old *peo = sample->raw_data; |
| 545 | |
| 546 | c_state_start(peo->cpu_id, sample->time, peo->value); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | process_sample_power_end(struct perf_evsel *evsel __maybe_unused, |
| 552 | struct perf_sample *sample) |
| 553 | { |
| 554 | c_state_end(sample->cpu, sample->time); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int |
| 559 | process_sample_power_frequency(struct perf_evsel *evsel __maybe_unused, |
| 560 | struct perf_sample *sample) |
| 561 | { |
| 562 | struct power_entry_old *peo = sample->raw_data; |
| 563 | |
| 564 | p_state_change(peo->cpu_id, sample->time, peo->value); |
| 565 | return 0; |
| 566 | } |
| 567 | #endif /* SUPPORT_OLD_POWER_EVENTS */ |
| 568 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 569 | /* |
| 570 | * After the last sample we need to wrap up the current C/P state |
| 571 | * and close out each CPU for these. |
| 572 | */ |
| 573 | static void end_sample_processing(void) |
| 574 | { |
| 575 | u64 cpu; |
| 576 | struct power_event *pwr; |
| 577 | |
Arjan van de Ven | 39a90a8 | 2009-09-24 15:40:13 +0200 | [diff] [blame] | 578 | for (cpu = 0; cpu <= numcpus; cpu++) { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 579 | /* C state */ |
| 580 | #if 0 |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 581 | pwr = zalloc(sizeof(*pwr)); |
| 582 | if (!pwr) |
| 583 | return; |
| 584 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 585 | pwr->state = cpus_cstate_state[cpu]; |
| 586 | pwr->start_time = cpus_cstate_start_times[cpu]; |
| 587 | pwr->end_time = last_time; |
| 588 | pwr->cpu = cpu; |
| 589 | pwr->type = CSTATE; |
| 590 | pwr->next = power_events; |
| 591 | |
| 592 | power_events = pwr; |
| 593 | #endif |
| 594 | /* P state */ |
| 595 | |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 596 | pwr = zalloc(sizeof(*pwr)); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 597 | if (!pwr) |
| 598 | return; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 599 | |
| 600 | pwr->state = cpus_pstate_state[cpu]; |
| 601 | pwr->start_time = cpus_pstate_start_times[cpu]; |
| 602 | pwr->end_time = last_time; |
| 603 | pwr->cpu = cpu; |
| 604 | pwr->type = PSTATE; |
| 605 | pwr->next = power_events; |
| 606 | |
| 607 | if (!pwr->start_time) |
| 608 | pwr->start_time = first_time; |
| 609 | if (!pwr->state) |
| 610 | pwr->state = min_freq; |
| 611 | power_events = pwr; |
| 612 | } |
| 613 | } |
| 614 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 615 | /* |
| 616 | * Sort the pid datastructure |
| 617 | */ |
| 618 | static void sort_pids(void) |
| 619 | { |
| 620 | struct per_pid *new_list, *p, *cursor, *prev; |
| 621 | /* sort by ppid first, then by pid, lowest to highest */ |
| 622 | |
| 623 | new_list = NULL; |
| 624 | |
| 625 | while (all_data) { |
| 626 | p = all_data; |
| 627 | all_data = p->next; |
| 628 | p->next = NULL; |
| 629 | |
| 630 | if (new_list == NULL) { |
| 631 | new_list = p; |
| 632 | p->next = NULL; |
| 633 | continue; |
| 634 | } |
| 635 | prev = NULL; |
| 636 | cursor = new_list; |
| 637 | while (cursor) { |
| 638 | if (cursor->ppid > p->ppid || |
| 639 | (cursor->ppid == p->ppid && cursor->pid > p->pid)) { |
| 640 | /* must insert before */ |
| 641 | if (prev) { |
| 642 | p->next = prev->next; |
| 643 | prev->next = p; |
| 644 | cursor = NULL; |
| 645 | continue; |
| 646 | } else { |
| 647 | p->next = new_list; |
| 648 | new_list = p; |
| 649 | cursor = NULL; |
| 650 | continue; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | prev = cursor; |
| 655 | cursor = cursor->next; |
| 656 | if (!cursor) |
| 657 | prev->next = p; |
| 658 | } |
| 659 | } |
| 660 | all_data = new_list; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | static void draw_c_p_states(void) |
| 665 | { |
| 666 | struct power_event *pwr; |
| 667 | pwr = power_events; |
| 668 | |
| 669 | /* |
| 670 | * two pass drawing so that the P state bars are on top of the C state blocks |
| 671 | */ |
| 672 | while (pwr) { |
| 673 | if (pwr->type == CSTATE) |
| 674 | svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state); |
| 675 | pwr = pwr->next; |
| 676 | } |
| 677 | |
| 678 | pwr = power_events; |
| 679 | while (pwr) { |
| 680 | if (pwr->type == PSTATE) { |
| 681 | if (!pwr->state) |
| 682 | pwr->state = min_freq; |
| 683 | svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state); |
| 684 | } |
| 685 | pwr = pwr->next; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static void draw_wakeups(void) |
| 690 | { |
| 691 | struct wake_event *we; |
| 692 | struct per_pid *p; |
| 693 | struct per_pidcomm *c; |
| 694 | |
| 695 | we = wake_events; |
| 696 | while (we) { |
| 697 | int from = 0, to = 0; |
Arjan van de Ven | 4f1202c | 2009-09-20 18:13:28 +0200 | [diff] [blame] | 698 | char *task_from = NULL, *task_to = NULL; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 699 | |
| 700 | /* locate the column of the waker and wakee */ |
| 701 | p = all_data; |
| 702 | while (p) { |
| 703 | if (p->pid == we->waker || p->pid == we->wakee) { |
| 704 | c = p->all; |
| 705 | while (c) { |
| 706 | if (c->Y && c->start_time <= we->time && c->end_time >= we->time) { |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 707 | if (p->pid == we->waker && !from) { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 708 | from = c->Y; |
Arjan van de Ven | 3bc2a39 | 2009-10-20 06:46:49 +0900 | [diff] [blame] | 709 | task_from = strdup(c->comm); |
Arjan van de Ven | 4f1202c | 2009-09-20 18:13:28 +0200 | [diff] [blame] | 710 | } |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 711 | if (p->pid == we->wakee && !to) { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 712 | to = c->Y; |
Arjan van de Ven | 3bc2a39 | 2009-10-20 06:46:49 +0900 | [diff] [blame] | 713 | task_to = strdup(c->comm); |
Arjan van de Ven | 4f1202c | 2009-09-20 18:13:28 +0200 | [diff] [blame] | 714 | } |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 715 | } |
| 716 | c = c->next; |
| 717 | } |
Arjan van de Ven | 3bc2a39 | 2009-10-20 06:46:49 +0900 | [diff] [blame] | 718 | c = p->all; |
| 719 | while (c) { |
| 720 | if (p->pid == we->waker && !from) { |
| 721 | from = c->Y; |
| 722 | task_from = strdup(c->comm); |
| 723 | } |
| 724 | if (p->pid == we->wakee && !to) { |
| 725 | to = c->Y; |
| 726 | task_to = strdup(c->comm); |
| 727 | } |
| 728 | c = c->next; |
| 729 | } |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 730 | } |
| 731 | p = p->next; |
| 732 | } |
| 733 | |
Arjan van de Ven | 3bc2a39 | 2009-10-20 06:46:49 +0900 | [diff] [blame] | 734 | if (!task_from) { |
| 735 | task_from = malloc(40); |
| 736 | sprintf(task_from, "[%i]", we->waker); |
| 737 | } |
| 738 | if (!task_to) { |
| 739 | task_to = malloc(40); |
| 740 | sprintf(task_to, "[%i]", we->wakee); |
| 741 | } |
| 742 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 743 | if (we->waker == -1) |
| 744 | svg_interrupt(we->time, to); |
| 745 | else if (from && to && abs(from - to) == 1) |
| 746 | svg_wakeline(we->time, from, to); |
| 747 | else |
Arjan van de Ven | 4f1202c | 2009-09-20 18:13:28 +0200 | [diff] [blame] | 748 | svg_partial_wakeline(we->time, from, task_from, to, task_to); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 749 | we = we->next; |
Arjan van de Ven | 3bc2a39 | 2009-10-20 06:46:49 +0900 | [diff] [blame] | 750 | |
| 751 | free(task_from); |
| 752 | free(task_to); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
| 756 | static void draw_cpu_usage(void) |
| 757 | { |
| 758 | struct per_pid *p; |
| 759 | struct per_pidcomm *c; |
| 760 | struct cpu_sample *sample; |
| 761 | p = all_data; |
| 762 | while (p) { |
| 763 | c = p->all; |
| 764 | while (c) { |
| 765 | sample = c->samples; |
| 766 | while (sample) { |
| 767 | if (sample->type == TYPE_RUNNING) |
| 768 | svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm); |
| 769 | |
| 770 | sample = sample->next; |
| 771 | } |
| 772 | c = c->next; |
| 773 | } |
| 774 | p = p->next; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | static void draw_process_bars(void) |
| 779 | { |
| 780 | struct per_pid *p; |
| 781 | struct per_pidcomm *c; |
| 782 | struct cpu_sample *sample; |
| 783 | int Y = 0; |
| 784 | |
| 785 | Y = 2 * numcpus + 2; |
| 786 | |
| 787 | p = all_data; |
| 788 | while (p) { |
| 789 | c = p->all; |
| 790 | while (c) { |
| 791 | if (!c->display) { |
| 792 | c->Y = 0; |
| 793 | c = c->next; |
| 794 | continue; |
| 795 | } |
| 796 | |
Arjan van de Ven | a92fe7b | 2009-09-20 18:13:53 +0200 | [diff] [blame] | 797 | svg_box(Y, c->start_time, c->end_time, "process"); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 798 | sample = c->samples; |
| 799 | while (sample) { |
| 800 | if (sample->type == TYPE_RUNNING) |
Arjan van de Ven | a92fe7b | 2009-09-20 18:13:53 +0200 | [diff] [blame] | 801 | svg_sample(Y, sample->cpu, sample->start_time, sample->end_time); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 802 | if (sample->type == TYPE_BLOCKED) |
| 803 | svg_box(Y, sample->start_time, sample->end_time, "blocked"); |
| 804 | if (sample->type == TYPE_WAITING) |
Arjan van de Ven | a92fe7b | 2009-09-20 18:13:53 +0200 | [diff] [blame] | 805 | svg_waiting(Y, sample->start_time, sample->end_time); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 806 | sample = sample->next; |
| 807 | } |
| 808 | |
| 809 | if (c->comm) { |
| 810 | char comm[256]; |
| 811 | if (c->total_time > 5000000000) /* 5 seconds */ |
| 812 | sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0); |
| 813 | else |
| 814 | sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0); |
| 815 | |
| 816 | svg_text(Y, c->start_time, comm); |
| 817 | } |
| 818 | c->Y = Y; |
| 819 | Y++; |
| 820 | c = c->next; |
| 821 | } |
| 822 | p = p->next; |
| 823 | } |
| 824 | } |
| 825 | |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 826 | static void add_process_filter(const char *string) |
| 827 | { |
Arnaldo Carvalho de Melo | e0dcd6f | 2012-09-24 11:16:40 -0300 | [diff] [blame] | 828 | int pid = strtoull(string, NULL, 10); |
| 829 | struct process_filter *filt = malloc(sizeof(*filt)); |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 830 | |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 831 | if (!filt) |
| 832 | return; |
| 833 | |
| 834 | filt->name = strdup(string); |
| 835 | filt->pid = pid; |
| 836 | filt->next = process_filter; |
| 837 | |
| 838 | process_filter = filt; |
| 839 | } |
| 840 | |
| 841 | static int passes_filter(struct per_pid *p, struct per_pidcomm *c) |
| 842 | { |
| 843 | struct process_filter *filt; |
| 844 | if (!process_filter) |
| 845 | return 1; |
| 846 | |
| 847 | filt = process_filter; |
| 848 | while (filt) { |
| 849 | if (filt->pid && p->pid == filt->pid) |
| 850 | return 1; |
| 851 | if (strcmp(filt->name, c->comm) == 0) |
| 852 | return 1; |
| 853 | filt = filt->next; |
| 854 | } |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int determine_display_tasks_filtered(void) |
| 859 | { |
| 860 | struct per_pid *p; |
| 861 | struct per_pidcomm *c; |
| 862 | int count = 0; |
| 863 | |
| 864 | p = all_data; |
| 865 | while (p) { |
| 866 | p->display = 0; |
| 867 | if (p->start_time == 1) |
| 868 | p->start_time = first_time; |
| 869 | |
| 870 | /* no exit marker, task kept running to the end */ |
| 871 | if (p->end_time == 0) |
| 872 | p->end_time = last_time; |
| 873 | |
| 874 | c = p->all; |
| 875 | |
| 876 | while (c) { |
| 877 | c->display = 0; |
| 878 | |
| 879 | if (c->start_time == 1) |
| 880 | c->start_time = first_time; |
| 881 | |
| 882 | if (passes_filter(p, c)) { |
| 883 | c->display = 1; |
| 884 | p->display = 1; |
| 885 | count++; |
| 886 | } |
| 887 | |
| 888 | if (c->end_time == 0) |
| 889 | c->end_time = last_time; |
| 890 | |
| 891 | c = c->next; |
| 892 | } |
| 893 | p = p->next; |
| 894 | } |
| 895 | return count; |
| 896 | } |
| 897 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 898 | static int determine_display_tasks(u64 threshold) |
| 899 | { |
| 900 | struct per_pid *p; |
| 901 | struct per_pidcomm *c; |
| 902 | int count = 0; |
| 903 | |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 904 | if (process_filter) |
| 905 | return determine_display_tasks_filtered(); |
| 906 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 907 | p = all_data; |
| 908 | while (p) { |
| 909 | p->display = 0; |
| 910 | if (p->start_time == 1) |
| 911 | p->start_time = first_time; |
| 912 | |
| 913 | /* no exit marker, task kept running to the end */ |
| 914 | if (p->end_time == 0) |
| 915 | p->end_time = last_time; |
Stanislav Fomichev | 753c505 | 2013-11-01 20:25:47 +0400 | [diff] [blame] | 916 | if (p->total_time >= threshold) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 917 | p->display = 1; |
| 918 | |
| 919 | c = p->all; |
| 920 | |
| 921 | while (c) { |
| 922 | c->display = 0; |
| 923 | |
| 924 | if (c->start_time == 1) |
| 925 | c->start_time = first_time; |
| 926 | |
Stanislav Fomichev | 753c505 | 2013-11-01 20:25:47 +0400 | [diff] [blame] | 927 | if (c->total_time >= threshold) { |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 928 | c->display = 1; |
| 929 | count++; |
| 930 | } |
| 931 | |
| 932 | if (c->end_time == 0) |
| 933 | c->end_time = last_time; |
| 934 | |
| 935 | c = c->next; |
| 936 | } |
| 937 | p = p->next; |
| 938 | } |
| 939 | return count; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | |
| 944 | #define TIME_THRESH 10000000 |
| 945 | |
| 946 | static void write_svg_file(const char *filename) |
| 947 | { |
| 948 | u64 i; |
| 949 | int count; |
Stanislav Fomichev | 0a8eb27 | 2013-11-01 20:25:45 +0400 | [diff] [blame] | 950 | int thresh = TIME_THRESH; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 951 | |
| 952 | numcpus++; |
| 953 | |
Stanislav Fomichev | 753c505 | 2013-11-01 20:25:47 +0400 | [diff] [blame] | 954 | if (power_only) |
| 955 | proc_num = 0; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 956 | |
Stanislav Fomichev | 0a8eb27 | 2013-11-01 20:25:45 +0400 | [diff] [blame] | 957 | /* We'd like to show at least proc_num tasks; |
| 958 | * be less picky if we have fewer */ |
| 959 | do { |
| 960 | count = determine_display_tasks(thresh); |
| 961 | thresh /= 10; |
Stanislav Fomichev | 54874e3 | 2013-11-01 20:25:46 +0400 | [diff] [blame] | 962 | } while (!process_filter && thresh && count < proc_num); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 963 | |
Arjan van de Ven | 5094b65 | 2009-09-20 18:14:16 +0200 | [diff] [blame] | 964 | open_svg(filename, numcpus, count, first_time, last_time); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 965 | |
Arjan van de Ven | 5094b65 | 2009-09-20 18:14:16 +0200 | [diff] [blame] | 966 | svg_time_grid(); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 967 | svg_legenda(); |
| 968 | |
| 969 | for (i = 0; i < numcpus; i++) |
| 970 | svg_cpu_box(i, max_freq, turbo_frequency); |
| 971 | |
| 972 | draw_cpu_usage(); |
Stanislav Fomichev | 753c505 | 2013-11-01 20:25:47 +0400 | [diff] [blame] | 973 | if (proc_num) |
| 974 | draw_process_bars(); |
Stanislav Fomichev | c87097d | 2013-11-01 20:25:48 +0400 | [diff] [blame^] | 975 | if (!tasks_only) |
| 976 | draw_c_p_states(); |
Stanislav Fomichev | 753c505 | 2013-11-01 20:25:47 +0400 | [diff] [blame] | 977 | if (proc_num) |
| 978 | draw_wakeups(); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 979 | |
| 980 | svg_close(); |
| 981 | } |
| 982 | |
Feng Tang | 70cb4e9 | 2012-10-30 11:56:02 +0800 | [diff] [blame] | 983 | static int __cmd_timechart(const char *output_name) |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 984 | { |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 985 | struct perf_tool perf_timechart = { |
| 986 | .comm = process_comm_event, |
| 987 | .fork = process_fork_event, |
| 988 | .exit = process_exit_event, |
| 989 | .sample = process_sample_event, |
| 990 | .ordered_samples = true, |
| 991 | }; |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 992 | const struct perf_evsel_str_handler power_tracepoints[] = { |
| 993 | { "power:cpu_idle", process_sample_cpu_idle }, |
| 994 | { "power:cpu_frequency", process_sample_cpu_frequency }, |
| 995 | { "sched:sched_wakeup", process_sample_sched_wakeup }, |
| 996 | { "sched:sched_switch", process_sample_sched_switch }, |
| 997 | #ifdef SUPPORT_OLD_POWER_EVENTS |
| 998 | { "power:power_start", process_sample_power_start }, |
| 999 | { "power:power_end", process_sample_power_end }, |
| 1000 | { "power:power_frequency", process_sample_power_frequency }, |
| 1001 | #endif |
| 1002 | }; |
Jiri Olsa | f5fc141 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1003 | struct perf_data_file file = { |
| 1004 | .path = input_name, |
| 1005 | .mode = PERF_DATA_MODE_READ, |
| 1006 | }; |
| 1007 | |
| 1008 | struct perf_session *session = perf_session__new(&file, false, |
| 1009 | &perf_timechart); |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1010 | int ret = -EINVAL; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1011 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1012 | if (session == NULL) |
| 1013 | return -ENOMEM; |
| 1014 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1015 | if (!perf_session__has_traces(session, "timechart record")) |
| 1016 | goto out_delete; |
| 1017 | |
Jiri Olsa | 5936678 | 2013-07-11 17:28:30 +0200 | [diff] [blame] | 1018 | if (perf_session__set_tracepoints_handlers(session, |
| 1019 | power_tracepoints)) { |
| 1020 | pr_err("Initializing session tracepoint handlers failed\n"); |
| 1021 | goto out_delete; |
| 1022 | } |
| 1023 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1024 | ret = perf_session__process_events(session, &perf_timechart); |
Li Zefan | 5cbd080 | 2009-12-01 14:05:16 +0800 | [diff] [blame] | 1025 | if (ret) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1026 | goto out_delete; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1027 | |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1028 | end_sample_processing(); |
| 1029 | |
| 1030 | sort_pids(); |
| 1031 | |
| 1032 | write_svg_file(output_name); |
| 1033 | |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 1034 | pr_info("Written %2.1f seconds of trace to %s.\n", |
| 1035 | (last_time - first_time) / 1000000000.0, output_name); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1036 | out_delete: |
| 1037 | perf_session__delete(session); |
| 1038 | return ret; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1041 | static int __cmd_record(int argc, const char **argv) |
| 1042 | { |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1043 | #ifdef SUPPORT_OLD_POWER_EVENTS |
| 1044 | const char * const record_old_args[] = { |
Jiri Olsa | 4a4d371 | 2013-06-05 13:37:21 +0200 | [diff] [blame] | 1045 | "record", "-a", "-R", "-c", "1", |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1046 | "-e", "power:power_start", |
| 1047 | "-e", "power:power_end", |
| 1048 | "-e", "power:power_frequency", |
| 1049 | "-e", "sched:sched_wakeup", |
| 1050 | "-e", "sched:sched_switch", |
| 1051 | }; |
| 1052 | #endif |
| 1053 | const char * const record_new_args[] = { |
Jiri Olsa | 4a4d371 | 2013-06-05 13:37:21 +0200 | [diff] [blame] | 1054 | "record", "-a", "-R", "-c", "1", |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1055 | "-e", "power:cpu_frequency", |
| 1056 | "-e", "power:cpu_idle", |
| 1057 | "-e", "sched:sched_wakeup", |
| 1058 | "-e", "sched:sched_switch", |
| 1059 | }; |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1060 | unsigned int rec_argc, i, j; |
| 1061 | const char **rec_argv; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1062 | const char * const *record_args = record_new_args; |
| 1063 | unsigned int record_elems = ARRAY_SIZE(record_new_args); |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1064 | |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1065 | #ifdef SUPPORT_OLD_POWER_EVENTS |
| 1066 | if (!is_valid_tracepoint("power:cpu_idle") && |
| 1067 | is_valid_tracepoint("power:power_start")) { |
| 1068 | use_old_power_events = 1; |
| 1069 | record_args = record_old_args; |
| 1070 | record_elems = ARRAY_SIZE(record_old_args); |
| 1071 | } |
| 1072 | #endif |
| 1073 | |
| 1074 | rec_argc = record_elems + argc - 1; |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1075 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1076 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 1077 | if (rec_argv == NULL) |
| 1078 | return -ENOMEM; |
| 1079 | |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1080 | for (i = 0; i < record_elems; i++) |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1081 | rec_argv[i] = strdup(record_args[i]); |
| 1082 | |
| 1083 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 1084 | rec_argv[i] = argv[j]; |
| 1085 | |
| 1086 | return cmd_record(i, rec_argv, NULL); |
| 1087 | } |
| 1088 | |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 1089 | static int |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1090 | parse_process(const struct option *opt __maybe_unused, const char *arg, |
| 1091 | int __maybe_unused unset) |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 1092 | { |
| 1093 | if (arg) |
| 1094 | add_process_filter(arg); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1098 | int cmd_timechart(int argc, const char **argv, |
| 1099 | const char *prefix __maybe_unused) |
| 1100 | { |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1101 | const char *output_name = "output.svg"; |
| 1102 | const struct option options[] = { |
| 1103 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
| 1104 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
| 1105 | OPT_INTEGER('w', "width", &svg_page_width, "page width"), |
| 1106 | OPT_BOOLEAN('P', "power-only", &power_only, "output power data only"), |
Stanislav Fomichev | c87097d | 2013-11-01 20:25:48 +0400 | [diff] [blame^] | 1107 | OPT_BOOLEAN('T', "tasks-only", &tasks_only, |
| 1108 | "output processes data only"), |
Arjan van de Ven | bbe2987 | 2009-10-20 07:09:39 +0900 | [diff] [blame] | 1109 | OPT_CALLBACK('p', "process", NULL, "process", |
| 1110 | "process selector. Pass a pid or process name.", |
| 1111 | parse_process), |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 1112 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
| 1113 | "Look for files with symbols relative to this directory"), |
Stanislav Fomichev | 54874e3 | 2013-11-01 20:25:46 +0400 | [diff] [blame] | 1114 | OPT_INTEGER('n', "proc-num", &proc_num, |
| 1115 | "min. number of tasks to print"), |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1116 | OPT_END() |
Arnaldo Carvalho de Melo | 73bdc715 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 1117 | }; |
| 1118 | const char * const timechart_usage[] = { |
| 1119 | "perf timechart [<options>] {record}", |
| 1120 | NULL |
| 1121 | }; |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1122 | |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1123 | argc = parse_options(argc, argv, options, timechart_usage, |
| 1124 | PARSE_OPT_STOP_AT_NON_OPTION); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1125 | |
Stanislav Fomichev | c87097d | 2013-11-01 20:25:48 +0400 | [diff] [blame^] | 1126 | if (power_only && tasks_only) { |
| 1127 | pr_err("-P and -T options cannot be used at the same time.\n"); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1131 | symbol__init(); |
| 1132 | |
Arjan van de Ven | 3c09eeb | 2009-09-19 13:34:42 +0200 | [diff] [blame] | 1133 | if (argc && !strncmp(argv[0], "rec", 3)) |
| 1134 | return __cmd_record(argc, argv); |
| 1135 | else if (argc) |
| 1136 | usage_with_options(timechart_usage, options); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1137 | |
| 1138 | setup_pager(); |
| 1139 | |
Feng Tang | 70cb4e9 | 2012-10-30 11:56:02 +0800 | [diff] [blame] | 1140 | return __cmd_timechart(output_name); |
Arjan van de Ven | 1027498 | 2009-09-12 07:53:05 +0200 | [diff] [blame] | 1141 | } |