Commit | Line | Data |
---|---|---|
0a02ad93 IM |
1 | #include "builtin.h" |
2 | ||
3 | #include "util/util.h" | |
4 | #include "util/cache.h" | |
5 | #include "util/symbol.h" | |
6 | #include "util/thread.h" | |
7 | #include "util/header.h" | |
8 | ||
9 | #include "util/parse-options.h" | |
10 | ||
11 | #include "perf.h" | |
12 | #include "util/debug.h" | |
13 | ||
14 | #include "util/trace-event.h" | |
ec156764 | 15 | #include <sys/types.h> |
0a02ad93 | 16 | |
419ab0d6 FW |
17 | |
18 | #define MAX_CPUS 4096 | |
19 | ||
ec156764 IM |
20 | static char const *input_name = "perf.data"; |
21 | static int input; | |
22 | static unsigned long page_size; | |
23 | static unsigned long mmap_window = 32; | |
0a02ad93 | 24 | |
ec156764 | 25 | static unsigned long total_comm = 0; |
0a02ad93 | 26 | |
ec156764 IM |
27 | static struct rb_root threads; |
28 | static struct thread *last_match; | |
0a02ad93 | 29 | |
ec156764 IM |
30 | static struct perf_header *header; |
31 | static u64 sample_type; | |
0a02ad93 | 32 | |
419ab0d6 FW |
33 | static int replay_mode; |
34 | ||
0a02ad93 | 35 | |
ec156764 IM |
36 | /* |
37 | * Scheduler benchmarks | |
38 | */ | |
39 | #include <sys/resource.h> | |
40 | #include <sys/types.h> | |
41 | #include <sys/stat.h> | |
42 | #include <sys/time.h> | |
43 | #include <sys/prctl.h> | |
44 | ||
45 | #include <linux/unistd.h> | |
46 | ||
47 | #include <semaphore.h> | |
48 | #include <pthread.h> | |
49 | #include <signal.h> | |
50 | #include <values.h> | |
51 | #include <string.h> | |
52 | #include <unistd.h> | |
53 | #include <stdlib.h> | |
54 | #include <assert.h> | |
55 | #include <fcntl.h> | |
56 | #include <time.h> | |
57 | #include <math.h> | |
58 | ||
59 | #include <stdio.h> | |
60 | ||
61 | #define PR_SET_NAME 15 /* Set process name */ | |
62 | ||
63 | #define BUG_ON(x) assert(!(x)) | |
64 | ||
fbf94829 | 65 | #define DEBUG 0 |
ec156764 IM |
66 | |
67 | typedef unsigned long long nsec_t; | |
68 | ||
ec156764 IM |
69 | static nsec_t run_measurement_overhead; |
70 | static nsec_t sleep_measurement_overhead; | |
71 | ||
72 | static nsec_t get_nsecs(void) | |
73 | { | |
74 | struct timespec ts; | |
75 | ||
76 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
77 | ||
78 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; | |
79 | } | |
80 | ||
81 | static void burn_nsecs(nsec_t nsecs) | |
82 | { | |
83 | nsec_t T0 = get_nsecs(), T1; | |
84 | ||
85 | do { | |
86 | T1 = get_nsecs(); | |
87 | } while (T1 + run_measurement_overhead < T0 + nsecs); | |
88 | } | |
89 | ||
90 | static void sleep_nsecs(nsec_t nsecs) | |
91 | { | |
92 | struct timespec ts; | |
93 | ||
94 | ts.tv_nsec = nsecs % 999999999; | |
95 | ts.tv_sec = nsecs / 999999999; | |
96 | ||
97 | nanosleep(&ts, NULL); | |
98 | } | |
99 | ||
100 | static void calibrate_run_measurement_overhead(void) | |
101 | { | |
102 | nsec_t T0, T1, delta, min_delta = 1000000000ULL; | |
103 | int i; | |
104 | ||
105 | for (i = 0; i < 10; i++) { | |
106 | T0 = get_nsecs(); | |
107 | burn_nsecs(0); | |
108 | T1 = get_nsecs(); | |
109 | delta = T1-T0; | |
110 | min_delta = min(min_delta, delta); | |
111 | } | |
112 | run_measurement_overhead = min_delta; | |
113 | ||
ad236fd2 | 114 | printf("run measurement overhead: %Ld nsecs\n", min_delta); |
ec156764 IM |
115 | } |
116 | ||
117 | static void calibrate_sleep_measurement_overhead(void) | |
118 | { | |
119 | nsec_t T0, T1, delta, min_delta = 1000000000ULL; | |
120 | int i; | |
121 | ||
122 | for (i = 0; i < 10; i++) { | |
123 | T0 = get_nsecs(); | |
124 | sleep_nsecs(10000); | |
125 | T1 = get_nsecs(); | |
126 | delta = T1-T0; | |
127 | min_delta = min(min_delta, delta); | |
128 | } | |
129 | min_delta -= 10000; | |
130 | sleep_measurement_overhead = min_delta; | |
131 | ||
ad236fd2 | 132 | printf("sleep measurement overhead: %Ld nsecs\n", min_delta); |
ec156764 IM |
133 | } |
134 | ||
135 | #define COMM_LEN 20 | |
136 | #define SYM_LEN 129 | |
137 | ||
138 | #define MAX_PID 65536 | |
139 | ||
140 | static unsigned long nr_tasks; | |
141 | ||
142 | struct sched_event; | |
143 | ||
144 | struct task_desc { | |
145 | unsigned long nr; | |
146 | unsigned long pid; | |
147 | char comm[COMM_LEN]; | |
148 | ||
149 | unsigned long nr_events; | |
150 | unsigned long curr_event; | |
151 | struct sched_event **events; | |
152 | ||
153 | pthread_t thread; | |
154 | sem_t sleep_sem; | |
155 | ||
156 | sem_t ready_for_work; | |
157 | sem_t work_done_sem; | |
158 | ||
159 | nsec_t cpu_usage; | |
160 | }; | |
161 | ||
162 | enum sched_event_type { | |
163 | SCHED_EVENT_RUN, | |
164 | SCHED_EVENT_SLEEP, | |
165 | SCHED_EVENT_WAKEUP, | |
166 | }; | |
167 | ||
168 | struct sched_event { | |
169 | enum sched_event_type type; | |
170 | nsec_t timestamp; | |
171 | nsec_t duration; | |
172 | unsigned long nr; | |
173 | int specific_wait; | |
174 | sem_t *wait_sem; | |
175 | struct task_desc *wakee; | |
176 | }; | |
177 | ||
178 | static struct task_desc *pid_to_task[MAX_PID]; | |
179 | ||
180 | static struct task_desc **tasks; | |
181 | ||
182 | static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER; | |
183 | static nsec_t start_time; | |
184 | ||
185 | static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER; | |
186 | ||
187 | static unsigned long nr_run_events; | |
188 | static unsigned long nr_sleep_events; | |
189 | static unsigned long nr_wakeup_events; | |
190 | ||
191 | static unsigned long nr_sleep_corrections; | |
192 | static unsigned long nr_run_events_optimized; | |
193 | ||
194 | static struct sched_event * | |
195 | get_new_event(struct task_desc *task, nsec_t timestamp) | |
196 | { | |
197 | struct sched_event *event = calloc(1, sizeof(*event)); | |
198 | unsigned long idx = task->nr_events; | |
199 | size_t size; | |
200 | ||
201 | event->timestamp = timestamp; | |
202 | event->nr = idx; | |
203 | ||
204 | task->nr_events++; | |
205 | size = sizeof(struct sched_event *) * task->nr_events; | |
206 | task->events = realloc(task->events, size); | |
207 | BUG_ON(!task->events); | |
208 | ||
209 | task->events[idx] = event; | |
210 | ||
211 | return event; | |
212 | } | |
213 | ||
214 | static struct sched_event *last_event(struct task_desc *task) | |
215 | { | |
216 | if (!task->nr_events) | |
217 | return NULL; | |
218 | ||
219 | return task->events[task->nr_events - 1]; | |
220 | } | |
221 | ||
222 | static void | |
fbf94829 | 223 | add_sched_event_run(struct task_desc *task, nsec_t timestamp, u64 duration) |
ec156764 IM |
224 | { |
225 | struct sched_event *event, *curr_event = last_event(task); | |
226 | ||
227 | /* | |
fbf94829 IM |
228 | * optimize an existing RUN event by merging this one |
229 | * to it: | |
230 | */ | |
ec156764 IM |
231 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
232 | nr_run_events_optimized++; | |
233 | curr_event->duration += duration; | |
234 | return; | |
235 | } | |
236 | ||
237 | event = get_new_event(task, timestamp); | |
238 | ||
239 | event->type = SCHED_EVENT_RUN; | |
240 | event->duration = duration; | |
241 | ||
242 | nr_run_events++; | |
243 | } | |
244 | ||
245 | static unsigned long targetless_wakeups; | |
246 | static unsigned long multitarget_wakeups; | |
247 | ||
248 | static void | |
249 | add_sched_event_wakeup(struct task_desc *task, nsec_t timestamp, | |
250 | struct task_desc *wakee) | |
251 | { | |
252 | struct sched_event *event, *wakee_event; | |
253 | ||
254 | event = get_new_event(task, timestamp); | |
255 | event->type = SCHED_EVENT_WAKEUP; | |
256 | event->wakee = wakee; | |
257 | ||
258 | wakee_event = last_event(wakee); | |
259 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { | |
260 | targetless_wakeups++; | |
261 | return; | |
262 | } | |
263 | if (wakee_event->wait_sem) { | |
264 | multitarget_wakeups++; | |
265 | return; | |
266 | } | |
267 | ||
268 | wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem)); | |
269 | sem_init(wakee_event->wait_sem, 0, 0); | |
270 | wakee_event->specific_wait = 1; | |
271 | event->wait_sem = wakee_event->wait_sem; | |
272 | ||
273 | nr_wakeup_events++; | |
274 | } | |
275 | ||
276 | static void | |
277 | add_sched_event_sleep(struct task_desc *task, nsec_t timestamp, | |
ad236fd2 | 278 | u64 task_state __used) |
ec156764 IM |
279 | { |
280 | struct sched_event *event = get_new_event(task, timestamp); | |
281 | ||
282 | event->type = SCHED_EVENT_SLEEP; | |
283 | ||
284 | nr_sleep_events++; | |
285 | } | |
286 | ||
287 | static struct task_desc *register_pid(unsigned long pid, const char *comm) | |
288 | { | |
289 | struct task_desc *task; | |
290 | ||
291 | BUG_ON(pid >= MAX_PID); | |
292 | ||
293 | task = pid_to_task[pid]; | |
294 | ||
295 | if (task) | |
296 | return task; | |
297 | ||
298 | task = calloc(1, sizeof(*task)); | |
299 | task->pid = pid; | |
300 | task->nr = nr_tasks; | |
301 | strcpy(task->comm, comm); | |
302 | /* | |
303 | * every task starts in sleeping state - this gets ignored | |
304 | * if there's no wakeup pointing to this sleep state: | |
305 | */ | |
306 | add_sched_event_sleep(task, 0, 0); | |
307 | ||
308 | pid_to_task[pid] = task; | |
309 | nr_tasks++; | |
310 | tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *)); | |
311 | BUG_ON(!tasks); | |
312 | tasks[task->nr] = task; | |
313 | ||
ad236fd2 IM |
314 | if (verbose) |
315 | printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm); | |
ec156764 IM |
316 | |
317 | return task; | |
318 | } | |
319 | ||
320 | ||
ec156764 IM |
321 | static void print_task_traces(void) |
322 | { | |
323 | struct task_desc *task; | |
324 | unsigned long i; | |
325 | ||
326 | for (i = 0; i < nr_tasks; i++) { | |
327 | task = tasks[i]; | |
ad236fd2 | 328 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
ec156764 IM |
329 | task->nr, task->comm, task->pid, task->nr_events); |
330 | } | |
331 | } | |
332 | ||
333 | static void add_cross_task_wakeups(void) | |
334 | { | |
335 | struct task_desc *task1, *task2; | |
336 | unsigned long i, j; | |
337 | ||
338 | for (i = 0; i < nr_tasks; i++) { | |
339 | task1 = tasks[i]; | |
340 | j = i + 1; | |
341 | if (j == nr_tasks) | |
342 | j = 0; | |
343 | task2 = tasks[j]; | |
344 | add_sched_event_wakeup(task1, 0, task2); | |
345 | } | |
346 | } | |
347 | ||
348 | static void | |
fbf94829 | 349 | process_sched_event(struct task_desc *this_task __used, struct sched_event *event) |
ec156764 IM |
350 | { |
351 | int ret = 0; | |
352 | nsec_t now; | |
353 | long long delta; | |
354 | ||
355 | now = get_nsecs(); | |
356 | delta = start_time + event->timestamp - now; | |
357 | ||
ec156764 IM |
358 | switch (event->type) { |
359 | case SCHED_EVENT_RUN: | |
ec156764 IM |
360 | burn_nsecs(event->duration); |
361 | break; | |
362 | case SCHED_EVENT_SLEEP: | |
ec156764 IM |
363 | if (event->wait_sem) |
364 | ret = sem_wait(event->wait_sem); | |
365 | BUG_ON(ret); | |
366 | break; | |
367 | case SCHED_EVENT_WAKEUP: | |
ec156764 IM |
368 | if (event->wait_sem) |
369 | ret = sem_post(event->wait_sem); | |
370 | BUG_ON(ret); | |
371 | break; | |
372 | default: | |
373 | BUG_ON(1); | |
374 | } | |
375 | } | |
376 | ||
377 | static nsec_t get_cpu_usage_nsec_parent(void) | |
378 | { | |
379 | struct rusage ru; | |
380 | nsec_t sum; | |
381 | int err; | |
382 | ||
383 | err = getrusage(RUSAGE_SELF, &ru); | |
384 | BUG_ON(err); | |
385 | ||
386 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; | |
387 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; | |
388 | ||
389 | return sum; | |
390 | } | |
391 | ||
392 | static nsec_t get_cpu_usage_nsec_self(void) | |
393 | { | |
394 | char filename [] = "/proc/1234567890/sched"; | |
395 | unsigned long msecs, nsecs; | |
396 | char *line = NULL; | |
397 | nsec_t total = 0; | |
398 | size_t len = 0; | |
399 | ssize_t chars; | |
400 | FILE *file; | |
401 | int ret; | |
402 | ||
403 | sprintf(filename, "/proc/%d/sched", getpid()); | |
404 | file = fopen(filename, "r"); | |
405 | BUG_ON(!file); | |
406 | ||
407 | while ((chars = getline(&line, &len, file)) != -1) { | |
ec156764 IM |
408 | ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n", |
409 | &msecs, &nsecs); | |
410 | if (ret == 2) { | |
411 | total = msecs*1e6 + nsecs; | |
ec156764 IM |
412 | break; |
413 | } | |
414 | } | |
415 | if (line) | |
416 | free(line); | |
417 | fclose(file); | |
418 | ||
419 | return total; | |
420 | } | |
421 | ||
422 | static void *thread_func(void *ctx) | |
423 | { | |
424 | struct task_desc *this_task = ctx; | |
425 | nsec_t cpu_usage_0, cpu_usage_1; | |
426 | unsigned long i, ret; | |
427 | char comm2[22]; | |
428 | ||
ec156764 IM |
429 | sprintf(comm2, ":%s", this_task->comm); |
430 | prctl(PR_SET_NAME, comm2); | |
431 | ||
432 | again: | |
433 | ret = sem_post(&this_task->ready_for_work); | |
434 | BUG_ON(ret); | |
ec156764 IM |
435 | ret = pthread_mutex_lock(&start_work_mutex); |
436 | BUG_ON(ret); | |
437 | ret = pthread_mutex_unlock(&start_work_mutex); | |
438 | BUG_ON(ret); | |
ec156764 IM |
439 | |
440 | cpu_usage_0 = get_cpu_usage_nsec_self(); | |
441 | ||
442 | for (i = 0; i < this_task->nr_events; i++) { | |
443 | this_task->curr_event = i; | |
444 | process_sched_event(this_task, this_task->events[i]); | |
445 | } | |
446 | ||
447 | cpu_usage_1 = get_cpu_usage_nsec_self(); | |
448 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; | |
449 | ||
ec156764 IM |
450 | ret = sem_post(&this_task->work_done_sem); |
451 | BUG_ON(ret); | |
ec156764 IM |
452 | |
453 | ret = pthread_mutex_lock(&work_done_wait_mutex); | |
454 | BUG_ON(ret); | |
455 | ret = pthread_mutex_unlock(&work_done_wait_mutex); | |
456 | BUG_ON(ret); | |
ec156764 IM |
457 | |
458 | goto again; | |
459 | } | |
460 | ||
461 | static void create_tasks(void) | |
462 | { | |
463 | struct task_desc *task; | |
464 | pthread_attr_t attr; | |
465 | unsigned long i; | |
466 | int err; | |
467 | ||
468 | err = pthread_attr_init(&attr); | |
469 | BUG_ON(err); | |
470 | err = pthread_attr_setstacksize(&attr, (size_t)(16*1024)); | |
471 | BUG_ON(err); | |
472 | err = pthread_mutex_lock(&start_work_mutex); | |
473 | BUG_ON(err); | |
474 | err = pthread_mutex_lock(&work_done_wait_mutex); | |
475 | BUG_ON(err); | |
476 | for (i = 0; i < nr_tasks; i++) { | |
477 | task = tasks[i]; | |
478 | sem_init(&task->sleep_sem, 0, 0); | |
479 | sem_init(&task->ready_for_work, 0, 0); | |
480 | sem_init(&task->work_done_sem, 0, 0); | |
481 | task->curr_event = 0; | |
482 | err = pthread_create(&task->thread, &attr, thread_func, task); | |
483 | BUG_ON(err); | |
484 | } | |
485 | } | |
486 | ||
487 | static nsec_t cpu_usage; | |
488 | static nsec_t runavg_cpu_usage; | |
489 | static nsec_t parent_cpu_usage; | |
490 | static nsec_t runavg_parent_cpu_usage; | |
491 | ||
492 | static void wait_for_tasks(void) | |
493 | { | |
494 | nsec_t cpu_usage_0, cpu_usage_1; | |
495 | struct task_desc *task; | |
496 | unsigned long i, ret; | |
497 | ||
ec156764 | 498 | start_time = get_nsecs(); |
ec156764 IM |
499 | cpu_usage = 0; |
500 | pthread_mutex_unlock(&work_done_wait_mutex); | |
501 | ||
502 | for (i = 0; i < nr_tasks; i++) { | |
503 | task = tasks[i]; | |
504 | ret = sem_wait(&task->ready_for_work); | |
505 | BUG_ON(ret); | |
506 | sem_init(&task->ready_for_work, 0, 0); | |
507 | } | |
508 | ret = pthread_mutex_lock(&work_done_wait_mutex); | |
509 | BUG_ON(ret); | |
510 | ||
511 | cpu_usage_0 = get_cpu_usage_nsec_parent(); | |
512 | ||
513 | pthread_mutex_unlock(&start_work_mutex); | |
514 | ||
ec156764 IM |
515 | for (i = 0; i < nr_tasks; i++) { |
516 | task = tasks[i]; | |
517 | ret = sem_wait(&task->work_done_sem); | |
518 | BUG_ON(ret); | |
519 | sem_init(&task->work_done_sem, 0, 0); | |
520 | cpu_usage += task->cpu_usage; | |
521 | task->cpu_usage = 0; | |
522 | } | |
523 | ||
524 | cpu_usage_1 = get_cpu_usage_nsec_parent(); | |
525 | if (!runavg_cpu_usage) | |
526 | runavg_cpu_usage = cpu_usage; | |
527 | runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10; | |
528 | ||
529 | parent_cpu_usage = cpu_usage_1 - cpu_usage_0; | |
530 | if (!runavg_parent_cpu_usage) | |
531 | runavg_parent_cpu_usage = parent_cpu_usage; | |
532 | runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 + | |
533 | parent_cpu_usage)/10; | |
534 | ||
535 | ret = pthread_mutex_lock(&start_work_mutex); | |
536 | BUG_ON(ret); | |
537 | ||
538 | for (i = 0; i < nr_tasks; i++) { | |
539 | task = tasks[i]; | |
540 | sem_init(&task->sleep_sem, 0, 0); | |
541 | task->curr_event = 0; | |
542 | } | |
543 | } | |
544 | ||
545 | static int __cmd_sched(void); | |
546 | ||
547 | static void parse_trace(void) | |
548 | { | |
549 | __cmd_sched(); | |
550 | ||
ad236fd2 IM |
551 | printf("nr_run_events: %ld\n", nr_run_events); |
552 | printf("nr_sleep_events: %ld\n", nr_sleep_events); | |
553 | printf("nr_wakeup_events: %ld\n", nr_wakeup_events); | |
ec156764 IM |
554 | |
555 | if (targetless_wakeups) | |
ad236fd2 | 556 | printf("target-less wakeups: %ld\n", targetless_wakeups); |
ec156764 | 557 | if (multitarget_wakeups) |
ad236fd2 | 558 | printf("multi-target wakeups: %ld\n", multitarget_wakeups); |
ec156764 | 559 | if (nr_run_events_optimized) |
ad236fd2 | 560 | printf("run events optimized: %ld\n", |
ec156764 IM |
561 | nr_run_events_optimized); |
562 | } | |
563 | ||
564 | static unsigned long nr_runs; | |
565 | static nsec_t sum_runtime; | |
566 | static nsec_t sum_fluct; | |
567 | static nsec_t run_avg; | |
568 | ||
569 | static void run_one_test(void) | |
570 | { | |
571 | nsec_t T0, T1, delta, avg_delta, fluct, std_dev; | |
572 | ||
573 | T0 = get_nsecs(); | |
574 | wait_for_tasks(); | |
575 | T1 = get_nsecs(); | |
576 | ||
577 | delta = T1 - T0; | |
578 | sum_runtime += delta; | |
579 | nr_runs++; | |
580 | ||
581 | avg_delta = sum_runtime / nr_runs; | |
582 | if (delta < avg_delta) | |
583 | fluct = avg_delta - delta; | |
584 | else | |
585 | fluct = delta - avg_delta; | |
586 | sum_fluct += fluct; | |
587 | std_dev = sum_fluct / nr_runs / sqrt(nr_runs); | |
588 | if (!run_avg) | |
589 | run_avg = delta; | |
590 | run_avg = (run_avg*9 + delta)/10; | |
591 | ||
ad236fd2 | 592 | printf("#%-3ld: %0.3f, ", |
ec156764 IM |
593 | nr_runs, (double)delta/1000000.0); |
594 | ||
595 | #if 0 | |
ad236fd2 | 596 | printf("%0.2f +- %0.2f, ", |
ec156764 IM |
597 | (double)avg_delta/1e6, (double)std_dev/1e6); |
598 | #endif | |
ad236fd2 | 599 | printf("ravg: %0.2f, ", |
ec156764 IM |
600 | (double)run_avg/1e6); |
601 | ||
ad236fd2 | 602 | printf("cpu: %0.2f / %0.2f", |
ec156764 IM |
603 | (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6); |
604 | ||
605 | #if 0 | |
606 | /* | |
fbf94829 IM |
607 | * rusage statistics done by the parent, these are less |
608 | * accurate than the sum_exec_runtime based statistics: | |
609 | */ | |
ad236fd2 | 610 | printf(" [%0.2f / %0.2f]", |
ec156764 IM |
611 | (double)parent_cpu_usage/1e6, |
612 | (double)runavg_parent_cpu_usage/1e6); | |
613 | #endif | |
614 | ||
ad236fd2 | 615 | printf("\n"); |
ec156764 IM |
616 | |
617 | if (nr_sleep_corrections) | |
ad236fd2 | 618 | printf(" (%ld sleep corrections)\n", nr_sleep_corrections); |
ec156764 IM |
619 | nr_sleep_corrections = 0; |
620 | } | |
621 | ||
622 | static void test_calibrations(void) | |
623 | { | |
624 | nsec_t T0, T1; | |
625 | ||
626 | T0 = get_nsecs(); | |
627 | burn_nsecs(1e6); | |
628 | T1 = get_nsecs(); | |
629 | ||
ad236fd2 | 630 | printf("the run test took %Ld nsecs\n", T1-T0); |
ec156764 IM |
631 | |
632 | T0 = get_nsecs(); | |
633 | sleep_nsecs(1e6); | |
634 | T1 = get_nsecs(); | |
635 | ||
ad236fd2 | 636 | printf("the sleep test took %Ld nsecs\n", T1-T0); |
ec156764 IM |
637 | } |
638 | ||
0a02ad93 IM |
639 | static int |
640 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | |
641 | { | |
642 | struct thread *thread; | |
643 | ||
644 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | |
645 | ||
646 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", | |
647 | (void *)(offset + head), | |
648 | (void *)(long)(event->header.size), | |
649 | event->comm.comm, event->comm.pid); | |
650 | ||
651 | if (thread == NULL || | |
652 | thread__set_comm(thread, event->comm.comm)) { | |
653 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); | |
654 | return -1; | |
655 | } | |
656 | total_comm++; | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
46538818 FW |
661 | |
662 | struct raw_event_sample { | |
663 | u32 size; | |
664 | char data[0]; | |
665 | }; | |
666 | ||
667 | #define FILL_FIELD(ptr, field, event, data) \ | |
668 | ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data) | |
669 | ||
670 | #define FILL_ARRAY(ptr, array, event, data) \ | |
671 | do { \ | |
672 | void *__array = raw_field_ptr(event, #array, data); \ | |
673 | memcpy(ptr.array, __array, sizeof(ptr.array)); \ | |
674 | } while(0) | |
675 | ||
676 | #define FILL_COMMON_FIELDS(ptr, event, data) \ | |
677 | do { \ | |
678 | FILL_FIELD(ptr, common_type, event, data); \ | |
679 | FILL_FIELD(ptr, common_flags, event, data); \ | |
680 | FILL_FIELD(ptr, common_preempt_count, event, data); \ | |
681 | FILL_FIELD(ptr, common_pid, event, data); \ | |
682 | FILL_FIELD(ptr, common_tgid, event, data); \ | |
683 | } while (0) | |
684 | ||
419ab0d6 FW |
685 | |
686 | ||
687 | struct trace_switch_event { | |
688 | u32 size; | |
689 | ||
690 | u16 common_type; | |
691 | u8 common_flags; | |
692 | u8 common_preempt_count; | |
693 | u32 common_pid; | |
694 | u32 common_tgid; | |
695 | ||
696 | char prev_comm[16]; | |
697 | u32 prev_pid; | |
698 | u32 prev_prio; | |
699 | u64 prev_state; | |
700 | char next_comm[16]; | |
701 | u32 next_pid; | |
702 | u32 next_prio; | |
703 | }; | |
704 | ||
705 | ||
fbf94829 IM |
706 | struct trace_wakeup_event { |
707 | u32 size; | |
708 | ||
709 | u16 common_type; | |
710 | u8 common_flags; | |
711 | u8 common_preempt_count; | |
712 | u32 common_pid; | |
713 | u32 common_tgid; | |
714 | ||
715 | char comm[16]; | |
716 | u32 pid; | |
717 | ||
718 | u32 prio; | |
719 | u32 success; | |
720 | u32 cpu; | |
721 | }; | |
722 | ||
419ab0d6 FW |
723 | struct trace_fork_event { |
724 | u32 size; | |
46538818 | 725 | |
419ab0d6 FW |
726 | u16 common_type; |
727 | u8 common_flags; | |
728 | u8 common_preempt_count; | |
729 | u32 common_pid; | |
730 | u32 common_tgid; | |
731 | ||
732 | char parent_comm[16]; | |
733 | u32 parent_pid; | |
734 | char child_comm[16]; | |
735 | u32 child_pid; | |
736 | }; | |
737 | ||
738 | struct trace_sched_handler { | |
739 | void (*switch_event)(struct trace_switch_event *, | |
740 | struct event *, | |
741 | int cpu, | |
742 | u64 timestamp, | |
743 | struct thread *thread); | |
744 | ||
745 | void (*wakeup_event)(struct trace_wakeup_event *, | |
746 | struct event *, | |
747 | int cpu, | |
748 | u64 timestamp, | |
749 | struct thread *thread); | |
750 | ||
751 | void (*fork_event)(struct trace_fork_event *, | |
752 | struct event *, | |
753 | int cpu, | |
754 | u64 timestamp, | |
755 | struct thread *thread); | |
756 | }; | |
46538818 | 757 | |
46538818 | 758 | |
419ab0d6 FW |
759 | static void |
760 | replay_wakeup_event(struct trace_wakeup_event *wakeup_event, | |
761 | struct event *event, | |
762 | int cpu __used, | |
763 | u64 timestamp __used, | |
764 | struct thread *thread __used) | |
765 | { | |
766 | struct task_desc *waker, *wakee; | |
fbf94829 | 767 | |
ad236fd2 IM |
768 | if (verbose) { |
769 | printf("sched_wakeup event %p\n", event); | |
fbf94829 | 770 | |
ad236fd2 | 771 | printf(" ... pid %d woke up %s/%d\n", |
419ab0d6 FW |
772 | wakeup_event->common_pid, |
773 | wakeup_event->comm, | |
774 | wakeup_event->pid); | |
ad236fd2 | 775 | } |
fbf94829 | 776 | |
419ab0d6 FW |
777 | waker = register_pid(wakeup_event->common_pid, "<unknown>"); |
778 | wakee = register_pid(wakeup_event->pid, wakeup_event->comm); | |
fbf94829 IM |
779 | |
780 | add_sched_event_wakeup(waker, timestamp, wakee); | |
ec156764 IM |
781 | } |
782 | ||
419ab0d6 | 783 | static unsigned long cpu_last_switched[MAX_CPUS]; |
fbf94829 IM |
784 | |
785 | static void | |
419ab0d6 FW |
786 | replay_switch_event(struct trace_switch_event *switch_event, |
787 | struct event *event, | |
788 | int cpu, | |
789 | u64 timestamp, | |
790 | struct thread *thread __used) | |
ec156764 | 791 | { |
fbf94829 IM |
792 | struct task_desc *prev, *next; |
793 | u64 timestamp0; | |
794 | s64 delta; | |
795 | ||
ad236fd2 IM |
796 | if (verbose) |
797 | printf("sched_switch event %p\n", event); | |
798 | ||
fbf94829 IM |
799 | if (cpu >= MAX_CPUS || cpu < 0) |
800 | return; | |
801 | ||
802 | timestamp0 = cpu_last_switched[cpu]; | |
803 | if (timestamp0) | |
804 | delta = timestamp - timestamp0; | |
805 | else | |
806 | delta = 0; | |
807 | ||
808 | if (delta < 0) | |
809 | die("hm, delta: %Ld < 0 ?\n", delta); | |
810 | ||
ad236fd2 IM |
811 | if (verbose) { |
812 | printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n", | |
419ab0d6 FW |
813 | switch_event->prev_comm, switch_event->prev_pid, |
814 | switch_event->next_comm, switch_event->next_pid, | |
ad236fd2 IM |
815 | delta); |
816 | } | |
fbf94829 | 817 | |
419ab0d6 FW |
818 | prev = register_pid(switch_event->prev_pid, switch_event->prev_comm); |
819 | next = register_pid(switch_event->next_pid, switch_event->next_comm); | |
fbf94829 IM |
820 | |
821 | cpu_last_switched[cpu] = timestamp; | |
822 | ||
823 | add_sched_event_run(prev, timestamp, delta); | |
419ab0d6 | 824 | add_sched_event_sleep(prev, timestamp, switch_event->prev_state); |
fbf94829 IM |
825 | } |
826 | ||
fbf94829 | 827 | |
419ab0d6 FW |
828 | static void |
829 | replay_fork_event(struct trace_fork_event *fork_event, | |
830 | struct event *event, | |
831 | int cpu __used, | |
832 | u64 timestamp __used, | |
833 | struct thread *thread __used) | |
834 | { | |
835 | if (verbose) { | |
836 | printf("sched_fork event %p\n", event); | |
837 | printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid); | |
838 | printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid); | |
839 | } | |
840 | register_pid(fork_event->parent_pid, fork_event->parent_comm); | |
841 | register_pid(fork_event->child_pid, fork_event->child_comm); | |
842 | } | |
fbf94829 | 843 | |
419ab0d6 FW |
844 | static struct trace_sched_handler replay_ops = { |
845 | .wakeup_event = replay_wakeup_event, | |
846 | .switch_event = replay_switch_event, | |
847 | .fork_event = replay_fork_event, | |
fbf94829 IM |
848 | }; |
849 | ||
419ab0d6 FW |
850 | |
851 | static struct trace_sched_handler *trace_handler; | |
852 | ||
fbf94829 | 853 | static void |
419ab0d6 FW |
854 | process_sched_wakeup_event(struct raw_event_sample *raw, |
855 | struct event *event, | |
856 | int cpu __used, | |
857 | u64 timestamp __used, | |
858 | struct thread *thread __used) | |
859 | { | |
860 | struct trace_wakeup_event wakeup_event; | |
861 | ||
862 | FILL_COMMON_FIELDS(wakeup_event, event, raw->data); | |
863 | ||
864 | FILL_ARRAY(wakeup_event, comm, event, raw->data); | |
865 | FILL_FIELD(wakeup_event, pid, event, raw->data); | |
866 | FILL_FIELD(wakeup_event, prio, event, raw->data); | |
867 | FILL_FIELD(wakeup_event, success, event, raw->data); | |
868 | FILL_FIELD(wakeup_event, cpu, event, raw->data); | |
869 | ||
870 | trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread); | |
871 | } | |
872 | ||
873 | static void | |
874 | process_sched_switch_event(struct raw_event_sample *raw, | |
875 | struct event *event, | |
876 | int cpu __used, | |
877 | u64 timestamp __used, | |
878 | struct thread *thread __used) | |
879 | { | |
880 | struct trace_switch_event switch_event; | |
881 | ||
882 | FILL_COMMON_FIELDS(switch_event, event, raw->data); | |
883 | ||
884 | FILL_ARRAY(switch_event, prev_comm, event, raw->data); | |
885 | FILL_FIELD(switch_event, prev_pid, event, raw->data); | |
886 | FILL_FIELD(switch_event, prev_prio, event, raw->data); | |
887 | FILL_FIELD(switch_event, prev_state, event, raw->data); | |
888 | FILL_ARRAY(switch_event, next_comm, event, raw->data); | |
889 | FILL_FIELD(switch_event, next_pid, event, raw->data); | |
890 | FILL_FIELD(switch_event, next_prio, event, raw->data); | |
891 | ||
892 | trace_handler->switch_event(&switch_event, event, cpu, timestamp, thread); | |
893 | } | |
894 | ||
895 | static void | |
896 | process_sched_fork_event(struct raw_event_sample *raw, | |
897 | struct event *event, | |
898 | int cpu __used, | |
899 | u64 timestamp __used, | |
900 | struct thread *thread __used) | |
fbf94829 | 901 | { |
46538818 FW |
902 | struct trace_fork_event fork_event; |
903 | ||
904 | FILL_COMMON_FIELDS(fork_event, event, raw->data); | |
905 | ||
906 | FILL_ARRAY(fork_event, parent_comm, event, raw->data); | |
907 | FILL_FIELD(fork_event, parent_pid, event, raw->data); | |
908 | FILL_ARRAY(fork_event, child_comm, event, raw->data); | |
909 | FILL_FIELD(fork_event, child_pid, event, raw->data); | |
910 | ||
419ab0d6 | 911 | trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread); |
fbf94829 IM |
912 | } |
913 | ||
419ab0d6 FW |
914 | static void |
915 | process_sched_exit_event(struct event *event, | |
916 | int cpu __used, | |
917 | u64 timestamp __used, | |
918 | struct thread *thread __used) | |
fbf94829 | 919 | { |
ad236fd2 IM |
920 | if (verbose) |
921 | printf("sched_exit event %p\n", event); | |
ec156764 IM |
922 | } |
923 | ||
924 | static void | |
ad236fd2 | 925 | process_raw_event(event_t *raw_event __used, void *more_data, |
ec156764 IM |
926 | int cpu, u64 timestamp, struct thread *thread) |
927 | { | |
46538818 | 928 | struct raw_event_sample *raw = more_data; |
ec156764 IM |
929 | struct event *event; |
930 | int type; | |
931 | ||
932 | type = trace_parse_common_type(raw->data); | |
933 | event = trace_find_event(type); | |
934 | ||
ec156764 | 935 | if (!strcmp(event->name, "sched_switch")) |
46538818 | 936 | process_sched_switch_event(raw, event, cpu, timestamp, thread); |
ec156764 | 937 | if (!strcmp(event->name, "sched_wakeup")) |
46538818 | 938 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
fbf94829 | 939 | if (!strcmp(event->name, "sched_wakeup_new")) |
46538818 | 940 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
fbf94829 | 941 | if (!strcmp(event->name, "sched_process_fork")) |
46538818 | 942 | process_sched_fork_event(raw, event, cpu, timestamp, thread); |
fbf94829 IM |
943 | if (!strcmp(event->name, "sched_process_exit")) |
944 | process_sched_exit_event(event, cpu, timestamp, thread); | |
ec156764 IM |
945 | } |
946 | ||
0a02ad93 IM |
947 | static int |
948 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |
949 | { | |
950 | char level; | |
951 | int show = 0; | |
952 | struct dso *dso = NULL; | |
953 | struct thread *thread; | |
954 | u64 ip = event->ip.ip; | |
955 | u64 timestamp = -1; | |
956 | u32 cpu = -1; | |
957 | u64 period = 1; | |
958 | void *more_data = event->ip.__more_data; | |
959 | int cpumode; | |
960 | ||
961 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | |
962 | ||
963 | if (sample_type & PERF_SAMPLE_TIME) { | |
964 | timestamp = *(u64 *)more_data; | |
965 | more_data += sizeof(u64); | |
966 | } | |
967 | ||
968 | if (sample_type & PERF_SAMPLE_CPU) { | |
969 | cpu = *(u32 *)more_data; | |
970 | more_data += sizeof(u32); | |
971 | more_data += sizeof(u32); /* reserved */ | |
972 | } | |
973 | ||
974 | if (sample_type & PERF_SAMPLE_PERIOD) { | |
975 | period = *(u64 *)more_data; | |
976 | more_data += sizeof(u64); | |
977 | } | |
978 | ||
979 | dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", | |
980 | (void *)(offset + head), | |
981 | (void *)(long)(event->header.size), | |
982 | event->header.misc, | |
983 | event->ip.pid, event->ip.tid, | |
984 | (void *)(long)ip, | |
985 | (long long)period); | |
986 | ||
987 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | |
988 | ||
989 | if (thread == NULL) { | |
990 | eprintf("problem processing %d event, skipping it.\n", | |
991 | event->header.type); | |
992 | return -1; | |
993 | } | |
994 | ||
995 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; | |
996 | ||
997 | if (cpumode == PERF_EVENT_MISC_KERNEL) { | |
998 | show = SHOW_KERNEL; | |
999 | level = 'k'; | |
1000 | ||
1001 | dso = kernel_dso; | |
1002 | ||
1003 | dump_printf(" ...... dso: %s\n", dso->name); | |
1004 | ||
1005 | } else if (cpumode == PERF_EVENT_MISC_USER) { | |
1006 | ||
1007 | show = SHOW_USER; | |
1008 | level = '.'; | |
1009 | ||
1010 | } else { | |
1011 | show = SHOW_HV; | |
1012 | level = 'H'; | |
1013 | ||
1014 | dso = hypervisor_dso; | |
1015 | ||
1016 | dump_printf(" ...... dso: [hypervisor]\n"); | |
1017 | } | |
1018 | ||
ec156764 IM |
1019 | if (sample_type & PERF_SAMPLE_RAW) |
1020 | process_raw_event(event, more_data, cpu, timestamp, thread); | |
0a02ad93 IM |
1021 | |
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | static int | |
1026 | process_event(event_t *event, unsigned long offset, unsigned long head) | |
1027 | { | |
1028 | trace_event(event); | |
1029 | ||
1030 | switch (event->header.type) { | |
1031 | case PERF_EVENT_MMAP ... PERF_EVENT_LOST: | |
1032 | return 0; | |
1033 | ||
1034 | case PERF_EVENT_COMM: | |
1035 | return process_comm_event(event, offset, head); | |
1036 | ||
1037 | case PERF_EVENT_EXIT ... PERF_EVENT_READ: | |
1038 | return 0; | |
1039 | ||
1040 | case PERF_EVENT_SAMPLE: | |
1041 | return process_sample_event(event, offset, head); | |
1042 | ||
1043 | case PERF_EVENT_MAX: | |
1044 | default: | |
1045 | return -1; | |
1046 | } | |
1047 | ||
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | static int __cmd_sched(void) | |
1052 | { | |
1053 | int ret, rc = EXIT_FAILURE; | |
1054 | unsigned long offset = 0; | |
1055 | unsigned long head = 0; | |
1056 | struct stat perf_stat; | |
1057 | event_t *event; | |
1058 | uint32_t size; | |
1059 | char *buf; | |
1060 | ||
1061 | trace_report(); | |
1062 | register_idle_thread(&threads, &last_match); | |
1063 | ||
1064 | input = open(input_name, O_RDONLY); | |
1065 | if (input < 0) { | |
1066 | perror("failed to open file"); | |
1067 | exit(-1); | |
1068 | } | |
1069 | ||
1070 | ret = fstat(input, &perf_stat); | |
1071 | if (ret < 0) { | |
1072 | perror("failed to stat file"); | |
1073 | exit(-1); | |
1074 | } | |
1075 | ||
1076 | if (!perf_stat.st_size) { | |
1077 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
1078 | exit(0); | |
1079 | } | |
1080 | header = perf_header__read(input); | |
1081 | head = header->data_offset; | |
1082 | sample_type = perf_header__sample_type(header); | |
1083 | ||
1084 | if (!(sample_type & PERF_SAMPLE_RAW)) | |
1085 | die("No trace sample to read. Did you call perf record " | |
1086 | "without -R?"); | |
1087 | ||
1088 | if (load_kernel() < 0) { | |
1089 | perror("failed to load kernel symbols"); | |
1090 | return EXIT_FAILURE; | |
1091 | } | |
1092 | ||
1093 | remap: | |
1094 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | |
1095 | MAP_SHARED, input, offset); | |
1096 | if (buf == MAP_FAILED) { | |
1097 | perror("failed to mmap file"); | |
1098 | exit(-1); | |
1099 | } | |
1100 | ||
1101 | more: | |
1102 | event = (event_t *)(buf + head); | |
1103 | ||
1104 | size = event->header.size; | |
1105 | if (!size) | |
1106 | size = 8; | |
1107 | ||
1108 | if (head + event->header.size >= page_size * mmap_window) { | |
1109 | unsigned long shift = page_size * (head / page_size); | |
1110 | int res; | |
1111 | ||
1112 | res = munmap(buf, page_size * mmap_window); | |
1113 | assert(res == 0); | |
1114 | ||
1115 | offset += shift; | |
1116 | head -= shift; | |
1117 | goto remap; | |
1118 | } | |
1119 | ||
1120 | size = event->header.size; | |
1121 | ||
1122 | ||
1123 | if (!size || process_event(event, offset, head) < 0) { | |
1124 | ||
1125 | /* | |
1126 | * assume we lost track of the stream, check alignment, and | |
1127 | * increment a single u64 in the hope to catch on again 'soon'. | |
1128 | */ | |
1129 | ||
1130 | if (unlikely(head & 7)) | |
1131 | head &= ~7ULL; | |
1132 | ||
1133 | size = 8; | |
1134 | } | |
1135 | ||
1136 | head += size; | |
1137 | ||
1138 | if (offset + head < (unsigned long)perf_stat.st_size) | |
1139 | goto more; | |
1140 | ||
1141 | rc = EXIT_SUCCESS; | |
1142 | close(input); | |
1143 | ||
1144 | return rc; | |
1145 | } | |
1146 | ||
1147 | static const char * const annotate_usage[] = { | |
1148 | "perf trace [<options>] <command>", | |
1149 | NULL | |
1150 | }; | |
1151 | ||
1152 | static const struct option options[] = { | |
1153 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
1154 | "dump raw trace in ASCII"), | |
419ab0d6 FW |
1155 | OPT_BOOLEAN('r', "replay", &replay_mode, |
1156 | "replay sched behaviour from traces"), | |
0a02ad93 IM |
1157 | OPT_BOOLEAN('v', "verbose", &verbose, |
1158 | "be more verbose (show symbol address, etc)"), | |
1159 | OPT_END() | |
1160 | }; | |
1161 | ||
1162 | int cmd_sched(int argc, const char **argv, const char *prefix __used) | |
1163 | { | |
fbf94829 | 1164 | long nr_iterations = 10, i; |
ec156764 | 1165 | |
0a02ad93 IM |
1166 | symbol__init(); |
1167 | page_size = getpagesize(); | |
1168 | ||
1169 | argc = parse_options(argc, argv, options, annotate_usage, 0); | |
1170 | if (argc) { | |
1171 | /* | |
1172 | * Special case: if there's an argument left then assume tha | |
1173 | * it's a symbol filter: | |
1174 | */ | |
1175 | if (argc > 1) | |
1176 | usage_with_options(annotate_usage, options); | |
1177 | } | |
1178 | ||
fbf94829 | 1179 | // setup_pager(); |
0a02ad93 | 1180 | |
419ab0d6 FW |
1181 | if (replay_mode) |
1182 | trace_handler = &replay_ops; | |
1183 | else /* We may need a default subcommand */ | |
1184 | die("Please select a sub command (-r)\n"); | |
1185 | ||
ec156764 IM |
1186 | calibrate_run_measurement_overhead(); |
1187 | calibrate_sleep_measurement_overhead(); | |
1188 | ||
1189 | test_calibrations(); | |
1190 | ||
1191 | parse_trace(); | |
1192 | print_task_traces(); | |
1193 | add_cross_task_wakeups(); | |
1194 | ||
1195 | create_tasks(); | |
ad236fd2 | 1196 | printf("------------------------------------------------------------\n"); |
ec156764 IM |
1197 | for (i = 0; i < nr_iterations; i++) |
1198 | run_one_test(); | |
1199 | ||
1200 | return 0; | |
0a02ad93 | 1201 | } |