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