blob: 260f57a72ee014030660c58408a8c1775be22521 [file] [log] [blame]
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001#include "builtin.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02002#include "perf.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003
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"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020011#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020012
Ingo Molnar0a02ad92009-09-11 12:12:54 +020013#include "util/debug.h"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020014#include "util/data_map.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020015
Ingo Molnarec156762009-09-11 12:12:54 +020016#include <sys/types.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020017#include <sys/prctl.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020018
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020019#include <semaphore.h>
20#include <pthread.h>
21#include <math.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020022
Ingo Molnarec156762009-09-11 12:12:54 +020023static char const *input_name = "perf.data";
Ingo Molnar0a02ad92009-09-11 12:12:54 +020024
Ingo Molnarec156762009-09-11 12:12:54 +020025static unsigned long total_comm = 0;
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
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020030static char default_sort_order[] = "avg, max, switch, runtime";
31static char *sort_order = default_sort_order;
32
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020033static int profile_cpu = -1;
34
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020035static char *cwd;
36static int cwdlen;
37
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020038#define PR_SET_NAME 15 /* Set process name */
39#define MAX_CPUS 4096
Ingo Molnar0a02ad92009-09-11 12:12:54 +020040
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020041static u64 run_measurement_overhead;
42static u64 sleep_measurement_overhead;
Ingo Molnarec156762009-09-11 12:12:54 +020043
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020044#define COMM_LEN 20
45#define SYM_LEN 129
Ingo Molnarec156762009-09-11 12:12:54 +020046
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020047#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020048
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020049static unsigned long nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +020050
mingo39aeb522009-09-14 20:04:48 +020051struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020052
53struct task_desc {
54 unsigned long nr;
55 unsigned long pid;
56 char comm[COMM_LEN];
57
58 unsigned long nr_events;
59 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020060 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020061
62 pthread_t thread;
63 sem_t sleep_sem;
64
65 sem_t ready_for_work;
66 sem_t work_done_sem;
67
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020068 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020069};
70
71enum sched_event_type {
72 SCHED_EVENT_RUN,
73 SCHED_EVENT_SLEEP,
74 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020075 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020076};
77
mingo39aeb522009-09-14 20:04:48 +020078struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020079 enum sched_event_type type;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020080 u64 timestamp;
81 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020082 unsigned long nr;
83 int specific_wait;
84 sem_t *wait_sem;
85 struct task_desc *wakee;
86};
87
88static struct task_desc *pid_to_task[MAX_PID];
89
90static struct task_desc **tasks;
91
92static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020093static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020094
95static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
96
97static unsigned long nr_run_events;
98static unsigned long nr_sleep_events;
99static unsigned long nr_wakeup_events;
100
101static unsigned long nr_sleep_corrections;
102static unsigned long nr_run_events_optimized;
103
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200104static unsigned long targetless_wakeups;
105static unsigned long multitarget_wakeups;
106
107static u64 cpu_usage;
108static u64 runavg_cpu_usage;
109static u64 parent_cpu_usage;
110static u64 runavg_parent_cpu_usage;
111
112static unsigned long nr_runs;
113static u64 sum_runtime;
114static u64 sum_fluct;
115static u64 run_avg;
116
117static unsigned long replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200118static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200119static unsigned long nr_unordered_timestamps;
120static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200121static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200122static unsigned long nr_events;
123static unsigned long nr_lost_chunks;
124static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200125
126#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
127
128enum thread_state {
129 THREAD_SLEEPING = 0,
130 THREAD_WAIT_CPU,
131 THREAD_SCHED_IN,
132 THREAD_IGNORE
133};
134
135struct work_atom {
136 struct list_head list;
137 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200138 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200139 u64 wake_up_time;
140 u64 sched_in_time;
141 u64 runtime;
142};
143
mingo39aeb522009-09-14 20:04:48 +0200144struct work_atoms {
145 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200146 struct thread *thread;
147 struct rb_node node;
148 u64 max_lat;
149 u64 total_lat;
150 u64 nb_atoms;
151 u64 total_runtime;
152};
153
mingo39aeb522009-09-14 20:04:48 +0200154typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200155
156static struct rb_root atom_root, sorted_atom_root;
157
158static u64 all_runtime;
159static u64 all_count;
160
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200161
162static u64 get_nsecs(void)
163{
164 struct timespec ts;
165
166 clock_gettime(CLOCK_MONOTONIC, &ts);
167
168 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
169}
170
171static void burn_nsecs(u64 nsecs)
172{
173 u64 T0 = get_nsecs(), T1;
174
175 do {
176 T1 = get_nsecs();
177 } while (T1 + run_measurement_overhead < T0 + nsecs);
178}
179
180static void sleep_nsecs(u64 nsecs)
181{
182 struct timespec ts;
183
184 ts.tv_nsec = nsecs % 999999999;
185 ts.tv_sec = nsecs / 999999999;
186
187 nanosleep(&ts, NULL);
188}
189
190static void calibrate_run_measurement_overhead(void)
191{
192 u64 T0, T1, delta, min_delta = 1000000000ULL;
193 int i;
194
195 for (i = 0; i < 10; i++) {
196 T0 = get_nsecs();
197 burn_nsecs(0);
198 T1 = get_nsecs();
199 delta = T1-T0;
200 min_delta = min(min_delta, delta);
201 }
202 run_measurement_overhead = min_delta;
203
204 printf("run measurement overhead: %Ld nsecs\n", min_delta);
205}
206
207static void calibrate_sleep_measurement_overhead(void)
208{
209 u64 T0, T1, delta, min_delta = 1000000000ULL;
210 int i;
211
212 for (i = 0; i < 10; i++) {
213 T0 = get_nsecs();
214 sleep_nsecs(10000);
215 T1 = get_nsecs();
216 delta = T1-T0;
217 min_delta = min(min_delta, delta);
218 }
219 min_delta -= 10000;
220 sleep_measurement_overhead = min_delta;
221
222 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
223}
224
mingo39aeb522009-09-14 20:04:48 +0200225static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200226get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200227{
mingo39aeb522009-09-14 20:04:48 +0200228 struct sched_atom *event = calloc(1, sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200229 unsigned long idx = task->nr_events;
230 size_t size;
231
232 event->timestamp = timestamp;
233 event->nr = idx;
234
235 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200236 size = sizeof(struct sched_atom *) * task->nr_events;
237 task->atoms = realloc(task->atoms, size);
238 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200239
mingo39aeb522009-09-14 20:04:48 +0200240 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200241
242 return event;
243}
244
mingo39aeb522009-09-14 20:04:48 +0200245static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200246{
247 if (!task->nr_events)
248 return NULL;
249
mingo39aeb522009-09-14 20:04:48 +0200250 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200251}
252
253static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200254add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200255{
mingo39aeb522009-09-14 20:04:48 +0200256 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200257
258 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200259 * optimize an existing RUN event by merging this one
260 * to it:
261 */
Ingo Molnarec156762009-09-11 12:12:54 +0200262 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
263 nr_run_events_optimized++;
264 curr_event->duration += duration;
265 return;
266 }
267
268 event = get_new_event(task, timestamp);
269
270 event->type = SCHED_EVENT_RUN;
271 event->duration = duration;
272
273 nr_run_events++;
274}
275
Ingo Molnarec156762009-09-11 12:12:54 +0200276static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200277add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200278 struct task_desc *wakee)
279{
mingo39aeb522009-09-14 20:04:48 +0200280 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200281
282 event = get_new_event(task, timestamp);
283 event->type = SCHED_EVENT_WAKEUP;
284 event->wakee = wakee;
285
286 wakee_event = last_event(wakee);
287 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
288 targetless_wakeups++;
289 return;
290 }
291 if (wakee_event->wait_sem) {
292 multitarget_wakeups++;
293 return;
294 }
295
296 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
297 sem_init(wakee_event->wait_sem, 0, 0);
298 wakee_event->specific_wait = 1;
299 event->wait_sem = wakee_event->wait_sem;
300
301 nr_wakeup_events++;
302}
303
304static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200305add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200306 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200307{
mingo39aeb522009-09-14 20:04:48 +0200308 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200309
310 event->type = SCHED_EVENT_SLEEP;
311
312 nr_sleep_events++;
313}
314
315static struct task_desc *register_pid(unsigned long pid, const char *comm)
316{
317 struct task_desc *task;
318
319 BUG_ON(pid >= MAX_PID);
320
321 task = pid_to_task[pid];
322
323 if (task)
324 return task;
325
326 task = calloc(1, sizeof(*task));
327 task->pid = pid;
328 task->nr = nr_tasks;
329 strcpy(task->comm, comm);
330 /*
331 * every task starts in sleeping state - this gets ignored
332 * if there's no wakeup pointing to this sleep state:
333 */
334 add_sched_event_sleep(task, 0, 0);
335
336 pid_to_task[pid] = task;
337 nr_tasks++;
338 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
339 BUG_ON(!tasks);
340 tasks[task->nr] = task;
341
Ingo Molnarad236fd2009-09-11 12:12:54 +0200342 if (verbose)
343 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200344
345 return task;
346}
347
348
Ingo Molnarec156762009-09-11 12:12:54 +0200349static void print_task_traces(void)
350{
351 struct task_desc *task;
352 unsigned long i;
353
354 for (i = 0; i < nr_tasks; i++) {
355 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200356 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200357 task->nr, task->comm, task->pid, task->nr_events);
358 }
359}
360
361static void add_cross_task_wakeups(void)
362{
363 struct task_desc *task1, *task2;
364 unsigned long i, j;
365
366 for (i = 0; i < nr_tasks; i++) {
367 task1 = tasks[i];
368 j = i + 1;
369 if (j == nr_tasks)
370 j = 0;
371 task2 = tasks[j];
372 add_sched_event_wakeup(task1, 0, task2);
373 }
374}
375
376static void
mingo39aeb522009-09-14 20:04:48 +0200377process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200378{
379 int ret = 0;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200380 u64 now;
Ingo Molnarec156762009-09-11 12:12:54 +0200381 long long delta;
382
383 now = get_nsecs();
mingo39aeb522009-09-14 20:04:48 +0200384 delta = start_time + atom->timestamp - now;
Ingo Molnarec156762009-09-11 12:12:54 +0200385
mingo39aeb522009-09-14 20:04:48 +0200386 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200387 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200388 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200389 break;
390 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200391 if (atom->wait_sem)
392 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200393 BUG_ON(ret);
394 break;
395 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200396 if (atom->wait_sem)
397 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200398 BUG_ON(ret);
399 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200400 case SCHED_EVENT_MIGRATION:
401 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200402 default:
403 BUG_ON(1);
404 }
405}
406
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200407static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200408{
409 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200410 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200411 int err;
412
413 err = getrusage(RUSAGE_SELF, &ru);
414 BUG_ON(err);
415
416 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
417 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
418
419 return sum;
420}
421
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200422static u64 get_cpu_usage_nsec_self(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200423{
424 char filename [] = "/proc/1234567890/sched";
425 unsigned long msecs, nsecs;
426 char *line = NULL;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200427 u64 total = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200428 size_t len = 0;
429 ssize_t chars;
430 FILE *file;
431 int ret;
432
433 sprintf(filename, "/proc/%d/sched", getpid());
434 file = fopen(filename, "r");
435 BUG_ON(!file);
436
437 while ((chars = getline(&line, &len, file)) != -1) {
Ingo Molnarec156762009-09-11 12:12:54 +0200438 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
439 &msecs, &nsecs);
440 if (ret == 2) {
441 total = msecs*1e6 + nsecs;
Ingo Molnarec156762009-09-11 12:12:54 +0200442 break;
443 }
444 }
445 if (line)
446 free(line);
447 fclose(file);
448
449 return total;
450}
451
452static void *thread_func(void *ctx)
453{
454 struct task_desc *this_task = ctx;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200455 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200456 unsigned long i, ret;
457 char comm2[22];
458
Ingo Molnarec156762009-09-11 12:12:54 +0200459 sprintf(comm2, ":%s", this_task->comm);
460 prctl(PR_SET_NAME, comm2);
461
462again:
463 ret = sem_post(&this_task->ready_for_work);
464 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200465 ret = pthread_mutex_lock(&start_work_mutex);
466 BUG_ON(ret);
467 ret = pthread_mutex_unlock(&start_work_mutex);
468 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200469
470 cpu_usage_0 = get_cpu_usage_nsec_self();
471
472 for (i = 0; i < this_task->nr_events; i++) {
473 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200474 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200475 }
476
477 cpu_usage_1 = get_cpu_usage_nsec_self();
478 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
479
Ingo Molnarec156762009-09-11 12:12:54 +0200480 ret = sem_post(&this_task->work_done_sem);
481 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200482
483 ret = pthread_mutex_lock(&work_done_wait_mutex);
484 BUG_ON(ret);
485 ret = pthread_mutex_unlock(&work_done_wait_mutex);
486 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200487
488 goto again;
489}
490
491static void create_tasks(void)
492{
493 struct task_desc *task;
494 pthread_attr_t attr;
495 unsigned long i;
496 int err;
497
498 err = pthread_attr_init(&attr);
499 BUG_ON(err);
500 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
501 BUG_ON(err);
502 err = pthread_mutex_lock(&start_work_mutex);
503 BUG_ON(err);
504 err = pthread_mutex_lock(&work_done_wait_mutex);
505 BUG_ON(err);
506 for (i = 0; i < nr_tasks; i++) {
507 task = tasks[i];
508 sem_init(&task->sleep_sem, 0, 0);
509 sem_init(&task->ready_for_work, 0, 0);
510 sem_init(&task->work_done_sem, 0, 0);
511 task->curr_event = 0;
512 err = pthread_create(&task->thread, &attr, thread_func, task);
513 BUG_ON(err);
514 }
515}
516
Ingo Molnarec156762009-09-11 12:12:54 +0200517static void wait_for_tasks(void)
518{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200519 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200520 struct task_desc *task;
521 unsigned long i, ret;
522
Ingo Molnarec156762009-09-11 12:12:54 +0200523 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200524 cpu_usage = 0;
525 pthread_mutex_unlock(&work_done_wait_mutex);
526
527 for (i = 0; i < nr_tasks; i++) {
528 task = tasks[i];
529 ret = sem_wait(&task->ready_for_work);
530 BUG_ON(ret);
531 sem_init(&task->ready_for_work, 0, 0);
532 }
533 ret = pthread_mutex_lock(&work_done_wait_mutex);
534 BUG_ON(ret);
535
536 cpu_usage_0 = get_cpu_usage_nsec_parent();
537
538 pthread_mutex_unlock(&start_work_mutex);
539
Ingo Molnarec156762009-09-11 12:12:54 +0200540 for (i = 0; i < nr_tasks; i++) {
541 task = tasks[i];
542 ret = sem_wait(&task->work_done_sem);
543 BUG_ON(ret);
544 sem_init(&task->work_done_sem, 0, 0);
545 cpu_usage += task->cpu_usage;
546 task->cpu_usage = 0;
547 }
548
549 cpu_usage_1 = get_cpu_usage_nsec_parent();
550 if (!runavg_cpu_usage)
551 runavg_cpu_usage = cpu_usage;
552 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
553
554 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
555 if (!runavg_parent_cpu_usage)
556 runavg_parent_cpu_usage = parent_cpu_usage;
557 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
558 parent_cpu_usage)/10;
559
560 ret = pthread_mutex_lock(&start_work_mutex);
561 BUG_ON(ret);
562
563 for (i = 0; i < nr_tasks; i++) {
564 task = tasks[i];
565 sem_init(&task->sleep_sem, 0, 0);
566 task->curr_event = 0;
567 }
568}
569
Ingo Molnarec156762009-09-11 12:12:54 +0200570static void run_one_test(void)
571{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200572 u64 T0, T1, delta, avg_delta, fluct, std_dev;
Ingo Molnarec156762009-09-11 12:12:54 +0200573
574 T0 = get_nsecs();
575 wait_for_tasks();
576 T1 = get_nsecs();
577
578 delta = T1 - T0;
579 sum_runtime += delta;
580 nr_runs++;
581
582 avg_delta = sum_runtime / nr_runs;
583 if (delta < avg_delta)
584 fluct = avg_delta - delta;
585 else
586 fluct = delta - avg_delta;
587 sum_fluct += fluct;
588 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
589 if (!run_avg)
590 run_avg = delta;
591 run_avg = (run_avg*9 + delta)/10;
592
Ingo Molnarad236fd2009-09-11 12:12:54 +0200593 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200594 nr_runs, (double)delta/1000000.0);
595
Ingo Molnarad236fd2009-09-11 12:12:54 +0200596 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200597 (double)run_avg/1e6);
598
Ingo Molnarad236fd2009-09-11 12:12:54 +0200599 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200600 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
601
602#if 0
603 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200604 * rusage statistics done by the parent, these are less
605 * accurate than the sum_exec_runtime based statistics:
606 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200607 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200608 (double)parent_cpu_usage/1e6,
609 (double)runavg_parent_cpu_usage/1e6);
610#endif
611
Ingo Molnarad236fd2009-09-11 12:12:54 +0200612 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200613
614 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200615 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200616 nr_sleep_corrections = 0;
617}
618
619static void test_calibrations(void)
620{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200621 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200622
623 T0 = get_nsecs();
624 burn_nsecs(1e6);
625 T1 = get_nsecs();
626
Ingo Molnarad236fd2009-09-11 12:12:54 +0200627 printf("the run test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200628
629 T0 = get_nsecs();
630 sleep_nsecs(1e6);
631 T1 = get_nsecs();
632
Ingo Molnarad236fd2009-09-11 12:12:54 +0200633 printf("the sleep test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200634}
635
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200636static int
637process_comm_event(event_t *event, unsigned long offset, unsigned long head)
638{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300639 struct thread *thread = threads__findnew(event->comm.tid);
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200640
Ingo Molnardc02bf72009-09-16 13:45:00 +0200641 dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200642 (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)) {
Ingo Molnardc02bf72009-09-16 13:45:00 +0200648 dump_printf("problem processing perf_event_comm, skipping event.\n");
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200649 return -1;
650 }
651 total_comm++;
652
653 return 0;
654}
655
Frederic Weisbecker46538812009-09-12 02:43:45 +0200656
657struct raw_event_sample {
658 u32 size;
659 char data[0];
660};
661
662#define FILL_FIELD(ptr, field, event, data) \
663 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
664
665#define FILL_ARRAY(ptr, array, event, data) \
666do { \
667 void *__array = raw_field_ptr(event, #array, data); \
668 memcpy(ptr.array, __array, sizeof(ptr.array)); \
669} while(0)
670
671#define FILL_COMMON_FIELDS(ptr, event, data) \
672do { \
673 FILL_FIELD(ptr, common_type, event, data); \
674 FILL_FIELD(ptr, common_flags, event, data); \
675 FILL_FIELD(ptr, common_preempt_count, event, data); \
676 FILL_FIELD(ptr, common_pid, event, data); \
677 FILL_FIELD(ptr, common_tgid, event, data); \
678} while (0)
679
Ingo Molnarfbf94822009-09-11 12:12:54 +0200680
Ingo Molnarec156762009-09-11 12:12:54 +0200681
Ingo Molnarfbf94822009-09-11 12:12:54 +0200682struct trace_switch_event {
683 u32 size;
684
685 u16 common_type;
686 u8 common_flags;
687 u8 common_preempt_count;
688 u32 common_pid;
689 u32 common_tgid;
690
691 char prev_comm[16];
692 u32 prev_pid;
693 u32 prev_prio;
694 u64 prev_state;
695 char next_comm[16];
696 u32 next_pid;
697 u32 next_prio;
698};
699
mingo39aeb522009-09-14 20:04:48 +0200700struct trace_runtime_event {
701 u32 size;
702
703 u16 common_type;
704 u8 common_flags;
705 u8 common_preempt_count;
706 u32 common_pid;
707 u32 common_tgid;
708
709 char comm[16];
710 u32 pid;
711 u64 runtime;
712 u64 vruntime;
713};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200714
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200715struct trace_wakeup_event {
716 u32 size;
717
718 u16 common_type;
719 u8 common_flags;
720 u8 common_preempt_count;
721 u32 common_pid;
722 u32 common_tgid;
723
724 char comm[16];
725 u32 pid;
726
727 u32 prio;
728 u32 success;
729 u32 cpu;
730};
731
732struct trace_fork_event {
733 u32 size;
734
735 u16 common_type;
736 u8 common_flags;
737 u8 common_preempt_count;
738 u32 common_pid;
739 u32 common_tgid;
740
741 char parent_comm[16];
742 u32 parent_pid;
743 char child_comm[16];
744 u32 child_pid;
745};
746
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200747struct trace_migrate_task_event {
748 u32 size;
749
750 u16 common_type;
751 u8 common_flags;
752 u8 common_preempt_count;
753 u32 common_pid;
754 u32 common_tgid;
755
756 char comm[16];
757 u32 pid;
758
759 u32 prio;
760 u32 cpu;
761};
762
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200763struct trace_sched_handler {
764 void (*switch_event)(struct trace_switch_event *,
765 struct event *,
766 int cpu,
767 u64 timestamp,
768 struct thread *thread);
769
mingo39aeb522009-09-14 20:04:48 +0200770 void (*runtime_event)(struct trace_runtime_event *,
771 struct event *,
772 int cpu,
773 u64 timestamp,
774 struct thread *thread);
775
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200776 void (*wakeup_event)(struct trace_wakeup_event *,
777 struct event *,
778 int cpu,
779 u64 timestamp,
780 struct thread *thread);
781
782 void (*fork_event)(struct trace_fork_event *,
783 struct event *,
784 int cpu,
785 u64 timestamp,
786 struct thread *thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200787
788 void (*migrate_task_event)(struct trace_migrate_task_event *,
789 struct event *,
790 int cpu,
791 u64 timestamp,
792 struct thread *thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200793};
794
Ingo Molnarfbf94822009-09-11 12:12:54 +0200795
796static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200797replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
798 struct event *event,
799 int cpu __used,
800 u64 timestamp __used,
801 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200802{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200803 struct task_desc *waker, *wakee;
804
805 if (verbose) {
806 printf("sched_wakeup event %p\n", event);
807
808 printf(" ... pid %d woke up %s/%d\n",
809 wakeup_event->common_pid,
810 wakeup_event->comm,
811 wakeup_event->pid);
812 }
813
814 waker = register_pid(wakeup_event->common_pid, "<unknown>");
815 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
816
817 add_sched_event_wakeup(waker, timestamp, wakee);
818}
819
Ingo Molnard1153382009-09-14 18:22:53 +0200820static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200821
822static void
823replay_switch_event(struct trace_switch_event *switch_event,
824 struct event *event,
825 int cpu,
826 u64 timestamp,
827 struct thread *thread __used)
828{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200829 struct task_desc *prev, *next;
830 u64 timestamp0;
831 s64 delta;
832
Ingo Molnarad236fd2009-09-11 12:12:54 +0200833 if (verbose)
834 printf("sched_switch event %p\n", event);
835
Ingo Molnarfbf94822009-09-11 12:12:54 +0200836 if (cpu >= MAX_CPUS || cpu < 0)
837 return;
838
839 timestamp0 = cpu_last_switched[cpu];
840 if (timestamp0)
841 delta = timestamp - timestamp0;
842 else
843 delta = 0;
844
845 if (delta < 0)
846 die("hm, delta: %Ld < 0 ?\n", delta);
847
Ingo Molnarad236fd2009-09-11 12:12:54 +0200848 if (verbose) {
849 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200850 switch_event->prev_comm, switch_event->prev_pid,
851 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200852 delta);
853 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200854
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200855 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
856 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200857
858 cpu_last_switched[cpu] = timestamp;
859
860 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200861 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200862}
863
Ingo Molnarfbf94822009-09-11 12:12:54 +0200864
865static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200866replay_fork_event(struct trace_fork_event *fork_event,
867 struct event *event,
868 int cpu __used,
869 u64 timestamp __used,
870 struct thread *thread __used)
871{
872 if (verbose) {
873 printf("sched_fork event %p\n", event);
874 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
875 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
876 }
877 register_pid(fork_event->parent_pid, fork_event->parent_comm);
878 register_pid(fork_event->child_pid, fork_event->child_comm);
879}
880
881static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200882 .wakeup_event = replay_wakeup_event,
883 .switch_event = replay_switch_event,
884 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200885};
886
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200887struct sort_dimension {
888 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200889 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200890 struct list_head list;
891};
892
893static LIST_HEAD(cmp_pid);
894
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200895static int
mingo39aeb522009-09-14 20:04:48 +0200896thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200897{
898 struct sort_dimension *sort;
899 int ret = 0;
900
Ingo Molnarb5fae122009-09-11 12:12:54 +0200901 BUG_ON(list_empty(list));
902
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200903 list_for_each_entry(sort, list, list) {
904 ret = sort->cmp(l, r);
905 if (ret)
906 return ret;
907 }
908
909 return ret;
910}
911
mingo39aeb522009-09-14 20:04:48 +0200912static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200913thread_atoms_search(struct rb_root *root, struct thread *thread,
914 struct list_head *sort_list)
915{
916 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200917 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200918
919 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200920 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200921 int cmp;
922
mingo39aeb522009-09-14 20:04:48 +0200923 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200924
925 cmp = thread_lat_cmp(sort_list, &key, atoms);
926 if (cmp > 0)
927 node = node->rb_left;
928 else if (cmp < 0)
929 node = node->rb_right;
930 else {
931 BUG_ON(thread != atoms->thread);
932 return atoms;
933 }
934 }
935 return NULL;
936}
937
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938static void
mingo39aeb522009-09-14 20:04:48 +0200939__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200940 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941{
942 struct rb_node **new = &(root->rb_node), *parent = NULL;
943
944 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200945 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200946 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947
mingo39aeb522009-09-14 20:04:48 +0200948 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200949 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200950
951 cmp = thread_lat_cmp(sort_list, data, this);
952
953 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200955 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200956 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957 }
958
959 rb_link_node(&data->node, parent, new);
960 rb_insert_color(&data->node, root);
961}
962
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200963static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200964{
mingo39aeb522009-09-14 20:04:48 +0200965 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200966
Frederic Weisbecker17562202009-09-12 23:11:32 +0200967 atoms = calloc(sizeof(*atoms), 1);
968 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200969 die("No memory");
970
Frederic Weisbecker17562202009-09-12 23:11:32 +0200971 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200972 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200973 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200974}
975
976static void
977latency_fork_event(struct trace_fork_event *fork_event __used,
978 struct event *event __used,
979 int cpu __used,
980 u64 timestamp __used,
981 struct thread *thread __used)
982{
983 /* should insert the newcomer */
984}
985
Ingo Molnarea92ed52009-09-12 10:08:34 +0200986__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987static char sched_out_state(struct trace_switch_event *switch_event)
988{
989 const char *str = TASK_STATE_TO_CHAR_STR;
990
991 return str[switch_event->prev_state];
992}
993
994static void
mingo39aeb522009-09-14 20:04:48 +0200995add_sched_out_event(struct work_atoms *atoms,
996 char run_state,
997 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200998{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200999 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001000
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001001 atom = calloc(sizeof(*atom), 1);
1002 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001003 die("Non memory");
1004
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001005 atom->sched_out_time = timestamp;
1006
mingo39aeb522009-09-14 20:04:48 +02001007 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001008 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001009 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +02001010 }
1011
mingo39aeb522009-09-14 20:04:48 +02001012 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001013}
1014
1015static void
mingo39aeb522009-09-14 20:04:48 +02001016add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
1017{
1018 struct work_atom *atom;
1019
1020 BUG_ON(list_empty(&atoms->work_list));
1021
1022 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1023
1024 atom->runtime += delta;
1025 atoms->total_runtime += delta;
1026}
1027
1028static void
1029add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001031 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001032 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001033
mingo39aeb522009-09-14 20:04:48 +02001034 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001035 return;
1036
mingo39aeb522009-09-14 20:04:48 +02001037 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001038
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001039 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001040 return;
1041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001042 if (timestamp < atom->wake_up_time) {
1043 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001044 return;
1045 }
1046
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001047 atom->state = THREAD_SCHED_IN;
1048 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001049
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001050 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001051 atoms->total_lat += delta;
1052 if (delta > atoms->max_lat)
1053 atoms->max_lat = delta;
1054 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001055}
1056
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001057static void
1058latency_switch_event(struct trace_switch_event *switch_event,
1059 struct event *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001060 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001061 u64 timestamp,
1062 struct thread *thread __used)
1063{
mingo39aeb522009-09-14 20:04:48 +02001064 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001065 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001066 u64 timestamp0;
1067 s64 delta;
1068
mingo39aeb522009-09-14 20:04:48 +02001069 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001070
1071 timestamp0 = cpu_last_switched[cpu];
1072 cpu_last_switched[cpu] = timestamp;
1073 if (timestamp0)
1074 delta = timestamp - timestamp0;
1075 else
1076 delta = 0;
1077
1078 if (delta < 0)
1079 die("hm, delta: %Ld < 0 ?\n", delta);
1080
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001081
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001082 sched_out = threads__findnew(switch_event->prev_pid);
1083 sched_in = threads__findnew(switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001084
mingo39aeb522009-09-14 20:04:48 +02001085 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1086 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001087 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001088 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1089 if (!out_events)
1090 die("out-event: Internal tree error");
1091 }
1092 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1093
1094 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1095 if (!in_events) {
1096 thread_atoms_insert(sched_in);
1097 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1098 if (!in_events)
1099 die("in-event: Internal tree error");
1100 /*
1101 * Take came in we have not heard about yet,
1102 * add in an initial atom in runnable state:
1103 */
1104 add_sched_out_event(in_events, 'R', timestamp);
1105 }
1106 add_sched_in_event(in_events, timestamp);
1107}
1108
1109static void
1110latency_runtime_event(struct trace_runtime_event *runtime_event,
1111 struct event *event __used,
1112 int cpu,
1113 u64 timestamp,
1114 struct thread *this_thread __used)
1115{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001116 struct thread *thread = threads__findnew(runtime_event->pid);
1117 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001118
1119 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001120 if (!atoms) {
1121 thread_atoms_insert(thread);
1122 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1123 if (!atoms)
1124 die("in-event: Internal tree error");
1125 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001126 }
1127
mingo39aeb522009-09-14 20:04:48 +02001128 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001129}
1130
1131static void
1132latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
mingo39aeb522009-09-14 20:04:48 +02001133 struct event *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134 int cpu __used,
1135 u64 timestamp,
1136 struct thread *thread __used)
1137{
mingo39aeb522009-09-14 20:04:48 +02001138 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001139 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001140 struct thread *wakee;
1141
1142 /* Note for later, it may be interesting to observe the failing cases */
1143 if (!wakeup_event->success)
1144 return;
1145
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001146 wakee = threads__findnew(wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001147 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001148 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001149 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001150 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1151 if (!atoms)
1152 die("wakeup-event: Internal tree error");
1153 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001154 }
1155
mingo39aeb522009-09-14 20:04:48 +02001156 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001157
mingo39aeb522009-09-14 20:04:48 +02001158 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001159
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001160 /*
1161 * You WILL be missing events if you've recorded only
1162 * one CPU, or are only looking at only one, so don't
1163 * make useless noise.
1164 */
1165 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001166 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001167
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001168 nr_timestamps++;
1169 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001170 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001171 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001172 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001173
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001174 atom->state = THREAD_WAIT_CPU;
1175 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001176}
1177
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001178static void
1179latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1180 struct event *__event __used,
1181 int cpu __used,
1182 u64 timestamp,
1183 struct thread *thread __used)
1184{
1185 struct work_atoms *atoms;
1186 struct work_atom *atom;
1187 struct thread *migrant;
1188
1189 /*
1190 * Only need to worry about migration when profiling one CPU.
1191 */
1192 if (profile_cpu == -1)
1193 return;
1194
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001195 migrant = threads__findnew(migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001196 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1197 if (!atoms) {
1198 thread_atoms_insert(migrant);
1199 register_pid(migrant->pid, migrant->comm);
1200 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1201 if (!atoms)
1202 die("migration-event: Internal tree error");
1203 add_sched_out_event(atoms, 'R', timestamp);
1204 }
1205
1206 BUG_ON(list_empty(&atoms->work_list));
1207
1208 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1209 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1210
1211 nr_timestamps++;
1212
1213 if (atom->sched_out_time > timestamp)
1214 nr_unordered_timestamps++;
1215}
1216
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001217static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001218 .wakeup_event = latency_wakeup_event,
1219 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001220 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001221 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001222 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001223};
1224
mingo39aeb522009-09-14 20:04:48 +02001225static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001226{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001227 int i;
1228 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001229 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001230
mingo39aeb522009-09-14 20:04:48 +02001231 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001232 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001233 /*
1234 * Ignore idle threads:
1235 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001236 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001237 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001238
mingo39aeb522009-09-14 20:04:48 +02001239 all_runtime += work_list->total_runtime;
1240 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001241
Ingo Molnar80ed0982009-09-16 14:12:36 +02001242 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001243
mingo08f69e62009-09-14 18:30:44 +02001244 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001245 printf(" ");
1246
mingo39aeb522009-09-14 20:04:48 +02001247 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001248
Ingo Molnardc02bf72009-09-16 13:45:00 +02001249 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
mingo39aeb522009-09-14 20:04:48 +02001250 (double)work_list->total_runtime / 1e6,
1251 work_list->nb_atoms, (double)avg / 1e6,
1252 (double)work_list->max_lat / 1e6);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001253}
1254
mingo39aeb522009-09-14 20:04:48 +02001255static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001256{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001257 if (l->thread->pid < r->thread->pid)
1258 return -1;
1259 if (l->thread->pid > r->thread->pid)
1260 return 1;
1261
1262 return 0;
1263}
1264
1265static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001266 .name = "pid",
1267 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001268};
1269
mingo39aeb522009-09-14 20:04:48 +02001270static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001271{
1272 u64 avgl, avgr;
1273
1274 if (!l->nb_atoms)
1275 return -1;
1276
1277 if (!r->nb_atoms)
1278 return 1;
1279
1280 avgl = l->total_lat / l->nb_atoms;
1281 avgr = r->total_lat / r->nb_atoms;
1282
1283 if (avgl < avgr)
1284 return -1;
1285 if (avgl > avgr)
1286 return 1;
1287
1288 return 0;
1289}
1290
1291static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001292 .name = "avg",
1293 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001294};
1295
mingo39aeb522009-09-14 20:04:48 +02001296static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001297{
1298 if (l->max_lat < r->max_lat)
1299 return -1;
1300 if (l->max_lat > r->max_lat)
1301 return 1;
1302
1303 return 0;
1304}
1305
1306static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001307 .name = "max",
1308 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001309};
1310
mingo39aeb522009-09-14 20:04:48 +02001311static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001312{
1313 if (l->nb_atoms < r->nb_atoms)
1314 return -1;
1315 if (l->nb_atoms > r->nb_atoms)
1316 return 1;
1317
1318 return 0;
1319}
1320
1321static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001322 .name = "switch",
1323 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001324};
1325
mingo39aeb522009-09-14 20:04:48 +02001326static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001327{
1328 if (l->total_runtime < r->total_runtime)
1329 return -1;
1330 if (l->total_runtime > r->total_runtime)
1331 return 1;
1332
1333 return 0;
1334}
1335
1336static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001337 .name = "runtime",
1338 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001339};
1340
1341static struct sort_dimension *available_sorts[] = {
1342 &pid_sort_dimension,
1343 &avg_sort_dimension,
1344 &max_sort_dimension,
1345 &switch_sort_dimension,
1346 &runtime_sort_dimension,
1347};
1348
1349#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1350
1351static LIST_HEAD(sort_list);
1352
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001353static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001354{
1355 int i;
1356
1357 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1358 if (!strcmp(available_sorts[i]->name, tok)) {
1359 list_add_tail(&available_sorts[i]->list, list);
1360
1361 return 0;
1362 }
1363 }
1364
1365 return -1;
1366}
1367
1368static void setup_sorting(void);
1369
1370static void sort_lat(void)
1371{
1372 struct rb_node *node;
1373
1374 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001375 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001376 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001377 if (!node)
1378 break;
1379
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001380 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001381 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001382 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001383 }
1384}
1385
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001386static struct trace_sched_handler *trace_handler;
1387
1388static void
1389process_sched_wakeup_event(struct raw_event_sample *raw,
1390 struct event *event,
1391 int cpu __used,
1392 u64 timestamp __used,
1393 struct thread *thread __used)
1394{
1395 struct trace_wakeup_event wakeup_event;
1396
1397 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1398
1399 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1400 FILL_FIELD(wakeup_event, pid, event, raw->data);
1401 FILL_FIELD(wakeup_event, prio, event, raw->data);
1402 FILL_FIELD(wakeup_event, success, event, raw->data);
1403 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1404
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001405 if (trace_handler->wakeup_event)
1406 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001407}
1408
Ingo Molnarc8a37752009-09-16 14:07:00 +02001409/*
1410 * Track the current task - that way we can know whether there's any
1411 * weird events, such as a task being switched away that is not current.
1412 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001413static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001414
Ingo Molnarc8a37752009-09-16 14:07:00 +02001415static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1416
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001417static struct thread *curr_thread[MAX_CPUS];
1418
1419static char next_shortname1 = 'A';
1420static char next_shortname2 = '0';
1421
1422static void
1423map_switch_event(struct trace_switch_event *switch_event,
1424 struct event *event __used,
1425 int this_cpu,
1426 u64 timestamp,
1427 struct thread *thread __used)
1428{
1429 struct thread *sched_out, *sched_in;
1430 int new_shortname;
1431 u64 timestamp0;
1432 s64 delta;
1433 int cpu;
1434
1435 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1436
1437 if (this_cpu > max_cpu)
1438 max_cpu = this_cpu;
1439
1440 timestamp0 = cpu_last_switched[this_cpu];
1441 cpu_last_switched[this_cpu] = timestamp;
1442 if (timestamp0)
1443 delta = timestamp - timestamp0;
1444 else
1445 delta = 0;
1446
1447 if (delta < 0)
1448 die("hm, delta: %Ld < 0 ?\n", delta);
1449
1450
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001451 sched_out = threads__findnew(switch_event->prev_pid);
1452 sched_in = threads__findnew(switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001453
1454 curr_thread[this_cpu] = sched_in;
1455
1456 printf(" ");
1457
1458 new_shortname = 0;
1459 if (!sched_in->shortname[0]) {
1460 sched_in->shortname[0] = next_shortname1;
1461 sched_in->shortname[1] = next_shortname2;
1462
1463 if (next_shortname1 < 'Z') {
1464 next_shortname1++;
1465 } else {
1466 next_shortname1='A';
1467 if (next_shortname2 < '9') {
1468 next_shortname2++;
1469 } else {
1470 next_shortname2='0';
1471 }
1472 }
1473 new_shortname = 1;
1474 }
1475
1476 for (cpu = 0; cpu <= max_cpu; cpu++) {
1477 if (cpu != this_cpu)
1478 printf(" ");
1479 else
1480 printf("*");
1481
1482 if (curr_thread[cpu]) {
1483 if (curr_thread[cpu]->pid)
1484 printf("%2s ", curr_thread[cpu]->shortname);
1485 else
1486 printf(". ");
1487 } else
1488 printf(" ");
1489 }
1490
1491 printf(" %12.6f secs ", (double)timestamp/1e9);
1492 if (new_shortname) {
1493 printf("%s => %s:%d\n",
1494 sched_in->shortname, sched_in->comm, sched_in->pid);
1495 } else {
1496 printf("\n");
1497 }
1498}
1499
1500
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001501static void
1502process_sched_switch_event(struct raw_event_sample *raw,
1503 struct event *event,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001504 int this_cpu,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001505 u64 timestamp __used,
1506 struct thread *thread __used)
1507{
1508 struct trace_switch_event switch_event;
1509
1510 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1511
1512 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1513 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1514 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1515 FILL_FIELD(switch_event, prev_state, event, raw->data);
1516 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1517 FILL_FIELD(switch_event, next_pid, event, raw->data);
1518 FILL_FIELD(switch_event, next_prio, event, raw->data);
1519
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001520 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001521 /*
1522 * Are we trying to switch away a PID that is
1523 * not current?
1524 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001525 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001526 nr_context_switch_bugs++;
1527 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001528 if (trace_handler->switch_event)
1529 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001530
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001531 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001532}
1533
1534static void
mingo39aeb522009-09-14 20:04:48 +02001535process_sched_runtime_event(struct raw_event_sample *raw,
1536 struct event *event,
1537 int cpu __used,
1538 u64 timestamp __used,
1539 struct thread *thread __used)
1540{
1541 struct trace_runtime_event runtime_event;
1542
1543 FILL_ARRAY(runtime_event, comm, event, raw->data);
1544 FILL_FIELD(runtime_event, pid, event, raw->data);
1545 FILL_FIELD(runtime_event, runtime, event, raw->data);
1546 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1547
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001548 if (trace_handler->runtime_event)
1549 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001550}
1551
1552static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001553process_sched_fork_event(struct raw_event_sample *raw,
1554 struct event *event,
1555 int cpu __used,
1556 u64 timestamp __used,
1557 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001558{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001559 struct trace_fork_event fork_event;
1560
1561 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1562
1563 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1564 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1565 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1566 FILL_FIELD(fork_event, child_pid, event, raw->data);
1567
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001568 if (trace_handler->fork_event)
1569 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001570}
1571
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001572static void
1573process_sched_exit_event(struct event *event,
1574 int cpu __used,
1575 u64 timestamp __used,
1576 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001577{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001578 if (verbose)
1579 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001580}
1581
1582static void
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001583process_sched_migrate_task_event(struct raw_event_sample *raw,
1584 struct event *event,
1585 int cpu __used,
1586 u64 timestamp __used,
1587 struct thread *thread __used)
1588{
1589 struct trace_migrate_task_event migrate_task_event;
1590
1591 FILL_COMMON_FIELDS(migrate_task_event, event, raw->data);
1592
1593 FILL_ARRAY(migrate_task_event, comm, event, raw->data);
1594 FILL_FIELD(migrate_task_event, pid, event, raw->data);
1595 FILL_FIELD(migrate_task_event, prio, event, raw->data);
1596 FILL_FIELD(migrate_task_event, cpu, event, raw->data);
1597
1598 if (trace_handler->migrate_task_event)
1599 trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
1600}
1601
1602static void
Ingo Molnarad236fd2009-09-11 12:12:54 +02001603process_raw_event(event_t *raw_event __used, void *more_data,
Ingo Molnarec156762009-09-11 12:12:54 +02001604 int cpu, u64 timestamp, struct thread *thread)
1605{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001606 struct raw_event_sample *raw = more_data;
Ingo Molnarec156762009-09-11 12:12:54 +02001607 struct event *event;
1608 int type;
1609
1610 type = trace_parse_common_type(raw->data);
1611 event = trace_find_event(type);
1612
Ingo Molnarec156762009-09-11 12:12:54 +02001613 if (!strcmp(event->name, "sched_switch"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001614 process_sched_switch_event(raw, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001615 if (!strcmp(event->name, "sched_stat_runtime"))
1616 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001617 if (!strcmp(event->name, "sched_wakeup"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001618 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001619 if (!strcmp(event->name, "sched_wakeup_new"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001620 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001621 if (!strcmp(event->name, "sched_process_fork"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001622 process_sched_fork_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001623 if (!strcmp(event->name, "sched_process_exit"))
1624 process_sched_exit_event(event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001625 if (!strcmp(event->name, "sched_migrate_task"))
1626 process_sched_migrate_task_event(raw, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001627}
1628
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001629static int
1630process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1631{
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001632 struct thread *thread;
1633 u64 ip = event->ip.ip;
1634 u64 timestamp = -1;
1635 u32 cpu = -1;
1636 u64 period = 1;
1637 void *more_data = event->ip.__more_data;
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001638
1639 if (!(sample_type & PERF_SAMPLE_RAW))
1640 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001641
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001642 thread = threads__findnew(event->ip.pid);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001643
1644 if (sample_type & PERF_SAMPLE_TIME) {
1645 timestamp = *(u64 *)more_data;
1646 more_data += sizeof(u64);
1647 }
1648
1649 if (sample_type & PERF_SAMPLE_CPU) {
1650 cpu = *(u32 *)more_data;
1651 more_data += sizeof(u32);
1652 more_data += sizeof(u32); /* reserved */
1653 }
1654
1655 if (sample_type & PERF_SAMPLE_PERIOD) {
1656 period = *(u64 *)more_data;
1657 more_data += sizeof(u64);
1658 }
1659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001661 (void *)(offset + head),
1662 (void *)(long)(event->header.size),
1663 event->header.misc,
1664 event->ip.pid, event->ip.tid,
1665 (void *)(long)ip,
1666 (long long)period);
1667
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001668 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001669 pr_debug("problem processing %d event, skipping it.\n",
1670 event->header.type);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001671 return -1;
1672 }
1673
Julia Lawallf39cdf22009-10-17 08:43:17 +02001674 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1675
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001676 if (profile_cpu != -1 && profile_cpu != (int) cpu)
1677 return 0;
1678
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001679 process_raw_event(event, more_data, cpu, timestamp, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001680
1681 return 0;
1682}
1683
1684static int
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001685process_lost_event(event_t *event __used,
1686 unsigned long offset __used,
1687 unsigned long head __used)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001688{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001689 nr_lost_chunks++;
1690 nr_lost_events += event->lost.lost;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001691
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001692 return 0;
1693}
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001694
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001695static int sample_type_check(u64 type)
1696{
1697 sample_type = type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001698
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001699 if (!(sample_type & PERF_SAMPLE_RAW)) {
1700 fprintf(stderr,
1701 "No trace sample to read. Did you call perf record "
1702 "without -R?");
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001703 return -1;
1704 }
1705
1706 return 0;
1707}
1708
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001709static struct perf_file_handler file_handler = {
1710 .process_sample_event = process_sample_event,
1711 .process_comm_event = process_comm_event,
1712 .process_lost_event = process_lost_event,
1713 .sample_type_check = sample_type_check,
1714};
1715
Ingo Molnar46f392c2009-09-12 10:08:34 +02001716static int read_events(void)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001717{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001718 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001719 register_perf_file_handler(&file_handler);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001720
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001721 return mmap_dispatch_perf_file(&header, input_name, NULL, false, 0, 0,
1722 &cwdlen, &cwd);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001723}
1724
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001725static void print_bad_events(void)
1726{
1727 if (nr_unordered_timestamps && nr_timestamps) {
1728 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1729 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1730 nr_unordered_timestamps, nr_timestamps);
1731 }
1732 if (nr_lost_events && nr_events) {
1733 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1734 (double)nr_lost_events/(double)nr_events*100.0,
1735 nr_lost_events, nr_events, nr_lost_chunks);
1736 }
1737 if (nr_state_machine_bugs && nr_timestamps) {
1738 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1739 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1740 nr_state_machine_bugs, nr_timestamps);
1741 if (nr_lost_events)
1742 printf(" (due to lost events?)");
1743 printf("\n");
1744 }
1745 if (nr_context_switch_bugs && nr_timestamps) {
1746 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1747 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1748 nr_context_switch_bugs, nr_timestamps);
1749 if (nr_lost_events)
1750 printf(" (due to lost events?)");
1751 printf("\n");
1752 }
1753}
1754
1755static void __cmd_lat(void)
1756{
1757 struct rb_node *next;
1758
1759 setup_pager();
1760 read_events();
1761 sort_lat();
1762
1763 printf("\n -----------------------------------------------------------------------------------------\n");
1764 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1765 printf(" -----------------------------------------------------------------------------------------\n");
1766
1767 next = rb_first(&sorted_atom_root);
1768
1769 while (next) {
1770 struct work_atoms *work_list;
1771
1772 work_list = rb_entry(next, struct work_atoms, node);
1773 output_lat_thread(work_list);
1774 next = rb_next(next);
1775 }
1776
1777 printf(" -----------------------------------------------------------------------------------------\n");
1778 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1779 (double)all_runtime/1e6, all_count);
1780
1781 printf(" ---------------------------------------------------\n");
1782
1783 print_bad_events();
1784 printf("\n");
1785
1786}
1787
1788static struct trace_sched_handler map_ops = {
1789 .wakeup_event = NULL,
1790 .switch_event = map_switch_event,
1791 .runtime_event = NULL,
1792 .fork_event = NULL,
1793};
1794
1795static void __cmd_map(void)
1796{
Ingo Molnar40749d02009-09-17 18:24:55 +02001797 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1798
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001799 setup_pager();
1800 read_events();
1801 print_bad_events();
1802}
1803
1804static void __cmd_replay(void)
1805{
1806 unsigned long i;
1807
1808 calibrate_run_measurement_overhead();
1809 calibrate_sleep_measurement_overhead();
1810
1811 test_calibrations();
1812
1813 read_events();
1814
1815 printf("nr_run_events: %ld\n", nr_run_events);
1816 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1817 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1818
1819 if (targetless_wakeups)
1820 printf("target-less wakeups: %ld\n", targetless_wakeups);
1821 if (multitarget_wakeups)
1822 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1823 if (nr_run_events_optimized)
1824 printf("run atoms optimized: %ld\n",
1825 nr_run_events_optimized);
1826
1827 print_task_traces();
1828 add_cross_task_wakeups();
1829
1830 create_tasks();
1831 printf("------------------------------------------------------------\n");
1832 for (i = 0; i < replay_repeat; i++)
1833 run_one_test();
1834}
1835
1836
Ingo Molnar46f392c2009-09-12 10:08:34 +02001837static const char * const sched_usage[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001838 "perf sched [<options>] {record|latency|map|replay|trace}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001839 NULL
1840};
1841
Ingo Molnarf2858d82009-09-11 12:12:54 +02001842static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001843 OPT_STRING('i', "input", &input_name, "file",
1844 "input file name"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001845 OPT_BOOLEAN('v', "verbose", &verbose,
1846 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001847 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1848 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001849 OPT_END()
1850};
1851
1852static const char * const latency_usage[] = {
1853 "perf sched latency [<options>]",
1854 NULL
1855};
1856
1857static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001858 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1859 "sort by key(s): runtime, switch, avg, max"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001860 OPT_BOOLEAN('v', "verbose", &verbose,
1861 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001862 OPT_INTEGER('C', "CPU", &profile_cpu,
1863 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001864 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1865 "dump raw trace in ASCII"),
1866 OPT_END()
1867};
1868
1869static const char * const replay_usage[] = {
1870 "perf sched replay [<options>]",
1871 NULL
1872};
1873
1874static const struct option replay_options[] = {
1875 OPT_INTEGER('r', "repeat", &replay_repeat,
1876 "repeat the workload replay N times (-1: infinite)"),
1877 OPT_BOOLEAN('v', "verbose", &verbose,
1878 "be more verbose (show symbol address, etc)"),
1879 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1880 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001881 OPT_END()
1882};
1883
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001884static void setup_sorting(void)
1885{
1886 char *tmp, *tok, *str = strdup(sort_order);
1887
1888 for (tok = strtok_r(str, ", ", &tmp);
1889 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1890 if (sort_dimension__add(tok, &sort_list) < 0) {
1891 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001892 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001893 }
1894 }
1895
1896 free(str);
1897
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001898 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001899}
1900
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001901static const char *record_args[] = {
1902 "record",
1903 "-a",
1904 "-R",
Frederic Weisbeckerd1302522009-09-14 08:57:15 +02001905 "-M",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001906 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001907 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001908 "-c", "1",
1909 "-e", "sched:sched_switch:r",
1910 "-e", "sched:sched_stat_wait:r",
1911 "-e", "sched:sched_stat_sleep:r",
1912 "-e", "sched:sched_stat_iowait:r",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001913 "-e", "sched:sched_stat_runtime:r",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001914 "-e", "sched:sched_process_exit:r",
1915 "-e", "sched:sched_process_fork:r",
1916 "-e", "sched:sched_wakeup:r",
1917 "-e", "sched:sched_migrate_task:r",
1918};
1919
1920static int __cmd_record(int argc, const char **argv)
1921{
1922 unsigned int rec_argc, i, j;
1923 const char **rec_argv;
1924
1925 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1926 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1927
1928 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1929 rec_argv[i] = strdup(record_args[i]);
1930
1931 for (j = 1; j < (unsigned int)argc; j++, i++)
1932 rec_argv[i] = argv[j];
1933
1934 BUG_ON(i != rec_argc);
1935
1936 return cmd_record(i, rec_argv, NULL);
1937}
1938
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001939int cmd_sched(int argc, const char **argv, const char *prefix __used)
1940{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001941 symbol__init(0);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001942
Ingo Molnarf2858d82009-09-11 12:12:54 +02001943 argc = parse_options(argc, argv, sched_options, sched_usage,
1944 PARSE_OPT_STOP_AT_NON_OPTION);
1945 if (!argc)
1946 usage_with_options(sched_usage, sched_options);
1947
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001948 if (!strncmp(argv[0], "rec", 3)) {
1949 return __cmd_record(argc, argv);
1950 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001951 trace_handler = &lat_ops;
1952 if (argc > 1) {
1953 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1954 if (argc)
1955 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001956 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001957 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001958 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001959 } else if (!strcmp(argv[0], "map")) {
1960 trace_handler = &map_ops;
1961 setup_sorting();
1962 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001963 } else if (!strncmp(argv[0], "rep", 3)) {
1964 trace_handler = &replay_ops;
1965 if (argc) {
1966 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1967 if (argc)
1968 usage_with_options(replay_usage, replay_options);
1969 }
1970 __cmd_replay();
Ingo Molnarc13f0d32009-09-13 16:51:04 +02001971 } else if (!strcmp(argv[0], "trace")) {
1972 /*
1973 * Aliased to 'perf trace' for now:
1974 */
1975 return cmd_trace(argc, argv, prefix);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001976 } else {
1977 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001978 }
1979
Ingo Molnarec156762009-09-11 12:12:54 +02001980 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001981}