blob: 8d58d9e07a7bd8274049578ca4ab6940f9597a0e [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02009#include "util/session.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020010
11#include "util/parse-options.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020012#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020013
Ingo Molnar0a02ad92009-09-11 12:12:54 +020014#include "util/debug.h"
15
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020016#include <sys/prctl.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020017
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020018#include <semaphore.h>
19#include <pthread.h>
20#include <math.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020021
Ingo Molnarec156762009-09-11 12:12:54 +020022static char const *input_name = "perf.data";
Ingo Molnar0a02ad92009-09-11 12:12:54 +020023
Ingo Molnarec156762009-09-11 12:12:54 +020024static u64 sample_type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020025
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020026static char default_sort_order[] = "avg, max, switch, runtime";
27static char *sort_order = default_sort_order;
28
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020029static int profile_cpu = -1;
30
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020031#define PR_SET_NAME 15 /* Set process name */
32#define MAX_CPUS 4096
Ingo Molnar0a02ad92009-09-11 12:12:54 +020033
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020034static u64 run_measurement_overhead;
35static u64 sleep_measurement_overhead;
Ingo Molnarec156762009-09-11 12:12:54 +020036
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020037#define COMM_LEN 20
38#define SYM_LEN 129
Ingo Molnarec156762009-09-11 12:12:54 +020039
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020040#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020042static unsigned long nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +020043
mingo39aeb522009-09-14 20:04:48 +020044struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020045
46struct task_desc {
47 unsigned long nr;
48 unsigned long pid;
49 char comm[COMM_LEN];
50
51 unsigned long nr_events;
52 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020053 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020054
55 pthread_t thread;
56 sem_t sleep_sem;
57
58 sem_t ready_for_work;
59 sem_t work_done_sem;
60
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020061 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020062};
63
64enum sched_event_type {
65 SCHED_EVENT_RUN,
66 SCHED_EVENT_SLEEP,
67 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020068 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020069};
70
mingo39aeb522009-09-14 20:04:48 +020071struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020072 enum sched_event_type type;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020073 u64 timestamp;
74 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020075 unsigned long nr;
76 int specific_wait;
77 sem_t *wait_sem;
78 struct task_desc *wakee;
79};
80
81static struct task_desc *pid_to_task[MAX_PID];
82
83static struct task_desc **tasks;
84
85static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020086static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020087
88static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
89
90static unsigned long nr_run_events;
91static unsigned long nr_sleep_events;
92static unsigned long nr_wakeup_events;
93
94static unsigned long nr_sleep_corrections;
95static unsigned long nr_run_events_optimized;
96
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097static unsigned long targetless_wakeups;
98static unsigned long multitarget_wakeups;
99
100static u64 cpu_usage;
101static u64 runavg_cpu_usage;
102static u64 parent_cpu_usage;
103static u64 runavg_parent_cpu_usage;
104
105static unsigned long nr_runs;
106static u64 sum_runtime;
107static u64 sum_fluct;
108static u64 run_avg;
109
110static unsigned long replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200111static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200112static unsigned long nr_unordered_timestamps;
113static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200114static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200115static unsigned long nr_events;
116static unsigned long nr_lost_chunks;
117static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200118
119#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
120
121enum thread_state {
122 THREAD_SLEEPING = 0,
123 THREAD_WAIT_CPU,
124 THREAD_SCHED_IN,
125 THREAD_IGNORE
126};
127
128struct work_atom {
129 struct list_head list;
130 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200131 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200132 u64 wake_up_time;
133 u64 sched_in_time;
134 u64 runtime;
135};
136
mingo39aeb522009-09-14 20:04:48 +0200137struct work_atoms {
138 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200139 struct thread *thread;
140 struct rb_node node;
141 u64 max_lat;
Frederic Weisbecker37863102009-12-09 21:40:08 +0100142 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200143 u64 total_lat;
144 u64 nb_atoms;
145 u64 total_runtime;
146};
147
mingo39aeb522009-09-14 20:04:48 +0200148typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200149
150static struct rb_root atom_root, sorted_atom_root;
151
152static u64 all_runtime;
153static u64 all_count;
154
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200155
156static u64 get_nsecs(void)
157{
158 struct timespec ts;
159
160 clock_gettime(CLOCK_MONOTONIC, &ts);
161
162 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
163}
164
165static void burn_nsecs(u64 nsecs)
166{
167 u64 T0 = get_nsecs(), T1;
168
169 do {
170 T1 = get_nsecs();
171 } while (T1 + run_measurement_overhead < T0 + nsecs);
172}
173
174static void sleep_nsecs(u64 nsecs)
175{
176 struct timespec ts;
177
178 ts.tv_nsec = nsecs % 999999999;
179 ts.tv_sec = nsecs / 999999999;
180
181 nanosleep(&ts, NULL);
182}
183
184static void calibrate_run_measurement_overhead(void)
185{
186 u64 T0, T1, delta, min_delta = 1000000000ULL;
187 int i;
188
189 for (i = 0; i < 10; i++) {
190 T0 = get_nsecs();
191 burn_nsecs(0);
192 T1 = get_nsecs();
193 delta = T1-T0;
194 min_delta = min(min_delta, delta);
195 }
196 run_measurement_overhead = min_delta;
197
198 printf("run measurement overhead: %Ld nsecs\n", min_delta);
199}
200
201static void calibrate_sleep_measurement_overhead(void)
202{
203 u64 T0, T1, delta, min_delta = 1000000000ULL;
204 int i;
205
206 for (i = 0; i < 10; i++) {
207 T0 = get_nsecs();
208 sleep_nsecs(10000);
209 T1 = get_nsecs();
210 delta = T1-T0;
211 min_delta = min(min_delta, delta);
212 }
213 min_delta -= 10000;
214 sleep_measurement_overhead = min_delta;
215
216 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
217}
218
mingo39aeb522009-09-14 20:04:48 +0200219static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200220get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200221{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200222 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200223 unsigned long idx = task->nr_events;
224 size_t size;
225
226 event->timestamp = timestamp;
227 event->nr = idx;
228
229 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200230 size = sizeof(struct sched_atom *) * task->nr_events;
231 task->atoms = realloc(task->atoms, size);
232 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200233
mingo39aeb522009-09-14 20:04:48 +0200234 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200235
236 return event;
237}
238
mingo39aeb522009-09-14 20:04:48 +0200239static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200240{
241 if (!task->nr_events)
242 return NULL;
243
mingo39aeb522009-09-14 20:04:48 +0200244 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200245}
246
247static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200248add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200249{
mingo39aeb522009-09-14 20:04:48 +0200250 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200251
252 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200253 * optimize an existing RUN event by merging this one
254 * to it:
255 */
Ingo Molnarec156762009-09-11 12:12:54 +0200256 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
257 nr_run_events_optimized++;
258 curr_event->duration += duration;
259 return;
260 }
261
262 event = get_new_event(task, timestamp);
263
264 event->type = SCHED_EVENT_RUN;
265 event->duration = duration;
266
267 nr_run_events++;
268}
269
Ingo Molnarec156762009-09-11 12:12:54 +0200270static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200271add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200272 struct task_desc *wakee)
273{
mingo39aeb522009-09-14 20:04:48 +0200274 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200275
276 event = get_new_event(task, timestamp);
277 event->type = SCHED_EVENT_WAKEUP;
278 event->wakee = wakee;
279
280 wakee_event = last_event(wakee);
281 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
282 targetless_wakeups++;
283 return;
284 }
285 if (wakee_event->wait_sem) {
286 multitarget_wakeups++;
287 return;
288 }
289
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200290 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200291 sem_init(wakee_event->wait_sem, 0, 0);
292 wakee_event->specific_wait = 1;
293 event->wait_sem = wakee_event->wait_sem;
294
295 nr_wakeup_events++;
296}
297
298static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200299add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200300 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200301{
mingo39aeb522009-09-14 20:04:48 +0200302 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200303
304 event->type = SCHED_EVENT_SLEEP;
305
306 nr_sleep_events++;
307}
308
309static struct task_desc *register_pid(unsigned long pid, const char *comm)
310{
311 struct task_desc *task;
312
313 BUG_ON(pid >= MAX_PID);
314
315 task = pid_to_task[pid];
316
317 if (task)
318 return task;
319
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200320 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200321 task->pid = pid;
322 task->nr = nr_tasks;
323 strcpy(task->comm, comm);
324 /*
325 * every task starts in sleeping state - this gets ignored
326 * if there's no wakeup pointing to this sleep state:
327 */
328 add_sched_event_sleep(task, 0, 0);
329
330 pid_to_task[pid] = task;
331 nr_tasks++;
332 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
333 BUG_ON(!tasks);
334 tasks[task->nr] = task;
335
Ingo Molnarad236fd2009-09-11 12:12:54 +0200336 if (verbose)
337 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200338
339 return task;
340}
341
342
Ingo Molnarec156762009-09-11 12:12:54 +0200343static void print_task_traces(void)
344{
345 struct task_desc *task;
346 unsigned long i;
347
348 for (i = 0; i < nr_tasks; i++) {
349 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200350 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200351 task->nr, task->comm, task->pid, task->nr_events);
352 }
353}
354
355static void add_cross_task_wakeups(void)
356{
357 struct task_desc *task1, *task2;
358 unsigned long i, j;
359
360 for (i = 0; i < nr_tasks; i++) {
361 task1 = tasks[i];
362 j = i + 1;
363 if (j == nr_tasks)
364 j = 0;
365 task2 = tasks[j];
366 add_sched_event_wakeup(task1, 0, task2);
367 }
368}
369
370static void
mingo39aeb522009-09-14 20:04:48 +0200371process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200372{
373 int ret = 0;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200374 u64 now;
Ingo Molnarec156762009-09-11 12:12:54 +0200375 long long delta;
376
377 now = get_nsecs();
mingo39aeb522009-09-14 20:04:48 +0200378 delta = start_time + atom->timestamp - now;
Ingo Molnarec156762009-09-11 12:12:54 +0200379
mingo39aeb522009-09-14 20:04:48 +0200380 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200381 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200382 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200383 break;
384 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200385 if (atom->wait_sem)
386 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200387 BUG_ON(ret);
388 break;
389 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200390 if (atom->wait_sem)
391 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200392 BUG_ON(ret);
393 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200394 case SCHED_EVENT_MIGRATION:
395 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200396 default:
397 BUG_ON(1);
398 }
399}
400
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200401static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200402{
403 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200404 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200405 int err;
406
407 err = getrusage(RUSAGE_SELF, &ru);
408 BUG_ON(err);
409
410 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
411 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
412
413 return sum;
414}
415
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800416static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200417{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800418 struct perf_event_attr attr;
419 int fd;
420
421 memset(&attr, 0, sizeof(attr));
422
423 attr.type = PERF_TYPE_SOFTWARE;
424 attr.config = PERF_COUNT_SW_TASK_CLOCK;
425
426 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
427
428 if (fd < 0)
429 die("Error: sys_perf_event_open() syscall returned"
430 "with %d (%s)\n", fd, strerror(errno));
431 return fd;
432}
433
434static u64 get_cpu_usage_nsec_self(int fd)
435{
436 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200437 int ret;
438
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800439 ret = read(fd, &runtime, sizeof(runtime));
440 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200441
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800442 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200443}
444
445static void *thread_func(void *ctx)
446{
447 struct task_desc *this_task = ctx;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200448 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200449 unsigned long i, ret;
450 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800451 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200452
Ingo Molnarec156762009-09-11 12:12:54 +0200453 sprintf(comm2, ":%s", this_task->comm);
454 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800455 fd = self_open_counters();
Ingo Molnarec156762009-09-11 12:12:54 +0200456
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
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800465 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200466
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
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800472 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200473 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200474 ret = sem_post(&this_task->work_done_sem);
475 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200476
477 ret = pthread_mutex_lock(&work_done_wait_mutex);
478 BUG_ON(ret);
479 ret = pthread_mutex_unlock(&work_done_wait_mutex);
480 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200481
482 goto again;
483}
484
485static void create_tasks(void)
486{
487 struct task_desc *task;
488 pthread_attr_t attr;
489 unsigned long i;
490 int err;
491
492 err = pthread_attr_init(&attr);
493 BUG_ON(err);
494 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
495 BUG_ON(err);
496 err = pthread_mutex_lock(&start_work_mutex);
497 BUG_ON(err);
498 err = pthread_mutex_lock(&work_done_wait_mutex);
499 BUG_ON(err);
500 for (i = 0; i < nr_tasks; i++) {
501 task = tasks[i];
502 sem_init(&task->sleep_sem, 0, 0);
503 sem_init(&task->ready_for_work, 0, 0);
504 sem_init(&task->work_done_sem, 0, 0);
505 task->curr_event = 0;
506 err = pthread_create(&task->thread, &attr, thread_func, task);
507 BUG_ON(err);
508 }
509}
510
Ingo Molnarec156762009-09-11 12:12:54 +0200511static void wait_for_tasks(void)
512{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200513 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200514 struct task_desc *task;
515 unsigned long i, ret;
516
Ingo Molnarec156762009-09-11 12:12:54 +0200517 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200518 cpu_usage = 0;
519 pthread_mutex_unlock(&work_done_wait_mutex);
520
521 for (i = 0; i < nr_tasks; i++) {
522 task = tasks[i];
523 ret = sem_wait(&task->ready_for_work);
524 BUG_ON(ret);
525 sem_init(&task->ready_for_work, 0, 0);
526 }
527 ret = pthread_mutex_lock(&work_done_wait_mutex);
528 BUG_ON(ret);
529
530 cpu_usage_0 = get_cpu_usage_nsec_parent();
531
532 pthread_mutex_unlock(&start_work_mutex);
533
Ingo Molnarec156762009-09-11 12:12:54 +0200534 for (i = 0; i < nr_tasks; i++) {
535 task = tasks[i];
536 ret = sem_wait(&task->work_done_sem);
537 BUG_ON(ret);
538 sem_init(&task->work_done_sem, 0, 0);
539 cpu_usage += task->cpu_usage;
540 task->cpu_usage = 0;
541 }
542
543 cpu_usage_1 = get_cpu_usage_nsec_parent();
544 if (!runavg_cpu_usage)
545 runavg_cpu_usage = cpu_usage;
546 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
547
548 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
549 if (!runavg_parent_cpu_usage)
550 runavg_parent_cpu_usage = parent_cpu_usage;
551 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
552 parent_cpu_usage)/10;
553
554 ret = pthread_mutex_lock(&start_work_mutex);
555 BUG_ON(ret);
556
557 for (i = 0; i < nr_tasks; i++) {
558 task = tasks[i];
559 sem_init(&task->sleep_sem, 0, 0);
560 task->curr_event = 0;
561 }
562}
563
Ingo Molnarec156762009-09-11 12:12:54 +0200564static void run_one_test(void)
565{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200566 u64 T0, T1, delta, avg_delta, fluct, std_dev;
Ingo Molnarec156762009-09-11 12:12:54 +0200567
568 T0 = get_nsecs();
569 wait_for_tasks();
570 T1 = get_nsecs();
571
572 delta = T1 - T0;
573 sum_runtime += delta;
574 nr_runs++;
575
576 avg_delta = sum_runtime / nr_runs;
577 if (delta < avg_delta)
578 fluct = avg_delta - delta;
579 else
580 fluct = delta - avg_delta;
581 sum_fluct += fluct;
582 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
583 if (!run_avg)
584 run_avg = delta;
585 run_avg = (run_avg*9 + delta)/10;
586
Ingo Molnarad236fd2009-09-11 12:12:54 +0200587 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200588 nr_runs, (double)delta/1000000.0);
589
Ingo Molnarad236fd2009-09-11 12:12:54 +0200590 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200591 (double)run_avg/1e6);
592
Ingo Molnarad236fd2009-09-11 12:12:54 +0200593 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200594 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
595
596#if 0
597 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200598 * rusage statistics done by the parent, these are less
599 * accurate than the sum_exec_runtime based statistics:
600 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200601 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200602 (double)parent_cpu_usage/1e6,
603 (double)runavg_parent_cpu_usage/1e6);
604#endif
605
Ingo Molnarad236fd2009-09-11 12:12:54 +0200606 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200607
608 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200609 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200610 nr_sleep_corrections = 0;
611}
612
613static void test_calibrations(void)
614{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200615 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200616
617 T0 = get_nsecs();
618 burn_nsecs(1e6);
619 T1 = get_nsecs();
620
Ingo Molnarad236fd2009-09-11 12:12:54 +0200621 printf("the run test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200622
623 T0 = get_nsecs();
624 sleep_nsecs(1e6);
625 T1 = get_nsecs();
626
Ingo Molnarad236fd2009-09-11 12:12:54 +0200627 printf("the sleep test took %Ld nsecs\n", T1-T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200628}
629
Frederic Weisbecker46538812009-09-12 02:43:45 +0200630#define FILL_FIELD(ptr, field, event, data) \
631 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
632
633#define FILL_ARRAY(ptr, array, event, data) \
634do { \
635 void *__array = raw_field_ptr(event, #array, data); \
636 memcpy(ptr.array, __array, sizeof(ptr.array)); \
637} while(0)
638
639#define FILL_COMMON_FIELDS(ptr, event, data) \
640do { \
641 FILL_FIELD(ptr, common_type, event, data); \
642 FILL_FIELD(ptr, common_flags, event, data); \
643 FILL_FIELD(ptr, common_preempt_count, event, data); \
644 FILL_FIELD(ptr, common_pid, event, data); \
645 FILL_FIELD(ptr, common_tgid, event, data); \
646} while (0)
647
Ingo Molnarfbf94822009-09-11 12:12:54 +0200648
Ingo Molnarec156762009-09-11 12:12:54 +0200649
Ingo Molnarfbf94822009-09-11 12:12:54 +0200650struct trace_switch_event {
651 u32 size;
652
653 u16 common_type;
654 u8 common_flags;
655 u8 common_preempt_count;
656 u32 common_pid;
657 u32 common_tgid;
658
659 char prev_comm[16];
660 u32 prev_pid;
661 u32 prev_prio;
662 u64 prev_state;
663 char next_comm[16];
664 u32 next_pid;
665 u32 next_prio;
666};
667
mingo39aeb522009-09-14 20:04:48 +0200668struct trace_runtime_event {
669 u32 size;
670
671 u16 common_type;
672 u8 common_flags;
673 u8 common_preempt_count;
674 u32 common_pid;
675 u32 common_tgid;
676
677 char comm[16];
678 u32 pid;
679 u64 runtime;
680 u64 vruntime;
681};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200682
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200683struct trace_wakeup_event {
684 u32 size;
685
686 u16 common_type;
687 u8 common_flags;
688 u8 common_preempt_count;
689 u32 common_pid;
690 u32 common_tgid;
691
692 char comm[16];
693 u32 pid;
694
695 u32 prio;
696 u32 success;
697 u32 cpu;
698};
699
700struct trace_fork_event {
701 u32 size;
702
703 u16 common_type;
704 u8 common_flags;
705 u8 common_preempt_count;
706 u32 common_pid;
707 u32 common_tgid;
708
709 char parent_comm[16];
710 u32 parent_pid;
711 char child_comm[16];
712 u32 child_pid;
713};
714
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200715struct trace_migrate_task_event {
716 u32 size;
717
718 u16 common_type;
719 u8 common_flags;
720 u8 common_preempt_count;
721 u32 common_pid;
722 u32 common_tgid;
723
724 char comm[16];
725 u32 pid;
726
727 u32 prio;
728 u32 cpu;
729};
730
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200731struct trace_sched_handler {
732 void (*switch_event)(struct trace_switch_event *,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200733 struct perf_session *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200734 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 *,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200740 struct perf_session *,
mingo39aeb522009-09-14 20:04:48 +0200741 struct event *,
742 int cpu,
743 u64 timestamp,
744 struct thread *thread);
745
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200746 void (*wakeup_event)(struct trace_wakeup_event *,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200747 struct perf_session *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200748 struct event *,
749 int cpu,
750 u64 timestamp,
751 struct thread *thread);
752
753 void (*fork_event)(struct trace_fork_event *,
754 struct event *,
755 int cpu,
756 u64 timestamp,
757 struct thread *thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200758
759 void (*migrate_task_event)(struct trace_migrate_task_event *,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200760 struct perf_session *session,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200761 struct event *,
762 int cpu,
763 u64 timestamp,
764 struct thread *thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200765};
766
Ingo Molnarfbf94822009-09-11 12:12:54 +0200767
768static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200769replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200770 struct perf_session *session __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200771 struct event *event,
772 int cpu __used,
773 u64 timestamp __used,
774 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200775{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200776 struct task_desc *waker, *wakee;
777
778 if (verbose) {
779 printf("sched_wakeup event %p\n", event);
780
781 printf(" ... pid %d woke up %s/%d\n",
782 wakeup_event->common_pid,
783 wakeup_event->comm,
784 wakeup_event->pid);
785 }
786
787 waker = register_pid(wakeup_event->common_pid, "<unknown>");
788 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
789
790 add_sched_event_wakeup(waker, timestamp, wakee);
791}
792
Ingo Molnard1153382009-09-14 18:22:53 +0200793static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200794
795static void
796replay_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200797 struct perf_session *session __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200798 struct event *event,
799 int cpu,
800 u64 timestamp,
801 struct thread *thread __used)
802{
Ingo Molnarfbf94822009-09-11 12:12:54 +0200803 struct task_desc *prev, *next;
804 u64 timestamp0;
805 s64 delta;
806
Ingo Molnarad236fd2009-09-11 12:12:54 +0200807 if (verbose)
808 printf("sched_switch event %p\n", event);
809
Ingo Molnarfbf94822009-09-11 12:12:54 +0200810 if (cpu >= MAX_CPUS || cpu < 0)
811 return;
812
813 timestamp0 = cpu_last_switched[cpu];
814 if (timestamp0)
815 delta = timestamp - timestamp0;
816 else
817 delta = 0;
818
819 if (delta < 0)
820 die("hm, delta: %Ld < 0 ?\n", delta);
821
Ingo Molnarad236fd2009-09-11 12:12:54 +0200822 if (verbose) {
823 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200824 switch_event->prev_comm, switch_event->prev_pid,
825 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200826 delta);
827 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200828
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200829 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
830 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200831
832 cpu_last_switched[cpu] = timestamp;
833
834 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200835 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200836}
837
Ingo Molnarfbf94822009-09-11 12:12:54 +0200838
839static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200840replay_fork_event(struct trace_fork_event *fork_event,
841 struct event *event,
842 int cpu __used,
843 u64 timestamp __used,
844 struct thread *thread __used)
845{
846 if (verbose) {
847 printf("sched_fork event %p\n", event);
848 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
849 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
850 }
851 register_pid(fork_event->parent_pid, fork_event->parent_comm);
852 register_pid(fork_event->child_pid, fork_event->child_comm);
853}
854
855static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200856 .wakeup_event = replay_wakeup_event,
857 .switch_event = replay_switch_event,
858 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200859};
860
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200861struct sort_dimension {
862 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200863 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200864 struct list_head list;
865};
866
867static LIST_HEAD(cmp_pid);
868
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200869static int
mingo39aeb522009-09-14 20:04:48 +0200870thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200871{
872 struct sort_dimension *sort;
873 int ret = 0;
874
Ingo Molnarb5fae122009-09-11 12:12:54 +0200875 BUG_ON(list_empty(list));
876
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200877 list_for_each_entry(sort, list, list) {
878 ret = sort->cmp(l, r);
879 if (ret)
880 return ret;
881 }
882
883 return ret;
884}
885
mingo39aeb522009-09-14 20:04:48 +0200886static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200887thread_atoms_search(struct rb_root *root, struct thread *thread,
888 struct list_head *sort_list)
889{
890 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200891 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200892
893 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200894 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200895 int cmp;
896
mingo39aeb522009-09-14 20:04:48 +0200897 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200898
899 cmp = thread_lat_cmp(sort_list, &key, atoms);
900 if (cmp > 0)
901 node = node->rb_left;
902 else if (cmp < 0)
903 node = node->rb_right;
904 else {
905 BUG_ON(thread != atoms->thread);
906 return atoms;
907 }
908 }
909 return NULL;
910}
911
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200912static void
mingo39aeb522009-09-14 20:04:48 +0200913__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200914 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915{
916 struct rb_node **new = &(root->rb_node), *parent = NULL;
917
918 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200919 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200920 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200921
mingo39aeb522009-09-14 20:04:48 +0200922 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200924
925 cmp = thread_lat_cmp(sort_list, data, this);
926
927 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200928 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200929 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200930 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200931 }
932
933 rb_link_node(&data->node, parent, new);
934 rb_insert_color(&data->node, root);
935}
936
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200937static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200939 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Frederic Weisbecker17562202009-09-12 23:11:32 +0200940 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941 die("No memory");
942
Frederic Weisbecker17562202009-09-12 23:11:32 +0200943 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200944 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200945 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946}
947
948static void
949latency_fork_event(struct trace_fork_event *fork_event __used,
950 struct event *event __used,
951 int cpu __used,
952 u64 timestamp __used,
953 struct thread *thread __used)
954{
955 /* should insert the newcomer */
956}
957
Ingo Molnarea92ed52009-09-12 10:08:34 +0200958__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200959static char sched_out_state(struct trace_switch_event *switch_event)
960{
961 const char *str = TASK_STATE_TO_CHAR_STR;
962
963 return str[switch_event->prev_state];
964}
965
966static void
mingo39aeb522009-09-14 20:04:48 +0200967add_sched_out_event(struct work_atoms *atoms,
968 char run_state,
969 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200970{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200971 struct work_atom *atom = zalloc(sizeof(*atom));
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200972 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200973 die("Non memory");
974
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200975 atom->sched_out_time = timestamp;
976
mingo39aeb522009-09-14 20:04:48 +0200977 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200978 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200979 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200980 }
981
mingo39aeb522009-09-14 20:04:48 +0200982 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200983}
984
985static void
mingo39aeb522009-09-14 20:04:48 +0200986add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
987{
988 struct work_atom *atom;
989
990 BUG_ON(list_empty(&atoms->work_list));
991
992 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
993
994 atom->runtime += delta;
995 atoms->total_runtime += delta;
996}
997
998static void
999add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001000{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001001 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001002 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001003
mingo39aeb522009-09-14 20:04:48 +02001004 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001005 return;
1006
mingo39aeb522009-09-14 20:04:48 +02001007 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001008
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001009 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001010 return;
1011
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001012 if (timestamp < atom->wake_up_time) {
1013 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001014 return;
1015 }
1016
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001017 atom->state = THREAD_SCHED_IN;
1018 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001019
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001020 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001021 atoms->total_lat += delta;
Frederic Weisbecker37863102009-12-09 21:40:08 +01001022 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001023 atoms->max_lat = delta;
Frederic Weisbecker37863102009-12-09 21:40:08 +01001024 atoms->max_lat_at = timestamp;
1025 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001026 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027}
1028
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001029static void
1030latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001031 struct perf_session *session,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001032 struct event *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001033 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001034 u64 timestamp,
1035 struct thread *thread __used)
1036{
mingo39aeb522009-09-14 20:04:48 +02001037 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001038 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001039 u64 timestamp0;
1040 s64 delta;
1041
mingo39aeb522009-09-14 20:04:48 +02001042 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001043
1044 timestamp0 = cpu_last_switched[cpu];
1045 cpu_last_switched[cpu] = timestamp;
1046 if (timestamp0)
1047 delta = timestamp - timestamp0;
1048 else
1049 delta = 0;
1050
1051 if (delta < 0)
1052 die("hm, delta: %Ld < 0 ?\n", delta);
1053
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001054
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001055 sched_out = perf_session__findnew(session, switch_event->prev_pid);
1056 sched_in = perf_session__findnew(session, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001057
mingo39aeb522009-09-14 20:04:48 +02001058 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1059 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001060 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001061 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1062 if (!out_events)
1063 die("out-event: Internal tree error");
1064 }
1065 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1066
1067 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1068 if (!in_events) {
1069 thread_atoms_insert(sched_in);
1070 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1071 if (!in_events)
1072 die("in-event: Internal tree error");
1073 /*
1074 * Take came in we have not heard about yet,
1075 * add in an initial atom in runnable state:
1076 */
1077 add_sched_out_event(in_events, 'R', timestamp);
1078 }
1079 add_sched_in_event(in_events, timestamp);
1080}
1081
1082static void
1083latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001084 struct perf_session *session,
mingo39aeb522009-09-14 20:04:48 +02001085 struct event *event __used,
1086 int cpu,
1087 u64 timestamp,
1088 struct thread *this_thread __used)
1089{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001090 struct thread *thread = perf_session__findnew(session, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001091 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001092
1093 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001094 if (!atoms) {
1095 thread_atoms_insert(thread);
1096 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1097 if (!atoms)
1098 die("in-event: Internal tree error");
1099 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001100 }
1101
mingo39aeb522009-09-14 20:04:48 +02001102 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103}
1104
1105static void
1106latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001107 struct perf_session *session,
mingo39aeb522009-09-14 20:04:48 +02001108 struct event *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001109 int cpu __used,
1110 u64 timestamp,
1111 struct thread *thread __used)
1112{
mingo39aeb522009-09-14 20:04:48 +02001113 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001114 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001115 struct thread *wakee;
1116
1117 /* Note for later, it may be interesting to observe the failing cases */
1118 if (!wakeup_event->success)
1119 return;
1120
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001121 wakee = perf_session__findnew(session, wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001122 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001123 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001124 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001125 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1126 if (!atoms)
1127 die("wakeup-event: Internal tree error");
1128 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001129 }
1130
mingo39aeb522009-09-14 20:04:48 +02001131 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132
mingo39aeb522009-09-14 20:04:48 +02001133 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001135 /*
1136 * You WILL be missing events if you've recorded only
1137 * one CPU, or are only looking at only one, so don't
1138 * make useless noise.
1139 */
1140 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001141 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001143 nr_timestamps++;
1144 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001145 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001146 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001147 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001148
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001149 atom->state = THREAD_WAIT_CPU;
1150 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001151}
1152
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001153static void
1154latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001155 struct perf_session *session,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001156 struct event *__event __used,
1157 int cpu __used,
1158 u64 timestamp,
1159 struct thread *thread __used)
1160{
1161 struct work_atoms *atoms;
1162 struct work_atom *atom;
1163 struct thread *migrant;
1164
1165 /*
1166 * Only need to worry about migration when profiling one CPU.
1167 */
1168 if (profile_cpu == -1)
1169 return;
1170
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001171 migrant = perf_session__findnew(session, migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001172 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1173 if (!atoms) {
1174 thread_atoms_insert(migrant);
1175 register_pid(migrant->pid, migrant->comm);
1176 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1177 if (!atoms)
1178 die("migration-event: Internal tree error");
1179 add_sched_out_event(atoms, 'R', timestamp);
1180 }
1181
1182 BUG_ON(list_empty(&atoms->work_list));
1183
1184 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1185 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1186
1187 nr_timestamps++;
1188
1189 if (atom->sched_out_time > timestamp)
1190 nr_unordered_timestamps++;
1191}
1192
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001193static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001194 .wakeup_event = latency_wakeup_event,
1195 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001196 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001197 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001198 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001199};
1200
mingo39aeb522009-09-14 20:04:48 +02001201static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001202{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203 int i;
1204 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001205 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001206
mingo39aeb522009-09-14 20:04:48 +02001207 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001208 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001209 /*
1210 * Ignore idle threads:
1211 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001212 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001213 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001214
mingo39aeb522009-09-14 20:04:48 +02001215 all_runtime += work_list->total_runtime;
1216 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001217
Ingo Molnar80ed0982009-09-16 14:12:36 +02001218 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001219
mingo08f69e62009-09-14 18:30:44 +02001220 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001221 printf(" ");
1222
mingo39aeb522009-09-14 20:04:48 +02001223 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001224
Frederic Weisbecker37863102009-12-09 21:40:08 +01001225 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
mingo39aeb522009-09-14 20:04:48 +02001226 (double)work_list->total_runtime / 1e6,
1227 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker37863102009-12-09 21:40:08 +01001228 (double)work_list->max_lat / 1e6,
1229 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001230}
1231
mingo39aeb522009-09-14 20:04:48 +02001232static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001233{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001234 if (l->thread->pid < r->thread->pid)
1235 return -1;
1236 if (l->thread->pid > r->thread->pid)
1237 return 1;
1238
1239 return 0;
1240}
1241
1242static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001243 .name = "pid",
1244 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001245};
1246
mingo39aeb522009-09-14 20:04:48 +02001247static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001248{
1249 u64 avgl, avgr;
1250
1251 if (!l->nb_atoms)
1252 return -1;
1253
1254 if (!r->nb_atoms)
1255 return 1;
1256
1257 avgl = l->total_lat / l->nb_atoms;
1258 avgr = r->total_lat / r->nb_atoms;
1259
1260 if (avgl < avgr)
1261 return -1;
1262 if (avgl > avgr)
1263 return 1;
1264
1265 return 0;
1266}
1267
1268static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001269 .name = "avg",
1270 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001271};
1272
mingo39aeb522009-09-14 20:04:48 +02001273static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001274{
1275 if (l->max_lat < r->max_lat)
1276 return -1;
1277 if (l->max_lat > r->max_lat)
1278 return 1;
1279
1280 return 0;
1281}
1282
1283static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001284 .name = "max",
1285 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001286};
1287
mingo39aeb522009-09-14 20:04:48 +02001288static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001289{
1290 if (l->nb_atoms < r->nb_atoms)
1291 return -1;
1292 if (l->nb_atoms > r->nb_atoms)
1293 return 1;
1294
1295 return 0;
1296}
1297
1298static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001299 .name = "switch",
1300 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001301};
1302
mingo39aeb522009-09-14 20:04:48 +02001303static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001304{
1305 if (l->total_runtime < r->total_runtime)
1306 return -1;
1307 if (l->total_runtime > r->total_runtime)
1308 return 1;
1309
1310 return 0;
1311}
1312
1313static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001314 .name = "runtime",
1315 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001316};
1317
1318static struct sort_dimension *available_sorts[] = {
1319 &pid_sort_dimension,
1320 &avg_sort_dimension,
1321 &max_sort_dimension,
1322 &switch_sort_dimension,
1323 &runtime_sort_dimension,
1324};
1325
1326#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1327
1328static LIST_HEAD(sort_list);
1329
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001330static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001331{
1332 int i;
1333
1334 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1335 if (!strcmp(available_sorts[i]->name, tok)) {
1336 list_add_tail(&available_sorts[i]->list, list);
1337
1338 return 0;
1339 }
1340 }
1341
1342 return -1;
1343}
1344
1345static void setup_sorting(void);
1346
1347static void sort_lat(void)
1348{
1349 struct rb_node *node;
1350
1351 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001352 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001353 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001354 if (!node)
1355 break;
1356
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001357 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001358 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001359 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001360 }
1361}
1362
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001363static struct trace_sched_handler *trace_handler;
1364
1365static void
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001366process_sched_wakeup_event(void *data, struct perf_session *session,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001367 struct event *event,
1368 int cpu __used,
1369 u64 timestamp __used,
1370 struct thread *thread __used)
1371{
1372 struct trace_wakeup_event wakeup_event;
1373
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001374 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001375
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001376 FILL_ARRAY(wakeup_event, comm, event, data);
1377 FILL_FIELD(wakeup_event, pid, event, data);
1378 FILL_FIELD(wakeup_event, prio, event, data);
1379 FILL_FIELD(wakeup_event, success, event, data);
1380 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001381
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001382 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001383 trace_handler->wakeup_event(&wakeup_event, session, event,
1384 cpu, timestamp, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001385}
1386
Ingo Molnarc8a37752009-09-16 14:07:00 +02001387/*
1388 * Track the current task - that way we can know whether there's any
1389 * weird events, such as a task being switched away that is not current.
1390 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001391static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392
Ingo Molnarc8a37752009-09-16 14:07:00 +02001393static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1394
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001395static struct thread *curr_thread[MAX_CPUS];
1396
1397static char next_shortname1 = 'A';
1398static char next_shortname2 = '0';
1399
1400static void
1401map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001402 struct perf_session *session,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001403 struct event *event __used,
1404 int this_cpu,
1405 u64 timestamp,
1406 struct thread *thread __used)
1407{
1408 struct thread *sched_out, *sched_in;
1409 int new_shortname;
1410 u64 timestamp0;
1411 s64 delta;
1412 int cpu;
1413
1414 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1415
1416 if (this_cpu > max_cpu)
1417 max_cpu = this_cpu;
1418
1419 timestamp0 = cpu_last_switched[this_cpu];
1420 cpu_last_switched[this_cpu] = timestamp;
1421 if (timestamp0)
1422 delta = timestamp - timestamp0;
1423 else
1424 delta = 0;
1425
1426 if (delta < 0)
1427 die("hm, delta: %Ld < 0 ?\n", delta);
1428
1429
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001430 sched_out = perf_session__findnew(session, switch_event->prev_pid);
1431 sched_in = perf_session__findnew(session, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432
1433 curr_thread[this_cpu] = sched_in;
1434
1435 printf(" ");
1436
1437 new_shortname = 0;
1438 if (!sched_in->shortname[0]) {
1439 sched_in->shortname[0] = next_shortname1;
1440 sched_in->shortname[1] = next_shortname2;
1441
1442 if (next_shortname1 < 'Z') {
1443 next_shortname1++;
1444 } else {
1445 next_shortname1='A';
1446 if (next_shortname2 < '9') {
1447 next_shortname2++;
1448 } else {
1449 next_shortname2='0';
1450 }
1451 }
1452 new_shortname = 1;
1453 }
1454
1455 for (cpu = 0; cpu <= max_cpu; cpu++) {
1456 if (cpu != this_cpu)
1457 printf(" ");
1458 else
1459 printf("*");
1460
1461 if (curr_thread[cpu]) {
1462 if (curr_thread[cpu]->pid)
1463 printf("%2s ", curr_thread[cpu]->shortname);
1464 else
1465 printf(". ");
1466 } else
1467 printf(" ");
1468 }
1469
1470 printf(" %12.6f secs ", (double)timestamp/1e9);
1471 if (new_shortname) {
1472 printf("%s => %s:%d\n",
1473 sched_in->shortname, sched_in->comm, sched_in->pid);
1474 } else {
1475 printf("\n");
1476 }
1477}
1478
1479
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001480static void
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001481process_sched_switch_event(void *data, struct perf_session *session,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001482 struct event *event,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001483 int this_cpu,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001484 u64 timestamp __used,
1485 struct thread *thread __used)
1486{
1487 struct trace_switch_event switch_event;
1488
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001489 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001490
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001491 FILL_ARRAY(switch_event, prev_comm, event, data);
1492 FILL_FIELD(switch_event, prev_pid, event, data);
1493 FILL_FIELD(switch_event, prev_prio, event, data);
1494 FILL_FIELD(switch_event, prev_state, event, data);
1495 FILL_ARRAY(switch_event, next_comm, event, data);
1496 FILL_FIELD(switch_event, next_pid, event, data);
1497 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001498
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001499 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001500 /*
1501 * Are we trying to switch away a PID that is
1502 * not current?
1503 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001504 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001505 nr_context_switch_bugs++;
1506 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001507 if (trace_handler->switch_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001508 trace_handler->switch_event(&switch_event, session, event,
1509 this_cpu, timestamp, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001510
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001511 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001512}
1513
1514static void
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001515process_sched_runtime_event(void *data, struct perf_session *session,
mingo39aeb522009-09-14 20:04:48 +02001516 struct event *event,
1517 int cpu __used,
1518 u64 timestamp __used,
1519 struct thread *thread __used)
1520{
1521 struct trace_runtime_event runtime_event;
1522
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001523 FILL_ARRAY(runtime_event, comm, event, data);
1524 FILL_FIELD(runtime_event, pid, event, data);
1525 FILL_FIELD(runtime_event, runtime, event, data);
1526 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001527
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001528 if (trace_handler->runtime_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001529 trace_handler->runtime_event(&runtime_event, session, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001530}
1531
1532static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001533process_sched_fork_event(void *data,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001534 struct event *event,
1535 int cpu __used,
1536 u64 timestamp __used,
1537 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001538{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001539 struct trace_fork_event fork_event;
1540
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001541 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001542
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001543 FILL_ARRAY(fork_event, parent_comm, event, data);
1544 FILL_FIELD(fork_event, parent_pid, event, data);
1545 FILL_ARRAY(fork_event, child_comm, event, data);
1546 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001547
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001548 if (trace_handler->fork_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001549 trace_handler->fork_event(&fork_event, event,
1550 cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001551}
1552
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001553static void
1554process_sched_exit_event(struct event *event,
1555 int cpu __used,
1556 u64 timestamp __used,
1557 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001558{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001559 if (verbose)
1560 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001561}
1562
1563static void
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001564process_sched_migrate_task_event(void *data, struct perf_session *session,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001565 struct event *event,
1566 int cpu __used,
1567 u64 timestamp __used,
1568 struct thread *thread __used)
1569{
1570 struct trace_migrate_task_event migrate_task_event;
1571
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001572 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001573
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001574 FILL_ARRAY(migrate_task_event, comm, event, data);
1575 FILL_FIELD(migrate_task_event, pid, event, data);
1576 FILL_FIELD(migrate_task_event, prio, event, data);
1577 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001578
1579 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001580 trace_handler->migrate_task_event(&migrate_task_event, session,
1581 event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001582}
1583
1584static void
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001585process_raw_event(event_t *raw_event __used, struct perf_session *session,
1586 void *data, int cpu, u64 timestamp, struct thread *thread)
Ingo Molnarec156762009-09-11 12:12:54 +02001587{
Ingo Molnarec156762009-09-11 12:12:54 +02001588 struct event *event;
1589 int type;
1590
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +08001591
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001592 type = trace_parse_common_type(data);
Ingo Molnarec156762009-09-11 12:12:54 +02001593 event = trace_find_event(type);
1594
Ingo Molnarec156762009-09-11 12:12:54 +02001595 if (!strcmp(event->name, "sched_switch"))
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001596 process_sched_switch_event(data, session, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001597 if (!strcmp(event->name, "sched_stat_runtime"))
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001598 process_sched_runtime_event(data, session, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001599 if (!strcmp(event->name, "sched_wakeup"))
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001600 process_sched_wakeup_event(data, session, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001601 if (!strcmp(event->name, "sched_wakeup_new"))
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001602 process_sched_wakeup_event(data, session, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001603 if (!strcmp(event->name, "sched_process_fork"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001604 process_sched_fork_event(data, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001605 if (!strcmp(event->name, "sched_process_exit"))
1606 process_sched_exit_event(event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001607 if (!strcmp(event->name, "sched_migrate_task"))
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001608 process_sched_migrate_task_event(data, session, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001609}
1610
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001611static int process_sample_event(event_t *event, struct perf_session *session)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001612{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001613 struct sample_data data;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001614 struct thread *thread;
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001615
1616 if (!(sample_type & PERF_SAMPLE_RAW))
1617 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001618
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001619 memset(&data, 0, sizeof(data));
1620 data.time = -1;
1621 data.cpu = -1;
1622 data.period = -1;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001623
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001624 event__parse_sample(event, sample_type, &data);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001625
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001626 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001627 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001628 data.pid, data.tid,
1629 (void *)(long)data.ip,
1630 (long long)data.period);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001631
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001632 thread = perf_session__findnew(session, data.pid);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001633 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001634 pr_debug("problem processing %d event, skipping it.\n",
1635 event->header.type);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001636 return -1;
1637 }
1638
Julia Lawallf39cdf22009-10-17 08:43:17 +02001639 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1640
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +09001641 if (profile_cpu != -1 && profile_cpu != (int)data.cpu)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001642 return 0;
1643
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001644 process_raw_event(event, session, data.raw_data, data.cpu, data.time, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001645
1646 return 0;
1647}
1648
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001649static int process_lost_event(event_t *event __used,
1650 struct perf_session *session __used)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001651{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001652 nr_lost_chunks++;
1653 nr_lost_events += event->lost.lost;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001654
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001655 return 0;
1656}
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001657
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001658static int sample_type_check(u64 type)
1659{
1660 sample_type = type;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001661
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001662 if (!(sample_type & PERF_SAMPLE_RAW)) {
1663 fprintf(stderr,
1664 "No trace sample to read. Did you call perf record "
1665 "without -R?");
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001666 return -1;
1667 }
1668
1669 return 0;
1670}
1671
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -02001672static struct perf_event_ops event_ops = {
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001673 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02001674 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001675 .process_lost_event = process_lost_event,
1676 .sample_type_check = sample_type_check,
1677};
1678
Ingo Molnar46f392c2009-09-12 10:08:34 +02001679static int read_events(void)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001680{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001681 int err;
1682 struct perf_session *session = perf_session__new(input_name, O_RDONLY, 0);
1683
1684 if (session == NULL)
1685 return -ENOMEM;
1686
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -02001687 err = perf_session__process_events(session, &event_ops);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001688 perf_session__delete(session);
1689 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001690}
1691
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001692static void print_bad_events(void)
1693{
1694 if (nr_unordered_timestamps && nr_timestamps) {
1695 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1696 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1697 nr_unordered_timestamps, nr_timestamps);
1698 }
1699 if (nr_lost_events && nr_events) {
1700 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1701 (double)nr_lost_events/(double)nr_events*100.0,
1702 nr_lost_events, nr_events, nr_lost_chunks);
1703 }
1704 if (nr_state_machine_bugs && nr_timestamps) {
1705 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1706 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1707 nr_state_machine_bugs, nr_timestamps);
1708 if (nr_lost_events)
1709 printf(" (due to lost events?)");
1710 printf("\n");
1711 }
1712 if (nr_context_switch_bugs && nr_timestamps) {
1713 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1714 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1715 nr_context_switch_bugs, nr_timestamps);
1716 if (nr_lost_events)
1717 printf(" (due to lost events?)");
1718 printf("\n");
1719 }
1720}
1721
1722static void __cmd_lat(void)
1723{
1724 struct rb_node *next;
1725
1726 setup_pager();
1727 read_events();
1728 sort_lat();
1729
Frederic Weisbecker37863102009-12-09 21:40:08 +01001730 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1731 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1732 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
1734 next = rb_first(&sorted_atom_root);
1735
1736 while (next) {
1737 struct work_atoms *work_list;
1738
1739 work_list = rb_entry(next, struct work_atoms, node);
1740 output_lat_thread(work_list);
1741 next = rb_next(next);
1742 }
1743
1744 printf(" -----------------------------------------------------------------------------------------\n");
1745 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1746 (double)all_runtime/1e6, all_count);
1747
1748 printf(" ---------------------------------------------------\n");
1749
1750 print_bad_events();
1751 printf("\n");
1752
1753}
1754
1755static struct trace_sched_handler map_ops = {
1756 .wakeup_event = NULL,
1757 .switch_event = map_switch_event,
1758 .runtime_event = NULL,
1759 .fork_event = NULL,
1760};
1761
1762static void __cmd_map(void)
1763{
Ingo Molnar40749d02009-09-17 18:24:55 +02001764 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1765
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001766 setup_pager();
1767 read_events();
1768 print_bad_events();
1769}
1770
1771static void __cmd_replay(void)
1772{
1773 unsigned long i;
1774
1775 calibrate_run_measurement_overhead();
1776 calibrate_sleep_measurement_overhead();
1777
1778 test_calibrations();
1779
1780 read_events();
1781
1782 printf("nr_run_events: %ld\n", nr_run_events);
1783 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1784 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1785
1786 if (targetless_wakeups)
1787 printf("target-less wakeups: %ld\n", targetless_wakeups);
1788 if (multitarget_wakeups)
1789 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1790 if (nr_run_events_optimized)
1791 printf("run atoms optimized: %ld\n",
1792 nr_run_events_optimized);
1793
1794 print_task_traces();
1795 add_cross_task_wakeups();
1796
1797 create_tasks();
1798 printf("------------------------------------------------------------\n");
1799 for (i = 0; i < replay_repeat; i++)
1800 run_one_test();
1801}
1802
1803
Ingo Molnar46f392c2009-09-12 10:08:34 +02001804static const char * const sched_usage[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001805 "perf sched [<options>] {record|latency|map|replay|trace}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001806 NULL
1807};
1808
Ingo Molnarf2858d82009-09-11 12:12:54 +02001809static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001810 OPT_STRING('i', "input", &input_name, "file",
1811 "input file name"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001812 OPT_BOOLEAN('v', "verbose", &verbose,
1813 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001814 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1815 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001816 OPT_END()
1817};
1818
1819static const char * const latency_usage[] = {
1820 "perf sched latency [<options>]",
1821 NULL
1822};
1823
1824static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001825 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1826 "sort by key(s): runtime, switch, avg, max"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001827 OPT_BOOLEAN('v', "verbose", &verbose,
1828 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001829 OPT_INTEGER('C', "CPU", &profile_cpu,
1830 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001831 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1832 "dump raw trace in ASCII"),
1833 OPT_END()
1834};
1835
1836static const char * const replay_usage[] = {
1837 "perf sched replay [<options>]",
1838 NULL
1839};
1840
1841static const struct option replay_options[] = {
1842 OPT_INTEGER('r', "repeat", &replay_repeat,
1843 "repeat the workload replay N times (-1: infinite)"),
1844 OPT_BOOLEAN('v', "verbose", &verbose,
1845 "be more verbose (show symbol address, etc)"),
1846 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1847 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001848 OPT_END()
1849};
1850
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001851static void setup_sorting(void)
1852{
1853 char *tmp, *tok, *str = strdup(sort_order);
1854
1855 for (tok = strtok_r(str, ", ", &tmp);
1856 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1857 if (sort_dimension__add(tok, &sort_list) < 0) {
1858 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001859 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001860 }
1861 }
1862
1863 free(str);
1864
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001865 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001866}
1867
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001868static const char *record_args[] = {
1869 "record",
1870 "-a",
1871 "-R",
Frederic Weisbeckerd1302522009-09-14 08:57:15 +02001872 "-M",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001873 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001874 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001875 "-c", "1",
1876 "-e", "sched:sched_switch:r",
1877 "-e", "sched:sched_stat_wait:r",
1878 "-e", "sched:sched_stat_sleep:r",
1879 "-e", "sched:sched_stat_iowait:r",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001880 "-e", "sched:sched_stat_runtime:r",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001881 "-e", "sched:sched_process_exit:r",
1882 "-e", "sched:sched_process_fork:r",
1883 "-e", "sched:sched_wakeup:r",
1884 "-e", "sched:sched_migrate_task:r",
1885};
1886
1887static int __cmd_record(int argc, const char **argv)
1888{
1889 unsigned int rec_argc, i, j;
1890 const char **rec_argv;
1891
1892 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1893 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1894
1895 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1896 rec_argv[i] = strdup(record_args[i]);
1897
1898 for (j = 1; j < (unsigned int)argc; j++, i++)
1899 rec_argv[i] = argv[j];
1900
1901 BUG_ON(i != rec_argc);
1902
1903 return cmd_record(i, rec_argv, NULL);
1904}
1905
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001906int cmd_sched(int argc, const char **argv, const char *prefix __used)
1907{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001908 argc = parse_options(argc, argv, sched_options, sched_usage,
1909 PARSE_OPT_STOP_AT_NON_OPTION);
1910 if (!argc)
1911 usage_with_options(sched_usage, sched_options);
1912
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001913 /*
1914 * Aliased to 'perf trace' for now:
1915 */
1916 if (!strcmp(argv[0], "trace"))
1917 return cmd_trace(argc, argv, prefix);
1918
1919 symbol__init(0);
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001920 if (!strncmp(argv[0], "rec", 3)) {
1921 return __cmd_record(argc, argv);
1922 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001923 trace_handler = &lat_ops;
1924 if (argc > 1) {
1925 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1926 if (argc)
1927 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001928 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001929 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001930 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001931 } else if (!strcmp(argv[0], "map")) {
1932 trace_handler = &map_ops;
1933 setup_sorting();
1934 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001935 } else if (!strncmp(argv[0], "rep", 3)) {
1936 trace_handler = &replay_ops;
1937 if (argc) {
1938 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1939 if (argc)
1940 usage_with_options(replay_usage, replay_options);
1941 }
1942 __cmd_replay();
1943 } else {
1944 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001945 }
1946
Ingo Molnarec156762009-09-11 12:12:54 +02001947 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001948}