blob: 3261300c08f04bcb9d267fda7f57edddb2737df9 [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"
Yann Droneaud57480d22014-06-30 22:28:47 +020013#include "util/cloexec.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020014
15#include "util/parse-options.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020016#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020017
Ingo Molnar0a02ad92009-09-11 12:12:54 +020018#include "util/debug.h"
19
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020020#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020021#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020022
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020023#include <semaphore.h>
24#include <pthread.h>
25#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080026#include <api/fs/fs.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020027
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020028#define PR_SET_NAME 15 /* Set process name */
29#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020030#define COMM_LEN 20
31#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080032#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020033
mingo39aeb522009-09-14 20:04:48 +020034struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020035
36struct task_desc {
37 unsigned long nr;
38 unsigned long pid;
39 char comm[COMM_LEN];
40
41 unsigned long nr_events;
42 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020043 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020044
45 pthread_t thread;
46 sem_t sleep_sem;
47
48 sem_t ready_for_work;
49 sem_t work_done_sem;
50
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020051 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020052};
53
54enum sched_event_type {
55 SCHED_EVENT_RUN,
56 SCHED_EVENT_SLEEP,
57 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020058 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020059};
60
mingo39aeb522009-09-14 20:04:48 +020061struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020062 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030063 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020064 u64 timestamp;
65 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020066 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020067 sem_t *wait_sem;
68 struct task_desc *wakee;
69};
70
Dongshenge936e8e2014-05-05 16:05:54 +090071#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020072
73enum thread_state {
74 THREAD_SLEEPING = 0,
75 THREAD_WAIT_CPU,
76 THREAD_SCHED_IN,
77 THREAD_IGNORE
78};
79
80struct work_atom {
81 struct list_head list;
82 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020083 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020084 u64 wake_up_time;
85 u64 sched_in_time;
86 u64 runtime;
87};
88
mingo39aeb522009-09-14 20:04:48 +020089struct work_atoms {
90 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020091 struct thread *thread;
92 struct rb_node node;
93 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +010094 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020095 u64 total_lat;
96 u64 nb_atoms;
97 u64 total_runtime;
98};
99
mingo39aeb522009-09-14 20:04:48 +0200100typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200101
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300102struct perf_sched;
103
104struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300105 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
106 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300107
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300108 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
109 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300110
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300111 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
112 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300113
David Aherncb627502013-08-07 22:50:47 -0400114 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
115 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
116 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300117
118 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300119 struct perf_evsel *evsel,
120 struct perf_sample *sample,
121 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300122};
123
124struct perf_sched {
125 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300126 const char *sort_order;
127 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800128 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300129 struct task_desc **tasks;
130 const struct trace_sched_handler *tp_handler;
131 pthread_mutex_t start_work_mutex;
132 pthread_mutex_t work_done_wait_mutex;
133 int profile_cpu;
134/*
135 * Track the current task - that way we can know whether there's any
136 * weird events, such as a task being switched away that is not current.
137 */
138 int max_cpu;
139 u32 curr_pid[MAX_CPUS];
140 struct thread *curr_thread[MAX_CPUS];
141 char next_shortname1;
142 char next_shortname2;
143 unsigned int replay_repeat;
144 unsigned long nr_run_events;
145 unsigned long nr_sleep_events;
146 unsigned long nr_wakeup_events;
147 unsigned long nr_sleep_corrections;
148 unsigned long nr_run_events_optimized;
149 unsigned long targetless_wakeups;
150 unsigned long multitarget_wakeups;
151 unsigned long nr_runs;
152 unsigned long nr_timestamps;
153 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300154 unsigned long nr_context_switch_bugs;
155 unsigned long nr_events;
156 unsigned long nr_lost_chunks;
157 unsigned long nr_lost_events;
158 u64 run_measurement_overhead;
159 u64 sleep_measurement_overhead;
160 u64 start_time;
161 u64 cpu_usage;
162 u64 runavg_cpu_usage;
163 u64 parent_cpu_usage;
164 u64 runavg_parent_cpu_usage;
165 u64 sum_runtime;
166 u64 sum_fluct;
167 u64 run_avg;
168 u64 all_runtime;
169 u64 all_count;
170 u64 cpu_last_switched[MAX_CPUS];
171 struct rb_root atom_root, sorted_atom_root;
172 struct list_head sort_list, cmp_pid;
173};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200174
175static u64 get_nsecs(void)
176{
177 struct timespec ts;
178
179 clock_gettime(CLOCK_MONOTONIC, &ts);
180
181 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
182}
183
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300184static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200185{
186 u64 T0 = get_nsecs(), T1;
187
188 do {
189 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300190 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200191}
192
193static void sleep_nsecs(u64 nsecs)
194{
195 struct timespec ts;
196
197 ts.tv_nsec = nsecs % 999999999;
198 ts.tv_sec = nsecs / 999999999;
199
200 nanosleep(&ts, NULL);
201}
202
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300203static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200204{
205 u64 T0, T1, delta, min_delta = 1000000000ULL;
206 int i;
207
208 for (i = 0; i < 10; i++) {
209 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300210 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200211 T1 = get_nsecs();
212 delta = T1-T0;
213 min_delta = min(min_delta, delta);
214 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300215 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200216
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200217 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200218}
219
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300220static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200221{
222 u64 T0, T1, delta, min_delta = 1000000000ULL;
223 int i;
224
225 for (i = 0; i < 10; i++) {
226 T0 = get_nsecs();
227 sleep_nsecs(10000);
228 T1 = get_nsecs();
229 delta = T1-T0;
230 min_delta = min(min_delta, delta);
231 }
232 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300233 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200234
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200235 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200236}
237
mingo39aeb522009-09-14 20:04:48 +0200238static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200239get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200240{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200241 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200242 unsigned long idx = task->nr_events;
243 size_t size;
244
245 event->timestamp = timestamp;
246 event->nr = idx;
247
248 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200249 size = sizeof(struct sched_atom *) * task->nr_events;
250 task->atoms = realloc(task->atoms, size);
251 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200252
mingo39aeb522009-09-14 20:04:48 +0200253 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200254
255 return event;
256}
257
mingo39aeb522009-09-14 20:04:48 +0200258static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200259{
260 if (!task->nr_events)
261 return NULL;
262
mingo39aeb522009-09-14 20:04:48 +0200263 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200264}
265
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300266static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
267 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200268{
mingo39aeb522009-09-14 20:04:48 +0200269 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200270
271 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200272 * optimize an existing RUN event by merging this one
273 * to it:
274 */
Ingo Molnarec156762009-09-11 12:12:54 +0200275 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300276 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200277 curr_event->duration += duration;
278 return;
279 }
280
281 event = get_new_event(task, timestamp);
282
283 event->type = SCHED_EVENT_RUN;
284 event->duration = duration;
285
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300286 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200287}
288
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300289static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
290 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200291{
mingo39aeb522009-09-14 20:04:48 +0200292 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200293
294 event = get_new_event(task, timestamp);
295 event->type = SCHED_EVENT_WAKEUP;
296 event->wakee = wakee;
297
298 wakee_event = last_event(wakee);
299 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300300 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200301 return;
302 }
303 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300304 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200305 return;
306 }
307
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200308 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200309 sem_init(wakee_event->wait_sem, 0, 0);
310 wakee_event->specific_wait = 1;
311 event->wait_sem = wakee_event->wait_sem;
312
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300313 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200314}
315
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300316static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
317 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200318{
mingo39aeb522009-09-14 20:04:48 +0200319 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200320
321 event->type = SCHED_EVENT_SLEEP;
322
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300323 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200324}
325
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300326static struct task_desc *register_pid(struct perf_sched *sched,
327 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200328{
329 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800330 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200331
Yunlong Songcb06ac22015-03-31 21:46:30 +0800332 if (sched->pid_to_task == NULL) {
333 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
334 pid_max = MAX_PID;
335 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
336 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800337 if (pid >= (unsigned long)pid_max) {
338 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
339 sizeof(struct task_desc *))) == NULL);
340 while (pid >= (unsigned long)pid_max)
341 sched->pid_to_task[pid_max++] = NULL;
342 }
Ingo Molnarec156762009-09-11 12:12:54 +0200343
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300344 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200345
346 if (task)
347 return task;
348
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200349 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200350 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300351 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200352 strcpy(task->comm, comm);
353 /*
354 * every task starts in sleeping state - this gets ignored
355 * if there's no wakeup pointing to this sleep state:
356 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300357 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200358
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300359 sched->pid_to_task[pid] = task;
360 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800361 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300362 BUG_ON(!sched->tasks);
363 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200364
Ingo Molnarad236fd2009-09-11 12:12:54 +0200365 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300366 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200367
368 return task;
369}
370
371
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300372static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200373{
374 struct task_desc *task;
375 unsigned long i;
376
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300377 for (i = 0; i < sched->nr_tasks; i++) {
378 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200379 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200380 task->nr, task->comm, task->pid, task->nr_events);
381 }
382}
383
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300384static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200385{
386 struct task_desc *task1, *task2;
387 unsigned long i, j;
388
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300389 for (i = 0; i < sched->nr_tasks; i++) {
390 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200391 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300392 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200393 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300394 task2 = sched->tasks[j];
395 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200396 }
397}
398
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300399static void perf_sched__process_event(struct perf_sched *sched,
400 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200401{
402 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200403
mingo39aeb522009-09-14 20:04:48 +0200404 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200405 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300406 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200407 break;
408 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200409 if (atom->wait_sem)
410 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200411 BUG_ON(ret);
412 break;
413 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200414 if (atom->wait_sem)
415 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200416 BUG_ON(ret);
417 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200418 case SCHED_EVENT_MIGRATION:
419 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200420 default:
421 BUG_ON(1);
422 }
423}
424
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200425static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200426{
427 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200428 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200429 int err;
430
431 err = getrusage(RUSAGE_SELF, &ru);
432 BUG_ON(err);
433
434 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
435 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
436
437 return sum;
438}
439
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800440static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200441{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800442 struct perf_event_attr attr;
Masami Hiramatsufb74fbd2014-08-14 02:22:47 +0000443 char sbuf[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800444 int fd;
445
446 memset(&attr, 0, sizeof(attr));
447
448 attr.type = PERF_TYPE_SOFTWARE;
449 attr.config = PERF_COUNT_SW_TASK_CLOCK;
450
Yann Droneaud57480d22014-06-30 22:28:47 +0200451 fd = sys_perf_event_open(&attr, 0, -1, -1,
452 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800453
Yunlong Song1aff59b2015-03-31 21:46:33 +0800454 if (fd < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900455 pr_err("Error: sys_perf_event_open() syscall returned "
Masami Hiramatsufb74fbd2014-08-14 02:22:47 +0000456 "with %d (%s)\n", fd,
457 strerror_r(errno, sbuf, sizeof(sbuf)));
Yunlong Song1aff59b2015-03-31 21:46:33 +0800458 exit(EXIT_FAILURE);
459 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800460 return fd;
461}
462
463static u64 get_cpu_usage_nsec_self(int fd)
464{
465 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200466 int ret;
467
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800468 ret = read(fd, &runtime, sizeof(runtime));
469 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200470
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800471 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200472}
473
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300474struct sched_thread_parms {
475 struct task_desc *task;
476 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800477 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300478};
479
Ingo Molnarec156762009-09-11 12:12:54 +0200480static void *thread_func(void *ctx)
481{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300482 struct sched_thread_parms *parms = ctx;
483 struct task_desc *this_task = parms->task;
484 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200485 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200486 unsigned long i, ret;
487 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800488 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200489
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300490 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300491
Ingo Molnarec156762009-09-11 12:12:54 +0200492 sprintf(comm2, ":%s", this_task->comm);
493 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300494 if (fd < 0)
495 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200496again:
497 ret = sem_post(&this_task->ready_for_work);
498 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300499 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200500 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300501 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200502 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200503
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800504 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200505
506 for (i = 0; i < this_task->nr_events; i++) {
507 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300508 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200509 }
510
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800511 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200512 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200513 ret = sem_post(&this_task->work_done_sem);
514 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200515
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300516 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200517 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300518 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200519 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200520
521 goto again;
522}
523
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300524static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200525{
526 struct task_desc *task;
527 pthread_attr_t attr;
528 unsigned long i;
529 int err;
530
531 err = pthread_attr_init(&attr);
532 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200533 err = pthread_attr_setstacksize(&attr,
534 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200535 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300536 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200537 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300538 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200539 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300540 for (i = 0; i < sched->nr_tasks; i++) {
541 struct sched_thread_parms *parms = malloc(sizeof(*parms));
542 BUG_ON(parms == NULL);
543 parms->task = task = sched->tasks[i];
544 parms->sched = sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800545 parms->fd = self_open_counters();
Ingo Molnarec156762009-09-11 12:12:54 +0200546 sem_init(&task->sleep_sem, 0, 0);
547 sem_init(&task->ready_for_work, 0, 0);
548 sem_init(&task->work_done_sem, 0, 0);
549 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300550 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200551 BUG_ON(err);
552 }
553}
554
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300555static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200556{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200557 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200558 struct task_desc *task;
559 unsigned long i, ret;
560
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300561 sched->start_time = get_nsecs();
562 sched->cpu_usage = 0;
563 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200564
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300565 for (i = 0; i < sched->nr_tasks; i++) {
566 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200567 ret = sem_wait(&task->ready_for_work);
568 BUG_ON(ret);
569 sem_init(&task->ready_for_work, 0, 0);
570 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300571 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200572 BUG_ON(ret);
573
574 cpu_usage_0 = get_cpu_usage_nsec_parent();
575
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300576 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200577
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300578 for (i = 0; i < sched->nr_tasks; i++) {
579 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200580 ret = sem_wait(&task->work_done_sem);
581 BUG_ON(ret);
582 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300583 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200584 task->cpu_usage = 0;
585 }
586
587 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300588 if (!sched->runavg_cpu_usage)
589 sched->runavg_cpu_usage = sched->cpu_usage;
590 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200591
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300592 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
593 if (!sched->runavg_parent_cpu_usage)
594 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
595 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
596 sched->parent_cpu_usage)/10;
Ingo Molnarec156762009-09-11 12:12:54 +0200597
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300598 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200599 BUG_ON(ret);
600
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300601 for (i = 0; i < sched->nr_tasks; i++) {
602 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200603 sem_init(&task->sleep_sem, 0, 0);
604 task->curr_event = 0;
605 }
606}
607
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300608static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200609{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500610 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200611
612 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200614 T1 = get_nsecs();
615
616 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300617 sched->sum_runtime += delta;
618 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200619
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300620 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200621 if (delta < avg_delta)
622 fluct = avg_delta - delta;
623 else
624 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300625 sched->sum_fluct += fluct;
626 if (!sched->run_avg)
627 sched->run_avg = delta;
628 sched->run_avg = (sched->run_avg * 9 + delta) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200629
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300630 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200631
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300632 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200633
Ingo Molnarad236fd2009-09-11 12:12:54 +0200634 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300635 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200636
637#if 0
638 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200639 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300640 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200641 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200642 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643 (double)sched->parent_cpu_usage/1e6,
644 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200645#endif
646
Ingo Molnarad236fd2009-09-11 12:12:54 +0200647 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200648
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300649 if (sched->nr_sleep_corrections)
650 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
651 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200652}
653
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300654static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200655{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200656 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200657
658 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300659 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200660 T1 = get_nsecs();
661
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200662 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200663
664 T0 = get_nsecs();
665 sleep_nsecs(1e6);
666 T1 = get_nsecs();
667
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200668 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200669}
670
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300671static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300672replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300673 struct perf_evsel *evsel, struct perf_sample *sample,
674 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200675{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300676 const char *comm = perf_evsel__strval(evsel, sample, "comm");
677 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200678 struct task_desc *waker, *wakee;
679
680 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300681 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200682
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300683 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200684 }
685
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300686 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300687 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200688
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300690 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200691}
692
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300693static int replay_switch_event(struct perf_sched *sched,
694 struct perf_evsel *evsel,
695 struct perf_sample *sample,
696 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200697{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300698 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
699 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
700 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
701 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
702 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300703 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300704 u64 timestamp0, timestamp = sample->time;
705 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200706 s64 delta;
707
Ingo Molnarad236fd2009-09-11 12:12:54 +0200708 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300709 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200710
Ingo Molnarfbf94822009-09-11 12:12:54 +0200711 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300712 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200713
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300714 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200715 if (timestamp0)
716 delta = timestamp - timestamp0;
717 else
718 delta = 0;
719
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300720 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900721 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300722 return -1;
723 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200724
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300725 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
726 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200727
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300728 prev = register_pid(sched, prev_pid, prev_comm);
729 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200730
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300731 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200732
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300733 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300734 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300735
736 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200737}
738
David Aherncb627502013-08-07 22:50:47 -0400739static int replay_fork_event(struct perf_sched *sched,
740 union perf_event *event,
741 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200742{
David Aherncb627502013-08-07 22:50:47 -0400743 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300744
Adrian Hunter314add62013-08-27 11:23:03 +0300745 child = machine__findnew_thread(machine, event->fork.pid,
746 event->fork.tid);
747 parent = machine__findnew_thread(machine, event->fork.ppid,
748 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400749
750 if (child == NULL || parent == NULL) {
751 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
752 child, parent);
753 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200754 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300755
David Aherncb627502013-08-07 22:50:47 -0400756 if (verbose) {
757 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200758 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
759 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400760 }
761
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200762 register_pid(sched, parent->tid, thread__comm_str(parent));
763 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300764 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200765}
766
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200767struct sort_dimension {
768 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200769 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200770 struct list_head list;
771};
772
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200773static int
mingo39aeb522009-09-14 20:04:48 +0200774thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200775{
776 struct sort_dimension *sort;
777 int ret = 0;
778
Ingo Molnarb5fae122009-09-11 12:12:54 +0200779 BUG_ON(list_empty(list));
780
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200781 list_for_each_entry(sort, list, list) {
782 ret = sort->cmp(l, r);
783 if (ret)
784 return ret;
785 }
786
787 return ret;
788}
789
mingo39aeb522009-09-14 20:04:48 +0200790static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200791thread_atoms_search(struct rb_root *root, struct thread *thread,
792 struct list_head *sort_list)
793{
794 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200795 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200796
797 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200798 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200799 int cmp;
800
mingo39aeb522009-09-14 20:04:48 +0200801 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200802
803 cmp = thread_lat_cmp(sort_list, &key, atoms);
804 if (cmp > 0)
805 node = node->rb_left;
806 else if (cmp < 0)
807 node = node->rb_right;
808 else {
809 BUG_ON(thread != atoms->thread);
810 return atoms;
811 }
812 }
813 return NULL;
814}
815
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200816static void
mingo39aeb522009-09-14 20:04:48 +0200817__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200818 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200819{
820 struct rb_node **new = &(root->rb_node), *parent = NULL;
821
822 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200823 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200824 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200825
mingo39aeb522009-09-14 20:04:48 +0200826 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200827 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200828
829 cmp = thread_lat_cmp(sort_list, data, this);
830
831 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200832 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200833 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200834 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200835 }
836
837 rb_link_node(&data->node, parent, new);
838 rb_insert_color(&data->node, root);
839}
840
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300841static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200842{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200843 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300844 if (!atoms) {
845 pr_err("No memory at %s\n", __func__);
846 return -1;
847 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200848
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300849 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200850 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300851 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300852 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200853}
854
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300855static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200856{
857 const char *str = TASK_STATE_TO_CHAR_STR;
858
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300859 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200860}
861
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300862static int
mingo39aeb522009-09-14 20:04:48 +0200863add_sched_out_event(struct work_atoms *atoms,
864 char run_state,
865 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200866{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200867 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300868 if (!atom) {
869 pr_err("Non memory at %s", __func__);
870 return -1;
871 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200872
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200873 atom->sched_out_time = timestamp;
874
mingo39aeb522009-09-14 20:04:48 +0200875 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200876 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200877 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200878 }
879
mingo39aeb522009-09-14 20:04:48 +0200880 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300881 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200882}
883
884static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300885add_runtime_event(struct work_atoms *atoms, u64 delta,
886 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200887{
888 struct work_atom *atom;
889
890 BUG_ON(list_empty(&atoms->work_list));
891
892 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
893
894 atom->runtime += delta;
895 atoms->total_runtime += delta;
896}
897
898static void
899add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200900{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200901 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200902 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200903
mingo39aeb522009-09-14 20:04:48 +0200904 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905 return;
906
mingo39aeb522009-09-14 20:04:48 +0200907 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200908
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200909 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910 return;
911
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200912 if (timestamp < atom->wake_up_time) {
913 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200914 return;
915 }
916
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200917 atom->state = THREAD_SCHED_IN;
918 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200919
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200920 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200921 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100922 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200923 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100924 atoms->max_lat_at = timestamp;
925 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200926 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200927}
928
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300929static int latency_switch_event(struct perf_sched *sched,
930 struct perf_evsel *evsel,
931 struct perf_sample *sample,
932 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300934 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
935 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
936 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200937 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300939 u64 timestamp0, timestamp = sample->time;
940 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200941 s64 delta;
942
mingo39aeb522009-09-14 20:04:48 +0200943 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200944
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300945 timestamp0 = sched->cpu_last_switched[cpu];
946 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200947 if (timestamp0)
948 delta = timestamp - timestamp0;
949 else
950 delta = 0;
951
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300952 if (delta < 0) {
953 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
954 return -1;
955 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200956
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300957 sched_out = machine__findnew_thread(machine, -1, prev_pid);
958 sched_in = machine__findnew_thread(machine, -1, next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200959
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300960 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200961 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300962 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300963 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300964 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300965 if (!out_events) {
966 pr_err("out-event: Internal tree error");
967 return -1;
968 }
mingo39aeb522009-09-14 20:04:48 +0200969 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300970 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300971 return -1;
mingo39aeb522009-09-14 20:04:48 +0200972
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300973 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200974 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300975 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300976 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300977 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300978 if (!in_events) {
979 pr_err("in-event: Internal tree error");
980 return -1;
981 }
mingo39aeb522009-09-14 20:04:48 +0200982 /*
983 * Take came in we have not heard about yet,
984 * add in an initial atom in runnable state:
985 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300986 if (add_sched_out_event(in_events, 'R', timestamp))
987 return -1;
mingo39aeb522009-09-14 20:04:48 +0200988 }
989 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300990
991 return 0;
mingo39aeb522009-09-14 20:04:48 +0200992}
993
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300994static int latency_runtime_event(struct perf_sched *sched,
995 struct perf_evsel *evsel,
996 struct perf_sample *sample,
997 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +0200998{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300999 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1000 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001001 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001002 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001003 u64 timestamp = sample->time;
1004 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +02001005
1006 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001007 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001008 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001009 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001010 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001011 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001012 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001013 return -1;
1014 }
1015 if (add_sched_out_event(atoms, 'R', timestamp))
1016 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001017 }
1018
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001019 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001020 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001021}
1022
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001023static int latency_wakeup_event(struct perf_sched *sched,
1024 struct perf_evsel *evsel,
1025 struct perf_sample *sample,
1026 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001028 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001029 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001030 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001031 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001032 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001033
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001034 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001035 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001036 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001037 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001038 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001039 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001040 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001041 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001042 return -1;
1043 }
1044 if (add_sched_out_event(atoms, 'S', timestamp))
1045 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001046 }
1047
mingo39aeb522009-09-14 20:04:48 +02001048 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001049
mingo39aeb522009-09-14 20:04:48 +02001050 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001051
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001052 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001053 * As we do not guarantee the wakeup event happens when
1054 * task is out of run queue, also may happen when task is
1055 * on run queue and wakeup only change ->state to TASK_RUNNING,
1056 * then we should not set the ->wake_up_time when wake up a
1057 * task which is on run queue.
1058 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001059 * You WILL be missing events if you've recorded only
1060 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001061 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001062 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001063 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Dongsheng Yang67d62592014-05-13 10:38:21 +09001064 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001065
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001066 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001067 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001068 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001069 return 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001070 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001071
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001072 atom->state = THREAD_WAIT_CPU;
1073 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001074 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001075}
1076
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001077static int latency_migrate_task_event(struct perf_sched *sched,
1078 struct perf_evsel *evsel,
1079 struct perf_sample *sample,
1080 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001081{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001082 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001083 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001084 struct work_atoms *atoms;
1085 struct work_atom *atom;
1086 struct thread *migrant;
1087
1088 /*
1089 * Only need to worry about migration when profiling one CPU.
1090 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001091 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001092 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001093
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001094 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001095 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001096 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001097 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001098 return -1;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001099 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001100 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001101 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001102 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001103 return -1;
1104 }
1105 if (add_sched_out_event(atoms, 'R', timestamp))
1106 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001107 }
1108
1109 BUG_ON(list_empty(&atoms->work_list));
1110
1111 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1112 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1113
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001114 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001115
1116 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001117 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001118
1119 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001120}
1121
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001122static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001123{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124 int i;
1125 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001126 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001127
mingo39aeb522009-09-14 20:04:48 +02001128 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001129 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001130 /*
1131 * Ignore idle threads:
1132 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001133 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001134 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001135
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001136 sched->all_runtime += work_list->total_runtime;
1137 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001138
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001139 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001140
mingo08f69e62009-09-14 18:30:44 +02001141 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142 printf(" ");
1143
mingo39aeb522009-09-14 20:04:48 +02001144 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001145
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001146 printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
mingo39aeb522009-09-14 20:04:48 +02001147 (double)work_list->total_runtime / 1e6,
1148 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001149 (double)work_list->max_lat / 1e6,
1150 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001151}
1152
mingo39aeb522009-09-14 20:04:48 +02001153static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001154{
Adrian Hunter38051232013-07-04 16:20:31 +03001155 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001156 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001157 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001158 return 1;
1159
1160 return 0;
1161}
1162
mingo39aeb522009-09-14 20:04:48 +02001163static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001164{
1165 u64 avgl, avgr;
1166
1167 if (!l->nb_atoms)
1168 return -1;
1169
1170 if (!r->nb_atoms)
1171 return 1;
1172
1173 avgl = l->total_lat / l->nb_atoms;
1174 avgr = r->total_lat / r->nb_atoms;
1175
1176 if (avgl < avgr)
1177 return -1;
1178 if (avgl > avgr)
1179 return 1;
1180
1181 return 0;
1182}
1183
mingo39aeb522009-09-14 20:04:48 +02001184static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001185{
1186 if (l->max_lat < r->max_lat)
1187 return -1;
1188 if (l->max_lat > r->max_lat)
1189 return 1;
1190
1191 return 0;
1192}
1193
mingo39aeb522009-09-14 20:04:48 +02001194static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001195{
1196 if (l->nb_atoms < r->nb_atoms)
1197 return -1;
1198 if (l->nb_atoms > r->nb_atoms)
1199 return 1;
1200
1201 return 0;
1202}
1203
mingo39aeb522009-09-14 20:04:48 +02001204static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001205{
1206 if (l->total_runtime < r->total_runtime)
1207 return -1;
1208 if (l->total_runtime > r->total_runtime)
1209 return 1;
1210
1211 return 0;
1212}
1213
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001214static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001215{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001216 size_t i;
1217 static struct sort_dimension avg_sort_dimension = {
1218 .name = "avg",
1219 .cmp = avg_cmp,
1220 };
1221 static struct sort_dimension max_sort_dimension = {
1222 .name = "max",
1223 .cmp = max_cmp,
1224 };
1225 static struct sort_dimension pid_sort_dimension = {
1226 .name = "pid",
1227 .cmp = pid_cmp,
1228 };
1229 static struct sort_dimension runtime_sort_dimension = {
1230 .name = "runtime",
1231 .cmp = runtime_cmp,
1232 };
1233 static struct sort_dimension switch_sort_dimension = {
1234 .name = "switch",
1235 .cmp = switch_cmp,
1236 };
1237 struct sort_dimension *available_sorts[] = {
1238 &pid_sort_dimension,
1239 &avg_sort_dimension,
1240 &max_sort_dimension,
1241 &switch_sort_dimension,
1242 &runtime_sort_dimension,
1243 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001244
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001245 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001246 if (!strcmp(available_sorts[i]->name, tok)) {
1247 list_add_tail(&available_sorts[i]->list, list);
1248
1249 return 0;
1250 }
1251 }
1252
1253 return -1;
1254}
1255
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001256static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001257{
1258 struct rb_node *node;
1259
1260 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001261 struct work_atoms *data;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001262 node = rb_first(&sched->atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001263 if (!node)
1264 break;
1265
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001266 rb_erase(node, &sched->atom_root);
mingo39aeb522009-09-14 20:04:48 +02001267 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001268 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001269 }
1270}
1271
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001272static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001273 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001274 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001275 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001276{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001277 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001278
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001279 if (sched->tp_handler->wakeup_event)
1280 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001281
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001282 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001283}
1284
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001285static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1286 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001287{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001288 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1289 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001290 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001291 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001292 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001293 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001294
1295 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1296
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001297 if (this_cpu > sched->max_cpu)
1298 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001299
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001300 timestamp0 = sched->cpu_last_switched[this_cpu];
1301 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001302 if (timestamp0)
1303 delta = timestamp - timestamp0;
1304 else
1305 delta = 0;
1306
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001307 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001308 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001309 return -1;
1310 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001311
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001312 sched_in = machine__findnew_thread(machine, -1, next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001313
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001314 sched->curr_thread[this_cpu] = sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001315
1316 printf(" ");
1317
1318 new_shortname = 0;
1319 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001320 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1321 /*
1322 * Don't allocate a letter-number for swapper:0
1323 * as a shortname. Instead, we use '.' for it.
1324 */
1325 sched_in->shortname[0] = '.';
1326 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001327 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001328 sched_in->shortname[0] = sched->next_shortname1;
1329 sched_in->shortname[1] = sched->next_shortname2;
1330
1331 if (sched->next_shortname1 < 'Z') {
1332 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001333 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001334 sched->next_shortname1 = 'A';
1335 if (sched->next_shortname2 < '9')
1336 sched->next_shortname2++;
1337 else
1338 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001339 }
1340 }
1341 new_shortname = 1;
1342 }
1343
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001344 for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001345 if (cpu != this_cpu)
1346 printf(" ");
1347 else
1348 printf("*");
1349
Dongsheng6bcab4e2014-05-06 14:39:01 +09001350 if (sched->curr_thread[cpu])
1351 printf("%2s ", sched->curr_thread[cpu]->shortname);
1352 else
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001353 printf(" ");
1354 }
1355
1356 printf(" %12.6f secs ", (double)timestamp/1e9);
1357 if (new_shortname) {
1358 printf("%s => %s:%d\n",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001359 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001360 } else {
1361 printf("\n");
1362 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001363
1364 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001365}
1366
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001367static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001368 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001369 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001370 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001371{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001372 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001373 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001374 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1375 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001376
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001377 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001378 /*
1379 * Are we trying to switch away a PID that is
1380 * not current?
1381 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001382 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001383 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001384 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001385
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001386 if (sched->tp_handler->switch_event)
1387 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001388
1389 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001390 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001391}
1392
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001393static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001394 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001395 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001396 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001397{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001398 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001399
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001400 if (sched->tp_handler->runtime_event)
1401 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001402
1403 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001404}
1405
David Aherncb627502013-08-07 22:50:47 -04001406static int perf_sched__process_fork_event(struct perf_tool *tool,
1407 union perf_event *event,
1408 struct perf_sample *sample,
1409 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001410{
1411 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1412
David Aherncb627502013-08-07 22:50:47 -04001413 /* run the fork event through the perf machineruy */
1414 perf_event__process_fork(tool, event, sample, machine);
1415
1416 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001417 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001418 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001419
1420 return 0;
1421}
1422
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001423static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001424 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001425 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001426 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001427{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001428 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001429
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001430 if (sched->tp_handler->migrate_task_event)
1431 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001432
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001433 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001434}
1435
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001436typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001437 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001438 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001439 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001440
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001441static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1442 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001443 struct perf_sample *sample,
1444 struct perf_evsel *evsel,
1445 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001446{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001447 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001448
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001449 if (evsel->handler != NULL) {
1450 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001451 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001452 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001453
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001454 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001455}
1456
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001457static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001458{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001459 const struct perf_evsel_str_handler handlers[] = {
1460 { "sched:sched_switch", process_sched_switch_event, },
1461 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1462 { "sched:sched_wakeup", process_sched_wakeup_event, },
1463 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001464 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1465 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001466 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001467 struct perf_data_file file = {
1468 .path = input_name,
1469 .mode = PERF_DATA_MODE_READ,
1470 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001471 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001472
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001473 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001474 if (session == NULL) {
1475 pr_debug("No Memory for session\n");
1476 return -1;
1477 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001478
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001479 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001480
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001481 if (perf_session__set_tracepoints_handlers(session, handlers))
1482 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001483
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001484 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001485 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001486 if (err) {
1487 pr_err("Failed to process events, error %d", err);
1488 goto out_delete;
1489 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001490
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001491 sched->nr_events = session->evlist->stats.nr_events[0];
1492 sched->nr_lost_events = session->evlist->stats.total_lost;
1493 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001494 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001495
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001496 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001497out_delete:
1498 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001499 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001500}
1501
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001502static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001503{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001504 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001505 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001506 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1507 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001508 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001509 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001510 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001511 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1512 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001513 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001514 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001515 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001516 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1517 sched->nr_context_switch_bugs, sched->nr_timestamps);
1518 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001519 printf(" (due to lost events?)");
1520 printf("\n");
1521 }
1522}
1523
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001524static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001525{
1526 struct rb_node *next;
1527
1528 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001529
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001530 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001531 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001532
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001533 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001534
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001535 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1536 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1537 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001538
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001539 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001540
1541 while (next) {
1542 struct work_atoms *work_list;
1543
1544 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001545 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001546 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001547 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001548 }
1549
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001550 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001551 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001552 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001553
1554 printf(" ---------------------------------------------------\n");
1555
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001556 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001557 printf("\n");
1558
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001559 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001560}
1561
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001562static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001563{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001564 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
Ingo Molnar40749d02009-09-17 18:24:55 +02001565
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001566 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001567 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001568 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001569 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001570 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001571}
1572
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001573static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001574{
1575 unsigned long i;
1576
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001577 calibrate_run_measurement_overhead(sched);
1578 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001579
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001580 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001581
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001582 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001583 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001584
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001585 printf("nr_run_events: %ld\n", sched->nr_run_events);
1586 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1587 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001588
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001589 if (sched->targetless_wakeups)
1590 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1591 if (sched->multitarget_wakeups)
1592 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1593 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001594 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001595 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001596
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001597 print_task_traces(sched);
1598 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001599
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001600 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001601 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001602 for (i = 0; i < sched->replay_repeat; i++)
1603 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001604
1605 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001606}
1607
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001608static void setup_sorting(struct perf_sched *sched, const struct option *options,
1609 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001610{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001611 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001612
1613 for (tok = strtok_r(str, ", ", &tmp);
1614 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001615 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001616 error("Unknown --sort key: `%s'", tok);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001617 usage_with_options(usage_msg, options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001618 }
1619 }
1620
1621 free(str);
1622
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001623 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001624}
1625
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001626static int __cmd_record(int argc, const char **argv)
1627{
1628 unsigned int rec_argc, i, j;
1629 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001630 const char * const record_args[] = {
1631 "record",
1632 "-a",
1633 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001634 "-m", "1024",
1635 "-c", "1",
1636 "-e", "sched:sched_switch",
1637 "-e", "sched:sched_stat_wait",
1638 "-e", "sched:sched_stat_sleep",
1639 "-e", "sched:sched_stat_iowait",
1640 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001641 "-e", "sched:sched_process_fork",
1642 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001643 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001644 "-e", "sched:sched_migrate_task",
1645 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001646
1647 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1648 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1649
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001650 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001651 return -ENOMEM;
1652
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001653 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1654 rec_argv[i] = strdup(record_args[i]);
1655
1656 for (j = 1; j < (unsigned int)argc; j++, i++)
1657 rec_argv[i] = argv[j];
1658
1659 BUG_ON(i != rec_argc);
1660
1661 return cmd_record(i, rec_argv, NULL);
1662}
1663
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001664int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001665{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001666 const char default_sort_order[] = "avg, max, switch, runtime";
1667 struct perf_sched sched = {
1668 .tool = {
1669 .sample = perf_sched__process_tracepoint_sample,
1670 .comm = perf_event__process_comm,
1671 .lost = perf_event__process_lost,
1672 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001673 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001674 },
1675 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1676 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1677 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1678 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001679 .sort_order = default_sort_order,
1680 .replay_repeat = 10,
1681 .profile_cpu = -1,
1682 .next_shortname1 = 'A',
1683 .next_shortname2 = '0',
1684 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001685 const struct option latency_options[] = {
1686 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1687 "sort by key(s): runtime, switch, avg, max"),
1688 OPT_INCR('v', "verbose", &verbose,
1689 "be more verbose (show symbol address, etc)"),
1690 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1691 "CPU to profile on"),
1692 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1693 "dump raw trace in ASCII"),
1694 OPT_END()
1695 };
1696 const struct option replay_options[] = {
1697 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1698 "repeat the workload replay N times (-1: infinite)"),
1699 OPT_INCR('v', "verbose", &verbose,
1700 "be more verbose (show symbol address, etc)"),
1701 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1702 "dump raw trace in ASCII"),
1703 OPT_END()
1704 };
1705 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001706 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001707 "input file name"),
1708 OPT_INCR('v', "verbose", &verbose,
1709 "be more verbose (show symbol address, etc)"),
1710 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1711 "dump raw trace in ASCII"),
1712 OPT_END()
1713 };
1714 const char * const latency_usage[] = {
1715 "perf sched latency [<options>]",
1716 NULL
1717 };
1718 const char * const replay_usage[] = {
1719 "perf sched replay [<options>]",
1720 NULL
1721 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001722 const char *const sched_subcommands[] = { "record", "latency", "map",
1723 "replay", "script", NULL };
1724 const char *sched_usage[] = {
1725 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001726 NULL
1727 };
1728 struct trace_sched_handler lat_ops = {
1729 .wakeup_event = latency_wakeup_event,
1730 .switch_event = latency_switch_event,
1731 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001732 .migrate_task_event = latency_migrate_task_event,
1733 };
1734 struct trace_sched_handler map_ops = {
1735 .switch_event = map_switch_event,
1736 };
1737 struct trace_sched_handler replay_ops = {
1738 .wakeup_event = replay_wakeup_event,
1739 .switch_event = replay_switch_event,
1740 .fork_event = replay_fork_event,
1741 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001742 unsigned int i;
1743
1744 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1745 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001746
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001747 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1748 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001749 if (!argc)
1750 usage_with_options(sched_usage, sched_options);
1751
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001752 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001753 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001754 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001755 if (!strcmp(argv[0], "script"))
1756 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001757
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001758 if (!strncmp(argv[0], "rec", 3)) {
1759 return __cmd_record(argc, argv);
1760 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001761 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001762 if (argc > 1) {
1763 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1764 if (argc)
1765 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001766 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001767 setup_sorting(&sched, latency_options, latency_usage);
1768 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001769 } else if (!strcmp(argv[0], "map")) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001770 sched.tp_handler = &map_ops;
1771 setup_sorting(&sched, latency_options, latency_usage);
1772 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001773 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001774 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001775 if (argc) {
1776 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1777 if (argc)
1778 usage_with_options(replay_usage, replay_options);
1779 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001780 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001781 } else {
1782 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001783 }
1784
Ingo Molnarec156762009-09-11 12:12:54 +02001785 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001786}