blob: b5361a1d20e141d9d8003ac1bab917871ca26c7e [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 Olsaa151a372016-04-12 15:29:29 +020014#include "util/thread_map.h"
Jiri Olsa8cd91192016-04-12 15:29:27 +020015#include "util/color.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020016
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060017#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020018#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020019
Ingo Molnar0a02ad92009-09-11 12:12:54 +020020#include "util/debug.h"
21
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020022#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020023#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020024
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020025#include <semaphore.h>
26#include <pthread.h>
27#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080028#include <api/fs/fs.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020029
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020030#define PR_SET_NAME 15 /* Set process name */
31#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020032#define COMM_LEN 20
33#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080034#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020035
mingo39aeb522009-09-14 20:04:48 +020036struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020037
38struct task_desc {
39 unsigned long nr;
40 unsigned long pid;
41 char comm[COMM_LEN];
42
43 unsigned long nr_events;
44 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020045 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020046
47 pthread_t thread;
48 sem_t sleep_sem;
49
50 sem_t ready_for_work;
51 sem_t work_done_sem;
52
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020053 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020054};
55
56enum sched_event_type {
57 SCHED_EVENT_RUN,
58 SCHED_EVENT_SLEEP,
59 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020060 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020061};
62
mingo39aeb522009-09-14 20:04:48 +020063struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020064 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030065 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020066 u64 timestamp;
67 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020068 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020069 sem_t *wait_sem;
70 struct task_desc *wakee;
71};
72
Dongshenge936e8e2014-05-05 16:05:54 +090073#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020074
75enum thread_state {
76 THREAD_SLEEPING = 0,
77 THREAD_WAIT_CPU,
78 THREAD_SCHED_IN,
79 THREAD_IGNORE
80};
81
82struct work_atom {
83 struct list_head list;
84 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020085 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020086 u64 wake_up_time;
87 u64 sched_in_time;
88 u64 runtime;
89};
90
mingo39aeb522009-09-14 20:04:48 +020091struct work_atoms {
92 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020093 struct thread *thread;
94 struct rb_node node;
95 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +010096 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097 u64 total_lat;
98 u64 nb_atoms;
99 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400100 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200101};
102
mingo39aeb522009-09-14 20:04:48 +0200103typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200104
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300105struct perf_sched;
106
107struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300108 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
109 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300110
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300111 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
112 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300113
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300114 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
115 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300116
David Aherncb627502013-08-07 22:50:47 -0400117 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
118 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
119 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300120
121 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300122 struct perf_evsel *evsel,
123 struct perf_sample *sample,
124 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300125};
126
Jiri Olsaa151a372016-04-12 15:29:29 +0200127#define COLOR_PIDS PERF_COLOR_BLUE
128
Jiri Olsa99623c62016-04-12 15:29:26 +0200129struct perf_sched_map {
130 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
131 int *comp_cpus;
132 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200133 struct thread_map *color_pids;
134 const char *color_pids_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200135};
136
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300137struct perf_sched {
138 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300139 const char *sort_order;
140 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800141 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300142 struct task_desc **tasks;
143 const struct trace_sched_handler *tp_handler;
144 pthread_mutex_t start_work_mutex;
145 pthread_mutex_t work_done_wait_mutex;
146 int profile_cpu;
147/*
148 * Track the current task - that way we can know whether there's any
149 * weird events, such as a task being switched away that is not current.
150 */
151 int max_cpu;
152 u32 curr_pid[MAX_CPUS];
153 struct thread *curr_thread[MAX_CPUS];
154 char next_shortname1;
155 char next_shortname2;
156 unsigned int replay_repeat;
157 unsigned long nr_run_events;
158 unsigned long nr_sleep_events;
159 unsigned long nr_wakeup_events;
160 unsigned long nr_sleep_corrections;
161 unsigned long nr_run_events_optimized;
162 unsigned long targetless_wakeups;
163 unsigned long multitarget_wakeups;
164 unsigned long nr_runs;
165 unsigned long nr_timestamps;
166 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300167 unsigned long nr_context_switch_bugs;
168 unsigned long nr_events;
169 unsigned long nr_lost_chunks;
170 unsigned long nr_lost_events;
171 u64 run_measurement_overhead;
172 u64 sleep_measurement_overhead;
173 u64 start_time;
174 u64 cpu_usage;
175 u64 runavg_cpu_usage;
176 u64 parent_cpu_usage;
177 u64 runavg_parent_cpu_usage;
178 u64 sum_runtime;
179 u64 sum_fluct;
180 u64 run_avg;
181 u64 all_runtime;
182 u64 all_count;
183 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400184 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300185 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800186 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400187 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200188 struct perf_sched_map map;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300189};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200190
191static u64 get_nsecs(void)
192{
193 struct timespec ts;
194
195 clock_gettime(CLOCK_MONOTONIC, &ts);
196
197 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
198}
199
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300200static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200201{
202 u64 T0 = get_nsecs(), T1;
203
204 do {
205 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300206 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200207}
208
209static void sleep_nsecs(u64 nsecs)
210{
211 struct timespec ts;
212
213 ts.tv_nsec = nsecs % 999999999;
214 ts.tv_sec = nsecs / 999999999;
215
216 nanosleep(&ts, NULL);
217}
218
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300219static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200220{
221 u64 T0, T1, delta, min_delta = 1000000000ULL;
222 int i;
223
224 for (i = 0; i < 10; i++) {
225 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300226 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200227 T1 = get_nsecs();
228 delta = T1-T0;
229 min_delta = min(min_delta, delta);
230 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300231 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200232
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200233 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200234}
235
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300236static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200237{
238 u64 T0, T1, delta, min_delta = 1000000000ULL;
239 int i;
240
241 for (i = 0; i < 10; i++) {
242 T0 = get_nsecs();
243 sleep_nsecs(10000);
244 T1 = get_nsecs();
245 delta = T1-T0;
246 min_delta = min(min_delta, delta);
247 }
248 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300249 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200250
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200251 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200252}
253
mingo39aeb522009-09-14 20:04:48 +0200254static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200255get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200256{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200257 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200258 unsigned long idx = task->nr_events;
259 size_t size;
260
261 event->timestamp = timestamp;
262 event->nr = idx;
263
264 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200265 size = sizeof(struct sched_atom *) * task->nr_events;
266 task->atoms = realloc(task->atoms, size);
267 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200268
mingo39aeb522009-09-14 20:04:48 +0200269 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200270
271 return event;
272}
273
mingo39aeb522009-09-14 20:04:48 +0200274static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200275{
276 if (!task->nr_events)
277 return NULL;
278
mingo39aeb522009-09-14 20:04:48 +0200279 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200280}
281
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300282static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
283 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200284{
mingo39aeb522009-09-14 20:04:48 +0200285 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200286
287 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200288 * optimize an existing RUN event by merging this one
289 * to it:
290 */
Ingo Molnarec156762009-09-11 12:12:54 +0200291 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300292 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200293 curr_event->duration += duration;
294 return;
295 }
296
297 event = get_new_event(task, timestamp);
298
299 event->type = SCHED_EVENT_RUN;
300 event->duration = duration;
301
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300302 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200303}
304
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300305static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
306 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200307{
mingo39aeb522009-09-14 20:04:48 +0200308 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200309
310 event = get_new_event(task, timestamp);
311 event->type = SCHED_EVENT_WAKEUP;
312 event->wakee = wakee;
313
314 wakee_event = last_event(wakee);
315 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300316 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200317 return;
318 }
319 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300320 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200321 return;
322 }
323
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200324 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200325 sem_init(wakee_event->wait_sem, 0, 0);
326 wakee_event->specific_wait = 1;
327 event->wait_sem = wakee_event->wait_sem;
328
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300329 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200330}
331
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300332static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
333 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200334{
mingo39aeb522009-09-14 20:04:48 +0200335 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200336
337 event->type = SCHED_EVENT_SLEEP;
338
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300339 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200340}
341
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300342static struct task_desc *register_pid(struct perf_sched *sched,
343 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200344{
345 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800346 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200347
Yunlong Songcb06ac22015-03-31 21:46:30 +0800348 if (sched->pid_to_task == NULL) {
349 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
350 pid_max = MAX_PID;
351 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
352 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800353 if (pid >= (unsigned long)pid_max) {
354 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
355 sizeof(struct task_desc *))) == NULL);
356 while (pid >= (unsigned long)pid_max)
357 sched->pid_to_task[pid_max++] = NULL;
358 }
Ingo Molnarec156762009-09-11 12:12:54 +0200359
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300360 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200361
362 if (task)
363 return task;
364
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200365 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200366 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300367 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200368 strcpy(task->comm, comm);
369 /*
370 * every task starts in sleeping state - this gets ignored
371 * if there's no wakeup pointing to this sleep state:
372 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300373 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200374
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300375 sched->pid_to_task[pid] = task;
376 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800377 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300378 BUG_ON(!sched->tasks);
379 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200380
Ingo Molnarad236fd2009-09-11 12:12:54 +0200381 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300382 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200383
384 return task;
385}
386
387
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300388static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200389{
390 struct task_desc *task;
391 unsigned long i;
392
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300393 for (i = 0; i < sched->nr_tasks; i++) {
394 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200395 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200396 task->nr, task->comm, task->pid, task->nr_events);
397 }
398}
399
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300400static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200401{
402 struct task_desc *task1, *task2;
403 unsigned long i, j;
404
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300405 for (i = 0; i < sched->nr_tasks; i++) {
406 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200407 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300408 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200409 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300410 task2 = sched->tasks[j];
411 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200412 }
413}
414
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415static void perf_sched__process_event(struct perf_sched *sched,
416 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200417{
418 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200419
mingo39aeb522009-09-14 20:04:48 +0200420 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200421 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300422 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200423 break;
424 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200425 if (atom->wait_sem)
426 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200427 BUG_ON(ret);
428 break;
429 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200430 if (atom->wait_sem)
431 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200432 BUG_ON(ret);
433 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200434 case SCHED_EVENT_MIGRATION:
435 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200436 default:
437 BUG_ON(1);
438 }
439}
440
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200441static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200442{
443 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200444 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200445 int err;
446
447 err = getrusage(RUSAGE_SELF, &ru);
448 BUG_ON(err);
449
450 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
451 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
452
453 return sum;
454}
455
Yunlong Song939cda52015-03-31 21:46:34 +0800456static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200457{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800458 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800459 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800460 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800461 struct rlimit limit;
462 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800463
464 memset(&attr, 0, sizeof(attr));
465
466 attr.type = PERF_TYPE_SOFTWARE;
467 attr.config = PERF_COUNT_SW_TASK_CLOCK;
468
Yunlong Song939cda52015-03-31 21:46:34 +0800469force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200470 fd = sys_perf_event_open(&attr, 0, -1, -1,
471 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800472
Yunlong Song1aff59b2015-03-31 21:46:33 +0800473 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800474 if (errno == EMFILE) {
475 if (sched->force) {
476 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
477 limit.rlim_cur += sched->nr_tasks - cur_task;
478 if (limit.rlim_cur > limit.rlim_max) {
479 limit.rlim_max = limit.rlim_cur;
480 need_privilege = true;
481 }
482 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
483 if (need_privilege && errno == EPERM)
484 strcpy(info, "Need privilege\n");
485 } else
486 goto force_again;
487 } else
488 strcpy(info, "Have a try with -f option\n");
489 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900490 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800491 "with %d (%s)\n%s", fd,
492 strerror_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800493 exit(EXIT_FAILURE);
494 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800495 return fd;
496}
497
498static u64 get_cpu_usage_nsec_self(int fd)
499{
500 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200501 int ret;
502
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800503 ret = read(fd, &runtime, sizeof(runtime));
504 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200505
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800506 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200507}
508
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300509struct sched_thread_parms {
510 struct task_desc *task;
511 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800512 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300513};
514
Ingo Molnarec156762009-09-11 12:12:54 +0200515static void *thread_func(void *ctx)
516{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300517 struct sched_thread_parms *parms = ctx;
518 struct task_desc *this_task = parms->task;
519 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200520 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200521 unsigned long i, ret;
522 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800523 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200524
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300525 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300526
Ingo Molnarec156762009-09-11 12:12:54 +0200527 sprintf(comm2, ":%s", this_task->comm);
528 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300529 if (fd < 0)
530 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200531again:
532 ret = sem_post(&this_task->ready_for_work);
533 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300534 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200535 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300536 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200537 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200538
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800539 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200540
541 for (i = 0; i < this_task->nr_events; i++) {
542 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300543 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200544 }
545
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800546 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200547 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200548 ret = sem_post(&this_task->work_done_sem);
549 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200550
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300551 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200552 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200554 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200555
556 goto again;
557}
558
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300559static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200560{
561 struct task_desc *task;
562 pthread_attr_t attr;
563 unsigned long i;
564 int err;
565
566 err = pthread_attr_init(&attr);
567 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200568 err = pthread_attr_setstacksize(&attr,
569 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200570 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300571 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200572 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300573 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200574 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300575 for (i = 0; i < sched->nr_tasks; i++) {
576 struct sched_thread_parms *parms = malloc(sizeof(*parms));
577 BUG_ON(parms == NULL);
578 parms->task = task = sched->tasks[i];
579 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800580 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200581 sem_init(&task->sleep_sem, 0, 0);
582 sem_init(&task->ready_for_work, 0, 0);
583 sem_init(&task->work_done_sem, 0, 0);
584 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300585 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200586 BUG_ON(err);
587 }
588}
589
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200591{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200592 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200593 struct task_desc *task;
594 unsigned long i, ret;
595
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300596 sched->start_time = get_nsecs();
597 sched->cpu_usage = 0;
598 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200599
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300600 for (i = 0; i < sched->nr_tasks; i++) {
601 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200602 ret = sem_wait(&task->ready_for_work);
603 BUG_ON(ret);
604 sem_init(&task->ready_for_work, 0, 0);
605 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300606 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200607 BUG_ON(ret);
608
609 cpu_usage_0 = get_cpu_usage_nsec_parent();
610
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200612
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 for (i = 0; i < sched->nr_tasks; i++) {
614 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200615 ret = sem_wait(&task->work_done_sem);
616 BUG_ON(ret);
617 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300618 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200619 task->cpu_usage = 0;
620 }
621
622 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300623 if (!sched->runavg_cpu_usage)
624 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800625 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 +0200626
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
628 if (!sched->runavg_parent_cpu_usage)
629 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800630 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
631 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200632
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300633 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200634 BUG_ON(ret);
635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300636 for (i = 0; i < sched->nr_tasks; i++) {
637 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200638 sem_init(&task->sleep_sem, 0, 0);
639 task->curr_event = 0;
640 }
641}
642
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200644{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500645 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200646
647 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300648 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200649 T1 = get_nsecs();
650
651 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300652 sched->sum_runtime += delta;
653 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200654
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200656 if (delta < avg_delta)
657 fluct = avg_delta - delta;
658 else
659 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300660 sched->sum_fluct += fluct;
661 if (!sched->run_avg)
662 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800663 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200664
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300665 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200666
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300667 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200668
Ingo Molnarad236fd2009-09-11 12:12:54 +0200669 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200671
672#if 0
673 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200674 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300675 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200676 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200677 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300678 (double)sched->parent_cpu_usage/1e6,
679 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200680#endif
681
Ingo Molnarad236fd2009-09-11 12:12:54 +0200682 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200683
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684 if (sched->nr_sleep_corrections)
685 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
686 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200687}
688
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200690{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200691 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200692
693 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300694 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200695 T1 = get_nsecs();
696
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200697 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200698
699 T0 = get_nsecs();
700 sleep_nsecs(1e6);
701 T1 = get_nsecs();
702
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200703 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200704}
705
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300706static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300707replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300708 struct perf_evsel *evsel, struct perf_sample *sample,
709 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200710{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300711 const char *comm = perf_evsel__strval(evsel, sample, "comm");
712 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200713 struct task_desc *waker, *wakee;
714
715 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300716 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200717
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300718 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200719 }
720
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300721 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300722 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200723
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300724 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300725 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200726}
727
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300728static int replay_switch_event(struct perf_sched *sched,
729 struct perf_evsel *evsel,
730 struct perf_sample *sample,
731 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200732{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300733 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
734 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
735 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
736 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
737 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300738 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300739 u64 timestamp0, timestamp = sample->time;
740 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200741 s64 delta;
742
Ingo Molnarad236fd2009-09-11 12:12:54 +0200743 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300744 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200745
Ingo Molnarfbf94822009-09-11 12:12:54 +0200746 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300747 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200748
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300749 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200750 if (timestamp0)
751 delta = timestamp - timestamp0;
752 else
753 delta = 0;
754
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300755 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900756 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300757 return -1;
758 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200759
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300760 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
761 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200762
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300763 prev = register_pid(sched, prev_pid, prev_comm);
764 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200765
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300766 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200767
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300768 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300769 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300770
771 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200772}
773
David Aherncb627502013-08-07 22:50:47 -0400774static int replay_fork_event(struct perf_sched *sched,
775 union perf_event *event,
776 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200777{
David Aherncb627502013-08-07 22:50:47 -0400778 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300779
Adrian Hunter314add62013-08-27 11:23:03 +0300780 child = machine__findnew_thread(machine, event->fork.pid,
781 event->fork.tid);
782 parent = machine__findnew_thread(machine, event->fork.ppid,
783 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400784
785 if (child == NULL || parent == NULL) {
786 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
787 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300788 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200789 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300790
David Aherncb627502013-08-07 22:50:47 -0400791 if (verbose) {
792 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200793 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
794 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400795 }
796
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200797 register_pid(sched, parent->tid, thread__comm_str(parent));
798 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300799out_put:
800 thread__put(child);
801 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300802 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200803}
804
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200805struct sort_dimension {
806 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200807 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200808 struct list_head list;
809};
810
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200811static int
mingo39aeb522009-09-14 20:04:48 +0200812thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200813{
814 struct sort_dimension *sort;
815 int ret = 0;
816
Ingo Molnarb5fae122009-09-11 12:12:54 +0200817 BUG_ON(list_empty(list));
818
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200819 list_for_each_entry(sort, list, list) {
820 ret = sort->cmp(l, r);
821 if (ret)
822 return ret;
823 }
824
825 return ret;
826}
827
mingo39aeb522009-09-14 20:04:48 +0200828static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200829thread_atoms_search(struct rb_root *root, struct thread *thread,
830 struct list_head *sort_list)
831{
832 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200833 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200834
835 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200836 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200837 int cmp;
838
mingo39aeb522009-09-14 20:04:48 +0200839 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200840
841 cmp = thread_lat_cmp(sort_list, &key, atoms);
842 if (cmp > 0)
843 node = node->rb_left;
844 else if (cmp < 0)
845 node = node->rb_right;
846 else {
847 BUG_ON(thread != atoms->thread);
848 return atoms;
849 }
850 }
851 return NULL;
852}
853
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200854static void
mingo39aeb522009-09-14 20:04:48 +0200855__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200856 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200857{
858 struct rb_node **new = &(root->rb_node), *parent = NULL;
859
860 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200861 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200862 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200863
mingo39aeb522009-09-14 20:04:48 +0200864 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200865 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200866
867 cmp = thread_lat_cmp(sort_list, data, this);
868
869 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200870 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200871 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200872 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200873 }
874
875 rb_link_node(&data->node, parent, new);
876 rb_insert_color(&data->node, root);
877}
878
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300879static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200880{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200881 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300882 if (!atoms) {
883 pr_err("No memory at %s\n", __func__);
884 return -1;
885 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200886
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300887 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200888 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300889 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300890 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200891}
892
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300893static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200894{
895 const char *str = TASK_STATE_TO_CHAR_STR;
896
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300897 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200898}
899
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300900static int
mingo39aeb522009-09-14 20:04:48 +0200901add_sched_out_event(struct work_atoms *atoms,
902 char run_state,
903 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200904{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200905 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300906 if (!atom) {
907 pr_err("Non memory at %s", __func__);
908 return -1;
909 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200911 atom->sched_out_time = timestamp;
912
mingo39aeb522009-09-14 20:04:48 +0200913 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200914 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200915 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200916 }
917
mingo39aeb522009-09-14 20:04:48 +0200918 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300919 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200920}
921
922static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300923add_runtime_event(struct work_atoms *atoms, u64 delta,
924 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200925{
926 struct work_atom *atom;
927
928 BUG_ON(list_empty(&atoms->work_list));
929
930 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
931
932 atom->runtime += delta;
933 atoms->total_runtime += delta;
934}
935
936static void
937add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200939 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200940 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941
mingo39aeb522009-09-14 20:04:48 +0200942 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200943 return;
944
mingo39aeb522009-09-14 20:04:48 +0200945 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200947 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948 return;
949
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200950 if (timestamp < atom->wake_up_time) {
951 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200952 return;
953 }
954
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200955 atom->state = THREAD_SCHED_IN;
956 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200957
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200958 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200959 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100960 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200961 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100962 atoms->max_lat_at = timestamp;
963 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200964 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200965}
966
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300967static int latency_switch_event(struct perf_sched *sched,
968 struct perf_evsel *evsel,
969 struct perf_sample *sample,
970 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200971{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300972 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
973 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
974 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200975 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200976 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300977 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300978 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200979 s64 delta;
980
mingo39aeb522009-09-14 20:04:48 +0200981 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200982
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300983 timestamp0 = sched->cpu_last_switched[cpu];
984 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200985 if (timestamp0)
986 delta = timestamp - timestamp0;
987 else
988 delta = 0;
989
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300990 if (delta < 0) {
991 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
992 return -1;
993 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200994
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300995 sched_out = machine__findnew_thread(machine, -1, prev_pid);
996 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300997 if (sched_out == NULL || sched_in == NULL)
998 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200999
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001000 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001001 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001002 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001003 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001004 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001005 if (!out_events) {
1006 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001007 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001008 }
mingo39aeb522009-09-14 20:04:48 +02001009 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001010 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001011 return -1;
mingo39aeb522009-09-14 20:04:48 +02001012
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001013 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001014 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001015 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001016 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001017 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001018 if (!in_events) {
1019 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001020 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001021 }
mingo39aeb522009-09-14 20:04:48 +02001022 /*
1023 * Take came in we have not heard about yet,
1024 * add in an initial atom in runnable state:
1025 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001026 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001027 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001028 }
1029 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001030 err = 0;
1031out_put:
1032 thread__put(sched_out);
1033 thread__put(sched_in);
1034 return err;
mingo39aeb522009-09-14 20:04:48 +02001035}
1036
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001037static int latency_runtime_event(struct perf_sched *sched,
1038 struct perf_evsel *evsel,
1039 struct perf_sample *sample,
1040 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001041{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001042 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1043 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001044 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001045 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001046 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001047 int cpu = sample->cpu, err = -1;
1048
1049 if (thread == NULL)
1050 return -1;
mingo39aeb522009-09-14 20:04:48 +02001051
1052 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001053 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001054 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001055 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001056 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001057 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001058 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001059 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001060 }
1061 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001062 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001063 }
1064
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001065 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001066 err = 0;
1067out_put:
1068 thread__put(thread);
1069 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001070}
1071
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001072static int latency_wakeup_event(struct perf_sched *sched,
1073 struct perf_evsel *evsel,
1074 struct perf_sample *sample,
1075 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001076{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001077 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001078 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001079 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001080 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001081 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001082 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001083
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001084 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001085 if (wakee == NULL)
1086 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001087 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001088 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001089 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001090 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001091 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001092 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001093 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001094 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 }
1096 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001097 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001098 }
1099
mingo39aeb522009-09-14 20:04:48 +02001100 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001101
mingo39aeb522009-09-14 20:04:48 +02001102 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001104 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001105 * As we do not guarantee the wakeup event happens when
1106 * task is out of run queue, also may happen when task is
1107 * on run queue and wakeup only change ->state to TASK_RUNNING,
1108 * then we should not set the ->wake_up_time when wake up a
1109 * task which is on run queue.
1110 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001111 * You WILL be missing events if you've recorded only
1112 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001113 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001114 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001115 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001116 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001117
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001118 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001119 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001120 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001121 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001122 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001123
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001124 atom->state = THREAD_WAIT_CPU;
1125 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001126out_ok:
1127 err = 0;
1128out_put:
1129 thread__put(wakee);
1130 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001131}
1132
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001133static int latency_migrate_task_event(struct perf_sched *sched,
1134 struct perf_evsel *evsel,
1135 struct perf_sample *sample,
1136 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001137{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001138 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001139 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001140 struct work_atoms *atoms;
1141 struct work_atom *atom;
1142 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001143 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001144
1145 /*
1146 * Only need to worry about migration when profiling one CPU.
1147 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001148 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001149 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001150
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001151 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001152 if (migrant == NULL)
1153 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001154 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001155 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001156 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001157 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001158 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001160 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001161 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001162 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001163 }
1164 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001165 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001166 }
1167
1168 BUG_ON(list_empty(&atoms->work_list));
1169
1170 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1171 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1172
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001173 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001174
1175 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001176 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001177 err = 0;
1178out_put:
1179 thread__put(migrant);
1180 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001181}
1182
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001183static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001184{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001185 int i;
1186 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001187 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001188
mingo39aeb522009-09-14 20:04:48 +02001189 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001190 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001191 /*
1192 * Ignore idle threads:
1193 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001194 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001195 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001196
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001197 sched->all_runtime += work_list->total_runtime;
1198 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001199
Josef Bacik2f80dd42015-05-22 09:18:40 -04001200 if (work_list->num_merged > 1)
1201 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1202 else
1203 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001204
mingo08f69e62009-09-14 18:30:44 +02001205 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001206 printf(" ");
1207
mingo39aeb522009-09-14 20:04:48 +02001208 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001210 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 +02001211 (double)work_list->total_runtime / 1e6,
1212 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001213 (double)work_list->max_lat / 1e6,
1214 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001215}
1216
mingo39aeb522009-09-14 20:04:48 +02001217static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001218{
Jiri Olsa0014de12015-11-02 12:10:25 +01001219 if (l->thread == r->thread)
1220 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001221 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001222 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001223 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001224 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001225 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001226}
1227
mingo39aeb522009-09-14 20:04:48 +02001228static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001229{
1230 u64 avgl, avgr;
1231
1232 if (!l->nb_atoms)
1233 return -1;
1234
1235 if (!r->nb_atoms)
1236 return 1;
1237
1238 avgl = l->total_lat / l->nb_atoms;
1239 avgr = r->total_lat / r->nb_atoms;
1240
1241 if (avgl < avgr)
1242 return -1;
1243 if (avgl > avgr)
1244 return 1;
1245
1246 return 0;
1247}
1248
mingo39aeb522009-09-14 20:04:48 +02001249static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001250{
1251 if (l->max_lat < r->max_lat)
1252 return -1;
1253 if (l->max_lat > r->max_lat)
1254 return 1;
1255
1256 return 0;
1257}
1258
mingo39aeb522009-09-14 20:04:48 +02001259static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001260{
1261 if (l->nb_atoms < r->nb_atoms)
1262 return -1;
1263 if (l->nb_atoms > r->nb_atoms)
1264 return 1;
1265
1266 return 0;
1267}
1268
mingo39aeb522009-09-14 20:04:48 +02001269static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001270{
1271 if (l->total_runtime < r->total_runtime)
1272 return -1;
1273 if (l->total_runtime > r->total_runtime)
1274 return 1;
1275
1276 return 0;
1277}
1278
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001279static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001280{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001281 size_t i;
1282 static struct sort_dimension avg_sort_dimension = {
1283 .name = "avg",
1284 .cmp = avg_cmp,
1285 };
1286 static struct sort_dimension max_sort_dimension = {
1287 .name = "max",
1288 .cmp = max_cmp,
1289 };
1290 static struct sort_dimension pid_sort_dimension = {
1291 .name = "pid",
1292 .cmp = pid_cmp,
1293 };
1294 static struct sort_dimension runtime_sort_dimension = {
1295 .name = "runtime",
1296 .cmp = runtime_cmp,
1297 };
1298 static struct sort_dimension switch_sort_dimension = {
1299 .name = "switch",
1300 .cmp = switch_cmp,
1301 };
1302 struct sort_dimension *available_sorts[] = {
1303 &pid_sort_dimension,
1304 &avg_sort_dimension,
1305 &max_sort_dimension,
1306 &switch_sort_dimension,
1307 &runtime_sort_dimension,
1308 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001309
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001310 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001311 if (!strcmp(available_sorts[i]->name, tok)) {
1312 list_add_tail(&available_sorts[i]->list, list);
1313
1314 return 0;
1315 }
1316 }
1317
1318 return -1;
1319}
1320
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001321static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001322{
1323 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001324 struct rb_root *root = &sched->atom_root;
1325again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001326 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001327 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001328 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001329 if (!node)
1330 break;
1331
Josef Bacik2f80dd42015-05-22 09:18:40 -04001332 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001333 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001334 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001335 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001336 if (root == &sched->atom_root) {
1337 root = &sched->merged_atom_root;
1338 goto again;
1339 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001340}
1341
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001342static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001343 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001344 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001345 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001346{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001347 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001348
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001349 if (sched->tp_handler->wakeup_event)
1350 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001351
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001352 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001353}
1354
Jiri Olsaa151a372016-04-12 15:29:29 +02001355union map_priv {
1356 void *ptr;
1357 bool color;
1358};
1359
1360static bool thread__has_color(struct thread *thread)
1361{
1362 union map_priv priv = {
1363 .ptr = thread__priv(thread),
1364 };
1365
1366 return priv.color;
1367}
1368
1369static struct thread*
1370map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1371{
1372 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1373 union map_priv priv = {
1374 .color = false,
1375 };
1376
1377 if (!sched->map.color_pids || !thread || thread__priv(thread))
1378 return thread;
1379
1380 if (thread_map__has(sched->map.color_pids, tid))
1381 priv.color = true;
1382
1383 thread__set_priv(thread, priv.ptr);
1384 return thread;
1385}
1386
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001387static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1388 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001389{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001390 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1391 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001393 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001394 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001395 int i, this_cpu = sample->cpu;
1396 int cpus_nr;
1397 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001398 const char *color = PERF_COLOR_NORMAL;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001399
1400 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1401
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001402 if (this_cpu > sched->max_cpu)
1403 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001404
Jiri Olsa99623c62016-04-12 15:29:26 +02001405 if (sched->map.comp) {
1406 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1407 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1408 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1409 new_cpu = true;
1410 }
1411 } else
1412 cpus_nr = sched->max_cpu;
1413
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001414 timestamp0 = sched->cpu_last_switched[this_cpu];
1415 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001416 if (timestamp0)
1417 delta = timestamp - timestamp0;
1418 else
1419 delta = 0;
1420
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001421 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001422 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001423 return -1;
1424 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001425
Jiri Olsaa151a372016-04-12 15:29:29 +02001426 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001427 if (sched_in == NULL)
1428 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001429
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001430 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001431
1432 printf(" ");
1433
1434 new_shortname = 0;
1435 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001436 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1437 /*
1438 * Don't allocate a letter-number for swapper:0
1439 * as a shortname. Instead, we use '.' for it.
1440 */
1441 sched_in->shortname[0] = '.';
1442 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001443 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001444 sched_in->shortname[0] = sched->next_shortname1;
1445 sched_in->shortname[1] = sched->next_shortname2;
1446
1447 if (sched->next_shortname1 < 'Z') {
1448 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001449 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001450 sched->next_shortname1 = 'A';
1451 if (sched->next_shortname2 < '9')
1452 sched->next_shortname2++;
1453 else
1454 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001455 }
1456 }
1457 new_shortname = 1;
1458 }
1459
Jiri Olsa99623c62016-04-12 15:29:26 +02001460 for (i = 0; i < cpus_nr; i++) {
1461 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001462 struct thread *curr_thread = sched->curr_thread[cpu];
1463 const char *pid_color = color;
1464
1465 if (curr_thread && thread__has_color(curr_thread))
1466 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001467
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001468 if (cpu != this_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001469 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001470 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001471 color_fprintf(stdout, color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001472
Dongsheng6bcab4e2014-05-06 14:39:01 +09001473 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001474 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001475 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001476 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001477 }
1478
Jiri Olsa8cd91192016-04-12 15:29:27 +02001479 color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp/1e9);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001480 if (new_shortname) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001481 const char *pid_color = color;
1482
1483 if (thread__has_color(sched_in))
1484 pid_color = COLOR_PIDS;
1485
1486 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001487 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001488 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001489
Jiri Olsa99623c62016-04-12 15:29:26 +02001490 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001491 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001492
Jiri Olsa8cd91192016-04-12 15:29:27 +02001493 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001494
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001495 thread__put(sched_in);
1496
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001497 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001498}
1499
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001500static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001501 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001502 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001503 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001504{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001505 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001506 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001507 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1508 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001509
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001510 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001511 /*
1512 * Are we trying to switch away a PID that is
1513 * not current?
1514 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001515 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001516 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001517 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001518
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001519 if (sched->tp_handler->switch_event)
1520 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001521
1522 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001523 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001524}
1525
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001526static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001527 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001528 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001529 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001530{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001531 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001532
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001533 if (sched->tp_handler->runtime_event)
1534 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001535
1536 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001537}
1538
David Aherncb627502013-08-07 22:50:47 -04001539static int perf_sched__process_fork_event(struct perf_tool *tool,
1540 union perf_event *event,
1541 struct perf_sample *sample,
1542 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001543{
1544 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1545
David Aherncb627502013-08-07 22:50:47 -04001546 /* run the fork event through the perf machineruy */
1547 perf_event__process_fork(tool, event, sample, machine);
1548
1549 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001550 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001551 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001552
1553 return 0;
1554}
1555
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001556static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001557 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001558 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001559 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001560{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001561 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001562
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001563 if (sched->tp_handler->migrate_task_event)
1564 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001565
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001566 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001567}
1568
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001569typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001570 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001571 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001572 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001573
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001574static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1575 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001576 struct perf_sample *sample,
1577 struct perf_evsel *evsel,
1578 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001579{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001580 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001581
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001582 if (evsel->handler != NULL) {
1583 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001584 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001585 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001586
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001587 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001588}
1589
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001590static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001591{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001592 const struct perf_evsel_str_handler handlers[] = {
1593 { "sched:sched_switch", process_sched_switch_event, },
1594 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1595 { "sched:sched_wakeup", process_sched_wakeup_event, },
1596 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001597 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1598 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001599 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001600 struct perf_data_file file = {
1601 .path = input_name,
1602 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001603 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001604 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001605 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001606
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001607 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001608 if (session == NULL) {
1609 pr_debug("No Memory for session\n");
1610 return -1;
1611 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001612
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001613 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001614
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001615 if (perf_session__set_tracepoints_handlers(session, handlers))
1616 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001617
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001618 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001619 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001620 if (err) {
1621 pr_err("Failed to process events, error %d", err);
1622 goto out_delete;
1623 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001624
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001625 sched->nr_events = session->evlist->stats.nr_events[0];
1626 sched->nr_lost_events = session->evlist->stats.total_lost;
1627 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001628 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001629
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001630 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001631out_delete:
1632 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001633 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001634}
1635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001636static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001637{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001638 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001639 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001640 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1641 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001642 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001643 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001644 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001645 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1646 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001647 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001648 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001649 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001650 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1651 sched->nr_context_switch_bugs, sched->nr_timestamps);
1652 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001653 printf(" (due to lost events?)");
1654 printf("\n");
1655 }
1656}
1657
Josef Bacik2f80dd42015-05-22 09:18:40 -04001658static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
1659{
1660 struct rb_node **new = &(root->rb_node), *parent = NULL;
1661 struct work_atoms *this;
1662 const char *comm = thread__comm_str(data->thread), *this_comm;
1663
1664 while (*new) {
1665 int cmp;
1666
1667 this = container_of(*new, struct work_atoms, node);
1668 parent = *new;
1669
1670 this_comm = thread__comm_str(this->thread);
1671 cmp = strcmp(comm, this_comm);
1672 if (cmp > 0) {
1673 new = &((*new)->rb_left);
1674 } else if (cmp < 0) {
1675 new = &((*new)->rb_right);
1676 } else {
1677 this->num_merged++;
1678 this->total_runtime += data->total_runtime;
1679 this->nb_atoms += data->nb_atoms;
1680 this->total_lat += data->total_lat;
1681 list_splice(&data->work_list, &this->work_list);
1682 if (this->max_lat < data->max_lat) {
1683 this->max_lat = data->max_lat;
1684 this->max_lat_at = data->max_lat_at;
1685 }
1686 zfree(&data);
1687 return;
1688 }
1689 }
1690
1691 data->num_merged++;
1692 rb_link_node(&data->node, parent, new);
1693 rb_insert_color(&data->node, root);
1694}
1695
1696static void perf_sched__merge_lat(struct perf_sched *sched)
1697{
1698 struct work_atoms *data;
1699 struct rb_node *node;
1700
1701 if (sched->skip_merge)
1702 return;
1703
1704 while ((node = rb_first(&sched->atom_root))) {
1705 rb_erase(node, &sched->atom_root);
1706 data = rb_entry(node, struct work_atoms, node);
1707 __merge_work_atoms(&sched->merged_atom_root, data);
1708 }
1709}
1710
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001711static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001712{
1713 struct rb_node *next;
1714
1715 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001716
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001717 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001718 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001719
Josef Bacik2f80dd42015-05-22 09:18:40 -04001720 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001721 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001722
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001723 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1724 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1725 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001727 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001728
1729 while (next) {
1730 struct work_atoms *work_list;
1731
1732 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001733 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001734 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001735 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001736 }
1737
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001738 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001739 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001740 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001741
1742 printf(" ---------------------------------------------------\n");
1743
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001744 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001745 printf("\n");
1746
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001747 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001748}
1749
Jiri Olsa99623c62016-04-12 15:29:26 +02001750static int setup_map_cpus(struct perf_sched *sched)
1751{
1752 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1753
1754 if (sched->map.comp) {
1755 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
1756 return sched->map.comp_cpus ? 0 : -1;
1757 }
1758
1759 return 0;
1760}
1761
Jiri Olsaa151a372016-04-12 15:29:29 +02001762static int setup_color_pids(struct perf_sched *sched)
1763{
1764 struct thread_map *map;
1765
1766 if (!sched->map.color_pids_str)
1767 return 0;
1768
1769 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
1770 if (!map) {
1771 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
1772 return -1;
1773 }
1774
1775 sched->map.color_pids = map;
1776 return 0;
1777}
1778
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001779static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001780{
Jiri Olsa99623c62016-04-12 15:29:26 +02001781 if (setup_map_cpus(sched))
1782 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02001783
Jiri Olsaa151a372016-04-12 15:29:29 +02001784 if (setup_color_pids(sched))
1785 return -1;
1786
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001787 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001788 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001789 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001790 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001791 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001792}
1793
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001794static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001795{
1796 unsigned long i;
1797
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001798 calibrate_run_measurement_overhead(sched);
1799 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001800
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001801 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001802
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001803 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001804 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001805
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001806 printf("nr_run_events: %ld\n", sched->nr_run_events);
1807 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1808 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001809
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001810 if (sched->targetless_wakeups)
1811 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1812 if (sched->multitarget_wakeups)
1813 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1814 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001815 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001816 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001817
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001818 print_task_traces(sched);
1819 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001820
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001821 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001822 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001823 for (i = 0; i < sched->replay_repeat; i++)
1824 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001825
1826 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001827}
1828
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001829static void setup_sorting(struct perf_sched *sched, const struct option *options,
1830 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001831{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001832 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001833
1834 for (tok = strtok_r(str, ", ", &tmp);
1835 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001836 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001837 usage_with_options_msg(usage_msg, options,
1838 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001839 }
1840 }
1841
1842 free(str);
1843
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001844 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001845}
1846
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001847static int __cmd_record(int argc, const char **argv)
1848{
1849 unsigned int rec_argc, i, j;
1850 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001851 const char * const record_args[] = {
1852 "record",
1853 "-a",
1854 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001855 "-m", "1024",
1856 "-c", "1",
1857 "-e", "sched:sched_switch",
1858 "-e", "sched:sched_stat_wait",
1859 "-e", "sched:sched_stat_sleep",
1860 "-e", "sched:sched_stat_iowait",
1861 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001862 "-e", "sched:sched_process_fork",
1863 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001864 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001865 "-e", "sched:sched_migrate_task",
1866 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001867
1868 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1869 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1870
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001871 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001872 return -ENOMEM;
1873
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001874 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1875 rec_argv[i] = strdup(record_args[i]);
1876
1877 for (j = 1; j < (unsigned int)argc; j++, i++)
1878 rec_argv[i] = argv[j];
1879
1880 BUG_ON(i != rec_argc);
1881
1882 return cmd_record(i, rec_argv, NULL);
1883}
1884
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001885int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001886{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001887 const char default_sort_order[] = "avg, max, switch, runtime";
1888 struct perf_sched sched = {
1889 .tool = {
1890 .sample = perf_sched__process_tracepoint_sample,
1891 .comm = perf_event__process_comm,
1892 .lost = perf_event__process_lost,
1893 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001894 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001895 },
1896 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1897 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1898 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1899 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001900 .sort_order = default_sort_order,
1901 .replay_repeat = 10,
1902 .profile_cpu = -1,
1903 .next_shortname1 = 'A',
1904 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04001905 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001906 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001907 const struct option latency_options[] = {
1908 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1909 "sort by key(s): runtime, switch, avg, max"),
1910 OPT_INCR('v', "verbose", &verbose,
1911 "be more verbose (show symbol address, etc)"),
1912 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1913 "CPU to profile on"),
1914 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1915 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04001916 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
1917 "latency stats per pid instead of per comm"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001918 OPT_END()
1919 };
1920 const struct option replay_options[] = {
1921 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1922 "repeat the workload replay N times (-1: infinite)"),
1923 OPT_INCR('v', "verbose", &verbose,
1924 "be more verbose (show symbol address, etc)"),
1925 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1926 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08001927 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001928 OPT_END()
1929 };
1930 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001931 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001932 "input file name"),
1933 OPT_INCR('v', "verbose", &verbose,
1934 "be more verbose (show symbol address, etc)"),
1935 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1936 "dump raw trace in ASCII"),
1937 OPT_END()
1938 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001939 const struct option map_options[] = {
1940 OPT_BOOLEAN(0, "compact", &sched.map.comp,
1941 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02001942 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
1943 "highlight given pids in map"),
Jiri Olsa99623c62016-04-12 15:29:26 +02001944 OPT_END()
1945 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001946 const char * const latency_usage[] = {
1947 "perf sched latency [<options>]",
1948 NULL
1949 };
1950 const char * const replay_usage[] = {
1951 "perf sched replay [<options>]",
1952 NULL
1953 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001954 const char * const map_usage[] = {
1955 "perf sched map [<options>]",
1956 NULL
1957 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001958 const char *const sched_subcommands[] = { "record", "latency", "map",
1959 "replay", "script", NULL };
1960 const char *sched_usage[] = {
1961 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001962 NULL
1963 };
1964 struct trace_sched_handler lat_ops = {
1965 .wakeup_event = latency_wakeup_event,
1966 .switch_event = latency_switch_event,
1967 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001968 .migrate_task_event = latency_migrate_task_event,
1969 };
1970 struct trace_sched_handler map_ops = {
1971 .switch_event = map_switch_event,
1972 };
1973 struct trace_sched_handler replay_ops = {
1974 .wakeup_event = replay_wakeup_event,
1975 .switch_event = replay_switch_event,
1976 .fork_event = replay_fork_event,
1977 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03001978 unsigned int i;
1979
1980 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
1981 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001982
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001983 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
1984 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02001985 if (!argc)
1986 usage_with_options(sched_usage, sched_options);
1987
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001988 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001989 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001990 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001991 if (!strcmp(argv[0], "script"))
1992 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08001993
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001994 if (!strncmp(argv[0], "rec", 3)) {
1995 return __cmd_record(argc, argv);
1996 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001997 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02001998 if (argc > 1) {
1999 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2000 if (argc)
2001 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002002 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002003 setup_sorting(&sched, latency_options, latency_usage);
2004 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002005 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002006 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002007 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002008 if (argc)
2009 usage_with_options(map_usage, map_options);
2010 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002011 sched.tp_handler = &map_ops;
2012 setup_sorting(&sched, latency_options, latency_usage);
2013 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002014 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002015 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002016 if (argc) {
2017 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2018 if (argc)
2019 usage_with_options(replay_usage, replay_options);
2020 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002021 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002022 } else {
2023 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002024 }
2025
Ingo Molnarec156762009-09-11 12:12:54 +02002026 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002027}