blob: e1df7055ab823f718b02669037ff0b08251e244b [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 rb_root threads;
28static struct thread *last_match;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020029
Ingo Molnarec156762009-09-11 12:12:54 +020030static struct perf_header *header;
31static u64 sample_type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020032
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020033static char default_sort_order[] = "avg, max, switch, runtime";
34static char *sort_order = default_sort_order;
35
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020036static char *cwd;
37static int cwdlen;
38
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020039#define PR_SET_NAME 15 /* Set process name */
40#define MAX_CPUS 4096
Ingo Molnar0a02ad92009-09-11 12:12:54 +020041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020042#define BUG_ON(x) assert(!(x))
Ingo Molnarec156762009-09-11 12:12:54 +020043
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020044static u64 run_measurement_overhead;
45static u64 sleep_measurement_overhead;
Ingo Molnarec156762009-09-11 12:12:54 +020046
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020047#define COMM_LEN 20
48#define SYM_LEN 129
Ingo Molnarec156762009-09-11 12:12:54 +020049
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020050#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020051
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020052static unsigned long nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +020053
mingo39aeb522009-09-14 20:04:48 +020054struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020055
56struct task_desc {
57 unsigned long nr;
58 unsigned long pid;
59 char comm[COMM_LEN];
60
61 unsigned long nr_events;
62 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020063 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020064
65 pthread_t thread;
66 sem_t sleep_sem;
67
68 sem_t ready_for_work;
69 sem_t work_done_sem;
70
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020071 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020072};
73
74enum sched_event_type {
75 SCHED_EVENT_RUN,
76 SCHED_EVENT_SLEEP,
77 SCHED_EVENT_WAKEUP,
78};
79
mingo39aeb522009-09-14 20:04:48 +020080struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020081 enum sched_event_type type;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020082 u64 timestamp;
83 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020084 unsigned long nr;
85 int specific_wait;
86 sem_t *wait_sem;
87 struct task_desc *wakee;
88};
89
90static struct task_desc *pid_to_task[MAX_PID];
91
92static struct task_desc **tasks;
93
94static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020095static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020096
97static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
98
99static unsigned long nr_run_events;
100static unsigned long nr_sleep_events;
101static unsigned long nr_wakeup_events;
102
103static unsigned long nr_sleep_corrections;
104static unsigned long nr_run_events_optimized;
105
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200106static unsigned long targetless_wakeups;
107static unsigned long multitarget_wakeups;
108
109static u64 cpu_usage;
110static u64 runavg_cpu_usage;
111static u64 parent_cpu_usage;
112static u64 runavg_parent_cpu_usage;
113
114static unsigned long nr_runs;
115static u64 sum_runtime;
116static u64 sum_fluct;
117static u64 run_avg;
118
119static unsigned long replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200120static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200121static unsigned long nr_unordered_timestamps;
122static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200123static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200124static unsigned long nr_events;
125static unsigned long nr_lost_chunks;
126static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200127
128#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
129
130enum thread_state {
131 THREAD_SLEEPING = 0,
132 THREAD_WAIT_CPU,
133 THREAD_SCHED_IN,
134 THREAD_IGNORE
135};
136
137struct work_atom {
138 struct list_head list;
139 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200140 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200141 u64 wake_up_time;
142 u64 sched_in_time;
143 u64 runtime;
144};
145
mingo39aeb522009-09-14 20:04:48 +0200146struct work_atoms {
147 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200148 struct thread *thread;
149 struct rb_node node;
150 u64 max_lat;
151 u64 total_lat;
152 u64 nb_atoms;
153 u64 total_runtime;
154};
155
mingo39aeb522009-09-14 20:04:48 +0200156typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200157
158static struct rb_root atom_root, sorted_atom_root;
159
160static u64 all_runtime;
161static u64 all_count;
162
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200163
164static u64 get_nsecs(void)
165{
166 struct timespec ts;
167
168 clock_gettime(CLOCK_MONOTONIC, &ts);
169
170 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
171}
172
173static void burn_nsecs(u64 nsecs)
174{
175 u64 T0 = get_nsecs(), T1;
176
177 do {
178 T1 = get_nsecs();
179 } while (T1 + run_measurement_overhead < T0 + nsecs);
180}
181
182static void sleep_nsecs(u64 nsecs)
183{
184 struct timespec ts;
185
186 ts.tv_nsec = nsecs % 999999999;
187 ts.tv_sec = nsecs / 999999999;
188
189 nanosleep(&ts, NULL);
190}
191
192static void calibrate_run_measurement_overhead(void)
193{
194 u64 T0, T1, delta, min_delta = 1000000000ULL;
195 int i;
196
197 for (i = 0; i < 10; i++) {
198 T0 = get_nsecs();
199 burn_nsecs(0);
200 T1 = get_nsecs();
201 delta = T1-T0;
202 min_delta = min(min_delta, delta);
203 }
204 run_measurement_overhead = min_delta;
205
206 printf("run measurement overhead: %Ld nsecs\n", min_delta);
207}
208
209static void calibrate_sleep_measurement_overhead(void)
210{
211 u64 T0, T1, delta, min_delta = 1000000000ULL;
212 int i;
213
214 for (i = 0; i < 10; i++) {
215 T0 = get_nsecs();
216 sleep_nsecs(10000);
217 T1 = get_nsecs();
218 delta = T1-T0;
219 min_delta = min(min_delta, delta);
220 }
221 min_delta -= 10000;
222 sleep_measurement_overhead = min_delta;
223
224 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
225}
226
mingo39aeb522009-09-14 20:04:48 +0200227static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200228get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200229{
mingo39aeb522009-09-14 20:04:48 +0200230 struct sched_atom *event = calloc(1, sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200231 unsigned long idx = task->nr_events;
232 size_t size;
233
234 event->timestamp = timestamp;
235 event->nr = idx;
236
237 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200238 size = sizeof(struct sched_atom *) * task->nr_events;
239 task->atoms = realloc(task->atoms, size);
240 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200241
mingo39aeb522009-09-14 20:04:48 +0200242 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200243
244 return event;
245}
246
mingo39aeb522009-09-14 20:04:48 +0200247static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200248{
249 if (!task->nr_events)
250 return NULL;
251
mingo39aeb522009-09-14 20:04:48 +0200252 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200253}
254
255static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200256add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200257{
mingo39aeb522009-09-14 20:04:48 +0200258 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200259
260 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200261 * optimize an existing RUN event by merging this one
262 * to it:
263 */
Ingo Molnarec156762009-09-11 12:12:54 +0200264 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
265 nr_run_events_optimized++;
266 curr_event->duration += duration;
267 return;
268 }
269
270 event = get_new_event(task, timestamp);
271
272 event->type = SCHED_EVENT_RUN;
273 event->duration = duration;
274
275 nr_run_events++;
276}
277
Ingo Molnarec156762009-09-11 12:12:54 +0200278static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200279add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200280 struct task_desc *wakee)
281{
mingo39aeb522009-09-14 20:04:48 +0200282 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200283
284 event = get_new_event(task, timestamp);
285 event->type = SCHED_EVENT_WAKEUP;
286 event->wakee = wakee;
287
288 wakee_event = last_event(wakee);
289 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
290 targetless_wakeups++;
291 return;
292 }
293 if (wakee_event->wait_sem) {
294 multitarget_wakeups++;
295 return;
296 }
297
298 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
299 sem_init(wakee_event->wait_sem, 0, 0);
300 wakee_event->specific_wait = 1;
301 event->wait_sem = wakee_event->wait_sem;
302
303 nr_wakeup_events++;
304}
305
306static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200307add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200308 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200309{
mingo39aeb522009-09-14 20:04:48 +0200310 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200311
312 event->type = SCHED_EVENT_SLEEP;
313
314 nr_sleep_events++;
315}
316
317static struct task_desc *register_pid(unsigned long pid, const char *comm)
318{
319 struct task_desc *task;
320
321 BUG_ON(pid >= MAX_PID);
322
323 task = pid_to_task[pid];
324
325 if (task)
326 return task;
327
328 task = calloc(1, sizeof(*task));
329 task->pid = pid;
330 task->nr = nr_tasks;
331 strcpy(task->comm, comm);
332 /*
333 * every task starts in sleeping state - this gets ignored
334 * if there's no wakeup pointing to this sleep state:
335 */
336 add_sched_event_sleep(task, 0, 0);
337
338 pid_to_task[pid] = task;
339 nr_tasks++;
340 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
341 BUG_ON(!tasks);
342 tasks[task->nr] = task;
343
Ingo Molnarad236fd2009-09-11 12:12:54 +0200344 if (verbose)
345 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200346
347 return task;
348}
349
350
Ingo Molnarec156762009-09-11 12:12:54 +0200351static void print_task_traces(void)
352{
353 struct task_desc *task;
354 unsigned long i;
355
356 for (i = 0; i < nr_tasks; i++) {
357 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200358 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200359 task->nr, task->comm, task->pid, task->nr_events);
360 }
361}
362
363static void add_cross_task_wakeups(void)
364{
365 struct task_desc *task1, *task2;
366 unsigned long i, j;
367
368 for (i = 0; i < nr_tasks; i++) {
369 task1 = tasks[i];
370 j = i + 1;
371 if (j == nr_tasks)
372 j = 0;
373 task2 = tasks[j];
374 add_sched_event_wakeup(task1, 0, task2);
375 }
376}
377
378static void
mingo39aeb522009-09-14 20:04:48 +0200379process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200380{
381 int ret = 0;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200382 u64 now;
Ingo Molnarec156762009-09-11 12:12:54 +0200383 long long delta;
384
385 now = get_nsecs();
mingo39aeb522009-09-14 20:04:48 +0200386 delta = start_time + atom->timestamp - now;
Ingo Molnarec156762009-09-11 12:12:54 +0200387
mingo39aeb522009-09-14 20:04:48 +0200388 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200389 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200390 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200391 break;
392 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200393 if (atom->wait_sem)
394 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200395 BUG_ON(ret);
396 break;
397 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200398 if (atom->wait_sem)
399 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200400 BUG_ON(ret);
401 break;
402 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{
639 struct thread *thread;
640
641 thread = threads__findnew(event->comm.pid, &threads, &last_match);
642
Ingo Molnardc02bf72009-09-16 13:45:00 +0200643 dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200644 (void *)(offset + head),
645 (void *)(long)(event->header.size),
646 event->comm.comm, event->comm.pid);
647
648 if (thread == NULL ||
649 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnardc02bf72009-09-16 13:45:00 +0200650 dump_printf("problem processing perf_event_comm, skipping event.\n");
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200651 return -1;
652 }
653 total_comm++;
654
655 return 0;
656}
657
Frederic Weisbecker46538812009-09-12 02:43:45 +0200658
659struct raw_event_sample {
660 u32 size;
661 char data[0];
662};
663
664#define FILL_FIELD(ptr, field, event, data) \
665 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
666
667#define FILL_ARRAY(ptr, array, event, data) \
668do { \
669 void *__array = raw_field_ptr(event, #array, data); \
670 memcpy(ptr.array, __array, sizeof(ptr.array)); \
671} while(0)
672
673#define FILL_COMMON_FIELDS(ptr, event, data) \
674do { \
675 FILL_FIELD(ptr, common_type, event, data); \
676 FILL_FIELD(ptr, common_flags, event, data); \
677 FILL_FIELD(ptr, common_preempt_count, event, data); \
678 FILL_FIELD(ptr, common_pid, event, data); \
679 FILL_FIELD(ptr, common_tgid, event, data); \
680} while (0)
681
Ingo Molnarfbf94822009-09-11 12:12:54 +0200682
Ingo Molnarec156762009-09-11 12:12:54 +0200683
Ingo Molnarfbf94822009-09-11 12:12:54 +0200684struct trace_switch_event {
685 u32 size;
686
687 u16 common_type;
688 u8 common_flags;
689 u8 common_preempt_count;
690 u32 common_pid;
691 u32 common_tgid;
692
693 char prev_comm[16];
694 u32 prev_pid;
695 u32 prev_prio;
696 u64 prev_state;
697 char next_comm[16];
698 u32 next_pid;
699 u32 next_prio;
700};
701
mingo39aeb522009-09-14 20:04:48 +0200702struct trace_runtime_event {
703 u32 size;
704
705 u16 common_type;
706 u8 common_flags;
707 u8 common_preempt_count;
708 u32 common_pid;
709 u32 common_tgid;
710
711 char comm[16];
712 u32 pid;
713 u64 runtime;
714 u64 vruntime;
715};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200716
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200717struct trace_wakeup_event {
718 u32 size;
719
720 u16 common_type;
721 u8 common_flags;
722 u8 common_preempt_count;
723 u32 common_pid;
724 u32 common_tgid;
725
726 char comm[16];
727 u32 pid;
728
729 u32 prio;
730 u32 success;
731 u32 cpu;
732};
733
734struct trace_fork_event {
735 u32 size;
736
737 u16 common_type;
738 u8 common_flags;
739 u8 common_preempt_count;
740 u32 common_pid;
741 u32 common_tgid;
742
743 char parent_comm[16];
744 u32 parent_pid;
745 char child_comm[16];
746 u32 child_pid;
747};
748
749struct trace_sched_handler {
750 void (*switch_event)(struct trace_switch_event *,
751 struct event *,
752 int cpu,
753 u64 timestamp,
754 struct thread *thread);
755
mingo39aeb522009-09-14 20:04:48 +0200756 void (*runtime_event)(struct trace_runtime_event *,
757 struct event *,
758 int cpu,
759 u64 timestamp,
760 struct thread *thread);
761
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200762 void (*wakeup_event)(struct trace_wakeup_event *,
763 struct event *,
764 int cpu,
765 u64 timestamp,
766 struct thread *thread);
767
768 void (*fork_event)(struct trace_fork_event *,
769 struct event *,
770 int cpu,
771 u64 timestamp,
772 struct thread *thread);
773};
774
Ingo Molnarfbf94822009-09-11 12:12:54 +0200775
776static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200777replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
778 struct event *event,
779 int cpu __used,
780 u64 timestamp __used,
781 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200782{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200783 struct task_desc *waker, *wakee;
784
785 if (verbose) {
786 printf("sched_wakeup event %p\n", event);
787
788 printf(" ... pid %d woke up %s/%d\n",
789 wakeup_event->common_pid,
790 wakeup_event->comm,
791 wakeup_event->pid);
792 }
793
794 waker = register_pid(wakeup_event->common_pid, "<unknown>");
795 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
796
797 add_sched_event_wakeup(waker, timestamp, wakee);
798}
799
Ingo Molnard1153382009-09-14 18:22:53 +0200800static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200801
802static void
803replay_switch_event(struct trace_switch_event *switch_event,
804 struct event *event,
805 int cpu,
806 u64 timestamp,
807 struct thread *thread __used)
808{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200809 struct task_desc *prev, *next;
810 u64 timestamp0;
811 s64 delta;
812
Ingo Molnarad236fd2009-09-11 12:12:54 +0200813 if (verbose)
814 printf("sched_switch event %p\n", event);
815
Ingo Molnarfbf94822009-09-11 12:12:54 +0200816 if (cpu >= MAX_CPUS || cpu < 0)
817 return;
818
819 timestamp0 = cpu_last_switched[cpu];
820 if (timestamp0)
821 delta = timestamp - timestamp0;
822 else
823 delta = 0;
824
825 if (delta < 0)
826 die("hm, delta: %Ld < 0 ?\n", delta);
827
Ingo Molnarad236fd2009-09-11 12:12:54 +0200828 if (verbose) {
829 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200830 switch_event->prev_comm, switch_event->prev_pid,
831 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200832 delta);
833 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200834
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200835 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
836 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200837
838 cpu_last_switched[cpu] = timestamp;
839
840 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200841 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200842}
843
Ingo Molnarfbf94822009-09-11 12:12:54 +0200844
845static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200846replay_fork_event(struct trace_fork_event *fork_event,
847 struct event *event,
848 int cpu __used,
849 u64 timestamp __used,
850 struct thread *thread __used)
851{
852 if (verbose) {
853 printf("sched_fork event %p\n", event);
854 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
855 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
856 }
857 register_pid(fork_event->parent_pid, fork_event->parent_comm);
858 register_pid(fork_event->child_pid, fork_event->child_comm);
859}
860
861static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200862 .wakeup_event = replay_wakeup_event,
863 .switch_event = replay_switch_event,
864 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200865};
866
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200867struct sort_dimension {
868 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200869 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200870 struct list_head list;
871};
872
873static LIST_HEAD(cmp_pid);
874
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200875static int
mingo39aeb522009-09-14 20:04:48 +0200876thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200877{
878 struct sort_dimension *sort;
879 int ret = 0;
880
Ingo Molnarb5fae122009-09-11 12:12:54 +0200881 BUG_ON(list_empty(list));
882
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200883 list_for_each_entry(sort, list, list) {
884 ret = sort->cmp(l, r);
885 if (ret)
886 return ret;
887 }
888
889 return ret;
890}
891
mingo39aeb522009-09-14 20:04:48 +0200892static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200893thread_atoms_search(struct rb_root *root, struct thread *thread,
894 struct list_head *sort_list)
895{
896 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200897 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200898
899 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200900 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200901 int cmp;
902
mingo39aeb522009-09-14 20:04:48 +0200903 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200904
905 cmp = thread_lat_cmp(sort_list, &key, atoms);
906 if (cmp > 0)
907 node = node->rb_left;
908 else if (cmp < 0)
909 node = node->rb_right;
910 else {
911 BUG_ON(thread != atoms->thread);
912 return atoms;
913 }
914 }
915 return NULL;
916}
917
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200918static void
mingo39aeb522009-09-14 20:04:48 +0200919__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200920 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200921{
922 struct rb_node **new = &(root->rb_node), *parent = NULL;
923
924 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200925 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200926 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200927
mingo39aeb522009-09-14 20:04:48 +0200928 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200929 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200930
931 cmp = thread_lat_cmp(sort_list, data, this);
932
933 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200934 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200936 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200937 }
938
939 rb_link_node(&data->node, parent, new);
940 rb_insert_color(&data->node, root);
941}
942
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200943static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200944{
mingo39aeb522009-09-14 20:04:48 +0200945 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200946
Frederic Weisbecker17562202009-09-12 23:11:32 +0200947 atoms = calloc(sizeof(*atoms), 1);
948 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200949 die("No memory");
950
Frederic Weisbecker17562202009-09-12 23:11:32 +0200951 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200952 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200953 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954}
955
956static void
957latency_fork_event(struct trace_fork_event *fork_event __used,
958 struct event *event __used,
959 int cpu __used,
960 u64 timestamp __used,
961 struct thread *thread __used)
962{
963 /* should insert the newcomer */
964}
965
Ingo Molnarea92ed52009-09-12 10:08:34 +0200966__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200967static char sched_out_state(struct trace_switch_event *switch_event)
968{
969 const char *str = TASK_STATE_TO_CHAR_STR;
970
971 return str[switch_event->prev_state];
972}
973
974static void
mingo39aeb522009-09-14 20:04:48 +0200975add_sched_out_event(struct work_atoms *atoms,
976 char run_state,
977 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200979 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200980
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200981 atom = calloc(sizeof(*atom), 1);
982 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200983 die("Non memory");
984
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200985 atom->sched_out_time = timestamp;
986
mingo39aeb522009-09-14 20:04:48 +0200987 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200988 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200989 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200990 }
991
mingo39aeb522009-09-14 20:04:48 +0200992 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200993}
994
995static void
mingo39aeb522009-09-14 20:04:48 +0200996add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
997{
998 struct work_atom *atom;
999
1000 BUG_ON(list_empty(&atoms->work_list));
1001
1002 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1003
1004 atom->runtime += delta;
1005 atoms->total_runtime += delta;
1006}
1007
1008static void
1009add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001010{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001011 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001012 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001013
mingo39aeb522009-09-14 20:04:48 +02001014 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001015 return;
1016
mingo39aeb522009-09-14 20:04:48 +02001017 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001018
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001019 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001020 return;
1021
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001022 if (timestamp < atom->wake_up_time) {
1023 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024 return;
1025 }
1026
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001027 atom->state = THREAD_SCHED_IN;
1028 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001029
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001030 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001031 atoms->total_lat += delta;
1032 if (delta > atoms->max_lat)
1033 atoms->max_lat = delta;
1034 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001035}
1036
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001037static void
1038latency_switch_event(struct trace_switch_event *switch_event,
1039 struct event *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001040 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001041 u64 timestamp,
1042 struct thread *thread __used)
1043{
mingo39aeb522009-09-14 20:04:48 +02001044 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001045 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001046 u64 timestamp0;
1047 s64 delta;
1048
mingo39aeb522009-09-14 20:04:48 +02001049 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001050
1051 timestamp0 = cpu_last_switched[cpu];
1052 cpu_last_switched[cpu] = timestamp;
1053 if (timestamp0)
1054 delta = timestamp - timestamp0;
1055 else
1056 delta = 0;
1057
1058 if (delta < 0)
1059 die("hm, delta: %Ld < 0 ?\n", delta);
1060
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001061
1062 sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match);
1063 sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
1064
mingo39aeb522009-09-14 20:04:48 +02001065 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1066 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001067 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001068 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1069 if (!out_events)
1070 die("out-event: Internal tree error");
1071 }
1072 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1073
1074 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1075 if (!in_events) {
1076 thread_atoms_insert(sched_in);
1077 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1078 if (!in_events)
1079 die("in-event: Internal tree error");
1080 /*
1081 * Take came in we have not heard about yet,
1082 * add in an initial atom in runnable state:
1083 */
1084 add_sched_out_event(in_events, 'R', timestamp);
1085 }
1086 add_sched_in_event(in_events, timestamp);
1087}
1088
1089static void
1090latency_runtime_event(struct trace_runtime_event *runtime_event,
1091 struct event *event __used,
1092 int cpu,
1093 u64 timestamp,
1094 struct thread *this_thread __used)
1095{
1096 struct work_atoms *atoms;
1097 struct thread *thread;
1098
1099 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1100
1101 thread = threads__findnew(runtime_event->pid, &threads, &last_match);
1102 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1103 if (!atoms) {
1104 thread_atoms_insert(thread);
1105 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1106 if (!atoms)
1107 die("in-event: Internal tree error");
1108 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001109 }
1110
mingo39aeb522009-09-14 20:04:48 +02001111 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001112}
1113
1114static void
1115latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
mingo39aeb522009-09-14 20:04:48 +02001116 struct event *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001117 int cpu __used,
1118 u64 timestamp,
1119 struct thread *thread __used)
1120{
mingo39aeb522009-09-14 20:04:48 +02001121 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001122 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001123 struct thread *wakee;
1124
1125 /* Note for later, it may be interesting to observe the failing cases */
1126 if (!wakeup_event->success)
1127 return;
1128
1129 wakee = threads__findnew(wakeup_event->pid, &threads, &last_match);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001130 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001131 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001132 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001133 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1134 if (!atoms)
1135 die("wakeup-event: Internal tree error");
1136 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137 }
1138
mingo39aeb522009-09-14 20:04:48 +02001139 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001140
mingo39aeb522009-09-14 20:04:48 +02001141 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142
Ingo Molnardc02bf72009-09-16 13:45:00 +02001143 if (atom->state != THREAD_SLEEPING)
1144 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001145
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001146 nr_timestamps++;
1147 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001148 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001149 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001150 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001151
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001152 atom->state = THREAD_WAIT_CPU;
1153 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001154}
1155
1156static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001157 .wakeup_event = latency_wakeup_event,
1158 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001159 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001160 .fork_event = latency_fork_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001161};
1162
mingo39aeb522009-09-14 20:04:48 +02001163static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001164{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001165 int i;
1166 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001167 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001168
mingo39aeb522009-09-14 20:04:48 +02001169 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001170 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001171 /*
1172 * Ignore idle threads:
1173 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001174 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001175 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001176
mingo39aeb522009-09-14 20:04:48 +02001177 all_runtime += work_list->total_runtime;
1178 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001179
Ingo Molnar80ed0982009-09-16 14:12:36 +02001180 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001181
mingo08f69e62009-09-14 18:30:44 +02001182 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001183 printf(" ");
1184
mingo39aeb522009-09-14 20:04:48 +02001185 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001186
Ingo Molnardc02bf72009-09-16 13:45:00 +02001187 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
mingo39aeb522009-09-14 20:04:48 +02001188 (double)work_list->total_runtime / 1e6,
1189 work_list->nb_atoms, (double)avg / 1e6,
1190 (double)work_list->max_lat / 1e6);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001191}
1192
mingo39aeb522009-09-14 20:04:48 +02001193static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001194{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001195 if (l->thread->pid < r->thread->pid)
1196 return -1;
1197 if (l->thread->pid > r->thread->pid)
1198 return 1;
1199
1200 return 0;
1201}
1202
1203static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001204 .name = "pid",
1205 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001206};
1207
mingo39aeb522009-09-14 20:04:48 +02001208static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001209{
1210 u64 avgl, avgr;
1211
1212 if (!l->nb_atoms)
1213 return -1;
1214
1215 if (!r->nb_atoms)
1216 return 1;
1217
1218 avgl = l->total_lat / l->nb_atoms;
1219 avgr = r->total_lat / r->nb_atoms;
1220
1221 if (avgl < avgr)
1222 return -1;
1223 if (avgl > avgr)
1224 return 1;
1225
1226 return 0;
1227}
1228
1229static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001230 .name = "avg",
1231 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001232};
1233
mingo39aeb522009-09-14 20:04:48 +02001234static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001235{
1236 if (l->max_lat < r->max_lat)
1237 return -1;
1238 if (l->max_lat > r->max_lat)
1239 return 1;
1240
1241 return 0;
1242}
1243
1244static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001245 .name = "max",
1246 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001247};
1248
mingo39aeb522009-09-14 20:04:48 +02001249static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001250{
1251 if (l->nb_atoms < r->nb_atoms)
1252 return -1;
1253 if (l->nb_atoms > r->nb_atoms)
1254 return 1;
1255
1256 return 0;
1257}
1258
1259static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001260 .name = "switch",
1261 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001262};
1263
mingo39aeb522009-09-14 20:04:48 +02001264static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265{
1266 if (l->total_runtime < r->total_runtime)
1267 return -1;
1268 if (l->total_runtime > r->total_runtime)
1269 return 1;
1270
1271 return 0;
1272}
1273
1274static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001275 .name = "runtime",
1276 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001277};
1278
1279static struct sort_dimension *available_sorts[] = {
1280 &pid_sort_dimension,
1281 &avg_sort_dimension,
1282 &max_sort_dimension,
1283 &switch_sort_dimension,
1284 &runtime_sort_dimension,
1285};
1286
1287#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1288
1289static LIST_HEAD(sort_list);
1290
1291static int sort_dimension__add(char *tok, struct list_head *list)
1292{
1293 int i;
1294
1295 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1296 if (!strcmp(available_sorts[i]->name, tok)) {
1297 list_add_tail(&available_sorts[i]->list, list);
1298
1299 return 0;
1300 }
1301 }
1302
1303 return -1;
1304}
1305
1306static void setup_sorting(void);
1307
1308static void sort_lat(void)
1309{
1310 struct rb_node *node;
1311
1312 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001313 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001314 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001315 if (!node)
1316 break;
1317
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001318 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001319 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001320 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001321 }
1322}
1323
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001324static struct trace_sched_handler *trace_handler;
1325
1326static void
1327process_sched_wakeup_event(struct raw_event_sample *raw,
1328 struct event *event,
1329 int cpu __used,
1330 u64 timestamp __used,
1331 struct thread *thread __used)
1332{
1333 struct trace_wakeup_event wakeup_event;
1334
1335 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1336
1337 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1338 FILL_FIELD(wakeup_event, pid, event, raw->data);
1339 FILL_FIELD(wakeup_event, prio, event, raw->data);
1340 FILL_FIELD(wakeup_event, success, event, raw->data);
1341 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1342
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001343 if (trace_handler->wakeup_event)
1344 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001345}
1346
Ingo Molnarc8a37752009-09-16 14:07:00 +02001347/*
1348 * Track the current task - that way we can know whether there's any
1349 * weird events, such as a task being switched away that is not current.
1350 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001351static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001352
Ingo Molnarc8a37752009-09-16 14:07:00 +02001353static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1354
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001355static struct thread *curr_thread[MAX_CPUS];
1356
1357static char next_shortname1 = 'A';
1358static char next_shortname2 = '0';
1359
1360static void
1361map_switch_event(struct trace_switch_event *switch_event,
1362 struct event *event __used,
1363 int this_cpu,
1364 u64 timestamp,
1365 struct thread *thread __used)
1366{
1367 struct thread *sched_out, *sched_in;
1368 int new_shortname;
1369 u64 timestamp0;
1370 s64 delta;
1371 int cpu;
1372
1373 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1374
1375 if (this_cpu > max_cpu)
1376 max_cpu = this_cpu;
1377
1378 timestamp0 = cpu_last_switched[this_cpu];
1379 cpu_last_switched[this_cpu] = timestamp;
1380 if (timestamp0)
1381 delta = timestamp - timestamp0;
1382 else
1383 delta = 0;
1384
1385 if (delta < 0)
1386 die("hm, delta: %Ld < 0 ?\n", delta);
1387
1388
1389 sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match);
1390 sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
1391
1392 curr_thread[this_cpu] = sched_in;
1393
1394 printf(" ");
1395
1396 new_shortname = 0;
1397 if (!sched_in->shortname[0]) {
1398 sched_in->shortname[0] = next_shortname1;
1399 sched_in->shortname[1] = next_shortname2;
1400
1401 if (next_shortname1 < 'Z') {
1402 next_shortname1++;
1403 } else {
1404 next_shortname1='A';
1405 if (next_shortname2 < '9') {
1406 next_shortname2++;
1407 } else {
1408 next_shortname2='0';
1409 }
1410 }
1411 new_shortname = 1;
1412 }
1413
1414 for (cpu = 0; cpu <= max_cpu; cpu++) {
1415 if (cpu != this_cpu)
1416 printf(" ");
1417 else
1418 printf("*");
1419
1420 if (curr_thread[cpu]) {
1421 if (curr_thread[cpu]->pid)
1422 printf("%2s ", curr_thread[cpu]->shortname);
1423 else
1424 printf(". ");
1425 } else
1426 printf(" ");
1427 }
1428
1429 printf(" %12.6f secs ", (double)timestamp/1e9);
1430 if (new_shortname) {
1431 printf("%s => %s:%d\n",
1432 sched_in->shortname, sched_in->comm, sched_in->pid);
1433 } else {
1434 printf("\n");
1435 }
1436}
1437
1438
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001439static void
1440process_sched_switch_event(struct raw_event_sample *raw,
1441 struct event *event,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001442 int this_cpu,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001443 u64 timestamp __used,
1444 struct thread *thread __used)
1445{
1446 struct trace_switch_event switch_event;
1447
1448 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1449
1450 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1451 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1452 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1453 FILL_FIELD(switch_event, prev_state, event, raw->data);
1454 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1455 FILL_FIELD(switch_event, next_pid, event, raw->data);
1456 FILL_FIELD(switch_event, next_prio, event, raw->data);
1457
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001458 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001459 /*
1460 * Are we trying to switch away a PID that is
1461 * not current?
1462 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001463 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001464 nr_context_switch_bugs++;
1465 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001466 if (trace_handler->switch_event)
1467 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001468
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001470}
1471
1472static void
mingo39aeb522009-09-14 20:04:48 +02001473process_sched_runtime_event(struct raw_event_sample *raw,
1474 struct event *event,
1475 int cpu __used,
1476 u64 timestamp __used,
1477 struct thread *thread __used)
1478{
1479 struct trace_runtime_event runtime_event;
1480
1481 FILL_ARRAY(runtime_event, comm, event, raw->data);
1482 FILL_FIELD(runtime_event, pid, event, raw->data);
1483 FILL_FIELD(runtime_event, runtime, event, raw->data);
1484 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1485
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001486 if (trace_handler->runtime_event)
1487 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001488}
1489
1490static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001491process_sched_fork_event(struct raw_event_sample *raw,
1492 struct event *event,
1493 int cpu __used,
1494 u64 timestamp __used,
1495 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001496{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001497 struct trace_fork_event fork_event;
1498
1499 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1500
1501 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1502 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1503 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1504 FILL_FIELD(fork_event, child_pid, event, raw->data);
1505
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001506 if (trace_handler->fork_event)
1507 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001508}
1509
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001510static void
1511process_sched_exit_event(struct event *event,
1512 int cpu __used,
1513 u64 timestamp __used,
1514 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001515{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001516 if (verbose)
1517 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001518}
1519
1520static void
Ingo Molnarad236fd2009-09-11 12:12:54 +02001521process_raw_event(event_t *raw_event __used, void *more_data,
Ingo Molnarec156762009-09-11 12:12:54 +02001522 int cpu, u64 timestamp, struct thread *thread)
1523{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001524 struct raw_event_sample *raw = more_data;
Ingo Molnarec156762009-09-11 12:12:54 +02001525 struct event *event;
1526 int type;
1527
1528 type = trace_parse_common_type(raw->data);
1529 event = trace_find_event(type);
1530
Ingo Molnarec156762009-09-11 12:12:54 +02001531 if (!strcmp(event->name, "sched_switch"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001532 process_sched_switch_event(raw, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001533 if (!strcmp(event->name, "sched_stat_runtime"))
1534 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001535 if (!strcmp(event->name, "sched_wakeup"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001536 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001537 if (!strcmp(event->name, "sched_wakeup_new"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001538 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001539 if (!strcmp(event->name, "sched_process_fork"))
Frederic Weisbecker46538812009-09-12 02:43:45 +02001540 process_sched_fork_event(raw, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001541 if (!strcmp(event->name, "sched_process_exit"))
1542 process_sched_exit_event(event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001543}
1544
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001545static int
1546process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1547{
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001548 struct thread *thread;
1549 u64 ip = event->ip.ip;
1550 u64 timestamp = -1;
1551 u32 cpu = -1;
1552 u64 period = 1;
1553 void *more_data = event->ip.__more_data;
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001554
1555 if (!(sample_type & PERF_SAMPLE_RAW))
1556 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001557
1558 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1559
1560 if (sample_type & PERF_SAMPLE_TIME) {
1561 timestamp = *(u64 *)more_data;
1562 more_data += sizeof(u64);
1563 }
1564
1565 if (sample_type & PERF_SAMPLE_CPU) {
1566 cpu = *(u32 *)more_data;
1567 more_data += sizeof(u32);
1568 more_data += sizeof(u32); /* reserved */
1569 }
1570
1571 if (sample_type & PERF_SAMPLE_PERIOD) {
1572 period = *(u64 *)more_data;
1573 more_data += sizeof(u64);
1574 }
1575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001577 (void *)(offset + head),
1578 (void *)(long)(event->header.size),
1579 event->header.misc,
1580 event->ip.pid, event->ip.tid,
1581 (void *)(long)ip,
1582 (long long)period);
1583
1584 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1585
1586 if (thread == NULL) {
1587 eprintf("problem processing %d event, skipping it.\n",
1588 event->header.type);
1589 return -1;
1590 }
1591
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001592 process_raw_event(event, more_data, cpu, timestamp, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001593
1594 return 0;
1595}
1596
1597static int
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001598process_lost_event(event_t *event __used,
1599 unsigned long offset __used,
1600 unsigned long head __used)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001601{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001602 nr_lost_chunks++;
1603 nr_lost_events += event->lost.lost;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001604
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001605 return 0;
1606}
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001607
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001608static int sample_type_check(u64 type)
1609{
1610 sample_type = type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001611
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001612 if (!(sample_type & PERF_SAMPLE_RAW)) {
1613 fprintf(stderr,
1614 "No trace sample to read. Did you call perf record "
1615 "without -R?");
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001616 return -1;
1617 }
1618
1619 return 0;
1620}
1621
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001622static struct perf_file_handler file_handler = {
1623 .process_sample_event = process_sample_event,
1624 .process_comm_event = process_comm_event,
1625 .process_lost_event = process_lost_event,
1626 .sample_type_check = sample_type_check,
1627};
1628
Ingo Molnar46f392c2009-09-12 10:08:34 +02001629static int read_events(void)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001630{
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001631 register_idle_thread(&threads, &last_match);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001632 register_perf_file_handler(&file_handler);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001633
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001634 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001635}
1636
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001637static void print_bad_events(void)
1638{
1639 if (nr_unordered_timestamps && nr_timestamps) {
1640 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1641 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1642 nr_unordered_timestamps, nr_timestamps);
1643 }
1644 if (nr_lost_events && nr_events) {
1645 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1646 (double)nr_lost_events/(double)nr_events*100.0,
1647 nr_lost_events, nr_events, nr_lost_chunks);
1648 }
1649 if (nr_state_machine_bugs && nr_timestamps) {
1650 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1651 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1652 nr_state_machine_bugs, nr_timestamps);
1653 if (nr_lost_events)
1654 printf(" (due to lost events?)");
1655 printf("\n");
1656 }
1657 if (nr_context_switch_bugs && nr_timestamps) {
1658 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1659 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1660 nr_context_switch_bugs, nr_timestamps);
1661 if (nr_lost_events)
1662 printf(" (due to lost events?)");
1663 printf("\n");
1664 }
1665}
1666
1667static void __cmd_lat(void)
1668{
1669 struct rb_node *next;
1670
1671 setup_pager();
1672 read_events();
1673 sort_lat();
1674
1675 printf("\n -----------------------------------------------------------------------------------------\n");
1676 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1677 printf(" -----------------------------------------------------------------------------------------\n");
1678
1679 next = rb_first(&sorted_atom_root);
1680
1681 while (next) {
1682 struct work_atoms *work_list;
1683
1684 work_list = rb_entry(next, struct work_atoms, node);
1685 output_lat_thread(work_list);
1686 next = rb_next(next);
1687 }
1688
1689 printf(" -----------------------------------------------------------------------------------------\n");
1690 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1691 (double)all_runtime/1e6, all_count);
1692
1693 printf(" ---------------------------------------------------\n");
1694
1695 print_bad_events();
1696 printf("\n");
1697
1698}
1699
1700static struct trace_sched_handler map_ops = {
1701 .wakeup_event = NULL,
1702 .switch_event = map_switch_event,
1703 .runtime_event = NULL,
1704 .fork_event = NULL,
1705};
1706
1707static void __cmd_map(void)
1708{
Ingo Molnar40749d02009-09-17 18:24:55 +02001709 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1710
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001711 setup_pager();
1712 read_events();
1713 print_bad_events();
1714}
1715
1716static void __cmd_replay(void)
1717{
1718 unsigned long i;
1719
1720 calibrate_run_measurement_overhead();
1721 calibrate_sleep_measurement_overhead();
1722
1723 test_calibrations();
1724
1725 read_events();
1726
1727 printf("nr_run_events: %ld\n", nr_run_events);
1728 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1729 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1730
1731 if (targetless_wakeups)
1732 printf("target-less wakeups: %ld\n", targetless_wakeups);
1733 if (multitarget_wakeups)
1734 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1735 if (nr_run_events_optimized)
1736 printf("run atoms optimized: %ld\n",
1737 nr_run_events_optimized);
1738
1739 print_task_traces();
1740 add_cross_task_wakeups();
1741
1742 create_tasks();
1743 printf("------------------------------------------------------------\n");
1744 for (i = 0; i < replay_repeat; i++)
1745 run_one_test();
1746}
1747
1748
Ingo Molnar46f392c2009-09-12 10:08:34 +02001749static const char * const sched_usage[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001750 "perf sched [<options>] {record|latency|map|replay|trace}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001751 NULL
1752};
1753
Ingo Molnarf2858d82009-09-11 12:12:54 +02001754static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001755 OPT_STRING('i', "input", &input_name, "file",
1756 "input file name"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001757 OPT_BOOLEAN('v', "verbose", &verbose,
1758 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001759 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1760 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001761 OPT_END()
1762};
1763
1764static const char * const latency_usage[] = {
1765 "perf sched latency [<options>]",
1766 NULL
1767};
1768
1769static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001770 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1771 "sort by key(s): runtime, switch, avg, max"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001772 OPT_BOOLEAN('v', "verbose", &verbose,
1773 "be more verbose (show symbol address, etc)"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001774 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1775 "dump raw trace in ASCII"),
1776 OPT_END()
1777};
1778
1779static const char * const replay_usage[] = {
1780 "perf sched replay [<options>]",
1781 NULL
1782};
1783
1784static const struct option replay_options[] = {
1785 OPT_INTEGER('r', "repeat", &replay_repeat,
1786 "repeat the workload replay N times (-1: infinite)"),
1787 OPT_BOOLEAN('v', "verbose", &verbose,
1788 "be more verbose (show symbol address, etc)"),
1789 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1790 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001791 OPT_END()
1792};
1793
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001794static void setup_sorting(void)
1795{
1796 char *tmp, *tok, *str = strdup(sort_order);
1797
1798 for (tok = strtok_r(str, ", ", &tmp);
1799 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1800 if (sort_dimension__add(tok, &sort_list) < 0) {
1801 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001802 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001803 }
1804 }
1805
1806 free(str);
1807
1808 sort_dimension__add((char *)"pid", &cmp_pid);
1809}
1810
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001811static const char *record_args[] = {
1812 "record",
1813 "-a",
1814 "-R",
Frederic Weisbeckerd1302522009-09-14 08:57:15 +02001815 "-M",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001816 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001817 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001818 "-c", "1",
1819 "-e", "sched:sched_switch:r",
1820 "-e", "sched:sched_stat_wait:r",
1821 "-e", "sched:sched_stat_sleep:r",
1822 "-e", "sched:sched_stat_iowait:r",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001823 "-e", "sched:sched_stat_runtime:r",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001824 "-e", "sched:sched_process_exit:r",
1825 "-e", "sched:sched_process_fork:r",
1826 "-e", "sched:sched_wakeup:r",
1827 "-e", "sched:sched_migrate_task:r",
1828};
1829
1830static int __cmd_record(int argc, const char **argv)
1831{
1832 unsigned int rec_argc, i, j;
1833 const char **rec_argv;
1834
1835 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1836 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1837
1838 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1839 rec_argv[i] = strdup(record_args[i]);
1840
1841 for (j = 1; j < (unsigned int)argc; j++, i++)
1842 rec_argv[i] = argv[j];
1843
1844 BUG_ON(i != rec_argc);
1845
1846 return cmd_record(i, rec_argv, NULL);
1847}
1848
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001849int cmd_sched(int argc, const char **argv, const char *prefix __used)
1850{
1851 symbol__init();
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001852
Ingo Molnarf2858d82009-09-11 12:12:54 +02001853 argc = parse_options(argc, argv, sched_options, sched_usage,
1854 PARSE_OPT_STOP_AT_NON_OPTION);
1855 if (!argc)
1856 usage_with_options(sched_usage, sched_options);
1857
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001858 if (!strncmp(argv[0], "rec", 3)) {
1859 return __cmd_record(argc, argv);
1860 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001861 trace_handler = &lat_ops;
1862 if (argc > 1) {
1863 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1864 if (argc)
1865 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001866 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001867 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001868 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001869 } else if (!strcmp(argv[0], "map")) {
1870 trace_handler = &map_ops;
1871 setup_sorting();
1872 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001873 } else if (!strncmp(argv[0], "rep", 3)) {
1874 trace_handler = &replay_ops;
1875 if (argc) {
1876 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1877 if (argc)
1878 usage_with_options(replay_usage, replay_options);
1879 }
1880 __cmd_replay();
Ingo Molnarc13f0d32009-09-13 16:51:04 +02001881 } else if (!strcmp(argv[0], "trace")) {
1882 /*
1883 * Aliased to 'perf trace' for now:
1884 */
1885 return cmd_trace(argc, argv, prefix);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001886 } else {
1887 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001888 }
1889
Ingo Molnarec156762009-09-11 12:12:54 +02001890 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001891}