blob: 7de04b297c1458142fccb9c307d4b7671c9d3cfb [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 Olsa99623c62016-04-12 15:29:26 +0200138};
139
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300140struct perf_sched {
141 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300142 const char *sort_order;
143 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800144 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300145 struct task_desc **tasks;
146 const struct trace_sched_handler *tp_handler;
147 pthread_mutex_t start_work_mutex;
148 pthread_mutex_t work_done_wait_mutex;
149 int profile_cpu;
150/*
151 * Track the current task - that way we can know whether there's any
152 * weird events, such as a task being switched away that is not current.
153 */
154 int max_cpu;
155 u32 curr_pid[MAX_CPUS];
156 struct thread *curr_thread[MAX_CPUS];
157 char next_shortname1;
158 char next_shortname2;
159 unsigned int replay_repeat;
160 unsigned long nr_run_events;
161 unsigned long nr_sleep_events;
162 unsigned long nr_wakeup_events;
163 unsigned long nr_sleep_corrections;
164 unsigned long nr_run_events_optimized;
165 unsigned long targetless_wakeups;
166 unsigned long multitarget_wakeups;
167 unsigned long nr_runs;
168 unsigned long nr_timestamps;
169 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300170 unsigned long nr_context_switch_bugs;
171 unsigned long nr_events;
172 unsigned long nr_lost_chunks;
173 unsigned long nr_lost_events;
174 u64 run_measurement_overhead;
175 u64 sleep_measurement_overhead;
176 u64 start_time;
177 u64 cpu_usage;
178 u64 runavg_cpu_usage;
179 u64 parent_cpu_usage;
180 u64 runavg_parent_cpu_usage;
181 u64 sum_runtime;
182 u64 sum_fluct;
183 u64 run_avg;
184 u64 all_runtime;
185 u64 all_count;
186 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400187 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300188 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800189 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400190 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200191 struct perf_sched_map map;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300192};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200193
194static u64 get_nsecs(void)
195{
196 struct timespec ts;
197
198 clock_gettime(CLOCK_MONOTONIC, &ts);
199
200 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
201}
202
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300203static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200204{
205 u64 T0 = get_nsecs(), T1;
206
207 do {
208 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300209 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200210}
211
212static void sleep_nsecs(u64 nsecs)
213{
214 struct timespec ts;
215
216 ts.tv_nsec = nsecs % 999999999;
217 ts.tv_sec = nsecs / 999999999;
218
219 nanosleep(&ts, NULL);
220}
221
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300222static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200223{
224 u64 T0, T1, delta, min_delta = 1000000000ULL;
225 int i;
226
227 for (i = 0; i < 10; i++) {
228 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300229 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200230 T1 = get_nsecs();
231 delta = T1-T0;
232 min_delta = min(min_delta, delta);
233 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300234 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200235
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200236 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200237}
238
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300239static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200240{
241 u64 T0, T1, delta, min_delta = 1000000000ULL;
242 int i;
243
244 for (i = 0; i < 10; i++) {
245 T0 = get_nsecs();
246 sleep_nsecs(10000);
247 T1 = get_nsecs();
248 delta = T1-T0;
249 min_delta = min(min_delta, delta);
250 }
251 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300252 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200253
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200254 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200255}
256
mingo39aeb522009-09-14 20:04:48 +0200257static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200258get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200259{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200260 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200261 unsigned long idx = task->nr_events;
262 size_t size;
263
264 event->timestamp = timestamp;
265 event->nr = idx;
266
267 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200268 size = sizeof(struct sched_atom *) * task->nr_events;
269 task->atoms = realloc(task->atoms, size);
270 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200271
mingo39aeb522009-09-14 20:04:48 +0200272 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200273
274 return event;
275}
276
mingo39aeb522009-09-14 20:04:48 +0200277static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200278{
279 if (!task->nr_events)
280 return NULL;
281
mingo39aeb522009-09-14 20:04:48 +0200282 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200283}
284
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300285static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
286 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200287{
mingo39aeb522009-09-14 20:04:48 +0200288 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200289
290 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200291 * optimize an existing RUN event by merging this one
292 * to it:
293 */
Ingo Molnarec156762009-09-11 12:12:54 +0200294 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300295 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200296 curr_event->duration += duration;
297 return;
298 }
299
300 event = get_new_event(task, timestamp);
301
302 event->type = SCHED_EVENT_RUN;
303 event->duration = duration;
304
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300305 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200306}
307
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300308static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
309 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200310{
mingo39aeb522009-09-14 20:04:48 +0200311 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200312
313 event = get_new_event(task, timestamp);
314 event->type = SCHED_EVENT_WAKEUP;
315 event->wakee = wakee;
316
317 wakee_event = last_event(wakee);
318 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300319 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200320 return;
321 }
322 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300323 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200324 return;
325 }
326
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200327 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200328 sem_init(wakee_event->wait_sem, 0, 0);
329 wakee_event->specific_wait = 1;
330 event->wait_sem = wakee_event->wait_sem;
331
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300332 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200333}
334
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300335static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
336 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200337{
mingo39aeb522009-09-14 20:04:48 +0200338 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200339
340 event->type = SCHED_EVENT_SLEEP;
341
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300342 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200343}
344
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300345static struct task_desc *register_pid(struct perf_sched *sched,
346 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200347{
348 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800349 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200350
Yunlong Songcb06ac22015-03-31 21:46:30 +0800351 if (sched->pid_to_task == NULL) {
352 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
353 pid_max = MAX_PID;
354 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
355 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800356 if (pid >= (unsigned long)pid_max) {
357 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
358 sizeof(struct task_desc *))) == NULL);
359 while (pid >= (unsigned long)pid_max)
360 sched->pid_to_task[pid_max++] = NULL;
361 }
Ingo Molnarec156762009-09-11 12:12:54 +0200362
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300363 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200364
365 if (task)
366 return task;
367
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200368 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200369 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300370 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200371 strcpy(task->comm, comm);
372 /*
373 * every task starts in sleeping state - this gets ignored
374 * if there's no wakeup pointing to this sleep state:
375 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300376 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200377
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300378 sched->pid_to_task[pid] = task;
379 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800380 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300381 BUG_ON(!sched->tasks);
382 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200383
Ingo Molnarad236fd2009-09-11 12:12:54 +0200384 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300385 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200386
387 return task;
388}
389
390
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300391static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200392{
393 struct task_desc *task;
394 unsigned long i;
395
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300396 for (i = 0; i < sched->nr_tasks; i++) {
397 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200398 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200399 task->nr, task->comm, task->pid, task->nr_events);
400 }
401}
402
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300403static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200404{
405 struct task_desc *task1, *task2;
406 unsigned long i, j;
407
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300408 for (i = 0; i < sched->nr_tasks; i++) {
409 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200410 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300411 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200412 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300413 task2 = sched->tasks[j];
414 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200415 }
416}
417
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300418static void perf_sched__process_event(struct perf_sched *sched,
419 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200420{
421 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200422
mingo39aeb522009-09-14 20:04:48 +0200423 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200424 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300425 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200426 break;
427 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200428 if (atom->wait_sem)
429 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200430 BUG_ON(ret);
431 break;
432 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200433 if (atom->wait_sem)
434 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200435 BUG_ON(ret);
436 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200437 case SCHED_EVENT_MIGRATION:
438 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200439 default:
440 BUG_ON(1);
441 }
442}
443
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200444static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200445{
446 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200447 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200448 int err;
449
450 err = getrusage(RUSAGE_SELF, &ru);
451 BUG_ON(err);
452
453 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
454 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
455
456 return sum;
457}
458
Yunlong Song939cda52015-03-31 21:46:34 +0800459static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200460{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800461 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800462 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800463 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800464 struct rlimit limit;
465 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800466
467 memset(&attr, 0, sizeof(attr));
468
469 attr.type = PERF_TYPE_SOFTWARE;
470 attr.config = PERF_COUNT_SW_TASK_CLOCK;
471
Yunlong Song939cda52015-03-31 21:46:34 +0800472force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200473 fd = sys_perf_event_open(&attr, 0, -1, -1,
474 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800475
Yunlong Song1aff59b2015-03-31 21:46:33 +0800476 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800477 if (errno == EMFILE) {
478 if (sched->force) {
479 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
480 limit.rlim_cur += sched->nr_tasks - cur_task;
481 if (limit.rlim_cur > limit.rlim_max) {
482 limit.rlim_max = limit.rlim_cur;
483 need_privilege = true;
484 }
485 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
486 if (need_privilege && errno == EPERM)
487 strcpy(info, "Need privilege\n");
488 } else
489 goto force_again;
490 } else
491 strcpy(info, "Have a try with -f option\n");
492 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900493 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800494 "with %d (%s)\n%s", fd,
495 strerror_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800496 exit(EXIT_FAILURE);
497 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800498 return fd;
499}
500
501static u64 get_cpu_usage_nsec_self(int fd)
502{
503 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200504 int ret;
505
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800506 ret = read(fd, &runtime, sizeof(runtime));
507 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200508
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800509 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200510}
511
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300512struct sched_thread_parms {
513 struct task_desc *task;
514 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800515 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300516};
517
Ingo Molnarec156762009-09-11 12:12:54 +0200518static void *thread_func(void *ctx)
519{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300520 struct sched_thread_parms *parms = ctx;
521 struct task_desc *this_task = parms->task;
522 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200523 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200524 unsigned long i, ret;
525 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800526 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200527
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300528 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300529
Ingo Molnarec156762009-09-11 12:12:54 +0200530 sprintf(comm2, ":%s", this_task->comm);
531 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300532 if (fd < 0)
533 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200534again:
535 ret = sem_post(&this_task->ready_for_work);
536 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300537 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200538 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300539 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200540 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200541
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800542 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200543
544 for (i = 0; i < this_task->nr_events; i++) {
545 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300546 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200547 }
548
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800549 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200550 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200551 ret = sem_post(&this_task->work_done_sem);
552 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200553
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300554 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200555 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300556 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200557 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200558
559 goto again;
560}
561
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300562static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200563{
564 struct task_desc *task;
565 pthread_attr_t attr;
566 unsigned long i;
567 int err;
568
569 err = pthread_attr_init(&attr);
570 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200571 err = pthread_attr_setstacksize(&attr,
572 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200573 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300574 err = pthread_mutex_lock(&sched->start_work_mutex);
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->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200577 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300578 for (i = 0; i < sched->nr_tasks; i++) {
579 struct sched_thread_parms *parms = malloc(sizeof(*parms));
580 BUG_ON(parms == NULL);
581 parms->task = task = sched->tasks[i];
582 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800583 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200584 sem_init(&task->sleep_sem, 0, 0);
585 sem_init(&task->ready_for_work, 0, 0);
586 sem_init(&task->work_done_sem, 0, 0);
587 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300588 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200589 BUG_ON(err);
590 }
591}
592
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300593static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200594{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200595 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200596 struct task_desc *task;
597 unsigned long i, ret;
598
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300599 sched->start_time = get_nsecs();
600 sched->cpu_usage = 0;
601 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200602
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300603 for (i = 0; i < sched->nr_tasks; i++) {
604 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200605 ret = sem_wait(&task->ready_for_work);
606 BUG_ON(ret);
607 sem_init(&task->ready_for_work, 0, 0);
608 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300609 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200610 BUG_ON(ret);
611
612 cpu_usage_0 = get_cpu_usage_nsec_parent();
613
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300614 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200615
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300616 for (i = 0; i < sched->nr_tasks; i++) {
617 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200618 ret = sem_wait(&task->work_done_sem);
619 BUG_ON(ret);
620 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300621 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200622 task->cpu_usage = 0;
623 }
624
625 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300626 if (!sched->runavg_cpu_usage)
627 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800628 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 +0200629
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300630 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
631 if (!sched->runavg_parent_cpu_usage)
632 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800633 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
634 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300636 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200637 BUG_ON(ret);
638
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300639 for (i = 0; i < sched->nr_tasks; i++) {
640 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200641 sem_init(&task->sleep_sem, 0, 0);
642 task->curr_event = 0;
643 }
644}
645
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300646static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200647{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500648 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200649
650 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300651 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200652 T1 = get_nsecs();
653
654 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 sched->sum_runtime += delta;
656 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200657
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300658 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200659 if (delta < avg_delta)
660 fluct = avg_delta - delta;
661 else
662 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300663 sched->sum_fluct += fluct;
664 if (!sched->run_avg)
665 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800666 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200667
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300668 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
Ingo Molnarec156762009-09-11 12:12:54 +0200669
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200671
Ingo Molnarad236fd2009-09-11 12:12:54 +0200672 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200674
675#if 0
676 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200677 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300678 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200679 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200680 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300681 (double)sched->parent_cpu_usage/1e6,
682 (double)sched->runavg_parent_cpu_usage/1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200683#endif
684
Ingo Molnarad236fd2009-09-11 12:12:54 +0200685 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200686
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300687 if (sched->nr_sleep_corrections)
688 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
689 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200690}
691
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300692static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200693{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200694 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200695
696 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300697 burn_nsecs(sched, 1e6);
Ingo Molnarec156762009-09-11 12:12:54 +0200698 T1 = get_nsecs();
699
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200700 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200701
702 T0 = get_nsecs();
703 sleep_nsecs(1e6);
704 T1 = get_nsecs();
705
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200706 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200707}
708
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300709static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300710replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300711 struct perf_evsel *evsel, struct perf_sample *sample,
712 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200713{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300714 const char *comm = perf_evsel__strval(evsel, sample, "comm");
715 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200716 struct task_desc *waker, *wakee;
717
718 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300719 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200720
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300721 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200722 }
723
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300724 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300725 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300727 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300728 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200729}
730
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300731static int replay_switch_event(struct perf_sched *sched,
732 struct perf_evsel *evsel,
733 struct perf_sample *sample,
734 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200735{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300736 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
737 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
738 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
739 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
740 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300741 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300742 u64 timestamp0, timestamp = sample->time;
743 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200744 s64 delta;
745
Ingo Molnarad236fd2009-09-11 12:12:54 +0200746 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300747 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200748
Ingo Molnarfbf94822009-09-11 12:12:54 +0200749 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300750 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200751
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300752 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200753 if (timestamp0)
754 delta = timestamp - timestamp0;
755 else
756 delta = 0;
757
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300758 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900759 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300760 return -1;
761 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200762
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300763 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
764 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200765
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300766 prev = register_pid(sched, prev_pid, prev_comm);
767 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200768
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300769 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200770
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300771 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300772 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300773
774 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200775}
776
David Aherncb627502013-08-07 22:50:47 -0400777static int replay_fork_event(struct perf_sched *sched,
778 union perf_event *event,
779 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200780{
David Aherncb627502013-08-07 22:50:47 -0400781 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300782
Adrian Hunter314add62013-08-27 11:23:03 +0300783 child = machine__findnew_thread(machine, event->fork.pid,
784 event->fork.tid);
785 parent = machine__findnew_thread(machine, event->fork.ppid,
786 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400787
788 if (child == NULL || parent == NULL) {
789 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
790 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300791 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200792 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300793
David Aherncb627502013-08-07 22:50:47 -0400794 if (verbose) {
795 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200796 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
797 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400798 }
799
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200800 register_pid(sched, parent->tid, thread__comm_str(parent));
801 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300802out_put:
803 thread__put(child);
804 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300805 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200806}
807
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200808struct sort_dimension {
809 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200810 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200811 struct list_head list;
812};
813
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200814static int
mingo39aeb522009-09-14 20:04:48 +0200815thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200816{
817 struct sort_dimension *sort;
818 int ret = 0;
819
Ingo Molnarb5fae122009-09-11 12:12:54 +0200820 BUG_ON(list_empty(list));
821
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200822 list_for_each_entry(sort, list, list) {
823 ret = sort->cmp(l, r);
824 if (ret)
825 return ret;
826 }
827
828 return ret;
829}
830
mingo39aeb522009-09-14 20:04:48 +0200831static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200832thread_atoms_search(struct rb_root *root, struct thread *thread,
833 struct list_head *sort_list)
834{
835 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200836 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200837
838 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200839 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200840 int cmp;
841
mingo39aeb522009-09-14 20:04:48 +0200842 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200843
844 cmp = thread_lat_cmp(sort_list, &key, atoms);
845 if (cmp > 0)
846 node = node->rb_left;
847 else if (cmp < 0)
848 node = node->rb_right;
849 else {
850 BUG_ON(thread != atoms->thread);
851 return atoms;
852 }
853 }
854 return NULL;
855}
856
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200857static void
mingo39aeb522009-09-14 20:04:48 +0200858__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200859 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200860{
861 struct rb_node **new = &(root->rb_node), *parent = NULL;
862
863 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200864 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200865 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200866
mingo39aeb522009-09-14 20:04:48 +0200867 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200868 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200869
870 cmp = thread_lat_cmp(sort_list, data, this);
871
872 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200873 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200874 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200875 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200876 }
877
878 rb_link_node(&data->node, parent, new);
879 rb_insert_color(&data->node, root);
880}
881
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300882static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200883{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200884 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300885 if (!atoms) {
886 pr_err("No memory at %s\n", __func__);
887 return -1;
888 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200889
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300890 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200891 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300892 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300893 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200894}
895
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300896static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200897{
898 const char *str = TASK_STATE_TO_CHAR_STR;
899
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300900 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200901}
902
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300903static int
mingo39aeb522009-09-14 20:04:48 +0200904add_sched_out_event(struct work_atoms *atoms,
905 char run_state,
906 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200908 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300909 if (!atom) {
910 pr_err("Non memory at %s", __func__);
911 return -1;
912 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200913
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200914 atom->sched_out_time = timestamp;
915
mingo39aeb522009-09-14 20:04:48 +0200916 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200917 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200918 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200919 }
920
mingo39aeb522009-09-14 20:04:48 +0200921 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300922 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923}
924
925static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300926add_runtime_event(struct work_atoms *atoms, u64 delta,
927 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200928{
929 struct work_atom *atom;
930
931 BUG_ON(list_empty(&atoms->work_list));
932
933 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
934
935 atom->runtime += delta;
936 atoms->total_runtime += delta;
937}
938
939static void
940add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200942 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200943 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200944
mingo39aeb522009-09-14 20:04:48 +0200945 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946 return;
947
mingo39aeb522009-09-14 20:04:48 +0200948 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200949
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200950 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951 return;
952
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200953 if (timestamp < atom->wake_up_time) {
954 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200955 return;
956 }
957
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200958 atom->state = THREAD_SCHED_IN;
959 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200960
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200961 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200962 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100963 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200964 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100965 atoms->max_lat_at = timestamp;
966 }
Frederic Weisbecker66685672009-09-13 01:56:25 +0200967 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200968}
969
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300970static int latency_switch_event(struct perf_sched *sched,
971 struct perf_evsel *evsel,
972 struct perf_sample *sample,
973 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200974{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300975 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
976 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
977 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +0200978 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200979 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300980 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300981 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200982 s64 delta;
983
mingo39aeb522009-09-14 20:04:48 +0200984 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +0200985
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300986 timestamp0 = sched->cpu_last_switched[cpu];
987 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +0200988 if (timestamp0)
989 delta = timestamp - timestamp0;
990 else
991 delta = 0;
992
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300993 if (delta < 0) {
994 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
995 return -1;
996 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200997
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300998 sched_out = machine__findnew_thread(machine, -1, prev_pid);
999 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001000 if (sched_out == NULL || sched_in == NULL)
1001 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001002
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001003 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001004 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001005 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001006 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001007 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001008 if (!out_events) {
1009 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001010 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001011 }
mingo39aeb522009-09-14 20:04:48 +02001012 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001013 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001014 return -1;
mingo39aeb522009-09-14 20:04:48 +02001015
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001016 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001017 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001018 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001019 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001020 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001021 if (!in_events) {
1022 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001023 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001024 }
mingo39aeb522009-09-14 20:04:48 +02001025 /*
1026 * Take came in we have not heard about yet,
1027 * add in an initial atom in runnable state:
1028 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001029 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001030 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001031 }
1032 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001033 err = 0;
1034out_put:
1035 thread__put(sched_out);
1036 thread__put(sched_in);
1037 return err;
mingo39aeb522009-09-14 20:04:48 +02001038}
1039
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001040static int latency_runtime_event(struct perf_sched *sched,
1041 struct perf_evsel *evsel,
1042 struct perf_sample *sample,
1043 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001044{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001045 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1046 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001047 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001048 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001049 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001050 int cpu = sample->cpu, err = -1;
1051
1052 if (thread == NULL)
1053 return -1;
mingo39aeb522009-09-14 20:04:48 +02001054
1055 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001056 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001057 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001058 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001059 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001060 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001061 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001062 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001063 }
1064 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001065 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001066 }
1067
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001068 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001069 err = 0;
1070out_put:
1071 thread__put(thread);
1072 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001073}
1074
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001075static int latency_wakeup_event(struct perf_sched *sched,
1076 struct perf_evsel *evsel,
1077 struct perf_sample *sample,
1078 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001079{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001080 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001081 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001082 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001083 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001084 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001085 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001086
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001087 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001088 if (wakee == NULL)
1089 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001090 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001091 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001093 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001094 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001096 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001097 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001098 }
1099 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001100 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001101 }
1102
mingo39aeb522009-09-14 20:04:48 +02001103 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001104
mingo39aeb522009-09-14 20:04:48 +02001105 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001106
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001107 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001108 * As we do not guarantee the wakeup event happens when
1109 * task is out of run queue, also may happen when task is
1110 * on run queue and wakeup only change ->state to TASK_RUNNING,
1111 * then we should not set the ->wake_up_time when wake up a
1112 * task which is on run queue.
1113 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001114 * You WILL be missing events if you've recorded only
1115 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001116 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001117 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001118 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001119 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001121 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001122 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001123 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001124 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001125 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001126
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001127 atom->state = THREAD_WAIT_CPU;
1128 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001129out_ok:
1130 err = 0;
1131out_put:
1132 thread__put(wakee);
1133 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134}
1135
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001136static int latency_migrate_task_event(struct perf_sched *sched,
1137 struct perf_evsel *evsel,
1138 struct perf_sample *sample,
1139 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001140{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001141 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001142 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001143 struct work_atoms *atoms;
1144 struct work_atom *atom;
1145 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001146 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001147
1148 /*
1149 * Only need to worry about migration when profiling one CPU.
1150 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001151 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001152 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001153
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001154 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001155 if (migrant == NULL)
1156 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001157 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001158 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001160 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001161 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001162 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001163 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001164 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001165 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001166 }
1167 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001168 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001169 }
1170
1171 BUG_ON(list_empty(&atoms->work_list));
1172
1173 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1174 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1175
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001176 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001177
1178 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001179 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001180 err = 0;
1181out_put:
1182 thread__put(migrant);
1183 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001184}
1185
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001186static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001187{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001188 int i;
1189 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001190 u64 avg;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001191
mingo39aeb522009-09-14 20:04:48 +02001192 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001193 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001194 /*
1195 * Ignore idle threads:
1196 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001197 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001198 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001199
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001200 sched->all_runtime += work_list->total_runtime;
1201 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001202
Josef Bacik2f80dd42015-05-22 09:18:40 -04001203 if (work_list->num_merged > 1)
1204 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1205 else
1206 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001207
mingo08f69e62009-09-14 18:30:44 +02001208 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209 printf(" ");
1210
mingo39aeb522009-09-14 20:04:48 +02001211 avg = work_list->total_lat / work_list->nb_atoms;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001212
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001213 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 +02001214 (double)work_list->total_runtime / 1e6,
1215 work_list->nb_atoms, (double)avg / 1e6,
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001216 (double)work_list->max_lat / 1e6,
1217 (double)work_list->max_lat_at / 1e9);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001218}
1219
mingo39aeb522009-09-14 20:04:48 +02001220static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001221{
Jiri Olsa0014de12015-11-02 12:10:25 +01001222 if (l->thread == r->thread)
1223 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001224 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001225 return -1;
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;
Jiri Olsa0014de12015-11-02 12:10:25 +01001228 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001229}
1230
mingo39aeb522009-09-14 20:04:48 +02001231static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001232{
1233 u64 avgl, avgr;
1234
1235 if (!l->nb_atoms)
1236 return -1;
1237
1238 if (!r->nb_atoms)
1239 return 1;
1240
1241 avgl = l->total_lat / l->nb_atoms;
1242 avgr = r->total_lat / r->nb_atoms;
1243
1244 if (avgl < avgr)
1245 return -1;
1246 if (avgl > avgr)
1247 return 1;
1248
1249 return 0;
1250}
1251
mingo39aeb522009-09-14 20:04:48 +02001252static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001253{
1254 if (l->max_lat < r->max_lat)
1255 return -1;
1256 if (l->max_lat > r->max_lat)
1257 return 1;
1258
1259 return 0;
1260}
1261
mingo39aeb522009-09-14 20:04:48 +02001262static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001263{
1264 if (l->nb_atoms < r->nb_atoms)
1265 return -1;
1266 if (l->nb_atoms > r->nb_atoms)
1267 return 1;
1268
1269 return 0;
1270}
1271
mingo39aeb522009-09-14 20:04:48 +02001272static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001273{
1274 if (l->total_runtime < r->total_runtime)
1275 return -1;
1276 if (l->total_runtime > r->total_runtime)
1277 return 1;
1278
1279 return 0;
1280}
1281
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001282static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001283{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001284 size_t i;
1285 static struct sort_dimension avg_sort_dimension = {
1286 .name = "avg",
1287 .cmp = avg_cmp,
1288 };
1289 static struct sort_dimension max_sort_dimension = {
1290 .name = "max",
1291 .cmp = max_cmp,
1292 };
1293 static struct sort_dimension pid_sort_dimension = {
1294 .name = "pid",
1295 .cmp = pid_cmp,
1296 };
1297 static struct sort_dimension runtime_sort_dimension = {
1298 .name = "runtime",
1299 .cmp = runtime_cmp,
1300 };
1301 static struct sort_dimension switch_sort_dimension = {
1302 .name = "switch",
1303 .cmp = switch_cmp,
1304 };
1305 struct sort_dimension *available_sorts[] = {
1306 &pid_sort_dimension,
1307 &avg_sort_dimension,
1308 &max_sort_dimension,
1309 &switch_sort_dimension,
1310 &runtime_sort_dimension,
1311 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001312
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001313 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001314 if (!strcmp(available_sorts[i]->name, tok)) {
1315 list_add_tail(&available_sorts[i]->list, list);
1316
1317 return 0;
1318 }
1319 }
1320
1321 return -1;
1322}
1323
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001324static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001325{
1326 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001327 struct rb_root *root = &sched->atom_root;
1328again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001329 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001330 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001331 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001332 if (!node)
1333 break;
1334
Josef Bacik2f80dd42015-05-22 09:18:40 -04001335 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001336 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001337 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001338 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001339 if (root == &sched->atom_root) {
1340 root = &sched->merged_atom_root;
1341 goto again;
1342 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001343}
1344
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001345static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001346 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001347 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001348 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001349{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001350 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001351
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001352 if (sched->tp_handler->wakeup_event)
1353 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001354
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001355 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001356}
1357
Jiri Olsaa151a372016-04-12 15:29:29 +02001358union map_priv {
1359 void *ptr;
1360 bool color;
1361};
1362
1363static bool thread__has_color(struct thread *thread)
1364{
1365 union map_priv priv = {
1366 .ptr = thread__priv(thread),
1367 };
1368
1369 return priv.color;
1370}
1371
1372static struct thread*
1373map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1374{
1375 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1376 union map_priv priv = {
1377 .color = false,
1378 };
1379
1380 if (!sched->map.color_pids || !thread || thread__priv(thread))
1381 return thread;
1382
1383 if (thread_map__has(sched->map.color_pids, tid))
1384 priv.color = true;
1385
1386 thread__set_priv(thread, priv.ptr);
1387 return thread;
1388}
1389
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001390static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1391 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001392{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001393 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1394 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001395 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001396 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001397 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001398 int i, this_cpu = sample->cpu;
1399 int cpus_nr;
1400 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001401 const char *color = PERF_COLOR_NORMAL;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001402
1403 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1404
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001405 if (this_cpu > sched->max_cpu)
1406 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001407
Jiri Olsa99623c62016-04-12 15:29:26 +02001408 if (sched->map.comp) {
1409 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1410 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1411 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1412 new_cpu = true;
1413 }
1414 } else
1415 cpus_nr = sched->max_cpu;
1416
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001417 timestamp0 = sched->cpu_last_switched[this_cpu];
1418 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001419 if (timestamp0)
1420 delta = timestamp - timestamp0;
1421 else
1422 delta = 0;
1423
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001424 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001425 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001426 return -1;
1427 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001428
Jiri Olsaa151a372016-04-12 15:29:29 +02001429 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001430 if (sched_in == NULL)
1431 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001433 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001434
1435 printf(" ");
1436
1437 new_shortname = 0;
1438 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001439 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1440 /*
1441 * Don't allocate a letter-number for swapper:0
1442 * as a shortname. Instead, we use '.' for it.
1443 */
1444 sched_in->shortname[0] = '.';
1445 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001446 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001447 sched_in->shortname[0] = sched->next_shortname1;
1448 sched_in->shortname[1] = sched->next_shortname2;
1449
1450 if (sched->next_shortname1 < 'Z') {
1451 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001452 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001453 sched->next_shortname1 = 'A';
1454 if (sched->next_shortname2 < '9')
1455 sched->next_shortname2++;
1456 else
1457 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001458 }
1459 }
1460 new_shortname = 1;
1461 }
1462
Jiri Olsa99623c62016-04-12 15:29:26 +02001463 for (i = 0; i < cpus_nr; i++) {
1464 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001465 struct thread *curr_thread = sched->curr_thread[cpu];
1466 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001467 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001468
1469 if (curr_thread && thread__has_color(curr_thread))
1470 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001471
Jiri Olsacf294f22016-04-12 15:29:30 +02001472 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1473 cpu_color = COLOR_CPUS;
1474
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001475 if (cpu != this_cpu)
Jiri Olsacf294f22016-04-12 15:29:30 +02001476 color_fprintf(stdout, cpu_color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001477 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001478 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001479
Dongsheng6bcab4e2014-05-06 14:39:01 +09001480 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001481 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001482 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001483 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001484 }
1485
Jiri Olsa8cd91192016-04-12 15:29:27 +02001486 color_fprintf(stdout, color, " %12.6f secs ", (double)timestamp/1e9);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001487 if (new_shortname) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001488 const char *pid_color = color;
1489
1490 if (thread__has_color(sched_in))
1491 pid_color = COLOR_PIDS;
1492
1493 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001494 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001495 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001496
Jiri Olsa99623c62016-04-12 15:29:26 +02001497 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001498 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001499
Jiri Olsa8cd91192016-04-12 15:29:27 +02001500 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001501
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001502 thread__put(sched_in);
1503
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001504 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001505}
1506
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001507static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001508 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001509 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001510 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001511{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001512 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001513 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001514 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1515 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001516
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001517 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001518 /*
1519 * Are we trying to switch away a PID that is
1520 * not current?
1521 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001522 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001523 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001524 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001525
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001526 if (sched->tp_handler->switch_event)
1527 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001528
1529 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001530 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001531}
1532
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001533static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001534 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001535 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001536 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001537{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001538 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001539
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001540 if (sched->tp_handler->runtime_event)
1541 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001542
1543 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001544}
1545
David Aherncb627502013-08-07 22:50:47 -04001546static int perf_sched__process_fork_event(struct perf_tool *tool,
1547 union perf_event *event,
1548 struct perf_sample *sample,
1549 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001550{
1551 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1552
David Aherncb627502013-08-07 22:50:47 -04001553 /* run the fork event through the perf machineruy */
1554 perf_event__process_fork(tool, event, sample, machine);
1555
1556 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001557 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001558 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001559
1560 return 0;
1561}
1562
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001563static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001564 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001565 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001566 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001567{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001568 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001569
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001570 if (sched->tp_handler->migrate_task_event)
1571 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001572
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001573 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001574}
1575
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001576typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001577 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001578 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001579 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001580
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001581static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1582 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001583 struct perf_sample *sample,
1584 struct perf_evsel *evsel,
1585 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001586{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001587 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001588
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001589 if (evsel->handler != NULL) {
1590 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001591 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001592 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001593
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001594 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001595}
1596
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001597static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001598{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001599 const struct perf_evsel_str_handler handlers[] = {
1600 { "sched:sched_switch", process_sched_switch_event, },
1601 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1602 { "sched:sched_wakeup", process_sched_wakeup_event, },
1603 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001604 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1605 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001606 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001607 struct perf_data_file file = {
1608 .path = input_name,
1609 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001610 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001611 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001612 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001613
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001614 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001615 if (session == NULL) {
1616 pr_debug("No Memory for session\n");
1617 return -1;
1618 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001619
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001620 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001621
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001622 if (perf_session__set_tracepoints_handlers(session, handlers))
1623 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001624
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001625 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001626 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001627 if (err) {
1628 pr_err("Failed to process events, error %d", err);
1629 goto out_delete;
1630 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001631
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001632 sched->nr_events = session->evlist->stats.nr_events[0];
1633 sched->nr_lost_events = session->evlist->stats.total_lost;
1634 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001635 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001636
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001637 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001638out_delete:
1639 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001640 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001641}
1642
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001643static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001644{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001645 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001646 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001647 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
1648 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001649 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001650 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001651 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001652 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
1653 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001654 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001655 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001656 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001657 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
1658 sched->nr_context_switch_bugs, sched->nr_timestamps);
1659 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001660 printf(" (due to lost events?)");
1661 printf("\n");
1662 }
1663}
1664
Josef Bacik2f80dd42015-05-22 09:18:40 -04001665static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
1666{
1667 struct rb_node **new = &(root->rb_node), *parent = NULL;
1668 struct work_atoms *this;
1669 const char *comm = thread__comm_str(data->thread), *this_comm;
1670
1671 while (*new) {
1672 int cmp;
1673
1674 this = container_of(*new, struct work_atoms, node);
1675 parent = *new;
1676
1677 this_comm = thread__comm_str(this->thread);
1678 cmp = strcmp(comm, this_comm);
1679 if (cmp > 0) {
1680 new = &((*new)->rb_left);
1681 } else if (cmp < 0) {
1682 new = &((*new)->rb_right);
1683 } else {
1684 this->num_merged++;
1685 this->total_runtime += data->total_runtime;
1686 this->nb_atoms += data->nb_atoms;
1687 this->total_lat += data->total_lat;
1688 list_splice(&data->work_list, &this->work_list);
1689 if (this->max_lat < data->max_lat) {
1690 this->max_lat = data->max_lat;
1691 this->max_lat_at = data->max_lat_at;
1692 }
1693 zfree(&data);
1694 return;
1695 }
1696 }
1697
1698 data->num_merged++;
1699 rb_link_node(&data->node, parent, new);
1700 rb_insert_color(&data->node, root);
1701}
1702
1703static void perf_sched__merge_lat(struct perf_sched *sched)
1704{
1705 struct work_atoms *data;
1706 struct rb_node *node;
1707
1708 if (sched->skip_merge)
1709 return;
1710
1711 while ((node = rb_first(&sched->atom_root))) {
1712 rb_erase(node, &sched->atom_root);
1713 data = rb_entry(node, struct work_atoms, node);
1714 __merge_work_atoms(&sched->merged_atom_root, data);
1715 }
1716}
1717
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001718static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001719{
1720 struct rb_node *next;
1721
1722 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04001723
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001724 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001725 return -1;
David Ahernad9def72013-08-07 22:50:44 -04001726
Josef Bacik2f80dd42015-05-22 09:18:40 -04001727 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001728 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001729
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001730 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1731 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1732 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001733
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001734 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001735
1736 while (next) {
1737 struct work_atoms *work_list;
1738
1739 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001740 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001741 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001742 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001743 }
1744
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04001745 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001746 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001747 (double)sched->all_runtime / 1e6, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001748
1749 printf(" ---------------------------------------------------\n");
1750
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001751 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001752 printf("\n");
1753
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001754 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001755}
1756
Jiri Olsa99623c62016-04-12 15:29:26 +02001757static int setup_map_cpus(struct perf_sched *sched)
1758{
1759 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1760
1761 if (sched->map.comp) {
1762 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02001763 if (!sched->map.comp_cpus)
1764 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02001765 }
1766
1767 return 0;
1768}
1769
Jiri Olsaa151a372016-04-12 15:29:29 +02001770static int setup_color_pids(struct perf_sched *sched)
1771{
1772 struct thread_map *map;
1773
1774 if (!sched->map.color_pids_str)
1775 return 0;
1776
1777 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
1778 if (!map) {
1779 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
1780 return -1;
1781 }
1782
1783 sched->map.color_pids = map;
1784 return 0;
1785}
1786
Jiri Olsacf294f22016-04-12 15:29:30 +02001787static int setup_color_cpus(struct perf_sched *sched)
1788{
1789 struct cpu_map *map;
1790
1791 if (!sched->map.color_cpus_str)
1792 return 0;
1793
1794 map = cpu_map__new(sched->map.color_cpus_str);
1795 if (!map) {
1796 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
1797 return -1;
1798 }
1799
1800 sched->map.color_cpus = map;
1801 return 0;
1802}
1803
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001804static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001805{
Jiri Olsa99623c62016-04-12 15:29:26 +02001806 if (setup_map_cpus(sched))
1807 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02001808
Jiri Olsaa151a372016-04-12 15:29:29 +02001809 if (setup_color_pids(sched))
1810 return -1;
1811
Jiri Olsacf294f22016-04-12 15:29:30 +02001812 if (setup_color_cpus(sched))
1813 return -1;
1814
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001815 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001816 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001817 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001818 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001819 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001820}
1821
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001822static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001823{
1824 unsigned long i;
1825
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001826 calibrate_run_measurement_overhead(sched);
1827 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001828
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001829 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001830
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001831 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001832 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001833
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001834 printf("nr_run_events: %ld\n", sched->nr_run_events);
1835 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
1836 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001837
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001838 if (sched->targetless_wakeups)
1839 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
1840 if (sched->multitarget_wakeups)
1841 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
1842 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001843 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001844 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001845
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001846 print_task_traces(sched);
1847 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001848
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001849 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001850 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001851 for (i = 0; i < sched->replay_repeat; i++)
1852 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001853
1854 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001855}
1856
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001857static void setup_sorting(struct perf_sched *sched, const struct option *options,
1858 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001859{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001860 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001861
1862 for (tok = strtok_r(str, ", ", &tmp);
1863 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001864 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001865 usage_with_options_msg(usage_msg, options,
1866 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001867 }
1868 }
1869
1870 free(str);
1871
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001872 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001873}
1874
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001875static int __cmd_record(int argc, const char **argv)
1876{
1877 unsigned int rec_argc, i, j;
1878 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001879 const char * const record_args[] = {
1880 "record",
1881 "-a",
1882 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001883 "-m", "1024",
1884 "-c", "1",
1885 "-e", "sched:sched_switch",
1886 "-e", "sched:sched_stat_wait",
1887 "-e", "sched:sched_stat_sleep",
1888 "-e", "sched:sched_stat_iowait",
1889 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001890 "-e", "sched:sched_process_fork",
1891 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09001892 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001893 "-e", "sched:sched_migrate_task",
1894 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001895
1896 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1897 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1898
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02001899 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11001900 return -ENOMEM;
1901
Ingo Molnar1fc35b22009-09-13 09:44:29 +02001902 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1903 rec_argv[i] = strdup(record_args[i]);
1904
1905 for (j = 1; j < (unsigned int)argc; j++, i++)
1906 rec_argv[i] = argv[j];
1907
1908 BUG_ON(i != rec_argc);
1909
1910 return cmd_record(i, rec_argv, NULL);
1911}
1912
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001913int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001914{
Adrian Hunter8a39df82013-10-22 10:34:15 +03001915 const char default_sort_order[] = "avg, max, switch, runtime";
1916 struct perf_sched sched = {
1917 .tool = {
1918 .sample = perf_sched__process_tracepoint_sample,
1919 .comm = perf_event__process_comm,
1920 .lost = perf_event__process_lost,
1921 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001922 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001923 },
1924 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
1925 .sort_list = LIST_HEAD_INIT(sched.sort_list),
1926 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
1927 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001928 .sort_order = default_sort_order,
1929 .replay_repeat = 10,
1930 .profile_cpu = -1,
1931 .next_shortname1 = 'A',
1932 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04001933 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03001934 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001935 const struct option latency_options[] = {
1936 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
1937 "sort by key(s): runtime, switch, avg, max"),
1938 OPT_INCR('v', "verbose", &verbose,
1939 "be more verbose (show symbol address, etc)"),
1940 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
1941 "CPU to profile on"),
1942 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1943 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04001944 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
1945 "latency stats per pid instead of per comm"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001946 OPT_END()
1947 };
1948 const struct option replay_options[] = {
1949 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
1950 "repeat the workload replay N times (-1: infinite)"),
1951 OPT_INCR('v', "verbose", &verbose,
1952 "be more verbose (show symbol address, etc)"),
1953 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1954 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08001955 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001956 OPT_END()
1957 };
1958 const struct option sched_options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +08001959 OPT_STRING('i', "input", &input_name, "file",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001960 "input file name"),
1961 OPT_INCR('v', "verbose", &verbose,
1962 "be more verbose (show symbol address, etc)"),
1963 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1964 "dump raw trace in ASCII"),
1965 OPT_END()
1966 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001967 const struct option map_options[] = {
1968 OPT_BOOLEAN(0, "compact", &sched.map.comp,
1969 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02001970 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
1971 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02001972 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
1973 "highlight given CPUs in map"),
Jiri Olsa99623c62016-04-12 15:29:26 +02001974 OPT_END()
1975 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001976 const char * const latency_usage[] = {
1977 "perf sched latency [<options>]",
1978 NULL
1979 };
1980 const char * const replay_usage[] = {
1981 "perf sched replay [<options>]",
1982 NULL
1983 };
Jiri Olsa99623c62016-04-12 15:29:26 +02001984 const char * const map_usage[] = {
1985 "perf sched map [<options>]",
1986 NULL
1987 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04001988 const char *const sched_subcommands[] = { "record", "latency", "map",
1989 "replay", "script", NULL };
1990 const char *sched_usage[] = {
1991 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001992 NULL
1993 };
1994 struct trace_sched_handler lat_ops = {
1995 .wakeup_event = latency_wakeup_event,
1996 .switch_event = latency_switch_event,
1997 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001998 .migrate_task_event = latency_migrate_task_event,
1999 };
2000 struct trace_sched_handler map_ops = {
2001 .switch_event = map_switch_event,
2002 };
2003 struct trace_sched_handler replay_ops = {
2004 .wakeup_event = replay_wakeup_event,
2005 .switch_event = replay_switch_event,
2006 .fork_event = replay_fork_event,
2007 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002008 unsigned int i;
2009
2010 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2011 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002012
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002013 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2014 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002015 if (!argc)
2016 usage_with_options(sched_usage, sched_options);
2017
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002018 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002019 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002020 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002021 if (!strcmp(argv[0], "script"))
2022 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002023
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002024 if (!strncmp(argv[0], "rec", 3)) {
2025 return __cmd_record(argc, argv);
2026 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002027 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002028 if (argc > 1) {
2029 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2030 if (argc)
2031 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002032 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002033 setup_sorting(&sched, latency_options, latency_usage);
2034 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002035 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002036 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002037 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002038 if (argc)
2039 usage_with_options(map_usage, map_options);
2040 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002041 sched.tp_handler = &map_ops;
2042 setup_sorting(&sched, latency_options, latency_usage);
2043 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002044 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002045 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002046 if (argc) {
2047 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2048 if (argc)
2049 usage_with_options(replay_usage, replay_options);
2050 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002051 return perf_sched__replay(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002052 } else {
2053 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002054 }
2055
Ingo Molnarec156762009-09-11 12:12:54 +02002056 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002057}