blob: 0ee868e6f63ba2b11de0040634c392a79dc98fbe [file] [log] [blame]
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001#include "builtin.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02002#include "perf.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003
4#include "util/util.h"
5#include "util/cache.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02006#include "util/evsel.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02007#include "util/symbol.h"
8#include "util/thread.h"
9#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020012
13#include "util/parse-options.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020014#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020015
Ingo Molnar0a02ad92009-09-11 12:12:54 +020016#include "util/debug.h"
17
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020018#include <sys/prctl.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020019
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020020#include <semaphore.h>
21#include <pthread.h>
22#include <math.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020023
Ingo Molnarec156762009-09-11 12:12:54 +020024static char const *input_name = "perf.data";
Ingo Molnar0a02ad92009-09-11 12:12:54 +020025
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020026static char default_sort_order[] = "avg, max, switch, runtime";
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -030027static const char *sort_order = default_sort_order;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +020028
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020029static int profile_cpu = -1;
30
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020031#define PR_SET_NAME 15 /* Set process name */
32#define MAX_CPUS 4096
Ingo Molnar0a02ad92009-09-11 12:12:54 +020033
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020034static u64 run_measurement_overhead;
35static u64 sleep_measurement_overhead;
Ingo Molnarec156762009-09-11 12:12:54 +020036
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020037#define COMM_LEN 20
38#define SYM_LEN 129
Ingo Molnarec156762009-09-11 12:12:54 +020039
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020040#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020042static unsigned long nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +020043
mingo39aeb522009-09-14 20:04:48 +020044struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020045
46struct task_desc {
47 unsigned long nr;
48 unsigned long pid;
49 char comm[COMM_LEN];
50
51 unsigned long nr_events;
52 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020053 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020054
55 pthread_t thread;
56 sem_t sleep_sem;
57
58 sem_t ready_for_work;
59 sem_t work_done_sem;
60
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020061 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020062};
63
64enum sched_event_type {
65 SCHED_EVENT_RUN,
66 SCHED_EVENT_SLEEP,
67 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020068 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020069};
70
mingo39aeb522009-09-14 20:04:48 +020071struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020072 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030073 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020074 u64 timestamp;
75 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020076 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020077 sem_t *wait_sem;
78 struct task_desc *wakee;
79};
80
81static struct task_desc *pid_to_task[MAX_PID];
82
83static struct task_desc **tasks;
84
85static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020086static u64 start_time;
Ingo Molnarec156762009-09-11 12:12:54 +020087
88static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
89
90static unsigned long nr_run_events;
91static unsigned long nr_sleep_events;
92static unsigned long nr_wakeup_events;
93
94static unsigned long nr_sleep_corrections;
95static unsigned long nr_run_events_optimized;
96
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097static unsigned long targetless_wakeups;
98static unsigned long multitarget_wakeups;
99
100static u64 cpu_usage;
101static u64 runavg_cpu_usage;
102static u64 parent_cpu_usage;
103static u64 runavg_parent_cpu_usage;
104
105static unsigned long nr_runs;
106static u64 sum_runtime;
107static u64 sum_fluct;
108static u64 run_avg;
109
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -0300110static unsigned int replay_repeat = 10;
Ingo Molnarea57c4f2009-09-13 18:15:54 +0200111static unsigned long nr_timestamps;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200112static unsigned long nr_unordered_timestamps;
113static unsigned long nr_state_machine_bugs;
Ingo Molnarc8a37752009-09-16 14:07:00 +0200114static unsigned long nr_context_switch_bugs;
Ingo Molnardc02bf72009-09-16 13:45:00 +0200115static unsigned long nr_events;
116static unsigned long nr_lost_chunks;
117static unsigned long nr_lost_events;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200118
119#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
120
121enum thread_state {
122 THREAD_SLEEPING = 0,
123 THREAD_WAIT_CPU,
124 THREAD_SCHED_IN,
125 THREAD_IGNORE
126};
127
128struct work_atom {
129 struct list_head list;
130 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200131 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200132 u64 wake_up_time;
133 u64 sched_in_time;
134 u64 runtime;
135};
136
mingo39aeb522009-09-14 20:04:48 +0200137struct work_atoms {
138 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200139 struct thread *thread;
140 struct rb_node node;
141 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100142 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200143 u64 total_lat;
144 u64 nb_atoms;
145 u64 total_runtime;
146};
147
mingo39aeb522009-09-14 20:04:48 +0200148typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200149
150static struct rb_root atom_root, sorted_atom_root;
151
152static u64 all_runtime;
153static u64 all_count;
154
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200155
156static u64 get_nsecs(void)
157{
158 struct timespec ts;
159
160 clock_gettime(CLOCK_MONOTONIC, &ts);
161
162 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
163}
164
165static void burn_nsecs(u64 nsecs)
166{
167 u64 T0 = get_nsecs(), T1;
168
169 do {
170 T1 = get_nsecs();
171 } while (T1 + run_measurement_overhead < T0 + nsecs);
172}
173
174static void sleep_nsecs(u64 nsecs)
175{
176 struct timespec ts;
177
178 ts.tv_nsec = nsecs % 999999999;
179 ts.tv_sec = nsecs / 999999999;
180
181 nanosleep(&ts, NULL);
182}
183
184static void calibrate_run_measurement_overhead(void)
185{
186 u64 T0, T1, delta, min_delta = 1000000000ULL;
187 int i;
188
189 for (i = 0; i < 10; i++) {
190 T0 = get_nsecs();
191 burn_nsecs(0);
192 T1 = get_nsecs();
193 delta = T1-T0;
194 min_delta = min(min_delta, delta);
195 }
196 run_measurement_overhead = min_delta;
197
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200198 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200199}
200
201static void calibrate_sleep_measurement_overhead(void)
202{
203 u64 T0, T1, delta, min_delta = 1000000000ULL;
204 int i;
205
206 for (i = 0; i < 10; i++) {
207 T0 = get_nsecs();
208 sleep_nsecs(10000);
209 T1 = get_nsecs();
210 delta = T1-T0;
211 min_delta = min(min_delta, delta);
212 }
213 min_delta -= 10000;
214 sleep_measurement_overhead = min_delta;
215
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200216 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200217}
218
mingo39aeb522009-09-14 20:04:48 +0200219static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200220get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200221{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200222 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200223 unsigned long idx = task->nr_events;
224 size_t size;
225
226 event->timestamp = timestamp;
227 event->nr = idx;
228
229 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200230 size = sizeof(struct sched_atom *) * task->nr_events;
231 task->atoms = realloc(task->atoms, size);
232 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200233
mingo39aeb522009-09-14 20:04:48 +0200234 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200235
236 return event;
237}
238
mingo39aeb522009-09-14 20:04:48 +0200239static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200240{
241 if (!task->nr_events)
242 return NULL;
243
mingo39aeb522009-09-14 20:04:48 +0200244 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200245}
246
247static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200248add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200249{
mingo39aeb522009-09-14 20:04:48 +0200250 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200251
252 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200253 * optimize an existing RUN event by merging this one
254 * to it:
255 */
Ingo Molnarec156762009-09-11 12:12:54 +0200256 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
257 nr_run_events_optimized++;
258 curr_event->duration += duration;
259 return;
260 }
261
262 event = get_new_event(task, timestamp);
263
264 event->type = SCHED_EVENT_RUN;
265 event->duration = duration;
266
267 nr_run_events++;
268}
269
Ingo Molnarec156762009-09-11 12:12:54 +0200270static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200271add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
Ingo Molnarec156762009-09-11 12:12:54 +0200272 struct task_desc *wakee)
273{
mingo39aeb522009-09-14 20:04:48 +0200274 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200275
276 event = get_new_event(task, timestamp);
277 event->type = SCHED_EVENT_WAKEUP;
278 event->wakee = wakee;
279
280 wakee_event = last_event(wakee);
281 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
282 targetless_wakeups++;
283 return;
284 }
285 if (wakee_event->wait_sem) {
286 multitarget_wakeups++;
287 return;
288 }
289
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200290 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200291 sem_init(wakee_event->wait_sem, 0, 0);
292 wakee_event->specific_wait = 1;
293 event->wait_sem = wakee_event->wait_sem;
294
295 nr_wakeup_events++;
296}
297
298static void
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200299add_sched_event_sleep(struct task_desc *task, u64 timestamp,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200300 u64 task_state __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200301{
mingo39aeb522009-09-14 20:04:48 +0200302 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200303
304 event->type = SCHED_EVENT_SLEEP;
305
306 nr_sleep_events++;
307}
308
309static struct task_desc *register_pid(unsigned long pid, const char *comm)
310{
311 struct task_desc *task;
312
313 BUG_ON(pid >= MAX_PID);
314
315 task = pid_to_task[pid];
316
317 if (task)
318 return task;
319
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200320 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200321 task->pid = pid;
322 task->nr = nr_tasks;
323 strcpy(task->comm, comm);
324 /*
325 * every task starts in sleeping state - this gets ignored
326 * if there's no wakeup pointing to this sleep state:
327 */
328 add_sched_event_sleep(task, 0, 0);
329
330 pid_to_task[pid] = task;
331 nr_tasks++;
332 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
333 BUG_ON(!tasks);
334 tasks[task->nr] = task;
335
Ingo Molnarad236fd2009-09-11 12:12:54 +0200336 if (verbose)
337 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200338
339 return task;
340}
341
342
Ingo Molnarec156762009-09-11 12:12:54 +0200343static void print_task_traces(void)
344{
345 struct task_desc *task;
346 unsigned long i;
347
348 for (i = 0; i < nr_tasks; i++) {
349 task = tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200350 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200351 task->nr, task->comm, task->pid, task->nr_events);
352 }
353}
354
355static void add_cross_task_wakeups(void)
356{
357 struct task_desc *task1, *task2;
358 unsigned long i, j;
359
360 for (i = 0; i < nr_tasks; i++) {
361 task1 = tasks[i];
362 j = i + 1;
363 if (j == nr_tasks)
364 j = 0;
365 task2 = tasks[j];
366 add_sched_event_wakeup(task1, 0, task2);
367 }
368}
369
370static void
mingo39aeb522009-09-14 20:04:48 +0200371process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200372{
373 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200374
mingo39aeb522009-09-14 20:04:48 +0200375 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200376 case SCHED_EVENT_RUN:
mingo39aeb522009-09-14 20:04:48 +0200377 burn_nsecs(atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200378 break;
379 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200380 if (atom->wait_sem)
381 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200382 BUG_ON(ret);
383 break;
384 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200385 if (atom->wait_sem)
386 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200387 BUG_ON(ret);
388 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200389 case SCHED_EVENT_MIGRATION:
390 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200391 default:
392 BUG_ON(1);
393 }
394}
395
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200396static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200397{
398 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200399 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200400 int err;
401
402 err = getrusage(RUSAGE_SELF, &ru);
403 BUG_ON(err);
404
405 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
406 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
407
408 return sum;
409}
410
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800411static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200412{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800413 struct perf_event_attr attr;
414 int fd;
415
416 memset(&attr, 0, sizeof(attr));
417
418 attr.type = PERF_TYPE_SOFTWARE;
419 attr.config = PERF_COUNT_SW_TASK_CLOCK;
420
421 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
422
423 if (fd < 0)
424 die("Error: sys_perf_event_open() syscall returned"
425 "with %d (%s)\n", fd, strerror(errno));
426 return fd;
427}
428
429static u64 get_cpu_usage_nsec_self(int fd)
430{
431 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200432 int ret;
433
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800434 ret = read(fd, &runtime, sizeof(runtime));
435 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200436
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800437 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200438}
439
440static void *thread_func(void *ctx)
441{
442 struct task_desc *this_task = ctx;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200443 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200444 unsigned long i, ret;
445 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800446 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200447
Ingo Molnarec156762009-09-11 12:12:54 +0200448 sprintf(comm2, ":%s", this_task->comm);
449 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800450 fd = self_open_counters();
Ingo Molnarec156762009-09-11 12:12:54 +0200451
452again:
453 ret = sem_post(&this_task->ready_for_work);
454 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200455 ret = pthread_mutex_lock(&start_work_mutex);
456 BUG_ON(ret);
457 ret = pthread_mutex_unlock(&start_work_mutex);
458 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200459
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800460 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200461
462 for (i = 0; i < this_task->nr_events; i++) {
463 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200464 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200465 }
466
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800467 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200468 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200469 ret = sem_post(&this_task->work_done_sem);
470 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200471
472 ret = pthread_mutex_lock(&work_done_wait_mutex);
473 BUG_ON(ret);
474 ret = pthread_mutex_unlock(&work_done_wait_mutex);
475 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200476
477 goto again;
478}
479
480static void create_tasks(void)
481{
482 struct task_desc *task;
483 pthread_attr_t attr;
484 unsigned long i;
485 int err;
486
487 err = pthread_attr_init(&attr);
488 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200489 err = pthread_attr_setstacksize(&attr,
490 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200491 BUG_ON(err);
492 err = pthread_mutex_lock(&start_work_mutex);
493 BUG_ON(err);
494 err = pthread_mutex_lock(&work_done_wait_mutex);
495 BUG_ON(err);
496 for (i = 0; i < nr_tasks; i++) {
497 task = tasks[i];
498 sem_init(&task->sleep_sem, 0, 0);
499 sem_init(&task->ready_for_work, 0, 0);
500 sem_init(&task->work_done_sem, 0, 0);
501 task->curr_event = 0;
502 err = pthread_create(&task->thread, &attr, thread_func, task);
503 BUG_ON(err);
504 }
505}
506
Ingo Molnarec156762009-09-11 12:12:54 +0200507static void wait_for_tasks(void)
508{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200509 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200510 struct task_desc *task;
511 unsigned long i, ret;
512
Ingo Molnarec156762009-09-11 12:12:54 +0200513 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200514 cpu_usage = 0;
515 pthread_mutex_unlock(&work_done_wait_mutex);
516
517 for (i = 0; i < nr_tasks; i++) {
518 task = tasks[i];
519 ret = sem_wait(&task->ready_for_work);
520 BUG_ON(ret);
521 sem_init(&task->ready_for_work, 0, 0);
522 }
523 ret = pthread_mutex_lock(&work_done_wait_mutex);
524 BUG_ON(ret);
525
526 cpu_usage_0 = get_cpu_usage_nsec_parent();
527
528 pthread_mutex_unlock(&start_work_mutex);
529
Ingo Molnarec156762009-09-11 12:12:54 +0200530 for (i = 0; i < nr_tasks; i++) {
531 task = tasks[i];
532 ret = sem_wait(&task->work_done_sem);
533 BUG_ON(ret);
534 sem_init(&task->work_done_sem, 0, 0);
535 cpu_usage += task->cpu_usage;
536 task->cpu_usage = 0;
537 }
538
539 cpu_usage_1 = get_cpu_usage_nsec_parent();
540 if (!runavg_cpu_usage)
541 runavg_cpu_usage = cpu_usage;
542 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
543
544 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
545 if (!runavg_parent_cpu_usage)
546 runavg_parent_cpu_usage = parent_cpu_usage;
547 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
548 parent_cpu_usage)/10;
549
550 ret = pthread_mutex_lock(&start_work_mutex);
551 BUG_ON(ret);
552
553 for (i = 0; i < nr_tasks; i++) {
554 task = tasks[i];
555 sem_init(&task->sleep_sem, 0, 0);
556 task->curr_event = 0;
557 }
558}
559
Ingo Molnarec156762009-09-11 12:12:54 +0200560static void run_one_test(void)
561{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500562 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200563
564 T0 = get_nsecs();
565 wait_for_tasks();
566 T1 = get_nsecs();
567
568 delta = T1 - T0;
569 sum_runtime += delta;
570 nr_runs++;
571
572 avg_delta = sum_runtime / nr_runs;
573 if (delta < avg_delta)
574 fluct = avg_delta - delta;
575 else
576 fluct = delta - avg_delta;
577 sum_fluct += fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200578 if (!run_avg)
579 run_avg = delta;
580 run_avg = (run_avg*9 + delta)/10;
581
Ingo Molnarad236fd2009-09-11 12:12:54 +0200582 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200583 nr_runs, (double)delta/1000000.0);
584
Ingo Molnarad236fd2009-09-11 12:12:54 +0200585 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200586 (double)run_avg/1e6);
587
Ingo Molnarad236fd2009-09-11 12:12:54 +0200588 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200589 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
590
591#if 0
592 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200593 * rusage statistics done by the parent, these are less
594 * accurate than the sum_exec_runtime based statistics:
595 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200596 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200597 (double)parent_cpu_usage/1e6,
598 (double)runavg_parent_cpu_usage/1e6);
599#endif
600
Ingo Molnarad236fd2009-09-11 12:12:54 +0200601 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200602
603 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200604 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200605 nr_sleep_corrections = 0;
606}
607
608static void test_calibrations(void)
609{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200610 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200611
612 T0 = get_nsecs();
613 burn_nsecs(1e6);
614 T1 = get_nsecs();
615
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200616 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200617
618 T0 = get_nsecs();
619 sleep_nsecs(1e6);
620 T1 = get_nsecs();
621
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200622 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200623}
624
Frederic Weisbecker46538812009-09-12 02:43:45 +0200625#define FILL_FIELD(ptr, field, event, data) \
626 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
627
628#define FILL_ARRAY(ptr, array, event, data) \
629do { \
630 void *__array = raw_field_ptr(event, #array, data); \
631 memcpy(ptr.array, __array, sizeof(ptr.array)); \
632} while(0)
633
634#define FILL_COMMON_FIELDS(ptr, event, data) \
635do { \
636 FILL_FIELD(ptr, common_type, event, data); \
637 FILL_FIELD(ptr, common_flags, event, data); \
638 FILL_FIELD(ptr, common_preempt_count, event, data); \
639 FILL_FIELD(ptr, common_pid, event, data); \
640 FILL_FIELD(ptr, common_tgid, event, data); \
641} while (0)
642
Ingo Molnarfbf94822009-09-11 12:12:54 +0200643
Ingo Molnarec156762009-09-11 12:12:54 +0200644
Ingo Molnarfbf94822009-09-11 12:12:54 +0200645struct trace_switch_event {
646 u32 size;
647
648 u16 common_type;
649 u8 common_flags;
650 u8 common_preempt_count;
651 u32 common_pid;
652 u32 common_tgid;
653
654 char prev_comm[16];
655 u32 prev_pid;
656 u32 prev_prio;
657 u64 prev_state;
658 char next_comm[16];
659 u32 next_pid;
660 u32 next_prio;
661};
662
mingo39aeb522009-09-14 20:04:48 +0200663struct trace_runtime_event {
664 u32 size;
665
666 u16 common_type;
667 u8 common_flags;
668 u8 common_preempt_count;
669 u32 common_pid;
670 u32 common_tgid;
671
672 char comm[16];
673 u32 pid;
674 u64 runtime;
675 u64 vruntime;
676};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200677
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200678struct trace_wakeup_event {
679 u32 size;
680
681 u16 common_type;
682 u8 common_flags;
683 u8 common_preempt_count;
684 u32 common_pid;
685 u32 common_tgid;
686
687 char comm[16];
688 u32 pid;
689
690 u32 prio;
691 u32 success;
692 u32 cpu;
693};
694
695struct trace_fork_event {
696 u32 size;
697
698 u16 common_type;
699 u8 common_flags;
700 u8 common_preempt_count;
701 u32 common_pid;
702 u32 common_tgid;
703
704 char parent_comm[16];
705 u32 parent_pid;
706 char child_comm[16];
707 u32 child_pid;
708};
709
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200710struct trace_migrate_task_event {
711 u32 size;
712
713 u16 common_type;
714 u8 common_flags;
715 u8 common_preempt_count;
716 u32 common_pid;
717 u32 common_tgid;
718
719 char comm[16];
720 u32 pid;
721
722 u32 prio;
723 u32 cpu;
724};
725
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200726struct trace_sched_handler {
727 void (*switch_event)(struct trace_switch_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200728 struct machine *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200729 struct event *,
730 int cpu,
731 u64 timestamp,
732 struct thread *thread);
733
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 *,
mingo39aeb522009-09-14 20:04:48 +0200736 struct event *,
737 int cpu,
738 u64 timestamp,
739 struct thread *thread);
740
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200741 void (*wakeup_event)(struct trace_wakeup_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200742 struct machine *,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200743 struct event *,
744 int cpu,
745 u64 timestamp,
746 struct thread *thread);
747
748 void (*fork_event)(struct trace_fork_event *,
749 struct event *,
750 int cpu,
751 u64 timestamp,
752 struct thread *thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200753
754 void (*migrate_task_event)(struct trace_migrate_task_event *,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200755 struct machine *machine,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200756 struct event *,
757 int cpu,
758 u64 timestamp,
759 struct thread *thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200760};
761
Ingo Molnarfbf94822009-09-11 12:12:54 +0200762
763static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200764replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200765 struct machine *machine __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200766 struct event *event,
767 int cpu __used,
768 u64 timestamp __used,
769 struct thread *thread __used)
Ingo Molnarec156762009-09-11 12:12:54 +0200770{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200771 struct task_desc *waker, *wakee;
772
773 if (verbose) {
774 printf("sched_wakeup event %p\n", event);
775
776 printf(" ... pid %d woke up %s/%d\n",
777 wakeup_event->common_pid,
778 wakeup_event->comm,
779 wakeup_event->pid);
780 }
781
782 waker = register_pid(wakeup_event->common_pid, "<unknown>");
783 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
784
785 add_sched_event_wakeup(waker, timestamp, wakee);
786}
787
Ingo Molnard1153382009-09-14 18:22:53 +0200788static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200789
790static void
791replay_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200792 struct machine *machine __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200793 struct event *event,
794 int cpu,
795 u64 timestamp,
796 struct thread *thread __used)
797{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500798 struct task_desc *prev, __used *next;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200799 u64 timestamp0;
800 s64 delta;
801
Ingo Molnarad236fd2009-09-11 12:12:54 +0200802 if (verbose)
803 printf("sched_switch event %p\n", event);
804
Ingo Molnarfbf94822009-09-11 12:12:54 +0200805 if (cpu >= MAX_CPUS || cpu < 0)
806 return;
807
808 timestamp0 = cpu_last_switched[cpu];
809 if (timestamp0)
810 delta = timestamp - timestamp0;
811 else
812 delta = 0;
813
814 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200815 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200816
Ingo Molnarad236fd2009-09-11 12:12:54 +0200817 if (verbose) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200818 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200819 switch_event->prev_comm, switch_event->prev_pid,
820 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200821 delta);
822 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200823
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200824 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
825 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200826
827 cpu_last_switched[cpu] = timestamp;
828
829 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200830 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200831}
832
Ingo Molnarfbf94822009-09-11 12:12:54 +0200833
834static void
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200835replay_fork_event(struct trace_fork_event *fork_event,
836 struct event *event,
837 int cpu __used,
838 u64 timestamp __used,
839 struct thread *thread __used)
840{
841 if (verbose) {
842 printf("sched_fork event %p\n", event);
843 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
844 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
845 }
846 register_pid(fork_event->parent_pid, fork_event->parent_comm);
847 register_pid(fork_event->child_pid, fork_event->child_comm);
848}
849
850static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200851 .wakeup_event = replay_wakeup_event,
852 .switch_event = replay_switch_event,
853 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200854};
855
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200856struct sort_dimension {
857 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200858 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200859 struct list_head list;
860};
861
862static LIST_HEAD(cmp_pid);
863
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200864static int
mingo39aeb522009-09-14 20:04:48 +0200865thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200866{
867 struct sort_dimension *sort;
868 int ret = 0;
869
Ingo Molnarb5fae122009-09-11 12:12:54 +0200870 BUG_ON(list_empty(list));
871
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200872 list_for_each_entry(sort, list, list) {
873 ret = sort->cmp(l, r);
874 if (ret)
875 return ret;
876 }
877
878 return ret;
879}
880
mingo39aeb522009-09-14 20:04:48 +0200881static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200882thread_atoms_search(struct rb_root *root, struct thread *thread,
883 struct list_head *sort_list)
884{
885 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200886 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200887
888 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200889 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200890 int cmp;
891
mingo39aeb522009-09-14 20:04:48 +0200892 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200893
894 cmp = thread_lat_cmp(sort_list, &key, atoms);
895 if (cmp > 0)
896 node = node->rb_left;
897 else if (cmp < 0)
898 node = node->rb_right;
899 else {
900 BUG_ON(thread != atoms->thread);
901 return atoms;
902 }
903 }
904 return NULL;
905}
906
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907static void
mingo39aeb522009-09-14 20:04:48 +0200908__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200909 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910{
911 struct rb_node **new = &(root->rb_node), *parent = NULL;
912
913 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200914 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200915 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200916
mingo39aeb522009-09-14 20:04:48 +0200917 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200918 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200919
920 cmp = thread_lat_cmp(sort_list, data, this);
921
922 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200924 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200925 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200926 }
927
928 rb_link_node(&data->node, parent, new);
929 rb_insert_color(&data->node, root);
930}
931
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200932static void thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200934 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Frederic Weisbecker17562202009-09-12 23:11:32 +0200935 if (!atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200936 die("No memory");
937
Frederic Weisbecker17562202009-09-12 23:11:32 +0200938 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200939 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200940 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941}
942
943static void
944latency_fork_event(struct trace_fork_event *fork_event __used,
945 struct event *event __used,
946 int cpu __used,
947 u64 timestamp __used,
948 struct thread *thread __used)
949{
950 /* should insert the newcomer */
951}
952
Ingo Molnarea92ed52009-09-12 10:08:34 +0200953__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954static char sched_out_state(struct trace_switch_event *switch_event)
955{
956 const char *str = TASK_STATE_TO_CHAR_STR;
957
958 return str[switch_event->prev_state];
959}
960
961static void
mingo39aeb522009-09-14 20:04:48 +0200962add_sched_out_event(struct work_atoms *atoms,
963 char run_state,
964 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200965{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200966 struct work_atom *atom = zalloc(sizeof(*atom));
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200967 if (!atom)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200968 die("Non memory");
969
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200970 atom->sched_out_time = timestamp;
971
mingo39aeb522009-09-14 20:04:48 +0200972 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200973 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200974 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200975 }
976
mingo39aeb522009-09-14 20:04:48 +0200977 list_add_tail(&atom->list, &atoms->work_list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978}
979
980static void
mingo39aeb522009-09-14 20:04:48 +0200981add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
982{
983 struct work_atom *atom;
984
985 BUG_ON(list_empty(&atoms->work_list));
986
987 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
988
989 atom->runtime += delta;
990 atoms->total_runtime += delta;
991}
992
993static void
994add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200995{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200996 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200997 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200998
mingo39aeb522009-09-14 20:04:48 +0200999 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001000 return;
1001
mingo39aeb522009-09-14 20:04:48 +02001002 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001003
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001004 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001005 return;
1006
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001007 if (timestamp < atom->wake_up_time) {
1008 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001009 return;
1010 }
1011
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001012 atom->state = THREAD_SCHED_IN;
1013 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001014
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001015 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001016 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001017 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001018 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001019 atoms->max_lat_at = timestamp;
1020 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001021 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001022}
1023
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024static void
1025latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001026 struct machine *machine,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027 struct event *event __used,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001028 int cpu,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001029 u64 timestamp,
1030 struct thread *thread __used)
1031{
mingo39aeb522009-09-14 20:04:48 +02001032 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001033 struct thread *sched_out, *sched_in;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001034 u64 timestamp0;
1035 s64 delta;
1036
mingo39aeb522009-09-14 20:04:48 +02001037 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001038
1039 timestamp0 = cpu_last_switched[cpu];
1040 cpu_last_switched[cpu] = timestamp;
1041 if (timestamp0)
1042 delta = timestamp - timestamp0;
1043 else
1044 delta = 0;
1045
1046 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001047 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001048
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001049
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001050 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1051 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001052
mingo39aeb522009-09-14 20:04:48 +02001053 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1054 if (!out_events) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001055 thread_atoms_insert(sched_out);
mingo39aeb522009-09-14 20:04:48 +02001056 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1057 if (!out_events)
1058 die("out-event: Internal tree error");
1059 }
1060 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1061
1062 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1063 if (!in_events) {
1064 thread_atoms_insert(sched_in);
1065 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1066 if (!in_events)
1067 die("in-event: Internal tree error");
1068 /*
1069 * Take came in we have not heard about yet,
1070 * add in an initial atom in runnable state:
1071 */
1072 add_sched_out_event(in_events, 'R', timestamp);
1073 }
1074 add_sched_in_event(in_events, timestamp);
1075}
1076
1077static void
1078latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001079 struct machine *machine,
mingo39aeb522009-09-14 20:04:48 +02001080 struct event *event __used,
1081 int cpu,
1082 u64 timestamp,
1083 struct thread *this_thread __used)
1084{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001085 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001086 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001087
1088 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001089 if (!atoms) {
1090 thread_atoms_insert(thread);
1091 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1092 if (!atoms)
1093 die("in-event: Internal tree error");
1094 add_sched_out_event(atoms, 'R', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001095 }
1096
mingo39aeb522009-09-14 20:04:48 +02001097 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001098}
1099
1100static void
1101latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001102 struct machine *machine,
mingo39aeb522009-09-14 20:04:48 +02001103 struct event *__event __used,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001104 int cpu __used,
1105 u64 timestamp,
1106 struct thread *thread __used)
1107{
mingo39aeb522009-09-14 20:04:48 +02001108 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001109 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001110 struct thread *wakee;
1111
1112 /* Note for later, it may be interesting to observe the failing cases */
1113 if (!wakeup_event->success)
1114 return;
1115
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001116 wakee = machine__findnew_thread(machine, wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001117 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001118 if (!atoms) {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001119 thread_atoms_insert(wakee);
mingo39aeb522009-09-14 20:04:48 +02001120 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1121 if (!atoms)
1122 die("wakeup-event: Internal tree error");
1123 add_sched_out_event(atoms, 'S', timestamp);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124 }
1125
mingo39aeb522009-09-14 20:04:48 +02001126 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001127
mingo39aeb522009-09-14 20:04:48 +02001128 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001129
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001130 /*
1131 * You WILL be missing events if you've recorded only
1132 * one CPU, or are only looking at only one, so don't
1133 * make useless noise.
1134 */
1135 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001136 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001138 nr_timestamps++;
1139 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001140 nr_unordered_timestamps++;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001141 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001142 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001143
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001144 atom->state = THREAD_WAIT_CPU;
1145 atom->wake_up_time = timestamp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001146}
1147
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001148static void
1149latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001150 struct machine *machine,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001151 struct event *__event __used,
1152 int cpu __used,
1153 u64 timestamp,
1154 struct thread *thread __used)
1155{
1156 struct work_atoms *atoms;
1157 struct work_atom *atom;
1158 struct thread *migrant;
1159
1160 /*
1161 * Only need to worry about migration when profiling one CPU.
1162 */
1163 if (profile_cpu == -1)
1164 return;
1165
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001166 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001167 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1168 if (!atoms) {
1169 thread_atoms_insert(migrant);
1170 register_pid(migrant->pid, migrant->comm);
1171 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1172 if (!atoms)
1173 die("migration-event: Internal tree error");
1174 add_sched_out_event(atoms, 'R', timestamp);
1175 }
1176
1177 BUG_ON(list_empty(&atoms->work_list));
1178
1179 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1180 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1181
1182 nr_timestamps++;
1183
1184 if (atom->sched_out_time > timestamp)
1185 nr_unordered_timestamps++;
1186}
1187
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001188static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001189 .wakeup_event = latency_wakeup_event,
1190 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001191 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001192 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001193 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001194};
1195
mingo39aeb522009-09-14 20:04:48 +02001196static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001197{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001198 int i;
1199 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001200 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001201
mingo39aeb522009-09-14 20:04:48 +02001202 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001204 /*
1205 * Ignore idle threads:
1206 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001207 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001208 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209
mingo39aeb522009-09-14 20:04:48 +02001210 all_runtime += work_list->total_runtime;
1211 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001212
Ingo Molnar80ed0982009-09-16 14:12:36 +02001213 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001214
mingo08f69e62009-09-14 18:30:44 +02001215 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001216 printf(" ");
1217
mingo39aeb522009-09-14 20:04:48 +02001218 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001219
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001220 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 +02001221 (double)work_list->total_runtime / 1e6,
1222 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001223 (double)work_list->max_lat / 1e6,
1224 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001225}
1226
mingo39aeb522009-09-14 20:04:48 +02001227static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001228{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001229 if (l->thread->pid < r->thread->pid)
1230 return -1;
1231 if (l->thread->pid > r->thread->pid)
1232 return 1;
1233
1234 return 0;
1235}
1236
1237static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001238 .name = "pid",
1239 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001240};
1241
mingo39aeb522009-09-14 20:04:48 +02001242static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001243{
1244 u64 avgl, avgr;
1245
1246 if (!l->nb_atoms)
1247 return -1;
1248
1249 if (!r->nb_atoms)
1250 return 1;
1251
1252 avgl = l->total_lat / l->nb_atoms;
1253 avgr = r->total_lat / r->nb_atoms;
1254
1255 if (avgl < avgr)
1256 return -1;
1257 if (avgl > avgr)
1258 return 1;
1259
1260 return 0;
1261}
1262
1263static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001264 .name = "avg",
1265 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001266};
1267
mingo39aeb522009-09-14 20:04:48 +02001268static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001269{
1270 if (l->max_lat < r->max_lat)
1271 return -1;
1272 if (l->max_lat > r->max_lat)
1273 return 1;
1274
1275 return 0;
1276}
1277
1278static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001279 .name = "max",
1280 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001281};
1282
mingo39aeb522009-09-14 20:04:48 +02001283static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001284{
1285 if (l->nb_atoms < r->nb_atoms)
1286 return -1;
1287 if (l->nb_atoms > r->nb_atoms)
1288 return 1;
1289
1290 return 0;
1291}
1292
1293static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001294 .name = "switch",
1295 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001296};
1297
mingo39aeb522009-09-14 20:04:48 +02001298static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001299{
1300 if (l->total_runtime < r->total_runtime)
1301 return -1;
1302 if (l->total_runtime > r->total_runtime)
1303 return 1;
1304
1305 return 0;
1306}
1307
1308static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001309 .name = "runtime",
1310 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001311};
1312
1313static struct sort_dimension *available_sorts[] = {
1314 &pid_sort_dimension,
1315 &avg_sort_dimension,
1316 &max_sort_dimension,
1317 &switch_sort_dimension,
1318 &runtime_sort_dimension,
1319};
1320
1321#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1322
1323static LIST_HEAD(sort_list);
1324
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001325static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001326{
1327 int i;
1328
1329 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1330 if (!strcmp(available_sorts[i]->name, tok)) {
1331 list_add_tail(&available_sorts[i]->list, list);
1332
1333 return 0;
1334 }
1335 }
1336
1337 return -1;
1338}
1339
1340static void setup_sorting(void);
1341
1342static void sort_lat(void)
1343{
1344 struct rb_node *node;
1345
1346 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001347 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001348 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001349 if (!node)
1350 break;
1351
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001352 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001353 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001354 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001355 }
1356}
1357
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001358static struct trace_sched_handler *trace_handler;
1359
1360static void
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001361process_sched_wakeup_event(void *data, struct machine *machine,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001362 struct event *event,
1363 int cpu __used,
1364 u64 timestamp __used,
1365 struct thread *thread __used)
1366{
1367 struct trace_wakeup_event wakeup_event;
1368
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001369 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001370
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001371 FILL_ARRAY(wakeup_event, comm, event, data);
1372 FILL_FIELD(wakeup_event, pid, event, data);
1373 FILL_FIELD(wakeup_event, prio, event, data);
1374 FILL_FIELD(wakeup_event, success, event, data);
1375 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001376
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001377 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001378 trace_handler->wakeup_event(&wakeup_event, machine, event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001379 cpu, timestamp, thread);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001380}
1381
Ingo Molnarc8a37752009-09-16 14:07:00 +02001382/*
1383 * Track the current task - that way we can know whether there's any
1384 * weird events, such as a task being switched away that is not current.
1385 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001386static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001387
Ingo Molnarc8a37752009-09-16 14:07:00 +02001388static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1389
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001390static struct thread *curr_thread[MAX_CPUS];
1391
1392static char next_shortname1 = 'A';
1393static char next_shortname2 = '0';
1394
1395static void
1396map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001397 struct machine *machine,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001398 struct event *event __used,
1399 int this_cpu,
1400 u64 timestamp,
1401 struct thread *thread __used)
1402{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05001403 struct thread *sched_out __used, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001404 int new_shortname;
1405 u64 timestamp0;
1406 s64 delta;
1407 int cpu;
1408
1409 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1410
1411 if (this_cpu > max_cpu)
1412 max_cpu = this_cpu;
1413
1414 timestamp0 = cpu_last_switched[this_cpu];
1415 cpu_last_switched[this_cpu] = timestamp;
1416 if (timestamp0)
1417 delta = timestamp - timestamp0;
1418 else
1419 delta = 0;
1420
1421 if (delta < 0)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001422 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001423
1424
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001425 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1426 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001427
1428 curr_thread[this_cpu] = sched_in;
1429
1430 printf(" ");
1431
1432 new_shortname = 0;
1433 if (!sched_in->shortname[0]) {
1434 sched_in->shortname[0] = next_shortname1;
1435 sched_in->shortname[1] = next_shortname2;
1436
1437 if (next_shortname1 < 'Z') {
1438 next_shortname1++;
1439 } else {
1440 next_shortname1='A';
1441 if (next_shortname2 < '9') {
1442 next_shortname2++;
1443 } else {
1444 next_shortname2='0';
1445 }
1446 }
1447 new_shortname = 1;
1448 }
1449
1450 for (cpu = 0; cpu <= max_cpu; cpu++) {
1451 if (cpu != this_cpu)
1452 printf(" ");
1453 else
1454 printf("*");
1455
1456 if (curr_thread[cpu]) {
1457 if (curr_thread[cpu]->pid)
1458 printf("%2s ", curr_thread[cpu]->shortname);
1459 else
1460 printf(". ");
1461 } else
1462 printf(" ");
1463 }
1464
1465 printf(" %12.6f secs ", (double)timestamp/1e9);
1466 if (new_shortname) {
1467 printf("%s => %s:%d\n",
1468 sched_in->shortname, sched_in->comm, sched_in->pid);
1469 } else {
1470 printf("\n");
1471 }
1472}
1473
1474
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001475static void
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001476process_sched_switch_event(void *data, struct machine *machine,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001477 struct event *event,
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001478 int this_cpu,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001479 u64 timestamp __used,
1480 struct thread *thread __used)
1481{
1482 struct trace_switch_event switch_event;
1483
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001484 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001485
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001486 FILL_ARRAY(switch_event, prev_comm, event, data);
1487 FILL_FIELD(switch_event, prev_pid, event, data);
1488 FILL_FIELD(switch_event, prev_prio, event, data);
1489 FILL_FIELD(switch_event, prev_state, event, data);
1490 FILL_ARRAY(switch_event, next_comm, event, data);
1491 FILL_FIELD(switch_event, next_pid, event, data);
1492 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001493
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001494 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001495 /*
1496 * Are we trying to switch away a PID that is
1497 * not current?
1498 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001499 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001500 nr_context_switch_bugs++;
1501 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001502 if (trace_handler->switch_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001503 trace_handler->switch_event(&switch_event, machine, event,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001504 this_cpu, timestamp, thread);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001505
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001506 curr_pid[this_cpu] = switch_event.next_pid;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001507}
1508
1509static void
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001510process_sched_runtime_event(void *data, struct machine *machine,
mingo39aeb522009-09-14 20:04:48 +02001511 struct event *event,
1512 int cpu __used,
1513 u64 timestamp __used,
1514 struct thread *thread __used)
1515{
1516 struct trace_runtime_event runtime_event;
1517
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001518 FILL_ARRAY(runtime_event, comm, event, data);
1519 FILL_FIELD(runtime_event, pid, event, data);
1520 FILL_FIELD(runtime_event, runtime, event, data);
1521 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001522
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001523 if (trace_handler->runtime_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001524 trace_handler->runtime_event(&runtime_event, machine, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001525}
1526
1527static void
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001528process_sched_fork_event(void *data,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001529 struct event *event,
1530 int cpu __used,
1531 u64 timestamp __used,
1532 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001533{
Frederic Weisbecker46538812009-09-12 02:43:45 +02001534 struct trace_fork_event fork_event;
1535
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001536 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001537
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001538 FILL_ARRAY(fork_event, parent_comm, event, data);
1539 FILL_FIELD(fork_event, parent_pid, event, data);
1540 FILL_ARRAY(fork_event, child_comm, event, data);
1541 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001542
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001543 if (trace_handler->fork_event)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001544 trace_handler->fork_event(&fork_event, event,
1545 cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001546}
1547
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001548static void
1549process_sched_exit_event(struct event *event,
1550 int cpu __used,
1551 u64 timestamp __used,
1552 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001553{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001554 if (verbose)
1555 printf("sched_exit event %p\n", event);
Ingo Molnarec156762009-09-11 12:12:54 +02001556}
1557
1558static void
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001559process_sched_migrate_task_event(void *data, struct machine *machine,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001560 struct event *event,
1561 int cpu __used,
1562 u64 timestamp __used,
1563 struct thread *thread __used)
1564{
1565 struct trace_migrate_task_event migrate_task_event;
1566
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001567 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001568
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001569 FILL_ARRAY(migrate_task_event, comm, event, data);
1570 FILL_FIELD(migrate_task_event, pid, event, data);
1571 FILL_FIELD(migrate_task_event, prio, event, data);
1572 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001573
1574 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001575 trace_handler->migrate_task_event(&migrate_task_event, machine,
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02001576 event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001577}
1578
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001579static void process_raw_event(union perf_event *raw_event __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001580 struct machine *machine, void *data, int cpu,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001581 u64 timestamp, struct thread *thread)
Ingo Molnarec156762009-09-11 12:12:54 +02001582{
Ingo Molnarec156762009-09-11 12:12:54 +02001583 struct event *event;
1584 int type;
1585
Xiao Guangrongd8bd9e02009-12-07 12:06:29 +08001586
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001587 type = trace_parse_common_type(data);
Ingo Molnarec156762009-09-11 12:12:54 +02001588 event = trace_find_event(type);
1589
Ingo Molnarec156762009-09-11 12:12:54 +02001590 if (!strcmp(event->name, "sched_switch"))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001591 process_sched_switch_event(data, machine, event, cpu, timestamp, thread);
mingo39aeb522009-09-14 20:04:48 +02001592 if (!strcmp(event->name, "sched_stat_runtime"))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001593 process_sched_runtime_event(data, machine, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001594 if (!strcmp(event->name, "sched_wakeup"))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001595 process_sched_wakeup_event(data, machine, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001596 if (!strcmp(event->name, "sched_wakeup_new"))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001597 process_sched_wakeup_event(data, machine, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001598 if (!strcmp(event->name, "sched_process_fork"))
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001599 process_sched_fork_event(data, event, cpu, timestamp, thread);
Ingo Molnarfbf94822009-09-11 12:12:54 +02001600 if (!strcmp(event->name, "sched_process_exit"))
1601 process_sched_exit_event(event, cpu, timestamp, thread);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001602 if (!strcmp(event->name, "sched_migrate_task"))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001603 process_sched_migrate_task_event(data, machine, event, cpu, timestamp, thread);
Ingo Molnarec156762009-09-11 12:12:54 +02001604}
1605
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001606static int process_sample_event(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001607 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001608 struct perf_sample *sample,
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02001609 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001610 struct machine *machine)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001611{
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001612 struct thread *thread;
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001613
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02001614 if (!(evsel->attr.sample_type & PERF_SAMPLE_RAW))
Arnaldo Carvalho de Meloa80deb62009-09-28 15:23:51 -03001615 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001616
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001617 thread = machine__findnew_thread(machine, sample->pid);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001618 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001619 pr_debug("problem processing %d event, skipping it.\n",
1620 event->header.type);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001621 return -1;
1622 }
1623
Julia Lawallf39cdf22009-10-17 08:43:17 +02001624 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1625
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001626 if (profile_cpu != -1 && profile_cpu != (int)sample->cpu)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001627 return 0;
1628
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001629 process_raw_event(event, machine, sample->raw_data, sample->cpu,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001630 sample->time, thread);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001631
1632 return 0;
1633}
1634
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001635static struct perf_tool perf_sched = {
Frederic Weisbeckera64eae72010-04-24 00:29:40 +02001636 .sample = process_sample_event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001637 .comm = perf_event__process_comm,
1638 .lost = perf_event__process_lost,
1639 .fork = perf_event__process_task,
Frederic Weisbeckera64eae72010-04-24 00:29:40 +02001640 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001641};
1642
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001643static void read_events(bool destroy, struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001644{
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001645 int err = -EINVAL;
Ian Munsie21ef97f2010-12-10 14:09:16 +11001646 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001647 0, false, &perf_sched);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001648 if (session == NULL)
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001649 die("No Memory");
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001650
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001651 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001652 err = perf_session__process_events(session, &perf_sched);
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001653 if (err)
1654 die("Failed to process events, error %d", err);
1655
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001656 nr_events = session->hists.stats.nr_events[0];
1657 nr_lost_events = session->hists.stats.total_lost;
1658 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1659 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001660
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001661 if (destroy)
1662 perf_session__delete(session);
1663
1664 if (psession)
1665 *psession = session;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001666}
1667
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001668static void print_bad_events(void)
1669{
1670 if (nr_unordered_timestamps && nr_timestamps) {
1671 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1672 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1673 nr_unordered_timestamps, nr_timestamps);
1674 }
1675 if (nr_lost_events && nr_events) {
1676 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1677 (double)nr_lost_events/(double)nr_events*100.0,
1678 nr_lost_events, nr_events, nr_lost_chunks);
1679 }
1680 if (nr_state_machine_bugs && nr_timestamps) {
1681 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1682 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1683 nr_state_machine_bugs, nr_timestamps);
1684 if (nr_lost_events)
1685 printf(" (due to lost events?)");
1686 printf("\n");
1687 }
1688 if (nr_context_switch_bugs && nr_timestamps) {
1689 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1690 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1691 nr_context_switch_bugs, nr_timestamps);
1692 if (nr_lost_events)
1693 printf(" (due to lost events?)");
1694 printf("\n");
1695 }
1696}
1697
1698static void __cmd_lat(void)
1699{
1700 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001701 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001702
1703 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001704 read_events(false, &session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001705 sort_lat();
1706
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001707 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1708 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1709 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001710
1711 next = rb_first(&sorted_atom_root);
1712
1713 while (next) {
1714 struct work_atoms *work_list;
1715
1716 work_list = rb_entry(next, struct work_atoms, node);
1717 output_lat_thread(work_list);
1718 next = rb_next(next);
1719 }
1720
1721 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001722 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001723 (double)all_runtime/1e6, all_count);
1724
1725 printf(" ---------------------------------------------------\n");
1726
1727 print_bad_events();
1728 printf("\n");
1729
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001730 perf_session__delete(session);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001731}
1732
1733static struct trace_sched_handler map_ops = {
1734 .wakeup_event = NULL,
1735 .switch_event = map_switch_event,
1736 .runtime_event = NULL,
1737 .fork_event = NULL,
1738};
1739
1740static void __cmd_map(void)
1741{
Ingo Molnar40749d02009-09-17 18:24:55 +02001742 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1743
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001744 setup_pager();
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001745 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001746 print_bad_events();
1747}
1748
1749static void __cmd_replay(void)
1750{
1751 unsigned long i;
1752
1753 calibrate_run_measurement_overhead();
1754 calibrate_sleep_measurement_overhead();
1755
1756 test_calibrations();
1757
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001758 read_events(true, NULL);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001759
1760 printf("nr_run_events: %ld\n", nr_run_events);
1761 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1762 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1763
1764 if (targetless_wakeups)
1765 printf("target-less wakeups: %ld\n", targetless_wakeups);
1766 if (multitarget_wakeups)
1767 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1768 if (nr_run_events_optimized)
1769 printf("run atoms optimized: %ld\n",
1770 nr_run_events_optimized);
1771
1772 print_task_traces();
1773 add_cross_task_wakeups();
1774
1775 create_tasks();
1776 printf("------------------------------------------------------------\n");
1777 for (i = 0; i < replay_repeat; i++)
1778 run_one_test();
1779}
1780
1781
Ingo Molnar46f392c2009-09-12 10:08:34 +02001782static const char * const sched_usage[] = {
Jiri Olsa580cabe2011-08-09 14:46:51 +02001783 "perf sched [<options>] {record|latency|map|replay|script}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001784 NULL
1785};
1786
Ingo Molnarf2858d82009-09-11 12:12:54 +02001787static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001788 OPT_STRING('i', "input", &input_name, "file",
1789 "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001790 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001791 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001792 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1793 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001794 OPT_END()
1795};
1796
1797static const char * const latency_usage[] = {
1798 "perf sched latency [<options>]",
1799 NULL
1800};
1801
1802static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001803 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1804 "sort by key(s): runtime, switch, avg, max"),
Ian Munsiec0555642010-04-13 18:37:33 +10001805 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001806 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001807 OPT_INTEGER('C', "CPU", &profile_cpu,
1808 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001809 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1810 "dump raw trace in ASCII"),
1811 OPT_END()
1812};
1813
1814static const char * const replay_usage[] = {
1815 "perf sched replay [<options>]",
1816 NULL
1817};
1818
1819static const struct option replay_options[] = {
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001820 OPT_UINTEGER('r', "repeat", &replay_repeat,
1821 "repeat the workload replay N times (-1: infinite)"),
Ian Munsiec0555642010-04-13 18:37:33 +10001822 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001823 "be more verbose (show symbol address, etc)"),
1824 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1825 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001826 OPT_END()
1827};
1828
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001829static void setup_sorting(void)
1830{
1831 char *tmp, *tok, *str = strdup(sort_order);
1832
1833 for (tok = strtok_r(str, ", ", &tmp);
1834 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1835 if (sort_dimension__add(tok, &sort_list) < 0) {
1836 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001837 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001838 }
1839 }
1840
1841 free(str);
1842
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001843 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001844}
1845
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001846static const char *record_args[] = {
1847 "record",
1848 "-a",
1849 "-R",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001850 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001851 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001852 "-c", "1",
Stephane Eranian97101182011-01-12 10:29:05 +01001853 "-e", "sched:sched_switch",
1854 "-e", "sched:sched_stat_wait",
1855 "-e", "sched:sched_stat_sleep",
1856 "-e", "sched:sched_stat_iowait",
1857 "-e", "sched:sched_stat_runtime",
1858 "-e", "sched:sched_process_exit",
1859 "-e", "sched:sched_process_fork",
1860 "-e", "sched:sched_wakeup",
1861 "-e", "sched:sched_migrate_task",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001862};
1863
1864static int __cmd_record(int argc, const char **argv)
1865{
1866 unsigned int rec_argc, i, j;
1867 const char **rec_argv;
1868
1869 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1870 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1871
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001872 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001873 return -ENOMEM;
1874
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001875 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1876 rec_argv[i] = strdup(record_args[i]);
1877
1878 for (j = 1; j < (unsigned int)argc; j++, i++)
1879 rec_argv[i] = argv[j];
1880
1881 BUG_ON(i != rec_argc);
1882
1883 return cmd_record(i, rec_argv, NULL);
1884}
1885
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001886int cmd_sched(int argc, const char **argv, const char *prefix __used)
1887{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001888 argc = parse_options(argc, argv, sched_options, sched_usage,
1889 PARSE_OPT_STOP_AT_NON_OPTION);
1890 if (!argc)
1891 usage_with_options(sched_usage, sched_options);
1892
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001893 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001894 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001895 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001896 if (!strcmp(argv[0], "script"))
1897 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001898
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001899 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001900 if (!strncmp(argv[0], "rec", 3)) {
1901 return __cmd_record(argc, argv);
1902 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001903 trace_handler = &lat_ops;
1904 if (argc > 1) {
1905 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1906 if (argc)
1907 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001908 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001909 setup_sorting();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001910 __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001911 } else if (!strcmp(argv[0], "map")) {
1912 trace_handler = &map_ops;
1913 setup_sorting();
1914 __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001915 } else if (!strncmp(argv[0], "rep", 3)) {
1916 trace_handler = &replay_ops;
1917 if (argc) {
1918 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1919 if (argc)
1920 usage_with_options(replay_usage, replay_options);
1921 }
1922 __cmd_replay();
1923 } else {
1924 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001925 }
1926
Ingo Molnarec156762009-09-11 12:12:54 +02001927 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001928}