blob: 79f88fa3f7a3f72e943382162a894fe979cfbb53 [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
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020026#define PR_SET_NAME 15 /* Set process name */
27#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020028#define COMM_LEN 20
29#define SYM_LEN 129
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020030#define MAX_PID 65536
Ingo Molnarec156762009-09-11 12:12:54 +020031
mingo39aeb522009-09-14 20:04:48 +020032struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020033
34struct task_desc {
35 unsigned long nr;
36 unsigned long pid;
37 char comm[COMM_LEN];
38
39 unsigned long nr_events;
40 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020041 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020042
43 pthread_t thread;
44 sem_t sleep_sem;
45
46 sem_t ready_for_work;
47 sem_t work_done_sem;
48
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020049 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020050};
51
52enum sched_event_type {
53 SCHED_EVENT_RUN,
54 SCHED_EVENT_SLEEP,
55 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020056 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020057};
58
mingo39aeb522009-09-14 20:04:48 +020059struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020060 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030061 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020062 u64 timestamp;
63 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020064 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020065 sem_t *wait_sem;
66 struct task_desc *wakee;
67};
68
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020069#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
70
71enum thread_state {
72 THREAD_SLEEPING = 0,
73 THREAD_WAIT_CPU,
74 THREAD_SCHED_IN,
75 THREAD_IGNORE
76};
77
78struct work_atom {
79 struct list_head list;
80 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020081 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020082 u64 wake_up_time;
83 u64 sched_in_time;
84 u64 runtime;
85};
86
mingo39aeb522009-09-14 20:04:48 +020087struct work_atoms {
88 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020089 struct thread *thread;
90 struct rb_node node;
91 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +010092 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020093 u64 total_lat;
94 u64 nb_atoms;
95 u64 total_runtime;
96};
97
mingo39aeb522009-09-14 20:04:48 +020098typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020099
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300100struct trace_switch_event {
101 u32 size;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200102
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300103 u16 common_type;
104 u8 common_flags;
105 u8 common_preempt_count;
106 u32 common_pid;
107 u32 common_tgid;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200108
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300109 char prev_comm[16];
110 u32 prev_pid;
111 u32 prev_prio;
112 u64 prev_state;
113 char next_comm[16];
114 u32 next_pid;
115 u32 next_prio;
116};
117
118struct trace_runtime_event {
119 u32 size;
120
121 u16 common_type;
122 u8 common_flags;
123 u8 common_preempt_count;
124 u32 common_pid;
125 u32 common_tgid;
126
127 char comm[16];
128 u32 pid;
129 u64 runtime;
130 u64 vruntime;
131};
132
133struct trace_wakeup_event {
134 u32 size;
135
136 u16 common_type;
137 u8 common_flags;
138 u8 common_preempt_count;
139 u32 common_pid;
140 u32 common_tgid;
141
142 char comm[16];
143 u32 pid;
144
145 u32 prio;
146 u32 success;
147 u32 cpu;
148};
149
150struct trace_fork_event {
151 u32 size;
152
153 u16 common_type;
154 u8 common_flags;
155 u8 common_preempt_count;
156 u32 common_pid;
157 u32 common_tgid;
158
159 char parent_comm[16];
160 u32 parent_pid;
161 char child_comm[16];
162 u32 child_pid;
163};
164
165struct trace_migrate_task_event {
166 u32 size;
167
168 u16 common_type;
169 u8 common_flags;
170 u8 common_preempt_count;
171 u32 common_pid;
172 u32 common_tgid;
173
174 char comm[16];
175 u32 pid;
176
177 u32 prio;
178 u32 cpu;
179};
180
181struct perf_sched;
182
183struct trace_sched_handler {
184 int (*switch_event)(struct perf_sched *sched,
185 struct trace_switch_event *event,
186 struct machine *machine,
187 struct event_format *tp_format,
188 struct perf_sample *sample);
189
190 int (*runtime_event)(struct perf_sched *sched,
191 struct trace_runtime_event *event,
192 struct machine *machine,
193 struct perf_sample *sample);
194
195 int (*wakeup_event)(struct perf_sched *sched,
196 struct trace_wakeup_event *event,
197 struct machine *machine,
198 struct event_format *tp_format,
199 struct perf_sample *sample);
200
201 int (*fork_event)(struct perf_sched *sched,
202 struct trace_fork_event *event,
203 struct event_format *tp_format);
204
205 int (*migrate_task_event)(struct perf_sched *sched,
206 struct trace_migrate_task_event *event,
207 struct machine *machine,
208 struct perf_sample *sample);
209};
210
211struct perf_sched {
212 struct perf_tool tool;
213 const char *input_name;
214 const char *sort_order;
215 unsigned long nr_tasks;
216 struct task_desc *pid_to_task[MAX_PID];
217 struct task_desc **tasks;
218 const struct trace_sched_handler *tp_handler;
219 pthread_mutex_t start_work_mutex;
220 pthread_mutex_t work_done_wait_mutex;
221 int profile_cpu;
222/*
223 * Track the current task - that way we can know whether there's any
224 * weird events, such as a task being switched away that is not current.
225 */
226 int max_cpu;
227 u32 curr_pid[MAX_CPUS];
228 struct thread *curr_thread[MAX_CPUS];
229 char next_shortname1;
230 char next_shortname2;
231 unsigned int replay_repeat;
232 unsigned long nr_run_events;
233 unsigned long nr_sleep_events;
234 unsigned long nr_wakeup_events;
235 unsigned long nr_sleep_corrections;
236 unsigned long nr_run_events_optimized;
237 unsigned long targetless_wakeups;
238 unsigned long multitarget_wakeups;
239 unsigned long nr_runs;
240 unsigned long nr_timestamps;
241 unsigned long nr_unordered_timestamps;
242 unsigned long nr_state_machine_bugs;
243 unsigned long nr_context_switch_bugs;
244 unsigned long nr_events;
245 unsigned long nr_lost_chunks;
246 unsigned long nr_lost_events;
247 u64 run_measurement_overhead;
248 u64 sleep_measurement_overhead;
249 u64 start_time;
250 u64 cpu_usage;
251 u64 runavg_cpu_usage;
252 u64 parent_cpu_usage;
253 u64 runavg_parent_cpu_usage;
254 u64 sum_runtime;
255 u64 sum_fluct;
256 u64 run_avg;
257 u64 all_runtime;
258 u64 all_count;
259 u64 cpu_last_switched[MAX_CPUS];
260 struct rb_root atom_root, sorted_atom_root;
261 struct list_head sort_list, cmp_pid;
262};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200263
264static u64 get_nsecs(void)
265{
266 struct timespec ts;
267
268 clock_gettime(CLOCK_MONOTONIC, &ts);
269
270 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
271}
272
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300273static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200274{
275 u64 T0 = get_nsecs(), T1;
276
277 do {
278 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300279 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200280}
281
282static void sleep_nsecs(u64 nsecs)
283{
284 struct timespec ts;
285
286 ts.tv_nsec = nsecs % 999999999;
287 ts.tv_sec = nsecs / 999999999;
288
289 nanosleep(&ts, NULL);
290}
291
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300292static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200293{
294 u64 T0, T1, delta, min_delta = 1000000000ULL;
295 int i;
296
297 for (i = 0; i < 10; i++) {
298 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300299 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200300 T1 = get_nsecs();
301 delta = T1-T0;
302 min_delta = min(min_delta, delta);
303 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300304 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200305
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200306 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200307}
308
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300309static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200310{
311 u64 T0, T1, delta, min_delta = 1000000000ULL;
312 int i;
313
314 for (i = 0; i < 10; i++) {
315 T0 = get_nsecs();
316 sleep_nsecs(10000);
317 T1 = get_nsecs();
318 delta = T1-T0;
319 min_delta = min(min_delta, delta);
320 }
321 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300322 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200323
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200324 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200325}
326
mingo39aeb522009-09-14 20:04:48 +0200327static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200328get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200329{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200330 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200331 unsigned long idx = task->nr_events;
332 size_t size;
333
334 event->timestamp = timestamp;
335 event->nr = idx;
336
337 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200338 size = sizeof(struct sched_atom *) * task->nr_events;
339 task->atoms = realloc(task->atoms, size);
340 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200341
mingo39aeb522009-09-14 20:04:48 +0200342 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200343
344 return event;
345}
346
mingo39aeb522009-09-14 20:04:48 +0200347static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200348{
349 if (!task->nr_events)
350 return NULL;
351
mingo39aeb522009-09-14 20:04:48 +0200352 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200353}
354
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300355static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
356 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200357{
mingo39aeb522009-09-14 20:04:48 +0200358 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200359
360 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200361 * optimize an existing RUN event by merging this one
362 * to it:
363 */
Ingo Molnarec156762009-09-11 12:12:54 +0200364 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300365 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200366 curr_event->duration += duration;
367 return;
368 }
369
370 event = get_new_event(task, timestamp);
371
372 event->type = SCHED_EVENT_RUN;
373 event->duration = duration;
374
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300375 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200376}
377
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300378static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
379 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200380{
mingo39aeb522009-09-14 20:04:48 +0200381 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200382
383 event = get_new_event(task, timestamp);
384 event->type = SCHED_EVENT_WAKEUP;
385 event->wakee = wakee;
386
387 wakee_event = last_event(wakee);
388 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300389 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200390 return;
391 }
392 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300393 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200394 return;
395 }
396
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200397 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200398 sem_init(wakee_event->wait_sem, 0, 0);
399 wakee_event->specific_wait = 1;
400 event->wait_sem = wakee_event->wait_sem;
401
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300402 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200403}
404
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300405static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
406 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200407{
mingo39aeb522009-09-14 20:04:48 +0200408 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200409
410 event->type = SCHED_EVENT_SLEEP;
411
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300412 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200413}
414
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415static struct task_desc *register_pid(struct perf_sched *sched,
416 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200417{
418 struct task_desc *task;
419
420 BUG_ON(pid >= MAX_PID);
421
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300422 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200423
424 if (task)
425 return task;
426
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200427 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200428 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300429 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200430 strcpy(task->comm, comm);
431 /*
432 * every task starts in sleeping state - this gets ignored
433 * if there's no wakeup pointing to this sleep state:
434 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300435 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200436
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300437 sched->pid_to_task[pid] = task;
438 sched->nr_tasks++;
439 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_task *));
440 BUG_ON(!sched->tasks);
441 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200442
Ingo Molnarad236fd2009-09-11 12:12:54 +0200443 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300444 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200445
446 return task;
447}
448
449
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300450static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200451{
452 struct task_desc *task;
453 unsigned long i;
454
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300455 for (i = 0; i < sched->nr_tasks; i++) {
456 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200457 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200458 task->nr, task->comm, task->pid, task->nr_events);
459 }
460}
461
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300462static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200463{
464 struct task_desc *task1, *task2;
465 unsigned long i, j;
466
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300467 for (i = 0; i < sched->nr_tasks; i++) {
468 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200469 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300470 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200471 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300472 task2 = sched->tasks[j];
473 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200474 }
475}
476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300477static void perf_sched__process_event(struct perf_sched *sched,
478 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200479{
480 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200481
mingo39aeb522009-09-14 20:04:48 +0200482 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200483 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300484 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200485 break;
486 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200487 if (atom->wait_sem)
488 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200489 BUG_ON(ret);
490 break;
491 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200492 if (atom->wait_sem)
493 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200494 BUG_ON(ret);
495 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200496 case SCHED_EVENT_MIGRATION:
497 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200498 default:
499 BUG_ON(1);
500 }
501}
502
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200503static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200504{
505 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200506 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200507 int err;
508
509 err = getrusage(RUSAGE_SELF, &ru);
510 BUG_ON(err);
511
512 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
513 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
514
515 return sum;
516}
517
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800518static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200519{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800520 struct perf_event_attr attr;
521 int fd;
522
523 memset(&attr, 0, sizeof(attr));
524
525 attr.type = PERF_TYPE_SOFTWARE;
526 attr.config = PERF_COUNT_SW_TASK_CLOCK;
527
528 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
529
530 if (fd < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300531 pr_debug("Error: sys_perf_event_open() syscall returned"
532 "with %d (%s)\n", fd, strerror(errno));
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800533 return fd;
534}
535
536static u64 get_cpu_usage_nsec_self(int fd)
537{
538 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200539 int ret;
540
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800541 ret = read(fd, &runtime, sizeof(runtime));
542 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200543
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800544 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200545}
546
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300547struct sched_thread_parms {
548 struct task_desc *task;
549 struct perf_sched *sched;
550};
551
Ingo Molnarec156762009-09-11 12:12:54 +0200552static void *thread_func(void *ctx)
553{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300554 struct sched_thread_parms *parms = ctx;
555 struct task_desc *this_task = parms->task;
556 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200557 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200558 unsigned long i, ret;
559 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800560 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200561
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300562 free(parms);
563
Ingo Molnarec156762009-09-11 12:12:54 +0200564 sprintf(comm2, ":%s", this_task->comm);
565 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800566 fd = self_open_counters();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300567 if (fd < 0)
568 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200569again:
570 ret = sem_post(&this_task->ready_for_work);
571 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300572 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200573 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300574 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200575 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200576
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800577 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200578
579 for (i = 0; i < this_task->nr_events; i++) {
580 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300581 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200582 }
583
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800584 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200585 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200586 ret = sem_post(&this_task->work_done_sem);
587 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200588
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300589 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200590 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300591 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200592 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200593
594 goto again;
595}
596
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300597static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200598{
599 struct task_desc *task;
600 pthread_attr_t attr;
601 unsigned long i;
602 int err;
603
604 err = pthread_attr_init(&attr);
605 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200606 err = pthread_attr_setstacksize(&attr,
607 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200608 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300609 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200610 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200612 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 for (i = 0; i < sched->nr_tasks; i++) {
614 struct sched_thread_parms *parms = malloc(sizeof(*parms));
615 BUG_ON(parms == NULL);
616 parms->task = task = sched->tasks[i];
617 parms->sched = sched;
Ingo Molnarec156762009-09-11 12:12:54 +0200618 sem_init(&task->sleep_sem, 0, 0);
619 sem_init(&task->ready_for_work, 0, 0);
620 sem_init(&task->work_done_sem, 0, 0);
621 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300622 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200623 BUG_ON(err);
624 }
625}
626
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200628{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200629 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200630 struct task_desc *task;
631 unsigned long i, ret;
632
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300633 sched->start_time = get_nsecs();
634 sched->cpu_usage = 0;
635 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637 for (i = 0; i < sched->nr_tasks; i++) {
638 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200639 ret = sem_wait(&task->ready_for_work);
640 BUG_ON(ret);
641 sem_init(&task->ready_for_work, 0, 0);
642 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200644 BUG_ON(ret);
645
646 cpu_usage_0 = get_cpu_usage_nsec_parent();
647
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300648 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200649
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 for (i = 0; i < sched->nr_tasks; i++) {
651 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200652 ret = sem_wait(&task->work_done_sem);
653 BUG_ON(ret);
654 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200656 task->cpu_usage = 0;
657 }
658
659 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300660 if (!sched->runavg_cpu_usage)
661 sched->runavg_cpu_usage = sched->cpu_usage;
662 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200663
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300664 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
665 if (!sched->runavg_parent_cpu_usage)
666 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
667 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
668 sched->parent_cpu_usage)/10;
Ingo Molnarec156762009-09-11 12:12:54 +0200669
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200671 BUG_ON(ret);
672
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 for (i = 0; i < sched->nr_tasks; i++) {
674 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200675 sem_init(&task->sleep_sem, 0, 0);
676 task->curr_event = 0;
677 }
678}
679
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300680static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200681{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500682 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200683
684 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300685 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200686 T1 = get_nsecs();
687
688 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 sched->sum_runtime += delta;
690 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200691
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300692 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200693 if (delta < avg_delta)
694 fluct = avg_delta - delta;
695 else
696 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300697 sched->sum_fluct += fluct;
698 if (!sched->run_avg)
699 sched->run_avg = delta;
700 sched->run_avg = (sched->run_avg * 9 + delta) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200701
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300702 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200703
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300704 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200705
Ingo Molnarad236fd2009-09-11 12:12:54 +0200706 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300707 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200708
709#if 0
710 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200711 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300712 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200713 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200714 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300715 (double)sched->parent_cpu_usage/1e6,
716 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200717#endif
718
Ingo Molnarad236fd2009-09-11 12:12:54 +0200719 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200720
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300721 if (sched->nr_sleep_corrections)
722 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
723 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200724}
725
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300726static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200727{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200728 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200729
730 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300731 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200732 T1 = get_nsecs();
733
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200734 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200735
736 T0 = get_nsecs();
737 sleep_nsecs(1e6);
738 T1 = get_nsecs();
739
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200740 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200741}
742
Frederic Weisbecker46538812009-09-12 02:43:45 +0200743#define FILL_FIELD(ptr, field, event, data) \
744 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
745
746#define FILL_ARRAY(ptr, array, event, data) \
747do { \
748 void *__array = raw_field_ptr(event, #array, data); \
749 memcpy(ptr.array, __array, sizeof(ptr.array)); \
750} while(0)
751
752#define FILL_COMMON_FIELDS(ptr, event, data) \
753do { \
754 FILL_FIELD(ptr, common_type, event, data); \
755 FILL_FIELD(ptr, common_flags, event, data); \
756 FILL_FIELD(ptr, common_preempt_count, event, data); \
757 FILL_FIELD(ptr, common_pid, event, data); \
758 FILL_FIELD(ptr, common_tgid, event, data); \
759} while (0)
760
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300761static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300762replay_wakeup_event(struct perf_sched *sched,
763 struct trace_wakeup_event *wakeup_event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300764 struct machine *machine __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300765 struct event_format *event, struct perf_sample *sample)
Ingo Molnarec156762009-09-11 12:12:54 +0200766{
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200767 struct task_desc *waker, *wakee;
768
769 if (verbose) {
770 printf("sched_wakeup event %p\n", event);
771
772 printf(" ... pid %d woke up %s/%d\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300773 wakeup_event->common_pid, wakeup_event->comm, wakeup_event->pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200774 }
775
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300776 waker = register_pid(sched, wakeup_event->common_pid, "<unknown>");
777 wakee = register_pid(sched, wakeup_event->pid, wakeup_event->comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200778
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300779 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300780 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200781}
782
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300783static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300784replay_switch_event(struct perf_sched *sched,
785 struct trace_switch_event *switch_event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300786 struct machine *machine __maybe_unused,
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200787 struct event_format *event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300788 struct perf_sample *sample)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200789{
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300790 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300791 u64 timestamp0, timestamp = sample->time;
792 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200793 s64 delta;
794
Ingo Molnarad236fd2009-09-11 12:12:54 +0200795 if (verbose)
796 printf("sched_switch event %p\n", event);
797
Ingo Molnarfbf94822009-09-11 12:12:54 +0200798 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300799 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200800
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300801 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200802 if (timestamp0)
803 delta = timestamp - timestamp0;
804 else
805 delta = 0;
806
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300807 if (delta < 0) {
808 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
809 return -1;
810 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200811
Ingo Molnarad236fd2009-09-11 12:12:54 +0200812 if (verbose) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200813 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200814 switch_event->prev_comm, switch_event->prev_pid,
815 switch_event->next_comm, switch_event->next_pid,
Ingo Molnarad236fd2009-09-11 12:12:54 +0200816 delta);
817 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200818
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300819 prev = register_pid(sched, switch_event->prev_pid, switch_event->prev_comm);
820 next = register_pid(sched, switch_event->next_pid, switch_event->next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200821
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300822 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200823
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300824 add_sched_event_run(sched, prev, timestamp, delta);
825 add_sched_event_sleep(sched, prev, timestamp, switch_event->prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300826
827 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200828}
829
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300830static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300831replay_fork_event(struct perf_sched *sched, struct trace_fork_event *fork_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300832 struct event_format *event)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200833{
834 if (verbose) {
835 printf("sched_fork event %p\n", event);
836 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
837 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
838 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300839 register_pid(sched, fork_event->parent_pid, fork_event->parent_comm);
840 register_pid(sched, fork_event->child_pid, fork_event->child_comm);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300841 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200842}
843
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200844struct sort_dimension {
845 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200846 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200847 struct list_head list;
848};
849
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200850static int
mingo39aeb522009-09-14 20:04:48 +0200851thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200852{
853 struct sort_dimension *sort;
854 int ret = 0;
855
Ingo Molnarb5fae122009-09-11 12:12:54 +0200856 BUG_ON(list_empty(list));
857
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200858 list_for_each_entry(sort, list, list) {
859 ret = sort->cmp(l, r);
860 if (ret)
861 return ret;
862 }
863
864 return ret;
865}
866
mingo39aeb522009-09-14 20:04:48 +0200867static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200868thread_atoms_search(struct rb_root *root, struct thread *thread,
869 struct list_head *sort_list)
870{
871 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200872 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200873
874 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200875 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200876 int cmp;
877
mingo39aeb522009-09-14 20:04:48 +0200878 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200879
880 cmp = thread_lat_cmp(sort_list, &key, atoms);
881 if (cmp > 0)
882 node = node->rb_left;
883 else if (cmp < 0)
884 node = node->rb_right;
885 else {
886 BUG_ON(thread != atoms->thread);
887 return atoms;
888 }
889 }
890 return NULL;
891}
892
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200893static void
mingo39aeb522009-09-14 20:04:48 +0200894__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200895 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200896{
897 struct rb_node **new = &(root->rb_node), *parent = NULL;
898
899 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200900 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200901 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200902
mingo39aeb522009-09-14 20:04:48 +0200903 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200904 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200905
906 cmp = thread_lat_cmp(sort_list, data, this);
907
908 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200909 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200911 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200912 }
913
914 rb_link_node(&data->node, parent, new);
915 rb_insert_color(&data->node, root);
916}
917
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300918static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200919{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200920 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300921 if (!atoms) {
922 pr_err("No memory at %s\n", __func__);
923 return -1;
924 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200925
Frederic Weisbecker17562202009-09-12 23:11:32 +0200926 atoms->thread = thread;
mingo39aeb522009-09-14 20:04:48 +0200927 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300928 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300929 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930}
931
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300932static int latency_fork_event(struct perf_sched *sched __maybe_unused,
933 struct trace_fork_event *fork_event __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300934 struct event_format *event __maybe_unused)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935{
936 /* should insert the newcomer */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300937 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938}
939
940static char sched_out_state(struct trace_switch_event *switch_event)
941{
942 const char *str = TASK_STATE_TO_CHAR_STR;
943
944 return str[switch_event->prev_state];
945}
946
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300947static int
mingo39aeb522009-09-14 20:04:48 +0200948add_sched_out_event(struct work_atoms *atoms,
949 char run_state,
950 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200952 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300953 if (!atom) {
954 pr_err("Non memory at %s", __func__);
955 return -1;
956 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200958 atom->sched_out_time = timestamp;
959
mingo39aeb522009-09-14 20:04:48 +0200960 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200961 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200962 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200963 }
964
mingo39aeb522009-09-14 20:04:48 +0200965 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300966 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200967}
968
969static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300970add_runtime_event(struct work_atoms *atoms, u64 delta,
971 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200972{
973 struct work_atom *atom;
974
975 BUG_ON(list_empty(&atoms->work_list));
976
977 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
978
979 atom->runtime += delta;
980 atoms->total_runtime += delta;
981}
982
983static void
984add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200985{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200986 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200987 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988
mingo39aeb522009-09-14 20:04:48 +0200989 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200990 return;
991
mingo39aeb522009-09-14 20:04:48 +0200992 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200993
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200994 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200995 return;
996
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200997 if (timestamp < atom->wake_up_time) {
998 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200999 return;
1000 }
1001
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001002 atom->state = THREAD_SCHED_IN;
1003 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001004
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001005 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001006 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001007 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001008 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001009 atoms->max_lat_at = timestamp;
1010 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001011 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001012}
1013
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001014static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001015latency_switch_event(struct perf_sched *sched,
1016 struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001017 struct machine *machine,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001018 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001019 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001020{
mingo39aeb522009-09-14 20:04:48 +02001021 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001022 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001023 u64 timestamp0, timestamp = sample->time;
1024 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001025 s64 delta;
1026
mingo39aeb522009-09-14 20:04:48 +02001027 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001028
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001029 timestamp0 = sched->cpu_last_switched[cpu];
1030 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001031 if (timestamp0)
1032 delta = timestamp - timestamp0;
1033 else
1034 delta = 0;
1035
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001036 if (delta < 0) {
1037 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1038 return -1;
1039 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001040
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001041 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1042 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001044 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001045 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001046 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001047 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001048 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001049 if (!out_events) {
1050 pr_err("out-event: Internal tree error");
1051 return -1;
1052 }
mingo39aeb522009-09-14 20:04:48 +02001053 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001054 if (add_sched_out_event(out_events, sched_out_state(switch_event), timestamp))
1055 return -1;
mingo39aeb522009-09-14 20:04:48 +02001056
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001057 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001058 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001059 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001060 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001061 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001062 if (!in_events) {
1063 pr_err("in-event: Internal tree error");
1064 return -1;
1065 }
mingo39aeb522009-09-14 20:04:48 +02001066 /*
1067 * Take came in we have not heard about yet,
1068 * add in an initial atom in runnable state:
1069 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001070 if (add_sched_out_event(in_events, 'R', timestamp))
1071 return -1;
mingo39aeb522009-09-14 20:04:48 +02001072 }
1073 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001074
1075 return 0;
mingo39aeb522009-09-14 20:04:48 +02001076}
1077
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001078static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001079latency_runtime_event(struct perf_sched *sched,
1080 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 Melo0e9b07e2012-09-11 17:29:27 -03001084 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->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 Melo0e9b07e2012-09-11 17:29:27 -03001090 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001091 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->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
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001106latency_wakeup_event(struct perf_sched *sched,
1107 struct trace_wakeup_event *wakeup_event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001108 struct machine *machine,
1109 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001110 struct perf_sample *sample)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001111{
mingo39aeb522009-09-14 20:04:48 +02001112 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001113 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001114 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001115 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001116
1117 /* Note for later, it may be interesting to observe the failing cases */
1118 if (!wakeup_event->success)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001119 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001121 wakee = machine__findnew_thread(machine, wakeup_event->pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001122 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001123 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001124 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001125 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001126 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001127 if (!atoms) {
1128 pr_debug("wakeup-event: Internal tree error");
1129 return -1;
1130 }
1131 if (add_sched_out_event(atoms, 'S', timestamp))
1132 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001133 }
1134
mingo39aeb522009-09-14 20:04:48 +02001135 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001136
mingo39aeb522009-09-14 20:04:48 +02001137 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001138
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001139 /*
1140 * You WILL be missing events if you've recorded only
1141 * one CPU, or are only looking at only one, so don't
1142 * make useless noise.
1143 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001144 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1145 sched->nr_state_machine_bugs++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001146
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001147 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001148 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001149 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001150 return 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001151 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001152
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001153 atom->state = THREAD_WAIT_CPU;
1154 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001155 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001156}
1157
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001158static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159latency_migrate_task_event(struct perf_sched *sched,
1160 struct trace_migrate_task_event *migrate_task_event,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001161 struct machine *machine, struct perf_sample *sample)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001162{
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001163 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001164 struct work_atoms *atoms;
1165 struct work_atom *atom;
1166 struct thread *migrant;
1167
1168 /*
1169 * Only need to worry about migration when profiling one CPU.
1170 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001171 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001172 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001173
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001174 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001175 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001176 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001177 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001178 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001179 register_pid(sched, migrant->pid, migrant->comm);
1180 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001181 if (!atoms) {
1182 pr_debug("migration-event: Internal tree error");
1183 return -1;
1184 }
1185 if (add_sched_out_event(atoms, 'R', timestamp))
1186 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001187 }
1188
1189 BUG_ON(list_empty(&atoms->work_list));
1190
1191 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1192 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1193
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001194 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001195
1196 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001197 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001198
1199 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001200}
1201
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001202static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001204 int i;
1205 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001206 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001207
mingo39aeb522009-09-14 20:04:48 +02001208 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001210 /*
1211 * Ignore idle threads:
1212 */
Ingo Molnar80ed0982009-09-16 14:12:36 +02001213 if (!strcmp(work_list->thread->comm, "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001214 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001215
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001216 sched->all_runtime += work_list->total_runtime;
1217 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001218
Ingo Molnar80ed0982009-09-16 14:12:36 +02001219 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001220
mingo08f69e62009-09-14 18:30:44 +02001221 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001222 printf(" ");
1223
mingo39aeb522009-09-14 20:04:48 +02001224 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001225
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001226 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 +02001227 (double)work_list->total_runtime / 1e6,
1228 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001229 (double)work_list->max_lat / 1e6,
1230 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001231}
1232
mingo39aeb522009-09-14 20:04:48 +02001233static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001234{
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001235 if (l->thread->pid < r->thread->pid)
1236 return -1;
1237 if (l->thread->pid > r->thread->pid)
1238 return 1;
1239
1240 return 0;
1241}
1242
mingo39aeb522009-09-14 20:04:48 +02001243static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001244{
1245 u64 avgl, avgr;
1246
1247 if (!l->nb_atoms)
1248 return -1;
1249
1250 if (!r->nb_atoms)
1251 return 1;
1252
1253 avgl = l->total_lat / l->nb_atoms;
1254 avgr = r->total_lat / r->nb_atoms;
1255
1256 if (avgl < avgr)
1257 return -1;
1258 if (avgl > avgr)
1259 return 1;
1260
1261 return 0;
1262}
1263
mingo39aeb522009-09-14 20:04:48 +02001264static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265{
1266 if (l->max_lat < r->max_lat)
1267 return -1;
1268 if (l->max_lat > r->max_lat)
1269 return 1;
1270
1271 return 0;
1272}
1273
mingo39aeb522009-09-14 20:04:48 +02001274static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001275{
1276 if (l->nb_atoms < r->nb_atoms)
1277 return -1;
1278 if (l->nb_atoms > r->nb_atoms)
1279 return 1;
1280
1281 return 0;
1282}
1283
mingo39aeb522009-09-14 20:04:48 +02001284static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001285{
1286 if (l->total_runtime < r->total_runtime)
1287 return -1;
1288 if (l->total_runtime > r->total_runtime)
1289 return 1;
1290
1291 return 0;
1292}
1293
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001294static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001295{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001296 size_t i;
1297 static struct sort_dimension avg_sort_dimension = {
1298 .name = "avg",
1299 .cmp = avg_cmp,
1300 };
1301 static struct sort_dimension max_sort_dimension = {
1302 .name = "max",
1303 .cmp = max_cmp,
1304 };
1305 static struct sort_dimension pid_sort_dimension = {
1306 .name = "pid",
1307 .cmp = pid_cmp,
1308 };
1309 static struct sort_dimension runtime_sort_dimension = {
1310 .name = "runtime",
1311 .cmp = runtime_cmp,
1312 };
1313 static struct sort_dimension switch_sort_dimension = {
1314 .name = "switch",
1315 .cmp = switch_cmp,
1316 };
1317 struct sort_dimension *available_sorts[] = {
1318 &pid_sort_dimension,
1319 &avg_sort_dimension,
1320 &max_sort_dimension,
1321 &switch_sort_dimension,
1322 &runtime_sort_dimension,
1323 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001324
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001325 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001326 if (!strcmp(available_sorts[i]->name, tok)) {
1327 list_add_tail(&available_sorts[i]->list, list);
1328
1329 return 0;
1330 }
1331 }
1332
1333 return -1;
1334}
1335
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001336static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001337{
1338 struct rb_node *node;
1339
1340 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001341 struct work_atoms *data;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001342 node = rb_first(&sched->atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001343 if (!node)
1344 break;
1345
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001346 rb_erase(node, &sched->atom_root);
mingo39aeb522009-09-14 20:04:48 +02001347 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001348 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001349 }
1350}
1351
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001352static int process_sched_wakeup_event(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001353 struct event_format *event,
1354 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001355 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001356{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001357 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001358 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001359 struct trace_wakeup_event wakeup_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001360 int err = 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001361
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001362 FILL_COMMON_FIELDS(wakeup_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001363
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001364 FILL_ARRAY(wakeup_event, comm, event, data);
1365 FILL_FIELD(wakeup_event, pid, event, data);
1366 FILL_FIELD(wakeup_event, prio, event, data);
1367 FILL_FIELD(wakeup_event, success, event, data);
1368 FILL_FIELD(wakeup_event, cpu, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001369
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001370 if (sched->tp_handler->wakeup_event)
1371 err = sched->tp_handler->wakeup_event(sched, &wakeup_event, machine, event, sample);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001372
1373 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001374}
1375
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001376static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001377map_switch_event(struct perf_sched *sched,
1378 struct trace_switch_event *switch_event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001379 struct machine *machine,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001380 struct event_format *event __maybe_unused,
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001381 struct perf_sample *sample)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001382{
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001383 struct thread *sched_out __maybe_unused, *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001384 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001385 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001386 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001387 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001388
1389 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1390
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001391 if (this_cpu > sched->max_cpu)
1392 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001393
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001394 timestamp0 = sched->cpu_last_switched[this_cpu];
1395 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001396 if (timestamp0)
1397 delta = timestamp - timestamp0;
1398 else
1399 delta = 0;
1400
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001401 if (delta < 0) {
1402 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1403 return -1;
1404 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001405
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001406 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1407 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001408
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001409 sched->curr_thread[this_cpu] = sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001410
1411 printf(" ");
1412
1413 new_shortname = 0;
1414 if (!sched_in->shortname[0]) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001415 sched_in->shortname[0] = sched->next_shortname1;
1416 sched_in->shortname[1] = sched->next_shortname2;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001417
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001418 if (sched->next_shortname1 < 'Z') {
1419 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001420 } else {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001421 sched->next_shortname1='A';
1422 if (sched->next_shortname2 < '9') {
1423 sched->next_shortname2++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001424 } else {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001425 sched->next_shortname2='0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001426 }
1427 }
1428 new_shortname = 1;
1429 }
1430
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001431 for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432 if (cpu != this_cpu)
1433 printf(" ");
1434 else
1435 printf("*");
1436
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001437 if (sched->curr_thread[cpu]) {
1438 if (sched->curr_thread[cpu]->pid)
1439 printf("%2s ", sched->curr_thread[cpu]->shortname);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001440 else
1441 printf(". ");
1442 } else
1443 printf(" ");
1444 }
1445
1446 printf(" %12.6f secs ", (double)timestamp/1e9);
1447 if (new_shortname) {
1448 printf("%s => %s:%d\n",
1449 sched_in->shortname, sched_in->comm, sched_in->pid);
1450 } else {
1451 printf("\n");
1452 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001453
1454 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001455}
1456
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001457static int process_sched_switch_event(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001458 struct event_format *event,
1459 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001460 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001461{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001462 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001463 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001464 void *data = sample->raw_data;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001465 struct trace_switch_event switch_event;
1466
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001467 FILL_COMMON_FIELDS(switch_event, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001468
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001469 FILL_ARRAY(switch_event, prev_comm, event, data);
1470 FILL_FIELD(switch_event, prev_pid, event, data);
1471 FILL_FIELD(switch_event, prev_prio, event, data);
1472 FILL_FIELD(switch_event, prev_state, event, data);
1473 FILL_ARRAY(switch_event, next_comm, event, data);
1474 FILL_FIELD(switch_event, next_pid, event, data);
1475 FILL_FIELD(switch_event, next_prio, event, data);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001477 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001478 /*
1479 * Are we trying to switch away a PID that is
1480 * not current?
1481 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001482 if (sched->curr_pid[this_cpu] != switch_event.prev_pid)
1483 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001484 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001485 if (sched->tp_handler->switch_event)
1486 err = sched->tp_handler->switch_event(sched, &switch_event, machine, event, sample);
Ingo Molnarc8a37752009-09-16 14:07:00 +02001487
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001488 sched->curr_pid[this_cpu] = switch_event.next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001489 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001490}
1491
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001492static int process_sched_runtime_event(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001493 struct event_format *event,
1494 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001495 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001496{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001497 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001498 void *data = sample->raw_data;
mingo39aeb522009-09-14 20:04:48 +02001499 struct trace_runtime_event runtime_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001500 int err = 0;
mingo39aeb522009-09-14 20:04:48 +02001501
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001502 FILL_ARRAY(runtime_event, comm, event, data);
1503 FILL_FIELD(runtime_event, pid, event, data);
1504 FILL_FIELD(runtime_event, runtime, event, data);
1505 FILL_FIELD(runtime_event, vruntime, event, data);
mingo39aeb522009-09-14 20:04:48 +02001506
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001507 if (sched->tp_handler->runtime_event)
1508 err = sched->tp_handler->runtime_event(sched, &runtime_event, machine, sample);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001509
1510 return err;
mingo39aeb522009-09-14 20:04:48 +02001511}
1512
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001513static int process_sched_fork_event(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001514 struct event_format *event,
1515 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001516 struct machine *machine __maybe_unused)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001517{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001518 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001519 void *data = sample->raw_data;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001520 struct trace_fork_event fork_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001521 int err = 0;
Frederic Weisbecker46538812009-09-12 02:43:45 +02001522
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001523 FILL_COMMON_FIELDS(fork_event, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001524
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001525 FILL_ARRAY(fork_event, parent_comm, event, data);
1526 FILL_FIELD(fork_event, parent_pid, event, data);
1527 FILL_ARRAY(fork_event, child_comm, event, data);
1528 FILL_FIELD(fork_event, child_pid, event, data);
Frederic Weisbecker46538812009-09-12 02:43:45 +02001529
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001530 if (sched->tp_handler->fork_event)
1531 err = sched->tp_handler->fork_event(sched, &fork_event, event);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001532
1533 return err;
Ingo Molnarfbf94822009-09-11 12:12:54 +02001534}
1535
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001536static int process_sched_exit_event(struct perf_tool *tool __maybe_unused,
1537 struct event_format *event,
1538 struct perf_sample *sample __maybe_unused,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001539 struct machine *machine __maybe_unused)
Ingo Molnarfbf94822009-09-11 12:12:54 +02001540{
Ingo Molnarad236fd2009-09-11 12:12:54 +02001541 if (verbose)
1542 printf("sched_exit event %p\n", event);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001543
1544 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001545}
1546
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001547static int process_sched_migrate_task_event(struct perf_tool *tool,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001548 struct event_format *event,
1549 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001550 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001551{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001552 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001553 void *data = sample->raw_data;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001554 struct trace_migrate_task_event migrate_task_event;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001555 int err = 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001556
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001557 FILL_COMMON_FIELDS(migrate_task_event, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001558
Xiao Guangrongf48f6692009-12-07 13:04:04 +08001559 FILL_ARRAY(migrate_task_event, comm, event, data);
1560 FILL_FIELD(migrate_task_event, pid, event, data);
1561 FILL_FIELD(migrate_task_event, prio, event, data);
1562 FILL_FIELD(migrate_task_event, cpu, event, data);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001563
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001564 if (sched->tp_handler->migrate_task_event)
1565 err = sched->tp_handler->migrate_task_event(sched, &migrate_task_event, machine, sample);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001566
1567 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001568}
1569
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001570typedef int (*tracepoint_handler)(struct perf_tool *tool,
1571 struct event_format *tp_format,
1572 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001573 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001574
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001575static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1576 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001577 struct perf_sample *sample,
1578 struct perf_evsel *evsel,
1579 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001580{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001581 struct thread *thread = machine__findnew_thread(machine, sample->pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001582 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001583
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001584 if (thread == NULL) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001585 pr_debug("problem processing %s event, skipping it.\n",
Arnaldo Carvalho de Melo22c8b842012-06-12 13:55:13 -03001586 perf_evsel__name(evsel));
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001587 return -1;
1588 }
1589
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001590 evsel->hists.stats.total_period += sample->period;
1591 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
Julia Lawallf39cdf22009-10-17 08:43:17 +02001592
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001593 if (evsel->handler.func != NULL) {
1594 tracepoint_handler f = evsel->handler.func;
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001595 err = f(tool, evsel->tp_format, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001596 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001597
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001598 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001599}
1600
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001601static int perf_sched__read_events(struct perf_sched *sched, bool destroy,
1602 struct perf_session **psession)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001603{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001604 const struct perf_evsel_str_handler handlers[] = {
1605 { "sched:sched_switch", process_sched_switch_event, },
1606 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1607 { "sched:sched_wakeup", process_sched_wakeup_event, },
1608 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1609 { "sched:sched_process_fork", process_sched_fork_event, },
1610 { "sched:sched_process_exit", process_sched_exit_event, },
1611 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1612 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001613 struct perf_session *session;
1614
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001615 session = perf_session__new(sched->input_name, O_RDONLY, 0, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001616 if (session == NULL) {
1617 pr_debug("No Memory for session\n");
1618 return -1;
1619 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001620
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001621 if (perf_session__set_tracepoints_handlers(session, handlers))
1622 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001623
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001624 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001625 int err = perf_session__process_events(session, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001626 if (err) {
1627 pr_err("Failed to process events, error %d", err);
1628 goto out_delete;
1629 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001630
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001631 sched->nr_events = session->hists.stats.nr_events[0];
1632 sched->nr_lost_events = session->hists.stats.total_lost;
1633 sched->nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001634 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001635
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001636 if (destroy)
1637 perf_session__delete(session);
1638
1639 if (psession)
1640 *psession = session;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001641
1642 return 0;
1643
1644out_delete:
1645 perf_session__delete(session);
1646 return -1;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001647}
1648
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001649static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001650{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001651 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001652 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001653 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1654 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001655 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001656 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001657 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001658 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1659 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001660 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001661 if (sched->nr_state_machine_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001662 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001663 (double)sched->nr_state_machine_bugs/(double)sched->nr_timestamps*100.0,
1664 sched->nr_state_machine_bugs, sched->nr_timestamps);
1665 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001666 printf(" (due to lost events?)");
1667 printf("\n");
1668 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001669 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001670 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001671 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1672 sched->nr_context_switch_bugs, sched->nr_timestamps);
1673 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001674 printf(" (due to lost events?)");
1675 printf("\n");
1676 }
1677}
1678
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001679static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001680{
1681 struct rb_node *next;
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001682 struct perf_session *session;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001683
1684 setup_pager();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001685 if (perf_sched__read_events(sched, false, &session))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001686 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001687 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001688
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001689 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1690 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1691 printf(" ---------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001692
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001693 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001694
1695 while (next) {
1696 struct work_atoms *work_list;
1697
1698 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001699 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001700 next = rb_next(next);
1701 }
1702
1703 printf(" -----------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001704 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001705 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001706
1707 printf(" ---------------------------------------------------\n");
1708
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001709 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001710 printf("\n");
1711
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001712 perf_session__delete(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001713 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001714}
1715
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001716static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001717{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001718 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
Ingo Molnar40749d02009-09-17 18:24:55 +02001719
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001720 setup_pager();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001721 if (perf_sched__read_events(sched, true, NULL))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001722 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001723 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001724 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001725}
1726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001727static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001728{
1729 unsigned long i;
1730
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001731 calibrate_run_measurement_overhead(sched);
1732 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001734 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001735
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001736 if (perf_sched__read_events(sched, true, NULL))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001737 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001738
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001739 printf("nr_run_events: %ld\n", sched->nr_run_events);
1740 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1741 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001742
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001743 if (sched->targetless_wakeups)
1744 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1745 if (sched->multitarget_wakeups)
1746 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1747 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001748 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001749 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001750
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001751 print_task_traces(sched);
1752 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001753
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001754 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001755 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001756 for (i = 0; i < sched->replay_repeat; i++)
1757 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001758
1759 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001760}
1761
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001762static void setup_sorting(struct perf_sched *sched, const struct option *options,
1763 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001764{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001765 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001766
1767 for (tok = strtok_r(str, ", ", &tmp);
1768 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001769 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001770 error("Unknown --sort key: `%s'", tok);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001771 usage_with_options(usage_msg, options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001772 }
1773 }
1774
1775 free(str);
1776
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001777 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001778}
1779
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001780static int __cmd_record(int argc, const char **argv)
1781{
1782 unsigned int rec_argc, i, j;
1783 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001784 const char * const record_args[] = {
1785 "record",
1786 "-a",
1787 "-R",
1788 "-f",
1789 "-m", "1024",
1790 "-c", "1",
1791 "-e", "sched:sched_switch",
1792 "-e", "sched:sched_stat_wait",
1793 "-e", "sched:sched_stat_sleep",
1794 "-e", "sched:sched_stat_iowait",
1795 "-e", "sched:sched_stat_runtime",
1796 "-e", "sched:sched_process_exit",
1797 "-e", "sched:sched_process_fork",
1798 "-e", "sched:sched_wakeup",
1799 "-e", "sched:sched_migrate_task",
1800 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001801
1802 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1803 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1804
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001805 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001806 return -ENOMEM;
1807
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001808 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1809 rec_argv[i] = strdup(record_args[i]);
1810
1811 for (j = 1; j < (unsigned int)argc; j++, i++)
1812 rec_argv[i] = argv[j];
1813
1814 BUG_ON(i != rec_argc);
1815
1816 return cmd_record(i, rec_argv, NULL);
1817}
1818
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001819int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001820{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001821 const char default_sort_order[] = "avg, max, switch, runtime";
1822 struct perf_sched sched = {
1823 .tool = {
1824 .sample = perf_sched__process_tracepoint_sample,
1825 .comm = perf_event__process_comm,
1826 .lost = perf_event__process_lost,
1827 .fork = perf_event__process_task,
1828 .ordered_samples = true,
1829 },
1830 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1831 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1832 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1833 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
1834 .curr_pid = { [0 ... MAX_CPUS - 1] = -1 },
1835 .sort_order = default_sort_order,
1836 .replay_repeat = 10,
1837 .profile_cpu = -1,
1838 .next_shortname1 = 'A',
1839 .next_shortname2 = '0',
1840 };
1841 const struct option latency_options[] = {
1842 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1843 "sort by key(s): runtime, switch, avg, max"),
1844 OPT_INCR('v', "verbose", &verbose,
1845 "be more verbose (show symbol address, etc)"),
1846 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1847 "CPU to profile on"),
1848 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1849 "dump raw trace in ASCII"),
1850 OPT_END()
1851 };
1852 const struct option replay_options[] = {
1853 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1854 "repeat the workload replay N times (-1: infinite)"),
1855 OPT_INCR('v', "verbose", &verbose,
1856 "be more verbose (show symbol address, etc)"),
1857 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1858 "dump raw trace in ASCII"),
1859 OPT_END()
1860 };
1861 const struct option sched_options[] = {
1862 OPT_STRING('i', "input", &sched.input_name, "file",
1863 "input file name"),
1864 OPT_INCR('v', "verbose", &verbose,
1865 "be more verbose (show symbol address, etc)"),
1866 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1867 "dump raw trace in ASCII"),
1868 OPT_END()
1869 };
1870 const char * const latency_usage[] = {
1871 "perf sched latency [<options>]",
1872 NULL
1873 };
1874 const char * const replay_usage[] = {
1875 "perf sched replay [<options>]",
1876 NULL
1877 };
1878 const char * const sched_usage[] = {
1879 "perf sched [<options>] {record|latency|map|replay|script}",
1880 NULL
1881 };
1882 struct trace_sched_handler lat_ops = {
1883 .wakeup_event = latency_wakeup_event,
1884 .switch_event = latency_switch_event,
1885 .runtime_event = latency_runtime_event,
1886 .fork_event = latency_fork_event,
1887 .migrate_task_event = latency_migrate_task_event,
1888 };
1889 struct trace_sched_handler map_ops = {
1890 .switch_event = map_switch_event,
1891 };
1892 struct trace_sched_handler replay_ops = {
1893 .wakeup_event = replay_wakeup_event,
1894 .switch_event = replay_switch_event,
1895 .fork_event = replay_fork_event,
1896 };
1897
Ingo Molnarf2858d82009-09-11 12:12:54 +02001898 argc = parse_options(argc, argv, sched_options, sched_usage,
1899 PARSE_OPT_STOP_AT_NON_OPTION);
1900 if (!argc)
1901 usage_with_options(sched_usage, sched_options);
1902
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001903 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001904 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001905 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001906 if (!strcmp(argv[0], "script"))
1907 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001908
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001909 symbol__init();
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001910 if (!strncmp(argv[0], "rec", 3)) {
1911 return __cmd_record(argc, argv);
1912 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001913 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001914 if (argc > 1) {
1915 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1916 if (argc)
1917 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001918 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001919 setup_sorting(&sched, latency_options, latency_usage);
1920 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001921 } else if (!strcmp(argv[0], "map")) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001922 sched.tp_handler = &map_ops;
1923 setup_sorting(&sched, latency_options, latency_usage);
1924 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001925 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001926 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001927 if (argc) {
1928 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1929 if (argc)
1930 usage_with_options(replay_usage, replay_options);
1931 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001932 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001933 } else {
1934 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001935 }
1936
Ingo Molnarec156762009-09-11 12:12:54 +02001937 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001938}