blob: a25a023965bb2751e3499132a53482a129fbdf10 [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
mingo39aeb522009-09-14 20:04:48 +020046struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020047
48struct task_desc {
49 unsigned long nr;
50 unsigned long pid;
51 char comm[COMM_LEN];
52
53 unsigned long nr_events;
54 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020055 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020056
57 pthread_t thread;
58 sem_t sleep_sem;
59
60 sem_t ready_for_work;
61 sem_t work_done_sem;
62
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020063 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020064};
65
66enum sched_event_type {
67 SCHED_EVENT_RUN,
68 SCHED_EVENT_SLEEP,
69 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020070 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020071};
72
mingo39aeb522009-09-14 20:04:48 +020073struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020074 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030075 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020076 u64 timestamp;
77 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020078 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020079 sem_t *wait_sem;
80 struct task_desc *wakee;
81};
82
83static struct task_desc *pid_to_task[MAX_PID];
84
85static struct task_desc **tasks;
86
87static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020088static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020089
90static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
91
92static unsigned long nr_run_events;
93static unsigned long nr_sleep_events;
94static unsigned long nr_wakeup_events;
95
96static unsigned long nr_sleep_corrections;
97static unsigned long nr_run_events_optimized;
98
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020099static unsigned long targetless_wakeups;
100static unsigned long multitarget_wakeups;
101
102static u64 cpu_usage;
103static u64 runavg_cpu_usage;
104static u64 parent_cpu_usage;
105static u64 runavg_parent_cpu_usage;
106
107static unsigned long nr_runs;
108static u64 sum_runtime;
109static u64 sum_fluct;
110static u64 run_avg;
111
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -0300112static unsigned int replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200113static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200114static unsigned long nr_unordered_timestamps;
115static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200116static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200117static unsigned long nr_events;
118static unsigned long nr_lost_chunks;
119static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200120
121#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
122
123enum thread_state {
124 THREAD_SLEEPING = 0,
125 THREAD_WAIT_CPU,
126 THREAD_SCHED_IN,
127 THREAD_IGNORE
128};
129
130struct work_atom {
131 struct list_head list;
132 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200133 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200134 u64 wake_up_time;
135 u64 sched_in_time;
136 u64 runtime;
137};
138
mingo39aeb522009-09-14 20:04:48 +0200139struct work_atoms {
140 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200141 struct thread *thread;
142 struct rb_node node;
143 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100144 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200145 u64 total_lat;
146 u64 nb_atoms;
147 u64 total_runtime;
148};
149
mingo39aeb522009-09-14 20:04:48 +0200150typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200151
152static struct rb_root atom_root, sorted_atom_root;
153
154static u64 all_runtime;
155static u64 all_count;
156
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200157
158static u64 get_nsecs(void)
159{
160 struct timespec ts;
161
162 clock_gettime(CLOCK_MONOTONIC, &ts);
163
164 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
165}
166
167static void burn_nsecs(u64 nsecs)
168{
169 u64 T0 = get_nsecs(), T1;
170
171 do {
172 T1 = get_nsecs();
173 } while (T1 + run_measurement_overhead < T0 + nsecs);
174}
175
176static void sleep_nsecs(u64 nsecs)
177{
178 struct timespec ts;
179
180 ts.tv_nsec = nsecs % 999999999;
181 ts.tv_sec = nsecs / 999999999;
182
183 nanosleep(&ts, NULL);
184}
185
186static void calibrate_run_measurement_overhead(void)
187{
188 u64 T0, T1, delta, min_delta = 1000000000ULL;
189 int i;
190
191 for (i = 0; i < 10; i++) {
192 T0 = get_nsecs();
193 burn_nsecs(0);
194 T1 = get_nsecs();
195 delta = T1-T0;
196 min_delta = min(min_delta, delta);
197 }
198 run_measurement_overhead = min_delta;
199
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200200 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200201}
202
203static void calibrate_sleep_measurement_overhead(void)
204{
205 u64 T0, T1, delta, min_delta = 1000000000ULL;
206 int i;
207
208 for (i = 0; i < 10; i++) {
209 T0 = get_nsecs();
210 sleep_nsecs(10000);
211 T1 = get_nsecs();
212 delta = T1-T0;
213 min_delta = min(min_delta, delta);
214 }
215 min_delta -= 10000;
216 sleep_measurement_overhead = min_delta;
217
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200218 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200219}
220
mingo39aeb522009-09-14 20:04:48 +0200221static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200222get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200223{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200224 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200225 unsigned long idx = task->nr_events;
226 size_t size;
227
228 event->timestamp = timestamp;
229 event->nr = idx;
230
231 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200232 size = sizeof(struct sched_atom *) * task->nr_events;
233 task->atoms = realloc(task->atoms, size);
234 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200235
mingo39aeb522009-09-14 20:04:48 +0200236 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200237
238 return event;
239}
240
mingo39aeb522009-09-14 20:04:48 +0200241static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200242{
243 if (!task->nr_events)
244 return NULL;
245
mingo39aeb522009-09-14 20:04:48 +0200246 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200247}
248
249static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200250add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200251{
mingo39aeb522009-09-14 20:04:48 +0200252 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200253
254 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200255 * optimize an existing RUN event by merging this one
256 * to it:
257 */
Ingo Molnarec156762009-09-11 12:12:54 +0200258 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
259 nr_run_events_optimized++;
260 curr_event->duration += duration;
261 return;
262 }
263
264 event = get_new_event(task, timestamp);
265
266 event->type = SCHED_EVENT_RUN;
267 event->duration = duration;
268
269 nr_run_events++;
270}
271
Ingo Molnarec156762009-09-11 12:12:54 +0200272static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200273add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200274 struct task_desc *wakee)
275{
mingo39aeb522009-09-14 20:04:48 +0200276 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200277
278 event = get_new_event(task, timestamp);
279 event->type = SCHED_EVENT_WAKEUP;
280 event->wakee = wakee;
281
282 wakee_event = last_event(wakee);
283 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
284 targetless_wakeups++;
285 return;
286 }
287 if (wakee_event->wait_sem) {
288 multitarget_wakeups++;
289 return;
290 }
291
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200292 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200293 sem_init(wakee_event->wait_sem, 0, 0);
294 wakee_event->specific_wait = 1;
295 event->wait_sem = wakee_event->wait_sem;
296
297 nr_wakeup_events++;
298}
299
300static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200301add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200302 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200303{
mingo39aeb522009-09-14 20:04:48 +0200304 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200305
306 event->type = SCHED_EVENT_SLEEP;
307
308 nr_sleep_events++;
309}
310
311static struct task_desc *register_pid(unsigned long pid, const char *comm)
312{
313 struct task_desc *task;
314
315 BUG_ON(pid >= MAX_PID);
316
317 task = pid_to_task[pid];
318
319 if (task)
320 return task;
321
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200322 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200323 task->pid = pid;
324 task->nr = nr_tasks;
325 strcpy(task->comm, comm);
326 /*
327 * every task starts in sleeping state - this gets ignored
328 * if there's no wakeup pointing to this sleep state:
329 */
330 add_sched_event_sleep(task, 0, 0);
331
332 pid_to_task[pid] = task;
333 nr_tasks++;
334 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
335 BUG_ON(!tasks);
336 tasks[task->nr] = task;
337
Ingo Molnarad236fd2009-09-11 12:12:54 +0200338 if (verbose)
339 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200340
341 return task;
342}
343
344
Ingo Molnarec156762009-09-11 12:12:54 +0200345static void print_task_traces(void)
346{
347 struct task_desc *task;
348 unsigned long i;
349
350 for (i = 0; i < nr_tasks; i++) {
351 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200352 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200353 task->nr, task->comm, task->pid, task->nr_events);
354 }
355}
356
357static void add_cross_task_wakeups(void)
358{
359 struct task_desc *task1, *task2;
360 unsigned long i, j;
361
362 for (i = 0; i < nr_tasks; i++) {
363 task1 = tasks[i];
364 j = i + 1;
365 if (j == nr_tasks)
366 j = 0;
367 task2 = tasks[j];
368 add_sched_event_wakeup(task1, 0, task2);
369 }
370}
371
372static void
mingo39aeb522009-09-14 20:04:48 +0200373process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200374{
375 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200376
mingo39aeb522009-09-14 20:04:48 +0200377 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200378 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200379 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200380 break;
381 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200382 if (atom->wait_sem)
383 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200384 BUG_ON(ret);
385 break;
386 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200387 if (atom->wait_sem)
388 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200389 BUG_ON(ret);
390 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200391 case SCHED_EVENT_MIGRATION:
392 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200393 default:
394 BUG_ON(1);
395 }
396}
397
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200398static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200399{
400 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200401 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200402 int err;
403
404 err = getrusage(RUSAGE_SELF, &ru);
405 BUG_ON(err);
406
407 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
408 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
409
410 return sum;
411}
412
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800413static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200414{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800415 struct perf_event_attr attr;
416 int fd;
417
418 memset(&attr, 0, sizeof(attr));
419
420 attr.type = PERF_TYPE_SOFTWARE;
421 attr.config = PERF_COUNT_SW_TASK_CLOCK;
422
423 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
424
425 if (fd < 0)
426 die("Error: sys_perf_event_open() syscall returned"
427 "with %d (%s)\n", fd, strerror(errno));
428 return fd;
429}
430
431static u64 get_cpu_usage_nsec_self(int fd)
432{
433 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200434 int ret;
435
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800436 ret = read(fd, &runtime, sizeof(runtime));
437 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200438
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800439 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200440}
441
442static void *thread_func(void *ctx)
443{
444 struct task_desc *this_task = ctx;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200445 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200446 unsigned long i, ret;
447 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800448 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200449
Ingo Molnarec156762009-09-11 12:12:54 +0200450 sprintf(comm2, ":%s", this_task->comm);
451 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800452 fd = self_open_counters();
Ingo Molnarec156762009-09-11 12:12:54 +0200453
454again:
455 ret = sem_post(&this_task->ready_for_work);
456 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200457 ret = pthread_mutex_lock(&start_work_mutex);
458 BUG_ON(ret);
459 ret = pthread_mutex_unlock(&start_work_mutex);
460 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200461
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800462 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200463
464 for (i = 0; i < this_task->nr_events; i++) {
465 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200466 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200467 }
468
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800469 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200470 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200471 ret = sem_post(&this_task->work_done_sem);
472 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200473
474 ret = pthread_mutex_lock(&work_done_wait_mutex);
475 BUG_ON(ret);
476 ret = pthread_mutex_unlock(&work_done_wait_mutex);
477 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200478
479 goto again;
480}
481
482static void create_tasks(void)
483{
484 struct task_desc *task;
485 pthread_attr_t attr;
486 unsigned long i;
487 int err;
488
489 err = pthread_attr_init(&attr);
490 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200491 err = pthread_attr_setstacksize(&attr,
492 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200493 BUG_ON(err);
494 err = pthread_mutex_lock(&start_work_mutex);
495 BUG_ON(err);
496 err = pthread_mutex_lock(&work_done_wait_mutex);
497 BUG_ON(err);
498 for (i = 0; i < nr_tasks; i++) {
499 task = tasks[i];
500 sem_init(&task->sleep_sem, 0, 0);
501 sem_init(&task->ready_for_work, 0, 0);
502 sem_init(&task->work_done_sem, 0, 0);
503 task->curr_event = 0;
504 err = pthread_create(&task->thread, &attr, thread_func, task);
505 BUG_ON(err);
506 }
507}
508
Ingo Molnarec156762009-09-11 12:12:54 +0200509static void wait_for_tasks(void)
510{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200511 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200512 struct task_desc *task;
513 unsigned long i, ret;
514
Ingo Molnarec156762009-09-11 12:12:54 +0200515 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200516 cpu_usage = 0;
517 pthread_mutex_unlock(&work_done_wait_mutex);
518
519 for (i = 0; i < nr_tasks; i++) {
520 task = tasks[i];
521 ret = sem_wait(&task->ready_for_work);
522 BUG_ON(ret);
523 sem_init(&task->ready_for_work, 0, 0);
524 }
525 ret = pthread_mutex_lock(&work_done_wait_mutex);
526 BUG_ON(ret);
527
528 cpu_usage_0 = get_cpu_usage_nsec_parent();
529
530 pthread_mutex_unlock(&start_work_mutex);
531
Ingo Molnarec156762009-09-11 12:12:54 +0200532 for (i = 0; i < nr_tasks; i++) {
533 task = tasks[i];
534 ret = sem_wait(&task->work_done_sem);
535 BUG_ON(ret);
536 sem_init(&task->work_done_sem, 0, 0);
537 cpu_usage += task->cpu_usage;
538 task->cpu_usage = 0;
539 }
540
541 cpu_usage_1 = get_cpu_usage_nsec_parent();
542 if (!runavg_cpu_usage)
543 runavg_cpu_usage = cpu_usage;
544 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
545
546 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
547 if (!runavg_parent_cpu_usage)
548 runavg_parent_cpu_usage = parent_cpu_usage;
549 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
550 parent_cpu_usage)/10;
551
552 ret = pthread_mutex_lock(&start_work_mutex);
553 BUG_ON(ret);
554
555 for (i = 0; i < nr_tasks; i++) {
556 task = tasks[i];
557 sem_init(&task->sleep_sem, 0, 0);
558 task->curr_event = 0;
559 }
560}
561
Ingo Molnarec156762009-09-11 12:12:54 +0200562static void run_one_test(void)
563{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500564 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200565
566 T0 = get_nsecs();
567 wait_for_tasks();
568 T1 = get_nsecs();
569
570 delta = T1 - T0;
571 sum_runtime += delta;
572 nr_runs++;
573
574 avg_delta = sum_runtime / nr_runs;
575 if (delta < avg_delta)
576 fluct = avg_delta - delta;
577 else
578 fluct = delta - avg_delta;
579 sum_fluct += fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200580 if (!run_avg)
581 run_avg = delta;
582 run_avg = (run_avg*9 + delta)/10;
583
Ingo Molnarad236fd2009-09-11 12:12:54 +0200584 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200585 nr_runs, (double)delta/1000000.0);
586
Ingo Molnarad236fd2009-09-11 12:12:54 +0200587 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200588 (double)run_avg/1e6);
589
Ingo Molnarad236fd2009-09-11 12:12:54 +0200590 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200591 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
592
593#if 0
594 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200595 * rusage statistics done by the parent, these are less
596 * accurate than the sum_exec_runtime based statistics:
597 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200598 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200599 (double)parent_cpu_usage/1e6,
600 (double)runavg_parent_cpu_usage/1e6);
601#endif
602
Ingo Molnarad236fd2009-09-11 12:12:54 +0200603 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200604
605 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200606 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200607 nr_sleep_corrections = 0;
608}
609
610static void test_calibrations(void)
611{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200612 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200613
614 T0 = get_nsecs();
615 burn_nsecs(1e6);
616 T1 = get_nsecs();
617
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200618 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200619
620 T0 = get_nsecs();
621 sleep_nsecs(1e6);
622 T1 = get_nsecs();
623
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200624 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200625}
626
Frederic Weisbecker46538812009-09-12 02:43:45 +0200627#define FILL_FIELD(ptr, field, event, data) \
628 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
629
630#define FILL_ARRAY(ptr, array, event, data) \
631do { \
632 void *__array = raw_field_ptr(event, #array, data); \
633 memcpy(ptr.array, __array, sizeof(ptr.array)); \
634} while(0)
635
636#define FILL_COMMON_FIELDS(ptr, event, data) \
637do { \
638 FILL_FIELD(ptr, common_type, event, data); \
639 FILL_FIELD(ptr, common_flags, event, data); \
640 FILL_FIELD(ptr, common_preempt_count, event, data); \
641 FILL_FIELD(ptr, common_pid, event, data); \
642 FILL_FIELD(ptr, common_tgid, event, data); \
643} while (0)
644
Ingo Molnarfbf94822009-09-11 12:12:54 +0200645
Ingo Molnarec156762009-09-11 12:12:54 +0200646
Ingo Molnarfbf94822009-09-11 12:12:54 +0200647struct trace_switch_event {
648 u32 size;
649
650 u16 common_type;
651 u8 common_flags;
652 u8 common_preempt_count;
653 u32 common_pid;
654 u32 common_tgid;
655
656 char prev_comm[16];
657 u32 prev_pid;
658 u32 prev_prio;
659 u64 prev_state;
660 char next_comm[16];
661 u32 next_pid;
662 u32 next_prio;
663};
664
mingo39aeb522009-09-14 20:04:48 +0200665struct trace_runtime_event {
666 u32 size;
667
668 u16 common_type;
669 u8 common_flags;
670 u8 common_preempt_count;
671 u32 common_pid;
672 u32 common_tgid;
673
674 char comm[16];
675 u32 pid;
676 u64 runtime;
677 u64 vruntime;
678};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200679
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200680struct trace_wakeup_event {
681 u32 size;
682
683 u16 common_type;
684 u8 common_flags;
685 u8 common_preempt_count;
686 u32 common_pid;
687 u32 common_tgid;
688
689 char comm[16];
690 u32 pid;
691
692 u32 prio;
693 u32 success;
694 u32 cpu;
695};
696
697struct trace_fork_event {
698 u32 size;
699
700 u16 common_type;
701 u8 common_flags;
702 u8 common_preempt_count;
703 u32 common_pid;
704 u32 common_tgid;
705
706 char parent_comm[16];
707 u32 parent_pid;
708 char child_comm[16];
709 u32 child_pid;
710};
711
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200712struct trace_migrate_task_event {
713 u32 size;
714
715 u16 common_type;
716 u8 common_flags;
717 u8 common_preempt_count;
718 u32 common_pid;
719 u32 common_tgid;
720
721 char comm[16];
722 u32 pid;
723
724 u32 prio;
725 u32 cpu;
726};
727
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200728struct trace_sched_handler {
729 void (*switch_event)(struct trace_switch_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200730 struct machine *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200731 struct event_format *,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300732 struct perf_sample *sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200733
mingo39aeb522009-09-14 20:04:48 +0200734 void (*runtime_event)(struct trace_runtime_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200735 struct machine *,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300736 struct perf_sample *sample);
mingo39aeb522009-09-14 20:04:48 +0200737
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200738 void (*wakeup_event)(struct trace_wakeup_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200739 struct machine *,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200740 struct event_format *,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300741 struct perf_sample *sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200742
743 void (*fork_event)(struct trace_fork_event *,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300744 struct event_format *event);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200745
746 void (*migrate_task_event)(struct trace_migrate_task_event *,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300747 struct machine *machine,
748 struct perf_sample *sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200749};
750
Ingo Molnarfbf94822009-09-11 12:12:54 +0200751
752static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200753replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200754 struct machine *machine __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300755 struct event_format *event, struct perf_sample *sample)
Ingo Molnarec156762009-09-11 12:12:54 +0200756{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200757 struct task_desc *waker, *wakee;
758
759 if (verbose) {
760 printf("sched_wakeup event %p\n", event);
761
762 printf(" ... pid %d woke up %s/%d\n",
763 wakeup_event->common_pid,
764 wakeup_event->comm,
765 wakeup_event->pid);
766 }
767
768 waker = register_pid(wakeup_event->common_pid, "<unknown>");
769 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
770
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300771 add_sched_event_wakeup(waker, sample->time, wakee);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200772}
773
Ingo Molnard1153382009-09-14 18:22:53 +0200774static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200775
776static void
777replay_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200778 struct machine *machine __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200779 struct event_format *event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300780 struct perf_sample *sample)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200781{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500782 struct task_desc *prev, __used *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300783 u64 timestamp0, timestamp = sample->time;
784 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200785 s64 delta;
786
Ingo Molnarad236fd2009-09-11 12:12:54 +0200787 if (verbose)
788 printf("sched_switch event %p\n", event);
789
Ingo Molnarfbf94822009-09-11 12:12:54 +0200790 if (cpu >= MAX_CPUS || cpu < 0)
791 return;
792
793 timestamp0 = cpu_last_switched[cpu];
794 if (timestamp0)
795 delta = timestamp - timestamp0;
796 else
797 delta = 0;
798
799 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200800 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200801
Ingo Molnarad236fd2009-09-11 12:12:54 +0200802 if (verbose) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200803 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200804 switch_event->prev_comm, switch_event->prev_pid,
805 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200806 delta);
807 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200808
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200809 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
810 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200811
812 cpu_last_switched[cpu] = timestamp;
813
814 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200815 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200816}
817
Ingo Molnarfbf94822009-09-11 12:12:54 +0200818
819static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200820replay_fork_event(struct trace_fork_event *fork_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300821 struct event_format *event)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200822{
823 if (verbose) {
824 printf("sched_fork event %p\n", event);
825 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
826 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
827 }
828 register_pid(fork_event->parent_pid, fork_event->parent_comm);
829 register_pid(fork_event->child_pid, fork_event->child_comm);
830}
831
832static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200833 .wakeup_event = replay_wakeup_event,
834 .switch_event = replay_switch_event,
835 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200836};
837
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200838struct sort_dimension {
839 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200840 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200841 struct list_head list;
842};
843
844static LIST_HEAD(cmp_pid);
845
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200846static int
mingo39aeb522009-09-14 20:04:48 +0200847thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200848{
849 struct sort_dimension *sort;
850 int ret = 0;
851
Ingo Molnarb5fae122009-09-11 12:12:54 +0200852 BUG_ON(list_empty(list));
853
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200854 list_for_each_entry(sort, list, list) {
855 ret = sort->cmp(l, r);
856 if (ret)
857 return ret;
858 }
859
860 return ret;
861}
862
mingo39aeb522009-09-14 20:04:48 +0200863static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200864thread_atoms_search(struct rb_root *root, struct thread *thread,
865 struct list_head *sort_list)
866{
867 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200868 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200869
870 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200871 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200872 int cmp;
873
mingo39aeb522009-09-14 20:04:48 +0200874 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200875
876 cmp = thread_lat_cmp(sort_list, &key, atoms);
877 if (cmp > 0)
878 node = node->rb_left;
879 else if (cmp < 0)
880 node = node->rb_right;
881 else {
882 BUG_ON(thread != atoms->thread);
883 return atoms;
884 }
885 }
886 return NULL;
887}
888
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200889static void
mingo39aeb522009-09-14 20:04:48 +0200890__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200891 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200892{
893 struct rb_node **new = &(root->rb_node), *parent = NULL;
894
895 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200896 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200897 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200898
mingo39aeb522009-09-14 20:04:48 +0200899 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200900 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200901
902 cmp = thread_lat_cmp(sort_list, data, this);
903
904 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200906 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200907 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200908 }
909
910 rb_link_node(&data->node, parent, new);
911 rb_insert_color(&data->node, root);
912}
913
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200914static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200916 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Frederic Weisbecker17562202009-09-12 23:11:32 +0200917 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200918 die("No memory");
919
Frederic Weisbecker17562202009-09-12 23:11:32 +0200920 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200921 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200922 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923}
924
925static void
926latency_fork_event(struct trace_fork_event *fork_event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300927 struct event_format *event __used)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200928{
929 /* should insert the newcomer */
930}
931
Ingo Molnarea92ed52009-09-12 10:08:34 +0200932__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933static char sched_out_state(struct trace_switch_event *switch_event)
934{
935 const char *str = TASK_STATE_TO_CHAR_STR;
936
937 return str[switch_event->prev_state];
938}
939
940static void
mingo39aeb522009-09-14 20:04:48 +0200941add_sched_out_event(struct work_atoms *atoms,
942 char run_state,
943 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200944{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200945 struct work_atom *atom = zalloc(sizeof(*atom));
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200946 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947 die("Non memory");
948
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200949 atom->sched_out_time = timestamp;
950
mingo39aeb522009-09-14 20:04:48 +0200951 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200952 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200953 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200954 }
955
mingo39aeb522009-09-14 20:04:48 +0200956 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957}
958
959static void
mingo39aeb522009-09-14 20:04:48 +0200960add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
961{
962 struct work_atom *atom;
963
964 BUG_ON(list_empty(&atoms->work_list));
965
966 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
967
968 atom->runtime += delta;
969 atoms->total_runtime += delta;
970}
971
972static void
973add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200974{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200975 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200976 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200977
mingo39aeb522009-09-14 20:04:48 +0200978 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200979 return;
980
mingo39aeb522009-09-14 20:04:48 +0200981 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200983 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200984 return;
985
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200986 if (timestamp < atom->wake_up_time) {
987 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988 return;
989 }
990
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200991 atom->state = THREAD_SCHED_IN;
992 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200993
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200994 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200995 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100996 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200997 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100998 atoms->max_lat_at = timestamp;
999 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001000 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001001}
1002
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001003static void
1004latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001005 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001006 struct event_format *event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001007 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001008{
mingo39aeb522009-09-14 20:04:48 +02001009 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001010 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001011 u64 timestamp0, timestamp = sample->time;
1012 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001013 s64 delta;
1014
mingo39aeb522009-09-14 20:04:48 +02001015 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001016
1017 timestamp0 = cpu_last_switched[cpu];
1018 cpu_last_switched[cpu] = timestamp;
1019 if (timestamp0)
1020 delta = timestamp - timestamp0;
1021 else
1022 delta = 0;
1023
1024 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001025 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001026
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001028 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1029 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030
mingo39aeb522009-09-14 20:04:48 +02001031 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1032 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001033 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001034 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1035 if (!out_events)
1036 die("out-event: Internal tree error");
1037 }
1038 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1039
1040 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1041 if (!in_events) {
1042 thread_atoms_insert(sched_in);
1043 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1044 if (!in_events)
1045 die("in-event: Internal tree error");
1046 /*
1047 * Take came in we have not heard about yet,
1048 * add in an initial atom in runnable state:
1049 */
1050 add_sched_out_event(in_events, 'R', timestamp);
1051 }
1052 add_sched_in_event(in_events, timestamp);
1053}
1054
1055static void
1056latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001057 struct machine *machine, struct perf_sample *sample)
mingo39aeb522009-09-14 20:04:48 +02001058{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001059 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001060 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001061 u64 timestamp = sample->time;
1062 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +02001063
1064 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001065 if (!atoms) {
1066 thread_atoms_insert(thread);
1067 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1068 if (!atoms)
1069 die("in-event: Internal tree error");
1070 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001071 }
1072
mingo39aeb522009-09-14 20:04:48 +02001073 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001074}
1075
1076static void
1077latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001078 struct machine *machine, struct event_format *event __used,
1079 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001080{
mingo39aeb522009-09-14 20:04:48 +02001081 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001082 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001083 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001084 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001085
1086 /* Note for later, it may be interesting to observe the failing cases */
1087 if (!wakeup_event->success)
1088 return;
1089
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001090 wakee = machine__findnew_thread(machine, wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001091 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001092 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001093 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001094 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1095 if (!atoms)
1096 die("wakeup-event: Internal tree error");
1097 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001098 }
1099
mingo39aeb522009-09-14 20:04:48 +02001100 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001101
mingo39aeb522009-09-14 20:04:48 +02001102 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001104 /*
1105 * You WILL be missing events if you've recorded only
1106 * one CPU, or are only looking at only one, so don't
1107 * make useless noise.
1108 */
1109 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001110 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001111
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001112 nr_timestamps++;
1113 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001114 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001115 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001116 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001117
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001118 atom->state = THREAD_WAIT_CPU;
1119 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120}
1121
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001122static void
1123latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001124 struct machine *machine, struct perf_sample *sample)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001125{
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001126 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001127 struct work_atoms *atoms;
1128 struct work_atom *atom;
1129 struct thread *migrant;
1130
1131 /*
1132 * Only need to worry about migration when profiling one CPU.
1133 */
1134 if (profile_cpu == -1)
1135 return;
1136
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001137 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001138 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1139 if (!atoms) {
1140 thread_atoms_insert(migrant);
1141 register_pid(migrant->pid, migrant->comm);
1142 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1143 if (!atoms)
1144 die("migration-event: Internal tree error");
1145 add_sched_out_event(atoms, 'R', timestamp);
1146 }
1147
1148 BUG_ON(list_empty(&atoms->work_list));
1149
1150 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1151 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1152
1153 nr_timestamps++;
1154
1155 if (atom->sched_out_time > timestamp)
1156 nr_unordered_timestamps++;
1157}
1158
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001159static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001160 .wakeup_event = latency_wakeup_event,
1161 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001162 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001163 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001164 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001165};
1166
mingo39aeb522009-09-14 20:04:48 +02001167static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001168{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001169 int i;
1170 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001171 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001172
mingo39aeb522009-09-14 20:04:48 +02001173 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001174 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001175 /*
1176 * Ignore idle threads:
1177 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001178 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001179 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001180
mingo39aeb522009-09-14 20:04:48 +02001181 all_runtime += work_list->total_runtime;
1182 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001183
Ingo Molnar80ed0982009-09-16 14:12:36 +02001184 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001185
mingo08f69e62009-09-14 18:30:44 +02001186 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001187 printf(" ");
1188
mingo39aeb522009-09-14 20:04:48 +02001189 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001190
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001191 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 +02001192 (double)work_list->total_runtime / 1e6,
1193 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001194 (double)work_list->max_lat / 1e6,
1195 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001196}
1197
mingo39aeb522009-09-14 20:04:48 +02001198static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001199{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001200 if (l->thread->pid < r->thread->pid)
1201 return -1;
1202 if (l->thread->pid > r->thread->pid)
1203 return 1;
1204
1205 return 0;
1206}
1207
1208static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001209 .name = "pid",
1210 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001211};
1212
mingo39aeb522009-09-14 20:04:48 +02001213static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001214{
1215 u64 avgl, avgr;
1216
1217 if (!l->nb_atoms)
1218 return -1;
1219
1220 if (!r->nb_atoms)
1221 return 1;
1222
1223 avgl = l->total_lat / l->nb_atoms;
1224 avgr = r->total_lat / r->nb_atoms;
1225
1226 if (avgl < avgr)
1227 return -1;
1228 if (avgl > avgr)
1229 return 1;
1230
1231 return 0;
1232}
1233
1234static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001235 .name = "avg",
1236 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001237};
1238
mingo39aeb522009-09-14 20:04:48 +02001239static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001240{
1241 if (l->max_lat < r->max_lat)
1242 return -1;
1243 if (l->max_lat > r->max_lat)
1244 return 1;
1245
1246 return 0;
1247}
1248
1249static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001250 .name = "max",
1251 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001252};
1253
mingo39aeb522009-09-14 20:04:48 +02001254static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001255{
1256 if (l->nb_atoms < r->nb_atoms)
1257 return -1;
1258 if (l->nb_atoms > r->nb_atoms)
1259 return 1;
1260
1261 return 0;
1262}
1263
1264static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001265 .name = "switch",
1266 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001267};
1268
mingo39aeb522009-09-14 20:04:48 +02001269static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001270{
1271 if (l->total_runtime < r->total_runtime)
1272 return -1;
1273 if (l->total_runtime > r->total_runtime)
1274 return 1;
1275
1276 return 0;
1277}
1278
1279static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001280 .name = "runtime",
1281 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001282};
1283
1284static struct sort_dimension *available_sorts[] = {
1285 &pid_sort_dimension,
1286 &avg_sort_dimension,
1287 &max_sort_dimension,
1288 &switch_sort_dimension,
1289 &runtime_sort_dimension,
1290};
1291
1292#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1293
1294static LIST_HEAD(sort_list);
1295
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001296static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001297{
1298 int i;
1299
1300 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1301 if (!strcmp(available_sorts[i]->name, tok)) {
1302 list_add_tail(&available_sorts[i]->list, list);
1303
1304 return 0;
1305 }
1306 }
1307
1308 return -1;
1309}
1310
1311static void setup_sorting(void);
1312
1313static void sort_lat(void)
1314{
1315 struct rb_node *node;
1316
1317 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001318 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001319 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001320 if (!node)
1321 break;
1322
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001323 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001324 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001325 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001326 }
1327}
1328
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001329static struct trace_sched_handler *trace_handler;
1330
1331static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001332process_sched_wakeup_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001333 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001334 struct perf_sample *sample,
1335 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001336 struct thread *thread __used)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001337{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001338 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001339 struct trace_wakeup_event wakeup_event;
1340
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001341 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001342
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001343 FILL_ARRAY(wakeup_event, comm, event, data);
1344 FILL_FIELD(wakeup_event, pid, event, data);
1345 FILL_FIELD(wakeup_event, prio, event, data);
1346 FILL_FIELD(wakeup_event, success, event, data);
1347 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001348
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001349 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001350 trace_handler->wakeup_event(&wakeup_event, machine, event, sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001351}
1352
Ingo Molnarc8a37752009-09-16 14:07:00 +02001353/*
1354 * Track the current task - that way we can know whether there's any
1355 * weird events, such as a task being switched away that is not current.
1356 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001357static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001358
Ingo Molnarc8a37752009-09-16 14:07:00 +02001359static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1360
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001361static struct thread *curr_thread[MAX_CPUS];
1362
1363static char next_shortname1 = 'A';
1364static char next_shortname2 = '0';
1365
1366static void
1367map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001368 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001369 struct event_format *event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001370 struct perf_sample *sample)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001371{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05001372 struct thread *sched_out __used, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001373 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001374 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001375 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001376 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001377
1378 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1379
1380 if (this_cpu > max_cpu)
1381 max_cpu = this_cpu;
1382
1383 timestamp0 = cpu_last_switched[this_cpu];
1384 cpu_last_switched[this_cpu] = timestamp;
1385 if (timestamp0)
1386 delta = timestamp - timestamp0;
1387 else
1388 delta = 0;
1389
1390 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001391 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392
1393
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001394 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1395 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001396
1397 curr_thread[this_cpu] = sched_in;
1398
1399 printf(" ");
1400
1401 new_shortname = 0;
1402 if (!sched_in->shortname[0]) {
1403 sched_in->shortname[0] = next_shortname1;
1404 sched_in->shortname[1] = next_shortname2;
1405
1406 if (next_shortname1 < 'Z') {
1407 next_shortname1++;
1408 } else {
1409 next_shortname1='A';
1410 if (next_shortname2 < '9') {
1411 next_shortname2++;
1412 } else {
1413 next_shortname2='0';
1414 }
1415 }
1416 new_shortname = 1;
1417 }
1418
1419 for (cpu = 0; cpu <= max_cpu; cpu++) {
1420 if (cpu != this_cpu)
1421 printf(" ");
1422 else
1423 printf("*");
1424
1425 if (curr_thread[cpu]) {
1426 if (curr_thread[cpu]->pid)
1427 printf("%2s ", curr_thread[cpu]->shortname);
1428 else
1429 printf(". ");
1430 } else
1431 printf(" ");
1432 }
1433
1434 printf(" %12.6f secs ", (double)timestamp/1e9);
1435 if (new_shortname) {
1436 printf("%s => %s:%d\n",
1437 sched_in->shortname, sched_in->comm, sched_in->pid);
1438 } else {
1439 printf("\n");
1440 }
1441}
1442
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001443static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001444process_sched_switch_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001445 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001446 struct perf_sample *sample,
1447 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001448 struct thread *thread __used)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001449{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001450 int this_cpu = sample->cpu;
1451 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001452 struct trace_switch_event switch_event;
1453
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001454 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001455
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001456 FILL_ARRAY(switch_event, prev_comm, event, data);
1457 FILL_FIELD(switch_event, prev_pid, event, data);
1458 FILL_FIELD(switch_event, prev_prio, event, data);
1459 FILL_FIELD(switch_event, prev_state, event, data);
1460 FILL_ARRAY(switch_event, next_comm, event, data);
1461 FILL_FIELD(switch_event, next_pid, event, data);
1462 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001463
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001464 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001465 /*
1466 * Are we trying to switch away a PID that is
1467 * not current?
1468 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001470 nr_context_switch_bugs++;
1471 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001472 if (trace_handler->switch_event)
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001473 trace_handler->switch_event(&switch_event, machine, event, sample);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001474
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001475 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001476}
1477
1478static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001479process_sched_runtime_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001480 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001481 struct perf_sample *sample,
1482 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001483 struct thread *thread __used)
mingo39aeb522009-09-14 20:04:48 +02001484{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001485 void *data = sample->raw_data;
mingo39aeb522009-09-14 20:04:48 +02001486 struct trace_runtime_event runtime_event;
1487
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001488 FILL_ARRAY(runtime_event, comm, event, data);
1489 FILL_FIELD(runtime_event, pid, event, data);
1490 FILL_FIELD(runtime_event, runtime, event, data);
1491 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001492
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001493 if (trace_handler->runtime_event)
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001494 trace_handler->runtime_event(&runtime_event, machine, sample);
mingo39aeb522009-09-14 20:04:48 +02001495}
1496
1497static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001498process_sched_fork_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001499 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001500 struct perf_sample *sample,
1501 struct machine *machine __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001502 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001503{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001504 void *data = sample->raw_data;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001505 struct trace_fork_event fork_event;
1506
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001507 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001508
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001509 FILL_ARRAY(fork_event, parent_comm, event, data);
1510 FILL_FIELD(fork_event, parent_pid, event, data);
1511 FILL_ARRAY(fork_event, child_comm, event, data);
1512 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001513
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001514 if (trace_handler->fork_event)
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001515 trace_handler->fork_event(&fork_event, event);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001516}
1517
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001518static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001519process_sched_exit_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 __used,
1522 struct machine *machine __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001523 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001524{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001525 if (verbose)
1526 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001527}
1528
1529static void
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001530process_sched_migrate_task_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001531 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001532 struct perf_sample *sample,
1533 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001534 struct thread *thread __used)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001535{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001536 void *data = sample->raw_data;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001537 struct trace_migrate_task_event migrate_task_event;
1538
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001539 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001540
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001541 FILL_ARRAY(migrate_task_event, comm, event, data);
1542 FILL_FIELD(migrate_task_event, pid, event, data);
1543 FILL_FIELD(migrate_task_event, prio, event, data);
1544 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001545
1546 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001547 trace_handler->migrate_task_event(&migrate_task_event, machine, sample);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001548}
1549
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001550typedef void (*tracepoint_handler)(struct perf_tool *tool, struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001551 struct perf_sample *sample,
1552 struct machine *machine,
1553 struct thread *thread);
1554
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001555static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __used,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001556 union perf_event *event __used,
1557 struct perf_sample *sample,
1558 struct perf_evsel *evsel,
1559 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001560{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001561 struct thread *thread = machine__findnew_thread(machine, sample->pid);
Ingo Molnarec156762009-09-11 12:12:54 +02001562
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001563 if (thread == NULL) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001564 pr_debug("problem processing %s event, skipping it.\n",
Arnaldo Carvalho de Melo22c8b842012-06-12 13:55:13 -03001565 perf_evsel__name(evsel));
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001566 return -1;
1567 }
1568
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001569 evsel->hists.stats.total_period += sample->period;
1570 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
Julia Lawallf39cdf22009-10-17 08:43:17 +02001571
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001572 if (evsel->handler.func != NULL) {
1573 tracepoint_handler f = evsel->handler.func;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001574 f(tool, evsel->tp_format, sample, machine, thread);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001575 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001576
1577 return 0;
1578}
1579
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001580static struct perf_tool perf_sched = {
1581 .sample = perf_sched__process_tracepoint_sample,
1582 .comm = perf_event__process_comm,
1583 .lost = perf_event__process_lost,
1584 .fork = perf_event__process_task,
1585 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001586};
1587
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001588static void read_events(bool destroy, struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001589{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001590 int err = -EINVAL;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001591 const struct perf_evsel_str_handler handlers[] = {
1592 { "sched:sched_switch", process_sched_switch_event, },
1593 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1594 { "sched:sched_wakeup", process_sched_wakeup_event, },
1595 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1596 { "sched:sched_process_fork", process_sched_fork_event, },
1597 { "sched:sched_process_exit", process_sched_exit_event, },
1598 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1599 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001600 struct perf_session *session;
1601
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001602 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_sched);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001603 if (session == NULL)
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001604 die("No Memory");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001605
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001606 err = perf_session__set_tracepoints_handlers(session, handlers);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001607 assert(err == 0);
1608
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001609 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001610 err = perf_session__process_events(session, &perf_sched);
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001611 if (err)
1612 die("Failed to process events, error %d", err);
1613
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001614 nr_events = session->hists.stats.nr_events[0];
1615 nr_lost_events = session->hists.stats.total_lost;
1616 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1617 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001618
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001619 if (destroy)
1620 perf_session__delete(session);
1621
1622 if (psession)
1623 *psession = session;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001624}
1625
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001626static void print_bad_events(void)
1627{
1628 if (nr_unordered_timestamps && nr_timestamps) {
1629 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1630 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1631 nr_unordered_timestamps, nr_timestamps);
1632 }
1633 if (nr_lost_events && nr_events) {
1634 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1635 (double)nr_lost_events/(double)nr_events*100.0,
1636 nr_lost_events, nr_events, nr_lost_chunks);
1637 }
1638 if (nr_state_machine_bugs && nr_timestamps) {
1639 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1640 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1641 nr_state_machine_bugs, nr_timestamps);
1642 if (nr_lost_events)
1643 printf(" (due to lost events?)");
1644 printf("\n");
1645 }
1646 if (nr_context_switch_bugs && nr_timestamps) {
1647 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1648 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1649 nr_context_switch_bugs, nr_timestamps);
1650 if (nr_lost_events)
1651 printf(" (due to lost events?)");
1652 printf("\n");
1653 }
1654}
1655
1656static void __cmd_lat(void)
1657{
1658 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001659 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001660
1661 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001662 read_events(false, &session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001663 sort_lat();
1664
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001665 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1666 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1667 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001668
1669 next = rb_first(&sorted_atom_root);
1670
1671 while (next) {
1672 struct work_atoms *work_list;
1673
1674 work_list = rb_entry(next, struct work_atoms, node);
1675 output_lat_thread(work_list);
1676 next = rb_next(next);
1677 }
1678
1679 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001680 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001681 (double)all_runtime/1e6, all_count);
1682
1683 printf(" ---------------------------------------------------\n");
1684
1685 print_bad_events();
1686 printf("\n");
1687
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001688 perf_session__delete(session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001689}
1690
1691static struct trace_sched_handler map_ops = {
1692 .wakeup_event = NULL,
1693 .switch_event = map_switch_event,
1694 .runtime_event = NULL,
1695 .fork_event = NULL,
1696};
1697
1698static void __cmd_map(void)
1699{
Ingo Molnar40749d02009-09-17 18:24:55 +02001700 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1701
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001702 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001703 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001704 print_bad_events();
1705}
1706
1707static void __cmd_replay(void)
1708{
1709 unsigned long i;
1710
1711 calibrate_run_measurement_overhead();
1712 calibrate_sleep_measurement_overhead();
1713
1714 test_calibrations();
1715
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001716 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001717
1718 printf("nr_run_events: %ld\n", nr_run_events);
1719 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1720 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1721
1722 if (targetless_wakeups)
1723 printf("target-less wakeups: %ld\n", targetless_wakeups);
1724 if (multitarget_wakeups)
1725 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1726 if (nr_run_events_optimized)
1727 printf("run atoms optimized: %ld\n",
1728 nr_run_events_optimized);
1729
1730 print_task_traces();
1731 add_cross_task_wakeups();
1732
1733 create_tasks();
1734 printf("------------------------------------------------------------\n");
1735 for (i = 0; i < replay_repeat; i++)
1736 run_one_test();
1737}
1738
1739
Ingo Molnar46f392c2009-09-12 10:08:34 +02001740static const char * const sched_usage[] = {
Jiri Olsa580cabe2011-08-09 14:46:51 +02001741 "perf sched [<options>] {record|latency|map|replay|script}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001742 NULL
1743};
1744
Ingo Molnarf2858d82009-09-11 12:12:54 +02001745static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001746 OPT_STRING('i', "input", &input_name, "file",
1747 "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001748 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001749 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001750 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1751 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001752 OPT_END()
1753};
1754
1755static const char * const latency_usage[] = {
1756 "perf sched latency [<options>]",
1757 NULL
1758};
1759
1760static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001761 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1762 "sort by key(s): runtime, switch, avg, max"),
Ian Munsiec0555642010-04-13 18:37:33 +10001763 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001764 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001765 OPT_INTEGER('C', "CPU", &profile_cpu,
1766 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001767 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1768 "dump raw trace in ASCII"),
1769 OPT_END()
1770};
1771
1772static const char * const replay_usage[] = {
1773 "perf sched replay [<options>]",
1774 NULL
1775};
1776
1777static const struct option replay_options[] = {
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001778 OPT_UINTEGER('r', "repeat", &replay_repeat,
1779 "repeat the workload replay N times (-1: infinite)"),
Ian Munsiec0555642010-04-13 18:37:33 +10001780 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001781 "be more verbose (show symbol address, etc)"),
1782 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1783 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001784 OPT_END()
1785};
1786
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001787static void setup_sorting(void)
1788{
1789 char *tmp, *tok, *str = strdup(sort_order);
1790
1791 for (tok = strtok_r(str, ", ", &tmp);
1792 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1793 if (sort_dimension__add(tok, &sort_list) < 0) {
1794 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001795 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001796 }
1797 }
1798
1799 free(str);
1800
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001801 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001802}
1803
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001804static const char *record_args[] = {
1805 "record",
1806 "-a",
1807 "-R",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001808 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001809 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001810 "-c", "1",
Stephane Eranian97101182011-01-12 10:29:05 +01001811 "-e", "sched:sched_switch",
1812 "-e", "sched:sched_stat_wait",
1813 "-e", "sched:sched_stat_sleep",
1814 "-e", "sched:sched_stat_iowait",
1815 "-e", "sched:sched_stat_runtime",
1816 "-e", "sched:sched_process_exit",
1817 "-e", "sched:sched_process_fork",
1818 "-e", "sched:sched_wakeup",
1819 "-e", "sched:sched_migrate_task",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001820};
1821
1822static int __cmd_record(int argc, const char **argv)
1823{
1824 unsigned int rec_argc, i, j;
1825 const char **rec_argv;
1826
1827 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1828 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1829
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001830 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001831 return -ENOMEM;
1832
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001833 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1834 rec_argv[i] = strdup(record_args[i]);
1835
1836 for (j = 1; j < (unsigned int)argc; j++, i++)
1837 rec_argv[i] = argv[j];
1838
1839 BUG_ON(i != rec_argc);
1840
1841 return cmd_record(i, rec_argv, NULL);
1842}
1843
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001844int cmd_sched(int argc, const char **argv, const char *prefix __used)
1845{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001846 argc = parse_options(argc, argv, sched_options, sched_usage,
1847 PARSE_OPT_STOP_AT_NON_OPTION);
1848 if (!argc)
1849 usage_with_options(sched_usage, sched_options);
1850
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001851 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001852 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001853 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001854 if (!strcmp(argv[0], "script"))
1855 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001856
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001857 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001858 if (!strncmp(argv[0], "rec", 3)) {
1859 return __cmd_record(argc, argv);
1860 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001861 trace_handler = &lat_ops;
1862 if (argc > 1) {
1863 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1864 if (argc)
1865 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001866 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001867 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001868 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001869 } else if (!strcmp(argv[0], "map")) {
1870 trace_handler = &map_ops;
1871 setup_sorting();
1872 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001873 } else if (!strncmp(argv[0], "rep", 3)) {
1874 trace_handler = &replay_ops;
1875 if (argc) {
1876 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1877 if (argc)
1878 usage_with_options(replay_usage, replay_options);
1879 }
1880 __cmd_replay();
1881 } else {
1882 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001883 }
1884
Ingo Molnarec156762009-09-11 12:12:54 +02001885 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001886}