blob: 782f66d3610e2d60741e2573acc8a4dd209aabb2 [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)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300426 pr_debug("Error: sys_perf_event_open() syscall returned"
427 "with %d (%s)\n", fd, strerror(errno));
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800428 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();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300453 if (fd < 0)
454 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200455again:
456 ret = sem_post(&this_task->ready_for_work);
457 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200458 ret = pthread_mutex_lock(&start_work_mutex);
459 BUG_ON(ret);
460 ret = pthread_mutex_unlock(&start_work_mutex);
461 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200462
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800463 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200464
465 for (i = 0; i < this_task->nr_events; i++) {
466 this_task->curr_event = i;
mingo39aeb522009-09-14 20:04:48 +0200467 process_sched_event(this_task, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200468 }
469
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800470 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200471 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200472 ret = sem_post(&this_task->work_done_sem);
473 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200474
475 ret = pthread_mutex_lock(&work_done_wait_mutex);
476 BUG_ON(ret);
477 ret = pthread_mutex_unlock(&work_done_wait_mutex);
478 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200479
480 goto again;
481}
482
483static void create_tasks(void)
484{
485 struct task_desc *task;
486 pthread_attr_t attr;
487 unsigned long i;
488 int err;
489
490 err = pthread_attr_init(&attr);
491 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200492 err = pthread_attr_setstacksize(&attr,
493 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200494 BUG_ON(err);
495 err = pthread_mutex_lock(&start_work_mutex);
496 BUG_ON(err);
497 err = pthread_mutex_lock(&work_done_wait_mutex);
498 BUG_ON(err);
499 for (i = 0; i < nr_tasks; i++) {
500 task = tasks[i];
501 sem_init(&task->sleep_sem, 0, 0);
502 sem_init(&task->ready_for_work, 0, 0);
503 sem_init(&task->work_done_sem, 0, 0);
504 task->curr_event = 0;
505 err = pthread_create(&task->thread, &attr, thread_func, task);
506 BUG_ON(err);
507 }
508}
509
Ingo Molnarec156762009-09-11 12:12:54 +0200510static void wait_for_tasks(void)
511{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200512 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200513 struct task_desc *task;
514 unsigned long i, ret;
515
Ingo Molnarec156762009-09-11 12:12:54 +0200516 start_time = get_nsecs();
Ingo Molnarec156762009-09-11 12:12:54 +0200517 cpu_usage = 0;
518 pthread_mutex_unlock(&work_done_wait_mutex);
519
520 for (i = 0; i < nr_tasks; i++) {
521 task = tasks[i];
522 ret = sem_wait(&task->ready_for_work);
523 BUG_ON(ret);
524 sem_init(&task->ready_for_work, 0, 0);
525 }
526 ret = pthread_mutex_lock(&work_done_wait_mutex);
527 BUG_ON(ret);
528
529 cpu_usage_0 = get_cpu_usage_nsec_parent();
530
531 pthread_mutex_unlock(&start_work_mutex);
532
Ingo Molnarec156762009-09-11 12:12:54 +0200533 for (i = 0; i < nr_tasks; i++) {
534 task = tasks[i];
535 ret = sem_wait(&task->work_done_sem);
536 BUG_ON(ret);
537 sem_init(&task->work_done_sem, 0, 0);
538 cpu_usage += task->cpu_usage;
539 task->cpu_usage = 0;
540 }
541
542 cpu_usage_1 = get_cpu_usage_nsec_parent();
543 if (!runavg_cpu_usage)
544 runavg_cpu_usage = cpu_usage;
545 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
546
547 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
548 if (!runavg_parent_cpu_usage)
549 runavg_parent_cpu_usage = parent_cpu_usage;
550 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
551 parent_cpu_usage)/10;
552
553 ret = pthread_mutex_lock(&start_work_mutex);
554 BUG_ON(ret);
555
556 for (i = 0; i < nr_tasks; i++) {
557 task = tasks[i];
558 sem_init(&task->sleep_sem, 0, 0);
559 task->curr_event = 0;
560 }
561}
562
Ingo Molnarec156762009-09-11 12:12:54 +0200563static void run_one_test(void)
564{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500565 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200566
567 T0 = get_nsecs();
568 wait_for_tasks();
569 T1 = get_nsecs();
570
571 delta = T1 - T0;
572 sum_runtime += delta;
573 nr_runs++;
574
575 avg_delta = sum_runtime / nr_runs;
576 if (delta < avg_delta)
577 fluct = avg_delta - delta;
578 else
579 fluct = delta - avg_delta;
580 sum_fluct += fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200581 if (!run_avg)
582 run_avg = delta;
583 run_avg = (run_avg*9 + delta)/10;
584
Ingo Molnarad236fd2009-09-11 12:12:54 +0200585 printf("#%-3ld: %0.3f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200586 nr_runs, (double)delta/1000000.0);
587
Ingo Molnarad236fd2009-09-11 12:12:54 +0200588 printf("ravg: %0.2f, ",
Ingo Molnarec156762009-09-11 12:12:54 +0200589 (double)run_avg/1e6);
590
Ingo Molnarad236fd2009-09-11 12:12:54 +0200591 printf("cpu: %0.2f / %0.2f",
Ingo Molnarec156762009-09-11 12:12:54 +0200592 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
593
594#if 0
595 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200596 * rusage statistics done by the parent, these are less
597 * accurate than the sum_exec_runtime based statistics:
598 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200599 printf(" [%0.2f / %0.2f]",
Ingo Molnarec156762009-09-11 12:12:54 +0200600 (double)parent_cpu_usage/1e6,
601 (double)runavg_parent_cpu_usage/1e6);
602#endif
603
Ingo Molnarad236fd2009-09-11 12:12:54 +0200604 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200605
606 if (nr_sleep_corrections)
Ingo Molnarad236fd2009-09-11 12:12:54 +0200607 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
Ingo Molnarec156762009-09-11 12:12:54 +0200608 nr_sleep_corrections = 0;
609}
610
611static void test_calibrations(void)
612{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200613 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200614
615 T0 = get_nsecs();
616 burn_nsecs(1e6);
617 T1 = get_nsecs();
618
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200619 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200620
621 T0 = get_nsecs();
622 sleep_nsecs(1e6);
623 T1 = get_nsecs();
624
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200625 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200626}
627
Frederic Weisbecker46538812009-09-12 02:43:45 +0200628#define FILL_FIELD(ptr, field, event, data) \
629 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
630
631#define FILL_ARRAY(ptr, array, event, data) \
632do { \
633 void *__array = raw_field_ptr(event, #array, data); \
634 memcpy(ptr.array, __array, sizeof(ptr.array)); \
635} while(0)
636
637#define FILL_COMMON_FIELDS(ptr, event, data) \
638do { \
639 FILL_FIELD(ptr, common_type, event, data); \
640 FILL_FIELD(ptr, common_flags, event, data); \
641 FILL_FIELD(ptr, common_preempt_count, event, data); \
642 FILL_FIELD(ptr, common_pid, event, data); \
643 FILL_FIELD(ptr, common_tgid, event, data); \
644} while (0)
645
Ingo Molnarfbf94822009-09-11 12:12:54 +0200646
Ingo Molnarec156762009-09-11 12:12:54 +0200647
Ingo Molnarfbf94822009-09-11 12:12:54 +0200648struct trace_switch_event {
649 u32 size;
650
651 u16 common_type;
652 u8 common_flags;
653 u8 common_preempt_count;
654 u32 common_pid;
655 u32 common_tgid;
656
657 char prev_comm[16];
658 u32 prev_pid;
659 u32 prev_prio;
660 u64 prev_state;
661 char next_comm[16];
662 u32 next_pid;
663 u32 next_prio;
664};
665
mingo39aeb522009-09-14 20:04:48 +0200666struct trace_runtime_event {
667 u32 size;
668
669 u16 common_type;
670 u8 common_flags;
671 u8 common_preempt_count;
672 u32 common_pid;
673 u32 common_tgid;
674
675 char comm[16];
676 u32 pid;
677 u64 runtime;
678 u64 vruntime;
679};
Ingo Molnarfbf94822009-09-11 12:12:54 +0200680
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200681struct trace_wakeup_event {
682 u32 size;
683
684 u16 common_type;
685 u8 common_flags;
686 u8 common_preempt_count;
687 u32 common_pid;
688 u32 common_tgid;
689
690 char comm[16];
691 u32 pid;
692
693 u32 prio;
694 u32 success;
695 u32 cpu;
696};
697
698struct trace_fork_event {
699 u32 size;
700
701 u16 common_type;
702 u8 common_flags;
703 u8 common_preempt_count;
704 u32 common_pid;
705 u32 common_tgid;
706
707 char parent_comm[16];
708 u32 parent_pid;
709 char child_comm[16];
710 u32 child_pid;
711};
712
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200713struct trace_migrate_task_event {
714 u32 size;
715
716 u16 common_type;
717 u8 common_flags;
718 u8 common_preempt_count;
719 u32 common_pid;
720 u32 common_tgid;
721
722 char comm[16];
723 u32 pid;
724
725 u32 prio;
726 u32 cpu;
727};
728
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200729struct trace_sched_handler {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300730 int (*switch_event)(struct trace_switch_event *event,
731 struct machine *machine,
732 struct event_format *tp_format,
733 struct perf_sample *sample);
734
735 int (*runtime_event)(struct trace_runtime_event *event,
736 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300737 struct perf_sample *sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200738
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300739 int (*wakeup_event)(struct trace_wakeup_event *event,
740 struct machine *machine,
741 struct event_format *tp_format,
742 struct perf_sample *sample);
mingo39aeb522009-09-14 20:04:48 +0200743
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300744 int (*fork_event)(struct trace_fork_event *event,
745 struct event_format *tp_format);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200746
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300747 int (*migrate_task_event)(struct trace_migrate_task_event *event,
748 struct machine *machine,
749 struct perf_sample *sample);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200750};
751
Ingo Molnarfbf94822009-09-11 12:12:54 +0200752
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300753static int
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200754replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200755 struct machine *machine __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300756 struct event_format *event, struct perf_sample *sample)
Ingo Molnarec156762009-09-11 12:12:54 +0200757{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200758 struct task_desc *waker, *wakee;
759
760 if (verbose) {
761 printf("sched_wakeup event %p\n", event);
762
763 printf(" ... pid %d woke up %s/%d\n",
764 wakeup_event->common_pid,
765 wakeup_event->comm,
766 wakeup_event->pid);
767 }
768
769 waker = register_pid(wakeup_event->common_pid, "<unknown>");
770 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
771
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300772 add_sched_event_wakeup(waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300773 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200774}
775
Ingo Molnard1153382009-09-14 18:22:53 +0200776static u64 cpu_last_switched[MAX_CPUS];
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200777
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300778static int
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200779replay_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200780 struct machine *machine __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200781 struct event_format *event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300782 struct perf_sample *sample)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200783{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500784 struct task_desc *prev, __used *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300785 u64 timestamp0, timestamp = sample->time;
786 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200787 s64 delta;
788
Ingo Molnarad236fd2009-09-11 12:12:54 +0200789 if (verbose)
790 printf("sched_switch event %p\n", event);
791
Ingo Molnarfbf94822009-09-11 12:12:54 +0200792 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300793 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200794
795 timestamp0 = cpu_last_switched[cpu];
796 if (timestamp0)
797 delta = timestamp - timestamp0;
798 else
799 delta = 0;
800
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300801 if (delta < 0) {
802 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
803 return -1;
804 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200805
Ingo Molnarad236fd2009-09-11 12:12:54 +0200806 if (verbose) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200807 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200808 switch_event->prev_comm, switch_event->prev_pid,
809 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200810 delta);
811 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200812
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200813 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
814 next = register_pid(switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200815
816 cpu_last_switched[cpu] = timestamp;
817
818 add_sched_event_run(prev, timestamp, delta);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200819 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300820
821 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200822}
823
Ingo Molnarfbf94822009-09-11 12:12:54 +0200824
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300825static int
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200826replay_fork_event(struct trace_fork_event *fork_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300827 struct event_format *event)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200828{
829 if (verbose) {
830 printf("sched_fork event %p\n", event);
831 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
832 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
833 }
834 register_pid(fork_event->parent_pid, fork_event->parent_comm);
835 register_pid(fork_event->child_pid, fork_event->child_comm);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300836 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200837}
838
839static struct trace_sched_handler replay_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +0200840 .wakeup_event = replay_wakeup_event,
841 .switch_event = replay_switch_event,
842 .fork_event = replay_fork_event,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200843};
844
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200845struct sort_dimension {
846 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200847 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200848 struct list_head list;
849};
850
851static LIST_HEAD(cmp_pid);
852
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200853static int
mingo39aeb522009-09-14 20:04:48 +0200854thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200855{
856 struct sort_dimension *sort;
857 int ret = 0;
858
Ingo Molnarb5fae122009-09-11 12:12:54 +0200859 BUG_ON(list_empty(list));
860
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200861 list_for_each_entry(sort, list, list) {
862 ret = sort->cmp(l, r);
863 if (ret)
864 return ret;
865 }
866
867 return ret;
868}
869
mingo39aeb522009-09-14 20:04:48 +0200870static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200871thread_atoms_search(struct rb_root *root, struct thread *thread,
872 struct list_head *sort_list)
873{
874 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200875 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200876
877 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200878 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200879 int cmp;
880
mingo39aeb522009-09-14 20:04:48 +0200881 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200882
883 cmp = thread_lat_cmp(sort_list, &key, atoms);
884 if (cmp > 0)
885 node = node->rb_left;
886 else if (cmp < 0)
887 node = node->rb_right;
888 else {
889 BUG_ON(thread != atoms->thread);
890 return atoms;
891 }
892 }
893 return NULL;
894}
895
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200896static void
mingo39aeb522009-09-14 20:04:48 +0200897__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200898 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200899{
900 struct rb_node **new = &(root->rb_node), *parent = NULL;
901
902 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200903 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200904 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905
mingo39aeb522009-09-14 20:04:48 +0200906 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200908
909 cmp = thread_lat_cmp(sort_list, data, this);
910
911 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200912 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200913 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200914 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915 }
916
917 rb_link_node(&data->node, parent, new);
918 rb_insert_color(&data->node, root);
919}
920
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300921static int thread_atoms_insert(struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200922{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200923 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300924 if (!atoms) {
925 pr_err("No memory at %s\n", __func__);
926 return -1;
927 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200928
Frederic Weisbecker17562202009-09-12 23:11:32 +0200929 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200930 INIT_LIST_HEAD(&atoms->work_list);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200931 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300932 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933}
934
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300935static int
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200936latency_fork_event(struct trace_fork_event *fork_event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300937 struct event_format *event __used)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938{
939 /* should insert the newcomer */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300940 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941}
942
Ingo Molnarea92ed52009-09-12 10:08:34 +0200943__used
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200944static char sched_out_state(struct trace_switch_event *switch_event)
945{
946 const char *str = TASK_STATE_TO_CHAR_STR;
947
948 return str[switch_event->prev_state];
949}
950
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300951static int
mingo39aeb522009-09-14 20:04:48 +0200952add_sched_out_event(struct work_atoms *atoms,
953 char run_state,
954 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200955{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200956 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300957 if (!atom) {
958 pr_err("Non memory at %s", __func__);
959 return -1;
960 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200961
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200962 atom->sched_out_time = timestamp;
963
mingo39aeb522009-09-14 20:04:48 +0200964 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200965 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200966 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200967 }
968
mingo39aeb522009-09-14 20:04:48 +0200969 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300970 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200971}
972
973static void
mingo39aeb522009-09-14 20:04:48 +0200974add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
975{
976 struct work_atom *atom;
977
978 BUG_ON(list_empty(&atoms->work_list));
979
980 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
981
982 atom->runtime += delta;
983 atoms->total_runtime += delta;
984}
985
986static void
987add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200989 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200990 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200991
mingo39aeb522009-09-14 20:04:48 +0200992 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200993 return;
994
mingo39aeb522009-09-14 20:04:48 +0200995 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200996
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200997 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200998 return;
999
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001000 if (timestamp < atom->wake_up_time) {
1001 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001002 return;
1003 }
1004
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001005 atom->state = THREAD_SCHED_IN;
1006 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001007
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001008 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001009 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001010 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001011 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001012 atoms->max_lat_at = timestamp;
1013 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001014 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001015}
1016
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001017static int
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001018latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001019 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001020 struct event_format *event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001021 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001022{
mingo39aeb522009-09-14 20:04:48 +02001023 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001025 u64 timestamp0, timestamp = sample->time;
1026 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001027 s64 delta;
1028
mingo39aeb522009-09-14 20:04:48 +02001029 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001030
1031 timestamp0 = cpu_last_switched[cpu];
1032 cpu_last_switched[cpu] = timestamp;
1033 if (timestamp0)
1034 delta = timestamp - timestamp0;
1035 else
1036 delta = 0;
1037
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001038 if (delta < 0) {
1039 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1040 return -1;
1041 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001042
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001043 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1044 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001045
mingo39aeb522009-09-14 20:04:48 +02001046 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1047 if (!out_events) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001048 if (thread_atoms_insert(sched_out))
1049 return -1;
mingo39aeb522009-09-14 20:04:48 +02001050 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001051 if (!out_events) {
1052 pr_err("out-event: Internal tree error");
1053 return -1;
1054 }
mingo39aeb522009-09-14 20:04:48 +02001055 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001056 if (add_sched_out_event(out_events, sched_out_state(switch_event), timestamp))
1057 return -1;
mingo39aeb522009-09-14 20:04:48 +02001058
1059 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1060 if (!in_events) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001061 if (thread_atoms_insert(sched_in))
1062 return -1;
mingo39aeb522009-09-14 20:04:48 +02001063 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001064 if (!in_events) {
1065 pr_err("in-event: Internal tree error");
1066 return -1;
1067 }
mingo39aeb522009-09-14 20:04:48 +02001068 /*
1069 * Take came in we have not heard about yet,
1070 * add in an initial atom in runnable state:
1071 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001072 if (add_sched_out_event(in_events, 'R', timestamp))
1073 return -1;
mingo39aeb522009-09-14 20:04:48 +02001074 }
1075 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001076
1077 return 0;
mingo39aeb522009-09-14 20:04:48 +02001078}
1079
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001080static int
mingo39aeb522009-09-14 20:04:48 +02001081latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001082 struct machine *machine, struct perf_sample *sample)
mingo39aeb522009-09-14 20:04:48 +02001083{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001084 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001085 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001086 u64 timestamp = sample->time;
1087 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +02001088
1089 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001090 if (!atoms) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001091 if (thread_atoms_insert(thread))
1092 return -1;
mingo39aeb522009-09-14 20:04:48 +02001093 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001094 if (!atoms) {
1095 pr_debug("in-event: Internal tree error");
1096 return -1;
1097 }
1098 if (add_sched_out_event(atoms, 'R', timestamp))
1099 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001100 }
1101
mingo39aeb522009-09-14 20:04:48 +02001102 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001103 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001104}
1105
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001106static int
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001107latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001108 struct machine *machine, struct event_format *event __used,
1109 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001110{
mingo39aeb522009-09-14 20:04:48 +02001111 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001112 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001113 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001114 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001115
1116 /* Note for later, it may be interesting to observe the failing cases */
1117 if (!wakeup_event->success)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001118 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001119
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001120 wakee = machine__findnew_thread(machine, wakeup_event->pid);
Ingo Molnarb5fae122009-09-11 12:12:54 +02001121 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001122 if (!atoms) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001123 if (thread_atoms_insert(wakee))
1124 return -1;
mingo39aeb522009-09-14 20:04:48 +02001125 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001126 if (!atoms) {
1127 pr_debug("wakeup-event: Internal tree error");
1128 return -1;
1129 }
1130 if (add_sched_out_event(atoms, 'S', timestamp))
1131 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132 }
1133
mingo39aeb522009-09-14 20:04:48 +02001134 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001135
mingo39aeb522009-09-14 20:04:48 +02001136 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001138 /*
1139 * You WILL be missing events if you've recorded only
1140 * one CPU, or are only looking at only one, so don't
1141 * make useless noise.
1142 */
1143 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Ingo Molnardc02bf72009-09-16 13:45:00 +02001144 nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001145
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001146 nr_timestamps++;
1147 if (atom->sched_out_time > timestamp) {
Ingo Molnardc02bf72009-09-16 13:45:00 +02001148 nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001149 return 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001150 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001151
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001152 atom->state = THREAD_WAIT_CPU;
1153 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001154 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001155}
1156
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001157static int
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001158latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001159 struct machine *machine, struct perf_sample *sample)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001160{
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001161 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001162 struct work_atoms *atoms;
1163 struct work_atom *atom;
1164 struct thread *migrant;
1165
1166 /*
1167 * Only need to worry about migration when profiling one CPU.
1168 */
1169 if (profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001170 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001171
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001172 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001173 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1174 if (!atoms) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001175 if (thread_atoms_insert(migrant))
1176 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001177 register_pid(migrant->pid, migrant->comm);
1178 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001179 if (!atoms) {
1180 pr_debug("migration-event: Internal tree error");
1181 return -1;
1182 }
1183 if (add_sched_out_event(atoms, 'R', timestamp))
1184 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001185 }
1186
1187 BUG_ON(list_empty(&atoms->work_list));
1188
1189 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1190 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1191
1192 nr_timestamps++;
1193
1194 if (atom->sched_out_time > timestamp)
1195 nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001196
1197 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001198}
1199
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001200static struct trace_sched_handler lat_ops = {
Ingo Molnarea92ed52009-09-12 10:08:34 +02001201 .wakeup_event = latency_wakeup_event,
1202 .switch_event = latency_switch_event,
mingo39aeb522009-09-14 20:04:48 +02001203 .runtime_event = latency_runtime_event,
Ingo Molnarea92ed52009-09-12 10:08:34 +02001204 .fork_event = latency_fork_event,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001205 .migrate_task_event = latency_migrate_task_event,
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001206};
1207
mingo39aeb522009-09-14 20:04:48 +02001208static void output_lat_thread(struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001210 int i;
1211 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001212 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001213
mingo39aeb522009-09-14 20:04:48 +02001214 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001215 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001216 /*
1217 * Ignore idle threads:
1218 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001219 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001220 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001221
mingo39aeb522009-09-14 20:04:48 +02001222 all_runtime += work_list->total_runtime;
1223 all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001224
Ingo Molnar80ed0982009-09-16 14:12:36 +02001225 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001226
mingo08f69e62009-09-14 18:30:44 +02001227 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001228 printf(" ");
1229
mingo39aeb522009-09-14 20:04:48 +02001230 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001231
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001232 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 +02001233 (double)work_list->total_runtime / 1e6,
1234 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001235 (double)work_list->max_lat / 1e6,
1236 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001237}
1238
mingo39aeb522009-09-14 20:04:48 +02001239static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001240{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001241 if (l->thread->pid < r->thread->pid)
1242 return -1;
1243 if (l->thread->pid > r->thread->pid)
1244 return 1;
1245
1246 return 0;
1247}
1248
1249static struct sort_dimension pid_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001250 .name = "pid",
1251 .cmp = pid_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001252};
1253
mingo39aeb522009-09-14 20:04:48 +02001254static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001255{
1256 u64 avgl, avgr;
1257
1258 if (!l->nb_atoms)
1259 return -1;
1260
1261 if (!r->nb_atoms)
1262 return 1;
1263
1264 avgl = l->total_lat / l->nb_atoms;
1265 avgr = r->total_lat / r->nb_atoms;
1266
1267 if (avgl < avgr)
1268 return -1;
1269 if (avgl > avgr)
1270 return 1;
1271
1272 return 0;
1273}
1274
1275static struct sort_dimension avg_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001276 .name = "avg",
1277 .cmp = avg_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001278};
1279
mingo39aeb522009-09-14 20:04:48 +02001280static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001281{
1282 if (l->max_lat < r->max_lat)
1283 return -1;
1284 if (l->max_lat > r->max_lat)
1285 return 1;
1286
1287 return 0;
1288}
1289
1290static struct sort_dimension max_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001291 .name = "max",
1292 .cmp = max_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001293};
1294
mingo39aeb522009-09-14 20:04:48 +02001295static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001296{
1297 if (l->nb_atoms < r->nb_atoms)
1298 return -1;
1299 if (l->nb_atoms > r->nb_atoms)
1300 return 1;
1301
1302 return 0;
1303}
1304
1305static struct sort_dimension switch_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001306 .name = "switch",
1307 .cmp = switch_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001308};
1309
mingo39aeb522009-09-14 20:04:48 +02001310static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001311{
1312 if (l->total_runtime < r->total_runtime)
1313 return -1;
1314 if (l->total_runtime > r->total_runtime)
1315 return 1;
1316
1317 return 0;
1318}
1319
1320static struct sort_dimension runtime_sort_dimension = {
Ingo Molnarb5fae122009-09-11 12:12:54 +02001321 .name = "runtime",
1322 .cmp = runtime_cmp,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001323};
1324
1325static struct sort_dimension *available_sorts[] = {
1326 &pid_sort_dimension,
1327 &avg_sort_dimension,
1328 &max_sort_dimension,
1329 &switch_sort_dimension,
1330 &runtime_sort_dimension,
1331};
1332
1333#define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1334
1335static LIST_HEAD(sort_list);
1336
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001337static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001338{
1339 int i;
1340
1341 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1342 if (!strcmp(available_sorts[i]->name, tok)) {
1343 list_add_tail(&available_sorts[i]->list, list);
1344
1345 return 0;
1346 }
1347 }
1348
1349 return -1;
1350}
1351
1352static void setup_sorting(void);
1353
1354static void sort_lat(void)
1355{
1356 struct rb_node *node;
1357
1358 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001359 struct work_atoms *data;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001360 node = rb_first(&atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001361 if (!node)
1362 break;
1363
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001364 rb_erase(node, &atom_root);
mingo39aeb522009-09-14 20:04:48 +02001365 data = rb_entry(node, struct work_atoms, node);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001366 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001367 }
1368}
1369
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001370static struct trace_sched_handler *trace_handler;
1371
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001372static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001373process_sched_wakeup_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001374 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001375 struct perf_sample *sample,
1376 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001377 struct thread *thread __used)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001378{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001379 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001380 struct trace_wakeup_event wakeup_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001381 int err = 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001382
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001383 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001384
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001385 FILL_ARRAY(wakeup_event, comm, event, data);
1386 FILL_FIELD(wakeup_event, pid, event, data);
1387 FILL_FIELD(wakeup_event, prio, event, data);
1388 FILL_FIELD(wakeup_event, success, event, data);
1389 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001390
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001391 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001392 err = trace_handler->wakeup_event(&wakeup_event, machine, event, sample);
1393
1394 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001395}
1396
Ingo Molnarc8a37752009-09-16 14:07:00 +02001397/*
1398 * Track the current task - that way we can know whether there's any
1399 * weird events, such as a task being switched away that is not current.
1400 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001401static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001402
Ingo Molnarc8a37752009-09-16 14:07:00 +02001403static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1404
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001405static struct thread *curr_thread[MAX_CPUS];
1406
1407static char next_shortname1 = 'A';
1408static char next_shortname2 = '0';
1409
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001410static int
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001411map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001412 struct machine *machine,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001413 struct event_format *event __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001414 struct perf_sample *sample)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001415{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05001416 struct thread *sched_out __used, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001417 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001418 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001419 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001420 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001421
1422 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1423
1424 if (this_cpu > max_cpu)
1425 max_cpu = this_cpu;
1426
1427 timestamp0 = cpu_last_switched[this_cpu];
1428 cpu_last_switched[this_cpu] = timestamp;
1429 if (timestamp0)
1430 delta = timestamp - timestamp0;
1431 else
1432 delta = 0;
1433
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001434 if (delta < 0) {
1435 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1436 return -1;
1437 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001438
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001439 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1440 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001441
1442 curr_thread[this_cpu] = sched_in;
1443
1444 printf(" ");
1445
1446 new_shortname = 0;
1447 if (!sched_in->shortname[0]) {
1448 sched_in->shortname[0] = next_shortname1;
1449 sched_in->shortname[1] = next_shortname2;
1450
1451 if (next_shortname1 < 'Z') {
1452 next_shortname1++;
1453 } else {
1454 next_shortname1='A';
1455 if (next_shortname2 < '9') {
1456 next_shortname2++;
1457 } else {
1458 next_shortname2='0';
1459 }
1460 }
1461 new_shortname = 1;
1462 }
1463
1464 for (cpu = 0; cpu <= max_cpu; cpu++) {
1465 if (cpu != this_cpu)
1466 printf(" ");
1467 else
1468 printf("*");
1469
1470 if (curr_thread[cpu]) {
1471 if (curr_thread[cpu]->pid)
1472 printf("%2s ", curr_thread[cpu]->shortname);
1473 else
1474 printf(". ");
1475 } else
1476 printf(" ");
1477 }
1478
1479 printf(" %12.6f secs ", (double)timestamp/1e9);
1480 if (new_shortname) {
1481 printf("%s => %s:%d\n",
1482 sched_in->shortname, sched_in->comm, sched_in->pid);
1483 } else {
1484 printf("\n");
1485 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001486
1487 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001488}
1489
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001490static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001491process_sched_switch_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001492 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001493 struct perf_sample *sample,
1494 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001495 struct thread *thread __used)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001496{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001497 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001498 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001499 struct trace_switch_event switch_event;
1500
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001501 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001502
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001503 FILL_ARRAY(switch_event, prev_comm, event, data);
1504 FILL_FIELD(switch_event, prev_pid, event, data);
1505 FILL_FIELD(switch_event, prev_prio, event, data);
1506 FILL_FIELD(switch_event, prev_state, event, data);
1507 FILL_ARRAY(switch_event, next_comm, event, data);
1508 FILL_FIELD(switch_event, next_pid, event, data);
1509 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001510
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001511 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001512 /*
1513 * Are we trying to switch away a PID that is
1514 * not current?
1515 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001516 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001517 nr_context_switch_bugs++;
1518 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001519 if (trace_handler->switch_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001520 err = trace_handler->switch_event(&switch_event, machine, event, sample);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001521
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001522 curr_pid[this_cpu] = switch_event.next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001523 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001524}
1525
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001526static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001527process_sched_runtime_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001528 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001529 struct perf_sample *sample,
1530 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001531 struct thread *thread __used)
mingo39aeb522009-09-14 20:04:48 +02001532{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001533 void *data = sample->raw_data;
mingo39aeb522009-09-14 20:04:48 +02001534 struct trace_runtime_event runtime_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001535 int err = 0;
mingo39aeb522009-09-14 20:04:48 +02001536
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001537 FILL_ARRAY(runtime_event, comm, event, data);
1538 FILL_FIELD(runtime_event, pid, event, data);
1539 FILL_FIELD(runtime_event, runtime, event, data);
1540 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001541
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001542 if (trace_handler->runtime_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001543 err = trace_handler->runtime_event(&runtime_event, machine, sample);
1544
1545 return err;
mingo39aeb522009-09-14 20:04:48 +02001546}
1547
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001548static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001549process_sched_fork_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001550 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001551 struct perf_sample *sample,
1552 struct machine *machine __used,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001553 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001554{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001555 void *data = sample->raw_data;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001556 struct trace_fork_event fork_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001557 int err = 0;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001558
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001559 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001560
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001561 FILL_ARRAY(fork_event, parent_comm, event, data);
1562 FILL_FIELD(fork_event, parent_pid, event, data);
1563 FILL_ARRAY(fork_event, child_comm, event, data);
1564 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001565
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001566 if (trace_handler->fork_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001567 err = trace_handler->fork_event(&fork_event, event);
1568
1569 return err;
Ingo Molnarfbf94822009-09-11 12:12:54 +02001570}
1571
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001572static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001573process_sched_exit_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001574 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001575 struct perf_sample *sample __used,
1576 struct machine *machine __used,
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001577 struct thread *thread __used)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001578{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001579 if (verbose)
1580 printf("sched_exit event %p\n", event);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001581
1582 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001583}
1584
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001585static int
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001586process_sched_migrate_task_event(struct perf_tool *tool __used,
Steven Rostedtaaf045f2012-04-06 00:47:56 +02001587 struct event_format *event,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001588 struct perf_sample *sample,
1589 struct machine *machine,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001590 struct thread *thread __used)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001591{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001592 void *data = sample->raw_data;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001593 struct trace_migrate_task_event migrate_task_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001594 int err = 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001595
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001596 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001597
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001598 FILL_ARRAY(migrate_task_event, comm, event, data);
1599 FILL_FIELD(migrate_task_event, pid, event, data);
1600 FILL_FIELD(migrate_task_event, prio, event, data);
1601 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001602
1603 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001604 err = trace_handler->migrate_task_event(&migrate_task_event, machine, sample);
1605
1606 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001607}
1608
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001609typedef int (*tracepoint_handler)(struct perf_tool *tool,
1610 struct event_format *tp_format,
1611 struct perf_sample *sample,
1612 struct machine *machine,
1613 struct thread *thread);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001614
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001615static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __used,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001616 union perf_event *event __used,
1617 struct perf_sample *sample,
1618 struct perf_evsel *evsel,
1619 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001620{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001621 struct thread *thread = machine__findnew_thread(machine, sample->pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001622 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001623
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001624 if (thread == NULL) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001625 pr_debug("problem processing %s event, skipping it.\n",
Arnaldo Carvalho de Melo22c8b842012-06-12 13:55:13 -03001626 perf_evsel__name(evsel));
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001627 return -1;
1628 }
1629
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001630 evsel->hists.stats.total_period += sample->period;
1631 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
Julia Lawallf39cdf22009-10-17 08:43:17 +02001632
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001633 if (evsel->handler.func != NULL) {
1634 tracepoint_handler f = evsel->handler.func;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001635 err = f(tool, evsel->tp_format, sample, machine, thread);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001636 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001637
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001638 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001639}
1640
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001641static struct perf_tool perf_sched = {
1642 .sample = perf_sched__process_tracepoint_sample,
1643 .comm = perf_event__process_comm,
1644 .lost = perf_event__process_lost,
1645 .fork = perf_event__process_task,
1646 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001647};
1648
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001649static int read_events(bool destroy, struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001650{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001651 const struct perf_evsel_str_handler handlers[] = {
1652 { "sched:sched_switch", process_sched_switch_event, },
1653 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1654 { "sched:sched_wakeup", process_sched_wakeup_event, },
1655 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1656 { "sched:sched_process_fork", process_sched_fork_event, },
1657 { "sched:sched_process_exit", process_sched_exit_event, },
1658 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1659 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001660 struct perf_session *session;
1661
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001662 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001663 if (session == NULL) {
1664 pr_debug("No Memory for session\n");
1665 return -1;
1666 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001667
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001668 if (perf_session__set_tracepoints_handlers(session, handlers))
1669 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001670
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001671 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001672 int err = perf_session__process_events(session, &perf_sched);
1673 if (err) {
1674 pr_err("Failed to process events, error %d", err);
1675 goto out_delete;
1676 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001677
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001678 nr_events = session->hists.stats.nr_events[0];
1679 nr_lost_events = session->hists.stats.total_lost;
1680 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1681 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001682
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001683 if (destroy)
1684 perf_session__delete(session);
1685
1686 if (psession)
1687 *psession = session;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001688
1689 return 0;
1690
1691out_delete:
1692 perf_session__delete(session);
1693 return -1;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001694}
1695
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001696static void print_bad_events(void)
1697{
1698 if (nr_unordered_timestamps && nr_timestamps) {
1699 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1700 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1701 nr_unordered_timestamps, nr_timestamps);
1702 }
1703 if (nr_lost_events && nr_events) {
1704 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1705 (double)nr_lost_events/(double)nr_events*100.0,
1706 nr_lost_events, nr_events, nr_lost_chunks);
1707 }
1708 if (nr_state_machine_bugs && nr_timestamps) {
1709 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1710 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1711 nr_state_machine_bugs, nr_timestamps);
1712 if (nr_lost_events)
1713 printf(" (due to lost events?)");
1714 printf("\n");
1715 }
1716 if (nr_context_switch_bugs && nr_timestamps) {
1717 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1718 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1719 nr_context_switch_bugs, nr_timestamps);
1720 if (nr_lost_events)
1721 printf(" (due to lost events?)");
1722 printf("\n");
1723 }
1724}
1725
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001726static int __cmd_lat(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001727{
1728 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001729 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001730
1731 setup_pager();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001732 if (read_events(false, &session))
1733 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001734 sort_lat();
1735
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001736 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1737 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1738 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001739
1740 next = rb_first(&sorted_atom_root);
1741
1742 while (next) {
1743 struct work_atoms *work_list;
1744
1745 work_list = rb_entry(next, struct work_atoms, node);
1746 output_lat_thread(work_list);
1747 next = rb_next(next);
1748 }
1749
1750 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001751 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001752 (double)all_runtime/1e6, all_count);
1753
1754 printf(" ---------------------------------------------------\n");
1755
1756 print_bad_events();
1757 printf("\n");
1758
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001759 perf_session__delete(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001760 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001761}
1762
1763static struct trace_sched_handler map_ops = {
1764 .wakeup_event = NULL,
1765 .switch_event = map_switch_event,
1766 .runtime_event = NULL,
1767 .fork_event = NULL,
1768};
1769
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001770static int __cmd_map(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001771{
Ingo Molnar40749d02009-09-17 18:24:55 +02001772 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1773
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001774 setup_pager();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001775 if (read_events(true, NULL))
1776 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001777 print_bad_events();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001778 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001779}
1780
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001781static int __cmd_replay(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001782{
1783 unsigned long i;
1784
1785 calibrate_run_measurement_overhead();
1786 calibrate_sleep_measurement_overhead();
1787
1788 test_calibrations();
1789
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001790 if (read_events(true, NULL))
1791 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001792
1793 printf("nr_run_events: %ld\n", nr_run_events);
1794 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1795 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1796
1797 if (targetless_wakeups)
1798 printf("target-less wakeups: %ld\n", targetless_wakeups);
1799 if (multitarget_wakeups)
1800 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1801 if (nr_run_events_optimized)
1802 printf("run atoms optimized: %ld\n",
1803 nr_run_events_optimized);
1804
1805 print_task_traces();
1806 add_cross_task_wakeups();
1807
1808 create_tasks();
1809 printf("------------------------------------------------------------\n");
1810 for (i = 0; i < replay_repeat; i++)
1811 run_one_test();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001812
1813 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001814}
1815
1816
Ingo Molnar46f392c2009-09-12 10:08:34 +02001817static const char * const sched_usage[] = {
Jiri Olsa580cabe2011-08-09 14:46:51 +02001818 "perf sched [<options>] {record|latency|map|replay|script}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001819 NULL
1820};
1821
Ingo Molnarf2858d82009-09-11 12:12:54 +02001822static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001823 OPT_STRING('i', "input", &input_name, "file",
1824 "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001825 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001826 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001827 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1828 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001829 OPT_END()
1830};
1831
1832static const char * const latency_usage[] = {
1833 "perf sched latency [<options>]",
1834 NULL
1835};
1836
1837static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001838 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1839 "sort by key(s): runtime, switch, avg, max"),
Ian Munsiec0555642010-04-13 18:37:33 +10001840 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001841 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001842 OPT_INTEGER('C', "CPU", &profile_cpu,
1843 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001844 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1845 "dump raw trace in ASCII"),
1846 OPT_END()
1847};
1848
1849static const char * const replay_usage[] = {
1850 "perf sched replay [<options>]",
1851 NULL
1852};
1853
1854static const struct option replay_options[] = {
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001855 OPT_UINTEGER('r', "repeat", &replay_repeat,
1856 "repeat the workload replay N times (-1: infinite)"),
Ian Munsiec0555642010-04-13 18:37:33 +10001857 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001858 "be more verbose (show symbol address, etc)"),
1859 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1860 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001861 OPT_END()
1862};
1863
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001864static void setup_sorting(void)
1865{
1866 char *tmp, *tok, *str = strdup(sort_order);
1867
1868 for (tok = strtok_r(str, ", ", &tmp);
1869 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1870 if (sort_dimension__add(tok, &sort_list) < 0) {
1871 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001872 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001873 }
1874 }
1875
1876 free(str);
1877
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001878 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001879}
1880
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001881static const char *record_args[] = {
1882 "record",
1883 "-a",
1884 "-R",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001885 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001886 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001887 "-c", "1",
Stephane Eranian97101182011-01-12 10:29:05 +01001888 "-e", "sched:sched_switch",
1889 "-e", "sched:sched_stat_wait",
1890 "-e", "sched:sched_stat_sleep",
1891 "-e", "sched:sched_stat_iowait",
1892 "-e", "sched:sched_stat_runtime",
1893 "-e", "sched:sched_process_exit",
1894 "-e", "sched:sched_process_fork",
1895 "-e", "sched:sched_wakeup",
1896 "-e", "sched:sched_migrate_task",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001897};
1898
1899static int __cmd_record(int argc, const char **argv)
1900{
1901 unsigned int rec_argc, i, j;
1902 const char **rec_argv;
1903
1904 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1905 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1906
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001907 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001908 return -ENOMEM;
1909
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001910 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1911 rec_argv[i] = strdup(record_args[i]);
1912
1913 for (j = 1; j < (unsigned int)argc; j++, i++)
1914 rec_argv[i] = argv[j];
1915
1916 BUG_ON(i != rec_argc);
1917
1918 return cmd_record(i, rec_argv, NULL);
1919}
1920
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001921int cmd_sched(int argc, const char **argv, const char *prefix __used)
1922{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001923 argc = parse_options(argc, argv, sched_options, sched_usage,
1924 PARSE_OPT_STOP_AT_NON_OPTION);
1925 if (!argc)
1926 usage_with_options(sched_usage, sched_options);
1927
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001928 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001929 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001930 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001931 if (!strcmp(argv[0], "script"))
1932 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001933
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001934 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001935 if (!strncmp(argv[0], "rec", 3)) {
1936 return __cmd_record(argc, argv);
1937 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001938 trace_handler = &lat_ops;
1939 if (argc > 1) {
1940 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1941 if (argc)
1942 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001943 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001944 setup_sorting();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001945 return __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001946 } else if (!strcmp(argv[0], "map")) {
1947 trace_handler = &map_ops;
1948 setup_sorting();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001949 return __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001950 } else if (!strncmp(argv[0], "rep", 3)) {
1951 trace_handler = &replay_ops;
1952 if (argc) {
1953 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1954 if (argc)
1955 usage_with_options(replay_usage, replay_options);
1956 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001957 return __cmd_replay();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001958 } else {
1959 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001960 }
1961
Ingo Molnarec156762009-09-11 12:12:54 +02001962 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001963}