blob: 20d887b222e4073653fc282331b87e41523ce7ca [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 }
337 BUG_ON(pid >= (unsigned long)pid_max);
Ingo Molnarec156762009-09-11 12:12:54 +0200338
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300339 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200340
341 if (task)
342 return task;
343
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200344 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200345 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300346 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200347 strcpy(task->comm, comm);
348 /*
349 * every task starts in sleeping state - this gets ignored
350 * if there's no wakeup pointing to this sleep state:
351 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300352 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200353
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300354 sched->pid_to_task[pid] = task;
355 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800356 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300357 BUG_ON(!sched->tasks);
358 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200359
Ingo Molnarad236fd2009-09-11 12:12:54 +0200360 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300361 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200362
363 return task;
364}
365
366
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300367static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200368{
369 struct task_desc *task;
370 unsigned long i;
371
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300372 for (i = 0; i < sched->nr_tasks; i++) {
373 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200374 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200375 task->nr, task->comm, task->pid, task->nr_events);
376 }
377}
378
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300379static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200380{
381 struct task_desc *task1, *task2;
382 unsigned long i, j;
383
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300384 for (i = 0; i < sched->nr_tasks; i++) {
385 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200386 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300387 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200388 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300389 task2 = sched->tasks[j];
390 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200391 }
392}
393
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300394static void perf_sched__process_event(struct perf_sched *sched,
395 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200396{
397 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200398
mingo39aeb522009-09-14 20:04:48 +0200399 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200400 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300401 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200402 break;
403 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200404 if (atom->wait_sem)
405 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200406 BUG_ON(ret);
407 break;
408 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200409 if (atom->wait_sem)
410 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200411 BUG_ON(ret);
412 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200413 case SCHED_EVENT_MIGRATION:
414 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200415 default:
416 BUG_ON(1);
417 }
418}
419
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200420static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200421{
422 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200423 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200424 int err;
425
426 err = getrusage(RUSAGE_SELF, &ru);
427 BUG_ON(err);
428
429 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
430 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
431
432 return sum;
433}
434
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800435static int self_open_counters(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200436{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800437 struct perf_event_attr attr;
Masami Hiramatsufb74fbd2014-08-14 02:22:47 +0000438 char sbuf[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800439 int fd;
440
441 memset(&attr, 0, sizeof(attr));
442
443 attr.type = PERF_TYPE_SOFTWARE;
444 attr.config = PERF_COUNT_SW_TASK_CLOCK;
445
Yann Droneaud57480d22014-06-30 22:28:47 +0200446 fd = sys_perf_event_open(&attr, 0, -1, -1,
447 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800448
449 if (fd < 0)
Namhyung Kim60b7d142012-09-12 11:11:06 +0900450 pr_err("Error: sys_perf_event_open() syscall returned "
Masami Hiramatsufb74fbd2014-08-14 02:22:47 +0000451 "with %d (%s)\n", fd,
452 strerror_r(errno, sbuf, sizeof(sbuf)));
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800453 return fd;
454}
455
456static u64 get_cpu_usage_nsec_self(int fd)
457{
458 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200459 int ret;
460
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800461 ret = read(fd, &runtime, sizeof(runtime));
462 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200463
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800464 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200465}
466
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300467struct sched_thread_parms {
468 struct task_desc *task;
469 struct perf_sched *sched;
470};
471
Ingo Molnarec156762009-09-11 12:12:54 +0200472static void *thread_func(void *ctx)
473{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300474 struct sched_thread_parms *parms = ctx;
475 struct task_desc *this_task = parms->task;
476 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200477 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200478 unsigned long i, ret;
479 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800480 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200481
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300482 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300483
Ingo Molnarec156762009-09-11 12:12:54 +0200484 sprintf(comm2, ":%s", this_task->comm);
485 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800486 fd = self_open_counters();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300487 if (fd < 0)
488 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200489again:
490 ret = sem_post(&this_task->ready_for_work);
491 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300492 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200493 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300494 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200495 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200496
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800497 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200498
499 for (i = 0; i < this_task->nr_events; i++) {
500 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300501 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200502 }
503
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800504 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200505 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200506 ret = sem_post(&this_task->work_done_sem);
507 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200508
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300509 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200510 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300511 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200512 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200513
514 goto again;
515}
516
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300517static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200518{
519 struct task_desc *task;
520 pthread_attr_t attr;
521 unsigned long i;
522 int err;
523
524 err = pthread_attr_init(&attr);
525 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200526 err = pthread_attr_setstacksize(&attr,
527 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200528 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300529 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200530 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300531 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200532 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300533 for (i = 0; i < sched->nr_tasks; i++) {
534 struct sched_thread_parms *parms = malloc(sizeof(*parms));
535 BUG_ON(parms == NULL);
536 parms->task = task = sched->tasks[i];
537 parms->sched = sched;
Ingo Molnarec156762009-09-11 12:12:54 +0200538 sem_init(&task->sleep_sem, 0, 0);
539 sem_init(&task->ready_for_work, 0, 0);
540 sem_init(&task->work_done_sem, 0, 0);
541 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300542 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200543 BUG_ON(err);
544 }
545}
546
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300547static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200548{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200549 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200550 struct task_desc *task;
551 unsigned long i, ret;
552
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553 sched->start_time = get_nsecs();
554 sched->cpu_usage = 0;
555 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200556
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300557 for (i = 0; i < sched->nr_tasks; i++) {
558 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200559 ret = sem_wait(&task->ready_for_work);
560 BUG_ON(ret);
561 sem_init(&task->ready_for_work, 0, 0);
562 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300563 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200564 BUG_ON(ret);
565
566 cpu_usage_0 = get_cpu_usage_nsec_parent();
567
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300568 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200569
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300570 for (i = 0; i < sched->nr_tasks; i++) {
571 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200572 ret = sem_wait(&task->work_done_sem);
573 BUG_ON(ret);
574 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300575 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200576 task->cpu_usage = 0;
577 }
578
579 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 if (!sched->runavg_cpu_usage)
581 sched->runavg_cpu_usage = sched->cpu_usage;
582 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200583
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300584 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
585 if (!sched->runavg_parent_cpu_usage)
586 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
587 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
588 sched->parent_cpu_usage)/10;
Ingo Molnarec156762009-09-11 12:12:54 +0200589
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200591 BUG_ON(ret);
592
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300593 for (i = 0; i < sched->nr_tasks; i++) {
594 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200595 sem_init(&task->sleep_sem, 0, 0);
596 task->curr_event = 0;
597 }
598}
599
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300600static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200601{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500602 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200603
604 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300605 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200606 T1 = get_nsecs();
607
608 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300609 sched->sum_runtime += delta;
610 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200611
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200613 if (delta < avg_delta)
614 fluct = avg_delta - delta;
615 else
616 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300617 sched->sum_fluct += fluct;
618 if (!sched->run_avg)
619 sched->run_avg = delta;
620 sched->run_avg = (sched->run_avg * 9 + delta) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200621
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300622 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200623
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300624 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200625
Ingo Molnarad236fd2009-09-11 12:12:54 +0200626 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200628
629#if 0
630 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200631 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300632 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200633 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200634 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300635 (double)sched->parent_cpu_usage/1e6,
636 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200637#endif
638
Ingo Molnarad236fd2009-09-11 12:12:54 +0200639 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200640
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300641 if (sched->nr_sleep_corrections)
642 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
643 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200644}
645
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300646static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200647{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200648 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200649
650 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300651 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200652 T1 = get_nsecs();
653
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200654 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200655
656 T0 = get_nsecs();
657 sleep_nsecs(1e6);
658 T1 = get_nsecs();
659
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200660 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200661}
662
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300663static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300664replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300665 struct perf_evsel *evsel, struct perf_sample *sample,
666 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200667{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300668 const char *comm = perf_evsel__strval(evsel, sample, "comm");
669 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200670 struct task_desc *waker, *wakee;
671
672 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300673 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200674
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300675 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200676 }
677
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300678 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300679 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200680
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300681 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300682 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200683}
684
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300685static int replay_switch_event(struct perf_sched *sched,
686 struct perf_evsel *evsel,
687 struct perf_sample *sample,
688 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200689{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300690 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
691 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
692 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
693 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
694 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300695 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300696 u64 timestamp0, timestamp = sample->time;
697 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200698 s64 delta;
699
Ingo Molnarad236fd2009-09-11 12:12:54 +0200700 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300701 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200702
Ingo Molnarfbf94822009-09-11 12:12:54 +0200703 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300704 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200705
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300706 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200707 if (timestamp0)
708 delta = timestamp - timestamp0;
709 else
710 delta = 0;
711
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300712 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900713 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300714 return -1;
715 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200716
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300717 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
718 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200719
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300720 prev = register_pid(sched, prev_pid, prev_comm);
721 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200722
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300723 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200724
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300725 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300726 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300727
728 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200729}
730
David Aherncb627502013-08-07 22:50:47 -0400731static int replay_fork_event(struct perf_sched *sched,
732 union perf_event *event,
733 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200734{
David Aherncb627502013-08-07 22:50:47 -0400735 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300736
Adrian Hunter314add62013-08-27 11:23:03 +0300737 child = machine__findnew_thread(machine, event->fork.pid,
738 event->fork.tid);
739 parent = machine__findnew_thread(machine, event->fork.ppid,
740 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400741
742 if (child == NULL || parent == NULL) {
743 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
744 child, parent);
745 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200746 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300747
David Aherncb627502013-08-07 22:50:47 -0400748 if (verbose) {
749 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200750 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
751 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400752 }
753
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200754 register_pid(sched, parent->tid, thread__comm_str(parent));
755 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300756 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200757}
758
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200759struct sort_dimension {
760 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200761 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200762 struct list_head list;
763};
764
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200765static int
mingo39aeb522009-09-14 20:04:48 +0200766thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200767{
768 struct sort_dimension *sort;
769 int ret = 0;
770
Ingo Molnarb5fae122009-09-11 12:12:54 +0200771 BUG_ON(list_empty(list));
772
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200773 list_for_each_entry(sort, list, list) {
774 ret = sort->cmp(l, r);
775 if (ret)
776 return ret;
777 }
778
779 return ret;
780}
781
mingo39aeb522009-09-14 20:04:48 +0200782static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200783thread_atoms_search(struct rb_root *root, struct thread *thread,
784 struct list_head *sort_list)
785{
786 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200787 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200788
789 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200790 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200791 int cmp;
792
mingo39aeb522009-09-14 20:04:48 +0200793 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200794
795 cmp = thread_lat_cmp(sort_list, &key, atoms);
796 if (cmp > 0)
797 node = node->rb_left;
798 else if (cmp < 0)
799 node = node->rb_right;
800 else {
801 BUG_ON(thread != atoms->thread);
802 return atoms;
803 }
804 }
805 return NULL;
806}
807
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200808static void
mingo39aeb522009-09-14 20:04:48 +0200809__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200810 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200811{
812 struct rb_node **new = &(root->rb_node), *parent = NULL;
813
814 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200815 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200816 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200817
mingo39aeb522009-09-14 20:04:48 +0200818 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200819 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200820
821 cmp = thread_lat_cmp(sort_list, data, this);
822
823 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200824 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200825 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200826 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200827 }
828
829 rb_link_node(&data->node, parent, new);
830 rb_insert_color(&data->node, root);
831}
832
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300833static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200834{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200835 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300836 if (!atoms) {
837 pr_err("No memory at %s\n", __func__);
838 return -1;
839 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200840
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300841 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200842 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300843 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300844 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200845}
846
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300847static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200848{
849 const char *str = TASK_STATE_TO_CHAR_STR;
850
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300851 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200852}
853
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300854static int
mingo39aeb522009-09-14 20:04:48 +0200855add_sched_out_event(struct work_atoms *atoms,
856 char run_state,
857 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200858{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200859 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300860 if (!atom) {
861 pr_err("Non memory at %s", __func__);
862 return -1;
863 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200864
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200865 atom->sched_out_time = timestamp;
866
mingo39aeb522009-09-14 20:04:48 +0200867 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200868 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200869 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200870 }
871
mingo39aeb522009-09-14 20:04:48 +0200872 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300873 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200874}
875
876static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300877add_runtime_event(struct work_atoms *atoms, u64 delta,
878 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200879{
880 struct work_atom *atom;
881
882 BUG_ON(list_empty(&atoms->work_list));
883
884 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
885
886 atom->runtime += delta;
887 atoms->total_runtime += delta;
888}
889
890static void
891add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200892{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200893 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200894 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200895
mingo39aeb522009-09-14 20:04:48 +0200896 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200897 return;
898
mingo39aeb522009-09-14 20:04:48 +0200899 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200900
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200901 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200902 return;
903
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200904 if (timestamp < atom->wake_up_time) {
905 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200906 return;
907 }
908
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200909 atom->state = THREAD_SCHED_IN;
910 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200911
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200912 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200913 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100914 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200915 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100916 atoms->max_lat_at = timestamp;
917 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200918 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200919}
920
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300921static int latency_switch_event(struct perf_sched *sched,
922 struct perf_evsel *evsel,
923 struct perf_sample *sample,
924 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200925{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300926 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
927 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
928 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200929 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300931 u64 timestamp0, timestamp = sample->time;
932 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200933 s64 delta;
934
mingo39aeb522009-09-14 20:04:48 +0200935 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200936
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300937 timestamp0 = sched->cpu_last_switched[cpu];
938 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200939 if (timestamp0)
940 delta = timestamp - timestamp0;
941 else
942 delta = 0;
943
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300944 if (delta < 0) {
945 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
946 return -1;
947 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300949 sched_out = machine__findnew_thread(machine, -1, prev_pid);
950 sched_in = machine__findnew_thread(machine, -1, next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300952 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200953 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300954 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300955 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300956 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300957 if (!out_events) {
958 pr_err("out-event: Internal tree error");
959 return -1;
960 }
mingo39aeb522009-09-14 20:04:48 +0200961 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300962 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300963 return -1;
mingo39aeb522009-09-14 20:04:48 +0200964
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300965 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200966 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300967 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300968 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300969 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300970 if (!in_events) {
971 pr_err("in-event: Internal tree error");
972 return -1;
973 }
mingo39aeb522009-09-14 20:04:48 +0200974 /*
975 * Take came in we have not heard about yet,
976 * add in an initial atom in runnable state:
977 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300978 if (add_sched_out_event(in_events, 'R', timestamp))
979 return -1;
mingo39aeb522009-09-14 20:04:48 +0200980 }
981 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300982
983 return 0;
mingo39aeb522009-09-14 20:04:48 +0200984}
985
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300986static int latency_runtime_event(struct perf_sched *sched,
987 struct perf_evsel *evsel,
988 struct perf_sample *sample,
989 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +0200990{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300991 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
992 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300993 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300994 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300995 u64 timestamp = sample->time;
996 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +0200997
998 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +0200999 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001000 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001001 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001002 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001003 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001004 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001005 return -1;
1006 }
1007 if (add_sched_out_event(atoms, 'R', timestamp))
1008 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001009 }
1010
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001011 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001012 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001013}
1014
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001015static int latency_wakeup_event(struct perf_sched *sched,
1016 struct perf_evsel *evsel,
1017 struct perf_sample *sample,
1018 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001019{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001020 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001021 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001022 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001023 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001024 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001025
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001026 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001027 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001028 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001029 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001030 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001031 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001032 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001033 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001034 return -1;
1035 }
1036 if (add_sched_out_event(atoms, 'S', timestamp))
1037 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001038 }
1039
mingo39aeb522009-09-14 20:04:48 +02001040 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001041
mingo39aeb522009-09-14 20:04:48 +02001042 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001044 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001045 * As we do not guarantee the wakeup event happens when
1046 * task is out of run queue, also may happen when task is
1047 * on run queue and wakeup only change ->state to TASK_RUNNING,
1048 * then we should not set the ->wake_up_time when wake up a
1049 * task which is on run queue.
1050 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001051 * You WILL be missing events if you've recorded only
1052 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001053 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001054 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001055 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Dongsheng Yang67d62592014-05-13 10:38:21 +09001056 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001057
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001058 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001059 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001060 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001061 return 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001062 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001063
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001064 atom->state = THREAD_WAIT_CPU;
1065 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001066 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001067}
1068
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001069static int latency_migrate_task_event(struct perf_sched *sched,
1070 struct perf_evsel *evsel,
1071 struct perf_sample *sample,
1072 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001073{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001074 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001075 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001076 struct work_atoms *atoms;
1077 struct work_atom *atom;
1078 struct thread *migrant;
1079
1080 /*
1081 * Only need to worry about migration when profiling one CPU.
1082 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001083 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001084 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001085
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001086 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001087 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001088 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001089 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001090 return -1;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001091 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001093 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001094 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 return -1;
1096 }
1097 if (add_sched_out_event(atoms, 'R', timestamp))
1098 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001099 }
1100
1101 BUG_ON(list_empty(&atoms->work_list));
1102
1103 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1104 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1105
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001106 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001107
1108 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001109 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001110
1111 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001112}
1113
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001114static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001115{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001116 int i;
1117 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001118 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001119
mingo39aeb522009-09-14 20:04:48 +02001120 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001121 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001122 /*
1123 * Ignore idle threads:
1124 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001125 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001126 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001127
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001128 sched->all_runtime += work_list->total_runtime;
1129 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001130
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001131 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132
mingo08f69e62009-09-14 18:30:44 +02001133 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134 printf(" ");
1135
mingo39aeb522009-09-14 20:04:48 +02001136 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001138 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 +02001139 (double)work_list->total_runtime / 1e6,
1140 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001141 (double)work_list->max_lat / 1e6,
1142 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001143}
1144
mingo39aeb522009-09-14 20:04:48 +02001145static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001146{
Adrian Hunter38051232013-07-04 16:20:31 +03001147 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001148 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001149 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001150 return 1;
1151
1152 return 0;
1153}
1154
mingo39aeb522009-09-14 20:04:48 +02001155static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001156{
1157 u64 avgl, avgr;
1158
1159 if (!l->nb_atoms)
1160 return -1;
1161
1162 if (!r->nb_atoms)
1163 return 1;
1164
1165 avgl = l->total_lat / l->nb_atoms;
1166 avgr = r->total_lat / r->nb_atoms;
1167
1168 if (avgl < avgr)
1169 return -1;
1170 if (avgl > avgr)
1171 return 1;
1172
1173 return 0;
1174}
1175
mingo39aeb522009-09-14 20:04:48 +02001176static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001177{
1178 if (l->max_lat < r->max_lat)
1179 return -1;
1180 if (l->max_lat > r->max_lat)
1181 return 1;
1182
1183 return 0;
1184}
1185
mingo39aeb522009-09-14 20:04:48 +02001186static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001187{
1188 if (l->nb_atoms < r->nb_atoms)
1189 return -1;
1190 if (l->nb_atoms > r->nb_atoms)
1191 return 1;
1192
1193 return 0;
1194}
1195
mingo39aeb522009-09-14 20:04:48 +02001196static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001197{
1198 if (l->total_runtime < r->total_runtime)
1199 return -1;
1200 if (l->total_runtime > r->total_runtime)
1201 return 1;
1202
1203 return 0;
1204}
1205
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001206static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001207{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001208 size_t i;
1209 static struct sort_dimension avg_sort_dimension = {
1210 .name = "avg",
1211 .cmp = avg_cmp,
1212 };
1213 static struct sort_dimension max_sort_dimension = {
1214 .name = "max",
1215 .cmp = max_cmp,
1216 };
1217 static struct sort_dimension pid_sort_dimension = {
1218 .name = "pid",
1219 .cmp = pid_cmp,
1220 };
1221 static struct sort_dimension runtime_sort_dimension = {
1222 .name = "runtime",
1223 .cmp = runtime_cmp,
1224 };
1225 static struct sort_dimension switch_sort_dimension = {
1226 .name = "switch",
1227 .cmp = switch_cmp,
1228 };
1229 struct sort_dimension *available_sorts[] = {
1230 &pid_sort_dimension,
1231 &avg_sort_dimension,
1232 &max_sort_dimension,
1233 &switch_sort_dimension,
1234 &runtime_sort_dimension,
1235 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001236
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001237 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001238 if (!strcmp(available_sorts[i]->name, tok)) {
1239 list_add_tail(&available_sorts[i]->list, list);
1240
1241 return 0;
1242 }
1243 }
1244
1245 return -1;
1246}
1247
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001248static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001249{
1250 struct rb_node *node;
1251
1252 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001253 struct work_atoms *data;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001254 node = rb_first(&sched->atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001255 if (!node)
1256 break;
1257
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001258 rb_erase(node, &sched->atom_root);
mingo39aeb522009-09-14 20:04:48 +02001259 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001260 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001261 }
1262}
1263
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001264static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001265 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001266 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001267 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001268{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001269 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001270
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001271 if (sched->tp_handler->wakeup_event)
1272 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001273
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001274 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001275}
1276
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001277static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1278 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001279{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001280 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1281 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001282 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001283 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001284 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001285 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001286
1287 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1288
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001289 if (this_cpu > sched->max_cpu)
1290 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001291
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001292 timestamp0 = sched->cpu_last_switched[this_cpu];
1293 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001294 if (timestamp0)
1295 delta = timestamp - timestamp0;
1296 else
1297 delta = 0;
1298
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001299 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001300 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001301 return -1;
1302 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001303
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001304 sched_in = machine__findnew_thread(machine, -1, next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001305
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001306 sched->curr_thread[this_cpu] = sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001307
1308 printf(" ");
1309
1310 new_shortname = 0;
1311 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001312 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1313 /*
1314 * Don't allocate a letter-number for swapper:0
1315 * as a shortname. Instead, we use '.' for it.
1316 */
1317 sched_in->shortname[0] = '.';
1318 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001319 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001320 sched_in->shortname[0] = sched->next_shortname1;
1321 sched_in->shortname[1] = sched->next_shortname2;
1322
1323 if (sched->next_shortname1 < 'Z') {
1324 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001325 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001326 sched->next_shortname1 = 'A';
1327 if (sched->next_shortname2 < '9')
1328 sched->next_shortname2++;
1329 else
1330 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001331 }
1332 }
1333 new_shortname = 1;
1334 }
1335
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001336 for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001337 if (cpu != this_cpu)
1338 printf(" ");
1339 else
1340 printf("*");
1341
Dongsheng6bcab4e2014-05-06 14:39:01 +09001342 if (sched->curr_thread[cpu])
1343 printf("%2s ", sched->curr_thread[cpu]->shortname);
1344 else
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001345 printf(" ");
1346 }
1347
1348 printf(" %12.6f secs ", (double)timestamp/1e9);
1349 if (new_shortname) {
1350 printf("%s => %s:%d\n",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001351 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001352 } else {
1353 printf("\n");
1354 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001355
1356 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001357}
1358
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001359static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001360 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001361 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001362 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001363{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001364 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001365 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001366 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1367 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001368
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001369 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001370 /*
1371 * Are we trying to switch away a PID that is
1372 * not current?
1373 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001374 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001375 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001376 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001377
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001378 if (sched->tp_handler->switch_event)
1379 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001380
1381 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001382 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001383}
1384
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001385static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001386 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001387 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001388 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001389{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001390 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001391
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001392 if (sched->tp_handler->runtime_event)
1393 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001394
1395 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001396}
1397
David Aherncb627502013-08-07 22:50:47 -04001398static int perf_sched__process_fork_event(struct perf_tool *tool,
1399 union perf_event *event,
1400 struct perf_sample *sample,
1401 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001402{
1403 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1404
David Aherncb627502013-08-07 22:50:47 -04001405 /* run the fork event through the perf machineruy */
1406 perf_event__process_fork(tool, event, sample, machine);
1407
1408 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001409 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001410 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001411
1412 return 0;
1413}
1414
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001415static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001416 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001417 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001418 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001419{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001420 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001421
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001422 if (sched->tp_handler->migrate_task_event)
1423 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001424
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001425 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001426}
1427
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001428typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001429 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001430 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001431 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001432
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001433static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1434 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001435 struct perf_sample *sample,
1436 struct perf_evsel *evsel,
1437 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001438{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001439 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001440
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001441 if (evsel->handler != NULL) {
1442 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001443 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001444 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001445
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001446 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001447}
1448
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001449static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001450{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001451 const struct perf_evsel_str_handler handlers[] = {
1452 { "sched:sched_switch", process_sched_switch_event, },
1453 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1454 { "sched:sched_wakeup", process_sched_wakeup_event, },
1455 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001456 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1457 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001458 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001459 struct perf_data_file file = {
1460 .path = input_name,
1461 .mode = PERF_DATA_MODE_READ,
1462 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001463 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001464
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001465 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001466 if (session == NULL) {
1467 pr_debug("No Memory for session\n");
1468 return -1;
1469 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001470
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001471 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001472
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001473 if (perf_session__set_tracepoints_handlers(session, handlers))
1474 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001475
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001476 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001477 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001478 if (err) {
1479 pr_err("Failed to process events, error %d", err);
1480 goto out_delete;
1481 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001482
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001483 sched->nr_events = session->evlist->stats.nr_events[0];
1484 sched->nr_lost_events = session->evlist->stats.total_lost;
1485 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001486 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001487
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001488 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001489out_delete:
1490 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001491 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001492}
1493
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001494static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001495{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001496 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001497 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001498 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1499 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001500 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001501 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001502 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001503 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1504 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001505 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001506 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001507 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001508 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1509 sched->nr_context_switch_bugs, sched->nr_timestamps);
1510 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001511 printf(" (due to lost events?)");
1512 printf("\n");
1513 }
1514}
1515
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001516static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001517{
1518 struct rb_node *next;
1519
1520 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001521
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001522 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001523 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001524
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001525 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001526
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001527 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1528 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1529 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001530
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001531 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001532
1533 while (next) {
1534 struct work_atoms *work_list;
1535
1536 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001537 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001538 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001539 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001540 }
1541
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001542 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001543 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001544 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001545
1546 printf(" ---------------------------------------------------\n");
1547
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001548 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001549 printf("\n");
1550
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001551 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001552}
1553
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001554static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001555{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001556 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
Ingo Molnar40749d02009-09-17 18:24:55 +02001557
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001558 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001559 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001560 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001561 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001562 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001563}
1564
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001565static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001566{
1567 unsigned long i;
1568
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001569 calibrate_run_measurement_overhead(sched);
1570 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001571
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001572 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001573
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001574 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001575 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001576
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001577 printf("nr_run_events: %ld\n", sched->nr_run_events);
1578 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1579 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001580
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001581 if (sched->targetless_wakeups)
1582 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1583 if (sched->multitarget_wakeups)
1584 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1585 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001586 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001587 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001588
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001589 print_task_traces(sched);
1590 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001591
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001592 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001593 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001594 for (i = 0; i < sched->replay_repeat; i++)
1595 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001596
1597 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001598}
1599
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001600static void setup_sorting(struct perf_sched *sched, const struct option *options,
1601 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001602{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001603 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001604
1605 for (tok = strtok_r(str, ", ", &tmp);
1606 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001607 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001608 error("Unknown --sort key: `%s'", tok);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001609 usage_with_options(usage_msg, options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001610 }
1611 }
1612
1613 free(str);
1614
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001615 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001616}
1617
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001618static int __cmd_record(int argc, const char **argv)
1619{
1620 unsigned int rec_argc, i, j;
1621 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001622 const char * const record_args[] = {
1623 "record",
1624 "-a",
1625 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001626 "-m", "1024",
1627 "-c", "1",
1628 "-e", "sched:sched_switch",
1629 "-e", "sched:sched_stat_wait",
1630 "-e", "sched:sched_stat_sleep",
1631 "-e", "sched:sched_stat_iowait",
1632 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001633 "-e", "sched:sched_process_fork",
1634 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001635 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001636 "-e", "sched:sched_migrate_task",
1637 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001638
1639 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1640 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1641
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001642 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001643 return -ENOMEM;
1644
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001645 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1646 rec_argv[i] = strdup(record_args[i]);
1647
1648 for (j = 1; j < (unsigned int)argc; j++, i++)
1649 rec_argv[i] = argv[j];
1650
1651 BUG_ON(i != rec_argc);
1652
1653 return cmd_record(i, rec_argv, NULL);
1654}
1655
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001656int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001657{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001658 const char default_sort_order[] = "avg, max, switch, runtime";
1659 struct perf_sched sched = {
1660 .tool = {
1661 .sample = perf_sched__process_tracepoint_sample,
1662 .comm = perf_event__process_comm,
1663 .lost = perf_event__process_lost,
1664 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001665 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001666 },
1667 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1668 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1669 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1670 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001671 .sort_order = default_sort_order,
1672 .replay_repeat = 10,
1673 .profile_cpu = -1,
1674 .next_shortname1 = 'A',
1675 .next_shortname2 = '0',
1676 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001677 const struct option latency_options[] = {
1678 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1679 "sort by key(s): runtime, switch, avg, max"),
1680 OPT_INCR('v', "verbose", &verbose,
1681 "be more verbose (show symbol address, etc)"),
1682 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1683 "CPU to profile on"),
1684 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1685 "dump raw trace in ASCII"),
1686 OPT_END()
1687 };
1688 const struct option replay_options[] = {
1689 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1690 "repeat the workload replay N times (-1: infinite)"),
1691 OPT_INCR('v', "verbose", &verbose,
1692 "be more verbose (show symbol address, etc)"),
1693 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1694 "dump raw trace in ASCII"),
1695 OPT_END()
1696 };
1697 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001698 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001699 "input file name"),
1700 OPT_INCR('v', "verbose", &verbose,
1701 "be more verbose (show symbol address, etc)"),
1702 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1703 "dump raw trace in ASCII"),
1704 OPT_END()
1705 };
1706 const char * const latency_usage[] = {
1707 "perf sched latency [<options>]",
1708 NULL
1709 };
1710 const char * const replay_usage[] = {
1711 "perf sched replay [<options>]",
1712 NULL
1713 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001714 const char *const sched_subcommands[] = { "record", "latency", "map",
1715 "replay", "script", NULL };
1716 const char *sched_usage[] = {
1717 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001718 NULL
1719 };
1720 struct trace_sched_handler lat_ops = {
1721 .wakeup_event = latency_wakeup_event,
1722 .switch_event = latency_switch_event,
1723 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001724 .migrate_task_event = latency_migrate_task_event,
1725 };
1726 struct trace_sched_handler map_ops = {
1727 .switch_event = map_switch_event,
1728 };
1729 struct trace_sched_handler replay_ops = {
1730 .wakeup_event = replay_wakeup_event,
1731 .switch_event = replay_switch_event,
1732 .fork_event = replay_fork_event,
1733 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001734 unsigned int i;
1735
1736 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1737 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001738
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001739 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1740 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001741 if (!argc)
1742 usage_with_options(sched_usage, sched_options);
1743
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001744 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001745 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001746 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001747 if (!strcmp(argv[0], "script"))
1748 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001749
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001750 if (!strncmp(argv[0], "rec", 3)) {
1751 return __cmd_record(argc, argv);
1752 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001753 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001754 if (argc > 1) {
1755 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1756 if (argc)
1757 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001758 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001759 setup_sorting(&sched, latency_options, latency_usage);
1760 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001761 } else if (!strcmp(argv[0], "map")) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001762 sched.tp_handler = &map_ops;
1763 setup_sorting(&sched, latency_options, latency_usage);
1764 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001765 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001766 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001767 if (argc) {
1768 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1769 if (argc)
1770 usage_with_options(replay_usage, replay_options);
1771 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001772 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001773 } else {
1774 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001775 }
1776
Ingo Molnarec156762009-09-11 12:12:54 +02001777 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001778}