blob: 9ef28973f1983c80aaddec96bf9ded31745da691 [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"
Jiri Olsa8cd91192016-04-12 15:29:27 +020014#include "util/color.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020015
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020017#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020018
Ingo Molnar0a02ad92009-09-11 12:12:54 +020019#include "util/debug.h"
20
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020021#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020022#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020023
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020024#include <semaphore.h>
25#include <pthread.h>
26#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080027#include <api/fs/fs.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020028
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020029#define PR_SET_NAME 15 /* Set process name */
30#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020031#define COMM_LEN 20
32#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080033#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020034
mingo39aeb522009-09-14 20:04:48 +020035struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020036
37struct task_desc {
38 unsigned long nr;
39 unsigned long pid;
40 char comm[COMM_LEN];
41
42 unsigned long nr_events;
43 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020044 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020045
46 pthread_t thread;
47 sem_t sleep_sem;
48
49 sem_t ready_for_work;
50 sem_t work_done_sem;
51
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020052 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020053};
54
55enum sched_event_type {
56 SCHED_EVENT_RUN,
57 SCHED_EVENT_SLEEP,
58 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020059 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020060};
61
mingo39aeb522009-09-14 20:04:48 +020062struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020063 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030064 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020065 u64 timestamp;
66 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020067 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020068 sem_t *wait_sem;
69 struct task_desc *wakee;
70};
71
Dongshenge936e8e2014-05-05 16:05:54 +090072#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020073
74enum thread_state {
75 THREAD_SLEEPING = 0,
76 THREAD_WAIT_CPU,
77 THREAD_SCHED_IN,
78 THREAD_IGNORE
79};
80
81struct work_atom {
82 struct list_head list;
83 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020084 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020085 u64 wake_up_time;
86 u64 sched_in_time;
87 u64 runtime;
88};
89
mingo39aeb522009-09-14 20:04:48 +020090struct work_atoms {
91 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020092 struct thread *thread;
93 struct rb_node node;
94 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +010095 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020096 u64 total_lat;
97 u64 nb_atoms;
98 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -040099 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200100};
101
mingo39aeb522009-09-14 20:04:48 +0200102typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200103
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300104struct perf_sched;
105
106struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300107 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
108 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300109
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300110 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
111 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300112
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300113 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
114 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300115
David Aherncb627502013-08-07 22:50:47 -0400116 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
117 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
118 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300119
120 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300121 struct perf_evsel *evsel,
122 struct perf_sample *sample,
123 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300124};
125
Jiri Olsa99623c62016-04-12 15:29:26 +0200126struct perf_sched_map {
127 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
128 int *comp_cpus;
129 bool comp;
130};
131
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300132struct perf_sched {
133 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300134 const char *sort_order;
135 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800136 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300137 struct task_desc **tasks;
138 const struct trace_sched_handler *tp_handler;
139 pthread_mutex_t start_work_mutex;
140 pthread_mutex_t work_done_wait_mutex;
141 int profile_cpu;
142/*
143 * Track the current task - that way we can know whether there's any
144 * weird events, such as a task being switched away that is not current.
145 */
146 int max_cpu;
147 u32 curr_pid[MAX_CPUS];
148 struct thread *curr_thread[MAX_CPUS];
149 char next_shortname1;
150 char next_shortname2;
151 unsigned int replay_repeat;
152 unsigned long nr_run_events;
153 unsigned long nr_sleep_events;
154 unsigned long nr_wakeup_events;
155 unsigned long nr_sleep_corrections;
156 unsigned long nr_run_events_optimized;
157 unsigned long targetless_wakeups;
158 unsigned long multitarget_wakeups;
159 unsigned long nr_runs;
160 unsigned long nr_timestamps;
161 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300162 unsigned long nr_context_switch_bugs;
163 unsigned long nr_events;
164 unsigned long nr_lost_chunks;
165 unsigned long nr_lost_events;
166 u64 run_measurement_overhead;
167 u64 sleep_measurement_overhead;
168 u64 start_time;
169 u64 cpu_usage;
170 u64 runavg_cpu_usage;
171 u64 parent_cpu_usage;
172 u64 runavg_parent_cpu_usage;
173 u64 sum_runtime;
174 u64 sum_fluct;
175 u64 run_avg;
176 u64 all_runtime;
177 u64 all_count;
178 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400179 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300180 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800181 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400182 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200183 struct perf_sched_map map;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300184};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200185
186static u64 get_nsecs(void)
187{
188 struct timespec ts;
189
190 clock_gettime(CLOCK_MONOTONIC, &ts);
191
192 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
193}
194
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300195static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200196{
197 u64 T0 = get_nsecs(), T1;
198
199 do {
200 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300201 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200202}
203
204static void sleep_nsecs(u64 nsecs)
205{
206 struct timespec ts;
207
208 ts.tv_nsec = nsecs % 999999999;
209 ts.tv_sec = nsecs / 999999999;
210
211 nanosleep(&ts, NULL);
212}
213
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300214static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200215{
216 u64 T0, T1, delta, min_delta = 1000000000ULL;
217 int i;
218
219 for (i = 0; i < 10; i++) {
220 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300221 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200222 T1 = get_nsecs();
223 delta = T1-T0;
224 min_delta = min(min_delta, delta);
225 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300226 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200227
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200228 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200229}
230
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300231static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200232{
233 u64 T0, T1, delta, min_delta = 1000000000ULL;
234 int i;
235
236 for (i = 0; i < 10; i++) {
237 T0 = get_nsecs();
238 sleep_nsecs(10000);
239 T1 = get_nsecs();
240 delta = T1-T0;
241 min_delta = min(min_delta, delta);
242 }
243 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300244 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200245
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200246 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200247}
248
mingo39aeb522009-09-14 20:04:48 +0200249static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200250get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200251{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200252 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200253 unsigned long idx = task->nr_events;
254 size_t size;
255
256 event->timestamp = timestamp;
257 event->nr = idx;
258
259 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200260 size = sizeof(struct sched_atom *) * task->nr_events;
261 task->atoms = realloc(task->atoms, size);
262 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200263
mingo39aeb522009-09-14 20:04:48 +0200264 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200265
266 return event;
267}
268
mingo39aeb522009-09-14 20:04:48 +0200269static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200270{
271 if (!task->nr_events)
272 return NULL;
273
mingo39aeb522009-09-14 20:04:48 +0200274 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200275}
276
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300277static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
278 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200279{
mingo39aeb522009-09-14 20:04:48 +0200280 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200281
282 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200283 * optimize an existing RUN event by merging this one
284 * to it:
285 */
Ingo Molnarec156762009-09-11 12:12:54 +0200286 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300287 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200288 curr_event->duration += duration;
289 return;
290 }
291
292 event = get_new_event(task, timestamp);
293
294 event->type = SCHED_EVENT_RUN;
295 event->duration = duration;
296
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300297 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200298}
299
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300300static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
301 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200302{
mingo39aeb522009-09-14 20:04:48 +0200303 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200304
305 event = get_new_event(task, timestamp);
306 event->type = SCHED_EVENT_WAKEUP;
307 event->wakee = wakee;
308
309 wakee_event = last_event(wakee);
310 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300311 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200312 return;
313 }
314 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300315 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200316 return;
317 }
318
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200319 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200320 sem_init(wakee_event->wait_sem, 0, 0);
321 wakee_event->specific_wait = 1;
322 event->wait_sem = wakee_event->wait_sem;
323
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300324 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200325}
326
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300327static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
328 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200329{
mingo39aeb522009-09-14 20:04:48 +0200330 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200331
332 event->type = SCHED_EVENT_SLEEP;
333
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300334 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200335}
336
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300337static struct task_desc *register_pid(struct perf_sched *sched,
338 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200339{
340 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800341 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200342
Yunlong Songcb06ac22015-03-31 21:46:30 +0800343 if (sched->pid_to_task == NULL) {
344 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
345 pid_max = MAX_PID;
346 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
347 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800348 if (pid >= (unsigned long)pid_max) {
349 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
350 sizeof(struct task_desc *))) == NULL);
351 while (pid >= (unsigned long)pid_max)
352 sched->pid_to_task[pid_max++] = NULL;
353 }
Ingo Molnarec156762009-09-11 12:12:54 +0200354
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300355 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200356
357 if (task)
358 return task;
359
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200360 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200361 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300362 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200363 strcpy(task->comm, comm);
364 /*
365 * every task starts in sleeping state - this gets ignored
366 * if there's no wakeup pointing to this sleep state:
367 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300368 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200369
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300370 sched->pid_to_task[pid] = task;
371 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800372 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300373 BUG_ON(!sched->tasks);
374 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200375
Ingo Molnarad236fd2009-09-11 12:12:54 +0200376 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300377 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200378
379 return task;
380}
381
382
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300383static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200384{
385 struct task_desc *task;
386 unsigned long i;
387
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300388 for (i = 0; i < sched->nr_tasks; i++) {
389 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200390 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200391 task->nr, task->comm, task->pid, task->nr_events);
392 }
393}
394
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300395static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200396{
397 struct task_desc *task1, *task2;
398 unsigned long i, j;
399
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300400 for (i = 0; i < sched->nr_tasks; i++) {
401 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200402 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300403 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200404 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300405 task2 = sched->tasks[j];
406 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200407 }
408}
409
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300410static void perf_sched__process_event(struct perf_sched *sched,
411 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200412{
413 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200414
mingo39aeb522009-09-14 20:04:48 +0200415 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200416 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300417 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200418 break;
419 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200420 if (atom->wait_sem)
421 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200422 BUG_ON(ret);
423 break;
424 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200425 if (atom->wait_sem)
426 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200427 BUG_ON(ret);
428 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200429 case SCHED_EVENT_MIGRATION:
430 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200431 default:
432 BUG_ON(1);
433 }
434}
435
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200436static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200437{
438 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200439 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200440 int err;
441
442 err = getrusage(RUSAGE_SELF, &ru);
443 BUG_ON(err);
444
445 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
446 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
447
448 return sum;
449}
450
Yunlong Song939cda52015-03-31 21:46:34 +0800451static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200452{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800453 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800454 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800455 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800456 struct rlimit limit;
457 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800458
459 memset(&attr, 0, sizeof(attr));
460
461 attr.type = PERF_TYPE_SOFTWARE;
462 attr.config = PERF_COUNT_SW_TASK_CLOCK;
463
Yunlong Song939cda52015-03-31 21:46:34 +0800464force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200465 fd = sys_perf_event_open(&attr, 0, -1, -1,
466 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800467
Yunlong Song1aff59b2015-03-31 21:46:33 +0800468 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800469 if (errno == EMFILE) {
470 if (sched->force) {
471 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
472 limit.rlim_cur += sched->nr_tasks - cur_task;
473 if (limit.rlim_cur > limit.rlim_max) {
474 limit.rlim_max = limit.rlim_cur;
475 need_privilege = true;
476 }
477 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
478 if (need_privilege && errno == EPERM)
479 strcpy(info, "Need privilege\n");
480 } else
481 goto force_again;
482 } else
483 strcpy(info, "Have a try with -f option\n");
484 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900485 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800486 "with %d (%s)\n%s", fd,
487 strerror_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800488 exit(EXIT_FAILURE);
489 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800490 return fd;
491}
492
493static u64 get_cpu_usage_nsec_self(int fd)
494{
495 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200496 int ret;
497
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800498 ret = read(fd, &runtime, sizeof(runtime));
499 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200500
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800501 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200502}
503
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300504struct sched_thread_parms {
505 struct task_desc *task;
506 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800507 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300508};
509
Ingo Molnarec156762009-09-11 12:12:54 +0200510static void *thread_func(void *ctx)
511{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300512 struct sched_thread_parms *parms = ctx;
513 struct task_desc *this_task = parms->task;
514 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200515 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200516 unsigned long i, ret;
517 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800518 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200519
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300520 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300521
Ingo Molnarec156762009-09-11 12:12:54 +0200522 sprintf(comm2, ":%s", this_task->comm);
523 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300524 if (fd < 0)
525 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200526again:
527 ret = sem_post(&this_task->ready_for_work);
528 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300529 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200530 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300531 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200532 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200533
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800534 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200535
536 for (i = 0; i < this_task->nr_events; i++) {
537 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300538 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200539 }
540
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800541 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200542 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200543 ret = sem_post(&this_task->work_done_sem);
544 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200545
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300546 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200547 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300548 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200549 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200550
551 goto again;
552}
553
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300554static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200555{
556 struct task_desc *task;
557 pthread_attr_t attr;
558 unsigned long i;
559 int err;
560
561 err = pthread_attr_init(&attr);
562 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200563 err = pthread_attr_setstacksize(&attr,
564 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200565 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300566 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200567 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300568 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200569 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300570 for (i = 0; i < sched->nr_tasks; i++) {
571 struct sched_thread_parms *parms = malloc(sizeof(*parms));
572 BUG_ON(parms == NULL);
573 parms->task = task = sched->tasks[i];
574 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800575 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200576 sem_init(&task->sleep_sem, 0, 0);
577 sem_init(&task->ready_for_work, 0, 0);
578 sem_init(&task->work_done_sem, 0, 0);
579 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200581 BUG_ON(err);
582 }
583}
584
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300585static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200586{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200587 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200588 struct task_desc *task;
589 unsigned long i, ret;
590
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300591 sched->start_time = get_nsecs();
592 sched->cpu_usage = 0;
593 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200594
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595 for (i = 0; i < sched->nr_tasks; i++) {
596 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200597 ret = sem_wait(&task->ready_for_work);
598 BUG_ON(ret);
599 sem_init(&task->ready_for_work, 0, 0);
600 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300601 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200602 BUG_ON(ret);
603
604 cpu_usage_0 = get_cpu_usage_nsec_parent();
605
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300606 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200607
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300608 for (i = 0; i < sched->nr_tasks; i++) {
609 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200610 ret = sem_wait(&task->work_done_sem);
611 BUG_ON(ret);
612 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200614 task->cpu_usage = 0;
615 }
616
617 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300618 if (!sched->runavg_cpu_usage)
619 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800620 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 +0200621
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300622 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
623 if (!sched->runavg_parent_cpu_usage)
624 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800625 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
626 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200627
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300628 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200629 BUG_ON(ret);
630
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300631 for (i = 0; i < sched->nr_tasks; i++) {
632 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200633 sem_init(&task->sleep_sem, 0, 0);
634 task->curr_event = 0;
635 }
636}
637
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300638static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200639{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500640 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200641
642 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200644 T1 = get_nsecs();
645
646 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300647 sched->sum_runtime += delta;
648 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200649
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200651 if (delta < avg_delta)
652 fluct = avg_delta - delta;
653 else
654 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 sched->sum_fluct += fluct;
656 if (!sched->run_avg)
657 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800658 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200659
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300660 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200661
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300662 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200663
Ingo Molnarad236fd2009-09-11 12:12:54 +0200664 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300665 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200666
667#if 0
668 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200669 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200671 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200672 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 (double)sched->parent_cpu_usage/1e6,
674 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200675#endif
676
Ingo Molnarad236fd2009-09-11 12:12:54 +0200677 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200678
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300679 if (sched->nr_sleep_corrections)
680 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
681 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200682}
683
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200685{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200686 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200687
688 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200690 T1 = get_nsecs();
691
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200692 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200693
694 T0 = get_nsecs();
695 sleep_nsecs(1e6);
696 T1 = get_nsecs();
697
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200698 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200699}
700
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300701static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300702replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300703 struct perf_evsel *evsel, struct perf_sample *sample,
704 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200705{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300706 const char *comm = perf_evsel__strval(evsel, sample, "comm");
707 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200708 struct task_desc *waker, *wakee;
709
710 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300711 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200712
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300713 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200714 }
715
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300716 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300717 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200718
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300719 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300720 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200721}
722
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300723static int replay_switch_event(struct perf_sched *sched,
724 struct perf_evsel *evsel,
725 struct perf_sample *sample,
726 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200727{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300728 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
729 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
730 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
731 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
732 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300733 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300734 u64 timestamp0, timestamp = sample->time;
735 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200736 s64 delta;
737
Ingo Molnarad236fd2009-09-11 12:12:54 +0200738 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300739 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200740
Ingo Molnarfbf94822009-09-11 12:12:54 +0200741 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300742 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200743
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300744 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200745 if (timestamp0)
746 delta = timestamp - timestamp0;
747 else
748 delta = 0;
749
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300750 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900751 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300752 return -1;
753 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200754
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300755 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
756 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200757
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300758 prev = register_pid(sched, prev_pid, prev_comm);
759 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200760
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300761 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200762
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300763 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300764 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300765
766 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200767}
768
David Aherncb627502013-08-07 22:50:47 -0400769static int replay_fork_event(struct perf_sched *sched,
770 union perf_event *event,
771 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200772{
David Aherncb627502013-08-07 22:50:47 -0400773 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300774
Adrian Hunter314add62013-08-27 11:23:03 +0300775 child = machine__findnew_thread(machine, event->fork.pid,
776 event->fork.tid);
777 parent = machine__findnew_thread(machine, event->fork.ppid,
778 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400779
780 if (child == NULL || parent == NULL) {
781 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
782 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300783 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200784 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300785
David Aherncb627502013-08-07 22:50:47 -0400786 if (verbose) {
787 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200788 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
789 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400790 }
791
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200792 register_pid(sched, parent->tid, thread__comm_str(parent));
793 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300794out_put:
795 thread__put(child);
796 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300797 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200798}
799
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200800struct sort_dimension {
801 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200802 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200803 struct list_head list;
804};
805
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200806static int
mingo39aeb522009-09-14 20:04:48 +0200807thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200808{
809 struct sort_dimension *sort;
810 int ret = 0;
811
Ingo Molnarb5fae122009-09-11 12:12:54 +0200812 BUG_ON(list_empty(list));
813
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200814 list_for_each_entry(sort, list, list) {
815 ret = sort->cmp(l, r);
816 if (ret)
817 return ret;
818 }
819
820 return ret;
821}
822
mingo39aeb522009-09-14 20:04:48 +0200823static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200824thread_atoms_search(struct rb_root *root, struct thread *thread,
825 struct list_head *sort_list)
826{
827 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200828 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200829
830 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200831 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200832 int cmp;
833
mingo39aeb522009-09-14 20:04:48 +0200834 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200835
836 cmp = thread_lat_cmp(sort_list, &key, atoms);
837 if (cmp > 0)
838 node = node->rb_left;
839 else if (cmp < 0)
840 node = node->rb_right;
841 else {
842 BUG_ON(thread != atoms->thread);
843 return atoms;
844 }
845 }
846 return NULL;
847}
848
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200849static void
mingo39aeb522009-09-14 20:04:48 +0200850__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200851 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200852{
853 struct rb_node **new = &(root->rb_node), *parent = NULL;
854
855 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200856 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200857 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200858
mingo39aeb522009-09-14 20:04:48 +0200859 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200860 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200861
862 cmp = thread_lat_cmp(sort_list, data, this);
863
864 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200865 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200866 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200867 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200868 }
869
870 rb_link_node(&data->node, parent, new);
871 rb_insert_color(&data->node, root);
872}
873
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300874static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200875{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200876 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300877 if (!atoms) {
878 pr_err("No memory at %s\n", __func__);
879 return -1;
880 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200881
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300882 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200883 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300884 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300885 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200886}
887
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300888static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200889{
890 const char *str = TASK_STATE_TO_CHAR_STR;
891
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300892 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200893}
894
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300895static int
mingo39aeb522009-09-14 20:04:48 +0200896add_sched_out_event(struct work_atoms *atoms,
897 char run_state,
898 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200899{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200900 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300901 if (!atom) {
902 pr_err("Non memory at %s", __func__);
903 return -1;
904 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200906 atom->sched_out_time = timestamp;
907
mingo39aeb522009-09-14 20:04:48 +0200908 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200909 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200910 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200911 }
912
mingo39aeb522009-09-14 20:04:48 +0200913 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300914 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915}
916
917static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300918add_runtime_event(struct work_atoms *atoms, u64 delta,
919 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200920{
921 struct work_atom *atom;
922
923 BUG_ON(list_empty(&atoms->work_list));
924
925 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
926
927 atom->runtime += delta;
928 atoms->total_runtime += delta;
929}
930
931static void
932add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200933{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200934 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200935 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200936
mingo39aeb522009-09-14 20:04:48 +0200937 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938 return;
939
mingo39aeb522009-09-14 20:04:48 +0200940 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200942 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200943 return;
944
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200945 if (timestamp < atom->wake_up_time) {
946 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947 return;
948 }
949
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200950 atom->state = THREAD_SCHED_IN;
951 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200952
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200953 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200954 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100955 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200956 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100957 atoms->max_lat_at = timestamp;
958 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200959 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200960}
961
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300962static int latency_switch_event(struct perf_sched *sched,
963 struct perf_evsel *evsel,
964 struct perf_sample *sample,
965 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200966{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300967 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
968 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
969 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200970 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200971 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300972 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300973 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200974 s64 delta;
975
mingo39aeb522009-09-14 20:04:48 +0200976 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200977
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300978 timestamp0 = sched->cpu_last_switched[cpu];
979 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200980 if (timestamp0)
981 delta = timestamp - timestamp0;
982 else
983 delta = 0;
984
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300985 if (delta < 0) {
986 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
987 return -1;
988 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200989
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300990 sched_out = machine__findnew_thread(machine, -1, prev_pid);
991 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300992 if (sched_out == NULL || sched_in == NULL)
993 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200994
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300995 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +0200996 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300997 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300998 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300999 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001000 if (!out_events) {
1001 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001002 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001003 }
mingo39aeb522009-09-14 20:04:48 +02001004 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001005 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001006 return -1;
mingo39aeb522009-09-14 20:04:48 +02001007
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001008 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001009 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001010 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001011 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001012 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001013 if (!in_events) {
1014 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001015 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001016 }
mingo39aeb522009-09-14 20:04:48 +02001017 /*
1018 * Take came in we have not heard about yet,
1019 * add in an initial atom in runnable state:
1020 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001021 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001022 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001023 }
1024 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001025 err = 0;
1026out_put:
1027 thread__put(sched_out);
1028 thread__put(sched_in);
1029 return err;
mingo39aeb522009-09-14 20:04:48 +02001030}
1031
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001032static int latency_runtime_event(struct perf_sched *sched,
1033 struct perf_evsel *evsel,
1034 struct perf_sample *sample,
1035 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001036{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001037 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1038 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001039 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001040 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001041 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001042 int cpu = sample->cpu, err = -1;
1043
1044 if (thread == NULL)
1045 return -1;
mingo39aeb522009-09-14 20:04:48 +02001046
1047 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001048 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001049 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001050 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001051 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001052 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001053 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001054 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001055 }
1056 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001057 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001058 }
1059
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001060 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001061 err = 0;
1062out_put:
1063 thread__put(thread);
1064 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001065}
1066
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001067static int latency_wakeup_event(struct perf_sched *sched,
1068 struct perf_evsel *evsel,
1069 struct perf_sample *sample,
1070 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001071{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001072 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001073 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001074 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001075 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001076 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001077 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001078
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001079 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001080 if (wakee == NULL)
1081 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001082 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001083 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001084 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001085 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001086 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001087 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001088 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001089 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001090 }
1091 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001092 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001093 }
1094
mingo39aeb522009-09-14 20:04:48 +02001095 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001096
mingo39aeb522009-09-14 20:04:48 +02001097 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001098
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001099 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001100 * As we do not guarantee the wakeup event happens when
1101 * task is out of run queue, also may happen when task is
1102 * on run queue and wakeup only change ->state to TASK_RUNNING,
1103 * then we should not set the ->wake_up_time when wake up a
1104 * task which is on run queue.
1105 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001106 * You WILL be missing events if you've recorded only
1107 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001108 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001109 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001110 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001111 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001112
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001113 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001114 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001115 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001116 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001117 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001118
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001119 atom->state = THREAD_WAIT_CPU;
1120 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001121out_ok:
1122 err = 0;
1123out_put:
1124 thread__put(wakee);
1125 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001126}
1127
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001128static int latency_migrate_task_event(struct perf_sched *sched,
1129 struct perf_evsel *evsel,
1130 struct perf_sample *sample,
1131 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001132{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001133 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001134 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001135 struct work_atoms *atoms;
1136 struct work_atom *atom;
1137 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001138 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001139
1140 /*
1141 * Only need to worry about migration when profiling one CPU.
1142 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001143 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001144 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001145
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001146 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001147 if (migrant == NULL)
1148 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001149 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001150 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001151 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001152 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001153 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001154 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001155 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001156 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001157 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001158 }
1159 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001160 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001161 }
1162
1163 BUG_ON(list_empty(&atoms->work_list));
1164
1165 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1166 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1167
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001168 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001169
1170 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001171 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001172 err = 0;
1173out_put:
1174 thread__put(migrant);
1175 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001176}
1177
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001178static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001179{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001180 int i;
1181 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001182 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001183
mingo39aeb522009-09-14 20:04:48 +02001184 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001185 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001186 /*
1187 * Ignore idle threads:
1188 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001189 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001190 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001191
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001192 sched->all_runtime += work_list->total_runtime;
1193 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001194
Josef Bacik2f80dd42015-05-22 09:18:40 -04001195 if (work_list->num_merged > 1)
1196 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1197 else
1198 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001199
mingo08f69e62009-09-14 18:30:44 +02001200 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001201 printf(" ");
1202
mingo39aeb522009-09-14 20:04:48 +02001203 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001204
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001205 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 +02001206 (double)work_list->total_runtime / 1e6,
1207 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001208 (double)work_list->max_lat / 1e6,
1209 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001210}
1211
mingo39aeb522009-09-14 20:04:48 +02001212static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001213{
Jiri Olsa0014de12015-11-02 12:10:25 +01001214 if (l->thread == r->thread)
1215 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001216 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001217 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001218 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001219 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001220 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001221}
1222
mingo39aeb522009-09-14 20:04:48 +02001223static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001224{
1225 u64 avgl, avgr;
1226
1227 if (!l->nb_atoms)
1228 return -1;
1229
1230 if (!r->nb_atoms)
1231 return 1;
1232
1233 avgl = l->total_lat / l->nb_atoms;
1234 avgr = r->total_lat / r->nb_atoms;
1235
1236 if (avgl < avgr)
1237 return -1;
1238 if (avgl > avgr)
1239 return 1;
1240
1241 return 0;
1242}
1243
mingo39aeb522009-09-14 20:04:48 +02001244static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001245{
1246 if (l->max_lat < r->max_lat)
1247 return -1;
1248 if (l->max_lat > r->max_lat)
1249 return 1;
1250
1251 return 0;
1252}
1253
mingo39aeb522009-09-14 20:04:48 +02001254static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001255{
1256 if (l->nb_atoms < r->nb_atoms)
1257 return -1;
1258 if (l->nb_atoms > r->nb_atoms)
1259 return 1;
1260
1261 return 0;
1262}
1263
mingo39aeb522009-09-14 20:04:48 +02001264static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265{
1266 if (l->total_runtime < r->total_runtime)
1267 return -1;
1268 if (l->total_runtime > r->total_runtime)
1269 return 1;
1270
1271 return 0;
1272}
1273
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001274static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001275{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001276 size_t i;
1277 static struct sort_dimension avg_sort_dimension = {
1278 .name = "avg",
1279 .cmp = avg_cmp,
1280 };
1281 static struct sort_dimension max_sort_dimension = {
1282 .name = "max",
1283 .cmp = max_cmp,
1284 };
1285 static struct sort_dimension pid_sort_dimension = {
1286 .name = "pid",
1287 .cmp = pid_cmp,
1288 };
1289 static struct sort_dimension runtime_sort_dimension = {
1290 .name = "runtime",
1291 .cmp = runtime_cmp,
1292 };
1293 static struct sort_dimension switch_sort_dimension = {
1294 .name = "switch",
1295 .cmp = switch_cmp,
1296 };
1297 struct sort_dimension *available_sorts[] = {
1298 &pid_sort_dimension,
1299 &avg_sort_dimension,
1300 &max_sort_dimension,
1301 &switch_sort_dimension,
1302 &runtime_sort_dimension,
1303 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001304
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001305 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001306 if (!strcmp(available_sorts[i]->name, tok)) {
1307 list_add_tail(&available_sorts[i]->list, list);
1308
1309 return 0;
1310 }
1311 }
1312
1313 return -1;
1314}
1315
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001316static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001317{
1318 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001319 struct rb_root *root = &sched->atom_root;
1320again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001321 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001322 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001323 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001324 if (!node)
1325 break;
1326
Josef Bacik2f80dd42015-05-22 09:18:40 -04001327 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001328 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001329 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001330 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001331 if (root == &sched->atom_root) {
1332 root = &sched->merged_atom_root;
1333 goto again;
1334 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001335}
1336
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001337static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001338 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001339 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001340 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001341{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001342 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001343
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001344 if (sched->tp_handler->wakeup_event)
1345 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001346
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001347 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001348}
1349
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001350static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1351 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001352{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001353 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1354 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001355 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001356 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001357 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001358 int i, this_cpu = sample->cpu;
1359 int cpus_nr;
1360 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001361 const char *color = PERF_COLOR_NORMAL;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001362
1363 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1364
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001365 if (this_cpu > sched->max_cpu)
1366 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001367
Jiri Olsa99623c62016-04-12 15:29:26 +02001368 if (sched->map.comp) {
1369 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1370 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1371 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1372 new_cpu = true;
1373 }
1374 } else
1375 cpus_nr = sched->max_cpu;
1376
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001377 timestamp0 = sched->cpu_last_switched[this_cpu];
1378 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001379 if (timestamp0)
1380 delta = timestamp - timestamp0;
1381 else
1382 delta = 0;
1383
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001384 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001385 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001386 return -1;
1387 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001388
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001389 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001390 if (sched_in == NULL)
1391 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001393 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001394
1395 printf(" ");
1396
1397 new_shortname = 0;
1398 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001399 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1400 /*
1401 * Don't allocate a letter-number for swapper:0
1402 * as a shortname. Instead, we use '.' for it.
1403 */
1404 sched_in->shortname[0] = '.';
1405 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001406 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001407 sched_in->shortname[0] = sched->next_shortname1;
1408 sched_in->shortname[1] = sched->next_shortname2;
1409
1410 if (sched->next_shortname1 < 'Z') {
1411 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001412 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001413 sched->next_shortname1 = 'A';
1414 if (sched->next_shortname2 < '9')
1415 sched->next_shortname2++;
1416 else
1417 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001418 }
1419 }
1420 new_shortname = 1;
1421 }
1422
Jiri Olsa99623c62016-04-12 15:29:26 +02001423 for (i = 0; i < cpus_nr; i++) {
1424 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
1425
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001426 if (cpu != this_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001427 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001428 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001429 color_fprintf(stdout, color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001430
Dongsheng6bcab4e2014-05-06 14:39:01 +09001431 if (sched->curr_thread[cpu])
Jiri Olsa8cd91192016-04-12 15:29:27 +02001432 color_fprintf(stdout, color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001433 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001434 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001435 }
1436
Jiri Olsa8cd91192016-04-12 15:29:27 +02001437 color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp/1e9);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001438 if (new_shortname) {
Jiri Olsa8cd91192016-04-12 15:29:27 +02001439 color_fprintf(stdout, color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001440 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001441 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001442
Jiri Olsa99623c62016-04-12 15:29:26 +02001443 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001444 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001445
Jiri Olsa8cd91192016-04-12 15:29:27 +02001446 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001447
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001448 thread__put(sched_in);
1449
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001450 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001451}
1452
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001453static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001454 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001455 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001456 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001457{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001458 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001459 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001460 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1461 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001462
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001463 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001464 /*
1465 * Are we trying to switch away a PID that is
1466 * not current?
1467 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001468 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001469 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001470 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001471
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001472 if (sched->tp_handler->switch_event)
1473 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001474
1475 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001476 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001477}
1478
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001479static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001480 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001481 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001482 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001483{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001484 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001485
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001486 if (sched->tp_handler->runtime_event)
1487 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001488
1489 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001490}
1491
David Aherncb627502013-08-07 22:50:47 -04001492static int perf_sched__process_fork_event(struct perf_tool *tool,
1493 union perf_event *event,
1494 struct perf_sample *sample,
1495 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001496{
1497 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1498
David Aherncb627502013-08-07 22:50:47 -04001499 /* run the fork event through the perf machineruy */
1500 perf_event__process_fork(tool, event, sample, machine);
1501
1502 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001503 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001504 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001505
1506 return 0;
1507}
1508
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001509static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001510 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001511 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001512 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001513{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001514 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001515
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001516 if (sched->tp_handler->migrate_task_event)
1517 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001518
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001519 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001520}
1521
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001522typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001523 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001524 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001525 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001526
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001527static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1528 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001529 struct perf_sample *sample,
1530 struct perf_evsel *evsel,
1531 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001532{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001533 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001534
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001535 if (evsel->handler != NULL) {
1536 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001537 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001538 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001539
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001540 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001541}
1542
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001543static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001544{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001545 const struct perf_evsel_str_handler handlers[] = {
1546 { "sched:sched_switch", process_sched_switch_event, },
1547 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1548 { "sched:sched_wakeup", process_sched_wakeup_event, },
1549 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001550 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1551 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001552 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001553 struct perf_data_file file = {
1554 .path = input_name,
1555 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001556 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001557 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001558 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001559
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001560 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001561 if (session == NULL) {
1562 pr_debug("No Memory for session\n");
1563 return -1;
1564 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001565
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001566 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001567
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001568 if (perf_session__set_tracepoints_handlers(session, handlers))
1569 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001570
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001571 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001572 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001573 if (err) {
1574 pr_err("Failed to process events, error %d", err);
1575 goto out_delete;
1576 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001577
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001578 sched->nr_events = session->evlist->stats.nr_events[0];
1579 sched->nr_lost_events = session->evlist->stats.total_lost;
1580 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001581 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001582
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001583 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001584out_delete:
1585 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001586 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001587}
1588
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001589static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001590{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001591 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001592 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001593 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1594 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001595 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001596 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001597 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001598 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1599 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001600 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001601 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001602 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001603 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1604 sched->nr_context_switch_bugs, sched->nr_timestamps);
1605 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001606 printf(" (due to lost events?)");
1607 printf("\n");
1608 }
1609}
1610
Josef Bacik2f80dd42015-05-22 09:18:40 -04001611static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
1612{
1613 struct rb_node **new = &(root->rb_node), *parent = NULL;
1614 struct work_atoms *this;
1615 const char *comm = thread__comm_str(data->thread), *this_comm;
1616
1617 while (*new) {
1618 int cmp;
1619
1620 this = container_of(*new, struct work_atoms, node);
1621 parent = *new;
1622
1623 this_comm = thread__comm_str(this->thread);
1624 cmp = strcmp(comm, this_comm);
1625 if (cmp > 0) {
1626 new = &((*new)->rb_left);
1627 } else if (cmp < 0) {
1628 new = &((*new)->rb_right);
1629 } else {
1630 this->num_merged++;
1631 this->total_runtime += data->total_runtime;
1632 this->nb_atoms += data->nb_atoms;
1633 this->total_lat += data->total_lat;
1634 list_splice(&data->work_list, &this->work_list);
1635 if (this->max_lat < data->max_lat) {
1636 this->max_lat = data->max_lat;
1637 this->max_lat_at = data->max_lat_at;
1638 }
1639 zfree(&data);
1640 return;
1641 }
1642 }
1643
1644 data->num_merged++;
1645 rb_link_node(&data->node, parent, new);
1646 rb_insert_color(&data->node, root);
1647}
1648
1649static void perf_sched__merge_lat(struct perf_sched *sched)
1650{
1651 struct work_atoms *data;
1652 struct rb_node *node;
1653
1654 if (sched->skip_merge)
1655 return;
1656
1657 while ((node = rb_first(&sched->atom_root))) {
1658 rb_erase(node, &sched->atom_root);
1659 data = rb_entry(node, struct work_atoms, node);
1660 __merge_work_atoms(&sched->merged_atom_root, data);
1661 }
1662}
1663
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001664static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001665{
1666 struct rb_node *next;
1667
1668 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001669
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001670 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001671 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001672
Josef Bacik2f80dd42015-05-22 09:18:40 -04001673 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001674 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001675
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001676 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1677 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1678 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001679
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001680 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001681
1682 while (next) {
1683 struct work_atoms *work_list;
1684
1685 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001686 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001687 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001688 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001689 }
1690
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001691 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001692 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001693 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001694
1695 printf(" ---------------------------------------------------\n");
1696
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001697 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001698 printf("\n");
1699
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001700 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001701}
1702
Jiri Olsa99623c62016-04-12 15:29:26 +02001703static int setup_map_cpus(struct perf_sched *sched)
1704{
1705 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1706
1707 if (sched->map.comp) {
1708 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
1709 return sched->map.comp_cpus ? 0 : -1;
1710 }
1711
1712 return 0;
1713}
1714
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001715static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001716{
Jiri Olsa99623c62016-04-12 15:29:26 +02001717 if (setup_map_cpus(sched))
1718 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02001719
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001720 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001721 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001722 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001723 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001724 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001725}
1726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001727static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001728{
1729 unsigned long i;
1730
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001731 calibrate_run_measurement_overhead(sched);
1732 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001734 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001735
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001736 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001737 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001738
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001739 printf("nr_run_events: %ld\n", sched->nr_run_events);
1740 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1741 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001742
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001743 if (sched->targetless_wakeups)
1744 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1745 if (sched->multitarget_wakeups)
1746 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1747 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001748 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001749 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001750
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001751 print_task_traces(sched);
1752 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001753
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001754 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001755 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001756 for (i = 0; i < sched->replay_repeat; i++)
1757 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001758
1759 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001760}
1761
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001762static void setup_sorting(struct perf_sched *sched, const struct option *options,
1763 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001764{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001765 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001766
1767 for (tok = strtok_r(str, ", ", &tmp);
1768 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001769 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001770 usage_with_options_msg(usage_msg, options,
1771 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001772 }
1773 }
1774
1775 free(str);
1776
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001777 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001778}
1779
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001780static int __cmd_record(int argc, const char **argv)
1781{
1782 unsigned int rec_argc, i, j;
1783 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001784 const char * const record_args[] = {
1785 "record",
1786 "-a",
1787 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001788 "-m", "1024",
1789 "-c", "1",
1790 "-e", "sched:sched_switch",
1791 "-e", "sched:sched_stat_wait",
1792 "-e", "sched:sched_stat_sleep",
1793 "-e", "sched:sched_stat_iowait",
1794 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001795 "-e", "sched:sched_process_fork",
1796 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001797 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001798 "-e", "sched:sched_migrate_task",
1799 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001800
1801 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1802 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1803
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001804 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001805 return -ENOMEM;
1806
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001807 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1808 rec_argv[i] = strdup(record_args[i]);
1809
1810 for (j = 1; j < (unsigned int)argc; j++, i++)
1811 rec_argv[i] = argv[j];
1812
1813 BUG_ON(i != rec_argc);
1814
1815 return cmd_record(i, rec_argv, NULL);
1816}
1817
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001818int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001819{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001820 const char default_sort_order[] = "avg, max, switch, runtime";
1821 struct perf_sched sched = {
1822 .tool = {
1823 .sample = perf_sched__process_tracepoint_sample,
1824 .comm = perf_event__process_comm,
1825 .lost = perf_event__process_lost,
1826 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001827 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001828 },
1829 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1830 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1831 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1832 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001833 .sort_order = default_sort_order,
1834 .replay_repeat = 10,
1835 .profile_cpu = -1,
1836 .next_shortname1 = 'A',
1837 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04001838 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001839 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001840 const struct option latency_options[] = {
1841 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1842 "sort by key(s): runtime, switch, avg, max"),
1843 OPT_INCR('v', "verbose", &verbose,
1844 "be more verbose (show symbol address, etc)"),
1845 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1846 "CPU to profile on"),
1847 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1848 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04001849 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
1850 "latency stats per pid instead of per comm"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001851 OPT_END()
1852 };
1853 const struct option replay_options[] = {
1854 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1855 "repeat the workload replay N times (-1: infinite)"),
1856 OPT_INCR('v', "verbose", &verbose,
1857 "be more verbose (show symbol address, etc)"),
1858 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1859 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08001860 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001861 OPT_END()
1862 };
1863 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001864 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001865 "input file name"),
1866 OPT_INCR('v', "verbose", &verbose,
1867 "be more verbose (show symbol address, etc)"),
1868 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1869 "dump raw trace in ASCII"),
1870 OPT_END()
1871 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001872 const struct option map_options[] = {
1873 OPT_BOOLEAN(0, "compact", &sched.map.comp,
1874 "map output in compact mode"),
1875 OPT_END()
1876 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001877 const char * const latency_usage[] = {
1878 "perf sched latency [<options>]",
1879 NULL
1880 };
1881 const char * const replay_usage[] = {
1882 "perf sched replay [<options>]",
1883 NULL
1884 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001885 const char * const map_usage[] = {
1886 "perf sched map [<options>]",
1887 NULL
1888 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001889 const char *const sched_subcommands[] = { "record", "latency", "map",
1890 "replay", "script", NULL };
1891 const char *sched_usage[] = {
1892 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001893 NULL
1894 };
1895 struct trace_sched_handler lat_ops = {
1896 .wakeup_event = latency_wakeup_event,
1897 .switch_event = latency_switch_event,
1898 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001899 .migrate_task_event = latency_migrate_task_event,
1900 };
1901 struct trace_sched_handler map_ops = {
1902 .switch_event = map_switch_event,
1903 };
1904 struct trace_sched_handler replay_ops = {
1905 .wakeup_event = replay_wakeup_event,
1906 .switch_event = replay_switch_event,
1907 .fork_event = replay_fork_event,
1908 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001909 unsigned int i;
1910
1911 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1912 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001913
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001914 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1915 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001916 if (!argc)
1917 usage_with_options(sched_usage, sched_options);
1918
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001919 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001920 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001921 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001922 if (!strcmp(argv[0], "script"))
1923 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001924
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001925 if (!strncmp(argv[0], "rec", 3)) {
1926 return __cmd_record(argc, argv);
1927 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001928 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001929 if (argc > 1) {
1930 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1931 if (argc)
1932 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001933 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001934 setup_sorting(&sched, latency_options, latency_usage);
1935 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001936 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02001937 if (argc) {
1938 argc = parse_options(argc, argv, map_options, replay_usage, 0);
1939 if (argc)
1940 usage_with_options(map_usage, map_options);
1941 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001942 sched.tp_handler = &map_ops;
1943 setup_sorting(&sched, latency_options, latency_usage);
1944 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001945 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001946 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001947 if (argc) {
1948 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1949 if (argc)
1950 usage_with_options(replay_usage, replay_options);
1951 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001952 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001953 } else {
1954 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001955 }
1956
Ingo Molnarec156762009-09-11 12:12:54 +02001957 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001958}