Commit | Line | Data |
---|---|---|
10274989 AV |
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 | ||
c85cffa5 JO |
15 | #include <traceevent/event-parse.h> |
16 | ||
10274989 AV |
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" | |
5936678e | 24 | #include "util/evlist.h" |
e3f42609 | 25 | #include "util/evsel.h" |
10274989 AV |
26 | #include <linux/rbtree.h> |
27 | #include "util/symbol.h" | |
10274989 AV |
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" | |
5cbd0805 | 35 | #include "util/event.h" |
301a0b02 | 36 | #include "util/session.h" |
10274989 | 37 | #include "util/svghelper.h" |
45694aa7 | 38 | #include "util/tool.h" |
f5fc1412 | 39 | #include "util/data.h" |
10274989 | 40 | |
20c457b8 TR |
41 | #define SUPPORT_OLD_POWER_EVENTS 1 |
42 | #define PWR_EVENT_EXIT -1 | |
43 | ||
54874e32 | 44 | static int proc_num = 15; |
20c457b8 | 45 | |
10274989 AV |
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 | ||
c0555642 | 53 | static bool power_only; |
c87097d3 | 54 | static bool tasks_only; |
39a90a8e | 55 | |
10274989 | 56 | |
10274989 AV |
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; | |
10274989 AV |
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 | ||
bbe2987b AV |
155 | struct process_filter; |
156 | struct process_filter { | |
5cbd0805 LZ |
157 | char *name; |
158 | int pid; | |
159 | struct process_filter *next; | |
bbe2987b AV |
160 | }; |
161 | ||
162 | static struct process_filter *process_filter; | |
163 | ||
164 | ||
10274989 AV |
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 | } | |
e0dcd6fb | 174 | cursor = zalloc(sizeof(*cursor)); |
10274989 | 175 | assert(cursor != NULL); |
10274989 AV |
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 | } | |
e0dcd6fb | 200 | c = zalloc(sizeof(*c)); |
10274989 | 201 | assert(c != NULL); |
10274989 AV |
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) { | |
e0dcd6fb | 243 | c = zalloc(sizeof(*c)); |
10274989 | 244 | assert(c != NULL); |
10274989 AV |
245 | p->current = c; |
246 | c->next = p->all; | |
247 | p->all = c; | |
248 | } | |
249 | ||
e0dcd6fb | 250 | sample = zalloc(sizeof(*sample)); |
10274989 | 251 | assert(sample != NULL); |
10274989 AV |
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; | |
10274989 AV |
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 | ||
1d037ca1 | 277 | static int process_comm_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 278 | union perf_event *event, |
1d037ca1 IT |
279 | struct perf_sample *sample __maybe_unused, |
280 | struct machine *machine __maybe_unused) | |
10274989 | 281 | { |
8f06d7e6 | 282 | pid_set_comm(event->comm.tid, event->comm.comm); |
10274989 AV |
283 | return 0; |
284 | } | |
d8f66248 | 285 | |
1d037ca1 | 286 | static int process_fork_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 287 | union perf_event *event, |
1d037ca1 IT |
288 | struct perf_sample *sample __maybe_unused, |
289 | struct machine *machine __maybe_unused) | |
10274989 AV |
290 | { |
291 | pid_fork(event->fork.pid, event->fork.ppid, event->fork.time); | |
292 | return 0; | |
293 | } | |
294 | ||
1d037ca1 | 295 | static int process_exit_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 296 | union perf_event *event, |
1d037ca1 IT |
297 | struct perf_sample *sample __maybe_unused, |
298 | struct machine *machine __maybe_unused) | |
10274989 AV |
299 | { |
300 | pid_exit(event->fork.pid, event->fork.time); | |
301 | return 0; | |
302 | } | |
303 | ||
304 | struct trace_entry { | |
10274989 AV |
305 | unsigned short type; |
306 | unsigned char flags; | |
307 | unsigned char preempt_count; | |
308 | int pid; | |
028c5152 | 309 | int lock_depth; |
10274989 AV |
310 | }; |
311 | ||
20c457b8 TR |
312 | #ifdef SUPPORT_OLD_POWER_EVENTS |
313 | static int use_old_power_events; | |
314 | struct power_entry_old { | |
10274989 | 315 | struct trace_entry te; |
4c21adf2 TR |
316 | u64 type; |
317 | u64 value; | |
318 | u64 cpu_id; | |
10274989 | 319 | }; |
20c457b8 TR |
320 | #endif |
321 | ||
322 | struct power_processor_entry { | |
323 | struct trace_entry te; | |
324 | u32 state; | |
325 | u32 cpu_id; | |
326 | }; | |
10274989 AV |
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 | ||
10274989 AV |
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 | { | |
e0dcd6fb ACM |
356 | struct power_event *pwr = zalloc(sizeof(*pwr)); |
357 | ||
10274989 AV |
358 | if (!pwr) |
359 | return; | |
10274989 AV |
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; | |
10274989 AV |
374 | |
375 | if (new_freq > 8000000) /* detect invalid data */ | |
376 | return; | |
377 | ||
e0dcd6fb | 378 | pwr = zalloc(sizeof(*pwr)); |
10274989 AV |
379 | if (!pwr) |
380 | return; | |
10274989 AV |
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 | { | |
10274989 AV |
410 | struct per_pid *p; |
411 | struct wakeup_entry *wake = (void *)te; | |
e0dcd6fb | 412 | struct wake_event *we = zalloc(sizeof(*we)); |
10274989 | 413 | |
10274989 AV |
414 | if (!we) |
415 | return; | |
416 | ||
10274989 AV |
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 | ||
33e26a1b JL |
455 | p->current->state_since = timestamp; |
456 | p->current->state = TYPE_RUNNING; | |
10274989 AV |
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 | ||
5936678e JO |
469 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
470 | struct perf_sample *sample); | |
10274989 | 471 | |
1d037ca1 IT |
472 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
473 | union perf_event *event __maybe_unused, | |
8d50e5b4 | 474 | struct perf_sample *sample, |
e3f42609 | 475 | struct perf_evsel *evsel, |
1d037ca1 | 476 | struct machine *machine __maybe_unused) |
10274989 | 477 | { |
e3f42609 | 478 | if (evsel->attr.sample_type & PERF_SAMPLE_TIME) { |
640c03ce ACM |
479 | if (!first_time || first_time > sample->time) |
480 | first_time = sample->time; | |
481 | if (last_time < sample->time) | |
482 | last_time = sample->time; | |
10274989 | 483 | } |
180f95e2 | 484 | |
5936678e JO |
485 | if (sample->cpu > numcpus) |
486 | numcpus = sample->cpu; | |
487 | ||
744a9719 ACM |
488 | if (evsel->handler != NULL) { |
489 | tracepoint_handler f = evsel->handler; | |
5936678e JO |
490 | return f(evsel, sample); |
491 | } | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
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 | } | |
10274989 | 528 | |
5936678e JO |
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; | |
10274989 | 534 | |
5936678e JO |
535 | sched_switch(sample->cpu, sample->time, te); |
536 | return 0; | |
537 | } | |
20c457b8 TR |
538 | |
539 | #ifdef SUPPORT_OLD_POWER_EVENTS | |
5936678e JO |
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); | |
10274989 AV |
565 | return 0; |
566 | } | |
5936678e | 567 | #endif /* SUPPORT_OLD_POWER_EVENTS */ |
10274989 AV |
568 | |
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 | ||
39a90a8e | 578 | for (cpu = 0; cpu <= numcpus; cpu++) { |
e0dcd6fb ACM |
579 | /* C state */ |
580 | #if 0 | |
581 | pwr = zalloc(sizeof(*pwr)); | |
10274989 AV |
582 | if (!pwr) |
583 | return; | |
10274989 | 584 | |
10274989 AV |
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 | ||
e0dcd6fb | 596 | pwr = zalloc(sizeof(*pwr)); |
10274989 AV |
597 | if (!pwr) |
598 | return; | |
10274989 AV |
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 | ||
10274989 AV |
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; | |
4f1202c8 | 698 | char *task_from = NULL, *task_to = NULL; |
10274989 AV |
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) { | |
bbe2987b | 707 | if (p->pid == we->waker && !from) { |
10274989 | 708 | from = c->Y; |
3bc2a39c | 709 | task_from = strdup(c->comm); |
4f1202c8 | 710 | } |
bbe2987b | 711 | if (p->pid == we->wakee && !to) { |
10274989 | 712 | to = c->Y; |
3bc2a39c | 713 | task_to = strdup(c->comm); |
4f1202c8 | 714 | } |
10274989 AV |
715 | } |
716 | c = c->next; | |
717 | } | |
3bc2a39c AV |
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 | } | |
10274989 AV |
730 | } |
731 | p = p->next; | |
732 | } | |
733 | ||
3bc2a39c AV |
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 | ||
10274989 AV |
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 | |
4f1202c8 | 748 | svg_partial_wakeline(we->time, from, task_from, to, task_to); |
10274989 | 749 | we = we->next; |
3bc2a39c AV |
750 | |
751 | free(task_from); | |
752 | free(task_to); | |
10274989 AV |
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 | ||
a92fe7b3 | 797 | svg_box(Y, c->start_time, c->end_time, "process"); |
10274989 AV |
798 | sample = c->samples; |
799 | while (sample) { | |
800 | if (sample->type == TYPE_RUNNING) | |
cbb2e81e | 801 | svg_running(Y, sample->cpu, sample->start_time, sample->end_time); |
10274989 | 802 | if (sample->type == TYPE_BLOCKED) |
cbb2e81e | 803 | svg_blocked(Y, sample->cpu, sample->start_time, sample->end_time); |
10274989 | 804 | if (sample->type == TYPE_WAITING) |
cbb2e81e | 805 | svg_waiting(Y, sample->cpu, sample->start_time, sample->end_time); |
10274989 AV |
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 | ||
bbe2987b AV |
826 | static void add_process_filter(const char *string) |
827 | { | |
e0dcd6fb ACM |
828 | int pid = strtoull(string, NULL, 10); |
829 | struct process_filter *filt = malloc(sizeof(*filt)); | |
bbe2987b | 830 | |
bbe2987b AV |
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 | ||
10274989 AV |
898 | static int determine_display_tasks(u64 threshold) |
899 | { | |
900 | struct per_pid *p; | |
901 | struct per_pidcomm *c; | |
902 | int count = 0; | |
903 | ||
bbe2987b AV |
904 | if (process_filter) |
905 | return determine_display_tasks_filtered(); | |
906 | ||
10274989 AV |
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; | |
753c505d | 916 | if (p->total_time >= threshold) |
10274989 AV |
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 | ||
753c505d | 927 | if (c->total_time >= threshold) { |
10274989 AV |
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; | |
0a8eb275 | 950 | int thresh = TIME_THRESH; |
10274989 AV |
951 | |
952 | numcpus++; | |
953 | ||
753c505d SF |
954 | if (power_only) |
955 | proc_num = 0; | |
10274989 | 956 | |
0a8eb275 SF |
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; | |
54874e32 | 962 | } while (!process_filter && thresh && count < proc_num); |
10274989 | 963 | |
5094b655 | 964 | open_svg(filename, numcpus, count, first_time, last_time); |
10274989 | 965 | |
5094b655 | 966 | svg_time_grid(); |
10274989 AV |
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(); | |
753c505d SF |
973 | if (proc_num) |
974 | draw_process_bars(); | |
c87097d3 SF |
975 | if (!tasks_only) |
976 | draw_c_p_states(); | |
753c505d SF |
977 | if (proc_num) |
978 | draw_wakeups(); | |
10274989 AV |
979 | |
980 | svg_close(); | |
981 | } | |
982 | ||
70cb4e96 | 983 | static int __cmd_timechart(const char *output_name) |
5cbd0805 | 984 | { |
73bdc715 ACM |
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 | }; | |
5936678e JO |
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 | }; | |
f5fc1412 JO |
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); | |
d549c769 | 1010 | int ret = -EINVAL; |
10274989 | 1011 | |
94c744b6 ACM |
1012 | if (session == NULL) |
1013 | return -ENOMEM; | |
1014 | ||
d549c769 ACM |
1015 | if (!perf_session__has_traces(session, "timechart record")) |
1016 | goto out_delete; | |
1017 | ||
5936678e JO |
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 | ||
45694aa7 | 1024 | ret = perf_session__process_events(session, &perf_timechart); |
5cbd0805 | 1025 | if (ret) |
94c744b6 | 1026 | goto out_delete; |
10274989 | 1027 | |
10274989 AV |
1028 | end_sample_processing(); |
1029 | ||
1030 | sort_pids(); | |
1031 | ||
1032 | write_svg_file(output_name); | |
1033 | ||
6beba7ad ACM |
1034 | pr_info("Written %2.1f seconds of trace to %s.\n", |
1035 | (last_time - first_time) / 1000000000.0, output_name); | |
94c744b6 ACM |
1036 | out_delete: |
1037 | perf_session__delete(session); | |
1038 | return ret; | |
10274989 AV |
1039 | } |
1040 | ||
3c09eebd AV |
1041 | static int __cmd_record(int argc, const char **argv) |
1042 | { | |
73bdc715 ACM |
1043 | #ifdef SUPPORT_OLD_POWER_EVENTS |
1044 | const char * const record_old_args[] = { | |
4a4d371a | 1045 | "record", "-a", "-R", "-c", "1", |
73bdc715 ACM |
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[] = { | |
4a4d371a | 1054 | "record", "-a", "-R", "-c", "1", |
73bdc715 ACM |
1055 | "-e", "power:cpu_frequency", |
1056 | "-e", "power:cpu_idle", | |
1057 | "-e", "sched:sched_wakeup", | |
1058 | "-e", "sched:sched_switch", | |
1059 | }; | |
3c09eebd AV |
1060 | unsigned int rec_argc, i, j; |
1061 | const char **rec_argv; | |
20c457b8 TR |
1062 | const char * const *record_args = record_new_args; |
1063 | unsigned int record_elems = ARRAY_SIZE(record_new_args); | |
1064 | ||
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 | |
3c09eebd | 1073 | |
20c457b8 | 1074 | rec_argc = record_elems + argc - 1; |
3c09eebd AV |
1075 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
1076 | ||
ce47dc56 CS |
1077 | if (rec_argv == NULL) |
1078 | return -ENOMEM; | |
1079 | ||
20c457b8 | 1080 | for (i = 0; i < record_elems; i++) |
3c09eebd AV |
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 | ||
bbe2987b | 1089 | static int |
1d037ca1 IT |
1090 | parse_process(const struct option *opt __maybe_unused, const char *arg, |
1091 | int __maybe_unused unset) | |
bbe2987b AV |
1092 | { |
1093 | if (arg) | |
1094 | add_process_filter(arg); | |
1095 | return 0; | |
1096 | } | |
1097 | ||
73bdc715 ACM |
1098 | int cmd_timechart(int argc, const char **argv, |
1099 | const char *prefix __maybe_unused) | |
1100 | { | |
73bdc715 ACM |
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"), | |
c87097d3 SF |
1107 | OPT_BOOLEAN('T', "tasks-only", &tasks_only, |
1108 | "output processes data only"), | |
bbe2987b AV |
1109 | OPT_CALLBACK('p', "process", NULL, "process", |
1110 | "process selector. Pass a pid or process name.", | |
1111 | parse_process), | |
ec5761ea DA |
1112 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
1113 | "Look for files with symbols relative to this directory"), | |
54874e32 SF |
1114 | OPT_INTEGER('n', "proc-num", &proc_num, |
1115 | "min. number of tasks to print"), | |
10274989 | 1116 | OPT_END() |
73bdc715 ACM |
1117 | }; |
1118 | const char * const timechart_usage[] = { | |
1119 | "perf timechart [<options>] {record}", | |
1120 | NULL | |
1121 | }; | |
10274989 | 1122 | |
3c09eebd AV |
1123 | argc = parse_options(argc, argv, options, timechart_usage, |
1124 | PARSE_OPT_STOP_AT_NON_OPTION); | |
10274989 | 1125 | |
c87097d3 SF |
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 | ||
655000e7 ACM |
1131 | symbol__init(); |
1132 | ||
3c09eebd AV |
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); | |
10274989 AV |
1137 | |
1138 | setup_pager(); | |
1139 | ||
70cb4e96 | 1140 | return __cmd_timechart(output_name); |
10274989 | 1141 | } |