blob: afa057666c2adf68ec1279dc29a027744ec38fff [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
Jiri Olsacf294f22016-04-12 15:29:30 +0200128#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200129
Jiri Olsa99623c62016-04-12 15:29:26 +0200130struct perf_sched_map {
131 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
132 int *comp_cpus;
133 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200134 struct thread_map *color_pids;
135 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200136 struct cpu_map *color_cpus;
137 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200138 struct cpu_map *cpus;
139 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200140};
141
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300142struct perf_sched {
143 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300144 const char *sort_order;
145 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800146 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300147 struct task_desc **tasks;
148 const struct trace_sched_handler *tp_handler;
149 pthread_mutex_t start_work_mutex;
150 pthread_mutex_t work_done_wait_mutex;
151 int profile_cpu;
152/*
153 * Track the current task - that way we can know whether there's any
154 * weird events, such as a task being switched away that is not current.
155 */
156 int max_cpu;
157 u32 curr_pid[MAX_CPUS];
158 struct thread *curr_thread[MAX_CPUS];
159 char next_shortname1;
160 char next_shortname2;
161 unsigned int replay_repeat;
162 unsigned long nr_run_events;
163 unsigned long nr_sleep_events;
164 unsigned long nr_wakeup_events;
165 unsigned long nr_sleep_corrections;
166 unsigned long nr_run_events_optimized;
167 unsigned long targetless_wakeups;
168 unsigned long multitarget_wakeups;
169 unsigned long nr_runs;
170 unsigned long nr_timestamps;
171 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300172 unsigned long nr_context_switch_bugs;
173 unsigned long nr_events;
174 unsigned long nr_lost_chunks;
175 unsigned long nr_lost_events;
176 u64 run_measurement_overhead;
177 u64 sleep_measurement_overhead;
178 u64 start_time;
179 u64 cpu_usage;
180 u64 runavg_cpu_usage;
181 u64 parent_cpu_usage;
182 u64 runavg_parent_cpu_usage;
183 u64 sum_runtime;
184 u64 sum_fluct;
185 u64 run_avg;
186 u64 all_runtime;
187 u64 all_count;
188 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400189 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300190 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800191 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400192 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200193 struct perf_sched_map map;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300194};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200195
196static u64 get_nsecs(void)
197{
198 struct timespec ts;
199
200 clock_gettime(CLOCK_MONOTONIC, &ts);
201
202 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
203}
204
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300205static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200206{
207 u64 T0 = get_nsecs(), T1;
208
209 do {
210 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300211 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200212}
213
214static void sleep_nsecs(u64 nsecs)
215{
216 struct timespec ts;
217
218 ts.tv_nsec = nsecs % 999999999;
219 ts.tv_sec = nsecs / 999999999;
220
221 nanosleep(&ts, NULL);
222}
223
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300224static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200225{
226 u64 T0, T1, delta, min_delta = 1000000000ULL;
227 int i;
228
229 for (i = 0; i < 10; i++) {
230 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300231 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200232 T1 = get_nsecs();
233 delta = T1-T0;
234 min_delta = min(min_delta, delta);
235 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300236 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200237
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200238 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200239}
240
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300241static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200242{
243 u64 T0, T1, delta, min_delta = 1000000000ULL;
244 int i;
245
246 for (i = 0; i < 10; i++) {
247 T0 = get_nsecs();
248 sleep_nsecs(10000);
249 T1 = get_nsecs();
250 delta = T1-T0;
251 min_delta = min(min_delta, delta);
252 }
253 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300254 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200255
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200256 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200257}
258
mingo39aeb522009-09-14 20:04:48 +0200259static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200260get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200261{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200262 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200263 unsigned long idx = task->nr_events;
264 size_t size;
265
266 event->timestamp = timestamp;
267 event->nr = idx;
268
269 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200270 size = sizeof(struct sched_atom *) * task->nr_events;
271 task->atoms = realloc(task->atoms, size);
272 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200273
mingo39aeb522009-09-14 20:04:48 +0200274 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200275
276 return event;
277}
278
mingo39aeb522009-09-14 20:04:48 +0200279static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200280{
281 if (!task->nr_events)
282 return NULL;
283
mingo39aeb522009-09-14 20:04:48 +0200284 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200285}
286
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300287static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
288 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200289{
mingo39aeb522009-09-14 20:04:48 +0200290 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200291
292 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200293 * optimize an existing RUN event by merging this one
294 * to it:
295 */
Ingo Molnarec156762009-09-11 12:12:54 +0200296 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300297 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200298 curr_event->duration += duration;
299 return;
300 }
301
302 event = get_new_event(task, timestamp);
303
304 event->type = SCHED_EVENT_RUN;
305 event->duration = duration;
306
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300307 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200308}
309
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300310static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
311 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200312{
mingo39aeb522009-09-14 20:04:48 +0200313 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200314
315 event = get_new_event(task, timestamp);
316 event->type = SCHED_EVENT_WAKEUP;
317 event->wakee = wakee;
318
319 wakee_event = last_event(wakee);
320 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300321 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200322 return;
323 }
324 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300325 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200326 return;
327 }
328
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200329 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200330 sem_init(wakee_event->wait_sem, 0, 0);
331 wakee_event->specific_wait = 1;
332 event->wait_sem = wakee_event->wait_sem;
333
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300334 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200335}
336
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300337static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
338 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200339{
mingo39aeb522009-09-14 20:04:48 +0200340 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200341
342 event->type = SCHED_EVENT_SLEEP;
343
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300344 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200345}
346
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300347static struct task_desc *register_pid(struct perf_sched *sched,
348 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200349{
350 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800351 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200352
Yunlong Songcb06ac22015-03-31 21:46:30 +0800353 if (sched->pid_to_task == NULL) {
354 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
355 pid_max = MAX_PID;
356 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
357 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800358 if (pid >= (unsigned long)pid_max) {
359 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
360 sizeof(struct task_desc *))) == NULL);
361 while (pid >= (unsigned long)pid_max)
362 sched->pid_to_task[pid_max++] = NULL;
363 }
Ingo Molnarec156762009-09-11 12:12:54 +0200364
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300365 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200366
367 if (task)
368 return task;
369
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200370 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200371 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300372 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200373 strcpy(task->comm, comm);
374 /*
375 * every task starts in sleeping state - this gets ignored
376 * if there's no wakeup pointing to this sleep state:
377 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300378 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200379
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300380 sched->pid_to_task[pid] = task;
381 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800382 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300383 BUG_ON(!sched->tasks);
384 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200385
Ingo Molnarad236fd2009-09-11 12:12:54 +0200386 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300387 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200388
389 return task;
390}
391
392
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300393static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200394{
395 struct task_desc *task;
396 unsigned long i;
397
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300398 for (i = 0; i < sched->nr_tasks; i++) {
399 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200400 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200401 task->nr, task->comm, task->pid, task->nr_events);
402 }
403}
404
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300405static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200406{
407 struct task_desc *task1, *task2;
408 unsigned long i, j;
409
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300410 for (i = 0; i < sched->nr_tasks; i++) {
411 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200412 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300413 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200414 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415 task2 = sched->tasks[j];
416 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200417 }
418}
419
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300420static void perf_sched__process_event(struct perf_sched *sched,
421 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200422{
423 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200424
mingo39aeb522009-09-14 20:04:48 +0200425 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200426 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300427 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200428 break;
429 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200430 if (atom->wait_sem)
431 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200432 BUG_ON(ret);
433 break;
434 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200435 if (atom->wait_sem)
436 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200437 BUG_ON(ret);
438 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200439 case SCHED_EVENT_MIGRATION:
440 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200441 default:
442 BUG_ON(1);
443 }
444}
445
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200446static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200447{
448 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200449 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200450 int err;
451
452 err = getrusage(RUSAGE_SELF, &ru);
453 BUG_ON(err);
454
455 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
456 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
457
458 return sum;
459}
460
Yunlong Song939cda52015-03-31 21:46:34 +0800461static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200462{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800463 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800464 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800465 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800466 struct rlimit limit;
467 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800468
469 memset(&attr, 0, sizeof(attr));
470
471 attr.type = PERF_TYPE_SOFTWARE;
472 attr.config = PERF_COUNT_SW_TASK_CLOCK;
473
Yunlong Song939cda52015-03-31 21:46:34 +0800474force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200475 fd = sys_perf_event_open(&attr, 0, -1, -1,
476 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800477
Yunlong Song1aff59b2015-03-31 21:46:33 +0800478 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800479 if (errno == EMFILE) {
480 if (sched->force) {
481 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
482 limit.rlim_cur += sched->nr_tasks - cur_task;
483 if (limit.rlim_cur > limit.rlim_max) {
484 limit.rlim_max = limit.rlim_cur;
485 need_privilege = true;
486 }
487 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
488 if (need_privilege && errno == EPERM)
489 strcpy(info, "Need privilege\n");
490 } else
491 goto force_again;
492 } else
493 strcpy(info, "Have a try with -f option\n");
494 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900495 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800496 "with %d (%s)\n%s", fd,
497 strerror_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800498 exit(EXIT_FAILURE);
499 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800500 return fd;
501}
502
503static u64 get_cpu_usage_nsec_self(int fd)
504{
505 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200506 int ret;
507
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800508 ret = read(fd, &runtime, sizeof(runtime));
509 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200510
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800511 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200512}
513
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300514struct sched_thread_parms {
515 struct task_desc *task;
516 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800517 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300518};
519
Ingo Molnarec156762009-09-11 12:12:54 +0200520static void *thread_func(void *ctx)
521{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300522 struct sched_thread_parms *parms = ctx;
523 struct task_desc *this_task = parms->task;
524 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200525 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200526 unsigned long i, ret;
527 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800528 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200529
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300530 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300531
Ingo Molnarec156762009-09-11 12:12:54 +0200532 sprintf(comm2, ":%s", this_task->comm);
533 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300534 if (fd < 0)
535 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200536again:
537 ret = sem_post(&this_task->ready_for_work);
538 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300539 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200540 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300541 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200542 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200543
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800544 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200545
546 for (i = 0; i < this_task->nr_events; i++) {
547 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300548 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200549 }
550
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800551 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200552 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200553 ret = sem_post(&this_task->work_done_sem);
554 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200555
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300556 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200557 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300558 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200559 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200560
561 goto again;
562}
563
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300564static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200565{
566 struct task_desc *task;
567 pthread_attr_t attr;
568 unsigned long i;
569 int err;
570
571 err = pthread_attr_init(&attr);
572 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200573 err = pthread_attr_setstacksize(&attr,
574 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200575 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300576 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200577 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300578 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200579 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 for (i = 0; i < sched->nr_tasks; i++) {
581 struct sched_thread_parms *parms = malloc(sizeof(*parms));
582 BUG_ON(parms == NULL);
583 parms->task = task = sched->tasks[i];
584 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800585 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200586 sem_init(&task->sleep_sem, 0, 0);
587 sem_init(&task->ready_for_work, 0, 0);
588 sem_init(&task->work_done_sem, 0, 0);
589 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200591 BUG_ON(err);
592 }
593}
594
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200596{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200597 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200598 struct task_desc *task;
599 unsigned long i, ret;
600
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300601 sched->start_time = get_nsecs();
602 sched->cpu_usage = 0;
603 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200604
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300605 for (i = 0; i < sched->nr_tasks; i++) {
606 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200607 ret = sem_wait(&task->ready_for_work);
608 BUG_ON(ret);
609 sem_init(&task->ready_for_work, 0, 0);
610 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200612 BUG_ON(ret);
613
614 cpu_usage_0 = get_cpu_usage_nsec_parent();
615
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300616 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200617
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300618 for (i = 0; i < sched->nr_tasks; i++) {
619 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200620 ret = sem_wait(&task->work_done_sem);
621 BUG_ON(ret);
622 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300623 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200624 task->cpu_usage = 0;
625 }
626
627 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300628 if (!sched->runavg_cpu_usage)
629 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800630 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 +0200631
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300632 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
633 if (!sched->runavg_parent_cpu_usage)
634 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800635 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
636 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200637
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300638 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200639 BUG_ON(ret);
640
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300641 for (i = 0; i < sched->nr_tasks; i++) {
642 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200643 sem_init(&task->sleep_sem, 0, 0);
644 task->curr_event = 0;
645 }
646}
647
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300648static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200649{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500650 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200651
652 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300653 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200654 T1 = get_nsecs();
655
656 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300657 sched->sum_runtime += delta;
658 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200659
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300660 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200661 if (delta < avg_delta)
662 fluct = avg_delta - delta;
663 else
664 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300665 sched->sum_fluct += fluct;
666 if (!sched->run_avg)
667 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800668 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200669
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200671
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300672 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200673
Ingo Molnarad236fd2009-09-11 12:12:54 +0200674 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300675 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200676
677#if 0
678 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200679 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300680 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200681 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200682 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300683 (double)sched->parent_cpu_usage/1e6,
684 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200685#endif
686
Ingo Molnarad236fd2009-09-11 12:12:54 +0200687 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200688
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 if (sched->nr_sleep_corrections)
690 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
691 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200692}
693
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300694static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200695{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200696 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200697
698 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300699 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200700 T1 = get_nsecs();
701
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200702 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200703
704 T0 = get_nsecs();
705 sleep_nsecs(1e6);
706 T1 = get_nsecs();
707
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200708 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200709}
710
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300711static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300712replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300713 struct perf_evsel *evsel, struct perf_sample *sample,
714 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200715{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300716 const char *comm = perf_evsel__strval(evsel, sample, "comm");
717 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200718 struct task_desc *waker, *wakee;
719
720 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300721 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200722
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300723 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200724 }
725
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300726 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300727 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200728
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300729 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300730 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200731}
732
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300733static int replay_switch_event(struct perf_sched *sched,
734 struct perf_evsel *evsel,
735 struct perf_sample *sample,
736 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200737{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300738 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
739 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
740 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
741 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
742 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300743 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300744 u64 timestamp0, timestamp = sample->time;
745 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200746 s64 delta;
747
Ingo Molnarad236fd2009-09-11 12:12:54 +0200748 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300749 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200750
Ingo Molnarfbf94822009-09-11 12:12:54 +0200751 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300752 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200753
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300754 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200755 if (timestamp0)
756 delta = timestamp - timestamp0;
757 else
758 delta = 0;
759
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300760 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900761 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300762 return -1;
763 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200764
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300765 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
766 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200767
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300768 prev = register_pid(sched, prev_pid, prev_comm);
769 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200770
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300771 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200772
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300773 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300774 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300775
776 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200777}
778
David Aherncb627502013-08-07 22:50:47 -0400779static int replay_fork_event(struct perf_sched *sched,
780 union perf_event *event,
781 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200782{
David Aherncb627502013-08-07 22:50:47 -0400783 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300784
Adrian Hunter314add62013-08-27 11:23:03 +0300785 child = machine__findnew_thread(machine, event->fork.pid,
786 event->fork.tid);
787 parent = machine__findnew_thread(machine, event->fork.ppid,
788 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400789
790 if (child == NULL || parent == NULL) {
791 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
792 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300793 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200794 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300795
David Aherncb627502013-08-07 22:50:47 -0400796 if (verbose) {
797 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200798 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
799 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400800 }
801
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200802 register_pid(sched, parent->tid, thread__comm_str(parent));
803 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300804out_put:
805 thread__put(child);
806 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300807 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200808}
809
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200810struct sort_dimension {
811 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200812 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200813 struct list_head list;
814};
815
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200816static int
mingo39aeb522009-09-14 20:04:48 +0200817thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200818{
819 struct sort_dimension *sort;
820 int ret = 0;
821
Ingo Molnarb5fae122009-09-11 12:12:54 +0200822 BUG_ON(list_empty(list));
823
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200824 list_for_each_entry(sort, list, list) {
825 ret = sort->cmp(l, r);
826 if (ret)
827 return ret;
828 }
829
830 return ret;
831}
832
mingo39aeb522009-09-14 20:04:48 +0200833static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200834thread_atoms_search(struct rb_root *root, struct thread *thread,
835 struct list_head *sort_list)
836{
837 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200838 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200839
840 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200841 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200842 int cmp;
843
mingo39aeb522009-09-14 20:04:48 +0200844 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200845
846 cmp = thread_lat_cmp(sort_list, &key, atoms);
847 if (cmp > 0)
848 node = node->rb_left;
849 else if (cmp < 0)
850 node = node->rb_right;
851 else {
852 BUG_ON(thread != atoms->thread);
853 return atoms;
854 }
855 }
856 return NULL;
857}
858
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200859static void
mingo39aeb522009-09-14 20:04:48 +0200860__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200861 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200862{
863 struct rb_node **new = &(root->rb_node), *parent = NULL;
864
865 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200866 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200867 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200868
mingo39aeb522009-09-14 20:04:48 +0200869 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200870 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200871
872 cmp = thread_lat_cmp(sort_list, data, this);
873
874 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200875 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200876 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200877 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200878 }
879
880 rb_link_node(&data->node, parent, new);
881 rb_insert_color(&data->node, root);
882}
883
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300884static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200885{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200886 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300887 if (!atoms) {
888 pr_err("No memory at %s\n", __func__);
889 return -1;
890 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200891
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300892 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200893 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300894 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300895 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200896}
897
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300898static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200899{
900 const char *str = TASK_STATE_TO_CHAR_STR;
901
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300902 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200903}
904
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300905static int
mingo39aeb522009-09-14 20:04:48 +0200906add_sched_out_event(struct work_atoms *atoms,
907 char run_state,
908 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200909{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200910 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300911 if (!atom) {
912 pr_err("Non memory at %s", __func__);
913 return -1;
914 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200916 atom->sched_out_time = timestamp;
917
mingo39aeb522009-09-14 20:04:48 +0200918 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200919 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200920 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200921 }
922
mingo39aeb522009-09-14 20:04:48 +0200923 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300924 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200925}
926
927static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300928add_runtime_event(struct work_atoms *atoms, u64 delta,
929 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200930{
931 struct work_atom *atom;
932
933 BUG_ON(list_empty(&atoms->work_list));
934
935 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
936
937 atom->runtime += delta;
938 atoms->total_runtime += delta;
939}
940
941static void
942add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200943{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200944 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200945 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946
mingo39aeb522009-09-14 20:04:48 +0200947 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948 return;
949
mingo39aeb522009-09-14 20:04:48 +0200950 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200952 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200953 return;
954
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200955 if (timestamp < atom->wake_up_time) {
956 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957 return;
958 }
959
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200960 atom->state = THREAD_SCHED_IN;
961 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200962
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200963 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200964 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100965 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200966 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100967 atoms->max_lat_at = timestamp;
968 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200969 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200970}
971
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300972static int latency_switch_event(struct perf_sched *sched,
973 struct perf_evsel *evsel,
974 struct perf_sample *sample,
975 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200976{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300977 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
978 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
979 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200980 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200981 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300982 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300983 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200984 s64 delta;
985
mingo39aeb522009-09-14 20:04:48 +0200986 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200987
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300988 timestamp0 = sched->cpu_last_switched[cpu];
989 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200990 if (timestamp0)
991 delta = timestamp - timestamp0;
992 else
993 delta = 0;
994
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300995 if (delta < 0) {
996 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
997 return -1;
998 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200999
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001000 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1001 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001002 if (sched_out == NULL || sched_in == NULL)
1003 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001004
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001005 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001006 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001007 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001008 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001009 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001010 if (!out_events) {
1011 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001012 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001013 }
mingo39aeb522009-09-14 20:04:48 +02001014 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001015 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001016 return -1;
mingo39aeb522009-09-14 20:04:48 +02001017
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001018 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001019 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001020 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001021 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001022 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001023 if (!in_events) {
1024 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001025 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001026 }
mingo39aeb522009-09-14 20:04:48 +02001027 /*
1028 * Take came in we have not heard about yet,
1029 * add in an initial atom in runnable state:
1030 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001031 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001032 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001033 }
1034 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001035 err = 0;
1036out_put:
1037 thread__put(sched_out);
1038 thread__put(sched_in);
1039 return err;
mingo39aeb522009-09-14 20:04:48 +02001040}
1041
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001042static int latency_runtime_event(struct perf_sched *sched,
1043 struct perf_evsel *evsel,
1044 struct perf_sample *sample,
1045 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001046{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001047 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1048 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001049 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001050 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001051 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001052 int cpu = sample->cpu, err = -1;
1053
1054 if (thread == NULL)
1055 return -1;
mingo39aeb522009-09-14 20:04:48 +02001056
1057 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001058 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001059 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001060 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001061 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001062 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001063 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001064 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001065 }
1066 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001067 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001068 }
1069
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001070 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001071 err = 0;
1072out_put:
1073 thread__put(thread);
1074 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001075}
1076
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001077static int latency_wakeup_event(struct perf_sched *sched,
1078 struct perf_evsel *evsel,
1079 struct perf_sample *sample,
1080 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001081{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001082 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001083 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001084 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001085 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001086 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001087 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001088
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001089 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001090 if (wakee == NULL)
1091 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001093 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001094 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001095 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001096 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001097 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001098 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001099 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001100 }
1101 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001102 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103 }
1104
mingo39aeb522009-09-14 20:04:48 +02001105 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001106
mingo39aeb522009-09-14 20:04:48 +02001107 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001108
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001109 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001110 * As we do not guarantee the wakeup event happens when
1111 * task is out of run queue, also may happen when task is
1112 * on run queue and wakeup only change ->state to TASK_RUNNING,
1113 * then we should not set the ->wake_up_time when wake up a
1114 * task which is on run queue.
1115 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001116 * You WILL be missing events if you've recorded only
1117 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001118 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001119 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001120 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001121 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001122
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001123 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001124 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001125 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001126 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001127 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001128
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001129 atom->state = THREAD_WAIT_CPU;
1130 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001131out_ok:
1132 err = 0;
1133out_put:
1134 thread__put(wakee);
1135 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001136}
1137
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001138static int latency_migrate_task_event(struct perf_sched *sched,
1139 struct perf_evsel *evsel,
1140 struct perf_sample *sample,
1141 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001142{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001143 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001144 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001145 struct work_atoms *atoms;
1146 struct work_atom *atom;
1147 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001148 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001149
1150 /*
1151 * Only need to worry about migration when profiling one CPU.
1152 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001153 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001154 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001155
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001156 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001157 if (migrant == NULL)
1158 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001160 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001161 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001162 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001163 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001164 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001165 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001166 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001167 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001168 }
1169 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001170 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001171 }
1172
1173 BUG_ON(list_empty(&atoms->work_list));
1174
1175 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1176 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1177
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001178 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001179
1180 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001181 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001182 err = 0;
1183out_put:
1184 thread__put(migrant);
1185 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001186}
1187
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001188static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001189{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001190 int i;
1191 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001192 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001193
mingo39aeb522009-09-14 20:04:48 +02001194 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001195 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001196 /*
1197 * Ignore idle threads:
1198 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001199 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001200 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001201
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001202 sched->all_runtime += work_list->total_runtime;
1203 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001204
Josef Bacik2f80dd42015-05-22 09:18:40 -04001205 if (work_list->num_merged > 1)
1206 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1207 else
1208 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209
mingo08f69e62009-09-14 18:30:44 +02001210 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001211 printf(" ");
1212
mingo39aeb522009-09-14 20:04:48 +02001213 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001214
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001215 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 +02001216 (double)work_list->total_runtime / 1e6,
1217 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001218 (double)work_list->max_lat / 1e6,
1219 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001220}
1221
mingo39aeb522009-09-14 20:04:48 +02001222static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001223{
Jiri Olsa0014de12015-11-02 12:10:25 +01001224 if (l->thread == r->thread)
1225 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001226 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001227 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001228 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001229 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001230 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001231}
1232
mingo39aeb522009-09-14 20:04:48 +02001233static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001234{
1235 u64 avgl, avgr;
1236
1237 if (!l->nb_atoms)
1238 return -1;
1239
1240 if (!r->nb_atoms)
1241 return 1;
1242
1243 avgl = l->total_lat / l->nb_atoms;
1244 avgr = r->total_lat / r->nb_atoms;
1245
1246 if (avgl < avgr)
1247 return -1;
1248 if (avgl > avgr)
1249 return 1;
1250
1251 return 0;
1252}
1253
mingo39aeb522009-09-14 20:04:48 +02001254static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001255{
1256 if (l->max_lat < r->max_lat)
1257 return -1;
1258 if (l->max_lat > r->max_lat)
1259 return 1;
1260
1261 return 0;
1262}
1263
mingo39aeb522009-09-14 20:04:48 +02001264static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265{
1266 if (l->nb_atoms < r->nb_atoms)
1267 return -1;
1268 if (l->nb_atoms > r->nb_atoms)
1269 return 1;
1270
1271 return 0;
1272}
1273
mingo39aeb522009-09-14 20:04:48 +02001274static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001275{
1276 if (l->total_runtime < r->total_runtime)
1277 return -1;
1278 if (l->total_runtime > r->total_runtime)
1279 return 1;
1280
1281 return 0;
1282}
1283
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001284static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001285{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001286 size_t i;
1287 static struct sort_dimension avg_sort_dimension = {
1288 .name = "avg",
1289 .cmp = avg_cmp,
1290 };
1291 static struct sort_dimension max_sort_dimension = {
1292 .name = "max",
1293 .cmp = max_cmp,
1294 };
1295 static struct sort_dimension pid_sort_dimension = {
1296 .name = "pid",
1297 .cmp = pid_cmp,
1298 };
1299 static struct sort_dimension runtime_sort_dimension = {
1300 .name = "runtime",
1301 .cmp = runtime_cmp,
1302 };
1303 static struct sort_dimension switch_sort_dimension = {
1304 .name = "switch",
1305 .cmp = switch_cmp,
1306 };
1307 struct sort_dimension *available_sorts[] = {
1308 &pid_sort_dimension,
1309 &avg_sort_dimension,
1310 &max_sort_dimension,
1311 &switch_sort_dimension,
1312 &runtime_sort_dimension,
1313 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001314
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001315 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001316 if (!strcmp(available_sorts[i]->name, tok)) {
1317 list_add_tail(&available_sorts[i]->list, list);
1318
1319 return 0;
1320 }
1321 }
1322
1323 return -1;
1324}
1325
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001326static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001327{
1328 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001329 struct rb_root *root = &sched->atom_root;
1330again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001331 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001332 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001333 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001334 if (!node)
1335 break;
1336
Josef Bacik2f80dd42015-05-22 09:18:40 -04001337 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001338 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001339 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001340 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001341 if (root == &sched->atom_root) {
1342 root = &sched->merged_atom_root;
1343 goto again;
1344 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001345}
1346
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001347static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001348 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001349 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001350 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001351{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001352 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001353
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001354 if (sched->tp_handler->wakeup_event)
1355 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001356
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001357 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001358}
1359
Jiri Olsaa151a372016-04-12 15:29:29 +02001360union map_priv {
1361 void *ptr;
1362 bool color;
1363};
1364
1365static bool thread__has_color(struct thread *thread)
1366{
1367 union map_priv priv = {
1368 .ptr = thread__priv(thread),
1369 };
1370
1371 return priv.color;
1372}
1373
1374static struct thread*
1375map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1376{
1377 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1378 union map_priv priv = {
1379 .color = false,
1380 };
1381
1382 if (!sched->map.color_pids || !thread || thread__priv(thread))
1383 return thread;
1384
1385 if (thread_map__has(sched->map.color_pids, tid))
1386 priv.color = true;
1387
1388 thread__set_priv(thread, priv.ptr);
1389 return thread;
1390}
1391
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001392static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1393 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001394{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001395 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1396 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001397 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001398 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001399 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001400 int i, this_cpu = sample->cpu;
1401 int cpus_nr;
1402 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001403 const char *color = PERF_COLOR_NORMAL;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001404
1405 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1406
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001407 if (this_cpu > sched->max_cpu)
1408 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001409
Jiri Olsa99623c62016-04-12 15:29:26 +02001410 if (sched->map.comp) {
1411 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1412 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1413 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1414 new_cpu = true;
1415 }
1416 } else
1417 cpus_nr = sched->max_cpu;
1418
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001419 timestamp0 = sched->cpu_last_switched[this_cpu];
1420 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001421 if (timestamp0)
1422 delta = timestamp - timestamp0;
1423 else
1424 delta = 0;
1425
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001426 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001427 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001428 return -1;
1429 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001430
Jiri Olsaa151a372016-04-12 15:29:29 +02001431 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001432 if (sched_in == NULL)
1433 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001434
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001435 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001436
1437 printf(" ");
1438
1439 new_shortname = 0;
1440 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001441 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1442 /*
1443 * Don't allocate a letter-number for swapper:0
1444 * as a shortname. Instead, we use '.' for it.
1445 */
1446 sched_in->shortname[0] = '.';
1447 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001448 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001449 sched_in->shortname[0] = sched->next_shortname1;
1450 sched_in->shortname[1] = sched->next_shortname2;
1451
1452 if (sched->next_shortname1 < 'Z') {
1453 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001454 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001455 sched->next_shortname1 = 'A';
1456 if (sched->next_shortname2 < '9')
1457 sched->next_shortname2++;
1458 else
1459 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001460 }
1461 }
1462 new_shortname = 1;
1463 }
1464
Jiri Olsa99623c62016-04-12 15:29:26 +02001465 for (i = 0; i < cpus_nr; i++) {
1466 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001467 struct thread *curr_thread = sched->curr_thread[cpu];
1468 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001469 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001470
1471 if (curr_thread && thread__has_color(curr_thread))
1472 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001473
Jiri Olsa73643bb2016-04-12 15:29:31 +02001474 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1475 continue;
1476
Jiri Olsacf294f22016-04-12 15:29:30 +02001477 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1478 cpu_color = COLOR_CPUS;
1479
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001480 if (cpu != this_cpu)
Jiri Olsacf294f22016-04-12 15:29:30 +02001481 color_fprintf(stdout, cpu_color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001482 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001483 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001484
Dongsheng6bcab4e2014-05-06 14:39:01 +09001485 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001486 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001487 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001488 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001489 }
1490
Jiri Olsa73643bb2016-04-12 15:29:31 +02001491 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1492 goto out;
1493
Jiri Olsa8cd91192016-04-12 15:29:27 +02001494 color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp/1e9);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001495 if (new_shortname) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001496 const char *pid_color = color;
1497
1498 if (thread__has_color(sched_in))
1499 pid_color = COLOR_PIDS;
1500
1501 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001502 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001503 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001504
Jiri Olsa99623c62016-04-12 15:29:26 +02001505 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001506 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001507
Jiri Olsa73643bb2016-04-12 15:29:31 +02001508out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001509 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001510
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001511 thread__put(sched_in);
1512
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001513 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001514}
1515
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001516static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001517 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001518 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001519 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001520{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001521 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001522 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001523 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1524 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001525
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001526 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001527 /*
1528 * Are we trying to switch away a PID that is
1529 * not current?
1530 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001531 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001532 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001533 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001534
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001535 if (sched->tp_handler->switch_event)
1536 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001537
1538 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001539 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001540}
1541
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001542static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001543 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001544 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001545 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001546{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001547 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001548
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001549 if (sched->tp_handler->runtime_event)
1550 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001551
1552 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001553}
1554
David Aherncb627502013-08-07 22:50:47 -04001555static int perf_sched__process_fork_event(struct perf_tool *tool,
1556 union perf_event *event,
1557 struct perf_sample *sample,
1558 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001559{
1560 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1561
David Aherncb627502013-08-07 22:50:47 -04001562 /* run the fork event through the perf machineruy */
1563 perf_event__process_fork(tool, event, sample, machine);
1564
1565 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001566 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001567 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001568
1569 return 0;
1570}
1571
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001572static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001573 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001574 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001575 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001576{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001577 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001578
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001579 if (sched->tp_handler->migrate_task_event)
1580 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001581
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001582 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001583}
1584
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001585typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001586 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001587 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001588 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001589
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001590static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1591 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001592 struct perf_sample *sample,
1593 struct perf_evsel *evsel,
1594 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001595{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001596 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001597
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001598 if (evsel->handler != NULL) {
1599 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001600 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001601 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001602
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001603 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001604}
1605
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001606static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001607{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001608 const struct perf_evsel_str_handler handlers[] = {
1609 { "sched:sched_switch", process_sched_switch_event, },
1610 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1611 { "sched:sched_wakeup", process_sched_wakeup_event, },
1612 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001613 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1614 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001615 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001616 struct perf_data_file file = {
1617 .path = input_name,
1618 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001619 .force = sched->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001620 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001621 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001622
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001623 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001624 if (session == NULL) {
1625 pr_debug("No Memory for session\n");
1626 return -1;
1627 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001628
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001629 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001630
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001631 if (perf_session__set_tracepoints_handlers(session, handlers))
1632 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001633
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001634 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001635 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001636 if (err) {
1637 pr_err("Failed to process events, error %d", err);
1638 goto out_delete;
1639 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001640
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001641 sched->nr_events = session->evlist->stats.nr_events[0];
1642 sched->nr_lost_events = session->evlist->stats.total_lost;
1643 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001644 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001645
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001646 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001647out_delete:
1648 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001649 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001650}
1651
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001652static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001653{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001654 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001655 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001656 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1657 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001658 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001659 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001660 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001661 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1662 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001663 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001664 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001665 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001666 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1667 sched->nr_context_switch_bugs, sched->nr_timestamps);
1668 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001669 printf(" (due to lost events?)");
1670 printf("\n");
1671 }
1672}
1673
Josef Bacik2f80dd42015-05-22 09:18:40 -04001674static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
1675{
1676 struct rb_node **new = &(root->rb_node), *parent = NULL;
1677 struct work_atoms *this;
1678 const char *comm = thread__comm_str(data->thread), *this_comm;
1679
1680 while (*new) {
1681 int cmp;
1682
1683 this = container_of(*new, struct work_atoms, node);
1684 parent = *new;
1685
1686 this_comm = thread__comm_str(this->thread);
1687 cmp = strcmp(comm, this_comm);
1688 if (cmp > 0) {
1689 new = &((*new)->rb_left);
1690 } else if (cmp < 0) {
1691 new = &((*new)->rb_right);
1692 } else {
1693 this->num_merged++;
1694 this->total_runtime += data->total_runtime;
1695 this->nb_atoms += data->nb_atoms;
1696 this->total_lat += data->total_lat;
1697 list_splice(&data->work_list, &this->work_list);
1698 if (this->max_lat < data->max_lat) {
1699 this->max_lat = data->max_lat;
1700 this->max_lat_at = data->max_lat_at;
1701 }
1702 zfree(&data);
1703 return;
1704 }
1705 }
1706
1707 data->num_merged++;
1708 rb_link_node(&data->node, parent, new);
1709 rb_insert_color(&data->node, root);
1710}
1711
1712static void perf_sched__merge_lat(struct perf_sched *sched)
1713{
1714 struct work_atoms *data;
1715 struct rb_node *node;
1716
1717 if (sched->skip_merge)
1718 return;
1719
1720 while ((node = rb_first(&sched->atom_root))) {
1721 rb_erase(node, &sched->atom_root);
1722 data = rb_entry(node, struct work_atoms, node);
1723 __merge_work_atoms(&sched->merged_atom_root, data);
1724 }
1725}
1726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001727static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001728{
1729 struct rb_node *next;
1730
1731 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001732
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001733 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001734 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001735
Josef Bacik2f80dd42015-05-22 09:18:40 -04001736 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001737 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001738
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001739 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1740 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1741 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001742
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001743 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001744
1745 while (next) {
1746 struct work_atoms *work_list;
1747
1748 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001749 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001750 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001751 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001752 }
1753
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001754 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001755 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001756 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001757
1758 printf(" ---------------------------------------------------\n");
1759
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001760 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001761 printf("\n");
1762
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001763 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001764}
1765
Jiri Olsa99623c62016-04-12 15:29:26 +02001766static int setup_map_cpus(struct perf_sched *sched)
1767{
Jiri Olsa73643bb2016-04-12 15:29:31 +02001768 struct cpu_map *map;
1769
Jiri Olsa99623c62016-04-12 15:29:26 +02001770 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1771
1772 if (sched->map.comp) {
1773 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02001774 if (!sched->map.comp_cpus)
1775 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02001776 }
1777
Jiri Olsa73643bb2016-04-12 15:29:31 +02001778 if (!sched->map.cpus_str)
1779 return 0;
1780
1781 map = cpu_map__new(sched->map.cpus_str);
1782 if (!map) {
1783 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
1784 return -1;
1785 }
1786
1787 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02001788 return 0;
1789}
1790
Jiri Olsaa151a372016-04-12 15:29:29 +02001791static int setup_color_pids(struct perf_sched *sched)
1792{
1793 struct thread_map *map;
1794
1795 if (!sched->map.color_pids_str)
1796 return 0;
1797
1798 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
1799 if (!map) {
1800 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
1801 return -1;
1802 }
1803
1804 sched->map.color_pids = map;
1805 return 0;
1806}
1807
Jiri Olsacf294f22016-04-12 15:29:30 +02001808static int setup_color_cpus(struct perf_sched *sched)
1809{
1810 struct cpu_map *map;
1811
1812 if (!sched->map.color_cpus_str)
1813 return 0;
1814
1815 map = cpu_map__new(sched->map.color_cpus_str);
1816 if (!map) {
1817 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
1818 return -1;
1819 }
1820
1821 sched->map.color_cpus = map;
1822 return 0;
1823}
1824
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001825static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001826{
Jiri Olsa99623c62016-04-12 15:29:26 +02001827 if (setup_map_cpus(sched))
1828 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02001829
Jiri Olsaa151a372016-04-12 15:29:29 +02001830 if (setup_color_pids(sched))
1831 return -1;
1832
Jiri Olsacf294f22016-04-12 15:29:30 +02001833 if (setup_color_cpus(sched))
1834 return -1;
1835
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001836 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001837 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001838 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001839 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001840 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001841}
1842
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001843static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001844{
1845 unsigned long i;
1846
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001847 calibrate_run_measurement_overhead(sched);
1848 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001849
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001850 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001851
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001852 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001853 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001854
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001855 printf("nr_run_events: %ld\n", sched->nr_run_events);
1856 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1857 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001858
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001859 if (sched->targetless_wakeups)
1860 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1861 if (sched->multitarget_wakeups)
1862 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1863 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001864 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001865 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001866
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001867 print_task_traces(sched);
1868 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001869
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001870 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001871 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001872 for (i = 0; i < sched->replay_repeat; i++)
1873 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001874
1875 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001876}
1877
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001878static void setup_sorting(struct perf_sched *sched, const struct option *options,
1879 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001880{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001881 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001882
1883 for (tok = strtok_r(str, ", ", &tmp);
1884 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001885 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001886 usage_with_options_msg(usage_msg, options,
1887 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001888 }
1889 }
1890
1891 free(str);
1892
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001893 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001894}
1895
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001896static int __cmd_record(int argc, const char **argv)
1897{
1898 unsigned int rec_argc, i, j;
1899 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001900 const char * const record_args[] = {
1901 "record",
1902 "-a",
1903 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001904 "-m", "1024",
1905 "-c", "1",
1906 "-e", "sched:sched_switch",
1907 "-e", "sched:sched_stat_wait",
1908 "-e", "sched:sched_stat_sleep",
1909 "-e", "sched:sched_stat_iowait",
1910 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001911 "-e", "sched:sched_process_fork",
1912 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001913 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001914 "-e", "sched:sched_migrate_task",
1915 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001916
1917 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1918 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1919
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001920 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001921 return -ENOMEM;
1922
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001923 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1924 rec_argv[i] = strdup(record_args[i]);
1925
1926 for (j = 1; j < (unsigned int)argc; j++, i++)
1927 rec_argv[i] = argv[j];
1928
1929 BUG_ON(i != rec_argc);
1930
1931 return cmd_record(i, rec_argv, NULL);
1932}
1933
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001934int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001935{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001936 const char default_sort_order[] = "avg, max, switch, runtime";
1937 struct perf_sched sched = {
1938 .tool = {
1939 .sample = perf_sched__process_tracepoint_sample,
1940 .comm = perf_event__process_comm,
1941 .lost = perf_event__process_lost,
1942 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001943 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001944 },
1945 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1946 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1947 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1948 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001949 .sort_order = default_sort_order,
1950 .replay_repeat = 10,
1951 .profile_cpu = -1,
1952 .next_shortname1 = 'A',
1953 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04001954 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001955 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001956 const struct option latency_options[] = {
1957 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1958 "sort by key(s): runtime, switch, avg, max"),
1959 OPT_INCR('v', "verbose", &verbose,
1960 "be more verbose (show symbol address, etc)"),
1961 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1962 "CPU to profile on"),
1963 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1964 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04001965 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
1966 "latency stats per pid instead of per comm"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001967 OPT_END()
1968 };
1969 const struct option replay_options[] = {
1970 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1971 "repeat the workload replay N times (-1: infinite)"),
1972 OPT_INCR('v', "verbose", &verbose,
1973 "be more verbose (show symbol address, etc)"),
1974 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1975 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08001976 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001977 OPT_END()
1978 };
1979 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001980 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001981 "input file name"),
1982 OPT_INCR('v', "verbose", &verbose,
1983 "be more verbose (show symbol address, etc)"),
1984 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1985 "dump raw trace in ASCII"),
1986 OPT_END()
1987 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001988 const struct option map_options[] = {
1989 OPT_BOOLEAN(0, "compact", &sched.map.comp,
1990 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02001991 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
1992 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02001993 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
1994 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02001995 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
1996 "display given CPUs in map"),
Jiri Olsa99623c62016-04-12 15:29:26 +02001997 OPT_END()
1998 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001999 const char * const latency_usage[] = {
2000 "perf sched latency [<options>]",
2001 NULL
2002 };
2003 const char * const replay_usage[] = {
2004 "perf sched replay [<options>]",
2005 NULL
2006 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002007 const char * const map_usage[] = {
2008 "perf sched map [<options>]",
2009 NULL
2010 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002011 const char *const sched_subcommands[] = { "record", "latency", "map",
2012 "replay", "script", NULL };
2013 const char *sched_usage[] = {
2014 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002015 NULL
2016 };
2017 struct trace_sched_handler lat_ops = {
2018 .wakeup_event = latency_wakeup_event,
2019 .switch_event = latency_switch_event,
2020 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002021 .migrate_task_event = latency_migrate_task_event,
2022 };
2023 struct trace_sched_handler map_ops = {
2024 .switch_event = map_switch_event,
2025 };
2026 struct trace_sched_handler replay_ops = {
2027 .wakeup_event = replay_wakeup_event,
2028 .switch_event = replay_switch_event,
2029 .fork_event = replay_fork_event,
2030 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002031 unsigned int i;
2032
2033 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2034 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002035
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002036 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2037 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002038 if (!argc)
2039 usage_with_options(sched_usage, sched_options);
2040
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08002041 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002042 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08002043 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002044 if (!strcmp(argv[0], "script"))
2045 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c5a2009-12-07 12:04:49 +08002046
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002047 if (!strncmp(argv[0], "rec", 3)) {
2048 return __cmd_record(argc, argv);
2049 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002050 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002051 if (argc > 1) {
2052 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2053 if (argc)
2054 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002055 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002056 setup_sorting(&sched, latency_options, latency_usage);
2057 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002058 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002059 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002060 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002061 if (argc)
2062 usage_with_options(map_usage, map_options);
2063 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002064 sched.tp_handler = &map_ops;
2065 setup_sorting(&sched, latency_options, latency_usage);
2066 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002067 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002068 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002069 if (argc) {
2070 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2071 if (argc)
2072 usage_with_options(replay_usage, replay_options);
2073 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002074 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002075 } else {
2076 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002077 }
2078
Ingo Molnarec156762009-09-11 12:12:54 +02002079 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002080}