]>
Commit | Line | Data |
---|---|---|
0a02ad93 | 1 | #include "builtin.h" |
b1ffe8f3 | 2 | #include "perf.h" |
0a02ad93 IM |
3 | |
4 | #include "util/util.h" | |
5 | #include "util/cache.h" | |
6 | #include "util/symbol.h" | |
7 | #include "util/thread.h" | |
8 | #include "util/header.h" | |
9 | ||
10 | #include "util/parse-options.h" | |
b1ffe8f3 | 11 | #include "util/trace-event.h" |
0a02ad93 | 12 | |
0a02ad93 | 13 | #include "util/debug.h" |
016e92fb | 14 | #include "util/data_map.h" |
0a02ad93 | 15 | |
ec156764 | 16 | #include <sys/types.h> |
b1ffe8f3 | 17 | #include <sys/prctl.h> |
0a02ad93 | 18 | |
b1ffe8f3 IM |
19 | #include <semaphore.h> |
20 | #include <pthread.h> | |
21 | #include <math.h> | |
419ab0d6 | 22 | |
ec156764 | 23 | static char const *input_name = "perf.data"; |
0a02ad93 | 24 | |
ec156764 | 25 | static unsigned long total_comm = 0; |
0a02ad93 | 26 | |
ec156764 IM |
27 | static struct perf_header *header; |
28 | static u64 sample_type; | |
0a02ad93 | 29 | |
daa1d7a5 FW |
30 | static char default_sort_order[] = "avg, max, switch, runtime"; |
31 | static char *sort_order = default_sort_order; | |
32 | ||
55ffb7a6 MG |
33 | static int profile_cpu = -1; |
34 | ||
016e92fb FW |
35 | static char *cwd; |
36 | static int cwdlen; | |
37 | ||
b1ffe8f3 IM |
38 | #define PR_SET_NAME 15 /* Set process name */ |
39 | #define MAX_CPUS 4096 | |
0a02ad93 | 40 | |
b1ffe8f3 | 41 | #define BUG_ON(x) assert(!(x)) |
ec156764 | 42 | |
b1ffe8f3 IM |
43 | static u64 run_measurement_overhead; |
44 | static u64 sleep_measurement_overhead; | |
ec156764 | 45 | |
b1ffe8f3 IM |
46 | #define COMM_LEN 20 |
47 | #define SYM_LEN 129 | |
ec156764 | 48 | |
b1ffe8f3 | 49 | #define MAX_PID 65536 |
ec156764 | 50 | |
b1ffe8f3 | 51 | static unsigned long nr_tasks; |
ec156764 | 52 | |
39aeb52f | 53 | struct sched_atom; |
ec156764 | 54 | |
b1ffe8f3 IM |
55 | struct task_desc { |
56 | unsigned long nr; | |
57 | unsigned long pid; | |
58 | char comm[COMM_LEN]; | |
ec156764 | 59 | |
b1ffe8f3 IM |
60 | unsigned long nr_events; |
61 | unsigned long curr_event; | |
39aeb52f | 62 | struct sched_atom **atoms; |
b1ffe8f3 IM |
63 | |
64 | pthread_t thread; | |
65 | sem_t sleep_sem; | |
ec156764 | 66 | |
b1ffe8f3 IM |
67 | sem_t ready_for_work; |
68 | sem_t work_done_sem; | |
69 | ||
70 | u64 cpu_usage; | |
71 | }; | |
72 | ||
73 | enum sched_event_type { | |
74 | SCHED_EVENT_RUN, | |
75 | SCHED_EVENT_SLEEP, | |
76 | SCHED_EVENT_WAKEUP, | |
55ffb7a6 | 77 | SCHED_EVENT_MIGRATION, |
b1ffe8f3 IM |
78 | }; |
79 | ||
39aeb52f | 80 | struct sched_atom { |
b1ffe8f3 IM |
81 | enum sched_event_type type; |
82 | u64 timestamp; | |
83 | u64 duration; | |
84 | unsigned long nr; | |
85 | int specific_wait; | |
86 | sem_t *wait_sem; | |
87 | struct task_desc *wakee; | |
88 | }; | |
89 | ||
90 | static struct task_desc *pid_to_task[MAX_PID]; | |
91 | ||
92 | static struct task_desc **tasks; | |
93 | ||
94 | static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER; | |
95 | static u64 start_time; | |
96 | ||
97 | static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER; | |
ec156764 | 98 | |
b1ffe8f3 IM |
99 | static unsigned long nr_run_events; |
100 | static unsigned long nr_sleep_events; | |
101 | static unsigned long nr_wakeup_events; | |
102 | ||
103 | static unsigned long nr_sleep_corrections; | |
104 | static unsigned long nr_run_events_optimized; | |
105 | ||
106 | static unsigned long targetless_wakeups; | |
107 | static unsigned long multitarget_wakeups; | |
108 | ||
109 | static u64 cpu_usage; | |
110 | static u64 runavg_cpu_usage; | |
111 | static u64 parent_cpu_usage; | |
112 | static u64 runavg_parent_cpu_usage; | |
113 | ||
114 | static unsigned long nr_runs; | |
115 | static u64 sum_runtime; | |
116 | static u64 sum_fluct; | |
117 | static u64 run_avg; | |
118 | ||
119 | static unsigned long replay_repeat = 10; | |
ea57c4f5 | 120 | static unsigned long nr_timestamps; |
dc02bf71 IM |
121 | static unsigned long nr_unordered_timestamps; |
122 | static unsigned long nr_state_machine_bugs; | |
c8a37751 | 123 | static unsigned long nr_context_switch_bugs; |
dc02bf71 IM |
124 | static unsigned long nr_events; |
125 | static unsigned long nr_lost_chunks; | |
126 | static unsigned long nr_lost_events; | |
b1ffe8f3 IM |
127 | |
128 | #define TASK_STATE_TO_CHAR_STR "RSDTtZX" | |
129 | ||
130 | enum thread_state { | |
131 | THREAD_SLEEPING = 0, | |
132 | THREAD_WAIT_CPU, | |
133 | THREAD_SCHED_IN, | |
134 | THREAD_IGNORE | |
135 | }; | |
136 | ||
137 | struct work_atom { | |
138 | struct list_head list; | |
139 | enum thread_state state; | |
aa1ab9d2 | 140 | u64 sched_out_time; |
b1ffe8f3 IM |
141 | u64 wake_up_time; |
142 | u64 sched_in_time; | |
143 | u64 runtime; | |
144 | }; | |
145 | ||
39aeb52f | 146 | struct work_atoms { |
147 | struct list_head work_list; | |
b1ffe8f3 IM |
148 | struct thread *thread; |
149 | struct rb_node node; | |
150 | u64 max_lat; | |
151 | u64 total_lat; | |
152 | u64 nb_atoms; | |
153 | u64 total_runtime; | |
154 | }; | |
155 | ||
39aeb52f | 156 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
b1ffe8f3 IM |
157 | |
158 | static struct rb_root atom_root, sorted_atom_root; | |
159 | ||
160 | static u64 all_runtime; | |
161 | static u64 all_count; | |
162 | ||
b1ffe8f3 IM |
163 | |
164 | static u64 get_nsecs(void) | |
ec156764 IM |
165 | { |
166 | struct timespec ts; | |
167 | ||
168 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
169 | ||
170 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; | |
171 | } | |
172 | ||
b1ffe8f3 | 173 | static void burn_nsecs(u64 nsecs) |
ec156764 | 174 | { |
b1ffe8f3 | 175 | u64 T0 = get_nsecs(), T1; |
ec156764 IM |
176 | |
177 | do { | |
178 | T1 = get_nsecs(); | |
179 | } while (T1 + run_measurement_overhead < T0 + nsecs); | |
180 | } | |
181 | ||
b1ffe8f3 | 182 | static void sleep_nsecs(u64 nsecs) |
ec156764 IM |
183 | { |
184 | struct timespec ts; | |
185 | ||
186 | ts.tv_nsec = nsecs % 999999999; | |
187 | ts.tv_sec = nsecs / 999999999; | |
188 | ||
189 | nanosleep(&ts, NULL); | |
190 | } | |
191 | ||
192 | static void calibrate_run_measurement_overhead(void) | |
193 | { | |
b1ffe8f3 | 194 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
ec156764 IM |
195 | int i; |
196 | ||
197 | for (i = 0; i < 10; i++) { | |
198 | T0 = get_nsecs(); | |
199 | burn_nsecs(0); | |
200 | T1 = get_nsecs(); | |
201 | delta = T1-T0; | |
202 | min_delta = min(min_delta, delta); | |
203 | } | |
204 | run_measurement_overhead = min_delta; | |
205 | ||
ad236fd2 | 206 | printf("run measurement overhead: %Ld nsecs\n", min_delta); |
ec156764 IM |
207 | } |
208 | ||
209 | static void calibrate_sleep_measurement_overhead(void) | |
210 | { | |
b1ffe8f3 | 211 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
ec156764 IM |
212 | int i; |
213 | ||
214 | for (i = 0; i < 10; i++) { | |
215 | T0 = get_nsecs(); | |
216 | sleep_nsecs(10000); | |
217 | T1 = get_nsecs(); | |
218 | delta = T1-T0; | |
219 | min_delta = min(min_delta, delta); | |
220 | } | |
221 | min_delta -= 10000; | |
222 | sleep_measurement_overhead = min_delta; | |
223 | ||
ad236fd2 | 224 | printf("sleep measurement overhead: %Ld nsecs\n", min_delta); |
ec156764 IM |
225 | } |
226 | ||
39aeb52f | 227 | static struct sched_atom * |
b1ffe8f3 | 228 | get_new_event(struct task_desc *task, u64 timestamp) |
ec156764 | 229 | { |
39aeb52f | 230 | struct sched_atom *event = calloc(1, sizeof(*event)); |
ec156764 IM |
231 | unsigned long idx = task->nr_events; |
232 | size_t size; | |
233 | ||
234 | event->timestamp = timestamp; | |
235 | event->nr = idx; | |
236 | ||
237 | task->nr_events++; | |
39aeb52f | 238 | size = sizeof(struct sched_atom *) * task->nr_events; |
239 | task->atoms = realloc(task->atoms, size); | |
240 | BUG_ON(!task->atoms); | |
ec156764 | 241 | |
39aeb52f | 242 | task->atoms[idx] = event; |
ec156764 IM |
243 | |
244 | return event; | |
245 | } | |
246 | ||
39aeb52f | 247 | static struct sched_atom *last_event(struct task_desc *task) |
ec156764 IM |
248 | { |
249 | if (!task->nr_events) | |
250 | return NULL; | |
251 | ||
39aeb52f | 252 | return task->atoms[task->nr_events - 1]; |
ec156764 IM |
253 | } |
254 | ||
255 | static void | |
b1ffe8f3 | 256 | add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration) |
ec156764 | 257 | { |
39aeb52f | 258 | struct sched_atom *event, *curr_event = last_event(task); |
ec156764 IM |
259 | |
260 | /* | |
fbf94829 IM |
261 | * optimize an existing RUN event by merging this one |
262 | * to it: | |
263 | */ | |
ec156764 IM |
264 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
265 | nr_run_events_optimized++; | |
266 | curr_event->duration += duration; | |
267 | return; | |
268 | } | |
269 | ||
270 | event = get_new_event(task, timestamp); | |
271 | ||
272 | event->type = SCHED_EVENT_RUN; | |
273 | event->duration = duration; | |
274 | ||
275 | nr_run_events++; | |
276 | } | |
277 | ||
ec156764 | 278 | static void |
b1ffe8f3 | 279 | add_sched_event_wakeup(struct task_desc *task, u64 timestamp, |
ec156764 IM |
280 | struct task_desc *wakee) |
281 | { | |
39aeb52f | 282 | struct sched_atom *event, *wakee_event; |
ec156764 IM |
283 | |
284 | event = get_new_event(task, timestamp); | |
285 | event->type = SCHED_EVENT_WAKEUP; | |
286 | event->wakee = wakee; | |
287 | ||
288 | wakee_event = last_event(wakee); | |
289 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { | |
290 | targetless_wakeups++; | |
291 | return; | |
292 | } | |
293 | if (wakee_event->wait_sem) { | |
294 | multitarget_wakeups++; | |
295 | return; | |
296 | } | |
297 | ||
298 | wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem)); | |
299 | sem_init(wakee_event->wait_sem, 0, 0); | |
300 | wakee_event->specific_wait = 1; | |
301 | event->wait_sem = wakee_event->wait_sem; | |
302 | ||
303 | nr_wakeup_events++; | |
304 | } | |
305 | ||
306 | static void | |
b1ffe8f3 | 307 | add_sched_event_sleep(struct task_desc *task, u64 timestamp, |
ad236fd2 | 308 | u64 task_state __used) |
ec156764 | 309 | { |
39aeb52f | 310 | struct sched_atom *event = get_new_event(task, timestamp); |
ec156764 IM |
311 | |
312 | event->type = SCHED_EVENT_SLEEP; | |
313 | ||
314 | nr_sleep_events++; | |
315 | } | |
316 | ||
317 | static struct task_desc *register_pid(unsigned long pid, const char *comm) | |
318 | { | |
319 | struct task_desc *task; | |
320 | ||
321 | BUG_ON(pid >= MAX_PID); | |
322 | ||
323 | task = pid_to_task[pid]; | |
324 | ||
325 | if (task) | |
326 | return task; | |
327 | ||
328 | task = calloc(1, sizeof(*task)); | |
329 | task->pid = pid; | |
330 | task->nr = nr_tasks; | |
331 | strcpy(task->comm, comm); | |
332 | /* | |
333 | * every task starts in sleeping state - this gets ignored | |
334 | * if there's no wakeup pointing to this sleep state: | |
335 | */ | |
336 | add_sched_event_sleep(task, 0, 0); | |
337 | ||
338 | pid_to_task[pid] = task; | |
339 | nr_tasks++; | |
340 | tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *)); | |
341 | BUG_ON(!tasks); | |
342 | tasks[task->nr] = task; | |
343 | ||
ad236fd2 IM |
344 | if (verbose) |
345 | printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm); | |
ec156764 IM |
346 | |
347 | return task; | |
348 | } | |
349 | ||
350 | ||
ec156764 IM |
351 | static void print_task_traces(void) |
352 | { | |
353 | struct task_desc *task; | |
354 | unsigned long i; | |
355 | ||
356 | for (i = 0; i < nr_tasks; i++) { | |
357 | task = tasks[i]; | |
ad236fd2 | 358 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
ec156764 IM |
359 | task->nr, task->comm, task->pid, task->nr_events); |
360 | } | |
361 | } | |
362 | ||
363 | static void add_cross_task_wakeups(void) | |
364 | { | |
365 | struct task_desc *task1, *task2; | |
366 | unsigned long i, j; | |
367 | ||
368 | for (i = 0; i < nr_tasks; i++) { | |
369 | task1 = tasks[i]; | |
370 | j = i + 1; | |
371 | if (j == nr_tasks) | |
372 | j = 0; | |
373 | task2 = tasks[j]; | |
374 | add_sched_event_wakeup(task1, 0, task2); | |
375 | } | |
376 | } | |
377 | ||
378 | static void | |
39aeb52f | 379 | process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom) |
ec156764 IM |
380 | { |
381 | int ret = 0; | |
b1ffe8f3 | 382 | u64 now; |
ec156764 IM |
383 | long long delta; |
384 | ||
385 | now = get_nsecs(); | |
39aeb52f | 386 | delta = start_time + atom->timestamp - now; |
ec156764 | 387 | |
39aeb52f | 388 | switch (atom->type) { |
ec156764 | 389 | case SCHED_EVENT_RUN: |
39aeb52f | 390 | burn_nsecs(atom->duration); |
ec156764 IM |
391 | break; |
392 | case SCHED_EVENT_SLEEP: | |
39aeb52f | 393 | if (atom->wait_sem) |
394 | ret = sem_wait(atom->wait_sem); | |
ec156764 IM |
395 | BUG_ON(ret); |
396 | break; | |
397 | case SCHED_EVENT_WAKEUP: | |
39aeb52f | 398 | if (atom->wait_sem) |
399 | ret = sem_post(atom->wait_sem); | |
ec156764 IM |
400 | BUG_ON(ret); |
401 | break; | |
55ffb7a6 MG |
402 | case SCHED_EVENT_MIGRATION: |
403 | break; | |
ec156764 IM |
404 | default: |
405 | BUG_ON(1); | |
406 | } | |
407 | } | |
408 | ||
b1ffe8f3 | 409 | static u64 get_cpu_usage_nsec_parent(void) |
ec156764 IM |
410 | { |
411 | struct rusage ru; | |
b1ffe8f3 | 412 | u64 sum; |
ec156764 IM |
413 | int err; |
414 | ||
415 | err = getrusage(RUSAGE_SELF, &ru); | |
416 | BUG_ON(err); | |
417 | ||
418 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; | |
419 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; | |
420 | ||
421 | return sum; | |
422 | } | |
423 | ||
b1ffe8f3 | 424 | static u64 get_cpu_usage_nsec_self(void) |
ec156764 IM |
425 | { |
426 | char filename [] = "/proc/1234567890/sched"; | |
427 | unsigned long msecs, nsecs; | |
428 | char *line = NULL; | |
b1ffe8f3 | 429 | u64 total = 0; |
ec156764 IM |
430 | size_t len = 0; |
431 | ssize_t chars; | |
432 | FILE *file; | |
433 | int ret; | |
434 | ||
435 | sprintf(filename, "/proc/%d/sched", getpid()); | |
436 | file = fopen(filename, "r"); | |
437 | BUG_ON(!file); | |
438 | ||
439 | while ((chars = getline(&line, &len, file)) != -1) { | |
ec156764 IM |
440 | ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n", |
441 | &msecs, &nsecs); | |
442 | if (ret == 2) { | |
443 | total = msecs*1e6 + nsecs; | |
ec156764 IM |
444 | break; |
445 | } | |
446 | } | |
447 | if (line) | |
448 | free(line); | |
449 | fclose(file); | |
450 | ||
451 | return total; | |
452 | } | |
453 | ||
454 | static void *thread_func(void *ctx) | |
455 | { | |
456 | struct task_desc *this_task = ctx; | |
b1ffe8f3 | 457 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
458 | unsigned long i, ret; |
459 | char comm2[22]; | |
460 | ||
ec156764 IM |
461 | sprintf(comm2, ":%s", this_task->comm); |
462 | prctl(PR_SET_NAME, comm2); | |
463 | ||
464 | again: | |
465 | ret = sem_post(&this_task->ready_for_work); | |
466 | BUG_ON(ret); | |
ec156764 IM |
467 | ret = pthread_mutex_lock(&start_work_mutex); |
468 | BUG_ON(ret); | |
469 | ret = pthread_mutex_unlock(&start_work_mutex); | |
470 | BUG_ON(ret); | |
ec156764 IM |
471 | |
472 | cpu_usage_0 = get_cpu_usage_nsec_self(); | |
473 | ||
474 | for (i = 0; i < this_task->nr_events; i++) { | |
475 | this_task->curr_event = i; | |
39aeb52f | 476 | process_sched_event(this_task, this_task->atoms[i]); |
ec156764 IM |
477 | } |
478 | ||
479 | cpu_usage_1 = get_cpu_usage_nsec_self(); | |
480 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; | |
481 | ||
ec156764 IM |
482 | ret = sem_post(&this_task->work_done_sem); |
483 | BUG_ON(ret); | |
ec156764 IM |
484 | |
485 | ret = pthread_mutex_lock(&work_done_wait_mutex); | |
486 | BUG_ON(ret); | |
487 | ret = pthread_mutex_unlock(&work_done_wait_mutex); | |
488 | BUG_ON(ret); | |
ec156764 IM |
489 | |
490 | goto again; | |
491 | } | |
492 | ||
493 | static void create_tasks(void) | |
494 | { | |
495 | struct task_desc *task; | |
496 | pthread_attr_t attr; | |
497 | unsigned long i; | |
498 | int err; | |
499 | ||
500 | err = pthread_attr_init(&attr); | |
501 | BUG_ON(err); | |
502 | err = pthread_attr_setstacksize(&attr, (size_t)(16*1024)); | |
503 | BUG_ON(err); | |
504 | err = pthread_mutex_lock(&start_work_mutex); | |
505 | BUG_ON(err); | |
506 | err = pthread_mutex_lock(&work_done_wait_mutex); | |
507 | BUG_ON(err); | |
508 | for (i = 0; i < nr_tasks; i++) { | |
509 | task = tasks[i]; | |
510 | sem_init(&task->sleep_sem, 0, 0); | |
511 | sem_init(&task->ready_for_work, 0, 0); | |
512 | sem_init(&task->work_done_sem, 0, 0); | |
513 | task->curr_event = 0; | |
514 | err = pthread_create(&task->thread, &attr, thread_func, task); | |
515 | BUG_ON(err); | |
516 | } | |
517 | } | |
518 | ||
ec156764 IM |
519 | static void wait_for_tasks(void) |
520 | { | |
b1ffe8f3 | 521 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
522 | struct task_desc *task; |
523 | unsigned long i, ret; | |
524 | ||
ec156764 | 525 | start_time = get_nsecs(); |
ec156764 IM |
526 | cpu_usage = 0; |
527 | pthread_mutex_unlock(&work_done_wait_mutex); | |
528 | ||
529 | for (i = 0; i < nr_tasks; i++) { | |
530 | task = tasks[i]; | |
531 | ret = sem_wait(&task->ready_for_work); | |
532 | BUG_ON(ret); | |
533 | sem_init(&task->ready_for_work, 0, 0); | |
534 | } | |
535 | ret = pthread_mutex_lock(&work_done_wait_mutex); | |
536 | BUG_ON(ret); | |
537 | ||
538 | cpu_usage_0 = get_cpu_usage_nsec_parent(); | |
539 | ||
540 | pthread_mutex_unlock(&start_work_mutex); | |
541 | ||
ec156764 IM |
542 | for (i = 0; i < nr_tasks; i++) { |
543 | task = tasks[i]; | |
544 | ret = sem_wait(&task->work_done_sem); | |
545 | BUG_ON(ret); | |
546 | sem_init(&task->work_done_sem, 0, 0); | |
547 | cpu_usage += task->cpu_usage; | |
548 | task->cpu_usage = 0; | |
549 | } | |
550 | ||
551 | cpu_usage_1 = get_cpu_usage_nsec_parent(); | |
552 | if (!runavg_cpu_usage) | |
553 | runavg_cpu_usage = cpu_usage; | |
554 | runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10; | |
555 | ||
556 | parent_cpu_usage = cpu_usage_1 - cpu_usage_0; | |
557 | if (!runavg_parent_cpu_usage) | |
558 | runavg_parent_cpu_usage = parent_cpu_usage; | |
559 | runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 + | |
560 | parent_cpu_usage)/10; | |
561 | ||
562 | ret = pthread_mutex_lock(&start_work_mutex); | |
563 | BUG_ON(ret); | |
564 | ||
565 | for (i = 0; i < nr_tasks; i++) { | |
566 | task = tasks[i]; | |
567 | sem_init(&task->sleep_sem, 0, 0); | |
568 | task->curr_event = 0; | |
569 | } | |
570 | } | |
571 | ||
ec156764 IM |
572 | static void run_one_test(void) |
573 | { | |
b1ffe8f3 | 574 | u64 T0, T1, delta, avg_delta, fluct, std_dev; |
ec156764 IM |
575 | |
576 | T0 = get_nsecs(); | |
577 | wait_for_tasks(); | |
578 | T1 = get_nsecs(); | |
579 | ||
580 | delta = T1 - T0; | |
581 | sum_runtime += delta; | |
582 | nr_runs++; | |
583 | ||
584 | avg_delta = sum_runtime / nr_runs; | |
585 | if (delta < avg_delta) | |
586 | fluct = avg_delta - delta; | |
587 | else | |
588 | fluct = delta - avg_delta; | |
589 | sum_fluct += fluct; | |
590 | std_dev = sum_fluct / nr_runs / sqrt(nr_runs); | |
591 | if (!run_avg) | |
592 | run_avg = delta; | |
593 | run_avg = (run_avg*9 + delta)/10; | |
594 | ||
ad236fd2 | 595 | printf("#%-3ld: %0.3f, ", |
ec156764 IM |
596 | nr_runs, (double)delta/1000000.0); |
597 | ||
ad236fd2 | 598 | printf("ravg: %0.2f, ", |
ec156764 IM |
599 | (double)run_avg/1e6); |
600 | ||
ad236fd2 | 601 | printf("cpu: %0.2f / %0.2f", |
ec156764 IM |
602 | (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6); |
603 | ||
604 | #if 0 | |
605 | /* | |
fbf94829 IM |
606 | * rusage statistics done by the parent, these are less |
607 | * accurate than the sum_exec_runtime based statistics: | |
608 | */ | |
ad236fd2 | 609 | printf(" [%0.2f / %0.2f]", |
ec156764 IM |
610 | (double)parent_cpu_usage/1e6, |
611 | (double)runavg_parent_cpu_usage/1e6); | |
612 | #endif | |
613 | ||
ad236fd2 | 614 | printf("\n"); |
ec156764 IM |
615 | |
616 | if (nr_sleep_corrections) | |
ad236fd2 | 617 | printf(" (%ld sleep corrections)\n", nr_sleep_corrections); |
ec156764 IM |
618 | nr_sleep_corrections = 0; |
619 | } | |
620 | ||
621 | static void test_calibrations(void) | |
622 | { | |
b1ffe8f3 | 623 | u64 T0, T1; |
ec156764 IM |
624 | |
625 | T0 = get_nsecs(); | |
626 | burn_nsecs(1e6); | |
627 | T1 = get_nsecs(); | |
628 | ||
ad236fd2 | 629 | printf("the run test took %Ld nsecs\n", T1-T0); |
ec156764 IM |
630 | |
631 | T0 = get_nsecs(); | |
632 | sleep_nsecs(1e6); | |
633 | T1 = get_nsecs(); | |
634 | ||
ad236fd2 | 635 | printf("the sleep test took %Ld nsecs\n", T1-T0); |
ec156764 IM |
636 | } |
637 | ||
0a02ad93 IM |
638 | static int |
639 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | |
640 | { | |
d5b889f2 | 641 | struct thread *thread = threads__findnew(event->comm.tid); |
0a02ad93 | 642 | |
dc02bf71 | 643 | dump_printf("%p [%p]: perf_event_comm: %s:%d\n", |
0a02ad93 IM |
644 | (void *)(offset + head), |
645 | (void *)(long)(event->header.size), | |
646 | event->comm.comm, event->comm.pid); | |
647 | ||
648 | if (thread == NULL || | |
649 | thread__set_comm(thread, event->comm.comm)) { | |
dc02bf71 | 650 | dump_printf("problem processing perf_event_comm, skipping event.\n"); |
0a02ad93 IM |
651 | return -1; |
652 | } | |
653 | total_comm++; | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
46538818 FW |
658 | |
659 | struct raw_event_sample { | |
660 | u32 size; | |
661 | char data[0]; | |
662 | }; | |
663 | ||
664 | #define FILL_FIELD(ptr, field, event, data) \ | |
665 | ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data) | |
666 | ||
667 | #define FILL_ARRAY(ptr, array, event, data) \ | |
668 | do { \ | |
669 | void *__array = raw_field_ptr(event, #array, data); \ | |
670 | memcpy(ptr.array, __array, sizeof(ptr.array)); \ | |
671 | } while(0) | |
672 | ||
673 | #define FILL_COMMON_FIELDS(ptr, event, data) \ | |
674 | do { \ | |
675 | FILL_FIELD(ptr, common_type, event, data); \ | |
676 | FILL_FIELD(ptr, common_flags, event, data); \ | |
677 | FILL_FIELD(ptr, common_preempt_count, event, data); \ | |
678 | FILL_FIELD(ptr, common_pid, event, data); \ | |
679 | FILL_FIELD(ptr, common_tgid, event, data); \ | |
680 | } while (0) | |
681 | ||
419ab0d6 FW |
682 | |
683 | ||
684 | struct trace_switch_event { | |
685 | u32 size; | |
686 | ||
687 | u16 common_type; | |
688 | u8 common_flags; | |
689 | u8 common_preempt_count; | |
690 | u32 common_pid; | |
691 | u32 common_tgid; | |
692 | ||
693 | char prev_comm[16]; | |
694 | u32 prev_pid; | |
695 | u32 prev_prio; | |
696 | u64 prev_state; | |
697 | char next_comm[16]; | |
698 | u32 next_pid; | |
699 | u32 next_prio; | |
700 | }; | |
701 | ||
39aeb52f | 702 | struct trace_runtime_event { |
703 | u32 size; | |
704 | ||
705 | u16 common_type; | |
706 | u8 common_flags; | |
707 | u8 common_preempt_count; | |
708 | u32 common_pid; | |
709 | u32 common_tgid; | |
710 | ||
711 | char comm[16]; | |
712 | u32 pid; | |
713 | u64 runtime; | |
714 | u64 vruntime; | |
715 | }; | |
419ab0d6 | 716 | |
fbf94829 IM |
717 | struct trace_wakeup_event { |
718 | u32 size; | |
719 | ||
720 | u16 common_type; | |
721 | u8 common_flags; | |
722 | u8 common_preempt_count; | |
723 | u32 common_pid; | |
724 | u32 common_tgid; | |
725 | ||
726 | char comm[16]; | |
727 | u32 pid; | |
728 | ||
729 | u32 prio; | |
730 | u32 success; | |
731 | u32 cpu; | |
732 | }; | |
733 | ||
419ab0d6 FW |
734 | struct trace_fork_event { |
735 | u32 size; | |
46538818 | 736 | |
419ab0d6 FW |
737 | u16 common_type; |
738 | u8 common_flags; | |
739 | u8 common_preempt_count; | |
740 | u32 common_pid; | |
741 | u32 common_tgid; | |
742 | ||
743 | char parent_comm[16]; | |
744 | u32 parent_pid; | |
745 | char child_comm[16]; | |
746 | u32 child_pid; | |
747 | }; | |
748 | ||
55ffb7a6 MG |
749 | struct trace_migrate_task_event { |
750 | u32 size; | |
751 | ||
752 | u16 common_type; | |
753 | u8 common_flags; | |
754 | u8 common_preempt_count; | |
755 | u32 common_pid; | |
756 | u32 common_tgid; | |
757 | ||
758 | char comm[16]; | |
759 | u32 pid; | |
760 | ||
761 | u32 prio; | |
762 | u32 cpu; | |
763 | }; | |
764 | ||
419ab0d6 FW |
765 | struct trace_sched_handler { |
766 | void (*switch_event)(struct trace_switch_event *, | |
767 | struct event *, | |
768 | int cpu, | |
769 | u64 timestamp, | |
770 | struct thread *thread); | |
771 | ||
39aeb52f | 772 | void (*runtime_event)(struct trace_runtime_event *, |
773 | struct event *, | |
774 | int cpu, | |
775 | u64 timestamp, | |
776 | struct thread *thread); | |
777 | ||
419ab0d6 FW |
778 | void (*wakeup_event)(struct trace_wakeup_event *, |
779 | struct event *, | |
780 | int cpu, | |
781 | u64 timestamp, | |
782 | struct thread *thread); | |
783 | ||
784 | void (*fork_event)(struct trace_fork_event *, | |
785 | struct event *, | |
786 | int cpu, | |
787 | u64 timestamp, | |
788 | struct thread *thread); | |
55ffb7a6 MG |
789 | |
790 | void (*migrate_task_event)(struct trace_migrate_task_event *, | |
791 | struct event *, | |
792 | int cpu, | |
793 | u64 timestamp, | |
794 | struct thread *thread); | |
419ab0d6 | 795 | }; |
46538818 | 796 | |
46538818 | 797 | |
419ab0d6 FW |
798 | static void |
799 | replay_wakeup_event(struct trace_wakeup_event *wakeup_event, | |
800 | struct event *event, | |
801 | int cpu __used, | |
802 | u64 timestamp __used, | |
803 | struct thread *thread __used) | |
804 | { | |
805 | struct task_desc *waker, *wakee; | |
fbf94829 | 806 | |
ad236fd2 IM |
807 | if (verbose) { |
808 | printf("sched_wakeup event %p\n", event); | |
fbf94829 | 809 | |
ad236fd2 | 810 | printf(" ... pid %d woke up %s/%d\n", |
419ab0d6 FW |
811 | wakeup_event->common_pid, |
812 | wakeup_event->comm, | |
813 | wakeup_event->pid); | |
ad236fd2 | 814 | } |
fbf94829 | 815 | |
419ab0d6 FW |
816 | waker = register_pid(wakeup_event->common_pid, "<unknown>"); |
817 | wakee = register_pid(wakeup_event->pid, wakeup_event->comm); | |
fbf94829 IM |
818 | |
819 | add_sched_event_wakeup(waker, timestamp, wakee); | |
ec156764 IM |
820 | } |
821 | ||
d1153389 | 822 | static u64 cpu_last_switched[MAX_CPUS]; |
fbf94829 IM |
823 | |
824 | static void | |
419ab0d6 FW |
825 | replay_switch_event(struct trace_switch_event *switch_event, |
826 | struct event *event, | |
827 | int cpu, | |
828 | u64 timestamp, | |
829 | struct thread *thread __used) | |
ec156764 | 830 | { |
fbf94829 IM |
831 | struct task_desc *prev, *next; |
832 | u64 timestamp0; | |
833 | s64 delta; | |
834 | ||
ad236fd2 IM |
835 | if (verbose) |
836 | printf("sched_switch event %p\n", event); | |
837 | ||
fbf94829 IM |
838 | if (cpu >= MAX_CPUS || cpu < 0) |
839 | return; | |
840 | ||
841 | timestamp0 = cpu_last_switched[cpu]; | |
842 | if (timestamp0) | |
843 | delta = timestamp - timestamp0; | |
844 | else | |
845 | delta = 0; | |
846 | ||
847 | if (delta < 0) | |
848 | die("hm, delta: %Ld < 0 ?\n", delta); | |
849 | ||
ad236fd2 IM |
850 | if (verbose) { |
851 | printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n", | |
419ab0d6 FW |
852 | switch_event->prev_comm, switch_event->prev_pid, |
853 | switch_event->next_comm, switch_event->next_pid, | |
ad236fd2 IM |
854 | delta); |
855 | } | |
fbf94829 | 856 | |
419ab0d6 FW |
857 | prev = register_pid(switch_event->prev_pid, switch_event->prev_comm); |
858 | next = register_pid(switch_event->next_pid, switch_event->next_comm); | |
fbf94829 IM |
859 | |
860 | cpu_last_switched[cpu] = timestamp; | |
861 | ||
862 | add_sched_event_run(prev, timestamp, delta); | |
419ab0d6 | 863 | add_sched_event_sleep(prev, timestamp, switch_event->prev_state); |
fbf94829 IM |
864 | } |
865 | ||
fbf94829 | 866 | |
419ab0d6 FW |
867 | static void |
868 | replay_fork_event(struct trace_fork_event *fork_event, | |
869 | struct event *event, | |
870 | int cpu __used, | |
871 | u64 timestamp __used, | |
872 | struct thread *thread __used) | |
873 | { | |
874 | if (verbose) { | |
875 | printf("sched_fork event %p\n", event); | |
876 | printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid); | |
877 | printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid); | |
878 | } | |
879 | register_pid(fork_event->parent_pid, fork_event->parent_comm); | |
880 | register_pid(fork_event->child_pid, fork_event->child_comm); | |
881 | } | |
fbf94829 | 882 | |
419ab0d6 | 883 | static struct trace_sched_handler replay_ops = { |
ea92ed5a IM |
884 | .wakeup_event = replay_wakeup_event, |
885 | .switch_event = replay_switch_event, | |
886 | .fork_event = replay_fork_event, | |
fbf94829 IM |
887 | }; |
888 | ||
b1ffe8f3 IM |
889 | struct sort_dimension { |
890 | const char *name; | |
b5fae128 | 891 | sort_fn_t cmp; |
b1ffe8f3 IM |
892 | struct list_head list; |
893 | }; | |
894 | ||
895 | static LIST_HEAD(cmp_pid); | |
896 | ||
daa1d7a5 | 897 | static int |
39aeb52f | 898 | thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
899 | { |
900 | struct sort_dimension *sort; | |
901 | int ret = 0; | |
902 | ||
b5fae128 IM |
903 | BUG_ON(list_empty(list)); |
904 | ||
daa1d7a5 FW |
905 | list_for_each_entry(sort, list, list) { |
906 | ret = sort->cmp(l, r); | |
907 | if (ret) | |
908 | return ret; | |
909 | } | |
910 | ||
911 | return ret; | |
912 | } | |
913 | ||
39aeb52f | 914 | static struct work_atoms * |
b5fae128 IM |
915 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
916 | struct list_head *sort_list) | |
917 | { | |
918 | struct rb_node *node = root->rb_node; | |
39aeb52f | 919 | struct work_atoms key = { .thread = thread }; |
b5fae128 IM |
920 | |
921 | while (node) { | |
39aeb52f | 922 | struct work_atoms *atoms; |
b5fae128 IM |
923 | int cmp; |
924 | ||
39aeb52f | 925 | atoms = container_of(node, struct work_atoms, node); |
b5fae128 IM |
926 | |
927 | cmp = thread_lat_cmp(sort_list, &key, atoms); | |
928 | if (cmp > 0) | |
929 | node = node->rb_left; | |
930 | else if (cmp < 0) | |
931 | node = node->rb_right; | |
932 | else { | |
933 | BUG_ON(thread != atoms->thread); | |
934 | return atoms; | |
935 | } | |
936 | } | |
937 | return NULL; | |
938 | } | |
939 | ||
cdce9d73 | 940 | static void |
39aeb52f | 941 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
daa1d7a5 | 942 | struct list_head *sort_list) |
cdce9d73 FW |
943 | { |
944 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
945 | ||
946 | while (*new) { | |
39aeb52f | 947 | struct work_atoms *this; |
daa1d7a5 | 948 | int cmp; |
cdce9d73 | 949 | |
39aeb52f | 950 | this = container_of(*new, struct work_atoms, node); |
cdce9d73 | 951 | parent = *new; |
daa1d7a5 FW |
952 | |
953 | cmp = thread_lat_cmp(sort_list, data, this); | |
954 | ||
955 | if (cmp > 0) | |
cdce9d73 | 956 | new = &((*new)->rb_left); |
cdce9d73 | 957 | else |
daa1d7a5 | 958 | new = &((*new)->rb_right); |
cdce9d73 FW |
959 | } |
960 | ||
961 | rb_link_node(&data->node, parent, new); | |
962 | rb_insert_color(&data->node, root); | |
963 | } | |
964 | ||
b1ffe8f3 | 965 | static void thread_atoms_insert(struct thread *thread) |
cdce9d73 | 966 | { |
39aeb52f | 967 | struct work_atoms *atoms; |
b1ffe8f3 | 968 | |
17562205 FW |
969 | atoms = calloc(sizeof(*atoms), 1); |
970 | if (!atoms) | |
cdce9d73 FW |
971 | die("No memory"); |
972 | ||
17562205 | 973 | atoms->thread = thread; |
39aeb52f | 974 | INIT_LIST_HEAD(&atoms->work_list); |
b1ffe8f3 | 975 | __thread_latency_insert(&atom_root, atoms, &cmp_pid); |
cdce9d73 FW |
976 | } |
977 | ||
978 | static void | |
979 | latency_fork_event(struct trace_fork_event *fork_event __used, | |
980 | struct event *event __used, | |
981 | int cpu __used, | |
982 | u64 timestamp __used, | |
983 | struct thread *thread __used) | |
984 | { | |
985 | /* should insert the newcomer */ | |
986 | } | |
987 | ||
ea92ed5a | 988 | __used |
cdce9d73 FW |
989 | static char sched_out_state(struct trace_switch_event *switch_event) |
990 | { | |
991 | const char *str = TASK_STATE_TO_CHAR_STR; | |
992 | ||
993 | return str[switch_event->prev_state]; | |
994 | } | |
995 | ||
996 | static void | |
39aeb52f | 997 | add_sched_out_event(struct work_atoms *atoms, |
998 | char run_state, | |
999 | u64 timestamp) | |
cdce9d73 | 1000 | { |
b1ffe8f3 | 1001 | struct work_atom *atom; |
cdce9d73 | 1002 | |
b1ffe8f3 IM |
1003 | atom = calloc(sizeof(*atom), 1); |
1004 | if (!atom) | |
cdce9d73 FW |
1005 | die("Non memory"); |
1006 | ||
aa1ab9d2 FW |
1007 | atom->sched_out_time = timestamp; |
1008 | ||
39aeb52f | 1009 | if (run_state == 'R') { |
b1ffe8f3 | 1010 | atom->state = THREAD_WAIT_CPU; |
aa1ab9d2 | 1011 | atom->wake_up_time = atom->sched_out_time; |
c6ced611 FW |
1012 | } |
1013 | ||
39aeb52f | 1014 | list_add_tail(&atom->list, &atoms->work_list); |
cdce9d73 FW |
1015 | } |
1016 | ||
1017 | static void | |
39aeb52f | 1018 | add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used) |
1019 | { | |
1020 | struct work_atom *atom; | |
1021 | ||
1022 | BUG_ON(list_empty(&atoms->work_list)); | |
1023 | ||
1024 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
1025 | ||
1026 | atom->runtime += delta; | |
1027 | atoms->total_runtime += delta; | |
1028 | } | |
1029 | ||
1030 | static void | |
1031 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) | |
cdce9d73 | 1032 | { |
b1ffe8f3 | 1033 | struct work_atom *atom; |
66685678 | 1034 | u64 delta; |
cdce9d73 | 1035 | |
39aeb52f | 1036 | if (list_empty(&atoms->work_list)) |
cdce9d73 FW |
1037 | return; |
1038 | ||
39aeb52f | 1039 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 1040 | |
b1ffe8f3 | 1041 | if (atom->state != THREAD_WAIT_CPU) |
cdce9d73 FW |
1042 | return; |
1043 | ||
b1ffe8f3 IM |
1044 | if (timestamp < atom->wake_up_time) { |
1045 | atom->state = THREAD_IGNORE; | |
cdce9d73 FW |
1046 | return; |
1047 | } | |
1048 | ||
b1ffe8f3 IM |
1049 | atom->state = THREAD_SCHED_IN; |
1050 | atom->sched_in_time = timestamp; | |
66685678 | 1051 | |
b1ffe8f3 | 1052 | delta = atom->sched_in_time - atom->wake_up_time; |
66685678 FW |
1053 | atoms->total_lat += delta; |
1054 | if (delta > atoms->max_lat) | |
1055 | atoms->max_lat = delta; | |
1056 | atoms->nb_atoms++; | |
cdce9d73 FW |
1057 | } |
1058 | ||
cdce9d73 FW |
1059 | static void |
1060 | latency_switch_event(struct trace_switch_event *switch_event, | |
1061 | struct event *event __used, | |
ea92ed5a | 1062 | int cpu, |
cdce9d73 FW |
1063 | u64 timestamp, |
1064 | struct thread *thread __used) | |
1065 | { | |
39aeb52f | 1066 | struct work_atoms *out_events, *in_events; |
cdce9d73 | 1067 | struct thread *sched_out, *sched_in; |
ea92ed5a IM |
1068 | u64 timestamp0; |
1069 | s64 delta; | |
1070 | ||
39aeb52f | 1071 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
ea92ed5a IM |
1072 | |
1073 | timestamp0 = cpu_last_switched[cpu]; | |
1074 | cpu_last_switched[cpu] = timestamp; | |
1075 | if (timestamp0) | |
1076 | delta = timestamp - timestamp0; | |
1077 | else | |
1078 | delta = 0; | |
1079 | ||
1080 | if (delta < 0) | |
1081 | die("hm, delta: %Ld < 0 ?\n", delta); | |
1082 | ||
cdce9d73 | 1083 | |
d5b889f2 ACM |
1084 | sched_out = threads__findnew(switch_event->prev_pid); |
1085 | sched_in = threads__findnew(switch_event->next_pid); | |
cdce9d73 | 1086 | |
39aeb52f | 1087 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); |
1088 | if (!out_events) { | |
1089 | thread_atoms_insert(sched_out); | |
1090 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); | |
1091 | if (!out_events) | |
1092 | die("out-event: Internal tree error"); | |
1093 | } | |
1094 | add_sched_out_event(out_events, sched_out_state(switch_event), timestamp); | |
1095 | ||
1096 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); | |
1097 | if (!in_events) { | |
b1ffe8f3 | 1098 | thread_atoms_insert(sched_in); |
39aeb52f | 1099 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); |
1100 | if (!in_events) | |
1101 | die("in-event: Internal tree error"); | |
1102 | /* | |
1103 | * Take came in we have not heard about yet, | |
1104 | * add in an initial atom in runnable state: | |
1105 | */ | |
1106 | add_sched_out_event(in_events, 'R', timestamp); | |
cdce9d73 | 1107 | } |
39aeb52f | 1108 | add_sched_in_event(in_events, timestamp); |
1109 | } | |
cdce9d73 | 1110 | |
39aeb52f | 1111 | static void |
1112 | latency_runtime_event(struct trace_runtime_event *runtime_event, | |
1113 | struct event *event __used, | |
1114 | int cpu, | |
1115 | u64 timestamp, | |
1116 | struct thread *this_thread __used) | |
1117 | { | |
d5b889f2 ACM |
1118 | struct thread *thread = threads__findnew(runtime_event->pid); |
1119 | struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); | |
39aeb52f | 1120 | |
1121 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); | |
39aeb52f | 1122 | if (!atoms) { |
1123 | thread_atoms_insert(thread); | |
1124 | atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); | |
1125 | if (!atoms) | |
1126 | die("in-event: Internal tree error"); | |
1127 | add_sched_out_event(atoms, 'R', timestamp); | |
cdce9d73 FW |
1128 | } |
1129 | ||
39aeb52f | 1130 | add_runtime_event(atoms, runtime_event->runtime, timestamp); |
cdce9d73 FW |
1131 | } |
1132 | ||
1133 | static void | |
1134 | latency_wakeup_event(struct trace_wakeup_event *wakeup_event, | |
39aeb52f | 1135 | struct event *__event __used, |
cdce9d73 FW |
1136 | int cpu __used, |
1137 | u64 timestamp, | |
1138 | struct thread *thread __used) | |
1139 | { | |
39aeb52f | 1140 | struct work_atoms *atoms; |
b1ffe8f3 | 1141 | struct work_atom *atom; |
cdce9d73 FW |
1142 | struct thread *wakee; |
1143 | ||
1144 | /* Note for later, it may be interesting to observe the failing cases */ | |
1145 | if (!wakeup_event->success) | |
1146 | return; | |
1147 | ||
d5b889f2 | 1148 | wakee = threads__findnew(wakeup_event->pid); |
b5fae128 | 1149 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); |
17562205 | 1150 | if (!atoms) { |
b1ffe8f3 | 1151 | thread_atoms_insert(wakee); |
39aeb52f | 1152 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); |
1153 | if (!atoms) | |
1154 | die("wakeup-event: Internal tree error"); | |
1155 | add_sched_out_event(atoms, 'S', timestamp); | |
cdce9d73 FW |
1156 | } |
1157 | ||
39aeb52f | 1158 | BUG_ON(list_empty(&atoms->work_list)); |
cdce9d73 | 1159 | |
39aeb52f | 1160 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 1161 | |
55ffb7a6 MG |
1162 | /* |
1163 | * You WILL be missing events if you've recorded only | |
1164 | * one CPU, or are only looking at only one, so don't | |
1165 | * make useless noise. | |
1166 | */ | |
1167 | if (profile_cpu == -1 && atom->state != THREAD_SLEEPING) | |
dc02bf71 | 1168 | nr_state_machine_bugs++; |
cdce9d73 | 1169 | |
ea57c4f5 IM |
1170 | nr_timestamps++; |
1171 | if (atom->sched_out_time > timestamp) { | |
dc02bf71 | 1172 | nr_unordered_timestamps++; |
aa1ab9d2 | 1173 | return; |
ea57c4f5 | 1174 | } |
aa1ab9d2 | 1175 | |
b1ffe8f3 IM |
1176 | atom->state = THREAD_WAIT_CPU; |
1177 | atom->wake_up_time = timestamp; | |
cdce9d73 FW |
1178 | } |
1179 | ||
55ffb7a6 MG |
1180 | static void |
1181 | latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event, | |
1182 | struct event *__event __used, | |
1183 | int cpu __used, | |
1184 | u64 timestamp, | |
1185 | struct thread *thread __used) | |
1186 | { | |
1187 | struct work_atoms *atoms; | |
1188 | struct work_atom *atom; | |
1189 | struct thread *migrant; | |
1190 | ||
1191 | /* | |
1192 | * Only need to worry about migration when profiling one CPU. | |
1193 | */ | |
1194 | if (profile_cpu == -1) | |
1195 | return; | |
1196 | ||
d5b889f2 | 1197 | migrant = threads__findnew(migrate_task_event->pid); |
55ffb7a6 MG |
1198 | atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid); |
1199 | if (!atoms) { | |
1200 | thread_atoms_insert(migrant); | |
1201 | register_pid(migrant->pid, migrant->comm); | |
1202 | atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid); | |
1203 | if (!atoms) | |
1204 | die("migration-event: Internal tree error"); | |
1205 | add_sched_out_event(atoms, 'R', timestamp); | |
1206 | } | |
1207 | ||
1208 | BUG_ON(list_empty(&atoms->work_list)); | |
1209 | ||
1210 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
1211 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; | |
1212 | ||
1213 | nr_timestamps++; | |
1214 | ||
1215 | if (atom->sched_out_time > timestamp) | |
1216 | nr_unordered_timestamps++; | |
1217 | } | |
1218 | ||
cdce9d73 | 1219 | static struct trace_sched_handler lat_ops = { |
ea92ed5a IM |
1220 | .wakeup_event = latency_wakeup_event, |
1221 | .switch_event = latency_switch_event, | |
39aeb52f | 1222 | .runtime_event = latency_runtime_event, |
ea92ed5a | 1223 | .fork_event = latency_fork_event, |
55ffb7a6 | 1224 | .migrate_task_event = latency_migrate_task_event, |
cdce9d73 FW |
1225 | }; |
1226 | ||
39aeb52f | 1227 | static void output_lat_thread(struct work_atoms *work_list) |
cdce9d73 | 1228 | { |
cdce9d73 FW |
1229 | int i; |
1230 | int ret; | |
66685678 | 1231 | u64 avg; |
cdce9d73 | 1232 | |
39aeb52f | 1233 | if (!work_list->nb_atoms) |
cdce9d73 | 1234 | return; |
ea57c4f5 IM |
1235 | /* |
1236 | * Ignore idle threads: | |
1237 | */ | |
80ed0987 | 1238 | if (!strcmp(work_list->thread->comm, "swapper")) |
ea57c4f5 | 1239 | return; |
cdce9d73 | 1240 | |
39aeb52f | 1241 | all_runtime += work_list->total_runtime; |
1242 | all_count += work_list->nb_atoms; | |
66685678 | 1243 | |
80ed0987 | 1244 | ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid); |
cdce9d73 | 1245 | |
08f69e6c | 1246 | for (i = 0; i < 24 - ret; i++) |
cdce9d73 FW |
1247 | printf(" "); |
1248 | ||
39aeb52f | 1249 | avg = work_list->total_lat / work_list->nb_atoms; |
cdce9d73 | 1250 | |
dc02bf71 | 1251 | printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n", |
39aeb52f | 1252 | (double)work_list->total_runtime / 1e6, |
1253 | work_list->nb_atoms, (double)avg / 1e6, | |
1254 | (double)work_list->max_lat / 1e6); | |
cdce9d73 FW |
1255 | } |
1256 | ||
39aeb52f | 1257 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 | 1258 | { |
daa1d7a5 FW |
1259 | if (l->thread->pid < r->thread->pid) |
1260 | return -1; | |
1261 | if (l->thread->pid > r->thread->pid) | |
1262 | return 1; | |
1263 | ||
1264 | return 0; | |
1265 | } | |
1266 | ||
1267 | static struct sort_dimension pid_sort_dimension = { | |
b5fae128 IM |
1268 | .name = "pid", |
1269 | .cmp = pid_cmp, | |
daa1d7a5 FW |
1270 | }; |
1271 | ||
39aeb52f | 1272 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1273 | { |
1274 | u64 avgl, avgr; | |
1275 | ||
1276 | if (!l->nb_atoms) | |
1277 | return -1; | |
1278 | ||
1279 | if (!r->nb_atoms) | |
1280 | return 1; | |
1281 | ||
1282 | avgl = l->total_lat / l->nb_atoms; | |
1283 | avgr = r->total_lat / r->nb_atoms; | |
1284 | ||
1285 | if (avgl < avgr) | |
1286 | return -1; | |
1287 | if (avgl > avgr) | |
1288 | return 1; | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | static struct sort_dimension avg_sort_dimension = { | |
b5fae128 IM |
1294 | .name = "avg", |
1295 | .cmp = avg_cmp, | |
daa1d7a5 FW |
1296 | }; |
1297 | ||
39aeb52f | 1298 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1299 | { |
1300 | if (l->max_lat < r->max_lat) | |
1301 | return -1; | |
1302 | if (l->max_lat > r->max_lat) | |
1303 | return 1; | |
1304 | ||
1305 | return 0; | |
1306 | } | |
1307 | ||
1308 | static struct sort_dimension max_sort_dimension = { | |
b5fae128 IM |
1309 | .name = "max", |
1310 | .cmp = max_cmp, | |
daa1d7a5 FW |
1311 | }; |
1312 | ||
39aeb52f | 1313 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1314 | { |
1315 | if (l->nb_atoms < r->nb_atoms) | |
1316 | return -1; | |
1317 | if (l->nb_atoms > r->nb_atoms) | |
1318 | return 1; | |
1319 | ||
1320 | return 0; | |
1321 | } | |
1322 | ||
1323 | static struct sort_dimension switch_sort_dimension = { | |
b5fae128 IM |
1324 | .name = "switch", |
1325 | .cmp = switch_cmp, | |
daa1d7a5 FW |
1326 | }; |
1327 | ||
39aeb52f | 1328 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1329 | { |
1330 | if (l->total_runtime < r->total_runtime) | |
1331 | return -1; | |
1332 | if (l->total_runtime > r->total_runtime) | |
1333 | return 1; | |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | ||
1338 | static struct sort_dimension runtime_sort_dimension = { | |
b5fae128 IM |
1339 | .name = "runtime", |
1340 | .cmp = runtime_cmp, | |
daa1d7a5 FW |
1341 | }; |
1342 | ||
1343 | static struct sort_dimension *available_sorts[] = { | |
1344 | &pid_sort_dimension, | |
1345 | &avg_sort_dimension, | |
1346 | &max_sort_dimension, | |
1347 | &switch_sort_dimension, | |
1348 | &runtime_sort_dimension, | |
1349 | }; | |
1350 | ||
1351 | #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *)) | |
1352 | ||
1353 | static LIST_HEAD(sort_list); | |
1354 | ||
cbef79a8 | 1355 | static int sort_dimension__add(const char *tok, struct list_head *list) |
daa1d7a5 FW |
1356 | { |
1357 | int i; | |
1358 | ||
1359 | for (i = 0; i < NB_AVAILABLE_SORTS; i++) { | |
1360 | if (!strcmp(available_sorts[i]->name, tok)) { | |
1361 | list_add_tail(&available_sorts[i]->list, list); | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | return -1; | |
1368 | } | |
1369 | ||
1370 | static void setup_sorting(void); | |
1371 | ||
1372 | static void sort_lat(void) | |
1373 | { | |
1374 | struct rb_node *node; | |
1375 | ||
1376 | for (;;) { | |
39aeb52f | 1377 | struct work_atoms *data; |
b1ffe8f3 | 1378 | node = rb_first(&atom_root); |
daa1d7a5 FW |
1379 | if (!node) |
1380 | break; | |
1381 | ||
b1ffe8f3 | 1382 | rb_erase(node, &atom_root); |
39aeb52f | 1383 | data = rb_entry(node, struct work_atoms, node); |
b1ffe8f3 | 1384 | __thread_latency_insert(&sorted_atom_root, data, &sort_list); |
daa1d7a5 FW |
1385 | } |
1386 | } | |
1387 | ||
419ab0d6 FW |
1388 | static struct trace_sched_handler *trace_handler; |
1389 | ||
fbf94829 | 1390 | static void |
419ab0d6 FW |
1391 | process_sched_wakeup_event(struct raw_event_sample *raw, |
1392 | struct event *event, | |
1393 | int cpu __used, | |
1394 | u64 timestamp __used, | |
1395 | struct thread *thread __used) | |
1396 | { | |
1397 | struct trace_wakeup_event wakeup_event; | |
1398 | ||
1399 | FILL_COMMON_FIELDS(wakeup_event, event, raw->data); | |
1400 | ||
1401 | FILL_ARRAY(wakeup_event, comm, event, raw->data); | |
1402 | FILL_FIELD(wakeup_event, pid, event, raw->data); | |
1403 | FILL_FIELD(wakeup_event, prio, event, raw->data); | |
1404 | FILL_FIELD(wakeup_event, success, event, raw->data); | |
1405 | FILL_FIELD(wakeup_event, cpu, event, raw->data); | |
1406 | ||
0ec04e16 IM |
1407 | if (trace_handler->wakeup_event) |
1408 | trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread); | |
419ab0d6 FW |
1409 | } |
1410 | ||
c8a37751 IM |
1411 | /* |
1412 | * Track the current task - that way we can know whether there's any | |
1413 | * weird events, such as a task being switched away that is not current. | |
1414 | */ | |
40749d0f | 1415 | static int max_cpu; |
0ec04e16 | 1416 | |
c8a37751 IM |
1417 | static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 }; |
1418 | ||
0ec04e16 IM |
1419 | static struct thread *curr_thread[MAX_CPUS]; |
1420 | ||
1421 | static char next_shortname1 = 'A'; | |
1422 | static char next_shortname2 = '0'; | |
1423 | ||
1424 | static void | |
1425 | map_switch_event(struct trace_switch_event *switch_event, | |
1426 | struct event *event __used, | |
1427 | int this_cpu, | |
1428 | u64 timestamp, | |
1429 | struct thread *thread __used) | |
1430 | { | |
1431 | struct thread *sched_out, *sched_in; | |
1432 | int new_shortname; | |
1433 | u64 timestamp0; | |
1434 | s64 delta; | |
1435 | int cpu; | |
1436 | ||
1437 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); | |
1438 | ||
1439 | if (this_cpu > max_cpu) | |
1440 | max_cpu = this_cpu; | |
1441 | ||
1442 | timestamp0 = cpu_last_switched[this_cpu]; | |
1443 | cpu_last_switched[this_cpu] = timestamp; | |
1444 | if (timestamp0) | |
1445 | delta = timestamp - timestamp0; | |
1446 | else | |
1447 | delta = 0; | |
1448 | ||
1449 | if (delta < 0) | |
1450 | die("hm, delta: %Ld < 0 ?\n", delta); | |
1451 | ||
1452 | ||
d5b889f2 ACM |
1453 | sched_out = threads__findnew(switch_event->prev_pid); |
1454 | sched_in = threads__findnew(switch_event->next_pid); | |
0ec04e16 IM |
1455 | |
1456 | curr_thread[this_cpu] = sched_in; | |
1457 | ||
1458 | printf(" "); | |
1459 | ||
1460 | new_shortname = 0; | |
1461 | if (!sched_in->shortname[0]) { | |
1462 | sched_in->shortname[0] = next_shortname1; | |
1463 | sched_in->shortname[1] = next_shortname2; | |
1464 | ||
1465 | if (next_shortname1 < 'Z') { | |
1466 | next_shortname1++; | |
1467 | } else { | |
1468 | next_shortname1='A'; | |
1469 | if (next_shortname2 < '9') { | |
1470 | next_shortname2++; | |
1471 | } else { | |
1472 | next_shortname2='0'; | |
1473 | } | |
1474 | } | |
1475 | new_shortname = 1; | |
1476 | } | |
1477 | ||
1478 | for (cpu = 0; cpu <= max_cpu; cpu++) { | |
1479 | if (cpu != this_cpu) | |
1480 | printf(" "); | |
1481 | else | |
1482 | printf("*"); | |
1483 | ||
1484 | if (curr_thread[cpu]) { | |
1485 | if (curr_thread[cpu]->pid) | |
1486 | printf("%2s ", curr_thread[cpu]->shortname); | |
1487 | else | |
1488 | printf(". "); | |
1489 | } else | |
1490 | printf(" "); | |
1491 | } | |
1492 | ||
1493 | printf(" %12.6f secs ", (double)timestamp/1e9); | |
1494 | if (new_shortname) { | |
1495 | printf("%s => %s:%d\n", | |
1496 | sched_in->shortname, sched_in->comm, sched_in->pid); | |
1497 | } else { | |
1498 | printf("\n"); | |
1499 | } | |
1500 | } | |
1501 | ||
1502 | ||
419ab0d6 FW |
1503 | static void |
1504 | process_sched_switch_event(struct raw_event_sample *raw, | |
1505 | struct event *event, | |
0ec04e16 | 1506 | int this_cpu, |
419ab0d6 FW |
1507 | u64 timestamp __used, |
1508 | struct thread *thread __used) | |
1509 | { | |
1510 | struct trace_switch_event switch_event; | |
1511 | ||
1512 | FILL_COMMON_FIELDS(switch_event, event, raw->data); | |
1513 | ||
1514 | FILL_ARRAY(switch_event, prev_comm, event, raw->data); | |
1515 | FILL_FIELD(switch_event, prev_pid, event, raw->data); | |
1516 | FILL_FIELD(switch_event, prev_prio, event, raw->data); | |
1517 | FILL_FIELD(switch_event, prev_state, event, raw->data); | |
1518 | FILL_ARRAY(switch_event, next_comm, event, raw->data); | |
1519 | FILL_FIELD(switch_event, next_pid, event, raw->data); | |
1520 | FILL_FIELD(switch_event, next_prio, event, raw->data); | |
1521 | ||
0ec04e16 | 1522 | if (curr_pid[this_cpu] != (u32)-1) { |
c8a37751 IM |
1523 | /* |
1524 | * Are we trying to switch away a PID that is | |
1525 | * not current? | |
1526 | */ | |
0ec04e16 | 1527 | if (curr_pid[this_cpu] != switch_event.prev_pid) |
c8a37751 IM |
1528 | nr_context_switch_bugs++; |
1529 | } | |
0ec04e16 IM |
1530 | if (trace_handler->switch_event) |
1531 | trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread); | |
c8a37751 | 1532 | |
0ec04e16 | 1533 | curr_pid[this_cpu] = switch_event.next_pid; |
419ab0d6 FW |
1534 | } |
1535 | ||
39aeb52f | 1536 | static void |
1537 | process_sched_runtime_event(struct raw_event_sample *raw, | |
1538 | struct event *event, | |
1539 | int cpu __used, | |
1540 | u64 timestamp __used, | |
1541 | struct thread *thread __used) | |
1542 | { | |
1543 | struct trace_runtime_event runtime_event; | |
1544 | ||
1545 | FILL_ARRAY(runtime_event, comm, event, raw->data); | |
1546 | FILL_FIELD(runtime_event, pid, event, raw->data); | |
1547 | FILL_FIELD(runtime_event, runtime, event, raw->data); | |
1548 | FILL_FIELD(runtime_event, vruntime, event, raw->data); | |
1549 | ||
0ec04e16 IM |
1550 | if (trace_handler->runtime_event) |
1551 | trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread); | |
39aeb52f | 1552 | } |
1553 | ||
419ab0d6 FW |
1554 | static void |
1555 | process_sched_fork_event(struct raw_event_sample *raw, | |
1556 | struct event *event, | |
1557 | int cpu __used, | |
1558 | u64 timestamp __used, | |
1559 | struct thread *thread __used) | |
fbf94829 | 1560 | { |
46538818 FW |
1561 | struct trace_fork_event fork_event; |
1562 | ||
1563 | FILL_COMMON_FIELDS(fork_event, event, raw->data); | |
1564 | ||
1565 | FILL_ARRAY(fork_event, parent_comm, event, raw->data); | |
1566 | FILL_FIELD(fork_event, parent_pid, event, raw->data); | |
1567 | FILL_ARRAY(fork_event, child_comm, event, raw->data); | |
1568 | FILL_FIELD(fork_event, child_pid, event, raw->data); | |
1569 | ||
0ec04e16 IM |
1570 | if (trace_handler->fork_event) |
1571 | trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread); | |
fbf94829 IM |
1572 | } |
1573 | ||
419ab0d6 FW |
1574 | static void |
1575 | process_sched_exit_event(struct event *event, | |
1576 | int cpu __used, | |
1577 | u64 timestamp __used, | |
1578 | struct thread *thread __used) | |
fbf94829 | 1579 | { |
ad236fd2 IM |
1580 | if (verbose) |
1581 | printf("sched_exit event %p\n", event); | |
ec156764 IM |
1582 | } |
1583 | ||
55ffb7a6 MG |
1584 | static void |
1585 | process_sched_migrate_task_event(struct raw_event_sample *raw, | |
1586 | struct event *event, | |
1587 | int cpu __used, | |
1588 | u64 timestamp __used, | |
1589 | struct thread *thread __used) | |
1590 | { | |
1591 | struct trace_migrate_task_event migrate_task_event; | |
1592 | ||
1593 | FILL_COMMON_FIELDS(migrate_task_event, event, raw->data); | |
1594 | ||
1595 | FILL_ARRAY(migrate_task_event, comm, event, raw->data); | |
1596 | FILL_FIELD(migrate_task_event, pid, event, raw->data); | |
1597 | FILL_FIELD(migrate_task_event, prio, event, raw->data); | |
1598 | FILL_FIELD(migrate_task_event, cpu, event, raw->data); | |
1599 | ||
1600 | if (trace_handler->migrate_task_event) | |
1601 | trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread); | |
1602 | } | |
1603 | ||
ec156764 | 1604 | static void |
ad236fd2 | 1605 | process_raw_event(event_t *raw_event __used, void *more_data, |
ec156764 IM |
1606 | int cpu, u64 timestamp, struct thread *thread) |
1607 | { | |
46538818 | 1608 | struct raw_event_sample *raw = more_data; |
ec156764 IM |
1609 | struct event *event; |
1610 | int type; | |
1611 | ||
1612 | type = trace_parse_common_type(raw->data); | |
1613 | event = trace_find_event(type); | |
1614 | ||
ec156764 | 1615 | if (!strcmp(event->name, "sched_switch")) |
46538818 | 1616 | process_sched_switch_event(raw, event, cpu, timestamp, thread); |
39aeb52f | 1617 | if (!strcmp(event->name, "sched_stat_runtime")) |
1618 | process_sched_runtime_event(raw, event, cpu, timestamp, thread); | |
ec156764 | 1619 | if (!strcmp(event->name, "sched_wakeup")) |
46538818 | 1620 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
fbf94829 | 1621 | if (!strcmp(event->name, "sched_wakeup_new")) |
46538818 | 1622 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
fbf94829 | 1623 | if (!strcmp(event->name, "sched_process_fork")) |
46538818 | 1624 | process_sched_fork_event(raw, event, cpu, timestamp, thread); |
fbf94829 IM |
1625 | if (!strcmp(event->name, "sched_process_exit")) |
1626 | process_sched_exit_event(event, cpu, timestamp, thread); | |
55ffb7a6 MG |
1627 | if (!strcmp(event->name, "sched_migrate_task")) |
1628 | process_sched_migrate_task_event(raw, event, cpu, timestamp, thread); | |
ec156764 IM |
1629 | } |
1630 | ||
0a02ad93 IM |
1631 | static int |
1632 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |
1633 | { | |
0a02ad93 IM |
1634 | struct thread *thread; |
1635 | u64 ip = event->ip.ip; | |
1636 | u64 timestamp = -1; | |
1637 | u32 cpu = -1; | |
1638 | u64 period = 1; | |
1639 | void *more_data = event->ip.__more_data; | |
a80deb62 ACM |
1640 | |
1641 | if (!(sample_type & PERF_SAMPLE_RAW)) | |
1642 | return 0; | |
0a02ad93 | 1643 | |
d5b889f2 | 1644 | thread = threads__findnew(event->ip.pid); |
0a02ad93 IM |
1645 | |
1646 | if (sample_type & PERF_SAMPLE_TIME) { | |
1647 | timestamp = *(u64 *)more_data; | |
1648 | more_data += sizeof(u64); | |
1649 | } | |
1650 | ||
1651 | if (sample_type & PERF_SAMPLE_CPU) { | |
1652 | cpu = *(u32 *)more_data; | |
1653 | more_data += sizeof(u32); | |
1654 | more_data += sizeof(u32); /* reserved */ | |
1655 | } | |
1656 | ||
1657 | if (sample_type & PERF_SAMPLE_PERIOD) { | |
1658 | period = *(u64 *)more_data; | |
1659 | more_data += sizeof(u64); | |
1660 | } | |
1661 | ||
cdd6c482 | 1662 | dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", |
0a02ad93 IM |
1663 | (void *)(offset + head), |
1664 | (void *)(long)(event->header.size), | |
1665 | event->header.misc, | |
1666 | event->ip.pid, event->ip.tid, | |
1667 | (void *)(long)ip, | |
1668 | (long long)period); | |
1669 | ||
1670 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | |
1671 | ||
1672 | if (thread == NULL) { | |
1673 | eprintf("problem processing %d event, skipping it.\n", | |
1674 | event->header.type); | |
1675 | return -1; | |
1676 | } | |
1677 | ||
55ffb7a6 MG |
1678 | if (profile_cpu != -1 && profile_cpu != (int) cpu) |
1679 | return 0; | |
1680 | ||
a80deb62 | 1681 | process_raw_event(event, more_data, cpu, timestamp, thread); |
0a02ad93 IM |
1682 | |
1683 | return 0; | |
1684 | } | |
1685 | ||
1686 | static int | |
016e92fb FW |
1687 | process_lost_event(event_t *event __used, |
1688 | unsigned long offset __used, | |
1689 | unsigned long head __used) | |
0a02ad93 | 1690 | { |
016e92fb FW |
1691 | nr_lost_chunks++; |
1692 | nr_lost_events += event->lost.lost; | |
0a02ad93 | 1693 | |
016e92fb FW |
1694 | return 0; |
1695 | } | |
0a02ad93 | 1696 | |
016e92fb FW |
1697 | static int sample_type_check(u64 type) |
1698 | { | |
1699 | sample_type = type; | |
0a02ad93 | 1700 | |
016e92fb FW |
1701 | if (!(sample_type & PERF_SAMPLE_RAW)) { |
1702 | fprintf(stderr, | |
1703 | "No trace sample to read. Did you call perf record " | |
1704 | "without -R?"); | |
0a02ad93 IM |
1705 | return -1; |
1706 | } | |
1707 | ||
1708 | return 0; | |
1709 | } | |
1710 | ||
016e92fb FW |
1711 | static struct perf_file_handler file_handler = { |
1712 | .process_sample_event = process_sample_event, | |
1713 | .process_comm_event = process_comm_event, | |
1714 | .process_lost_event = process_lost_event, | |
1715 | .sample_type_check = sample_type_check, | |
1716 | }; | |
1717 | ||
46f392c9 | 1718 | static int read_events(void) |
0a02ad93 | 1719 | { |
d5b889f2 | 1720 | register_idle_thread(); |
016e92fb | 1721 | register_perf_file_handler(&file_handler); |
0a02ad93 | 1722 | |
016e92fb | 1723 | return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd); |
0a02ad93 IM |
1724 | } |
1725 | ||
0ec04e16 IM |
1726 | static void print_bad_events(void) |
1727 | { | |
1728 | if (nr_unordered_timestamps && nr_timestamps) { | |
1729 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", | |
1730 | (double)nr_unordered_timestamps/(double)nr_timestamps*100.0, | |
1731 | nr_unordered_timestamps, nr_timestamps); | |
1732 | } | |
1733 | if (nr_lost_events && nr_events) { | |
1734 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", | |
1735 | (double)nr_lost_events/(double)nr_events*100.0, | |
1736 | nr_lost_events, nr_events, nr_lost_chunks); | |
1737 | } | |
1738 | if (nr_state_machine_bugs && nr_timestamps) { | |
1739 | printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)", | |
1740 | (double)nr_state_machine_bugs/(double)nr_timestamps*100.0, | |
1741 | nr_state_machine_bugs, nr_timestamps); | |
1742 | if (nr_lost_events) | |
1743 | printf(" (due to lost events?)"); | |
1744 | printf("\n"); | |
1745 | } | |
1746 | if (nr_context_switch_bugs && nr_timestamps) { | |
1747 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", | |
1748 | (double)nr_context_switch_bugs/(double)nr_timestamps*100.0, | |
1749 | nr_context_switch_bugs, nr_timestamps); | |
1750 | if (nr_lost_events) | |
1751 | printf(" (due to lost events?)"); | |
1752 | printf("\n"); | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | static void __cmd_lat(void) | |
1757 | { | |
1758 | struct rb_node *next; | |
1759 | ||
1760 | setup_pager(); | |
1761 | read_events(); | |
1762 | sort_lat(); | |
1763 | ||
1764 | printf("\n -----------------------------------------------------------------------------------------\n"); | |
1765 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n"); | |
1766 | printf(" -----------------------------------------------------------------------------------------\n"); | |
1767 | ||
1768 | next = rb_first(&sorted_atom_root); | |
1769 | ||
1770 | while (next) { | |
1771 | struct work_atoms *work_list; | |
1772 | ||
1773 | work_list = rb_entry(next, struct work_atoms, node); | |
1774 | output_lat_thread(work_list); | |
1775 | next = rb_next(next); | |
1776 | } | |
1777 | ||
1778 | printf(" -----------------------------------------------------------------------------------------\n"); | |
1779 | printf(" TOTAL: |%11.3f ms |%9Ld |\n", | |
1780 | (double)all_runtime/1e6, all_count); | |
1781 | ||
1782 | printf(" ---------------------------------------------------\n"); | |
1783 | ||
1784 | print_bad_events(); | |
1785 | printf("\n"); | |
1786 | ||
1787 | } | |
1788 | ||
1789 | static struct trace_sched_handler map_ops = { | |
1790 | .wakeup_event = NULL, | |
1791 | .switch_event = map_switch_event, | |
1792 | .runtime_event = NULL, | |
1793 | .fork_event = NULL, | |
1794 | }; | |
1795 | ||
1796 | static void __cmd_map(void) | |
1797 | { | |
40749d0f IM |
1798 | max_cpu = sysconf(_SC_NPROCESSORS_CONF); |
1799 | ||
0ec04e16 IM |
1800 | setup_pager(); |
1801 | read_events(); | |
1802 | print_bad_events(); | |
1803 | } | |
1804 | ||
1805 | static void __cmd_replay(void) | |
1806 | { | |
1807 | unsigned long i; | |
1808 | ||
1809 | calibrate_run_measurement_overhead(); | |
1810 | calibrate_sleep_measurement_overhead(); | |
1811 | ||
1812 | test_calibrations(); | |
1813 | ||
1814 | read_events(); | |
1815 | ||
1816 | printf("nr_run_events: %ld\n", nr_run_events); | |
1817 | printf("nr_sleep_events: %ld\n", nr_sleep_events); | |
1818 | printf("nr_wakeup_events: %ld\n", nr_wakeup_events); | |
1819 | ||
1820 | if (targetless_wakeups) | |
1821 | printf("target-less wakeups: %ld\n", targetless_wakeups); | |
1822 | if (multitarget_wakeups) | |
1823 | printf("multi-target wakeups: %ld\n", multitarget_wakeups); | |
1824 | if (nr_run_events_optimized) | |
1825 | printf("run atoms optimized: %ld\n", | |
1826 | nr_run_events_optimized); | |
1827 | ||
1828 | print_task_traces(); | |
1829 | add_cross_task_wakeups(); | |
1830 | ||
1831 | create_tasks(); | |
1832 | printf("------------------------------------------------------------\n"); | |
1833 | for (i = 0; i < replay_repeat; i++) | |
1834 | run_one_test(); | |
1835 | } | |
1836 | ||
1837 | ||
46f392c9 | 1838 | static const char * const sched_usage[] = { |
4b77a729 | 1839 | "perf sched [<options>] {record|latency|map|replay|trace}", |
0a02ad93 IM |
1840 | NULL |
1841 | }; | |
1842 | ||
f2858d8a | 1843 | static const struct option sched_options[] = { |
4b77a729 MG |
1844 | OPT_STRING('i', "input", &input_name, "file", |
1845 | "input file name"), | |
f2858d8a IM |
1846 | OPT_BOOLEAN('v', "verbose", &verbose, |
1847 | "be more verbose (show symbol address, etc)"), | |
0a02ad93 IM |
1848 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1849 | "dump raw trace in ASCII"), | |
f2858d8a IM |
1850 | OPT_END() |
1851 | }; | |
1852 | ||
1853 | static const char * const latency_usage[] = { | |
1854 | "perf sched latency [<options>]", | |
1855 | NULL | |
1856 | }; | |
1857 | ||
1858 | static const struct option latency_options[] = { | |
daa1d7a5 FW |
1859 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
1860 | "sort by key(s): runtime, switch, avg, max"), | |
0a02ad93 IM |
1861 | OPT_BOOLEAN('v', "verbose", &verbose, |
1862 | "be more verbose (show symbol address, etc)"), | |
55ffb7a6 MG |
1863 | OPT_INTEGER('C', "CPU", &profile_cpu, |
1864 | "CPU to profile on"), | |
f2858d8a IM |
1865 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1866 | "dump raw trace in ASCII"), | |
1867 | OPT_END() | |
1868 | }; | |
1869 | ||
1870 | static const char * const replay_usage[] = { | |
1871 | "perf sched replay [<options>]", | |
1872 | NULL | |
1873 | }; | |
1874 | ||
1875 | static const struct option replay_options[] = { | |
1876 | OPT_INTEGER('r', "repeat", &replay_repeat, | |
1877 | "repeat the workload replay N times (-1: infinite)"), | |
1878 | OPT_BOOLEAN('v', "verbose", &verbose, | |
1879 | "be more verbose (show symbol address, etc)"), | |
1880 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
1881 | "dump raw trace in ASCII"), | |
0a02ad93 IM |
1882 | OPT_END() |
1883 | }; | |
1884 | ||
daa1d7a5 FW |
1885 | static void setup_sorting(void) |
1886 | { | |
1887 | char *tmp, *tok, *str = strdup(sort_order); | |
1888 | ||
1889 | for (tok = strtok_r(str, ", ", &tmp); | |
1890 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
1891 | if (sort_dimension__add(tok, &sort_list) < 0) { | |
1892 | error("Unknown --sort key: `%s'", tok); | |
f2858d8a | 1893 | usage_with_options(latency_usage, latency_options); |
daa1d7a5 FW |
1894 | } |
1895 | } | |
1896 | ||
1897 | free(str); | |
1898 | ||
cbef79a8 | 1899 | sort_dimension__add("pid", &cmp_pid); |
daa1d7a5 FW |
1900 | } |
1901 | ||
1fc35b29 IM |
1902 | static const char *record_args[] = { |
1903 | "record", | |
1904 | "-a", | |
1905 | "-R", | |
d1302522 | 1906 | "-M", |
ea57c4f5 | 1907 | "-f", |
dc02bf71 | 1908 | "-m", "1024", |
1fc35b29 IM |
1909 | "-c", "1", |
1910 | "-e", "sched:sched_switch:r", | |
1911 | "-e", "sched:sched_stat_wait:r", | |
1912 | "-e", "sched:sched_stat_sleep:r", | |
1913 | "-e", "sched:sched_stat_iowait:r", | |
ea57c4f5 | 1914 | "-e", "sched:sched_stat_runtime:r", |
1fc35b29 IM |
1915 | "-e", "sched:sched_process_exit:r", |
1916 | "-e", "sched:sched_process_fork:r", | |
1917 | "-e", "sched:sched_wakeup:r", | |
1918 | "-e", "sched:sched_migrate_task:r", | |
1919 | }; | |
1920 | ||
1921 | static int __cmd_record(int argc, const char **argv) | |
1922 | { | |
1923 | unsigned int rec_argc, i, j; | |
1924 | const char **rec_argv; | |
1925 | ||
1926 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
1927 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
1928 | ||
1929 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | |
1930 | rec_argv[i] = strdup(record_args[i]); | |
1931 | ||
1932 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
1933 | rec_argv[i] = argv[j]; | |
1934 | ||
1935 | BUG_ON(i != rec_argc); | |
1936 | ||
1937 | return cmd_record(i, rec_argv, NULL); | |
1938 | } | |
1939 | ||
0a02ad93 IM |
1940 | int cmd_sched(int argc, const char **argv, const char *prefix __used) |
1941 | { | |
1942 | symbol__init(); | |
0a02ad93 | 1943 | |
f2858d8a IM |
1944 | argc = parse_options(argc, argv, sched_options, sched_usage, |
1945 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1946 | if (!argc) | |
1947 | usage_with_options(sched_usage, sched_options); | |
0a02ad93 | 1948 | |
1fc35b29 IM |
1949 | if (!strncmp(argv[0], "rec", 3)) { |
1950 | return __cmd_record(argc, argv); | |
1951 | } else if (!strncmp(argv[0], "lat", 3)) { | |
cdce9d73 | 1952 | trace_handler = &lat_ops; |
f2858d8a IM |
1953 | if (argc > 1) { |
1954 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); | |
1955 | if (argc) | |
1956 | usage_with_options(latency_usage, latency_options); | |
f2858d8a | 1957 | } |
b5fae128 | 1958 | setup_sorting(); |
46f392c9 | 1959 | __cmd_lat(); |
0ec04e16 IM |
1960 | } else if (!strcmp(argv[0], "map")) { |
1961 | trace_handler = &map_ops; | |
1962 | setup_sorting(); | |
1963 | __cmd_map(); | |
f2858d8a IM |
1964 | } else if (!strncmp(argv[0], "rep", 3)) { |
1965 | trace_handler = &replay_ops; | |
1966 | if (argc) { | |
1967 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); | |
1968 | if (argc) | |
1969 | usage_with_options(replay_usage, replay_options); | |
1970 | } | |
1971 | __cmd_replay(); | |
c13f0d3c IM |
1972 | } else if (!strcmp(argv[0], "trace")) { |
1973 | /* | |
1974 | * Aliased to 'perf trace' for now: | |
1975 | */ | |
1976 | return cmd_trace(argc, argv, prefix); | |
f2858d8a IM |
1977 | } else { |
1978 | usage_with_options(sched_usage, sched_options); | |
1979 | } | |
1980 | ||
ec156764 | 1981 | return 0; |
0a02ad93 | 1982 | } |