blob: 82e8ec2c43b7ccebbcf8fdf995d98809127bde70 [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,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300302 u64 task_state __maybe_unused)
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
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300372static void process_sched_event(struct task_desc *this_task __maybe_unused,
373 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,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300755 struct machine *machine __maybe_unused,
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,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300780 struct machine *machine __maybe_unused,
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{
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300784 struct task_desc *prev, __maybe_unused *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
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300935static int latency_fork_event(struct trace_fork_event *fork_event __maybe_unused,
936 struct event_format *event __maybe_unused)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200937{
938 /* should insert the newcomer */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300939 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200940}
941
942static char sched_out_state(struct trace_switch_event *switch_event)
943{
944 const char *str = TASK_STATE_TO_CHAR_STR;
945
946 return str[switch_event->prev_state];
947}
948
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300949static int
mingo39aeb522009-09-14 20:04:48 +0200950add_sched_out_event(struct work_atoms *atoms,
951 char run_state,
952 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200953{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200954 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300955 if (!atom) {
956 pr_err("Non memory at %s", __func__);
957 return -1;
958 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200959
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200960 atom->sched_out_time = timestamp;
961
mingo39aeb522009-09-14 20:04:48 +0200962 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200963 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200964 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200965 }
966
mingo39aeb522009-09-14 20:04:48 +0200967 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300968 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200969}
970
971static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300972add_runtime_event(struct work_atoms *atoms, u64 delta,
973 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200974{
975 struct work_atom *atom;
976
977 BUG_ON(list_empty(&atoms->work_list));
978
979 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
980
981 atom->runtime += delta;
982 atoms->total_runtime += delta;
983}
984
985static void
986add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200988 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200989 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200990
mingo39aeb522009-09-14 20:04:48 +0200991 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200992 return;
993
mingo39aeb522009-09-14 20:04:48 +0200994 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200995
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200996 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200997 return;
998
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200999 if (timestamp < atom->wake_up_time) {
1000 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001001 return;
1002 }
1003
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001004 atom->state = THREAD_SCHED_IN;
1005 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001006
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001007 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001008 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001009 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001010 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001011 atoms->max_lat_at = timestamp;
1012 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001013 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001014}
1015
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001016static int
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001017latency_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001018 struct machine *machine,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001019 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001020 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001021{
mingo39aeb522009-09-14 20:04:48 +02001022 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001023 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001024 u64 timestamp0, timestamp = sample->time;
1025 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001026 s64 delta;
1027
mingo39aeb522009-09-14 20:04:48 +02001028 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001029
1030 timestamp0 = cpu_last_switched[cpu];
1031 cpu_last_switched[cpu] = timestamp;
1032 if (timestamp0)
1033 delta = timestamp - timestamp0;
1034 else
1035 delta = 0;
1036
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001037 if (delta < 0) {
1038 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1039 return -1;
1040 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001041
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001042 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1043 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001044
mingo39aeb522009-09-14 20:04:48 +02001045 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1046 if (!out_events) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001047 if (thread_atoms_insert(sched_out))
1048 return -1;
mingo39aeb522009-09-14 20:04:48 +02001049 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001050 if (!out_events) {
1051 pr_err("out-event: Internal tree error");
1052 return -1;
1053 }
mingo39aeb522009-09-14 20:04:48 +02001054 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001055 if (add_sched_out_event(out_events, sched_out_state(switch_event), timestamp))
1056 return -1;
mingo39aeb522009-09-14 20:04:48 +02001057
1058 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1059 if (!in_events) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001060 if (thread_atoms_insert(sched_in))
1061 return -1;
mingo39aeb522009-09-14 20:04:48 +02001062 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001063 if (!in_events) {
1064 pr_err("in-event: Internal tree error");
1065 return -1;
1066 }
mingo39aeb522009-09-14 20:04:48 +02001067 /*
1068 * Take came in we have not heard about yet,
1069 * add in an initial atom in runnable state:
1070 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001071 if (add_sched_out_event(in_events, 'R', timestamp))
1072 return -1;
mingo39aeb522009-09-14 20:04:48 +02001073 }
1074 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001075
1076 return 0;
mingo39aeb522009-09-14 20:04:48 +02001077}
1078
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001079static int
mingo39aeb522009-09-14 20:04:48 +02001080latency_runtime_event(struct trace_runtime_event *runtime_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001081 struct machine *machine, struct perf_sample *sample)
mingo39aeb522009-09-14 20:04:48 +02001082{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001083 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -03001084 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001085 u64 timestamp = sample->time;
1086 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +02001087
1088 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001089 if (!atoms) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001090 if (thread_atoms_insert(thread))
1091 return -1;
mingo39aeb522009-09-14 20:04:48 +02001092 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001093 if (!atoms) {
1094 pr_debug("in-event: Internal tree error");
1095 return -1;
1096 }
1097 if (add_sched_out_event(atoms, 'R', timestamp))
1098 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001099 }
1100
mingo39aeb522009-09-14 20:04:48 +02001101 add_runtime_event(atoms, runtime_event->runtime, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001102 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103}
1104
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001105static int
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001106latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001107 struct machine *machine,
1108 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001109 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
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001372static int process_sched_wakeup_event(struct perf_tool *tool __maybe_unused,
1373 struct event_format *event,
1374 struct perf_sample *sample,
1375 struct machine *machine,
1376 struct thread *thread __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001377{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001378 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001379 struct trace_wakeup_event wakeup_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001380 int err = 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001381
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001382 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001383
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001384 FILL_ARRAY(wakeup_event, comm, event, data);
1385 FILL_FIELD(wakeup_event, pid, event, data);
1386 FILL_FIELD(wakeup_event, prio, event, data);
1387 FILL_FIELD(wakeup_event, success, event, data);
1388 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001389
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001390 if (trace_handler->wakeup_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001391 err = trace_handler->wakeup_event(&wakeup_event, machine, event, sample);
1392
1393 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001394}
1395
Ingo Molnarc8a37752009-09-16 14:07:00 +02001396/*
1397 * Track the current task - that way we can know whether there's any
1398 * weird events, such as a task being switched away that is not current.
1399 */
Ingo Molnar40749d02009-09-17 18:24:55 +02001400static int max_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001401
Ingo Molnarc8a37752009-09-16 14:07:00 +02001402static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1403
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001404static struct thread *curr_thread[MAX_CPUS];
1405
1406static char next_shortname1 = 'A';
1407static char next_shortname2 = '0';
1408
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001409static int
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001410map_switch_event(struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001411 struct machine *machine,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001412 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001413 struct perf_sample *sample)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001414{
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001415 struct thread *sched_out __maybe_unused, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001416 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001417 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001418 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001419 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001420
1421 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1422
1423 if (this_cpu > max_cpu)
1424 max_cpu = this_cpu;
1425
1426 timestamp0 = cpu_last_switched[this_cpu];
1427 cpu_last_switched[this_cpu] = timestamp;
1428 if (timestamp0)
1429 delta = timestamp - timestamp0;
1430 else
1431 delta = 0;
1432
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001433 if (delta < 0) {
1434 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1435 return -1;
1436 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001437
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001438 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1439 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001440
1441 curr_thread[this_cpu] = sched_in;
1442
1443 printf(" ");
1444
1445 new_shortname = 0;
1446 if (!sched_in->shortname[0]) {
1447 sched_in->shortname[0] = next_shortname1;
1448 sched_in->shortname[1] = next_shortname2;
1449
1450 if (next_shortname1 < 'Z') {
1451 next_shortname1++;
1452 } else {
1453 next_shortname1='A';
1454 if (next_shortname2 < '9') {
1455 next_shortname2++;
1456 } else {
1457 next_shortname2='0';
1458 }
1459 }
1460 new_shortname = 1;
1461 }
1462
1463 for (cpu = 0; cpu <= max_cpu; cpu++) {
1464 if (cpu != this_cpu)
1465 printf(" ");
1466 else
1467 printf("*");
1468
1469 if (curr_thread[cpu]) {
1470 if (curr_thread[cpu]->pid)
1471 printf("%2s ", curr_thread[cpu]->shortname);
1472 else
1473 printf(". ");
1474 } else
1475 printf(" ");
1476 }
1477
1478 printf(" %12.6f secs ", (double)timestamp/1e9);
1479 if (new_shortname) {
1480 printf("%s => %s:%d\n",
1481 sched_in->shortname, sched_in->comm, sched_in->pid);
1482 } else {
1483 printf("\n");
1484 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001485
1486 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001487}
1488
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001489static int process_sched_switch_event(struct perf_tool *tool __maybe_unused,
1490 struct event_format *event,
1491 struct perf_sample *sample,
1492 struct machine *machine,
1493 struct thread *thread __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001494{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001495 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001496 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001497 struct trace_switch_event switch_event;
1498
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001499 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001500
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001501 FILL_ARRAY(switch_event, prev_comm, event, data);
1502 FILL_FIELD(switch_event, prev_pid, event, data);
1503 FILL_FIELD(switch_event, prev_prio, event, data);
1504 FILL_FIELD(switch_event, prev_state, event, data);
1505 FILL_ARRAY(switch_event, next_comm, event, data);
1506 FILL_FIELD(switch_event, next_pid, event, data);
1507 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001508
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001509 if (curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001510 /*
1511 * Are we trying to switch away a PID that is
1512 * not current?
1513 */
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001514 if (curr_pid[this_cpu] != switch_event.prev_pid)
Ingo Molnarc8a37752009-09-16 14:07:00 +02001515 nr_context_switch_bugs++;
1516 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001517 if (trace_handler->switch_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001518 err = trace_handler->switch_event(&switch_event, machine, event, sample);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001519
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001520 curr_pid[this_cpu] = switch_event.next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001521 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001522}
1523
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001524static int process_sched_runtime_event(struct perf_tool *tool __maybe_unused,
1525 struct event_format *event,
1526 struct perf_sample *sample,
1527 struct machine *machine,
1528 struct thread *thread __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +02001529{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001530 void *data = sample->raw_data;
mingo39aeb522009-09-14 20:04:48 +02001531 struct trace_runtime_event runtime_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001532 int err = 0;
mingo39aeb522009-09-14 20:04:48 +02001533
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001534 FILL_ARRAY(runtime_event, comm, event, data);
1535 FILL_FIELD(runtime_event, pid, event, data);
1536 FILL_FIELD(runtime_event, runtime, event, data);
1537 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001538
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001539 if (trace_handler->runtime_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001540 err = trace_handler->runtime_event(&runtime_event, machine, sample);
1541
1542 return err;
mingo39aeb522009-09-14 20:04:48 +02001543}
1544
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001545static int process_sched_fork_event(struct perf_tool *tool __maybe_unused,
1546 struct event_format *event,
1547 struct perf_sample *sample,
1548 struct machine *machine __maybe_unused,
1549 struct thread *thread __maybe_unused)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001550{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001551 void *data = sample->raw_data;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001552 struct trace_fork_event fork_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001553 int err = 0;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001554
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001555 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001556
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001557 FILL_ARRAY(fork_event, parent_comm, event, data);
1558 FILL_FIELD(fork_event, parent_pid, event, data);
1559 FILL_ARRAY(fork_event, child_comm, event, data);
1560 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001561
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001562 if (trace_handler->fork_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001563 err = trace_handler->fork_event(&fork_event, event);
1564
1565 return err;
Ingo Molnarfbf94822009-09-11 12:12:54 +02001566}
1567
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001568static int process_sched_exit_event(struct perf_tool *tool __maybe_unused,
1569 struct event_format *event,
1570 struct perf_sample *sample __maybe_unused,
1571 struct machine *machine __maybe_unused,
1572 struct thread *thread __maybe_unused)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001573{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001574 if (verbose)
1575 printf("sched_exit event %p\n", event);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001576
1577 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001578}
1579
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001580static int process_sched_migrate_task_event(struct perf_tool *tool __maybe_unused,
1581 struct event_format *event,
1582 struct perf_sample *sample,
1583 struct machine *machine,
1584 struct thread *thread __maybe_unused)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001585{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001586 void *data = sample->raw_data;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001587 struct trace_migrate_task_event migrate_task_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001588 int err = 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001589
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001590 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001591
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001592 FILL_ARRAY(migrate_task_event, comm, event, data);
1593 FILL_FIELD(migrate_task_event, pid, event, data);
1594 FILL_FIELD(migrate_task_event, prio, event, data);
1595 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001596
1597 if (trace_handler->migrate_task_event)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001598 err = trace_handler->migrate_task_event(&migrate_task_event, machine, sample);
1599
1600 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001601}
1602
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001603typedef int (*tracepoint_handler)(struct perf_tool *tool,
1604 struct event_format *tp_format,
1605 struct perf_sample *sample,
1606 struct machine *machine,
1607 struct thread *thread);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001608
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001609static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1610 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001611 struct perf_sample *sample,
1612 struct perf_evsel *evsel,
1613 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001614{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001615 struct thread *thread = machine__findnew_thread(machine, sample->pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001616 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001617
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001618 if (thread == NULL) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001619 pr_debug("problem processing %s event, skipping it.\n",
Arnaldo Carvalho de Melo22c8b842012-06-12 13:55:13 -03001620 perf_evsel__name(evsel));
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001621 return -1;
1622 }
1623
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001624 evsel->hists.stats.total_period += sample->period;
1625 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
Julia Lawallf39cdf22009-10-17 08:43:17 +02001626
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001627 if (evsel->handler.func != NULL) {
1628 tracepoint_handler f = evsel->handler.func;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001629 err = f(tool, evsel->tp_format, sample, machine, thread);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001630 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001631
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001632 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001633}
1634
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001635static struct perf_tool perf_sched = {
1636 .sample = perf_sched__process_tracepoint_sample,
1637 .comm = perf_event__process_comm,
1638 .lost = perf_event__process_lost,
1639 .fork = perf_event__process_task,
1640 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +02001641};
1642
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001643static int read_events(bool destroy, struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001644{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001645 const struct perf_evsel_str_handler handlers[] = {
1646 { "sched:sched_switch", process_sched_switch_event, },
1647 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1648 { "sched:sched_wakeup", process_sched_wakeup_event, },
1649 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1650 { "sched:sched_process_fork", process_sched_fork_event, },
1651 { "sched:sched_process_exit", process_sched_exit_event, },
1652 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1653 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001654 struct perf_session *session;
1655
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001656 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001657 if (session == NULL) {
1658 pr_debug("No Memory for session\n");
1659 return -1;
1660 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001661
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001662 if (perf_session__set_tracepoints_handlers(session, handlers))
1663 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001664
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001665 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001666 int err = perf_session__process_events(session, &perf_sched);
1667 if (err) {
1668 pr_err("Failed to process events, error %d", err);
1669 goto out_delete;
1670 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001671
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001672 nr_events = session->hists.stats.nr_events[0];
1673 nr_lost_events = session->hists.stats.total_lost;
1674 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1675 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001676
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001677 if (destroy)
1678 perf_session__delete(session);
1679
1680 if (psession)
1681 *psession = session;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001682
1683 return 0;
1684
1685out_delete:
1686 perf_session__delete(session);
1687 return -1;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001688}
1689
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001690static void print_bad_events(void)
1691{
1692 if (nr_unordered_timestamps && nr_timestamps) {
1693 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1694 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1695 nr_unordered_timestamps, nr_timestamps);
1696 }
1697 if (nr_lost_events && nr_events) {
1698 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1699 (double)nr_lost_events/(double)nr_events*100.0,
1700 nr_lost_events, nr_events, nr_lost_chunks);
1701 }
1702 if (nr_state_machine_bugs && nr_timestamps) {
1703 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1704 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1705 nr_state_machine_bugs, nr_timestamps);
1706 if (nr_lost_events)
1707 printf(" (due to lost events?)");
1708 printf("\n");
1709 }
1710 if (nr_context_switch_bugs && nr_timestamps) {
1711 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1712 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1713 nr_context_switch_bugs, nr_timestamps);
1714 if (nr_lost_events)
1715 printf(" (due to lost events?)");
1716 printf("\n");
1717 }
1718}
1719
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001720static int __cmd_lat(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001721{
1722 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001723 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001724
1725 setup_pager();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001726 if (read_events(false, &session))
1727 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001728 sort_lat();
1729
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001730 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1731 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1732 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
1734 next = rb_first(&sorted_atom_root);
1735
1736 while (next) {
1737 struct work_atoms *work_list;
1738
1739 work_list = rb_entry(next, struct work_atoms, node);
1740 output_lat_thread(work_list);
1741 next = rb_next(next);
1742 }
1743
1744 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001745 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001746 (double)all_runtime/1e6, all_count);
1747
1748 printf(" ---------------------------------------------------\n");
1749
1750 print_bad_events();
1751 printf("\n");
1752
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001753 perf_session__delete(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001754 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001755}
1756
1757static struct trace_sched_handler map_ops = {
1758 .wakeup_event = NULL,
1759 .switch_event = map_switch_event,
1760 .runtime_event = NULL,
1761 .fork_event = NULL,
1762};
1763
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001764static int __cmd_map(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001765{
Ingo Molnar40749d02009-09-17 18:24:55 +02001766 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1767
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001768 setup_pager();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001769 if (read_events(true, NULL))
1770 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001771 print_bad_events();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001772 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001773}
1774
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001775static int __cmd_replay(void)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001776{
1777 unsigned long i;
1778
1779 calibrate_run_measurement_overhead();
1780 calibrate_sleep_measurement_overhead();
1781
1782 test_calibrations();
1783
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001784 if (read_events(true, NULL))
1785 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001786
1787 printf("nr_run_events: %ld\n", nr_run_events);
1788 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1789 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1790
1791 if (targetless_wakeups)
1792 printf("target-less wakeups: %ld\n", targetless_wakeups);
1793 if (multitarget_wakeups)
1794 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1795 if (nr_run_events_optimized)
1796 printf("run atoms optimized: %ld\n",
1797 nr_run_events_optimized);
1798
1799 print_task_traces();
1800 add_cross_task_wakeups();
1801
1802 create_tasks();
1803 printf("------------------------------------------------------------\n");
1804 for (i = 0; i < replay_repeat; i++)
1805 run_one_test();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001806
1807 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001808}
1809
1810
Ingo Molnar46f392c2009-09-12 10:08:34 +02001811static const char * const sched_usage[] = {
Jiri Olsa580cabe2011-08-09 14:46:51 +02001812 "perf sched [<options>] {record|latency|map|replay|script}",
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001813 NULL
1814};
1815
Ingo Molnarf2858d82009-09-11 12:12:54 +02001816static const struct option sched_options[] = {
Mike Galbraith4b77a722009-09-18 08:22:24 +02001817 OPT_STRING('i', "input", &input_name, "file",
1818 "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001819 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001820 "be more verbose (show symbol address, etc)"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001821 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1822 "dump raw trace in ASCII"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001823 OPT_END()
1824};
1825
1826static const char * const latency_usage[] = {
1827 "perf sched latency [<options>]",
1828 NULL
1829};
1830
1831static const struct option latency_options[] = {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001832 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1833 "sort by key(s): runtime, switch, avg, max"),
Ian Munsiec0555642010-04-13 18:37:33 +10001834 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001835 "be more verbose (show symbol address, etc)"),
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001836 OPT_INTEGER('C', "CPU", &profile_cpu,
1837 "CPU to profile on"),
Ingo Molnarf2858d82009-09-11 12:12:54 +02001838 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1839 "dump raw trace in ASCII"),
1840 OPT_END()
1841};
1842
1843static const char * const replay_usage[] = {
1844 "perf sched replay [<options>]",
1845 NULL
1846};
1847
1848static const struct option replay_options[] = {
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -03001849 OPT_UINTEGER('r', "repeat", &replay_repeat,
1850 "repeat the workload replay N times (-1: infinite)"),
Ian Munsiec0555642010-04-13 18:37:33 +10001851 OPT_INCR('v', "verbose", &verbose,
Ingo Molnarf2858d82009-09-11 12:12:54 +02001852 "be more verbose (show symbol address, etc)"),
1853 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1854 "dump raw trace in ASCII"),
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001855 OPT_END()
1856};
1857
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001858static void setup_sorting(void)
1859{
1860 char *tmp, *tok, *str = strdup(sort_order);
1861
1862 for (tok = strtok_r(str, ", ", &tmp);
1863 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1864 if (sort_dimension__add(tok, &sort_list) < 0) {
1865 error("Unknown --sort key: `%s'", tok);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001866 usage_with_options(latency_usage, latency_options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001867 }
1868 }
1869
1870 free(str);
1871
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001872 sort_dimension__add("pid", &cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001873}
1874
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001875static const char *record_args[] = {
1876 "record",
1877 "-a",
1878 "-R",
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001879 "-f",
Ingo Molnardc02bf72009-09-16 13:45:00 +02001880 "-m", "1024",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001881 "-c", "1",
Stephane Eranian97101182011-01-12 10:29:05 +01001882 "-e", "sched:sched_switch",
1883 "-e", "sched:sched_stat_wait",
1884 "-e", "sched:sched_stat_sleep",
1885 "-e", "sched:sched_stat_iowait",
1886 "-e", "sched:sched_stat_runtime",
1887 "-e", "sched:sched_process_exit",
1888 "-e", "sched:sched_process_fork",
1889 "-e", "sched:sched_wakeup",
1890 "-e", "sched:sched_migrate_task",
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001891};
1892
1893static int __cmd_record(int argc, const char **argv)
1894{
1895 unsigned int rec_argc, i, j;
1896 const char **rec_argv;
1897
1898 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1899 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1900
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001901 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001902 return -ENOMEM;
1903
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001904 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1905 rec_argv[i] = strdup(record_args[i]);
1906
1907 for (j = 1; j < (unsigned int)argc; j++, i++)
1908 rec_argv[i] = argv[j];
1909
1910 BUG_ON(i != rec_argc);
1911
1912 return cmd_record(i, rec_argv, NULL);
1913}
1914
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001915int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001916{
Ingo Molnarf2858d82009-09-11 12:12:54 +02001917 argc = parse_options(argc, argv, sched_options, sched_usage,
1918 PARSE_OPT_STOP_AT_NON_OPTION);
1919 if (!argc)
1920 usage_with_options(sched_usage, sched_options);
1921
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001922 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001923 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001924 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001925 if (!strcmp(argv[0], "script"))
1926 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001927
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001928 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001929 if (!strncmp(argv[0], "rec", 3)) {
1930 return __cmd_record(argc, argv);
1931 } else if (!strncmp(argv[0], "lat", 3)) {
Ingo Molnarf2858d82009-09-11 12:12:54 +02001932 trace_handler = &lat_ops;
1933 if (argc > 1) {
1934 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1935 if (argc)
1936 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001937 }
Ingo Molnarb5fae122009-09-11 12:12:54 +02001938 setup_sorting();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001939 return __cmd_lat();
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001940 } else if (!strcmp(argv[0], "map")) {
1941 trace_handler = &map_ops;
1942 setup_sorting();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001943 return __cmd_map();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001944 } else if (!strncmp(argv[0], "rep", 3)) {
1945 trace_handler = &replay_ops;
1946 if (argc) {
1947 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1948 if (argc)
1949 usage_with_options(replay_usage, replay_options);
1950 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001951 return __cmd_replay();
Ingo Molnarf2858d82009-09-11 12:12:54 +02001952 } else {
1953 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001954 }
1955
Ingo Molnarec156762009-09-11 12:12:54 +02001956 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001957}