blob: de93a2604528ade9b0df91c16e0ed0c9861a4818 [file] [log] [blame]
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001#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"
Ingo Molnarec156762009-09-11 12:12:54 +020015#include <sys/types.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020016
Ingo Molnarec156762009-09-11 12:12:54 +020017static char const *input_name = "perf.data";
18static int input;
19static unsigned long page_size;
20static unsigned long mmap_window = 32;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021
Ingo Molnarec156762009-09-11 12:12:54 +020022static unsigned long total_comm = 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020023
Ingo Molnarec156762009-09-11 12:12:54 +020024static struct rb_root threads;
25static struct thread *last_match;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020026
Ingo Molnarec156762009-09-11 12:12:54 +020027static struct perf_header *header;
28static u64 sample_type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020029
30
Ingo Molnarec156762009-09-11 12:12:54 +020031/*
32 * Scheduler benchmarks
33 */
34#include <sys/resource.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/time.h>
38#include <sys/prctl.h>
39
40#include <linux/unistd.h>
41
42#include <semaphore.h>
43#include <pthread.h>
44#include <signal.h>
45#include <values.h>
46#include <string.h>
47#include <unistd.h>
48#include <stdlib.h>
49#include <assert.h>
50#include <fcntl.h>
51#include <time.h>
52#include <math.h>
53
54#include <stdio.h>
55
56#define PR_SET_NAME 15 /* Set process name */
57
58#define BUG_ON(x) assert(!(x))
59
Ingo Molnarfbf94822009-09-11 12:12:54 +020060#define DEBUG 0
Ingo Molnarec156762009-09-11 12:12:54 +020061
62typedef unsigned long long nsec_t;
63
Ingo Molnarec156762009-09-11 12:12:54 +020064static nsec_t run_measurement_overhead;
65static nsec_t sleep_measurement_overhead;
66
67static nsec_t get_nsecs(void)
68{
69 struct timespec ts;
70
71 clock_gettime(CLOCK_MONOTONIC, &ts);
72
73 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
74}
75
76static void burn_nsecs(nsec_t nsecs)
77{
78 nsec_t T0 = get_nsecs(), T1;
79
80 do {
81 T1 = get_nsecs();
82 } while (T1 + run_measurement_overhead < T0 + nsecs);
83}
84
85static void sleep_nsecs(nsec_t nsecs)
86{
87 struct timespec ts;
88
89 ts.tv_nsec = nsecs % 999999999;
90 ts.tv_sec = nsecs / 999999999;
91
92 nanosleep(&ts, NULL);
93}
94
95static void calibrate_run_measurement_overhead(void)
96{
97 nsec_t T0, T1, delta, min_delta = 1000000000ULL;
98 int i;
99
100 for (i = 0; i < 10; i++) {
101 T0 = get_nsecs();
102 burn_nsecs(0);
103 T1 = get_nsecs();
104 delta = T1-T0;
105 min_delta = min(min_delta, delta);
106 }
107 run_measurement_overhead = min_delta;
108
Ingo Molnarad236fd2009-09-11 12:12:54 +0200109 printf("run measurement overhead: %Ld nsecs\n", min_delta);
Ingo Molnarec156762009-09-11 12:12:54 +0200110}
111
112static void calibrate_sleep_measurement_overhead(void)
113{
114 nsec_t T0, T1, delta, min_delta = 1000000000ULL;
115 int i;
116
117 for (i = 0; i < 10; i++) {
118 T0 = get_nsecs();
119 sleep_nsecs(10000);
120 T1 = get_nsecs();
121 delta = T1-T0;
122 min_delta = min(min_delta, delta);
123 }
124 min_delta -= 10000;
125 sleep_measurement_overhead = min_delta;
126
Ingo Molnarad236fd2009-09-11 12:12:54 +0200127 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
Ingo Molnarec156762009-09-11 12:12:54 +0200128}
129
130#define COMM_LEN 20
131#define SYM_LEN 129
132
133#define MAX_PID 65536
134
135static unsigned long nr_tasks;
136
137struct sched_event;
138
139struct task_desc {
140 unsigned long nr;
141 unsigned long pid;
142 char comm[COMM_LEN];
143
144 unsigned long nr_events;
145 unsigned long curr_event;
146 struct sched_event **events;
147
148 pthread_t thread;
149 sem_t sleep_sem;
150
151 sem_t ready_for_work;
152 sem_t work_done_sem;
153
154 nsec_t cpu_usage;
155};
156
157enum sched_event_type {
158 SCHED_EVENT_RUN,
159 SCHED_EVENT_SLEEP,
160 SCHED_EVENT_WAKEUP,
161};
162
163struct sched_event {
164 enum sched_event_type type;
165 nsec_t timestamp;
166 nsec_t duration;
167 unsigned long nr;
168 int specific_wait;
169 sem_t *wait_sem;
170 struct task_desc *wakee;
171};
172
173static struct task_desc *pid_to_task[MAX_PID];
174
175static struct task_desc **tasks;
176
177static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
178static nsec_t start_time;
179
180static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
181
182static unsigned long nr_run_events;
183static unsigned long nr_sleep_events;
184static unsigned long nr_wakeup_events;
185
186static unsigned long nr_sleep_corrections;
187static unsigned long nr_run_events_optimized;
188
189static struct sched_event *
190get_new_event(struct task_desc *task, nsec_t timestamp)
191{
192 struct sched_event *event = calloc(1, sizeof(*event));
193 unsigned long idx = task->nr_events;
194 size_t size;
195
196 event->timestamp = timestamp;
197 event->nr = idx;
198
199 task->nr_events++;
200 size = sizeof(struct sched_event *) * task->nr_events;
201 task->events = realloc(task->events, size);
202 BUG_ON(!task->events);
203
204 task->events[idx] = event;
205
206 return event;
207}
208
209static struct sched_event *last_event(struct task_desc *task)
210{
211 if (!task->nr_events)
212 return NULL;
213
214 return task->events[task->nr_events - 1];
215}
216
217static void
Ingo Molnarfbf94822009-09-11 12:12:54 +0200218add_sched_event_run(struct task_desc *task, nsec_t timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200219{
220 struct sched_event *event, *curr_event = last_event(task);
221
222 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200223 * optimize an existing RUN event by merging this one
224 * to it:
225 */
Ingo Molnarec156762009-09-11 12:12:54 +0200226 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
227 nr_run_events_optimized++;
228 curr_event->duration += duration;
229 return;
230 }
231
232 event = get_new_event(task, timestamp);
233
234 event->type = SCHED_EVENT_RUN;
235 event->duration = duration;
236
237 nr_run_events++;
238}
239
240static unsigned long targetless_wakeups;
241static unsigned long multitarget_wakeups;
242
243static void
244add_sched_event_wakeup(struct task_desc *task, nsec_t timestamp,
245 struct task_desc *wakee)
246{
247 struct sched_event *event, *wakee_event;
248
249 event = get_new_event(task, timestamp);
250 event->type = SCHED_EVENT_WAKEUP;
251 event->wakee = wakee;
252
253 wakee_event = last_event(wakee);
254 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
255 targetless_wakeups++;
256 return;
257 }
258 if (wakee_event->wait_sem) {
259 multitarget_wakeups++;
260 return;
261 }
262
263 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
264 sem_init(wakee_event->wait_sem, 0, 0);
265 wakee_event->specific_wait = 1;
266 event->wait_sem = wakee_event->wait_sem;
267
268 nr_wakeup_events++;
269}
270
271static void
272add_sched_event_sleep(struct task_desc *task, nsec_t timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200273 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200274{
275 struct sched_event *event = get_new_event(task, timestamp);
276
277 event->type = SCHED_EVENT_SLEEP;
278
279 nr_sleep_events++;
280}
281
282static struct task_desc *register_pid(unsigned long pid, const char *comm)
283{
284 struct task_desc *task;
285
286 BUG_ON(pid >= MAX_PID);
287
288 task = pid_to_task[pid];
289
290 if (task)
291 return task;
292
293 task = calloc(1, sizeof(*task));
294 task->pid = pid;
295 task->nr = nr_tasks;
296 strcpy(task->comm, comm);
297 /*
298 * every task starts in sleeping state - this gets ignored
299 * if there's no wakeup pointing to this sleep state:
300 */
301 add_sched_event_sleep(task, 0, 0);
302
303 pid_to_task[pid] = task;
304 nr_tasks++;
305 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
306 BUG_ON(!tasks);
307 tasks[task->nr] = task;
308
Ingo Molnarad236fd2009-09-11 12:12:54 +0200309 if (verbose)
310 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200311
312 return task;
313}
314
315
Ingo Molnarec156762009-09-11 12:12:54 +0200316static void print_task_traces(void)
317{
318 struct task_desc *task;
319 unsigned long i;
320
321 for (i = 0; i < nr_tasks; i++) {
322 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200323 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200324 task->nr, task->comm, task->pid, task->nr_events);
325 }
326}
327
328static void add_cross_task_wakeups(void)
329{
330 struct task_desc *task1, *task2;
331 unsigned long i, j;
332
333 for (i = 0; i < nr_tasks; i++) {
334 task1 = tasks[i];
335 j = i + 1;
336 if (j == nr_tasks)
337 j = 0;
338 task2 = tasks[j];
339 add_sched_event_wakeup(task1, 0, task2);
340 }
341}
342
343static void
Ingo Molnarfbf94822009-09-11 12:12:54 +0200344process_sched_event(struct task_desc *this_task __used, struct sched_event *event)
Ingo Molnarec156762009-09-11 12:12:54 +0200345{
346 int ret = 0;
347 nsec_t now;
348 long long delta;
349
350 now = get_nsecs();
351 delta = start_time + event->timestamp - now;
352
Ingo Molnarec156762009-09-11 12:12:54 +0200353 switch (event->type) {
354 case SCHED_EVENT_RUN:
Ingo Molnarec156762009-09-11 12:12:54 +0200355 burn_nsecs(event->duration);
356 break;
357 case SCHED_EVENT_SLEEP:
Ingo Molnarec156762009-09-11 12:12:54 +0200358 if (event->wait_sem)
359 ret = sem_wait(event->wait_sem);
360 BUG_ON(ret);
361 break;
362 case SCHED_EVENT_WAKEUP:
Ingo Molnarec156762009-09-11 12:12:54 +0200363 if (event->wait_sem)
364 ret = sem_post(event->wait_sem);
365 BUG_ON(ret);
366 break;
367 default:
368 BUG_ON(1);
369 }
370}
371
372static nsec_t get_cpu_usage_nsec_parent(void)
373{
374 struct rusage ru;
375 nsec_t sum;
376 int err;
377
378 err = getrusage(RUSAGE_SELF, &ru);
379 BUG_ON(err);
380
381 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
382 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
383
384 return sum;
385}
386
387static nsec_t get_cpu_usage_nsec_self(void)
388{
389 char filename [] = "/proc/1234567890/sched";
390 unsigned long msecs, nsecs;
391 char *line = NULL;
392 nsec_t total = 0;
393 size_t len = 0;
394 ssize_t chars;
395 FILE *file;
396 int ret;
397
398 sprintf(filename, "/proc/%d/sched", getpid());
399 file = fopen(filename, "r");
400 BUG_ON(!file);
401
402 while ((chars = getline(&line, &len, file)) != -1) {
Ingo Molnarec156762009-09-11 12:12:54 +0200403 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
404 &msecs, &nsecs);
405 if (ret == 2) {
406 total = msecs*1e6 + nsecs;
Ingo Molnarec156762009-09-11 12:12:54 +0200407 break;
408 }
409 }
410 if (line)
411 free(line);
412 fclose(file);
413
414 return total;
415}
416
417static void *thread_func(void *ctx)
418{
419 struct task_desc *this_task = ctx;
420 nsec_t cpu_usage_0, cpu_usage_1;
421 unsigned long i, ret;
422 char comm2[22];
423
Ingo Molnarec156762009-09-11 12:12:54 +0200424 sprintf(comm2, ":%s", this_task->comm);
425 prctl(PR_SET_NAME, comm2);
426
427again:
428 ret = sem_post(&this_task->ready_for_work);
429 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200430 ret = pthread_mutex_lock(&start_work_mutex);
431 BUG_ON(ret);
432 ret = pthread_mutex_unlock(&start_work_mutex);
433 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200434
435 cpu_usage_0 = get_cpu_usage_nsec_self();
436
437 for (i = 0; i < this_task->nr_events; i++) {
438 this_task->curr_event = i;
439 process_sched_event(this_task, this_task->events[i]);
440 }
441
442 cpu_usage_1 = get_cpu_usage_nsec_self();
443 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
444
Ingo Molnarec156762009-09-11 12:12:54 +0200445 ret = sem_post(&this_task->work_done_sem);
446 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200447
448 ret = pthread_mutex_lock(&work_done_wait_mutex);
449 BUG_ON(ret);
450 ret = pthread_mutex_unlock(&work_done_wait_mutex);
451 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200452
453 goto again;
454}
455
456static void create_tasks(void)
457{
458 struct task_desc *task;
459 pthread_attr_t attr;
460 unsigned long i;
461 int err;
462
463 err = pthread_attr_init(&attr);
464 BUG_ON(err);
465 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
466 BUG_ON(err);
467 err = pthread_mutex_lock(&start_work_mutex);
468 BUG_ON(err);
469 err = pthread_mutex_lock(&work_done_wait_mutex);
470 BUG_ON(err);
471 for (i = 0; i < nr_tasks; i++) {
472 task = tasks[i];
473 sem_init(&task->sleep_sem, 0, 0);
474 sem_init(&task->ready_for_work, 0, 0);
475 sem_init(&task->work_done_sem, 0, 0);
476 task->curr_event = 0;
477 err = pthread_create(&task->thread, &attr, thread_func, task);
478 BUG_ON(err);
479 }
480}
481
482static nsec_t cpu_usage;
483static nsec_t runavg_cpu_usage;
484static nsec_t parent_cpu_usage;
485static nsec_t runavg_parent_cpu_usage;
486
487static void wait_for_tasks(void)
488{
489 nsec_t cpu_usage_0, cpu_usage_1;
490 struct task_desc *task;
491 unsigned long i, ret;
492
Ingo Molnarec156762009-09-11 12:12:54 +0200493 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200494 cpu_usage = 0;
495 pthread_mutex_unlock(&work_done_wait_mutex);
496
497 for (i = 0; i < nr_tasks; i++) {
498 task = tasks[i];
499 ret = sem_wait(&task->ready_for_work);
500 BUG_ON(ret);
501 sem_init(&task->ready_for_work, 0, 0);
502 }
503 ret = pthread_mutex_lock(&work_done_wait_mutex);
504 BUG_ON(ret);
505
506 cpu_usage_0 = get_cpu_usage_nsec_parent();
507
508 pthread_mutex_unlock(&start_work_mutex);
509
Ingo Molnarec156762009-09-11 12:12:54 +0200510 for (i = 0; i < nr_tasks; i++) {
511 task = tasks[i];
512 ret = sem_wait(&task->work_done_sem);
513 BUG_ON(ret);
514 sem_init(&task->work_done_sem, 0, 0);
515 cpu_usage += task->cpu_usage;
516 task->cpu_usage = 0;
517 }
518
519 cpu_usage_1 = get_cpu_usage_nsec_parent();
520 if (!runavg_cpu_usage)
521 runavg_cpu_usage = cpu_usage;
522 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
523
524 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
525 if (!runavg_parent_cpu_usage)
526 runavg_parent_cpu_usage = parent_cpu_usage;
527 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
528 parent_cpu_usage)/10;
529
530 ret = pthread_mutex_lock(&start_work_mutex);
531 BUG_ON(ret);
532
533 for (i = 0; i < nr_tasks; i++) {
534 task = tasks[i];
535 sem_init(&task->sleep_sem, 0, 0);
536 task->curr_event = 0;
537 }
538}
539
540static int __cmd_sched(void);
541
542static void parse_trace(void)
543{
544 __cmd_sched();
545
Ingo Molnarad236fd2009-09-11 12:12:54 +0200546 printf("nr_run_events: %ld\n", nr_run_events);
547 printf("nr_sleep_events: %ld\n", nr_sleep_events);
548 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
Ingo Molnarec156762009-09-11 12:12:54 +0200549
550 if (targetless_wakeups)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200551 printf("target-less wakeups: %ld\n", targetless_wakeups);
Ingo Molnarec156762009-09-11 12:12:54 +0200552 if (multitarget_wakeups)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200553 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
Ingo Molnarec156762009-09-11 12:12:54 +0200554 if (nr_run_events_optimized)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200555 printf("run events optimized: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200556 nr_run_events_optimized);
557}
558
559static unsigned long nr_runs;
560static nsec_t sum_runtime;
561static nsec_t sum_fluct;
562static nsec_t run_avg;
563
564static void run_one_test(void)
565{
566 nsec_t T0, T1, delta, avg_delta, fluct, std_dev;
567
568 T0 = get_nsecs();
569 wait_for_tasks();
570 T1 = get_nsecs();
571
572 delta = T1 - T0;
573 sum_runtime += delta;
574 nr_runs++;
575
576 avg_delta = sum_runtime / nr_runs;
577 if (delta < avg_delta)
578 fluct = avg_delta - delta;
579 else
580 fluct = delta - avg_delta;
581 sum_fluct += fluct;
582 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
583 if (!run_avg)
584 run_avg = delta;
585 run_avg = (run_avg*9 + delta)/10;
586
Ingo Molnarad236fd2009-09-11 12:12:54 +0200587 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200588 nr_runs, (double)delta/1000000.0);
589
590#if 0
Ingo Molnarad236fd2009-09-11 12:12:54 +0200591 printf("%0.2f +- %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200592 (double)avg_delta/1e6, (double)std_dev/1e6);
593#endif
Ingo Molnarad236fd2009-09-11 12:12:54 +0200594 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200595 (double)run_avg/1e6);
596
Ingo Molnarad236fd2009-09-11 12:12:54 +0200597 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200598 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
599
600#if 0
601 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200602 * rusage statistics done by the parent, these are less
603 * accurate than the sum_exec_runtime based statistics:
604 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200605 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200606 (double)parent_cpu_usage/1e6,
607 (double)runavg_parent_cpu_usage/1e6);
608#endif
609
Ingo Molnarad236fd2009-09-11 12:12:54 +0200610 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200611
612 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200613 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200614 nr_sleep_corrections = 0;
615}
616
617static void test_calibrations(void)
618{
619 nsec_t T0, T1;
620
621 T0 = get_nsecs();
622 burn_nsecs(1e6);
623 T1 = get_nsecs();
624
Ingo Molnarad236fd2009-09-11 12:12:54 +0200625 printf("the run test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200626
627 T0 = get_nsecs();
628 sleep_nsecs(1e6);
629 T1 = get_nsecs();
630
Ingo Molnarad236fd2009-09-11 12:12:54 +0200631 printf("the sleep test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200632}
633
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200634static int
635process_comm_event(event_t *event, unsigned long offset, unsigned long head)
636{
637 struct thread *thread;
638
639 thread = threads__findnew(event->comm.pid, &threads, &last_match);
640
641 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
642 (void *)(offset + head),
643 (void *)(long)(event->header.size),
644 event->comm.comm, event->comm.pid);
645
646 if (thread == NULL ||
647 thread__set_comm(thread, event->comm.comm)) {
648 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
649 return -1;
650 }
651 total_comm++;
652
653 return 0;
654}
655
Ingo Molnarfbf94822009-09-11 12:12:54 +0200656struct trace_wakeup_event {
657 u32 size;
658
659 u16 common_type;
660 u8 common_flags;
661 u8 common_preempt_count;
662 u32 common_pid;
663 u32 common_tgid;
664
665 char comm[16];
666 u32 pid;
667
668 u32 prio;
669 u32 success;
670 u32 cpu;
671};
672
673static void
674process_sched_wakeup_event(struct trace_wakeup_event *wakeup_event, struct event *event,
Ingo Molnarec156762009-09-11 12:12:54 +0200675 int cpu __used, u64 timestamp __used, struct thread *thread __used)
676{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200677 struct task_desc *waker, *wakee;
678
Ingo Molnarad236fd2009-09-11 12:12:54 +0200679 if (verbose) {
680 printf("sched_wakeup event %p\n", event);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200681
Ingo Molnarad236fd2009-09-11 12:12:54 +0200682 printf(" ... pid %d woke up %s/%d\n",
683 wakeup_event->common_pid,
684 wakeup_event->comm,
685 wakeup_event->pid);
686 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200687
688 waker = register_pid(wakeup_event->common_pid, "<unknown>");
689 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
690
691 add_sched_event_wakeup(waker, timestamp, wakee);
Ingo Molnarec156762009-09-11 12:12:54 +0200692}
693
Ingo Molnarfbf94822009-09-11 12:12:54 +0200694struct trace_switch_event {
695 u32 size;
696
697 u16 common_type;
698 u8 common_flags;
699 u8 common_preempt_count;
700 u32 common_pid;
701 u32 common_tgid;
702
703 char prev_comm[16];
704 u32 prev_pid;
705 u32 prev_prio;
706 u64 prev_state;
707 char next_comm[16];
708 u32 next_pid;
709 u32 next_prio;
710};
711
712#define MAX_CPUS 4096
713
714unsigned long cpu_last_switched[MAX_CPUS];
715
716static void
717process_sched_switch_event(struct trace_switch_event *switch_event, struct event *event,
Ingo Molnarec156762009-09-11 12:12:54 +0200718 int cpu __used, u64 timestamp __used, struct thread *thread __used)
719{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200720 struct task_desc *prev, *next;
721 u64 timestamp0;
722 s64 delta;
723
Ingo Molnarad236fd2009-09-11 12:12:54 +0200724 if (verbose)
725 printf("sched_switch event %p\n", event);
726
Ingo Molnarfbf94822009-09-11 12:12:54 +0200727 if (cpu >= MAX_CPUS || cpu < 0)
728 return;
729
730 timestamp0 = cpu_last_switched[cpu];
731 if (timestamp0)
732 delta = timestamp - timestamp0;
733 else
734 delta = 0;
735
736 if (delta < 0)
737 die("hm, delta: %Ld < 0 ?\n", delta);
738
Ingo Molnarad236fd2009-09-11 12:12:54 +0200739 if (verbose) {
740 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
741 switch_event->prev_comm, switch_event->prev_pid,
742 switch_event->next_comm, switch_event->next_pid,
743 delta);
744 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200745
746 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
747 next = register_pid(switch_event->next_pid, switch_event->next_comm);
748
749 cpu_last_switched[cpu] = timestamp;
750
751 add_sched_event_run(prev, timestamp, delta);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200752 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200753}
754
755struct trace_fork_event {
756 u32 size;
757
758 u16 common_type;
759 u8 common_flags;
760 u8 common_preempt_count;
761 u32 common_pid;
762 u32 common_tgid;
763
764 char parent_comm[16];
765 u32 parent_pid;
766 char child_comm[16];
767 u32 child_pid;
768};
769
770static void
771process_sched_fork_event(struct trace_fork_event *fork_event, struct event *event,
772 int cpu __used, u64 timestamp __used, struct thread *thread __used)
773{
Ingo Molnarad236fd2009-09-11 12:12:54 +0200774 if (verbose) {
775 printf("sched_fork event %p\n", event);
776 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
777 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
778 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200779 register_pid(fork_event->parent_pid, fork_event->parent_comm);
780 register_pid(fork_event->child_pid, fork_event->child_comm);
781}
782
783static void process_sched_exit_event(struct event *event,
784 int cpu __used, u64 timestamp __used, struct thread *thread __used)
785{
Ingo Molnarad236fd2009-09-11 12:12:54 +0200786 if (verbose)
787 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +0200788}
789
790static void
Ingo Molnarad236fd2009-09-11 12:12:54 +0200791process_raw_event(event_t *raw_event __used, void *more_data,
Ingo Molnarec156762009-09-11 12:12:54 +0200792 int cpu, u64 timestamp, struct thread *thread)
793{
794 struct {
795 u32 size;
796 char data[0];
797 } *raw = more_data;
798 struct event *event;
799 int type;
800
801 type = trace_parse_common_type(raw->data);
802 event = trace_find_event(type);
803
Ingo Molnarec156762009-09-11 12:12:54 +0200804 if (!strcmp(event->name, "sched_switch"))
Ingo Molnarfbf94822009-09-11 12:12:54 +0200805 process_sched_switch_event(more_data, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +0200806 if (!strcmp(event->name, "sched_wakeup"))
Ingo Molnarfbf94822009-09-11 12:12:54 +0200807 process_sched_wakeup_event(more_data, event, cpu, timestamp, thread);
808 if (!strcmp(event->name, "sched_wakeup_new"))
809 process_sched_wakeup_event(more_data, event, cpu, timestamp, thread);
810 if (!strcmp(event->name, "sched_process_fork"))
811 process_sched_fork_event(more_data, event, cpu, timestamp, thread);
812 if (!strcmp(event->name, "sched_process_exit"))
813 process_sched_exit_event(event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +0200814}
815
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200816static int
817process_sample_event(event_t *event, unsigned long offset, unsigned long head)
818{
819 char level;
820 int show = 0;
821 struct dso *dso = NULL;
822 struct thread *thread;
823 u64 ip = event->ip.ip;
824 u64 timestamp = -1;
825 u32 cpu = -1;
826 u64 period = 1;
827 void *more_data = event->ip.__more_data;
828 int cpumode;
829
830 thread = threads__findnew(event->ip.pid, &threads, &last_match);
831
832 if (sample_type & PERF_SAMPLE_TIME) {
833 timestamp = *(u64 *)more_data;
834 more_data += sizeof(u64);
835 }
836
837 if (sample_type & PERF_SAMPLE_CPU) {
838 cpu = *(u32 *)more_data;
839 more_data += sizeof(u32);
840 more_data += sizeof(u32); /* reserved */
841 }
842
843 if (sample_type & PERF_SAMPLE_PERIOD) {
844 period = *(u64 *)more_data;
845 more_data += sizeof(u64);
846 }
847
848 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
849 (void *)(offset + head),
850 (void *)(long)(event->header.size),
851 event->header.misc,
852 event->ip.pid, event->ip.tid,
853 (void *)(long)ip,
854 (long long)period);
855
856 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
857
858 if (thread == NULL) {
859 eprintf("problem processing %d event, skipping it.\n",
860 event->header.type);
861 return -1;
862 }
863
864 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
865
866 if (cpumode == PERF_EVENT_MISC_KERNEL) {
867 show = SHOW_KERNEL;
868 level = 'k';
869
870 dso = kernel_dso;
871
872 dump_printf(" ...... dso: %s\n", dso->name);
873
874 } else if (cpumode == PERF_EVENT_MISC_USER) {
875
876 show = SHOW_USER;
877 level = '.';
878
879 } else {
880 show = SHOW_HV;
881 level = 'H';
882
883 dso = hypervisor_dso;
884
885 dump_printf(" ...... dso: [hypervisor]\n");
886 }
887
Ingo Molnarec156762009-09-11 12:12:54 +0200888 if (sample_type & PERF_SAMPLE_RAW)
889 process_raw_event(event, more_data, cpu, timestamp, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200890
891 return 0;
892}
893
894static int
895process_event(event_t *event, unsigned long offset, unsigned long head)
896{
897 trace_event(event);
898
899 switch (event->header.type) {
900 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
901 return 0;
902
903 case PERF_EVENT_COMM:
904 return process_comm_event(event, offset, head);
905
906 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
907 return 0;
908
909 case PERF_EVENT_SAMPLE:
910 return process_sample_event(event, offset, head);
911
912 case PERF_EVENT_MAX:
913 default:
914 return -1;
915 }
916
917 return 0;
918}
919
920static int __cmd_sched(void)
921{
922 int ret, rc = EXIT_FAILURE;
923 unsigned long offset = 0;
924 unsigned long head = 0;
925 struct stat perf_stat;
926 event_t *event;
927 uint32_t size;
928 char *buf;
929
930 trace_report();
931 register_idle_thread(&threads, &last_match);
932
933 input = open(input_name, O_RDONLY);
934 if (input < 0) {
935 perror("failed to open file");
936 exit(-1);
937 }
938
939 ret = fstat(input, &perf_stat);
940 if (ret < 0) {
941 perror("failed to stat file");
942 exit(-1);
943 }
944
945 if (!perf_stat.st_size) {
946 fprintf(stderr, "zero-sized file, nothing to do!\n");
947 exit(0);
948 }
949 header = perf_header__read(input);
950 head = header->data_offset;
951 sample_type = perf_header__sample_type(header);
952
953 if (!(sample_type & PERF_SAMPLE_RAW))
954 die("No trace sample to read. Did you call perf record "
955 "without -R?");
956
957 if (load_kernel() < 0) {
958 perror("failed to load kernel symbols");
959 return EXIT_FAILURE;
960 }
961
962remap:
963 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
964 MAP_SHARED, input, offset);
965 if (buf == MAP_FAILED) {
966 perror("failed to mmap file");
967 exit(-1);
968 }
969
970more:
971 event = (event_t *)(buf + head);
972
973 size = event->header.size;
974 if (!size)
975 size = 8;
976
977 if (head + event->header.size >= page_size * mmap_window) {
978 unsigned long shift = page_size * (head / page_size);
979 int res;
980
981 res = munmap(buf, page_size * mmap_window);
982 assert(res == 0);
983
984 offset += shift;
985 head -= shift;
986 goto remap;
987 }
988
989 size = event->header.size;
990
991
992 if (!size || process_event(event, offset, head) < 0) {
993
994 /*
995 * assume we lost track of the stream, check alignment, and
996 * increment a single u64 in the hope to catch on again 'soon'.
997 */
998
999 if (unlikely(head & 7))
1000 head &= ~7ULL;
1001
1002 size = 8;
1003 }
1004
1005 head += size;
1006
1007 if (offset + head < (unsigned long)perf_stat.st_size)
1008 goto more;
1009
1010 rc = EXIT_SUCCESS;
1011 close(input);
1012
1013 return rc;
1014}
1015
1016static const char * const annotate_usage[] = {
1017 "perf trace [<options>] <command>",
1018 NULL
1019};
1020
1021static const struct option options[] = {
1022 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1023 "dump raw trace in ASCII"),
1024 OPT_BOOLEAN('v', "verbose", &verbose,
1025 "be more verbose (show symbol address, etc)"),
1026 OPT_END()
1027};
1028
1029int cmd_sched(int argc, const char **argv, const char *prefix __used)
1030{
Ingo Molnarfbf94822009-09-11 12:12:54 +02001031 long nr_iterations = 10, i;
Ingo Molnarec156762009-09-11 12:12:54 +02001032
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001033 symbol__init();
1034 page_size = getpagesize();
1035
1036 argc = parse_options(argc, argv, options, annotate_usage, 0);
1037 if (argc) {
1038 /*
1039 * Special case: if there's an argument left then assume tha
1040 * it's a symbol filter:
1041 */
1042 if (argc > 1)
1043 usage_with_options(annotate_usage, options);
1044 }
1045
Ingo Molnarfbf94822009-09-11 12:12:54 +02001046// setup_pager();
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001047
Ingo Molnarec156762009-09-11 12:12:54 +02001048 calibrate_run_measurement_overhead();
1049 calibrate_sleep_measurement_overhead();
1050
1051 test_calibrations();
1052
1053 parse_trace();
1054 print_task_traces();
1055 add_cross_task_wakeups();
1056
1057 create_tasks();
Ingo Molnarad236fd2009-09-11 12:12:54 +02001058 printf("------------------------------------------------------------\n");
Ingo Molnarec156762009-09-11 12:12:54 +02001059 for (i = 0; i < nr_iterations; i++)
1060 run_one_test();
1061
1062 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001063}