blob: 64dd94667055d38aa8ea17151882d458c4b8636f [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
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060015#include <subcmd/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;
Josef Bacik2f80dd42015-05-22 09:18:40 -040098 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020099};
100
mingo39aeb522009-09-14 20:04:48 +0200101typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200102
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300103struct perf_sched;
104
105struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300106 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
107 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300108
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300109 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
110 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300111
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300112 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
113 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300114
David Aherncb627502013-08-07 22:50:47 -0400115 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
116 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
117 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300118
119 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300120 struct perf_evsel *evsel,
121 struct perf_sample *sample,
122 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300123};
124
Jiri Olsa99623c62016-04-12 15:29:26 +0200125struct perf_sched_map {
126 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
127 int *comp_cpus;
128 bool comp;
129};
130
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300131struct perf_sched {
132 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300133 const char *sort_order;
134 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800135 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300136 struct task_desc **tasks;
137 const struct trace_sched_handler *tp_handler;
138 pthread_mutex_t start_work_mutex;
139 pthread_mutex_t work_done_wait_mutex;
140 int profile_cpu;
141/*
142 * Track the current task - that way we can know whether there's any
143 * weird events, such as a task being switched away that is not current.
144 */
145 int max_cpu;
146 u32 curr_pid[MAX_CPUS];
147 struct thread *curr_thread[MAX_CPUS];
148 char next_shortname1;
149 char next_shortname2;
150 unsigned int replay_repeat;
151 unsigned long nr_run_events;
152 unsigned long nr_sleep_events;
153 unsigned long nr_wakeup_events;
154 unsigned long nr_sleep_corrections;
155 unsigned long nr_run_events_optimized;
156 unsigned long targetless_wakeups;
157 unsigned long multitarget_wakeups;
158 unsigned long nr_runs;
159 unsigned long nr_timestamps;
160 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300161 unsigned long nr_context_switch_bugs;
162 unsigned long nr_events;
163 unsigned long nr_lost_chunks;
164 unsigned long nr_lost_events;
165 u64 run_measurement_overhead;
166 u64 sleep_measurement_overhead;
167 u64 start_time;
168 u64 cpu_usage;
169 u64 runavg_cpu_usage;
170 u64 parent_cpu_usage;
171 u64 runavg_parent_cpu_usage;
172 u64 sum_runtime;
173 u64 sum_fluct;
174 u64 run_avg;
175 u64 all_runtime;
176 u64 all_count;
177 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400178 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300179 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800180 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400181 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200182 struct perf_sched_map map;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300183};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200184
185static u64 get_nsecs(void)
186{
187 struct timespec ts;
188
189 clock_gettime(CLOCK_MONOTONIC, &ts);
190
191 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
192}
193
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300194static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200195{
196 u64 T0 = get_nsecs(), T1;
197
198 do {
199 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300200 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200201}
202
203static void sleep_nsecs(u64 nsecs)
204{
205 struct timespec ts;
206
207 ts.tv_nsec = nsecs % 999999999;
208 ts.tv_sec = nsecs / 999999999;
209
210 nanosleep(&ts, NULL);
211}
212
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300213static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200214{
215 u64 T0, T1, delta, min_delta = 1000000000ULL;
216 int i;
217
218 for (i = 0; i < 10; i++) {
219 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300220 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200221 T1 = get_nsecs();
222 delta = T1-T0;
223 min_delta = min(min_delta, delta);
224 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300225 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200226
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200227 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200228}
229
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300230static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200231{
232 u64 T0, T1, delta, min_delta = 1000000000ULL;
233 int i;
234
235 for (i = 0; i < 10; i++) {
236 T0 = get_nsecs();
237 sleep_nsecs(10000);
238 T1 = get_nsecs();
239 delta = T1-T0;
240 min_delta = min(min_delta, delta);
241 }
242 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300243 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200244
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200245 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200246}
247
mingo39aeb522009-09-14 20:04:48 +0200248static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200249get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200250{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200251 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200252 unsigned long idx = task->nr_events;
253 size_t size;
254
255 event->timestamp = timestamp;
256 event->nr = idx;
257
258 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200259 size = sizeof(struct sched_atom *) * task->nr_events;
260 task->atoms = realloc(task->atoms, size);
261 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200262
mingo39aeb522009-09-14 20:04:48 +0200263 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200264
265 return event;
266}
267
mingo39aeb522009-09-14 20:04:48 +0200268static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200269{
270 if (!task->nr_events)
271 return NULL;
272
mingo39aeb522009-09-14 20:04:48 +0200273 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200274}
275
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300276static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
277 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200278{
mingo39aeb522009-09-14 20:04:48 +0200279 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200280
281 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200282 * optimize an existing RUN event by merging this one
283 * to it:
284 */
Ingo Molnarec156762009-09-11 12:12:54 +0200285 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300286 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200287 curr_event->duration += duration;
288 return;
289 }
290
291 event = get_new_event(task, timestamp);
292
293 event->type = SCHED_EVENT_RUN;
294 event->duration = duration;
295
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300296 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200297}
298
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300299static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
300 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200301{
mingo39aeb522009-09-14 20:04:48 +0200302 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200303
304 event = get_new_event(task, timestamp);
305 event->type = SCHED_EVENT_WAKEUP;
306 event->wakee = wakee;
307
308 wakee_event = last_event(wakee);
309 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300310 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200311 return;
312 }
313 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300314 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200315 return;
316 }
317
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200318 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200319 sem_init(wakee_event->wait_sem, 0, 0);
320 wakee_event->specific_wait = 1;
321 event->wait_sem = wakee_event->wait_sem;
322
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300323 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200324}
325
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300326static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
327 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200328{
mingo39aeb522009-09-14 20:04:48 +0200329 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200330
331 event->type = SCHED_EVENT_SLEEP;
332
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300333 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200334}
335
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300336static struct task_desc *register_pid(struct perf_sched *sched,
337 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200338{
339 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800340 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200341
Yunlong Songcb06ac22015-03-31 21:46:30 +0800342 if (sched->pid_to_task == NULL) {
343 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
344 pid_max = MAX_PID;
345 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
346 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800347 if (pid >= (unsigned long)pid_max) {
348 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
349 sizeof(struct task_desc *))) == NULL);
350 while (pid >= (unsigned long)pid_max)
351 sched->pid_to_task[pid_max++] = NULL;
352 }
Ingo Molnarec156762009-09-11 12:12:54 +0200353
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300354 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200355
356 if (task)
357 return task;
358
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200359 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200360 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300361 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200362 strcpy(task->comm, comm);
363 /*
364 * every task starts in sleeping state - this gets ignored
365 * if there's no wakeup pointing to this sleep state:
366 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300367 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200368
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300369 sched->pid_to_task[pid] = task;
370 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800371 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300372 BUG_ON(!sched->tasks);
373 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200374
Ingo Molnarad236fd2009-09-11 12:12:54 +0200375 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300376 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200377
378 return task;
379}
380
381
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300382static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200383{
384 struct task_desc *task;
385 unsigned long i;
386
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300387 for (i = 0; i < sched->nr_tasks; i++) {
388 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200389 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200390 task->nr, task->comm, task->pid, task->nr_events);
391 }
392}
393
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300394static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200395{
396 struct task_desc *task1, *task2;
397 unsigned long i, j;
398
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300399 for (i = 0; i < sched->nr_tasks; i++) {
400 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200401 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300402 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200403 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300404 task2 = sched->tasks[j];
405 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200406 }
407}
408
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300409static void perf_sched__process_event(struct perf_sched *sched,
410 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200411{
412 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200413
mingo39aeb522009-09-14 20:04:48 +0200414 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200415 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300416 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200417 break;
418 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200419 if (atom->wait_sem)
420 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200421 BUG_ON(ret);
422 break;
423 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200424 if (atom->wait_sem)
425 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200426 BUG_ON(ret);
427 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200428 case SCHED_EVENT_MIGRATION:
429 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200430 default:
431 BUG_ON(1);
432 }
433}
434
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200435static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200436{
437 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200438 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200439 int err;
440
441 err = getrusage(RUSAGE_SELF, &ru);
442 BUG_ON(err);
443
444 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
445 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
446
447 return sum;
448}
449
Yunlong Song939cda52015-03-31 21:46:34 +0800450static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200451{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800452 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800453 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800454 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800455 struct rlimit limit;
456 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800457
458 memset(&attr, 0, sizeof(attr));
459
460 attr.type = PERF_TYPE_SOFTWARE;
461 attr.config = PERF_COUNT_SW_TASK_CLOCK;
462
Yunlong Song939cda52015-03-31 21:46:34 +0800463force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200464 fd = sys_perf_event_open(&attr, 0, -1, -1,
465 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800466
Yunlong Song1aff59b2015-03-31 21:46:33 +0800467 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800468 if (errno == EMFILE) {
469 if (sched->force) {
470 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
471 limit.rlim_cur += sched->nr_tasks - cur_task;
472 if (limit.rlim_cur > limit.rlim_max) {
473 limit.rlim_max = limit.rlim_cur;
474 need_privilege = true;
475 }
476 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
477 if (need_privilege && errno == EPERM)
478 strcpy(info, "Need privilege\n");
479 } else
480 goto force_again;
481 } else
482 strcpy(info, "Have a try with -f option\n");
483 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900484 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800485 "with %d (%s)\n%s", fd,
486 strerror_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800487 exit(EXIT_FAILURE);
488 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800489 return fd;
490}
491
492static u64 get_cpu_usage_nsec_self(int fd)
493{
494 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200495 int ret;
496
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800497 ret = read(fd, &runtime, sizeof(runtime));
498 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200499
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800500 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200501}
502
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300503struct sched_thread_parms {
504 struct task_desc *task;
505 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800506 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300507};
508
Ingo Molnarec156762009-09-11 12:12:54 +0200509static void *thread_func(void *ctx)
510{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300511 struct sched_thread_parms *parms = ctx;
512 struct task_desc *this_task = parms->task;
513 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200514 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200515 unsigned long i, ret;
516 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800517 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200518
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300519 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300520
Ingo Molnarec156762009-09-11 12:12:54 +0200521 sprintf(comm2, ":%s", this_task->comm);
522 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300523 if (fd < 0)
524 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200525again:
526 ret = sem_post(&this_task->ready_for_work);
527 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300528 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200529 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300530 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200531 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200532
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800533 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200534
535 for (i = 0; i < this_task->nr_events; i++) {
536 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300537 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200538 }
539
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800540 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200541 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200542 ret = sem_post(&this_task->work_done_sem);
543 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200544
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300545 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200546 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300547 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200548 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200549
550 goto again;
551}
552
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200554{
555 struct task_desc *task;
556 pthread_attr_t attr;
557 unsigned long i;
558 int err;
559
560 err = pthread_attr_init(&attr);
561 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200562 err = pthread_attr_setstacksize(&attr,
563 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200564 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300565 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200566 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300567 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200568 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300569 for (i = 0; i < sched->nr_tasks; i++) {
570 struct sched_thread_parms *parms = malloc(sizeof(*parms));
571 BUG_ON(parms == NULL);
572 parms->task = task = sched->tasks[i];
573 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800574 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200575 sem_init(&task->sleep_sem, 0, 0);
576 sem_init(&task->ready_for_work, 0, 0);
577 sem_init(&task->work_done_sem, 0, 0);
578 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300579 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200580 BUG_ON(err);
581 }
582}
583
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300584static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200585{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200586 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200587 struct task_desc *task;
588 unsigned long i, ret;
589
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590 sched->start_time = get_nsecs();
591 sched->cpu_usage = 0;
592 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200593
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300594 for (i = 0; i < sched->nr_tasks; i++) {
595 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200596 ret = sem_wait(&task->ready_for_work);
597 BUG_ON(ret);
598 sem_init(&task->ready_for_work, 0, 0);
599 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300600 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200601 BUG_ON(ret);
602
603 cpu_usage_0 = get_cpu_usage_nsec_parent();
604
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300605 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200606
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300607 for (i = 0; i < sched->nr_tasks; i++) {
608 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200609 ret = sem_wait(&task->work_done_sem);
610 BUG_ON(ret);
611 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200613 task->cpu_usage = 0;
614 }
615
616 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300617 if (!sched->runavg_cpu_usage)
618 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800619 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200620
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300621 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
622 if (!sched->runavg_parent_cpu_usage)
623 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800624 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
625 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200626
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200628 BUG_ON(ret);
629
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300630 for (i = 0; i < sched->nr_tasks; i++) {
631 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200632 sem_init(&task->sleep_sem, 0, 0);
633 task->curr_event = 0;
634 }
635}
636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200638{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500639 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200640
641 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300642 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200643 T1 = get_nsecs();
644
645 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300646 sched->sum_runtime += delta;
647 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200648
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300649 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200650 if (delta < avg_delta)
651 fluct = avg_delta - delta;
652 else
653 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300654 sched->sum_fluct += fluct;
655 if (!sched->run_avg)
656 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800657 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200658
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300659 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200660
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300661 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200662
Ingo Molnarad236fd2009-09-11 12:12:54 +0200663 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300664 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200665
666#if 0
667 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200668 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300669 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200670 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200671 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300672 (double)sched->parent_cpu_usage/1e6,
673 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200674#endif
675
Ingo Molnarad236fd2009-09-11 12:12:54 +0200676 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200677
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300678 if (sched->nr_sleep_corrections)
679 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
680 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200681}
682
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300683static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200684{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200685 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200686
687 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300688 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200689 T1 = get_nsecs();
690
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200691 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200692
693 T0 = get_nsecs();
694 sleep_nsecs(1e6);
695 T1 = get_nsecs();
696
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200697 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200698}
699
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300700static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300701replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300702 struct perf_evsel *evsel, struct perf_sample *sample,
703 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200704{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300705 const char *comm = perf_evsel__strval(evsel, sample, "comm");
706 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200707 struct task_desc *waker, *wakee;
708
709 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300710 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200711
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300712 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200713 }
714
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300715 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300716 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200717
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300718 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300719 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200720}
721
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300722static int replay_switch_event(struct perf_sched *sched,
723 struct perf_evsel *evsel,
724 struct perf_sample *sample,
725 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200726{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300727 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
728 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
729 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
730 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
731 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300732 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300733 u64 timestamp0, timestamp = sample->time;
734 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200735 s64 delta;
736
Ingo Molnarad236fd2009-09-11 12:12:54 +0200737 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300738 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200739
Ingo Molnarfbf94822009-09-11 12:12:54 +0200740 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300741 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200742
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300743 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200744 if (timestamp0)
745 delta = timestamp - timestamp0;
746 else
747 delta = 0;
748
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300749 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900750 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300751 return -1;
752 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200753
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300754 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
755 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200756
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300757 prev = register_pid(sched, prev_pid, prev_comm);
758 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200759
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300760 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200761
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300762 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300763 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300764
765 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200766}
767
David Aherncb627502013-08-07 22:50:47 -0400768static int replay_fork_event(struct perf_sched *sched,
769 union perf_event *event,
770 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200771{
David Aherncb627502013-08-07 22:50:47 -0400772 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300773
Adrian Hunter314add62013-08-27 11:23:03 +0300774 child = machine__findnew_thread(machine, event->fork.pid,
775 event->fork.tid);
776 parent = machine__findnew_thread(machine, event->fork.ppid,
777 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400778
779 if (child == NULL || parent == NULL) {
780 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
781 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300782 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200783 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300784
David Aherncb627502013-08-07 22:50:47 -0400785 if (verbose) {
786 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200787 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
788 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400789 }
790
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200791 register_pid(sched, parent->tid, thread__comm_str(parent));
792 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300793out_put:
794 thread__put(child);
795 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300796 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200797}
798
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200799struct sort_dimension {
800 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200801 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200802 struct list_head list;
803};
804
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200805static int
mingo39aeb522009-09-14 20:04:48 +0200806thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200807{
808 struct sort_dimension *sort;
809 int ret = 0;
810
Ingo Molnarb5fae122009-09-11 12:12:54 +0200811 BUG_ON(list_empty(list));
812
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200813 list_for_each_entry(sort, list, list) {
814 ret = sort->cmp(l, r);
815 if (ret)
816 return ret;
817 }
818
819 return ret;
820}
821
mingo39aeb522009-09-14 20:04:48 +0200822static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200823thread_atoms_search(struct rb_root *root, struct thread *thread,
824 struct list_head *sort_list)
825{
826 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200827 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200828
829 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200830 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200831 int cmp;
832
mingo39aeb522009-09-14 20:04:48 +0200833 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200834
835 cmp = thread_lat_cmp(sort_list, &key, atoms);
836 if (cmp > 0)
837 node = node->rb_left;
838 else if (cmp < 0)
839 node = node->rb_right;
840 else {
841 BUG_ON(thread != atoms->thread);
842 return atoms;
843 }
844 }
845 return NULL;
846}
847
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200848static void
mingo39aeb522009-09-14 20:04:48 +0200849__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200850 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200851{
852 struct rb_node **new = &(root->rb_node), *parent = NULL;
853
854 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200855 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200856 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200857
mingo39aeb522009-09-14 20:04:48 +0200858 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200859 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200860
861 cmp = thread_lat_cmp(sort_list, data, this);
862
863 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200864 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200865 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200866 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200867 }
868
869 rb_link_node(&data->node, parent, new);
870 rb_insert_color(&data->node, root);
871}
872
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300873static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200874{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200875 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300876 if (!atoms) {
877 pr_err("No memory at %s\n", __func__);
878 return -1;
879 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200880
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300881 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200882 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300883 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300884 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200885}
886
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300887static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200888{
889 const char *str = TASK_STATE_TO_CHAR_STR;
890
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300891 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200892}
893
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300894static int
mingo39aeb522009-09-14 20:04:48 +0200895add_sched_out_event(struct work_atoms *atoms,
896 char run_state,
897 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200898{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200899 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300900 if (!atom) {
901 pr_err("Non memory at %s", __func__);
902 return -1;
903 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200904
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200905 atom->sched_out_time = timestamp;
906
mingo39aeb522009-09-14 20:04:48 +0200907 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200908 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200909 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200910 }
911
mingo39aeb522009-09-14 20:04:48 +0200912 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300913 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200914}
915
916static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300917add_runtime_event(struct work_atoms *atoms, u64 delta,
918 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200919{
920 struct work_atom *atom;
921
922 BUG_ON(list_empty(&atoms->work_list));
923
924 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
925
926 atom->runtime += delta;
927 atoms->total_runtime += delta;
928}
929
930static void
931add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200932{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200933 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200934 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935
mingo39aeb522009-09-14 20:04:48 +0200936 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200937 return;
938
mingo39aeb522009-09-14 20:04:48 +0200939 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200940
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200941 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200942 return;
943
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200944 if (timestamp < atom->wake_up_time) {
945 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946 return;
947 }
948
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200949 atom->state = THREAD_SCHED_IN;
950 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200951
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200952 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200953 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100954 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200955 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100956 atoms->max_lat_at = timestamp;
957 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200958 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200959}
960
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300961static int latency_switch_event(struct perf_sched *sched,
962 struct perf_evsel *evsel,
963 struct perf_sample *sample,
964 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200965{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300966 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
967 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
968 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200969 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200970 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300971 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300972 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200973 s64 delta;
974
mingo39aeb522009-09-14 20:04:48 +0200975 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200976
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300977 timestamp0 = sched->cpu_last_switched[cpu];
978 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200979 if (timestamp0)
980 delta = timestamp - timestamp0;
981 else
982 delta = 0;
983
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300984 if (delta < 0) {
985 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
986 return -1;
987 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300989 sched_out = machine__findnew_thread(machine, -1, prev_pid);
990 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300991 if (sched_out == NULL || sched_in == NULL)
992 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200993
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300994 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200995 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300996 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300997 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300998 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300999 if (!out_events) {
1000 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001001 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001002 }
mingo39aeb522009-09-14 20:04:48 +02001003 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001004 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001005 return -1;
mingo39aeb522009-09-14 20:04:48 +02001006
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001007 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001008 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001009 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001010 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001011 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001012 if (!in_events) {
1013 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001014 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001015 }
mingo39aeb522009-09-14 20:04:48 +02001016 /*
1017 * Take came in we have not heard about yet,
1018 * add in an initial atom in runnable state:
1019 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001020 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001021 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001022 }
1023 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001024 err = 0;
1025out_put:
1026 thread__put(sched_out);
1027 thread__put(sched_in);
1028 return err;
mingo39aeb522009-09-14 20:04:48 +02001029}
1030
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001031static int latency_runtime_event(struct perf_sched *sched,
1032 struct perf_evsel *evsel,
1033 struct perf_sample *sample,
1034 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001035{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001036 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1037 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001038 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001039 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001040 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001041 int cpu = sample->cpu, err = -1;
1042
1043 if (thread == NULL)
1044 return -1;
mingo39aeb522009-09-14 20:04:48 +02001045
1046 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001047 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001048 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001049 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001050 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001051 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001052 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001053 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001054 }
1055 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001056 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001057 }
1058
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001059 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001060 err = 0;
1061out_put:
1062 thread__put(thread);
1063 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001064}
1065
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001066static int latency_wakeup_event(struct perf_sched *sched,
1067 struct perf_evsel *evsel,
1068 struct perf_sample *sample,
1069 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001070{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001071 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001072 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001073 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001074 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001075 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001076 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001077
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001078 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001079 if (wakee == NULL)
1080 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001081 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001082 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001083 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001084 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001085 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001086 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001087 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001088 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001089 }
1090 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001091 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001092 }
1093
mingo39aeb522009-09-14 20:04:48 +02001094 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001095
mingo39aeb522009-09-14 20:04:48 +02001096 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001097
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001098 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001099 * As we do not guarantee the wakeup event happens when
1100 * task is out of run queue, also may happen when task is
1101 * on run queue and wakeup only change ->state to TASK_RUNNING,
1102 * then we should not set the ->wake_up_time when wake up a
1103 * task which is on run queue.
1104 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001105 * You WILL be missing events if you've recorded only
1106 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001107 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001108 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001109 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001110 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001111
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001112 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001113 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001114 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001115 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001116 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001117
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001118 atom->state = THREAD_WAIT_CPU;
1119 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001120out_ok:
1121 err = 0;
1122out_put:
1123 thread__put(wakee);
1124 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001125}
1126
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001127static int latency_migrate_task_event(struct perf_sched *sched,
1128 struct perf_evsel *evsel,
1129 struct perf_sample *sample,
1130 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001131{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001132 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001133 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001134 struct work_atoms *atoms;
1135 struct work_atom *atom;
1136 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001137 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001138
1139 /*
1140 * Only need to worry about migration when profiling one CPU.
1141 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001142 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001143 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001144
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001145 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001146 if (migrant == NULL)
1147 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001148 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001149 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001150 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001151 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001152 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001153 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001154 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001155 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001156 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001157 }
1158 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001159 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001160 }
1161
1162 BUG_ON(list_empty(&atoms->work_list));
1163
1164 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1165 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1166
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001167 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001168
1169 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001170 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001171 err = 0;
1172out_put:
1173 thread__put(migrant);
1174 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001175}
1176
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001177static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001178{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001179 int i;
1180 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001181 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001182
mingo39aeb522009-09-14 20:04:48 +02001183 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001184 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001185 /*
1186 * Ignore idle threads:
1187 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001188 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001189 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001190
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001191 sched->all_runtime += work_list->total_runtime;
1192 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001193
Josef Bacik2f80dd42015-05-22 09:18:40 -04001194 if (work_list->num_merged > 1)
1195 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1196 else
1197 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001198
mingo08f69e62009-09-14 18:30:44 +02001199 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001200 printf(" ");
1201
mingo39aeb522009-09-14 20:04:48 +02001202 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001204 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 +02001205 (double)work_list->total_runtime / 1e6,
1206 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001207 (double)work_list->max_lat / 1e6,
1208 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209}
1210
mingo39aeb522009-09-14 20:04:48 +02001211static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001212{
Jiri Olsa0014de12015-11-02 12:10:25 +01001213 if (l->thread == r->thread)
1214 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001215 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001216 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001217 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001218 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001219 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001220}
1221
mingo39aeb522009-09-14 20:04:48 +02001222static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001223{
1224 u64 avgl, avgr;
1225
1226 if (!l->nb_atoms)
1227 return -1;
1228
1229 if (!r->nb_atoms)
1230 return 1;
1231
1232 avgl = l->total_lat / l->nb_atoms;
1233 avgr = r->total_lat / r->nb_atoms;
1234
1235 if (avgl < avgr)
1236 return -1;
1237 if (avgl > avgr)
1238 return 1;
1239
1240 return 0;
1241}
1242
mingo39aeb522009-09-14 20:04:48 +02001243static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001244{
1245 if (l->max_lat < r->max_lat)
1246 return -1;
1247 if (l->max_lat > r->max_lat)
1248 return 1;
1249
1250 return 0;
1251}
1252
mingo39aeb522009-09-14 20:04:48 +02001253static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001254{
1255 if (l->nb_atoms < r->nb_atoms)
1256 return -1;
1257 if (l->nb_atoms > r->nb_atoms)
1258 return 1;
1259
1260 return 0;
1261}
1262
mingo39aeb522009-09-14 20:04:48 +02001263static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001264{
1265 if (l->total_runtime < r->total_runtime)
1266 return -1;
1267 if (l->total_runtime > r->total_runtime)
1268 return 1;
1269
1270 return 0;
1271}
1272
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001273static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001274{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001275 size_t i;
1276 static struct sort_dimension avg_sort_dimension = {
1277 .name = "avg",
1278 .cmp = avg_cmp,
1279 };
1280 static struct sort_dimension max_sort_dimension = {
1281 .name = "max",
1282 .cmp = max_cmp,
1283 };
1284 static struct sort_dimension pid_sort_dimension = {
1285 .name = "pid",
1286 .cmp = pid_cmp,
1287 };
1288 static struct sort_dimension runtime_sort_dimension = {
1289 .name = "runtime",
1290 .cmp = runtime_cmp,
1291 };
1292 static struct sort_dimension switch_sort_dimension = {
1293 .name = "switch",
1294 .cmp = switch_cmp,
1295 };
1296 struct sort_dimension *available_sorts[] = {
1297 &pid_sort_dimension,
1298 &avg_sort_dimension,
1299 &max_sort_dimension,
1300 &switch_sort_dimension,
1301 &runtime_sort_dimension,
1302 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001303
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001304 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001305 if (!strcmp(available_sorts[i]->name, tok)) {
1306 list_add_tail(&available_sorts[i]->list, list);
1307
1308 return 0;
1309 }
1310 }
1311
1312 return -1;
1313}
1314
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001315static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001316{
1317 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001318 struct rb_root *root = &sched->atom_root;
1319again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001320 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001321 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001322 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001323 if (!node)
1324 break;
1325
Josef Bacik2f80dd42015-05-22 09:18:40 -04001326 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001327 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001328 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001329 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001330 if (root == &sched->atom_root) {
1331 root = &sched->merged_atom_root;
1332 goto again;
1333 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001334}
1335
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001336static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001337 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001338 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001339 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001340{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001341 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001342
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001343 if (sched->tp_handler->wakeup_event)
1344 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001345
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001346 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001347}
1348
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001349static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1350 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001351{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001352 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1353 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001354 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001355 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001356 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001357 int i, this_cpu = sample->cpu;
1358 int cpus_nr;
1359 bool new_cpu = false;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001360
1361 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1362
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001363 if (this_cpu > sched->max_cpu)
1364 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001365
Jiri Olsa99623c62016-04-12 15:29:26 +02001366 if (sched->map.comp) {
1367 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1368 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1369 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1370 new_cpu = true;
1371 }
1372 } else
1373 cpus_nr = sched->max_cpu;
1374
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001375 timestamp0 = sched->cpu_last_switched[this_cpu];
1376 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001377 if (timestamp0)
1378 delta = timestamp - timestamp0;
1379 else
1380 delta = 0;
1381
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001382 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001383 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001384 return -1;
1385 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001386
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001387 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001388 if (sched_in == NULL)
1389 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001390
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001391 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392
1393 printf(" ");
1394
1395 new_shortname = 0;
1396 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001397 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1398 /*
1399 * Don't allocate a letter-number for swapper:0
1400 * as a shortname. Instead, we use '.' for it.
1401 */
1402 sched_in->shortname[0] = '.';
1403 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001404 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001405 sched_in->shortname[0] = sched->next_shortname1;
1406 sched_in->shortname[1] = sched->next_shortname2;
1407
1408 if (sched->next_shortname1 < 'Z') {
1409 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001410 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001411 sched->next_shortname1 = 'A';
1412 if (sched->next_shortname2 < '9')
1413 sched->next_shortname2++;
1414 else
1415 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001416 }
1417 }
1418 new_shortname = 1;
1419 }
1420
Jiri Olsa99623c62016-04-12 15:29:26 +02001421 for (i = 0; i < cpus_nr; i++) {
1422 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
1423
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001424 if (cpu != this_cpu)
1425 printf(" ");
1426 else
1427 printf("*");
1428
Dongsheng6bcab4e2014-05-06 14:39:01 +09001429 if (sched->curr_thread[cpu])
1430 printf("%2s ", sched->curr_thread[cpu]->shortname);
1431 else
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432 printf(" ");
1433 }
1434
1435 printf(" %12.6f secs ", (double)timestamp/1e9);
1436 if (new_shortname) {
Jiri Olsa99623c62016-04-12 15:29:26 +02001437 printf("%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001438 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001439 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001440
Jiri Olsa99623c62016-04-12 15:29:26 +02001441 if (sched->map.comp && new_cpu)
1442 printf(" (CPU %d)", this_cpu);
1443
1444 printf("\n");
1445
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001446 thread__put(sched_in);
1447
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001448 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001449}
1450
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001451static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001452 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001453 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001454 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001455{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001456 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001457 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001458 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1459 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001460
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001461 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001462 /*
1463 * Are we trying to switch away a PID that is
1464 * not current?
1465 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001466 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001467 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001468 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001469
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001470 if (sched->tp_handler->switch_event)
1471 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001472
1473 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001474 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001475}
1476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001477static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001478 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001479 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001480 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001481{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001482 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001483
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001484 if (sched->tp_handler->runtime_event)
1485 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001486
1487 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001488}
1489
David Aherncb627502013-08-07 22:50:47 -04001490static int perf_sched__process_fork_event(struct perf_tool *tool,
1491 union perf_event *event,
1492 struct perf_sample *sample,
1493 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001494{
1495 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1496
David Aherncb627502013-08-07 22:50:47 -04001497 /* run the fork event through the perf machineruy */
1498 perf_event__process_fork(tool, event, sample, machine);
1499
1500 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001501 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001502 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001503
1504 return 0;
1505}
1506
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001507static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001508 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001509 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001510 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001511{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001512 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001513
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001514 if (sched->tp_handler->migrate_task_event)
1515 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001516
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001517 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001518}
1519
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001520typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001521 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001522 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001523 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001524
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001525static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1526 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001527 struct perf_sample *sample,
1528 struct perf_evsel *evsel,
1529 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001530{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001531 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001532
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001533 if (evsel->handler != NULL) {
1534 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001535 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001536 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001537
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001538 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001539}
1540
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001541static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001542{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001543 const struct perf_evsel_str_handler handlers[] = {
1544 { "sched:sched_switch", process_sched_switch_event, },
1545 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1546 { "sched:sched_wakeup", process_sched_wakeup_event, },
1547 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001548 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1549 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001550 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001551 struct perf_data_file file = {
1552 .path = input_name,
1553 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001554 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001555 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001556 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001557
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001558 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001559 if (session == NULL) {
1560 pr_debug("No Memory for session\n");
1561 return -1;
1562 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001563
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001564 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001565
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001566 if (perf_session__set_tracepoints_handlers(session, handlers))
1567 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001568
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001569 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001570 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001571 if (err) {
1572 pr_err("Failed to process events, error %d", err);
1573 goto out_delete;
1574 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001575
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001576 sched->nr_events = session->evlist->stats.nr_events[0];
1577 sched->nr_lost_events = session->evlist->stats.total_lost;
1578 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001579 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001580
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001581 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001582out_delete:
1583 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001584 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001585}
1586
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001587static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001588{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001589 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001590 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001591 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1592 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001593 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001594 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001595 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001596 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1597 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001598 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001599 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001600 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001601 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1602 sched->nr_context_switch_bugs, sched->nr_timestamps);
1603 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001604 printf(" (due to lost events?)");
1605 printf("\n");
1606 }
1607}
1608
Josef Bacik2f80dd42015-05-22 09:18:40 -04001609static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
1610{
1611 struct rb_node **new = &(root->rb_node), *parent = NULL;
1612 struct work_atoms *this;
1613 const char *comm = thread__comm_str(data->thread), *this_comm;
1614
1615 while (*new) {
1616 int cmp;
1617
1618 this = container_of(*new, struct work_atoms, node);
1619 parent = *new;
1620
1621 this_comm = thread__comm_str(this->thread);
1622 cmp = strcmp(comm, this_comm);
1623 if (cmp > 0) {
1624 new = &((*new)->rb_left);
1625 } else if (cmp < 0) {
1626 new = &((*new)->rb_right);
1627 } else {
1628 this->num_merged++;
1629 this->total_runtime += data->total_runtime;
1630 this->nb_atoms += data->nb_atoms;
1631 this->total_lat += data->total_lat;
1632 list_splice(&data->work_list, &this->work_list);
1633 if (this->max_lat < data->max_lat) {
1634 this->max_lat = data->max_lat;
1635 this->max_lat_at = data->max_lat_at;
1636 }
1637 zfree(&data);
1638 return;
1639 }
1640 }
1641
1642 data->num_merged++;
1643 rb_link_node(&data->node, parent, new);
1644 rb_insert_color(&data->node, root);
1645}
1646
1647static void perf_sched__merge_lat(struct perf_sched *sched)
1648{
1649 struct work_atoms *data;
1650 struct rb_node *node;
1651
1652 if (sched->skip_merge)
1653 return;
1654
1655 while ((node = rb_first(&sched->atom_root))) {
1656 rb_erase(node, &sched->atom_root);
1657 data = rb_entry(node, struct work_atoms, node);
1658 __merge_work_atoms(&sched->merged_atom_root, data);
1659 }
1660}
1661
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001662static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001663{
1664 struct rb_node *next;
1665
1666 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001667
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001668 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001669 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001670
Josef Bacik2f80dd42015-05-22 09:18:40 -04001671 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001672 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001673
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001674 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1675 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1676 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001677
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001678 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001679
1680 while (next) {
1681 struct work_atoms *work_list;
1682
1683 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001684 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001685 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001686 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001687 }
1688
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001689 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001690 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001691 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001692
1693 printf(" ---------------------------------------------------\n");
1694
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001695 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001696 printf("\n");
1697
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001698 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001699}
1700
Jiri Olsa99623c62016-04-12 15:29:26 +02001701static int setup_map_cpus(struct perf_sched *sched)
1702{
1703 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1704
1705 if (sched->map.comp) {
1706 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
1707 return sched->map.comp_cpus ? 0 : -1;
1708 }
1709
1710 return 0;
1711}
1712
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001713static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001714{
Jiri Olsa99623c62016-04-12 15:29:26 +02001715 if (setup_map_cpus(sched))
1716 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02001717
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001718 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001719 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001720 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001721 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001722 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001723}
1724
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001725static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001726{
1727 unsigned long i;
1728
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001729 calibrate_run_measurement_overhead(sched);
1730 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001731
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001732 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001734 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001735 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001736
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001737 printf("nr_run_events: %ld\n", sched->nr_run_events);
1738 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1739 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001740
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001741 if (sched->targetless_wakeups)
1742 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1743 if (sched->multitarget_wakeups)
1744 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1745 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001746 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001747 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001748
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001749 print_task_traces(sched);
1750 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001751
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001752 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001753 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001754 for (i = 0; i < sched->replay_repeat; i++)
1755 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001756
1757 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001758}
1759
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001760static void setup_sorting(struct perf_sched *sched, const struct option *options,
1761 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001762{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001763 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001764
1765 for (tok = strtok_r(str, ", ", &tmp);
1766 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001767 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001768 usage_with_options_msg(usage_msg, options,
1769 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001770 }
1771 }
1772
1773 free(str);
1774
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001775 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001776}
1777
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001778static int __cmd_record(int argc, const char **argv)
1779{
1780 unsigned int rec_argc, i, j;
1781 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001782 const char * const record_args[] = {
1783 "record",
1784 "-a",
1785 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001786 "-m", "1024",
1787 "-c", "1",
1788 "-e", "sched:sched_switch",
1789 "-e", "sched:sched_stat_wait",
1790 "-e", "sched:sched_stat_sleep",
1791 "-e", "sched:sched_stat_iowait",
1792 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001793 "-e", "sched:sched_process_fork",
1794 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001795 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001796 "-e", "sched:sched_migrate_task",
1797 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001798
1799 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1800 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1801
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001802 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001803 return -ENOMEM;
1804
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001805 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1806 rec_argv[i] = strdup(record_args[i]);
1807
1808 for (j = 1; j < (unsigned int)argc; j++, i++)
1809 rec_argv[i] = argv[j];
1810
1811 BUG_ON(i != rec_argc);
1812
1813 return cmd_record(i, rec_argv, NULL);
1814}
1815
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001816int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001817{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001818 const char default_sort_order[] = "avg, max, switch, runtime";
1819 struct perf_sched sched = {
1820 .tool = {
1821 .sample = perf_sched__process_tracepoint_sample,
1822 .comm = perf_event__process_comm,
1823 .lost = perf_event__process_lost,
1824 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001825 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001826 },
1827 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1828 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1829 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1830 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001831 .sort_order = default_sort_order,
1832 .replay_repeat = 10,
1833 .profile_cpu = -1,
1834 .next_shortname1 = 'A',
1835 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04001836 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001837 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001838 const struct option latency_options[] = {
1839 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1840 "sort by key(s): runtime, switch, avg, max"),
1841 OPT_INCR('v', "verbose", &verbose,
1842 "be more verbose (show symbol address, etc)"),
1843 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1844 "CPU to profile on"),
1845 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1846 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04001847 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
1848 "latency stats per pid instead of per comm"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001849 OPT_END()
1850 };
1851 const struct option replay_options[] = {
1852 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1853 "repeat the workload replay N times (-1: infinite)"),
1854 OPT_INCR('v', "verbose", &verbose,
1855 "be more verbose (show symbol address, etc)"),
1856 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1857 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08001858 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001859 OPT_END()
1860 };
1861 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001862 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001863 "input file name"),
1864 OPT_INCR('v', "verbose", &verbose,
1865 "be more verbose (show symbol address, etc)"),
1866 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1867 "dump raw trace in ASCII"),
1868 OPT_END()
1869 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001870 const struct option map_options[] = {
1871 OPT_BOOLEAN(0, "compact", &sched.map.comp,
1872 "map output in compact mode"),
1873 OPT_END()
1874 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001875 const char * const latency_usage[] = {
1876 "perf sched latency [<options>]",
1877 NULL
1878 };
1879 const char * const replay_usage[] = {
1880 "perf sched replay [<options>]",
1881 NULL
1882 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001883 const char * const map_usage[] = {
1884 "perf sched map [<options>]",
1885 NULL
1886 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001887 const char *const sched_subcommands[] = { "record", "latency", "map",
1888 "replay", "script", NULL };
1889 const char *sched_usage[] = {
1890 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001891 NULL
1892 };
1893 struct trace_sched_handler lat_ops = {
1894 .wakeup_event = latency_wakeup_event,
1895 .switch_event = latency_switch_event,
1896 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001897 .migrate_task_event = latency_migrate_task_event,
1898 };
1899 struct trace_sched_handler map_ops = {
1900 .switch_event = map_switch_event,
1901 };
1902 struct trace_sched_handler replay_ops = {
1903 .wakeup_event = replay_wakeup_event,
1904 .switch_event = replay_switch_event,
1905 .fork_event = replay_fork_event,
1906 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001907 unsigned int i;
1908
1909 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1910 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001911
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001912 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1913 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001914 if (!argc)
1915 usage_with_options(sched_usage, sched_options);
1916
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001917 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001918 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001919 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001920 if (!strcmp(argv[0], "script"))
1921 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001922
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001923 if (!strncmp(argv[0], "rec", 3)) {
1924 return __cmd_record(argc, argv);
1925 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001926 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001927 if (argc > 1) {
1928 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1929 if (argc)
1930 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001931 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001932 setup_sorting(&sched, latency_options, latency_usage);
1933 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001934 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02001935 if (argc) {
1936 argc = parse_options(argc, argv, map_options, replay_usage, 0);
1937 if (argc)
1938 usage_with_options(map_usage, map_options);
1939 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001940 sched.tp_handler = &map_ops;
1941 setup_sorting(&sched, latency_options, latency_usage);
1942 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001943 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001944 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001945 if (argc) {
1946 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1947 if (argc)
1948 usage_with_options(replay_usage, replay_options);
1949 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001950 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001951 } else {
1952 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001953 }
1954
Ingo Molnarec156762009-09-11 12:12:54 +02001955 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001956}