blob: 19f43faa9f810cd4786ca3e8e64fb9a2a9484735 [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 struct perf_header *header;
26static u64 sample_type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020027
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020028static char default_sort_order[] = "avg, max, switch, runtime";
29static char *sort_order = default_sort_order;
30
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020031static int profile_cpu = -1;
32
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020033#define PR_SET_NAME 15 /* Set process name */
34#define MAX_CPUS 4096
Ingo Molnar0a02ad92009-09-11 12:12:54 +020035
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020036static u64 run_measurement_overhead;
37static u64 sleep_measurement_overhead;
Ingo Molnarec156762009-09-11 12:12:54 +020038
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020039#define COMM_LEN 20
40#define SYM_LEN 129
Ingo Molnarec156762009-09-11 12:12:54 +020041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020042#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020043
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020044static unsigned long nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +020045
mingo39aeb522009-09-14 20:04:48 +020046struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020047
48struct task_desc {
49 unsigned long nr;
50 unsigned long pid;
51 char comm[COMM_LEN];
52
53 unsigned long nr_events;
54 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020055 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020056
57 pthread_t thread;
58 sem_t sleep_sem;
59
60 sem_t ready_for_work;
61 sem_t work_done_sem;
62
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020063 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020064};
65
66enum sched_event_type {
67 SCHED_EVENT_RUN,
68 SCHED_EVENT_SLEEP,
69 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020070 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020071};
72
mingo39aeb522009-09-14 20:04:48 +020073struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020074 enum sched_event_type type;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020075 u64 timestamp;
76 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020077 unsigned long nr;
78 int specific_wait;
79 sem_t *wait_sem;
80 struct task_desc *wakee;
81};
82
83static struct task_desc *pid_to_task[MAX_PID];
84
85static struct task_desc **tasks;
86
87static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020088static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020089
90static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
91
92static unsigned long nr_run_events;
93static unsigned long nr_sleep_events;
94static unsigned long nr_wakeup_events;
95
96static unsigned long nr_sleep_corrections;
97static unsigned long nr_run_events_optimized;
98
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020099static unsigned long targetless_wakeups;
100static unsigned long multitarget_wakeups;
101
102static u64 cpu_usage;
103static u64 runavg_cpu_usage;
104static u64 parent_cpu_usage;
105static u64 runavg_parent_cpu_usage;
106
107static unsigned long nr_runs;
108static u64 sum_runtime;
109static u64 sum_fluct;
110static u64 run_avg;
111
112static unsigned long replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200113static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200114static unsigned long nr_unordered_timestamps;
115static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200116static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200117static unsigned long nr_events;
118static unsigned long nr_lost_chunks;
119static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200120
121#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
122
123enum thread_state {
124 THREAD_SLEEPING = 0,
125 THREAD_WAIT_CPU,
126 THREAD_SCHED_IN,
127 THREAD_IGNORE
128};
129
130struct work_atom {
131 struct list_head list;
132 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200133 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200134 u64 wake_up_time;
135 u64 sched_in_time;
136 u64 runtime;
137};
138
mingo39aeb522009-09-14 20:04:48 +0200139struct work_atoms {
140 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200141 struct thread *thread;
142 struct rb_node node;
143 u64 max_lat;
144 u64 total_lat;
145 u64 nb_atoms;
146 u64 total_runtime;
147};
148
mingo39aeb522009-09-14 20:04:48 +0200149typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200150
151static struct rb_root atom_root, sorted_atom_root;
152
153static u64 all_runtime;
154static u64 all_count;
155
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200156
157static u64 get_nsecs(void)
158{
159 struct timespec ts;
160
161 clock_gettime(CLOCK_MONOTONIC, &ts);
162
163 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
164}
165
166static void burn_nsecs(u64 nsecs)
167{
168 u64 T0 = get_nsecs(), T1;
169
170 do {
171 T1 = get_nsecs();
172 } while (T1 + run_measurement_overhead < T0 + nsecs);
173}
174
175static void sleep_nsecs(u64 nsecs)
176{
177 struct timespec ts;
178
179 ts.tv_nsec = nsecs % 999999999;
180 ts.tv_sec = nsecs / 999999999;
181
182 nanosleep(&ts, NULL);
183}
184
185static void calibrate_run_measurement_overhead(void)
186{
187 u64 T0, T1, delta, min_delta = 1000000000ULL;
188 int i;
189
190 for (i = 0; i < 10; i++) {
191 T0 = get_nsecs();
192 burn_nsecs(0);
193 T1 = get_nsecs();
194 delta = T1-T0;
195 min_delta = min(min_delta, delta);
196 }
197 run_measurement_overhead = min_delta;
198
199 printf("run measurement overhead: %Ld nsecs\n", min_delta);
200}
201
202static void calibrate_sleep_measurement_overhead(void)
203{
204 u64 T0, T1, delta, min_delta = 1000000000ULL;
205 int i;
206
207 for (i = 0; i < 10; i++) {
208 T0 = get_nsecs();
209 sleep_nsecs(10000);
210 T1 = get_nsecs();
211 delta = T1-T0;
212 min_delta = min(min_delta, delta);
213 }
214 min_delta -= 10000;
215 sleep_measurement_overhead = min_delta;
216
217 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
218}
219
mingo39aeb522009-09-14 20:04:48 +0200220static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200221get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200222{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200223 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200224 unsigned long idx = task->nr_events;
225 size_t size;
226
227 event->timestamp = timestamp;
228 event->nr = idx;
229
230 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200231 size = sizeof(struct sched_atom *) * task->nr_events;
232 task->atoms = realloc(task->atoms, size);
233 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200234
mingo39aeb522009-09-14 20:04:48 +0200235 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200236
237 return event;
238}
239
mingo39aeb522009-09-14 20:04:48 +0200240static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200241{
242 if (!task->nr_events)
243 return NULL;
244
mingo39aeb522009-09-14 20:04:48 +0200245 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200246}
247
248static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200249add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200250{
mingo39aeb522009-09-14 20:04:48 +0200251 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200252
253 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200254 * optimize an existing RUN event by merging this one
255 * to it:
256 */
Ingo Molnarec156762009-09-11 12:12:54 +0200257 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
258 nr_run_events_optimized++;
259 curr_event->duration += duration;
260 return;
261 }
262
263 event = get_new_event(task, timestamp);
264
265 event->type = SCHED_EVENT_RUN;
266 event->duration = duration;
267
268 nr_run_events++;
269}
270
Ingo Molnarec156762009-09-11 12:12:54 +0200271static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200272add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200273 struct task_desc *wakee)
274{
mingo39aeb522009-09-14 20:04:48 +0200275 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200276
277 event = get_new_event(task, timestamp);
278 event->type = SCHED_EVENT_WAKEUP;
279 event->wakee = wakee;
280
281 wakee_event = last_event(wakee);
282 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
283 targetless_wakeups++;
284 return;
285 }
286 if (wakee_event->wait_sem) {
287 multitarget_wakeups++;
288 return;
289 }
290
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200291 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200292 sem_init(wakee_event->wait_sem, 0, 0);
293 wakee_event->specific_wait = 1;
294 event->wait_sem = wakee_event->wait_sem;
295
296 nr_wakeup_events++;
297}
298
299static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200300add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200301 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200302{
mingo39aeb522009-09-14 20:04:48 +0200303 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200304
305 event->type = SCHED_EVENT_SLEEP;
306
307 nr_sleep_events++;
308}
309
310static struct task_desc *register_pid(unsigned long pid, const char *comm)
311{
312 struct task_desc *task;
313
314 BUG_ON(pid >= MAX_PID);
315
316 task = pid_to_task[pid];
317
318 if (task)
319 return task;
320
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200321 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200322 task->pid = pid;
323 task->nr = nr_tasks;
324 strcpy(task->comm, comm);
325 /*
326 * every task starts in sleeping state - this gets ignored
327 * if there's no wakeup pointing to this sleep state:
328 */
329 add_sched_event_sleep(task, 0, 0);
330
331 pid_to_task[pid] = task;
332 nr_tasks++;
333 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
334 BUG_ON(!tasks);
335 tasks[task->nr] = task;
336
Ingo Molnarad236fd2009-09-11 12:12:54 +0200337 if (verbose)
338 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200339
340 return task;
341}
342
343
Ingo Molnarec156762009-09-11 12:12:54 +0200344static void print_task_traces(void)
345{
346 struct task_desc *task;
347 unsigned long i;
348
349 for (i = 0; i < nr_tasks; i++) {
350 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200351 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200352 task->nr, task->comm, task->pid, task->nr_events);
353 }
354}
355
356static void add_cross_task_wakeups(void)
357{
358 struct task_desc *task1, *task2;
359 unsigned long i, j;
360
361 for (i = 0; i < nr_tasks; i++) {
362 task1 = tasks[i];
363 j = i + 1;
364 if (j == nr_tasks)
365 j = 0;
366 task2 = tasks[j];
367 add_sched_event_wakeup(task1, 0, task2);
368 }
369}
370
371static void
mingo39aeb522009-09-14 20:04:48 +0200372process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200373{
374 int ret = 0;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200375 u64 now;
Ingo Molnarec156762009-09-11 12:12:54 +0200376 long long delta;
377
378 now = get_nsecs();
mingo39aeb522009-09-14 20:04:48 +0200379 delta = start_time + atom->timestamp - now;
Ingo Molnarec156762009-09-11 12:12:54 +0200380
mingo39aeb522009-09-14 20:04:48 +0200381 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200382 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200383 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200384 break;
385 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200386 if (atom->wait_sem)
387 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200388 BUG_ON(ret);
389 break;
390 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200391 if (atom->wait_sem)
392 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200393 BUG_ON(ret);
394 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200395 case SCHED_EVENT_MIGRATION:
396 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200397 default:
398 BUG_ON(1);
399 }
400}
401
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200402static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200403{
404 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200405 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200406 int err;
407
408 err = getrusage(RUSAGE_SELF, &ru);
409 BUG_ON(err);
410
411 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
412 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
413
414 return sum;
415}
416
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200417static u64 get_cpu_usage_nsec_self(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200418{
419 char filename [] = "/proc/1234567890/sched";
420 unsigned long msecs, nsecs;
421 char *line = NULL;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200422 u64 total = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200423 size_t len = 0;
424 ssize_t chars;
425 FILE *file;
426 int ret;
427
428 sprintf(filename, "/proc/%d/sched", getpid());
429 file = fopen(filename, "r");
430 BUG_ON(!file);
431
432 while ((chars = getline(&line, &len, file)) != -1) {
Ingo Molnarec156762009-09-11 12:12:54 +0200433 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
434 &msecs, &nsecs);
435 if (ret == 2) {
436 total = msecs*1e6 + nsecs;
Ingo Molnarec156762009-09-11 12:12:54 +0200437 break;
438 }
439 }
440 if (line)
441 free(line);
442 fclose(file);
443
444 return total;
445}
446
447static void *thread_func(void *ctx)
448{
449 struct task_desc *this_task = ctx;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200450 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200451 unsigned long i, ret;
452 char comm2[22];
453
Ingo Molnarec156762009-09-11 12:12:54 +0200454 sprintf(comm2, ":%s", this_task->comm);
455 prctl(PR_SET_NAME, comm2);
456
457again:
458 ret = sem_post(&this_task->ready_for_work);
459 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200460 ret = pthread_mutex_lock(&start_work_mutex);
461 BUG_ON(ret);
462 ret = pthread_mutex_unlock(&start_work_mutex);
463 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200464
465 cpu_usage_0 = get_cpu_usage_nsec_self();
466
467 for (i = 0; i < this_task->nr_events; i++) {
468 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200469 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200470 }
471
472 cpu_usage_1 = get_cpu_usage_nsec_self();
473 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
474
Ingo Molnarec156762009-09-11 12:12:54 +0200475 ret = sem_post(&this_task->work_done_sem);
476 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200477
478 ret = pthread_mutex_lock(&work_done_wait_mutex);
479 BUG_ON(ret);
480 ret = pthread_mutex_unlock(&work_done_wait_mutex);
481 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200482
483 goto again;
484}
485
486static void create_tasks(void)
487{
488 struct task_desc *task;
489 pthread_attr_t attr;
490 unsigned long i;
491 int err;
492
493 err = pthread_attr_init(&attr);
494 BUG_ON(err);
495 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
496 BUG_ON(err);
497 err = pthread_mutex_lock(&start_work_mutex);
498 BUG_ON(err);
499 err = pthread_mutex_lock(&work_done_wait_mutex);
500 BUG_ON(err);
501 for (i = 0; i < nr_tasks; i++) {
502 task = tasks[i];
503 sem_init(&task->sleep_sem, 0, 0);
504 sem_init(&task->ready_for_work, 0, 0);
505 sem_init(&task->work_done_sem, 0, 0);
506 task->curr_event = 0;
507 err = pthread_create(&task->thread, &attr, thread_func, task);
508 BUG_ON(err);
509 }
510}
511
Ingo Molnarec156762009-09-11 12:12:54 +0200512static void wait_for_tasks(void)
513{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200514 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200515 struct task_desc *task;
516 unsigned long i, ret;
517
Ingo Molnarec156762009-09-11 12:12:54 +0200518 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200519 cpu_usage = 0;
520 pthread_mutex_unlock(&work_done_wait_mutex);
521
522 for (i = 0; i < nr_tasks; i++) {
523 task = tasks[i];
524 ret = sem_wait(&task->ready_for_work);
525 BUG_ON(ret);
526 sem_init(&task->ready_for_work, 0, 0);
527 }
528 ret = pthread_mutex_lock(&work_done_wait_mutex);
529 BUG_ON(ret);
530
531 cpu_usage_0 = get_cpu_usage_nsec_parent();
532
533 pthread_mutex_unlock(&start_work_mutex);
534
Ingo Molnarec156762009-09-11 12:12:54 +0200535 for (i = 0; i < nr_tasks; i++) {
536 task = tasks[i];
537 ret = sem_wait(&task->work_done_sem);
538 BUG_ON(ret);
539 sem_init(&task->work_done_sem, 0, 0);
540 cpu_usage += task->cpu_usage;
541 task->cpu_usage = 0;
542 }
543
544 cpu_usage_1 = get_cpu_usage_nsec_parent();
545 if (!runavg_cpu_usage)
546 runavg_cpu_usage = cpu_usage;
547 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
548
549 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
550 if (!runavg_parent_cpu_usage)
551 runavg_parent_cpu_usage = parent_cpu_usage;
552 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
553 parent_cpu_usage)/10;
554
555 ret = pthread_mutex_lock(&start_work_mutex);
556 BUG_ON(ret);
557
558 for (i = 0; i < nr_tasks; i++) {
559 task = tasks[i];
560 sem_init(&task->sleep_sem, 0, 0);
561 task->curr_event = 0;
562 }
563}
564
Ingo Molnarec156762009-09-11 12:12:54 +0200565static void run_one_test(void)
566{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200567 u64 T0, T1, delta, avg_delta, fluct, std_dev;
Ingo Molnarec156762009-09-11 12:12:54 +0200568
569 T0 = get_nsecs();
570 wait_for_tasks();
571 T1 = get_nsecs();
572
573 delta = T1 - T0;
574 sum_runtime += delta;
575 nr_runs++;
576
577 avg_delta = sum_runtime / nr_runs;
578 if (delta < avg_delta)
579 fluct = avg_delta - delta;
580 else
581 fluct = delta - avg_delta;
582 sum_fluct += fluct;
583 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
584 if (!run_avg)
585 run_avg = delta;
586 run_avg = (run_avg*9 + delta)/10;
587
Ingo Molnarad236fd2009-09-11 12:12:54 +0200588 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200589 nr_runs, (double)delta/1000000.0);
590
Ingo Molnarad236fd2009-09-11 12:12:54 +0200591 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200592 (double)run_avg/1e6);
593
Ingo Molnarad236fd2009-09-11 12:12:54 +0200594 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200595 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
596
597#if 0
598 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200599 * rusage statistics done by the parent, these are less
600 * accurate than the sum_exec_runtime based statistics:
601 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200602 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200603 (double)parent_cpu_usage/1e6,
604 (double)runavg_parent_cpu_usage/1e6);
605#endif
606
Ingo Molnarad236fd2009-09-11 12:12:54 +0200607 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200608
609 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200610 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200611 nr_sleep_corrections = 0;
612}
613
614static void test_calibrations(void)
615{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200616 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200617
618 T0 = get_nsecs();
619 burn_nsecs(1e6);
620 T1 = get_nsecs();
621
Ingo Molnarad236fd2009-09-11 12:12:54 +0200622 printf("the run test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200623
624 T0 = get_nsecs();
625 sleep_nsecs(1e6);
626 T1 = get_nsecs();
627
Ingo Molnarad236fd2009-09-11 12:12:54 +0200628 printf("the sleep test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200629}
630
Frederic Weisbecker46538812009-09-12 02:43:45 +0200631#define FILL_FIELD(ptr, field, event, data) \
632 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
633
634#define FILL_ARRAY(ptr, array, event, data) \
635do { \
636 void *__array = raw_field_ptr(event, #array, data); \
637 memcpy(ptr.array, __array, sizeof(ptr.array)); \
638} while(0)
639
640#define FILL_COMMON_FIELDS(ptr, event, data) \
641do { \
642 FILL_FIELD(ptr, common_type, event, data); \
643 FILL_FIELD(ptr, common_flags, event, data); \
644 FILL_FIELD(ptr, common_preempt_count, event, data); \
645 FILL_FIELD(ptr, common_pid, event, data); \
646 FILL_FIELD(ptr, common_tgid, event, data); \
647} while (0)
648
Ingo Molnarfbf94822009-09-11 12:12:54 +0200649
Ingo Molnarec156762009-09-11 12:12:54 +0200650
Ingo Molnarfbf94822009-09-11 12:12:54 +0200651struct trace_switch_event {
652 u32 size;
653
654 u16 common_type;
655 u8 common_flags;
656 u8 common_preempt_count;
657 u32 common_pid;
658 u32 common_tgid;
659
660 char prev_comm[16];
661 u32 prev_pid;
662 u32 prev_prio;
663 u64 prev_state;
664 char next_comm[16];
665 u32 next_pid;
666 u32 next_prio;
667};
668
mingo39aeb522009-09-14 20:04:48 +0200669struct trace_runtime_event {
670 u32 size;
671
672 u16 common_type;
673 u8 common_flags;
674 u8 common_preempt_count;
675 u32 common_pid;
676 u32 common_tgid;
677
678 char comm[16];
679 u32 pid;
680 u64 runtime;
681 u64 vruntime;
682};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200683
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200684struct trace_wakeup_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 comm[16];
694 u32 pid;
695
696 u32 prio;
697 u32 success;
698 u32 cpu;
699};
700
701struct trace_fork_event {
702 u32 size;
703
704 u16 common_type;
705 u8 common_flags;
706 u8 common_preempt_count;
707 u32 common_pid;
708 u32 common_tgid;
709
710 char parent_comm[16];
711 u32 parent_pid;
712 char child_comm[16];
713 u32 child_pid;
714};
715
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200716struct trace_migrate_task_event {
717 u32 size;
718
719 u16 common_type;
720 u8 common_flags;
721 u8 common_preempt_count;
722 u32 common_pid;
723 u32 common_tgid;
724
725 char comm[16];
726 u32 pid;
727
728 u32 prio;
729 u32 cpu;
730};
731
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200732struct trace_sched_handler {
733 void (*switch_event)(struct trace_switch_event *,
734 struct event *,
735 int cpu,
736 u64 timestamp,
737 struct thread *thread);
738
mingo39aeb522009-09-14 20:04:48 +0200739 void (*runtime_event)(struct trace_runtime_event *,
740 struct event *,
741 int cpu,
742 u64 timestamp,
743 struct thread *thread);
744
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200745 void (*wakeup_event)(struct trace_wakeup_event *,
746 struct event *,
747 int cpu,
748 u64 timestamp,
749 struct thread *thread);
750
751 void (*fork_event)(struct trace_fork_event *,
752 struct event *,
753 int cpu,
754 u64 timestamp,
755 struct thread *thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200756
757 void (*migrate_task_event)(struct trace_migrate_task_event *,
758 struct event *,
759 int cpu,
760 u64 timestamp,
761 struct thread *thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200762};
763
Ingo Molnarfbf94822009-09-11 12:12:54 +0200764
765static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200766replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
767 struct event *event,
768 int cpu __used,
769 u64 timestamp __used,
770 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200771{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200772 struct task_desc *waker, *wakee;
773
774 if (verbose) {
775 printf("sched_wakeup event %p\n", event);
776
777 printf(" ... pid %d woke up %s/%d\n",
778 wakeup_event->common_pid,
779 wakeup_event->comm,
780 wakeup_event->pid);
781 }
782
783 waker = register_pid(wakeup_event->common_pid, "<unknown>");
784 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
785
786 add_sched_event_wakeup(waker, timestamp, wakee);
787}
788
Ingo Molnard1153382009-09-14 18:22:53 +0200789static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200790
791static void
792replay_switch_event(struct trace_switch_event *switch_event,
793 struct event *event,
794 int cpu,
795 u64 timestamp,
796 struct thread *thread __used)
797{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200798 struct task_desc *prev, *next;
799 u64 timestamp0;
800 s64 delta;
801
Ingo Molnarad236fd2009-09-11 12:12:54 +0200802 if (verbose)
803 printf("sched_switch event %p\n", event);
804
Ingo Molnarfbf94822009-09-11 12:12:54 +0200805 if (cpu >= MAX_CPUS || cpu < 0)
806 return;
807
808 timestamp0 = cpu_last_switched[cpu];
809 if (timestamp0)
810 delta = timestamp - timestamp0;
811 else
812 delta = 0;
813
814 if (delta < 0)
815 die("hm, delta: %Ld < 0 ?\n", delta);
816
Ingo Molnarad236fd2009-09-11 12:12:54 +0200817 if (verbose) {
818 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200819 switch_event->prev_comm, switch_event->prev_pid,
820 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200821 delta);
822 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200823
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200824 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
825 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200826
827 cpu_last_switched[cpu] = timestamp;
828
829 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200830 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200831}
832
Ingo Molnarfbf94822009-09-11 12:12:54 +0200833
834static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200835replay_fork_event(struct trace_fork_event *fork_event,
836 struct event *event,
837 int cpu __used,
838 u64 timestamp __used,
839 struct thread *thread __used)
840{
841 if (verbose) {
842 printf("sched_fork event %p\n", event);
843 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
844 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
845 }
846 register_pid(fork_event->parent_pid, fork_event->parent_comm);
847 register_pid(fork_event->child_pid, fork_event->child_comm);
848}
849
850static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200851 .wakeup_event = replay_wakeup_event,
852 .switch_event = replay_switch_event,
853 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200854};
855
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200856struct sort_dimension {
857 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200858 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200859 struct list_head list;
860};
861
862static LIST_HEAD(cmp_pid);
863
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200864static int
mingo39aeb522009-09-14 20:04:48 +0200865thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200866{
867 struct sort_dimension *sort;
868 int ret = 0;
869
Ingo Molnarb5fae122009-09-11 12:12:54 +0200870 BUG_ON(list_empty(list));
871
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200872 list_for_each_entry(sort, list, list) {
873 ret = sort->cmp(l, r);
874 if (ret)
875 return ret;
876 }
877
878 return ret;
879}
880
mingo39aeb522009-09-14 20:04:48 +0200881static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200882thread_atoms_search(struct rb_root *root, struct thread *thread,
883 struct list_head *sort_list)
884{
885 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200886 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200887
888 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200889 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200890 int cmp;
891
mingo39aeb522009-09-14 20:04:48 +0200892 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200893
894 cmp = thread_lat_cmp(sort_list, &key, atoms);
895 if (cmp > 0)
896 node = node->rb_left;
897 else if (cmp < 0)
898 node = node->rb_right;
899 else {
900 BUG_ON(thread != atoms->thread);
901 return atoms;
902 }
903 }
904 return NULL;
905}
906
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907static void
mingo39aeb522009-09-14 20:04:48 +0200908__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200909 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910{
911 struct rb_node **new = &(root->rb_node), *parent = NULL;
912
913 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200914 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200915 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200916
mingo39aeb522009-09-14 20:04:48 +0200917 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200918 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200919
920 cmp = thread_lat_cmp(sort_list, data, this);
921
922 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200924 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200925 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200926 }
927
928 rb_link_node(&data->node, parent, new);
929 rb_insert_color(&data->node, root);
930}
931
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200932static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200934 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Frederic Weisbecker17562202009-09-12 23:11:32 +0200935 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200936 die("No memory");
937
Frederic Weisbecker17562202009-09-12 23:11:32 +0200938 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200939 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200940 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941}
942
943static void
944latency_fork_event(struct trace_fork_event *fork_event __used,
945 struct event *event __used,
946 int cpu __used,
947 u64 timestamp __used,
948 struct thread *thread __used)
949{
950 /* should insert the newcomer */
951}
952
Ingo Molnarea92ed52009-09-12 10:08:34 +0200953__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954static char sched_out_state(struct trace_switch_event *switch_event)
955{
956 const char *str = TASK_STATE_TO_CHAR_STR;
957
958 return str[switch_event->prev_state];
959}
960
961static void
mingo39aeb522009-09-14 20:04:48 +0200962add_sched_out_event(struct work_atoms *atoms,
963 char run_state,
964 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200965{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200966 struct work_atom *atom = zalloc(sizeof(*atom));
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200967 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200968 die("Non memory");
969
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200970 atom->sched_out_time = timestamp;
971
mingo39aeb522009-09-14 20:04:48 +0200972 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200973 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200974 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200975 }
976
mingo39aeb522009-09-14 20:04:48 +0200977 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978}
979
980static void
mingo39aeb522009-09-14 20:04:48 +0200981add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
982{
983 struct work_atom *atom;
984
985 BUG_ON(list_empty(&atoms->work_list));
986
987 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
988
989 atom->runtime += delta;
990 atoms->total_runtime += delta;
991}
992
993static void
994add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200995{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200996 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200997 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200998
mingo39aeb522009-09-14 20:04:48 +0200999 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001000 return;
1001
mingo39aeb522009-09-14 20:04:48 +02001002 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001003
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001004 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001005 return;
1006
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001007 if (timestamp < atom->wake_up_time) {
1008 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001009 return;
1010 }
1011
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001012 atom->state = THREAD_SCHED_IN;
1013 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001014
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001015 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001016 atoms->total_lat += delta;
1017 if (delta > atoms->max_lat)
1018 atoms->max_lat = delta;
1019 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001020}
1021
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001022static void
1023latency_switch_event(struct trace_switch_event *switch_event,
1024 struct event *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001025 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001026 u64 timestamp,
1027 struct thread *thread __used)
1028{
mingo39aeb522009-09-14 20:04:48 +02001029 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001031 u64 timestamp0;
1032 s64 delta;
1033
mingo39aeb522009-09-14 20:04:48 +02001034 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001035
1036 timestamp0 = cpu_last_switched[cpu];
1037 cpu_last_switched[cpu] = timestamp;
1038 if (timestamp0)
1039 delta = timestamp - timestamp0;
1040 else
1041 delta = 0;
1042
1043 if (delta < 0)
1044 die("hm, delta: %Ld < 0 ?\n", delta);
1045
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001046
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001047 sched_out = threads__findnew(switch_event->prev_pid);
1048 sched_in = threads__findnew(switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001049
mingo39aeb522009-09-14 20:04:48 +02001050 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1051 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001052 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001053 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1054 if (!out_events)
1055 die("out-event: Internal tree error");
1056 }
1057 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1058
1059 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1060 if (!in_events) {
1061 thread_atoms_insert(sched_in);
1062 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1063 if (!in_events)
1064 die("in-event: Internal tree error");
1065 /*
1066 * Take came in we have not heard about yet,
1067 * add in an initial atom in runnable state:
1068 */
1069 add_sched_out_event(in_events, 'R', timestamp);
1070 }
1071 add_sched_in_event(in_events, timestamp);
1072}
1073
1074static void
1075latency_runtime_event(struct trace_runtime_event *runtime_event,
1076 struct event *event __used,
1077 int cpu,
1078 u64 timestamp,
1079 struct thread *this_thread __used)
1080{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001081 struct thread *thread = threads__findnew(runtime_event->pid);
1082 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001083
1084 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001085 if (!atoms) {
1086 thread_atoms_insert(thread);
1087 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1088 if (!atoms)
1089 die("in-event: Internal tree error");
1090 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001091 }
1092
mingo39aeb522009-09-14 20:04:48 +02001093 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001094}
1095
1096static void
1097latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
mingo39aeb522009-09-14 20:04:48 +02001098 struct event *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001099 int cpu __used,
1100 u64 timestamp,
1101 struct thread *thread __used)
1102{
mingo39aeb522009-09-14 20:04:48 +02001103 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001104 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001105 struct thread *wakee;
1106
1107 /* Note for later, it may be interesting to observe the failing cases */
1108 if (!wakeup_event->success)
1109 return;
1110
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001111 wakee = threads__findnew(wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001112 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001113 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001114 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001115 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1116 if (!atoms)
1117 die("wakeup-event: Internal tree error");
1118 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001119 }
1120
mingo39aeb522009-09-14 20:04:48 +02001121 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001122
mingo39aeb522009-09-14 20:04:48 +02001123 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001125 /*
1126 * You WILL be missing events if you've recorded only
1127 * one CPU, or are only looking at only one, so don't
1128 * make useless noise.
1129 */
1130 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001131 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001133 nr_timestamps++;
1134 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001135 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001136 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001137 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001138
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001139 atom->state = THREAD_WAIT_CPU;
1140 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001141}
1142
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001143static void
1144latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1145 struct event *__event __used,
1146 int cpu __used,
1147 u64 timestamp,
1148 struct thread *thread __used)
1149{
1150 struct work_atoms *atoms;
1151 struct work_atom *atom;
1152 struct thread *migrant;
1153
1154 /*
1155 * Only need to worry about migration when profiling one CPU.
1156 */
1157 if (profile_cpu == -1)
1158 return;
1159
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001160 migrant = threads__findnew(migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001161 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1162 if (!atoms) {
1163 thread_atoms_insert(migrant);
1164 register_pid(migrant->pid, migrant->comm);
1165 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1166 if (!atoms)
1167 die("migration-event: Internal tree error");
1168 add_sched_out_event(atoms, 'R', timestamp);
1169 }
1170
1171 BUG_ON(list_empty(&atoms->work_list));
1172
1173 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1174 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1175
1176 nr_timestamps++;
1177
1178 if (atom->sched_out_time > timestamp)
1179 nr_unordered_timestamps++;
1180}
1181
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001182static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001183 .wakeup_event = latency_wakeup_event,
1184 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001185 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001186 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001187 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001188};
1189
mingo39aeb522009-09-14 20:04:48 +02001190static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001191{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001192 int i;
1193 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001194 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001195
mingo39aeb522009-09-14 20:04:48 +02001196 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001197 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001198 /*
1199 * Ignore idle threads:
1200 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001201 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001202 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203
mingo39aeb522009-09-14 20:04:48 +02001204 all_runtime += work_list->total_runtime;
1205 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001206
Ingo Molnar80ed0982009-09-16 14:12:36 +02001207 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001208
mingo08f69e62009-09-14 18:30:44 +02001209 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001210 printf(" ");
1211
mingo39aeb522009-09-14 20:04:48 +02001212 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001213
Ingo Molnardc02bf72009-09-16 13:45:00 +02001214 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
mingo39aeb522009-09-14 20:04:48 +02001215 (double)work_list->total_runtime / 1e6,
1216 work_list->nb_atoms, (double)avg / 1e6,
1217 (double)work_list->max_lat / 1e6);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001218}
1219
mingo39aeb522009-09-14 20:04:48 +02001220static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001221{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001222 if (l->thread->pid < r->thread->pid)
1223 return -1;
1224 if (l->thread->pid > r->thread->pid)
1225 return 1;
1226
1227 return 0;
1228}
1229
1230static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001231 .name = "pid",
1232 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001233};
1234
mingo39aeb522009-09-14 20:04:48 +02001235static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001236{
1237 u64 avgl, avgr;
1238
1239 if (!l->nb_atoms)
1240 return -1;
1241
1242 if (!r->nb_atoms)
1243 return 1;
1244
1245 avgl = l->total_lat / l->nb_atoms;
1246 avgr = r->total_lat / r->nb_atoms;
1247
1248 if (avgl < avgr)
1249 return -1;
1250 if (avgl > avgr)
1251 return 1;
1252
1253 return 0;
1254}
1255
1256static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001257 .name = "avg",
1258 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001259};
1260
mingo39aeb522009-09-14 20:04:48 +02001261static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001262{
1263 if (l->max_lat < r->max_lat)
1264 return -1;
1265 if (l->max_lat > r->max_lat)
1266 return 1;
1267
1268 return 0;
1269}
1270
1271static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001272 .name = "max",
1273 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001274};
1275
mingo39aeb522009-09-14 20:04:48 +02001276static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001277{
1278 if (l->nb_atoms < r->nb_atoms)
1279 return -1;
1280 if (l->nb_atoms > r->nb_atoms)
1281 return 1;
1282
1283 return 0;
1284}
1285
1286static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001287 .name = "switch",
1288 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001289};
1290
mingo39aeb522009-09-14 20:04:48 +02001291static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001292{
1293 if (l->total_runtime < r->total_runtime)
1294 return -1;
1295 if (l->total_runtime > r->total_runtime)
1296 return 1;
1297
1298 return 0;
1299}
1300
1301static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001302 .name = "runtime",
1303 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001304};
1305
1306static struct sort_dimension *available_sorts[] = {
1307 &pid_sort_dimension,
1308 &avg_sort_dimension,
1309 &max_sort_dimension,
1310 &switch_sort_dimension,
1311 &runtime_sort_dimension,
1312};
1313
1314#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1315
1316static LIST_HEAD(sort_list);
1317
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001318static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001319{
1320 int i;
1321
1322 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1323 if (!strcmp(available_sorts[i]->name, tok)) {
1324 list_add_tail(&available_sorts[i]->list, list);
1325
1326 return 0;
1327 }
1328 }
1329
1330 return -1;
1331}
1332
1333static void setup_sorting(void);
1334
1335static void sort_lat(void)
1336{
1337 struct rb_node *node;
1338
1339 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001340 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001341 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001342 if (!node)
1343 break;
1344
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001345 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001346 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001347 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001348 }
1349}
1350
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001351static struct trace_sched_handler *trace_handler;
1352
1353static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001354process_sched_wakeup_event(void *data,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001355 struct event *event,
1356 int cpu __used,
1357 u64 timestamp __used,
1358 struct thread *thread __used)
1359{
1360 struct trace_wakeup_event wakeup_event;
1361
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001362 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001363
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001364 FILL_ARRAY(wakeup_event, comm, event, data);
1365 FILL_FIELD(wakeup_event, pid, event, data);
1366 FILL_FIELD(wakeup_event, prio, event, data);
1367 FILL_FIELD(wakeup_event, success, event, data);
1368 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001369
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001370 if (trace_handler->wakeup_event)
1371 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001372}
1373
Ingo Molnarc8a37752009-09-16 14:07:00 +02001374/*
1375 * Track the current task - that way we can know whether there's any
1376 * weird events, such as a task being switched away that is not current.
1377 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001378static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001379
Ingo Molnarc8a37752009-09-16 14:07:00 +02001380static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1381
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001382static struct thread *curr_thread[MAX_CPUS];
1383
1384static char next_shortname1 = 'A';
1385static char next_shortname2 = '0';
1386
1387static void
1388map_switch_event(struct trace_switch_event *switch_event,
1389 struct event *event __used,
1390 int this_cpu,
1391 u64 timestamp,
1392 struct thread *thread __used)
1393{
1394 struct thread *sched_out, *sched_in;
1395 int new_shortname;
1396 u64 timestamp0;
1397 s64 delta;
1398 int cpu;
1399
1400 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1401
1402 if (this_cpu > max_cpu)
1403 max_cpu = this_cpu;
1404
1405 timestamp0 = cpu_last_switched[this_cpu];
1406 cpu_last_switched[this_cpu] = timestamp;
1407 if (timestamp0)
1408 delta = timestamp - timestamp0;
1409 else
1410 delta = 0;
1411
1412 if (delta < 0)
1413 die("hm, delta: %Ld < 0 ?\n", delta);
1414
1415
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001416 sched_out = threads__findnew(switch_event->prev_pid);
1417 sched_in = threads__findnew(switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001418
1419 curr_thread[this_cpu] = sched_in;
1420
1421 printf(" ");
1422
1423 new_shortname = 0;
1424 if (!sched_in->shortname[0]) {
1425 sched_in->shortname[0] = next_shortname1;
1426 sched_in->shortname[1] = next_shortname2;
1427
1428 if (next_shortname1 < 'Z') {
1429 next_shortname1++;
1430 } else {
1431 next_shortname1='A';
1432 if (next_shortname2 < '9') {
1433 next_shortname2++;
1434 } else {
1435 next_shortname2='0';
1436 }
1437 }
1438 new_shortname = 1;
1439 }
1440
1441 for (cpu = 0; cpu <= max_cpu; cpu++) {
1442 if (cpu != this_cpu)
1443 printf(" ");
1444 else
1445 printf("*");
1446
1447 if (curr_thread[cpu]) {
1448 if (curr_thread[cpu]->pid)
1449 printf("%2s ", curr_thread[cpu]->shortname);
1450 else
1451 printf(". ");
1452 } else
1453 printf(" ");
1454 }
1455
1456 printf(" %12.6f secs ", (double)timestamp/1e9);
1457 if (new_shortname) {
1458 printf("%s => %s:%d\n",
1459 sched_in->shortname, sched_in->comm, sched_in->pid);
1460 } else {
1461 printf("\n");
1462 }
1463}
1464
1465
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001466static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001467process_sched_switch_event(void *data,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001468 struct event *event,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469 int this_cpu,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001470 u64 timestamp __used,
1471 struct thread *thread __used)
1472{
1473 struct trace_switch_event switch_event;
1474
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001475 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001476
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001477 FILL_ARRAY(switch_event, prev_comm, event, data);
1478 FILL_FIELD(switch_event, prev_pid, event, data);
1479 FILL_FIELD(switch_event, prev_prio, event, data);
1480 FILL_FIELD(switch_event, prev_state, event, data);
1481 FILL_ARRAY(switch_event, next_comm, event, data);
1482 FILL_FIELD(switch_event, next_pid, event, data);
1483 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001484
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001485 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001486 /*
1487 * Are we trying to switch away a PID that is
1488 * not current?
1489 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001490 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001491 nr_context_switch_bugs++;
1492 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001493 if (trace_handler->switch_event)
1494 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001495
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001496 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001497}
1498
1499static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001500process_sched_runtime_event(void *data,
mingo39aeb522009-09-14 20:04:48 +02001501 struct event *event,
1502 int cpu __used,
1503 u64 timestamp __used,
1504 struct thread *thread __used)
1505{
1506 struct trace_runtime_event runtime_event;
1507
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001508 FILL_ARRAY(runtime_event, comm, event, data);
1509 FILL_FIELD(runtime_event, pid, event, data);
1510 FILL_FIELD(runtime_event, runtime, event, data);
1511 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001512
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001513 if (trace_handler->runtime_event)
1514 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001515}
1516
1517static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001518process_sched_fork_event(void *data,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001519 struct event *event,
1520 int cpu __used,
1521 u64 timestamp __used,
1522 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001523{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001524 struct trace_fork_event fork_event;
1525
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001526 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001527
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001528 FILL_ARRAY(fork_event, parent_comm, event, data);
1529 FILL_FIELD(fork_event, parent_pid, event, data);
1530 FILL_ARRAY(fork_event, child_comm, event, data);
1531 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001532
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001533 if (trace_handler->fork_event)
1534 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001535}
1536
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001537static void
1538process_sched_exit_event(struct event *event,
1539 int cpu __used,
1540 u64 timestamp __used,
1541 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001542{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001543 if (verbose)
1544 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001545}
1546
1547static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001548process_sched_migrate_task_event(void *data,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001549 struct event *event,
1550 int cpu __used,
1551 u64 timestamp __used,
1552 struct thread *thread __used)
1553{
1554 struct trace_migrate_task_event migrate_task_event;
1555
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001556 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001557
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001558 FILL_ARRAY(migrate_task_event, comm, event, data);
1559 FILL_FIELD(migrate_task_event, pid, event, data);
1560 FILL_FIELD(migrate_task_event, prio, event, data);
1561 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001562
1563 if (trace_handler->migrate_task_event)
1564 trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
1565}
1566
1567static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001568process_raw_event(event_t *raw_event __used, void *data,
Ingo Molnarec156762009-09-11 12:12:54 +02001569 int cpu, u64 timestamp, struct thread *thread)
1570{
Ingo Molnarec156762009-09-11 12:12:54 +02001571 struct event *event;
1572 int type;
1573
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +08001574
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001575 type = trace_parse_common_type(data);
Ingo Molnarec156762009-09-11 12:12:54 +02001576 event = trace_find_event(type);
1577
Ingo Molnarec156762009-09-11 12:12:54 +02001578 if (!strcmp(event->name, "sched_switch"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001579 process_sched_switch_event(data, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001580 if (!strcmp(event->name, "sched_stat_runtime"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001581 process_sched_runtime_event(data, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001582 if (!strcmp(event->name, "sched_wakeup"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001583 process_sched_wakeup_event(data, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001584 if (!strcmp(event->name, "sched_wakeup_new"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001585 process_sched_wakeup_event(data, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001586 if (!strcmp(event->name, "sched_process_fork"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001587 process_sched_fork_event(data, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001588 if (!strcmp(event->name, "sched_process_exit"))
1589 process_sched_exit_event(event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001590 if (!strcmp(event->name, "sched_migrate_task"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001591 process_sched_migrate_task_event(data, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001592}
1593
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001594static int process_sample_event(event_t *event)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001595{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001596 struct sample_data data;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001597 struct thread *thread;
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001598
1599 if (!(sample_type & PERF_SAMPLE_RAW))
1600 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001601
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001602 memset(&data, 0, sizeof(data));
1603 data.time = -1;
1604 data.cpu = -1;
1605 data.period = -1;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001606
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001607 event__parse_sample(event, sample_type, &data);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001608
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001609 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001610 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001611 data.pid, data.tid,
1612 (void *)(long)data.ip,
1613 (long long)data.period);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001614
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001615 thread = threads__findnew(data.pid);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001616 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001617 pr_debug("problem processing %d event, skipping it.\n",
1618 event->header.type);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001619 return -1;
1620 }
1621
Julia Lawallf39cdf22009-10-17 08:43:17 +02001622 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1623
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001624 if (profile_cpu != -1 && profile_cpu != (int)data.cpu)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001625 return 0;
1626
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001627 process_raw_event(event, data.raw_data, data.cpu, data.time, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001628
1629 return 0;
1630}
1631
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001632static int process_lost_event(event_t *event __used)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001633{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001634 nr_lost_chunks++;
1635 nr_lost_events += event->lost.lost;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001636
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001637 return 0;
1638}
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001639
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001640static int sample_type_check(u64 type)
1641{
1642 sample_type = type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001643
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001644 if (!(sample_type & PERF_SAMPLE_RAW)) {
1645 fprintf(stderr,
1646 "No trace sample to read. Did you call perf record "
1647 "without -R?");
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001648 return -1;
1649 }
1650
1651 return 0;
1652}
1653
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001654static struct perf_file_handler file_handler = {
1655 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001656 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001657 .process_lost_event = process_lost_event,
1658 .sample_type_check = sample_type_check,
1659};
1660
Ingo Molnar46f392c2009-09-12 10:08:34 +02001661static int read_events(void)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001662{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001663 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001664 register_perf_file_handler(&file_handler);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001665
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001666 return mmap_dispatch_perf_file(&header, input_name, 0, 0,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001667 &event__cwdlen, &event__cwd);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001668}
1669
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001670static void print_bad_events(void)
1671{
1672 if (nr_unordered_timestamps && nr_timestamps) {
1673 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1674 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1675 nr_unordered_timestamps, nr_timestamps);
1676 }
1677 if (nr_lost_events && nr_events) {
1678 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1679 (double)nr_lost_events/(double)nr_events*100.0,
1680 nr_lost_events, nr_events, nr_lost_chunks);
1681 }
1682 if (nr_state_machine_bugs && nr_timestamps) {
1683 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1684 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1685 nr_state_machine_bugs, nr_timestamps);
1686 if (nr_lost_events)
1687 printf(" (due to lost events?)");
1688 printf("\n");
1689 }
1690 if (nr_context_switch_bugs && nr_timestamps) {
1691 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1692 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1693 nr_context_switch_bugs, nr_timestamps);
1694 if (nr_lost_events)
1695 printf(" (due to lost events?)");
1696 printf("\n");
1697 }
1698}
1699
1700static void __cmd_lat(void)
1701{
1702 struct rb_node *next;
1703
1704 setup_pager();
1705 read_events();
1706 sort_lat();
1707
1708 printf("\n -----------------------------------------------------------------------------------------\n");
1709 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1710 printf(" -----------------------------------------------------------------------------------------\n");
1711
1712 next = rb_first(&sorted_atom_root);
1713
1714 while (next) {
1715 struct work_atoms *work_list;
1716
1717 work_list = rb_entry(next, struct work_atoms, node);
1718 output_lat_thread(work_list);
1719 next = rb_next(next);
1720 }
1721
1722 printf(" -----------------------------------------------------------------------------------------\n");
1723 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1724 (double)all_runtime/1e6, all_count);
1725
1726 printf(" ---------------------------------------------------\n");
1727
1728 print_bad_events();
1729 printf("\n");
1730
1731}
1732
1733static struct trace_sched_handler map_ops = {
1734 .wakeup_event = NULL,
1735 .switch_event = map_switch_event,
1736 .runtime_event = NULL,
1737 .fork_event = NULL,
1738};
1739
1740static void __cmd_map(void)
1741{
Ingo Molnar40749d02009-09-17 18:24:55 +02001742 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1743
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001744 setup_pager();
1745 read_events();
1746 print_bad_events();
1747}
1748
1749static void __cmd_replay(void)
1750{
1751 unsigned long i;
1752
1753 calibrate_run_measurement_overhead();
1754 calibrate_sleep_measurement_overhead();
1755
1756 test_calibrations();
1757
1758 read_events();
1759
1760 printf("nr_run_events: %ld\n", nr_run_events);
1761 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1762 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1763
1764 if (targetless_wakeups)
1765 printf("target-less wakeups: %ld\n", targetless_wakeups);
1766 if (multitarget_wakeups)
1767 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1768 if (nr_run_events_optimized)
1769 printf("run atoms optimized: %ld\n",
1770 nr_run_events_optimized);
1771
1772 print_task_traces();
1773 add_cross_task_wakeups();
1774
1775 create_tasks();
1776 printf("------------------------------------------------------------\n");
1777 for (i = 0; i < replay_repeat; i++)
1778 run_one_test();
1779}
1780
1781
Ingo Molnar46f392c2009-09-12 10:08:34 +02001782static const char * const sched_usage[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001783 "perf sched [<options>] {record|latency|map|replay|trace}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001784 NULL
1785};
1786
Ingo Molnarf2858d82009-09-11 12:12:54 +02001787static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001788 OPT_STRING('i', "input", &input_name, "file",
1789 "input file name"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001790 OPT_BOOLEAN('v', "verbose", &verbose,
1791 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001792 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1793 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001794 OPT_END()
1795};
1796
1797static const char * const latency_usage[] = {
1798 "perf sched latency [<options>]",
1799 NULL
1800};
1801
1802static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001803 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1804 "sort by key(s): runtime, switch, avg, max"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001805 OPT_BOOLEAN('v', "verbose", &verbose,
1806 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001807 OPT_INTEGER('C', "CPU", &profile_cpu,
1808 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001809 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1810 "dump raw trace in ASCII"),
1811 OPT_END()
1812};
1813
1814static const char * const replay_usage[] = {
1815 "perf sched replay [<options>]",
1816 NULL
1817};
1818
1819static const struct option replay_options[] = {
1820 OPT_INTEGER('r', "repeat", &replay_repeat,
1821 "repeat the workload replay N times (-1: infinite)"),
1822 OPT_BOOLEAN('v', "verbose", &verbose,
1823 "be more verbose (show symbol address, etc)"),
1824 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1825 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001826 OPT_END()
1827};
1828
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001829static void setup_sorting(void)
1830{
1831 char *tmp, *tok, *str = strdup(sort_order);
1832
1833 for (tok = strtok_r(str, ", ", &tmp);
1834 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1835 if (sort_dimension__add(tok, &sort_list) < 0) {
1836 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001837 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001838 }
1839 }
1840
1841 free(str);
1842
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001843 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001844}
1845
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001846static const char *record_args[] = {
1847 "record",
1848 "-a",
1849 "-R",
Frederic Weisbeckerd1302522009-09-14 08:57:15 +02001850 "-M",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001851 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001852 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001853 "-c", "1",
1854 "-e", "sched:sched_switch:r",
1855 "-e", "sched:sched_stat_wait:r",
1856 "-e", "sched:sched_stat_sleep:r",
1857 "-e", "sched:sched_stat_iowait:r",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001858 "-e", "sched:sched_stat_runtime:r",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001859 "-e", "sched:sched_process_exit:r",
1860 "-e", "sched:sched_process_fork:r",
1861 "-e", "sched:sched_wakeup:r",
1862 "-e", "sched:sched_migrate_task:r",
1863};
1864
1865static int __cmd_record(int argc, const char **argv)
1866{
1867 unsigned int rec_argc, i, j;
1868 const char **rec_argv;
1869
1870 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1871 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1872
1873 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1874 rec_argv[i] = strdup(record_args[i]);
1875
1876 for (j = 1; j < (unsigned int)argc; j++, i++)
1877 rec_argv[i] = argv[j];
1878
1879 BUG_ON(i != rec_argc);
1880
1881 return cmd_record(i, rec_argv, NULL);
1882}
1883
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001884int cmd_sched(int argc, const char **argv, const char *prefix __used)
1885{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001886 argc = parse_options(argc, argv, sched_options, sched_usage,
1887 PARSE_OPT_STOP_AT_NON_OPTION);
1888 if (!argc)
1889 usage_with_options(sched_usage, sched_options);
1890
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001891 /*
1892 * Aliased to 'perf trace' for now:
1893 */
1894 if (!strcmp(argv[0], "trace"))
1895 return cmd_trace(argc, argv, prefix);
1896
1897 symbol__init(0);
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001898 if (!strncmp(argv[0], "rec", 3)) {
1899 return __cmd_record(argc, argv);
1900 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001901 trace_handler = &lat_ops;
1902 if (argc > 1) {
1903 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1904 if (argc)
1905 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001906 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001907 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001908 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001909 } else if (!strcmp(argv[0], "map")) {
1910 trace_handler = &map_ops;
1911 setup_sorting();
1912 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001913 } else if (!strncmp(argv[0], "rep", 3)) {
1914 trace_handler = &replay_ops;
1915 if (argc) {
1916 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1917 if (argc)
1918 usage_with_options(replay_usage, replay_options);
1919 }
1920 __cmd_replay();
1921 } else {
1922 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001923 }
1924
Ingo Molnarec156762009-09-11 12:12:54 +02001925 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001926}