blob: 7a9ad2b1ee7601de26ed2d97ff91fae39a46f185 [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"
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02005#include "util/evlist.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02006#include "util/cache.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02007#include "util/evsel.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02008#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020013
14#include "util/parse-options.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020015#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020016
Ingo Molnar0a02ad92009-09-11 12:12:54 +020017#include "util/debug.h"
18
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020019#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020020#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020022#include <semaphore.h>
23#include <pthread.h>
24#include <math.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020025
Robert Richterefad1412011-12-07 10:02:54 +010026static const char *input_name;
Ingo Molnar0a02ad92009-09-11 12:12:54 +020027
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020028static char default_sort_order[] = "avg, max, switch, runtime";
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030029static const char *sort_order = default_sort_order;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020030
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
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -030046struct perf_sched {
47 struct perf_tool tool;
48 struct perf_session *session;
49};
50
mingo39aeb522009-09-14 20:04:48 +020051struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020052
53struct task_desc {
54 unsigned long nr;
55 unsigned long pid;
56 char comm[COMM_LEN];
57
58 unsigned long nr_events;
59 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020060 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020061
62 pthread_t thread;
63 sem_t sleep_sem;
64
65 sem_t ready_for_work;
66 sem_t work_done_sem;
67
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020068 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020069};
70
71enum sched_event_type {
72 SCHED_EVENT_RUN,
73 SCHED_EVENT_SLEEP,
74 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020075 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020076};
77
mingo39aeb522009-09-14 20:04:48 +020078struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020079 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030080 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020081 u64 timestamp;
82 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020083 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020084 sem_t *wait_sem;
85 struct task_desc *wakee;
86};
87
88static struct task_desc *pid_to_task[MAX_PID];
89
90static struct task_desc **tasks;
91
92static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020093static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020094
95static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
96
97static unsigned long nr_run_events;
98static unsigned long nr_sleep_events;
99static unsigned long nr_wakeup_events;
100
101static unsigned long nr_sleep_corrections;
102static unsigned long nr_run_events_optimized;
103
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200104static unsigned long targetless_wakeups;
105static unsigned long multitarget_wakeups;
106
107static u64 cpu_usage;
108static u64 runavg_cpu_usage;
109static u64 parent_cpu_usage;
110static u64 runavg_parent_cpu_usage;
111
112static unsigned long nr_runs;
113static u64 sum_runtime;
114static u64 sum_fluct;
115static u64 run_avg;
116
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -0300117static unsigned int replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200118static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200119static unsigned long nr_unordered_timestamps;
120static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200121static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200122static unsigned long nr_events;
123static unsigned long nr_lost_chunks;
124static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200125
126#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
127
128enum thread_state {
129 THREAD_SLEEPING = 0,
130 THREAD_WAIT_CPU,
131 THREAD_SCHED_IN,
132 THREAD_IGNORE
133};
134
135struct work_atom {
136 struct list_head list;
137 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200138 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200139 u64 wake_up_time;
140 u64 sched_in_time;
141 u64 runtime;
142};
143
mingo39aeb522009-09-14 20:04:48 +0200144struct work_atoms {
145 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200146 struct thread *thread;
147 struct rb_node node;
148 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100149 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200150 u64 total_lat;
151 u64 nb_atoms;
152 u64 total_runtime;
153};
154
mingo39aeb522009-09-14 20:04:48 +0200155typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200156
157static struct rb_root atom_root, sorted_atom_root;
158
159static u64 all_runtime;
160static u64 all_count;
161
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200162
163static u64 get_nsecs(void)
164{
165 struct timespec ts;
166
167 clock_gettime(CLOCK_MONOTONIC, &ts);
168
169 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
170}
171
172static void burn_nsecs(u64 nsecs)
173{
174 u64 T0 = get_nsecs(), T1;
175
176 do {
177 T1 = get_nsecs();
178 } while (T1 + run_measurement_overhead < T0 + nsecs);
179}
180
181static void sleep_nsecs(u64 nsecs)
182{
183 struct timespec ts;
184
185 ts.tv_nsec = nsecs % 999999999;
186 ts.tv_sec = nsecs / 999999999;
187
188 nanosleep(&ts, NULL);
189}
190
191static void calibrate_run_measurement_overhead(void)
192{
193 u64 T0, T1, delta, min_delta = 1000000000ULL;
194 int i;
195
196 for (i = 0; i < 10; i++) {
197 T0 = get_nsecs();
198 burn_nsecs(0);
199 T1 = get_nsecs();
200 delta = T1-T0;
201 min_delta = min(min_delta, delta);
202 }
203 run_measurement_overhead = min_delta;
204
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200205 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200206}
207
208static void calibrate_sleep_measurement_overhead(void)
209{
210 u64 T0, T1, delta, min_delta = 1000000000ULL;
211 int i;
212
213 for (i = 0; i < 10; i++) {
214 T0 = get_nsecs();
215 sleep_nsecs(10000);
216 T1 = get_nsecs();
217 delta = T1-T0;
218 min_delta = min(min_delta, delta);
219 }
220 min_delta -= 10000;
221 sleep_measurement_overhead = min_delta;
222
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200223 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200224}
225
mingo39aeb522009-09-14 20:04:48 +0200226static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200227get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200228{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200229 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200230 unsigned long idx = task->nr_events;
231 size_t size;
232
233 event->timestamp = timestamp;
234 event->nr = idx;
235
236 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200237 size = sizeof(struct sched_atom *) * task->nr_events;
238 task->atoms = realloc(task->atoms, size);
239 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200240
mingo39aeb522009-09-14 20:04:48 +0200241 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200242
243 return event;
244}
245
mingo39aeb522009-09-14 20:04:48 +0200246static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200247{
248 if (!task->nr_events)
249 return NULL;
250
mingo39aeb522009-09-14 20:04:48 +0200251 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200252}
253
254static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200255add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200256{
mingo39aeb522009-09-14 20:04:48 +0200257 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200258
259 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200260 * optimize an existing RUN event by merging this one
261 * to it:
262 */
Ingo Molnarec156762009-09-11 12:12:54 +0200263 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
264 nr_run_events_optimized++;
265 curr_event->duration += duration;
266 return;
267 }
268
269 event = get_new_event(task, timestamp);
270
271 event->type = SCHED_EVENT_RUN;
272 event->duration = duration;
273
274 nr_run_events++;
275}
276
Ingo Molnarec156762009-09-11 12:12:54 +0200277static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200278add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200279 struct task_desc *wakee)
280{
mingo39aeb522009-09-14 20:04:48 +0200281 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200282
283 event = get_new_event(task, timestamp);
284 event->type = SCHED_EVENT_WAKEUP;
285 event->wakee = wakee;
286
287 wakee_event = last_event(wakee);
288 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
289 targetless_wakeups++;
290 return;
291 }
292 if (wakee_event->wait_sem) {
293 multitarget_wakeups++;
294 return;
295 }
296
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200297 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200298 sem_init(wakee_event->wait_sem, 0, 0);
299 wakee_event->specific_wait = 1;
300 event->wait_sem = wakee_event->wait_sem;
301
302 nr_wakeup_events++;
303}
304
305static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200306add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200307 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200308{
mingo39aeb522009-09-14 20:04:48 +0200309 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200310
311 event->type = SCHED_EVENT_SLEEP;
312
313 nr_sleep_events++;
314}
315
316static struct task_desc *register_pid(unsigned long pid, const char *comm)
317{
318 struct task_desc *task;
319
320 BUG_ON(pid >= MAX_PID);
321
322 task = pid_to_task[pid];
323
324 if (task)
325 return task;
326
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200327 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200328 task->pid = pid;
329 task->nr = nr_tasks;
330 strcpy(task->comm, comm);
331 /*
332 * every task starts in sleeping state - this gets ignored
333 * if there's no wakeup pointing to this sleep state:
334 */
335 add_sched_event_sleep(task, 0, 0);
336
337 pid_to_task[pid] = task;
338 nr_tasks++;
339 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
340 BUG_ON(!tasks);
341 tasks[task->nr] = task;
342
Ingo Molnarad236fd2009-09-11 12:12:54 +0200343 if (verbose)
344 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200345
346 return task;
347}
348
349
Ingo Molnarec156762009-09-11 12:12:54 +0200350static void print_task_traces(void)
351{
352 struct task_desc *task;
353 unsigned long i;
354
355 for (i = 0; i < nr_tasks; i++) {
356 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200357 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200358 task->nr, task->comm, task->pid, task->nr_events);
359 }
360}
361
362static void add_cross_task_wakeups(void)
363{
364 struct task_desc *task1, *task2;
365 unsigned long i, j;
366
367 for (i = 0; i < nr_tasks; i++) {
368 task1 = tasks[i];
369 j = i + 1;
370 if (j == nr_tasks)
371 j = 0;
372 task2 = tasks[j];
373 add_sched_event_wakeup(task1, 0, task2);
374 }
375}
376
377static void
mingo39aeb522009-09-14 20:04:48 +0200378process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200379{
380 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200381
mingo39aeb522009-09-14 20:04:48 +0200382 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200383 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200384 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200385 break;
386 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200387 if (atom->wait_sem)
388 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200389 BUG_ON(ret);
390 break;
391 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200392 if (atom->wait_sem)
393 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200394 BUG_ON(ret);
395 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200396 case SCHED_EVENT_MIGRATION:
397 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200398 default:
399 BUG_ON(1);
400 }
401}
402
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200403static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200404{
405 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200406 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200407 int err;
408
409 err = getrusage(RUSAGE_SELF, &ru);
410 BUG_ON(err);
411
412 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
413 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
414
415 return sum;
416}
417
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800418static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200419{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800420 struct perf_event_attr attr;
421 int fd;
422
423 memset(&attr, 0, sizeof(attr));
424
425 attr.type = PERF_TYPE_SOFTWARE;
426 attr.config = PERF_COUNT_SW_TASK_CLOCK;
427
428 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
429
430 if (fd < 0)
431 die("Error: sys_perf_event_open() syscall returned"
432 "with %d (%s)\n", fd, strerror(errno));
433 return fd;
434}
435
436static u64 get_cpu_usage_nsec_self(int fd)
437{
438 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200439 int ret;
440
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800441 ret = read(fd, &runtime, sizeof(runtime));
442 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200443
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800444 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200445}
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];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800453 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200454
Ingo Molnarec156762009-09-11 12:12:54 +0200455 sprintf(comm2, ":%s", this_task->comm);
456 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800457 fd = self_open_counters();
Ingo Molnarec156762009-09-11 12:12:54 +0200458
459again:
460 ret = sem_post(&this_task->ready_for_work);
461 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200462 ret = pthread_mutex_lock(&start_work_mutex);
463 BUG_ON(ret);
464 ret = pthread_mutex_unlock(&start_work_mutex);
465 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200466
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800467 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200468
469 for (i = 0; i < this_task->nr_events; i++) {
470 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200471 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200472 }
473
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800474 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200475 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200476 ret = sem_post(&this_task->work_done_sem);
477 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200478
479 ret = pthread_mutex_lock(&work_done_wait_mutex);
480 BUG_ON(ret);
481 ret = pthread_mutex_unlock(&work_done_wait_mutex);
482 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200483
484 goto again;
485}
486
487static void create_tasks(void)
488{
489 struct task_desc *task;
490 pthread_attr_t attr;
491 unsigned long i;
492 int err;
493
494 err = pthread_attr_init(&attr);
495 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200496 err = pthread_attr_setstacksize(&attr,
497 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200498 BUG_ON(err);
499 err = pthread_mutex_lock(&start_work_mutex);
500 BUG_ON(err);
501 err = pthread_mutex_lock(&work_done_wait_mutex);
502 BUG_ON(err);
503 for (i = 0; i < nr_tasks; i++) {
504 task = tasks[i];
505 sem_init(&task->sleep_sem, 0, 0);
506 sem_init(&task->ready_for_work, 0, 0);
507 sem_init(&task->work_done_sem, 0, 0);
508 task->curr_event = 0;
509 err = pthread_create(&task->thread, &attr, thread_func, task);
510 BUG_ON(err);
511 }
512}
513
Ingo Molnarec156762009-09-11 12:12:54 +0200514static void wait_for_tasks(void)
515{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200516 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200517 struct task_desc *task;
518 unsigned long i, ret;
519
Ingo Molnarec156762009-09-11 12:12:54 +0200520 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200521 cpu_usage = 0;
522 pthread_mutex_unlock(&work_done_wait_mutex);
523
524 for (i = 0; i < nr_tasks; i++) {
525 task = tasks[i];
526 ret = sem_wait(&task->ready_for_work);
527 BUG_ON(ret);
528 sem_init(&task->ready_for_work, 0, 0);
529 }
530 ret = pthread_mutex_lock(&work_done_wait_mutex);
531 BUG_ON(ret);
532
533 cpu_usage_0 = get_cpu_usage_nsec_parent();
534
535 pthread_mutex_unlock(&start_work_mutex);
536
Ingo Molnarec156762009-09-11 12:12:54 +0200537 for (i = 0; i < nr_tasks; i++) {
538 task = tasks[i];
539 ret = sem_wait(&task->work_done_sem);
540 BUG_ON(ret);
541 sem_init(&task->work_done_sem, 0, 0);
542 cpu_usage += task->cpu_usage;
543 task->cpu_usage = 0;
544 }
545
546 cpu_usage_1 = get_cpu_usage_nsec_parent();
547 if (!runavg_cpu_usage)
548 runavg_cpu_usage = cpu_usage;
549 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
550
551 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
552 if (!runavg_parent_cpu_usage)
553 runavg_parent_cpu_usage = parent_cpu_usage;
554 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
555 parent_cpu_usage)/10;
556
557 ret = pthread_mutex_lock(&start_work_mutex);
558 BUG_ON(ret);
559
560 for (i = 0; i < nr_tasks; i++) {
561 task = tasks[i];
562 sem_init(&task->sleep_sem, 0, 0);
563 task->curr_event = 0;
564 }
565}
566
Ingo Molnarec156762009-09-11 12:12:54 +0200567static void run_one_test(void)
568{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500569 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200570
571 T0 = get_nsecs();
572 wait_for_tasks();
573 T1 = get_nsecs();
574
575 delta = T1 - T0;
576 sum_runtime += delta;
577 nr_runs++;
578
579 avg_delta = sum_runtime / nr_runs;
580 if (delta < avg_delta)
581 fluct = avg_delta - delta;
582 else
583 fluct = delta - avg_delta;
584 sum_fluct += fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200585 if (!run_avg)
586 run_avg = delta;
587 run_avg = (run_avg*9 + delta)/10;
588
Ingo Molnarad236fd2009-09-11 12:12:54 +0200589 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200590 nr_runs, (double)delta/1000000.0);
591
Ingo Molnarad236fd2009-09-11 12:12:54 +0200592 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200593 (double)run_avg/1e6);
594
Ingo Molnarad236fd2009-09-11 12:12:54 +0200595 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200596 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
597
598#if 0
599 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200600 * rusage statistics done by the parent, these are less
601 * accurate than the sum_exec_runtime based statistics:
602 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200603 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200604 (double)parent_cpu_usage/1e6,
605 (double)runavg_parent_cpu_usage/1e6);
606#endif
607
Ingo Molnarad236fd2009-09-11 12:12:54 +0200608 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200609
610 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200611 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200612 nr_sleep_corrections = 0;
613}
614
615static void test_calibrations(void)
616{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200617 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200618
619 T0 = get_nsecs();
620 burn_nsecs(1e6);
621 T1 = get_nsecs();
622
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200623 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200624
625 T0 = get_nsecs();
626 sleep_nsecs(1e6);
627 T1 = get_nsecs();
628
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200629 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200630}
631
Frederic Weisbecker46538812009-09-12 02:43:45 +0200632#define FILL_FIELD(ptr, field, event, data) \
633 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
634
635#define FILL_ARRAY(ptr, array, event, data) \
636do { \
637 void *__array = raw_field_ptr(event, #array, data); \
638 memcpy(ptr.array, __array, sizeof(ptr.array)); \
639} while(0)
640
641#define FILL_COMMON_FIELDS(ptr, event, data) \
642do { \
643 FILL_FIELD(ptr, common_type, event, data); \
644 FILL_FIELD(ptr, common_flags, event, data); \
645 FILL_FIELD(ptr, common_preempt_count, event, data); \
646 FILL_FIELD(ptr, common_pid, event, data); \
647 FILL_FIELD(ptr, common_tgid, event, data); \
648} while (0)
649
Ingo Molnarfbf94822009-09-11 12:12:54 +0200650
Ingo Molnarec156762009-09-11 12:12:54 +0200651
Ingo Molnarfbf94822009-09-11 12:12:54 +0200652struct trace_switch_event {
653 u32 size;
654
655 u16 common_type;
656 u8 common_flags;
657 u8 common_preempt_count;
658 u32 common_pid;
659 u32 common_tgid;
660
661 char prev_comm[16];
662 u32 prev_pid;
663 u32 prev_prio;
664 u64 prev_state;
665 char next_comm[16];
666 u32 next_pid;
667 u32 next_prio;
668};
669
mingo39aeb522009-09-14 20:04:48 +0200670struct trace_runtime_event {
671 u32 size;
672
673 u16 common_type;
674 u8 common_flags;
675 u8 common_preempt_count;
676 u32 common_pid;
677 u32 common_tgid;
678
679 char comm[16];
680 u32 pid;
681 u64 runtime;
682 u64 vruntime;
683};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200684
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200685struct trace_wakeup_event {
686 u32 size;
687
688 u16 common_type;
689 u8 common_flags;
690 u8 common_preempt_count;
691 u32 common_pid;
692 u32 common_tgid;
693
694 char comm[16];
695 u32 pid;
696
697 u32 prio;
698 u32 success;
699 u32 cpu;
700};
701
702struct trace_fork_event {
703 u32 size;
704
705 u16 common_type;
706 u8 common_flags;
707 u8 common_preempt_count;
708 u32 common_pid;
709 u32 common_tgid;
710
711 char parent_comm[16];
712 u32 parent_pid;
713 char child_comm[16];
714 u32 child_pid;
715};
716
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200717struct trace_migrate_task_event {
718 u32 size;
719
720 u16 common_type;
721 u8 common_flags;
722 u8 common_preempt_count;
723 u32 common_pid;
724 u32 common_tgid;
725
726 char comm[16];
727 u32 pid;
728
729 u32 prio;
730 u32 cpu;
731};
732
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200733struct trace_sched_handler {
734 void (*switch_event)(struct trace_switch_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200735 struct machine *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200736 struct event_format *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200737 int cpu,
738 u64 timestamp,
739 struct thread *thread);
740
mingo39aeb522009-09-14 20:04:48 +0200741 void (*runtime_event)(struct trace_runtime_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200742 struct machine *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200743 struct event_format *,
mingo39aeb522009-09-14 20:04:48 +0200744 int cpu,
745 u64 timestamp,
746 struct thread *thread);
747
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200748 void (*wakeup_event)(struct trace_wakeup_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200749 struct machine *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200750 struct event_format *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200751 int cpu,
752 u64 timestamp,
753 struct thread *thread);
754
755 void (*fork_event)(struct trace_fork_event *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200756 struct event_format *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200757 int cpu,
758 u64 timestamp,
759 struct thread *thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200760
761 void (*migrate_task_event)(struct trace_migrate_task_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200762 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200763 struct event_format *,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200764 int cpu,
765 u64 timestamp,
766 struct thread *thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200767};
768
Ingo Molnarfbf94822009-09-11 12:12:54 +0200769
770static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200771replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200772 struct machine *machine __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200773 struct event_format *event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200774 int cpu __used,
775 u64 timestamp __used,
776 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200777{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200778 struct task_desc *waker, *wakee;
779
780 if (verbose) {
781 printf("sched_wakeup event %p\n", event);
782
783 printf(" ... pid %d woke up %s/%d\n",
784 wakeup_event->common_pid,
785 wakeup_event->comm,
786 wakeup_event->pid);
787 }
788
789 waker = register_pid(wakeup_event->common_pid, "<unknown>");
790 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
791
792 add_sched_event_wakeup(waker, timestamp, wakee);
793}
794
Ingo Molnard1153382009-09-14 18:22:53 +0200795static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200796
797static void
798replay_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200799 struct machine *machine __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200800 struct event_format *event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200801 int cpu,
802 u64 timestamp,
803 struct thread *thread __used)
804{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500805 struct task_desc *prev, __used *next;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200806 u64 timestamp0;
807 s64 delta;
808
Ingo Molnarad236fd2009-09-11 12:12:54 +0200809 if (verbose)
810 printf("sched_switch event %p\n", event);
811
Ingo Molnarfbf94822009-09-11 12:12:54 +0200812 if (cpu >= MAX_CPUS || cpu < 0)
813 return;
814
815 timestamp0 = cpu_last_switched[cpu];
816 if (timestamp0)
817 delta = timestamp - timestamp0;
818 else
819 delta = 0;
820
821 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200822 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200823
Ingo Molnarad236fd2009-09-11 12:12:54 +0200824 if (verbose) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200825 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200826 switch_event->prev_comm, switch_event->prev_pid,
827 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200828 delta);
829 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200830
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200831 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
832 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200833
834 cpu_last_switched[cpu] = timestamp;
835
836 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200837 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200838}
839
Ingo Molnarfbf94822009-09-11 12:12:54 +0200840
841static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200842replay_fork_event(struct trace_fork_event *fork_event,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200843 struct event_format *event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200844 int cpu __used,
845 u64 timestamp __used,
846 struct thread *thread __used)
847{
848 if (verbose) {
849 printf("sched_fork event %p\n", event);
850 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
851 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
852 }
853 register_pid(fork_event->parent_pid, fork_event->parent_comm);
854 register_pid(fork_event->child_pid, fork_event->child_comm);
855}
856
857static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200858 .wakeup_event = replay_wakeup_event,
859 .switch_event = replay_switch_event,
860 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200861};
862
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200863struct sort_dimension {
864 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200865 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200866 struct list_head list;
867};
868
869static LIST_HEAD(cmp_pid);
870
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200871static int
mingo39aeb522009-09-14 20:04:48 +0200872thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200873{
874 struct sort_dimension *sort;
875 int ret = 0;
876
Ingo Molnarb5fae122009-09-11 12:12:54 +0200877 BUG_ON(list_empty(list));
878
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200879 list_for_each_entry(sort, list, list) {
880 ret = sort->cmp(l, r);
881 if (ret)
882 return ret;
883 }
884
885 return ret;
886}
887
mingo39aeb522009-09-14 20:04:48 +0200888static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200889thread_atoms_search(struct rb_root *root, struct thread *thread,
890 struct list_head *sort_list)
891{
892 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200893 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200894
895 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200896 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200897 int cmp;
898
mingo39aeb522009-09-14 20:04:48 +0200899 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200900
901 cmp = thread_lat_cmp(sort_list, &key, atoms);
902 if (cmp > 0)
903 node = node->rb_left;
904 else if (cmp < 0)
905 node = node->rb_right;
906 else {
907 BUG_ON(thread != atoms->thread);
908 return atoms;
909 }
910 }
911 return NULL;
912}
913
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200914static void
mingo39aeb522009-09-14 20:04:48 +0200915__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200916 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200917{
918 struct rb_node **new = &(root->rb_node), *parent = NULL;
919
920 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200921 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200922 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923
mingo39aeb522009-09-14 20:04:48 +0200924 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200925 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200926
927 cmp = thread_lat_cmp(sort_list, data, this);
928
929 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200931 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200932 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933 }
934
935 rb_link_node(&data->node, parent, new);
936 rb_insert_color(&data->node, root);
937}
938
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200939static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200940{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200941 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Frederic Weisbecker17562202009-09-12 23:11:32 +0200942 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200943 die("No memory");
944
Frederic Weisbecker17562202009-09-12 23:11:32 +0200945 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200946 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200947 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948}
949
950static void
951latency_fork_event(struct trace_fork_event *fork_event __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200952 struct event_format *event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200953 int cpu __used,
954 u64 timestamp __used,
955 struct thread *thread __used)
956{
957 /* should insert the newcomer */
958}
959
Ingo Molnarea92ed52009-09-12 10:08:34 +0200960__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200961static char sched_out_state(struct trace_switch_event *switch_event)
962{
963 const char *str = TASK_STATE_TO_CHAR_STR;
964
965 return str[switch_event->prev_state];
966}
967
968static void
mingo39aeb522009-09-14 20:04:48 +0200969add_sched_out_event(struct work_atoms *atoms,
970 char run_state,
971 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200972{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200973 struct work_atom *atom = zalloc(sizeof(*atom));
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200974 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200975 die("Non memory");
976
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200977 atom->sched_out_time = timestamp;
978
mingo39aeb522009-09-14 20:04:48 +0200979 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200980 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200981 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200982 }
983
mingo39aeb522009-09-14 20:04:48 +0200984 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200985}
986
987static void
mingo39aeb522009-09-14 20:04:48 +0200988add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
989{
990 struct work_atom *atom;
991
992 BUG_ON(list_empty(&atoms->work_list));
993
994 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
995
996 atom->runtime += delta;
997 atoms->total_runtime += delta;
998}
999
1000static void
1001add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001002{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001003 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001004 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001005
mingo39aeb522009-09-14 20:04:48 +02001006 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001007 return;
1008
mingo39aeb522009-09-14 20:04:48 +02001009 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001010
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001011 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001012 return;
1013
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001014 if (timestamp < atom->wake_up_time) {
1015 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001016 return;
1017 }
1018
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001019 atom->state = THREAD_SCHED_IN;
1020 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001021
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001022 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001023 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001024 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001025 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001026 atoms->max_lat_at = timestamp;
1027 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001028 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001029}
1030
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001031static void
1032latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001033 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001034 struct event_format *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001035 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001036 u64 timestamp,
1037 struct thread *thread __used)
1038{
mingo39aeb522009-09-14 20:04:48 +02001039 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001040 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001041 u64 timestamp0;
1042 s64 delta;
1043
mingo39aeb522009-09-14 20:04:48 +02001044 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001045
1046 timestamp0 = cpu_last_switched[cpu];
1047 cpu_last_switched[cpu] = timestamp;
1048 if (timestamp0)
1049 delta = timestamp - timestamp0;
1050 else
1051 delta = 0;
1052
1053 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001054 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001055
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001056
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001057 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1058 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001059
mingo39aeb522009-09-14 20:04:48 +02001060 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1061 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001062 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001063 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1064 if (!out_events)
1065 die("out-event: Internal tree error");
1066 }
1067 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1068
1069 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1070 if (!in_events) {
1071 thread_atoms_insert(sched_in);
1072 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1073 if (!in_events)
1074 die("in-event: Internal tree error");
1075 /*
1076 * Take came in we have not heard about yet,
1077 * add in an initial atom in runnable state:
1078 */
1079 add_sched_out_event(in_events, 'R', timestamp);
1080 }
1081 add_sched_in_event(in_events, timestamp);
1082}
1083
1084static void
1085latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001086 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001087 struct event_format *event __used,
mingo39aeb522009-09-14 20:04:48 +02001088 int cpu,
1089 u64 timestamp,
1090 struct thread *this_thread __used)
1091{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001092 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001093 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001094
1095 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001096 if (!atoms) {
1097 thread_atoms_insert(thread);
1098 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1099 if (!atoms)
1100 die("in-event: Internal tree error");
1101 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001102 }
1103
mingo39aeb522009-09-14 20:04:48 +02001104 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001105}
1106
1107static void
1108latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001109 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001110 struct event_format *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001111 int cpu __used,
1112 u64 timestamp,
1113 struct thread *thread __used)
1114{
mingo39aeb522009-09-14 20:04:48 +02001115 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001116 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001117 struct thread *wakee;
1118
1119 /* Note for later, it may be interesting to observe the failing cases */
1120 if (!wakeup_event->success)
1121 return;
1122
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001123 wakee = machine__findnew_thread(machine, wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001124 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001125 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001126 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001127 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1128 if (!atoms)
1129 die("wakeup-event: Internal tree error");
1130 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001131 }
1132
mingo39aeb522009-09-14 20:04:48 +02001133 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134
mingo39aeb522009-09-14 20:04:48 +02001135 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001136
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001137 /*
1138 * You WILL be missing events if you've recorded only
1139 * one CPU, or are only looking at only one, so don't
1140 * make useless noise.
1141 */
1142 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001143 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001144
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001145 nr_timestamps++;
1146 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001147 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001148 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001149 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001150
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001151 atom->state = THREAD_WAIT_CPU;
1152 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001153}
1154
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001155static void
1156latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001157 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001158 struct event_format *__event __used,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001159 int cpu __used,
1160 u64 timestamp,
1161 struct thread *thread __used)
1162{
1163 struct work_atoms *atoms;
1164 struct work_atom *atom;
1165 struct thread *migrant;
1166
1167 /*
1168 * Only need to worry about migration when profiling one CPU.
1169 */
1170 if (profile_cpu == -1)
1171 return;
1172
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001173 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001174 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1175 if (!atoms) {
1176 thread_atoms_insert(migrant);
1177 register_pid(migrant->pid, migrant->comm);
1178 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1179 if (!atoms)
1180 die("migration-event: Internal tree error");
1181 add_sched_out_event(atoms, 'R', timestamp);
1182 }
1183
1184 BUG_ON(list_empty(&atoms->work_list));
1185
1186 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1187 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1188
1189 nr_timestamps++;
1190
1191 if (atom->sched_out_time > timestamp)
1192 nr_unordered_timestamps++;
1193}
1194
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001195static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001196 .wakeup_event = latency_wakeup_event,
1197 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001198 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001199 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001200 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001201};
1202
mingo39aeb522009-09-14 20:04:48 +02001203static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001204{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001205 int i;
1206 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001207 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001208
mingo39aeb522009-09-14 20:04:48 +02001209 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001210 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001211 /*
1212 * Ignore idle threads:
1213 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001214 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001215 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001216
mingo39aeb522009-09-14 20:04:48 +02001217 all_runtime += work_list->total_runtime;
1218 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001219
Ingo Molnar80ed0982009-09-16 14:12:36 +02001220 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001221
mingo08f69e62009-09-14 18:30:44 +02001222 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001223 printf(" ");
1224
mingo39aeb522009-09-14 20:04:48 +02001225 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001226
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001227 printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
mingo39aeb522009-09-14 20:04:48 +02001228 (double)work_list->total_runtime / 1e6,
1229 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001230 (double)work_list->max_lat / 1e6,
1231 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001232}
1233
mingo39aeb522009-09-14 20:04:48 +02001234static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001235{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001236 if (l->thread->pid < r->thread->pid)
1237 return -1;
1238 if (l->thread->pid > r->thread->pid)
1239 return 1;
1240
1241 return 0;
1242}
1243
1244static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001245 .name = "pid",
1246 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001247};
1248
mingo39aeb522009-09-14 20:04:48 +02001249static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001250{
1251 u64 avgl, avgr;
1252
1253 if (!l->nb_atoms)
1254 return -1;
1255
1256 if (!r->nb_atoms)
1257 return 1;
1258
1259 avgl = l->total_lat / l->nb_atoms;
1260 avgr = r->total_lat / r->nb_atoms;
1261
1262 if (avgl < avgr)
1263 return -1;
1264 if (avgl > avgr)
1265 return 1;
1266
1267 return 0;
1268}
1269
1270static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001271 .name = "avg",
1272 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001273};
1274
mingo39aeb522009-09-14 20:04:48 +02001275static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001276{
1277 if (l->max_lat < r->max_lat)
1278 return -1;
1279 if (l->max_lat > r->max_lat)
1280 return 1;
1281
1282 return 0;
1283}
1284
1285static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001286 .name = "max",
1287 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001288};
1289
mingo39aeb522009-09-14 20:04:48 +02001290static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001291{
1292 if (l->nb_atoms < r->nb_atoms)
1293 return -1;
1294 if (l->nb_atoms > r->nb_atoms)
1295 return 1;
1296
1297 return 0;
1298}
1299
1300static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001301 .name = "switch",
1302 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001303};
1304
mingo39aeb522009-09-14 20:04:48 +02001305static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001306{
1307 if (l->total_runtime < r->total_runtime)
1308 return -1;
1309 if (l->total_runtime > r->total_runtime)
1310 return 1;
1311
1312 return 0;
1313}
1314
1315static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001316 .name = "runtime",
1317 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001318};
1319
1320static struct sort_dimension *available_sorts[] = {
1321 &pid_sort_dimension,
1322 &avg_sort_dimension,
1323 &max_sort_dimension,
1324 &switch_sort_dimension,
1325 &runtime_sort_dimension,
1326};
1327
1328#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1329
1330static LIST_HEAD(sort_list);
1331
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001332static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001333{
1334 int i;
1335
1336 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1337 if (!strcmp(available_sorts[i]->name, tok)) {
1338 list_add_tail(&available_sorts[i]->list, list);
1339
1340 return 0;
1341 }
1342 }
1343
1344 return -1;
1345}
1346
1347static void setup_sorting(void);
1348
1349static void sort_lat(void)
1350{
1351 struct rb_node *node;
1352
1353 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001354 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001355 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001356 if (!node)
1357 break;
1358
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001359 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001360 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001361 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001362 }
1363}
1364
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001365static struct trace_sched_handler *trace_handler;
1366
1367static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001368process_sched_wakeup_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001369 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001370 struct perf_sample *sample,
1371 struct machine *machine,
1372 struct thread *thread)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001373{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001374 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001375 struct trace_wakeup_event wakeup_event;
1376
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001377 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001378
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001379 FILL_ARRAY(wakeup_event, comm, event, data);
1380 FILL_FIELD(wakeup_event, pid, event, data);
1381 FILL_FIELD(wakeup_event, prio, event, data);
1382 FILL_FIELD(wakeup_event, success, event, data);
1383 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001384
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001385 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001386 trace_handler->wakeup_event(&wakeup_event, machine, event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001387 sample->cpu, sample->time, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001388}
1389
Ingo Molnarc8a37752009-09-16 14:07:00 +02001390/*
1391 * Track the current task - that way we can know whether there's any
1392 * weird events, such as a task being switched away that is not current.
1393 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001394static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001395
Ingo Molnarc8a37752009-09-16 14:07:00 +02001396static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1397
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001398static struct thread *curr_thread[MAX_CPUS];
1399
1400static char next_shortname1 = 'A';
1401static char next_shortname2 = '0';
1402
1403static void
1404map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001405 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001406 struct event_format *event __used,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001407 int this_cpu,
1408 u64 timestamp,
1409 struct thread *thread __used)
1410{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05001411 struct thread *sched_out __used, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001412 int new_shortname;
1413 u64 timestamp0;
1414 s64 delta;
1415 int cpu;
1416
1417 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1418
1419 if (this_cpu > max_cpu)
1420 max_cpu = this_cpu;
1421
1422 timestamp0 = cpu_last_switched[this_cpu];
1423 cpu_last_switched[this_cpu] = timestamp;
1424 if (timestamp0)
1425 delta = timestamp - timestamp0;
1426 else
1427 delta = 0;
1428
1429 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001430 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001431
1432
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001433 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1434 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001435
1436 curr_thread[this_cpu] = sched_in;
1437
1438 printf(" ");
1439
1440 new_shortname = 0;
1441 if (!sched_in->shortname[0]) {
1442 sched_in->shortname[0] = next_shortname1;
1443 sched_in->shortname[1] = next_shortname2;
1444
1445 if (next_shortname1 < 'Z') {
1446 next_shortname1++;
1447 } else {
1448 next_shortname1='A';
1449 if (next_shortname2 < '9') {
1450 next_shortname2++;
1451 } else {
1452 next_shortname2='0';
1453 }
1454 }
1455 new_shortname = 1;
1456 }
1457
1458 for (cpu = 0; cpu <= max_cpu; cpu++) {
1459 if (cpu != this_cpu)
1460 printf(" ");
1461 else
1462 printf("*");
1463
1464 if (curr_thread[cpu]) {
1465 if (curr_thread[cpu]->pid)
1466 printf("%2s ", curr_thread[cpu]->shortname);
1467 else
1468 printf(". ");
1469 } else
1470 printf(" ");
1471 }
1472
1473 printf(" %12.6f secs ", (double)timestamp/1e9);
1474 if (new_shortname) {
1475 printf("%s => %s:%d\n",
1476 sched_in->shortname, sched_in->comm, sched_in->pid);
1477 } else {
1478 printf("\n");
1479 }
1480}
1481
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001482static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001483process_sched_switch_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001484 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001485 struct perf_sample *sample,
1486 struct machine *machine,
1487 struct thread *thread)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001488{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001489 int this_cpu = sample->cpu;
1490 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001491 struct trace_switch_event switch_event;
1492
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001493 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001494
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001495 FILL_ARRAY(switch_event, prev_comm, event, data);
1496 FILL_FIELD(switch_event, prev_pid, event, data);
1497 FILL_FIELD(switch_event, prev_prio, event, data);
1498 FILL_FIELD(switch_event, prev_state, event, data);
1499 FILL_ARRAY(switch_event, next_comm, event, data);
1500 FILL_FIELD(switch_event, next_pid, event, data);
1501 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001502
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001503 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001504 /*
1505 * Are we trying to switch away a PID that is
1506 * not current?
1507 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001508 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001509 nr_context_switch_bugs++;
1510 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001511 if (trace_handler->switch_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001512 trace_handler->switch_event(&switch_event, machine, event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001513 this_cpu, sample->time, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001514
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001515 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001516}
1517
1518static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001519process_sched_runtime_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001520 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001521 struct perf_sample *sample,
1522 struct machine *machine,
1523 struct thread *thread)
mingo39aeb522009-09-14 20:04:48 +02001524{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001525 void *data = sample->raw_data;
mingo39aeb522009-09-14 20:04:48 +02001526 struct trace_runtime_event runtime_event;
1527
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001528 FILL_ARRAY(runtime_event, comm, event, data);
1529 FILL_FIELD(runtime_event, pid, event, data);
1530 FILL_FIELD(runtime_event, runtime, event, data);
1531 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001532
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001533 if (trace_handler->runtime_event)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001534 trace_handler->runtime_event(&runtime_event, machine, event,
1535 sample->cpu, sample->time, thread);
mingo39aeb522009-09-14 20:04:48 +02001536}
1537
1538static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001539process_sched_fork_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001540 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001541 struct perf_sample *sample,
1542 struct machine *machine __used,
1543 struct thread *thread)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001544{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001545 void *data = sample->raw_data;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001546 struct trace_fork_event fork_event;
1547
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001548 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001549
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001550 FILL_ARRAY(fork_event, parent_comm, event, data);
1551 FILL_FIELD(fork_event, parent_pid, event, data);
1552 FILL_ARRAY(fork_event, child_comm, event, data);
1553 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001554
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001555 if (trace_handler->fork_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001556 trace_handler->fork_event(&fork_event, event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001557 sample->cpu, sample->time, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001558}
1559
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001560static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001561process_sched_exit_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001562 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001563 struct perf_sample *sample __used,
1564 struct machine *machine __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001565 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001566{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001567 if (verbose)
1568 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001569}
1570
1571static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001572process_sched_migrate_task_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001573 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001574 struct perf_sample *sample,
1575 struct machine *machine,
1576 struct thread *thread)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001577{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001578 void *data = sample->raw_data;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001579 struct trace_migrate_task_event migrate_task_event;
1580
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001581 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001582
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001583 FILL_ARRAY(migrate_task_event, comm, event, data);
1584 FILL_FIELD(migrate_task_event, pid, event, data);
1585 FILL_FIELD(migrate_task_event, prio, event, data);
1586 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001587
1588 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001589 trace_handler->migrate_task_event(&migrate_task_event, machine,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001590 event, sample->cpu,
1591 sample->time, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001592}
1593
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001594typedef void (*tracepoint_handler)(struct perf_tool *tool, struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001595 struct perf_sample *sample,
1596 struct machine *machine,
1597 struct thread *thread);
1598
1599static int perf_sched__process_tracepoint_sample(struct perf_tool *tool,
1600 union perf_event *event __used,
1601 struct perf_sample *sample,
1602 struct perf_evsel *evsel,
1603 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001604{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001605 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1606 struct pevent *pevent = sched->session->pevent;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001607 struct thread *thread = machine__findnew_thread(machine, sample->pid);
Ingo Molnarec156762009-09-11 12:12:54 +02001608
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001609 if (thread == NULL) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001610 pr_debug("problem processing %s event, skipping it.\n",
Arnaldo Carvalho de Melo22c8b842012-06-12 13:55:13 -03001611 perf_evsel__name(evsel));
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001612 return -1;
1613 }
1614
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001615 evsel->hists.stats.total_period += sample->period;
1616 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
Julia Lawallf39cdf22009-10-17 08:43:17 +02001617
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001618 if (evsel->handler.func != NULL) {
1619 tracepoint_handler f = evsel->handler.func;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001620
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001621 if (evsel->handler.data == NULL)
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001622 evsel->handler.data = pevent_find_event(pevent,
1623 evsel->attr.config);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001624
1625 f(tool, evsel->handler.data, sample, machine, thread);
1626 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001627
1628 return 0;
1629}
1630
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001631static struct perf_sched perf_sched = {
1632 .tool = {
1633 .sample = perf_sched__process_tracepoint_sample,
1634 .comm = perf_event__process_comm,
1635 .lost = perf_event__process_lost,
1636 .fork = perf_event__process_task,
1637 .ordered_samples = true,
1638 },
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001639};
1640
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001641static void read_events(bool destroy, struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001642{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001643 int err = -EINVAL;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001644 const struct perf_evsel_str_handler handlers[] = {
1645 { "sched:sched_switch", process_sched_switch_event, },
1646 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1647 { "sched:sched_wakeup", process_sched_wakeup_event, },
1648 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1649 { "sched:sched_process_fork", process_sched_fork_event, },
1650 { "sched:sched_process_exit", process_sched_exit_event, },
1651 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1652 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001653 struct perf_session *session;
1654
1655 session = perf_session__new(input_name, O_RDONLY, 0, false,
1656 &perf_sched.tool);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001657 if (session == NULL)
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001658 die("No Memory");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001659
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001660 perf_sched.session = session;
1661
1662 err = perf_session__set_tracepoints_handlers(session, handlers);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001663 assert(err == 0);
1664
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001665 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001666 err = perf_session__process_events(session, &perf_sched.tool);
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001667 if (err)
1668 die("Failed to process events, error %d", err);
1669
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001670 nr_events = session->hists.stats.nr_events[0];
1671 nr_lost_events = session->hists.stats.total_lost;
1672 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1673 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001674
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001675 if (destroy)
1676 perf_session__delete(session);
1677
1678 if (psession)
1679 *psession = session;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001680}
1681
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001682static void print_bad_events(void)
1683{
1684 if (nr_unordered_timestamps && nr_timestamps) {
1685 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1686 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1687 nr_unordered_timestamps, nr_timestamps);
1688 }
1689 if (nr_lost_events && nr_events) {
1690 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1691 (double)nr_lost_events/(double)nr_events*100.0,
1692 nr_lost_events, nr_events, nr_lost_chunks);
1693 }
1694 if (nr_state_machine_bugs && nr_timestamps) {
1695 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1696 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1697 nr_state_machine_bugs, nr_timestamps);
1698 if (nr_lost_events)
1699 printf(" (due to lost events?)");
1700 printf("\n");
1701 }
1702 if (nr_context_switch_bugs && nr_timestamps) {
1703 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1704 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1705 nr_context_switch_bugs, nr_timestamps);
1706 if (nr_lost_events)
1707 printf(" (due to lost events?)");
1708 printf("\n");
1709 }
1710}
1711
1712static void __cmd_lat(void)
1713{
1714 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001715 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001716
1717 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001718 read_events(false, &session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001719 sort_lat();
1720
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001721 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1722 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1723 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001724
1725 next = rb_first(&sorted_atom_root);
1726
1727 while (next) {
1728 struct work_atoms *work_list;
1729
1730 work_list = rb_entry(next, struct work_atoms, node);
1731 output_lat_thread(work_list);
1732 next = rb_next(next);
1733 }
1734
1735 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001736 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001737 (double)all_runtime/1e6, all_count);
1738
1739 printf(" ---------------------------------------------------\n");
1740
1741 print_bad_events();
1742 printf("\n");
1743
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001744 perf_session__delete(session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001745}
1746
1747static struct trace_sched_handler map_ops = {
1748 .wakeup_event = NULL,
1749 .switch_event = map_switch_event,
1750 .runtime_event = NULL,
1751 .fork_event = NULL,
1752};
1753
1754static void __cmd_map(void)
1755{
Ingo Molnar40749d02009-09-17 18:24:55 +02001756 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1757
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001758 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001759 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001760 print_bad_events();
1761}
1762
1763static void __cmd_replay(void)
1764{
1765 unsigned long i;
1766
1767 calibrate_run_measurement_overhead();
1768 calibrate_sleep_measurement_overhead();
1769
1770 test_calibrations();
1771
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001772 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001773
1774 printf("nr_run_events: %ld\n", nr_run_events);
1775 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1776 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1777
1778 if (targetless_wakeups)
1779 printf("target-less wakeups: %ld\n", targetless_wakeups);
1780 if (multitarget_wakeups)
1781 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1782 if (nr_run_events_optimized)
1783 printf("run atoms optimized: %ld\n",
1784 nr_run_events_optimized);
1785
1786 print_task_traces();
1787 add_cross_task_wakeups();
1788
1789 create_tasks();
1790 printf("------------------------------------------------------------\n");
1791 for (i = 0; i < replay_repeat; i++)
1792 run_one_test();
1793}
1794
1795
Ingo Molnar46f392c2009-09-12 10:08:34 +02001796static const char * const sched_usage[] = {
Jiri Olsa580cabe2011-08-09 14:46:51 +02001797 "perf sched [<options>] {record|latency|map|replay|script}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001798 NULL
1799};
1800
Ingo Molnarf2858d82009-09-11 12:12:54 +02001801static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001802 OPT_STRING('i', "input", &input_name, "file",
1803 "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001804 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001805 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001806 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1807 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001808 OPT_END()
1809};
1810
1811static const char * const latency_usage[] = {
1812 "perf sched latency [<options>]",
1813 NULL
1814};
1815
1816static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001817 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1818 "sort by key(s): runtime, switch, avg, max"),
Ian Munsiec0555642010-04-13 18:37:33 +10001819 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001820 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001821 OPT_INTEGER('C', "CPU", &profile_cpu,
1822 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001823 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1824 "dump raw trace in ASCII"),
1825 OPT_END()
1826};
1827
1828static const char * const replay_usage[] = {
1829 "perf sched replay [<options>]",
1830 NULL
1831};
1832
1833static const struct option replay_options[] = {
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001834 OPT_UINTEGER('r', "repeat", &replay_repeat,
1835 "repeat the workload replay N times (-1: infinite)"),
Ian Munsiec0555642010-04-13 18:37:33 +10001836 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001837 "be more verbose (show symbol address, etc)"),
1838 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1839 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001840 OPT_END()
1841};
1842
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001843static void setup_sorting(void)
1844{
1845 char *tmp, *tok, *str = strdup(sort_order);
1846
1847 for (tok = strtok_r(str, ", ", &tmp);
1848 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1849 if (sort_dimension__add(tok, &sort_list) < 0) {
1850 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001851 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001852 }
1853 }
1854
1855 free(str);
1856
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001857 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001858}
1859
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001860static const char *record_args[] = {
1861 "record",
1862 "-a",
1863 "-R",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001864 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001865 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001866 "-c", "1",
Stephane Eranian97101182011-01-12 10:29:05 +01001867 "-e", "sched:sched_switch",
1868 "-e", "sched:sched_stat_wait",
1869 "-e", "sched:sched_stat_sleep",
1870 "-e", "sched:sched_stat_iowait",
1871 "-e", "sched:sched_stat_runtime",
1872 "-e", "sched:sched_process_exit",
1873 "-e", "sched:sched_process_fork",
1874 "-e", "sched:sched_wakeup",
1875 "-e", "sched:sched_migrate_task",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001876};
1877
1878static int __cmd_record(int argc, const char **argv)
1879{
1880 unsigned int rec_argc, i, j;
1881 const char **rec_argv;
1882
1883 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1884 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1885
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001886 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001887 return -ENOMEM;
1888
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001889 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1890 rec_argv[i] = strdup(record_args[i]);
1891
1892 for (j = 1; j < (unsigned int)argc; j++, i++)
1893 rec_argv[i] = argv[j];
1894
1895 BUG_ON(i != rec_argc);
1896
1897 return cmd_record(i, rec_argv, NULL);
1898}
1899
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001900int cmd_sched(int argc, const char **argv, const char *prefix __used)
1901{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001902 argc = parse_options(argc, argv, sched_options, sched_usage,
1903 PARSE_OPT_STOP_AT_NON_OPTION);
1904 if (!argc)
1905 usage_with_options(sched_usage, sched_options);
1906
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001907 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001908 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001909 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001910 if (!strcmp(argv[0], "script"))
1911 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001912
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001913 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001914 if (!strncmp(argv[0], "rec", 3)) {
1915 return __cmd_record(argc, argv);
1916 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001917 trace_handler = &lat_ops;
1918 if (argc > 1) {
1919 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1920 if (argc)
1921 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001922 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001923 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001924 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001925 } else if (!strcmp(argv[0], "map")) {
1926 trace_handler = &map_ops;
1927 setup_sorting();
1928 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001929 } else if (!strncmp(argv[0], "rep", 3)) {
1930 trace_handler = &replay_ops;
1931 if (argc) {
1932 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1933 if (argc)
1934 usage_with_options(replay_usage, replay_options);
1935 }
1936 __cmd_replay();
1937 } else {
1938 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001939 }
1940
Ingo Molnarec156762009-09-11 12:12:54 +02001941 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001942}