blob: dd714818fa4d0b8bbb89665283628ecbc7d7cbfe [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
454 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)));
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800458 return fd;
459}
460
461static u64 get_cpu_usage_nsec_self(int fd)
462{
463 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200464 int ret;
465
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800466 ret = read(fd, &runtime, sizeof(runtime));
467 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200468
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800469 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200470}
471
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300472struct sched_thread_parms {
473 struct task_desc *task;
474 struct perf_sched *sched;
475};
476
Ingo Molnarec156762009-09-11 12:12:54 +0200477static void *thread_func(void *ctx)
478{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300479 struct sched_thread_parms *parms = ctx;
480 struct task_desc *this_task = parms->task;
481 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200482 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200483 unsigned long i, ret;
484 char comm2[22];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800485 int fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200486
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300487 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300488
Ingo Molnarec156762009-09-11 12:12:54 +0200489 sprintf(comm2, ":%s", this_task->comm);
490 prctl(PR_SET_NAME, comm2);
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800491 fd = self_open_counters();
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300492 if (fd < 0)
493 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200494again:
495 ret = sem_post(&this_task->ready_for_work);
496 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300497 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200498 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300499 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200500 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200501
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800502 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200503
504 for (i = 0; i < this_task->nr_events; i++) {
505 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300506 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200507 }
508
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800509 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200510 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200511 ret = sem_post(&this_task->work_done_sem);
512 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200513
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300514 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200515 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300516 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200517 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200518
519 goto again;
520}
521
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300522static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200523{
524 struct task_desc *task;
525 pthread_attr_t attr;
526 unsigned long i;
527 int err;
528
529 err = pthread_attr_init(&attr);
530 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200531 err = pthread_attr_setstacksize(&attr,
532 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200533 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300534 err = pthread_mutex_lock(&sched->start_work_mutex);
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->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200537 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300538 for (i = 0; i < sched->nr_tasks; i++) {
539 struct sched_thread_parms *parms = malloc(sizeof(*parms));
540 BUG_ON(parms == NULL);
541 parms->task = task = sched->tasks[i];
542 parms->sched = sched;
Ingo Molnarec156762009-09-11 12:12:54 +0200543 sem_init(&task->sleep_sem, 0, 0);
544 sem_init(&task->ready_for_work, 0, 0);
545 sem_init(&task->work_done_sem, 0, 0);
546 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300547 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200548 BUG_ON(err);
549 }
550}
551
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300552static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200553{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200554 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200555 struct task_desc *task;
556 unsigned long i, ret;
557
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300558 sched->start_time = get_nsecs();
559 sched->cpu_usage = 0;
560 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200561
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300562 for (i = 0; i < sched->nr_tasks; i++) {
563 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200564 ret = sem_wait(&task->ready_for_work);
565 BUG_ON(ret);
566 sem_init(&task->ready_for_work, 0, 0);
567 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300568 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200569 BUG_ON(ret);
570
571 cpu_usage_0 = get_cpu_usage_nsec_parent();
572
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300573 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200574
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300575 for (i = 0; i < sched->nr_tasks; i++) {
576 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200577 ret = sem_wait(&task->work_done_sem);
578 BUG_ON(ret);
579 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200581 task->cpu_usage = 0;
582 }
583
584 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300585 if (!sched->runavg_cpu_usage)
586 sched->runavg_cpu_usage = sched->cpu_usage;
587 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200588
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300589 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
590 if (!sched->runavg_parent_cpu_usage)
591 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
592 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 +
593 sched->parent_cpu_usage)/10;
Ingo Molnarec156762009-09-11 12:12:54 +0200594
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200596 BUG_ON(ret);
597
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300598 for (i = 0; i < sched->nr_tasks; i++) {
599 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200600 sem_init(&task->sleep_sem, 0, 0);
601 task->curr_event = 0;
602 }
603}
604
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300605static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200606{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500607 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200608
609 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300610 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200611 T1 = get_nsecs();
612
613 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300614 sched->sum_runtime += delta;
615 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200616
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300617 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200618 if (delta < avg_delta)
619 fluct = avg_delta - delta;
620 else
621 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300622 sched->sum_fluct += fluct;
623 if (!sched->run_avg)
624 sched->run_avg = delta;
625 sched->run_avg = (sched->run_avg * 9 + delta) / 10;
Ingo Molnarec156762009-09-11 12:12:54 +0200626
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200628
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300629 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200630
Ingo Molnarad236fd2009-09-11 12:12:54 +0200631 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300632 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200633
634#if 0
635 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200636 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200638 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200639 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300640 (double)sched->parent_cpu_usage/1e6,
641 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200642#endif
643
Ingo Molnarad236fd2009-09-11 12:12:54 +0200644 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200645
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300646 if (sched->nr_sleep_corrections)
647 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
648 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200649}
650
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300651static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200652{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200653 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200654
655 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300656 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200657 T1 = get_nsecs();
658
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200659 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200660
661 T0 = get_nsecs();
662 sleep_nsecs(1e6);
663 T1 = get_nsecs();
664
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200665 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200666}
667
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300668static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300669replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300670 struct perf_evsel *evsel, struct perf_sample *sample,
671 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200672{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300673 const char *comm = perf_evsel__strval(evsel, sample, "comm");
674 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200675 struct task_desc *waker, *wakee;
676
677 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300678 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200679
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300680 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200681 }
682
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300683 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300684 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200685
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300686 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300687 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200688}
689
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300690static int replay_switch_event(struct perf_sched *sched,
691 struct perf_evsel *evsel,
692 struct perf_sample *sample,
693 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200694{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300695 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
696 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
697 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
698 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
699 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300700 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300701 u64 timestamp0, timestamp = sample->time;
702 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200703 s64 delta;
704
Ingo Molnarad236fd2009-09-11 12:12:54 +0200705 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300706 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200707
Ingo Molnarfbf94822009-09-11 12:12:54 +0200708 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300709 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200710
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300711 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200712 if (timestamp0)
713 delta = timestamp - timestamp0;
714 else
715 delta = 0;
716
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300717 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900718 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300719 return -1;
720 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200721
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300722 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
723 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200724
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300725 prev = register_pid(sched, prev_pid, prev_comm);
726 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200727
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300728 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200729
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300730 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300731 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300732
733 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200734}
735
David Aherncb627502013-08-07 22:50:47 -0400736static int replay_fork_event(struct perf_sched *sched,
737 union perf_event *event,
738 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200739{
David Aherncb627502013-08-07 22:50:47 -0400740 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300741
Adrian Hunter314add62013-08-27 11:23:03 +0300742 child = machine__findnew_thread(machine, event->fork.pid,
743 event->fork.tid);
744 parent = machine__findnew_thread(machine, event->fork.ppid,
745 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400746
747 if (child == NULL || parent == NULL) {
748 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
749 child, parent);
750 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200751 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300752
David Aherncb627502013-08-07 22:50:47 -0400753 if (verbose) {
754 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200755 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
756 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400757 }
758
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200759 register_pid(sched, parent->tid, thread__comm_str(parent));
760 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300761 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200762}
763
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200764struct sort_dimension {
765 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200766 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200767 struct list_head list;
768};
769
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200770static int
mingo39aeb522009-09-14 20:04:48 +0200771thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200772{
773 struct sort_dimension *sort;
774 int ret = 0;
775
Ingo Molnarb5fae122009-09-11 12:12:54 +0200776 BUG_ON(list_empty(list));
777
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200778 list_for_each_entry(sort, list, list) {
779 ret = sort->cmp(l, r);
780 if (ret)
781 return ret;
782 }
783
784 return ret;
785}
786
mingo39aeb522009-09-14 20:04:48 +0200787static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200788thread_atoms_search(struct rb_root *root, struct thread *thread,
789 struct list_head *sort_list)
790{
791 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200792 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200793
794 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200795 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200796 int cmp;
797
mingo39aeb522009-09-14 20:04:48 +0200798 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200799
800 cmp = thread_lat_cmp(sort_list, &key, atoms);
801 if (cmp > 0)
802 node = node->rb_left;
803 else if (cmp < 0)
804 node = node->rb_right;
805 else {
806 BUG_ON(thread != atoms->thread);
807 return atoms;
808 }
809 }
810 return NULL;
811}
812
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200813static void
mingo39aeb522009-09-14 20:04:48 +0200814__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200815 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200816{
817 struct rb_node **new = &(root->rb_node), *parent = NULL;
818
819 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200820 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200821 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200822
mingo39aeb522009-09-14 20:04:48 +0200823 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200824 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200825
826 cmp = thread_lat_cmp(sort_list, data, this);
827
828 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200829 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200830 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200831 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200832 }
833
834 rb_link_node(&data->node, parent, new);
835 rb_insert_color(&data->node, root);
836}
837
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300838static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200839{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200840 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300841 if (!atoms) {
842 pr_err("No memory at %s\n", __func__);
843 return -1;
844 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200845
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300846 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200847 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300848 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300849 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200850}
851
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300852static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200853{
854 const char *str = TASK_STATE_TO_CHAR_STR;
855
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300856 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200857}
858
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300859static int
mingo39aeb522009-09-14 20:04:48 +0200860add_sched_out_event(struct work_atoms *atoms,
861 char run_state,
862 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200863{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200864 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300865 if (!atom) {
866 pr_err("Non memory at %s", __func__);
867 return -1;
868 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200869
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200870 atom->sched_out_time = timestamp;
871
mingo39aeb522009-09-14 20:04:48 +0200872 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200873 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200874 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200875 }
876
mingo39aeb522009-09-14 20:04:48 +0200877 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300878 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200879}
880
881static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300882add_runtime_event(struct work_atoms *atoms, u64 delta,
883 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200884{
885 struct work_atom *atom;
886
887 BUG_ON(list_empty(&atoms->work_list));
888
889 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
890
891 atom->runtime += delta;
892 atoms->total_runtime += delta;
893}
894
895static void
896add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200897{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200898 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200899 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200900
mingo39aeb522009-09-14 20:04:48 +0200901 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200902 return;
903
mingo39aeb522009-09-14 20:04:48 +0200904 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200906 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907 return;
908
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200909 if (timestamp < atom->wake_up_time) {
910 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200911 return;
912 }
913
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200914 atom->state = THREAD_SCHED_IN;
915 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200916
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200917 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200918 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100919 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200920 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100921 atoms->max_lat_at = timestamp;
922 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200923 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200924}
925
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300926static int latency_switch_event(struct perf_sched *sched,
927 struct perf_evsel *evsel,
928 struct perf_sample *sample,
929 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300931 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
932 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
933 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200934 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300936 u64 timestamp0, timestamp = sample->time;
937 int cpu = sample->cpu;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200938 s64 delta;
939
mingo39aeb522009-09-14 20:04:48 +0200940 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200941
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300942 timestamp0 = sched->cpu_last_switched[cpu];
943 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200944 if (timestamp0)
945 delta = timestamp - timestamp0;
946 else
947 delta = 0;
948
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300949 if (delta < 0) {
950 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
951 return -1;
952 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200953
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300954 sched_out = machine__findnew_thread(machine, -1, prev_pid);
955 sched_in = machine__findnew_thread(machine, -1, next_pid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200956
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300957 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200958 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300959 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300960 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300961 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300962 if (!out_events) {
963 pr_err("out-event: Internal tree error");
964 return -1;
965 }
mingo39aeb522009-09-14 20:04:48 +0200966 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300967 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300968 return -1;
mingo39aeb522009-09-14 20:04:48 +0200969
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300970 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200971 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300972 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300973 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300974 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300975 if (!in_events) {
976 pr_err("in-event: Internal tree error");
977 return -1;
978 }
mingo39aeb522009-09-14 20:04:48 +0200979 /*
980 * Take came in we have not heard about yet,
981 * add in an initial atom in runnable state:
982 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300983 if (add_sched_out_event(in_events, 'R', timestamp))
984 return -1;
mingo39aeb522009-09-14 20:04:48 +0200985 }
986 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300987
988 return 0;
mingo39aeb522009-09-14 20:04:48 +0200989}
990
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300991static int latency_runtime_event(struct perf_sched *sched,
992 struct perf_evsel *evsel,
993 struct perf_sample *sample,
994 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +0200995{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300996 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
997 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300998 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300999 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001000 u64 timestamp = sample->time;
1001 int cpu = sample->cpu;
mingo39aeb522009-09-14 20:04:48 +02001002
1003 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001004 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001005 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001006 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001007 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001008 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001009 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001010 return -1;
1011 }
1012 if (add_sched_out_event(atoms, 'R', timestamp))
1013 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001014 }
1015
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001016 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001017 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001018}
1019
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001020static int latency_wakeup_event(struct perf_sched *sched,
1021 struct perf_evsel *evsel,
1022 struct perf_sample *sample,
1023 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001025 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001026 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001027 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001028 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001029 u64 timestamp = sample->time;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001031 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001032 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001033 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001034 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001035 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001036 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001037 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001038 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001039 return -1;
1040 }
1041 if (add_sched_out_event(atoms, 'S', timestamp))
1042 return -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043 }
1044
mingo39aeb522009-09-14 20:04:48 +02001045 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001046
mingo39aeb522009-09-14 20:04:48 +02001047 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001048
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001049 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001050 * As we do not guarantee the wakeup event happens when
1051 * task is out of run queue, also may happen when task is
1052 * on run queue and wakeup only change ->state to TASK_RUNNING,
1053 * then we should not set the ->wake_up_time when wake up a
1054 * task which is on run queue.
1055 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001056 * You WILL be missing events if you've recorded only
1057 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001058 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001059 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001060 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Dongsheng Yang67d62592014-05-13 10:38:21 +09001061 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001062
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001063 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001064 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001065 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001066 return 0;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001067 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001068
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001069 atom->state = THREAD_WAIT_CPU;
1070 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001071 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001072}
1073
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001074static int latency_migrate_task_event(struct perf_sched *sched,
1075 struct perf_evsel *evsel,
1076 struct perf_sample *sample,
1077 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001078{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001079 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001080 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001081 struct work_atoms *atoms;
1082 struct work_atom *atom;
1083 struct thread *migrant;
1084
1085 /*
1086 * Only need to worry about migration when profiling one CPU.
1087 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001088 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001089 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001090
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001091 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001093 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001094 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 return -1;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001096 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001097 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001098 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001099 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001100 return -1;
1101 }
1102 if (add_sched_out_event(atoms, 'R', timestamp))
1103 return -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001104 }
1105
1106 BUG_ON(list_empty(&atoms->work_list));
1107
1108 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1109 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1110
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001111 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001112
1113 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001114 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001115
1116 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001117}
1118
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001119static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001121 int i;
1122 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001123 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124
mingo39aeb522009-09-14 20:04:48 +02001125 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001126 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001127 /*
1128 * Ignore idle threads:
1129 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001130 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001131 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001133 sched->all_runtime += work_list->total_runtime;
1134 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001135
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001136 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137
mingo08f69e62009-09-14 18:30:44 +02001138 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001139 printf(" ");
1140
mingo39aeb522009-09-14 20:04:48 +02001141 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001143 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 +02001144 (double)work_list->total_runtime / 1e6,
1145 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001146 (double)work_list->max_lat / 1e6,
1147 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001148}
1149
mingo39aeb522009-09-14 20:04:48 +02001150static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001151{
Adrian Hunter38051232013-07-04 16:20:31 +03001152 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001153 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001154 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001155 return 1;
1156
1157 return 0;
1158}
1159
mingo39aeb522009-09-14 20:04:48 +02001160static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001161{
1162 u64 avgl, avgr;
1163
1164 if (!l->nb_atoms)
1165 return -1;
1166
1167 if (!r->nb_atoms)
1168 return 1;
1169
1170 avgl = l->total_lat / l->nb_atoms;
1171 avgr = r->total_lat / r->nb_atoms;
1172
1173 if (avgl < avgr)
1174 return -1;
1175 if (avgl > avgr)
1176 return 1;
1177
1178 return 0;
1179}
1180
mingo39aeb522009-09-14 20:04:48 +02001181static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001182{
1183 if (l->max_lat < r->max_lat)
1184 return -1;
1185 if (l->max_lat > r->max_lat)
1186 return 1;
1187
1188 return 0;
1189}
1190
mingo39aeb522009-09-14 20:04:48 +02001191static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001192{
1193 if (l->nb_atoms < r->nb_atoms)
1194 return -1;
1195 if (l->nb_atoms > r->nb_atoms)
1196 return 1;
1197
1198 return 0;
1199}
1200
mingo39aeb522009-09-14 20:04:48 +02001201static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001202{
1203 if (l->total_runtime < r->total_runtime)
1204 return -1;
1205 if (l->total_runtime > r->total_runtime)
1206 return 1;
1207
1208 return 0;
1209}
1210
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001211static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001212{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001213 size_t i;
1214 static struct sort_dimension avg_sort_dimension = {
1215 .name = "avg",
1216 .cmp = avg_cmp,
1217 };
1218 static struct sort_dimension max_sort_dimension = {
1219 .name = "max",
1220 .cmp = max_cmp,
1221 };
1222 static struct sort_dimension pid_sort_dimension = {
1223 .name = "pid",
1224 .cmp = pid_cmp,
1225 };
1226 static struct sort_dimension runtime_sort_dimension = {
1227 .name = "runtime",
1228 .cmp = runtime_cmp,
1229 };
1230 static struct sort_dimension switch_sort_dimension = {
1231 .name = "switch",
1232 .cmp = switch_cmp,
1233 };
1234 struct sort_dimension *available_sorts[] = {
1235 &pid_sort_dimension,
1236 &avg_sort_dimension,
1237 &max_sort_dimension,
1238 &switch_sort_dimension,
1239 &runtime_sort_dimension,
1240 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001241
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001242 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001243 if (!strcmp(available_sorts[i]->name, tok)) {
1244 list_add_tail(&available_sorts[i]->list, list);
1245
1246 return 0;
1247 }
1248 }
1249
1250 return -1;
1251}
1252
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001253static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001254{
1255 struct rb_node *node;
1256
1257 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001258 struct work_atoms *data;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001259 node = rb_first(&sched->atom_root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001260 if (!node)
1261 break;
1262
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001263 rb_erase(node, &sched->atom_root);
mingo39aeb522009-09-14 20:04:48 +02001264 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001265 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001266 }
1267}
1268
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001269static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001270 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001271 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001272 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001273{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001274 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001275
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001276 if (sched->tp_handler->wakeup_event)
1277 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001278
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001279 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001280}
1281
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001282static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1283 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001284{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001285 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1286 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001287 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001288 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001289 s64 delta;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001290 int cpu, this_cpu = sample->cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001291
1292 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1293
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001294 if (this_cpu > sched->max_cpu)
1295 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001296
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001297 timestamp0 = sched->cpu_last_switched[this_cpu];
1298 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001299 if (timestamp0)
1300 delta = timestamp - timestamp0;
1301 else
1302 delta = 0;
1303
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001304 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001305 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001306 return -1;
1307 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001308
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001309 sched_in = machine__findnew_thread(machine, -1, next_pid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001310
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001311 sched->curr_thread[this_cpu] = sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001312
1313 printf(" ");
1314
1315 new_shortname = 0;
1316 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001317 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1318 /*
1319 * Don't allocate a letter-number for swapper:0
1320 * as a shortname. Instead, we use '.' for it.
1321 */
1322 sched_in->shortname[0] = '.';
1323 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001324 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001325 sched_in->shortname[0] = sched->next_shortname1;
1326 sched_in->shortname[1] = sched->next_shortname2;
1327
1328 if (sched->next_shortname1 < 'Z') {
1329 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001330 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001331 sched->next_shortname1 = 'A';
1332 if (sched->next_shortname2 < '9')
1333 sched->next_shortname2++;
1334 else
1335 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001336 }
1337 }
1338 new_shortname = 1;
1339 }
1340
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001341 for (cpu = 0; cpu <= sched->max_cpu; cpu++) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001342 if (cpu != this_cpu)
1343 printf(" ");
1344 else
1345 printf("*");
1346
Dongsheng6bcab4e2014-05-06 14:39:01 +09001347 if (sched->curr_thread[cpu])
1348 printf("%2s ", sched->curr_thread[cpu]->shortname);
1349 else
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001350 printf(" ");
1351 }
1352
1353 printf(" %12.6f secs ", (double)timestamp/1e9);
1354 if (new_shortname) {
1355 printf("%s => %s:%d\n",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001356 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001357 } else {
1358 printf("\n");
1359 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001360
1361 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001362}
1363
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001364static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001365 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001366 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001367 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001368{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001369 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001370 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001371 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1372 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001373
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001374 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001375 /*
1376 * Are we trying to switch away a PID that is
1377 * not current?
1378 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001379 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001380 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001381 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001382
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001383 if (sched->tp_handler->switch_event)
1384 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001385
1386 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001387 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001388}
1389
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001390static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001391 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001392 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001393 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001394{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001395 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001396
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001397 if (sched->tp_handler->runtime_event)
1398 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001399
1400 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001401}
1402
David Aherncb627502013-08-07 22:50:47 -04001403static int perf_sched__process_fork_event(struct perf_tool *tool,
1404 union perf_event *event,
1405 struct perf_sample *sample,
1406 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001407{
1408 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1409
David Aherncb627502013-08-07 22:50:47 -04001410 /* run the fork event through the perf machineruy */
1411 perf_event__process_fork(tool, event, sample, machine);
1412
1413 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001414 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001415 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001416
1417 return 0;
1418}
1419
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001420static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001421 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001422 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001423 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001424{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001425 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001426
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001427 if (sched->tp_handler->migrate_task_event)
1428 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001429
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001430 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001431}
1432
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001433typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001434 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001435 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001436 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001437
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001438static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1439 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001440 struct perf_sample *sample,
1441 struct perf_evsel *evsel,
1442 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001443{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001444 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001445
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001446 if (evsel->handler != NULL) {
1447 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001448 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001449 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001450
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001451 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001452}
1453
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001454static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001455{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001456 const struct perf_evsel_str_handler handlers[] = {
1457 { "sched:sched_switch", process_sched_switch_event, },
1458 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1459 { "sched:sched_wakeup", process_sched_wakeup_event, },
1460 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001461 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1462 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001463 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001464 struct perf_data_file file = {
1465 .path = input_name,
1466 .mode = PERF_DATA_MODE_READ,
1467 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001468 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001469
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001470 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001471 if (session == NULL) {
1472 pr_debug("No Memory for session\n");
1473 return -1;
1474 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001475
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001476 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001477
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001478 if (perf_session__set_tracepoints_handlers(session, handlers))
1479 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001480
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001481 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001482 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001483 if (err) {
1484 pr_err("Failed to process events, error %d", err);
1485 goto out_delete;
1486 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001487
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001488 sched->nr_events = session->evlist->stats.nr_events[0];
1489 sched->nr_lost_events = session->evlist->stats.total_lost;
1490 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001491 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001492
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001493 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001494out_delete:
1495 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001496 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001497}
1498
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001499static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001500{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001501 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001502 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001503 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1504 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001505 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001506 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001507 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001508 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1509 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001510 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001511 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001512 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001513 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1514 sched->nr_context_switch_bugs, sched->nr_timestamps);
1515 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001516 printf(" (due to lost events?)");
1517 printf("\n");
1518 }
1519}
1520
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001521static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001522{
1523 struct rb_node *next;
1524
1525 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001526
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001527 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001528 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001529
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001530 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001531
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001532 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1533 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1534 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001535
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001536 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001537
1538 while (next) {
1539 struct work_atoms *work_list;
1540
1541 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001542 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001543 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001544 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001545 }
1546
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001547 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001548 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001549 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001550
1551 printf(" ---------------------------------------------------\n");
1552
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001553 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001554 printf("\n");
1555
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001556 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001557}
1558
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001559static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001560{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001561 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
Ingo Molnar40749d02009-09-17 18:24:55 +02001562
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001563 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001564 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001565 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001566 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001567 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001568}
1569
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001570static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001571{
1572 unsigned long i;
1573
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001574 calibrate_run_measurement_overhead(sched);
1575 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001576
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001577 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001578
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001579 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001580 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001581
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001582 printf("nr_run_events: %ld\n", sched->nr_run_events);
1583 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1584 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001585
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001586 if (sched->targetless_wakeups)
1587 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1588 if (sched->multitarget_wakeups)
1589 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1590 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001591 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001592 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001593
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001594 print_task_traces(sched);
1595 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001596
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001597 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001598 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001599 for (i = 0; i < sched->replay_repeat; i++)
1600 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001601
1602 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001603}
1604
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001605static void setup_sorting(struct perf_sched *sched, const struct option *options,
1606 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001607{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001608 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001609
1610 for (tok = strtok_r(str, ", ", &tmp);
1611 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001612 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001613 error("Unknown --sort key: `%s'", tok);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001614 usage_with_options(usage_msg, options);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001615 }
1616 }
1617
1618 free(str);
1619
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001620 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001621}
1622
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001623static int __cmd_record(int argc, const char **argv)
1624{
1625 unsigned int rec_argc, i, j;
1626 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001627 const char * const record_args[] = {
1628 "record",
1629 "-a",
1630 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001631 "-m", "1024",
1632 "-c", "1",
1633 "-e", "sched:sched_switch",
1634 "-e", "sched:sched_stat_wait",
1635 "-e", "sched:sched_stat_sleep",
1636 "-e", "sched:sched_stat_iowait",
1637 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001638 "-e", "sched:sched_process_fork",
1639 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001640 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001641 "-e", "sched:sched_migrate_task",
1642 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001643
1644 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1645 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1646
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001647 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001648 return -ENOMEM;
1649
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001650 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1651 rec_argv[i] = strdup(record_args[i]);
1652
1653 for (j = 1; j < (unsigned int)argc; j++, i++)
1654 rec_argv[i] = argv[j];
1655
1656 BUG_ON(i != rec_argc);
1657
1658 return cmd_record(i, rec_argv, NULL);
1659}
1660
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001661int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001662{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001663 const char default_sort_order[] = "avg, max, switch, runtime";
1664 struct perf_sched sched = {
1665 .tool = {
1666 .sample = perf_sched__process_tracepoint_sample,
1667 .comm = perf_event__process_comm,
1668 .lost = perf_event__process_lost,
1669 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001670 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001671 },
1672 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1673 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1674 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1675 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001676 .sort_order = default_sort_order,
1677 .replay_repeat = 10,
1678 .profile_cpu = -1,
1679 .next_shortname1 = 'A',
1680 .next_shortname2 = '0',
1681 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001682 const struct option latency_options[] = {
1683 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1684 "sort by key(s): runtime, switch, avg, max"),
1685 OPT_INCR('v', "verbose", &verbose,
1686 "be more verbose (show symbol address, etc)"),
1687 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1688 "CPU to profile on"),
1689 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1690 "dump raw trace in ASCII"),
1691 OPT_END()
1692 };
1693 const struct option replay_options[] = {
1694 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1695 "repeat the workload replay N times (-1: infinite)"),
1696 OPT_INCR('v', "verbose", &verbose,
1697 "be more verbose (show symbol address, etc)"),
1698 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1699 "dump raw trace in ASCII"),
1700 OPT_END()
1701 };
1702 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001703 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001704 "input file name"),
1705 OPT_INCR('v', "verbose", &verbose,
1706 "be more verbose (show symbol address, etc)"),
1707 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1708 "dump raw trace in ASCII"),
1709 OPT_END()
1710 };
1711 const char * const latency_usage[] = {
1712 "perf sched latency [<options>]",
1713 NULL
1714 };
1715 const char * const replay_usage[] = {
1716 "perf sched replay [<options>]",
1717 NULL
1718 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001719 const char *const sched_subcommands[] = { "record", "latency", "map",
1720 "replay", "script", NULL };
1721 const char *sched_usage[] = {
1722 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001723 NULL
1724 };
1725 struct trace_sched_handler lat_ops = {
1726 .wakeup_event = latency_wakeup_event,
1727 .switch_event = latency_switch_event,
1728 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001729 .migrate_task_event = latency_migrate_task_event,
1730 };
1731 struct trace_sched_handler map_ops = {
1732 .switch_event = map_switch_event,
1733 };
1734 struct trace_sched_handler replay_ops = {
1735 .wakeup_event = replay_wakeup_event,
1736 .switch_event = replay_switch_event,
1737 .fork_event = replay_fork_event,
1738 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001739 unsigned int i;
1740
1741 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1742 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001743
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001744 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1745 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001746 if (!argc)
1747 usage_with_options(sched_usage, sched_options);
1748
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001749 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001750 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001751 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001752 if (!strcmp(argv[0], "script"))
1753 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08001754
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001755 if (!strncmp(argv[0], "rec", 3)) {
1756 return __cmd_record(argc, argv);
1757 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001758 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001759 if (argc > 1) {
1760 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1761 if (argc)
1762 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001763 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001764 setup_sorting(&sched, latency_options, latency_usage);
1765 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001766 } else if (!strcmp(argv[0], "map")) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001767 sched.tp_handler = &map_ops;
1768 setup_sorting(&sched, latency_options, latency_usage);
1769 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001770 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001771 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001772 if (argc) {
1773 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1774 if (argc)
1775 usage_with_options(replay_usage, replay_options);
1776 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001777 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001778 } else {
1779 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001780 }
1781
Ingo Molnarec156762009-09-11 12:12:54 +02001782 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001783}