blob: 1f8731640809aee29e201cd7a913832ecb3612c0 [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"
David Ahern49394a22016-11-16 15:06:29 +090016#include "util/stat.h"
David Ahern6c973c92016-11-16 15:06:32 +090017#include "util/callchain.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020018
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020020#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021
Ingo Molnar0a02ad92009-09-11 12:12:54 +020022#include "util/debug.h"
23
David Ahern49394a22016-11-16 15:06:29 +090024#include <linux/log2.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020025#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020026#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020027
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020028#include <semaphore.h>
29#include <pthread.h>
30#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080031#include <api/fs/fs.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030032#include <linux/time64.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020033
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020034#define PR_SET_NAME 15 /* Set process name */
35#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020036#define COMM_LEN 20
37#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080038#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020039
mingo39aeb522009-09-14 20:04:48 +020040struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020041
42struct task_desc {
43 unsigned long nr;
44 unsigned long pid;
45 char comm[COMM_LEN];
46
47 unsigned long nr_events;
48 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020049 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020050
51 pthread_t thread;
52 sem_t sleep_sem;
53
54 sem_t ready_for_work;
55 sem_t work_done_sem;
56
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020057 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020058};
59
60enum sched_event_type {
61 SCHED_EVENT_RUN,
62 SCHED_EVENT_SLEEP,
63 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020064 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020065};
66
mingo39aeb522009-09-14 20:04:48 +020067struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020068 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030069 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020070 u64 timestamp;
71 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020072 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020073 sem_t *wait_sem;
74 struct task_desc *wakee;
75};
76
Dongshenge936e8e2014-05-05 16:05:54 +090077#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020078
79enum thread_state {
80 THREAD_SLEEPING = 0,
81 THREAD_WAIT_CPU,
82 THREAD_SCHED_IN,
83 THREAD_IGNORE
84};
85
86struct work_atom {
87 struct list_head list;
88 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020089 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020090 u64 wake_up_time;
91 u64 sched_in_time;
92 u64 runtime;
93};
94
mingo39aeb522009-09-14 20:04:48 +020095struct work_atoms {
96 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097 struct thread *thread;
98 struct rb_node node;
99 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100100 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200101 u64 total_lat;
102 u64 nb_atoms;
103 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400104 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200105};
106
mingo39aeb522009-09-14 20:04:48 +0200107typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200108
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300109struct perf_sched;
110
111struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300112 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
113 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300114
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300115 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
116 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300117
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300118 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
119 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300120
David Aherncb627502013-08-07 22:50:47 -0400121 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
122 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
123 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300124
125 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300126 struct perf_evsel *evsel,
127 struct perf_sample *sample,
128 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300129};
130
Jiri Olsaa151a372016-04-12 15:29:29 +0200131#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200132#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200133
Jiri Olsa99623c62016-04-12 15:29:26 +0200134struct perf_sched_map {
135 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
136 int *comp_cpus;
137 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200138 struct thread_map *color_pids;
139 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200140 struct cpu_map *color_cpus;
141 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200142 struct cpu_map *cpus;
143 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200144};
145
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300146struct perf_sched {
147 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300148 const char *sort_order;
149 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800150 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300151 struct task_desc **tasks;
152 const struct trace_sched_handler *tp_handler;
153 pthread_mutex_t start_work_mutex;
154 pthread_mutex_t work_done_wait_mutex;
155 int profile_cpu;
156/*
157 * Track the current task - that way we can know whether there's any
158 * weird events, such as a task being switched away that is not current.
159 */
160 int max_cpu;
161 u32 curr_pid[MAX_CPUS];
162 struct thread *curr_thread[MAX_CPUS];
163 char next_shortname1;
164 char next_shortname2;
165 unsigned int replay_repeat;
166 unsigned long nr_run_events;
167 unsigned long nr_sleep_events;
168 unsigned long nr_wakeup_events;
169 unsigned long nr_sleep_corrections;
170 unsigned long nr_run_events_optimized;
171 unsigned long targetless_wakeups;
172 unsigned long multitarget_wakeups;
173 unsigned long nr_runs;
174 unsigned long nr_timestamps;
175 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300176 unsigned long nr_context_switch_bugs;
177 unsigned long nr_events;
178 unsigned long nr_lost_chunks;
179 unsigned long nr_lost_events;
180 u64 run_measurement_overhead;
181 u64 sleep_measurement_overhead;
182 u64 start_time;
183 u64 cpu_usage;
184 u64 runavg_cpu_usage;
185 u64 parent_cpu_usage;
186 u64 runavg_parent_cpu_usage;
187 u64 sum_runtime;
188 u64 sum_fluct;
189 u64 run_avg;
190 u64 all_runtime;
191 u64 all_count;
192 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400193 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300194 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800195 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400196 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200197 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900198
199 /* options for timehist command */
200 bool summary;
201 bool summary_only;
David Ahern6c973c92016-11-16 15:06:32 +0900202 bool show_callchain;
203 unsigned int max_stack;
David Ahernfc1469f2016-11-16 15:06:31 +0900204 bool show_wakeups;
David Ahern52df1382016-11-16 15:06:30 +0900205 u64 skipped_samples;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300206};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200207
David Ahern49394a22016-11-16 15:06:29 +0900208/* per thread run time data */
209struct thread_runtime {
210 u64 last_time; /* time of previous sched in/out event */
211 u64 dt_run; /* run time */
212 u64 dt_wait; /* time between CPU access (off cpu) */
213 u64 dt_delay; /* time between wakeup and sched-in */
214 u64 ready_to_run; /* time of wakeup */
215
216 struct stats run_stats;
217 u64 total_run_time;
218};
219
220/* per event run time data */
221struct evsel_runtime {
222 u64 *last_time; /* time this event was last seen per cpu */
223 u32 ncpu; /* highest cpu slot allocated */
224};
225
226/* track idle times per cpu */
227static struct thread **idle_threads;
228static int idle_max_cpu;
229static char idle_comm[] = "<idle>";
230
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200231static u64 get_nsecs(void)
232{
233 struct timespec ts;
234
235 clock_gettime(CLOCK_MONOTONIC, &ts);
236
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300237 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200238}
239
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300240static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200241{
242 u64 T0 = get_nsecs(), T1;
243
244 do {
245 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300246 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200247}
248
249static void sleep_nsecs(u64 nsecs)
250{
251 struct timespec ts;
252
253 ts.tv_nsec = nsecs % 999999999;
254 ts.tv_sec = nsecs / 999999999;
255
256 nanosleep(&ts, NULL);
257}
258
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300259static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200260{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300261 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200262 int i;
263
264 for (i = 0; i < 10; i++) {
265 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300266 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200267 T1 = get_nsecs();
268 delta = T1-T0;
269 min_delta = min(min_delta, delta);
270 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300271 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200272
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200273 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200274}
275
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300276static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200277{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300278 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200279 int i;
280
281 for (i = 0; i < 10; i++) {
282 T0 = get_nsecs();
283 sleep_nsecs(10000);
284 T1 = get_nsecs();
285 delta = T1-T0;
286 min_delta = min(min_delta, delta);
287 }
288 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300289 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200290
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200291 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200292}
293
mingo39aeb522009-09-14 20:04:48 +0200294static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200295get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200296{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200297 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200298 unsigned long idx = task->nr_events;
299 size_t size;
300
301 event->timestamp = timestamp;
302 event->nr = idx;
303
304 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200305 size = sizeof(struct sched_atom *) * task->nr_events;
306 task->atoms = realloc(task->atoms, size);
307 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200308
mingo39aeb522009-09-14 20:04:48 +0200309 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200310
311 return event;
312}
313
mingo39aeb522009-09-14 20:04:48 +0200314static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200315{
316 if (!task->nr_events)
317 return NULL;
318
mingo39aeb522009-09-14 20:04:48 +0200319 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200320}
321
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300322static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
323 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200324{
mingo39aeb522009-09-14 20:04:48 +0200325 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200326
327 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200328 * optimize an existing RUN event by merging this one
329 * to it:
330 */
Ingo Molnarec156762009-09-11 12:12:54 +0200331 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300332 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200333 curr_event->duration += duration;
334 return;
335 }
336
337 event = get_new_event(task, timestamp);
338
339 event->type = SCHED_EVENT_RUN;
340 event->duration = duration;
341
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300342 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200343}
344
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300345static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
346 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200347{
mingo39aeb522009-09-14 20:04:48 +0200348 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200349
350 event = get_new_event(task, timestamp);
351 event->type = SCHED_EVENT_WAKEUP;
352 event->wakee = wakee;
353
354 wakee_event = last_event(wakee);
355 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300356 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200357 return;
358 }
359 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300360 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200361 return;
362 }
363
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200364 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200365 sem_init(wakee_event->wait_sem, 0, 0);
366 wakee_event->specific_wait = 1;
367 event->wait_sem = wakee_event->wait_sem;
368
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300369 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200370}
371
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300372static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
373 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200374{
mingo39aeb522009-09-14 20:04:48 +0200375 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200376
377 event->type = SCHED_EVENT_SLEEP;
378
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300379 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200380}
381
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300382static struct task_desc *register_pid(struct perf_sched *sched,
383 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200384{
385 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800386 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200387
Yunlong Songcb06ac22015-03-31 21:46:30 +0800388 if (sched->pid_to_task == NULL) {
389 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
390 pid_max = MAX_PID;
391 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
392 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800393 if (pid >= (unsigned long)pid_max) {
394 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
395 sizeof(struct task_desc *))) == NULL);
396 while (pid >= (unsigned long)pid_max)
397 sched->pid_to_task[pid_max++] = NULL;
398 }
Ingo Molnarec156762009-09-11 12:12:54 +0200399
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300400 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200401
402 if (task)
403 return task;
404
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200405 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200406 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300407 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200408 strcpy(task->comm, comm);
409 /*
410 * every task starts in sleeping state - this gets ignored
411 * if there's no wakeup pointing to this sleep state:
412 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300413 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200414
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415 sched->pid_to_task[pid] = task;
416 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800417 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300418 BUG_ON(!sched->tasks);
419 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200420
Ingo Molnarad236fd2009-09-11 12:12:54 +0200421 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300422 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200423
424 return task;
425}
426
427
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300428static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200429{
430 struct task_desc *task;
431 unsigned long i;
432
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300433 for (i = 0; i < sched->nr_tasks; i++) {
434 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200435 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200436 task->nr, task->comm, task->pid, task->nr_events);
437 }
438}
439
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300440static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200441{
442 struct task_desc *task1, *task2;
443 unsigned long i, j;
444
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300445 for (i = 0; i < sched->nr_tasks; i++) {
446 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200447 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300448 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200449 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300450 task2 = sched->tasks[j];
451 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200452 }
453}
454
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300455static void perf_sched__process_event(struct perf_sched *sched,
456 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200457{
458 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200459
mingo39aeb522009-09-14 20:04:48 +0200460 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200461 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300462 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200463 break;
464 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200465 if (atom->wait_sem)
466 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200467 BUG_ON(ret);
468 break;
469 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200470 if (atom->wait_sem)
471 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200472 BUG_ON(ret);
473 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200474 case SCHED_EVENT_MIGRATION:
475 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200476 default:
477 BUG_ON(1);
478 }
479}
480
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200481static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200482{
483 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200484 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200485 int err;
486
487 err = getrusage(RUSAGE_SELF, &ru);
488 BUG_ON(err);
489
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300490 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
491 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200492
493 return sum;
494}
495
Yunlong Song939cda52015-03-31 21:46:34 +0800496static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200497{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800498 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800499 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800500 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800501 struct rlimit limit;
502 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800503
504 memset(&attr, 0, sizeof(attr));
505
506 attr.type = PERF_TYPE_SOFTWARE;
507 attr.config = PERF_COUNT_SW_TASK_CLOCK;
508
Yunlong Song939cda52015-03-31 21:46:34 +0800509force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200510 fd = sys_perf_event_open(&attr, 0, -1, -1,
511 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800512
Yunlong Song1aff59b2015-03-31 21:46:33 +0800513 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800514 if (errno == EMFILE) {
515 if (sched->force) {
516 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
517 limit.rlim_cur += sched->nr_tasks - cur_task;
518 if (limit.rlim_cur > limit.rlim_max) {
519 limit.rlim_max = limit.rlim_cur;
520 need_privilege = true;
521 }
522 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
523 if (need_privilege && errno == EPERM)
524 strcpy(info, "Need privilege\n");
525 } else
526 goto force_again;
527 } else
528 strcpy(info, "Have a try with -f option\n");
529 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900530 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800531 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300532 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800533 exit(EXIT_FAILURE);
534 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800535 return fd;
536}
537
538static u64 get_cpu_usage_nsec_self(int fd)
539{
540 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200541 int ret;
542
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800543 ret = read(fd, &runtime, sizeof(runtime));
544 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200545
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800546 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200547}
548
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300549struct sched_thread_parms {
550 struct task_desc *task;
551 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800552 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553};
554
Ingo Molnarec156762009-09-11 12:12:54 +0200555static void *thread_func(void *ctx)
556{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300557 struct sched_thread_parms *parms = ctx;
558 struct task_desc *this_task = parms->task;
559 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200560 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200561 unsigned long i, ret;
562 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800563 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200564
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300565 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300566
Ingo Molnarec156762009-09-11 12:12:54 +0200567 sprintf(comm2, ":%s", this_task->comm);
568 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300569 if (fd < 0)
570 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200571again:
572 ret = sem_post(&this_task->ready_for_work);
573 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300574 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200575 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300576 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200577 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200578
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800579 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200580
581 for (i = 0; i < this_task->nr_events; i++) {
582 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300583 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200584 }
585
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800586 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200587 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200588 ret = sem_post(&this_task->work_done_sem);
589 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200590
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300591 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200592 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300593 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200594 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200595
596 goto again;
597}
598
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300599static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200600{
601 struct task_desc *task;
602 pthread_attr_t attr;
603 unsigned long i;
604 int err;
605
606 err = pthread_attr_init(&attr);
607 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200608 err = pthread_attr_setstacksize(&attr,
609 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200610 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200612 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200614 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300615 for (i = 0; i < sched->nr_tasks; i++) {
616 struct sched_thread_parms *parms = malloc(sizeof(*parms));
617 BUG_ON(parms == NULL);
618 parms->task = task = sched->tasks[i];
619 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800620 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200621 sem_init(&task->sleep_sem, 0, 0);
622 sem_init(&task->ready_for_work, 0, 0);
623 sem_init(&task->work_done_sem, 0, 0);
624 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300625 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200626 BUG_ON(err);
627 }
628}
629
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300630static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200631{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200632 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200633 struct task_desc *task;
634 unsigned long i, ret;
635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300636 sched->start_time = get_nsecs();
637 sched->cpu_usage = 0;
638 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200639
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300640 for (i = 0; i < sched->nr_tasks; i++) {
641 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200642 ret = sem_wait(&task->ready_for_work);
643 BUG_ON(ret);
644 sem_init(&task->ready_for_work, 0, 0);
645 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300646 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200647 BUG_ON(ret);
648
649 cpu_usage_0 = get_cpu_usage_nsec_parent();
650
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300651 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200652
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300653 for (i = 0; i < sched->nr_tasks; i++) {
654 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200655 ret = sem_wait(&task->work_done_sem);
656 BUG_ON(ret);
657 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300658 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200659 task->cpu_usage = 0;
660 }
661
662 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300663 if (!sched->runavg_cpu_usage)
664 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800665 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 +0200666
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300667 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
668 if (!sched->runavg_parent_cpu_usage)
669 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800670 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
671 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200672
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200674 BUG_ON(ret);
675
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300676 for (i = 0; i < sched->nr_tasks; i++) {
677 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200678 sem_init(&task->sleep_sem, 0, 0);
679 task->curr_event = 0;
680 }
681}
682
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300683static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200684{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500685 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200686
687 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300688 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200689 T1 = get_nsecs();
690
691 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300692 sched->sum_runtime += delta;
693 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200694
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300695 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200696 if (delta < avg_delta)
697 fluct = avg_delta - delta;
698 else
699 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300700 sched->sum_fluct += fluct;
701 if (!sched->run_avg)
702 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800703 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200704
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300705 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200706
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300707 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200708
Ingo Molnarad236fd2009-09-11 12:12:54 +0200709 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300710 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200711
712#if 0
713 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200714 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300715 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200716 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200717 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300718 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
719 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200720#endif
721
Ingo Molnarad236fd2009-09-11 12:12:54 +0200722 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200723
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300724 if (sched->nr_sleep_corrections)
725 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
726 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200727}
728
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300729static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200730{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200731 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200732
733 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300734 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200735 T1 = get_nsecs();
736
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200737 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200738
739 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300740 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200741 T1 = get_nsecs();
742
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200743 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200744}
745
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300746static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300747replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300748 struct perf_evsel *evsel, struct perf_sample *sample,
749 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200750{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300751 const char *comm = perf_evsel__strval(evsel, sample, "comm");
752 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200753 struct task_desc *waker, *wakee;
754
755 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300756 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200757
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300758 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200759 }
760
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300761 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300762 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200763
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300764 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300765 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200766}
767
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300768static int replay_switch_event(struct perf_sched *sched,
769 struct perf_evsel *evsel,
770 struct perf_sample *sample,
771 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200772{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300773 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
774 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
775 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
776 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
777 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300778 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300779 u64 timestamp0, timestamp = sample->time;
780 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200781 s64 delta;
782
Ingo Molnarad236fd2009-09-11 12:12:54 +0200783 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300784 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200785
Ingo Molnarfbf94822009-09-11 12:12:54 +0200786 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300787 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200788
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300789 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200790 if (timestamp0)
791 delta = timestamp - timestamp0;
792 else
793 delta = 0;
794
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300795 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900796 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300797 return -1;
798 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200799
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300800 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
801 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200802
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300803 prev = register_pid(sched, prev_pid, prev_comm);
804 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200805
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300806 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200807
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300808 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300809 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300810
811 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200812}
813
David Aherncb627502013-08-07 22:50:47 -0400814static int replay_fork_event(struct perf_sched *sched,
815 union perf_event *event,
816 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200817{
David Aherncb627502013-08-07 22:50:47 -0400818 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300819
Adrian Hunter314add62013-08-27 11:23:03 +0300820 child = machine__findnew_thread(machine, event->fork.pid,
821 event->fork.tid);
822 parent = machine__findnew_thread(machine, event->fork.ppid,
823 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400824
825 if (child == NULL || parent == NULL) {
826 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
827 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300828 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200829 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300830
David Aherncb627502013-08-07 22:50:47 -0400831 if (verbose) {
832 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200833 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
834 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400835 }
836
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200837 register_pid(sched, parent->tid, thread__comm_str(parent));
838 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300839out_put:
840 thread__put(child);
841 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300842 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200843}
844
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200845struct sort_dimension {
846 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200847 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200848 struct list_head list;
849};
850
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200851static int
mingo39aeb522009-09-14 20:04:48 +0200852thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200853{
854 struct sort_dimension *sort;
855 int ret = 0;
856
Ingo Molnarb5fae122009-09-11 12:12:54 +0200857 BUG_ON(list_empty(list));
858
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200859 list_for_each_entry(sort, list, list) {
860 ret = sort->cmp(l, r);
861 if (ret)
862 return ret;
863 }
864
865 return ret;
866}
867
mingo39aeb522009-09-14 20:04:48 +0200868static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200869thread_atoms_search(struct rb_root *root, struct thread *thread,
870 struct list_head *sort_list)
871{
872 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200873 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200874
875 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200876 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200877 int cmp;
878
mingo39aeb522009-09-14 20:04:48 +0200879 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200880
881 cmp = thread_lat_cmp(sort_list, &key, atoms);
882 if (cmp > 0)
883 node = node->rb_left;
884 else if (cmp < 0)
885 node = node->rb_right;
886 else {
887 BUG_ON(thread != atoms->thread);
888 return atoms;
889 }
890 }
891 return NULL;
892}
893
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200894static void
mingo39aeb522009-09-14 20:04:48 +0200895__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200896 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200897{
898 struct rb_node **new = &(root->rb_node), *parent = NULL;
899
900 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200901 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200902 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200903
mingo39aeb522009-09-14 20:04:48 +0200904 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200905 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200906
907 cmp = thread_lat_cmp(sort_list, data, this);
908
909 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200911 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200912 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200913 }
914
915 rb_link_node(&data->node, parent, new);
916 rb_insert_color(&data->node, root);
917}
918
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300919static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200920{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200921 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300922 if (!atoms) {
923 pr_err("No memory at %s\n", __func__);
924 return -1;
925 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200926
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300927 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200928 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300929 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300930 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200931}
932
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300933static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200934{
935 const char *str = TASK_STATE_TO_CHAR_STR;
936
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300937 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938}
939
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300940static int
mingo39aeb522009-09-14 20:04:48 +0200941add_sched_out_event(struct work_atoms *atoms,
942 char run_state,
943 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200944{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200945 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300946 if (!atom) {
947 pr_err("Non memory at %s", __func__);
948 return -1;
949 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200950
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200951 atom->sched_out_time = timestamp;
952
mingo39aeb522009-09-14 20:04:48 +0200953 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200954 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200955 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200956 }
957
mingo39aeb522009-09-14 20:04:48 +0200958 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300959 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200960}
961
962static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300963add_runtime_event(struct work_atoms *atoms, u64 delta,
964 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200965{
966 struct work_atom *atom;
967
968 BUG_ON(list_empty(&atoms->work_list));
969
970 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
971
972 atom->runtime += delta;
973 atoms->total_runtime += delta;
974}
975
976static void
977add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200979 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200980 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200981
mingo39aeb522009-09-14 20:04:48 +0200982 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200983 return;
984
mingo39aeb522009-09-14 20:04:48 +0200985 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200986
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200987 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988 return;
989
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200990 if (timestamp < atom->wake_up_time) {
991 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200992 return;
993 }
994
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200995 atom->state = THREAD_SCHED_IN;
996 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200997
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200998 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200999 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001000 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001001 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001002 atoms->max_lat_at = timestamp;
1003 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001004 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001005}
1006
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001007static int latency_switch_event(struct perf_sched *sched,
1008 struct perf_evsel *evsel,
1009 struct perf_sample *sample,
1010 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001011{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001012 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1013 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1014 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001015 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001016 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001017 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001018 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001019 s64 delta;
1020
mingo39aeb522009-09-14 20:04:48 +02001021 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001022
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001023 timestamp0 = sched->cpu_last_switched[cpu];
1024 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001025 if (timestamp0)
1026 delta = timestamp - timestamp0;
1027 else
1028 delta = 0;
1029
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001030 if (delta < 0) {
1031 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1032 return -1;
1033 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001034
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001035 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1036 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001037 if (sched_out == NULL || sched_in == NULL)
1038 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001039
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001040 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001041 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001042 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001043 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001044 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001045 if (!out_events) {
1046 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001047 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001048 }
mingo39aeb522009-09-14 20:04:48 +02001049 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001050 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001051 return -1;
mingo39aeb522009-09-14 20:04:48 +02001052
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001053 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001054 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001055 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001056 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001057 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001058 if (!in_events) {
1059 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001060 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001061 }
mingo39aeb522009-09-14 20:04:48 +02001062 /*
1063 * Take came in we have not heard about yet,
1064 * add in an initial atom in runnable state:
1065 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001066 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001067 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001068 }
1069 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001070 err = 0;
1071out_put:
1072 thread__put(sched_out);
1073 thread__put(sched_in);
1074 return err;
mingo39aeb522009-09-14 20:04:48 +02001075}
1076
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001077static int latency_runtime_event(struct perf_sched *sched,
1078 struct perf_evsel *evsel,
1079 struct perf_sample *sample,
1080 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001081{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001082 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1083 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001084 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001085 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001086 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001087 int cpu = sample->cpu, err = -1;
1088
1089 if (thread == NULL)
1090 return -1;
mingo39aeb522009-09-14 20:04:48 +02001091
1092 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001093 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001094 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001095 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001096 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001097 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001098 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001099 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001100 }
1101 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001102 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001103 }
1104
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001105 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001106 err = 0;
1107out_put:
1108 thread__put(thread);
1109 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001110}
1111
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001112static int latency_wakeup_event(struct perf_sched *sched,
1113 struct perf_evsel *evsel,
1114 struct perf_sample *sample,
1115 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001116{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001117 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001118 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001119 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001121 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001122 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001123
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001124 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001125 if (wakee == NULL)
1126 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001127 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001128 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001129 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001130 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001131 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001132 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001133 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001134 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001135 }
1136 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001137 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001138 }
1139
mingo39aeb522009-09-14 20:04:48 +02001140 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001141
mingo39aeb522009-09-14 20:04:48 +02001142 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001143
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001144 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001145 * As we do not guarantee the wakeup event happens when
1146 * task is out of run queue, also may happen when task is
1147 * on run queue and wakeup only change ->state to TASK_RUNNING,
1148 * then we should not set the ->wake_up_time when wake up a
1149 * task which is on run queue.
1150 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001151 * You WILL be missing events if you've recorded only
1152 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001153 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001154 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001155 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001156 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001157
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001158 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001159 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001160 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001161 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001162 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001163
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001164 atom->state = THREAD_WAIT_CPU;
1165 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001166out_ok:
1167 err = 0;
1168out_put:
1169 thread__put(wakee);
1170 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001171}
1172
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001173static int latency_migrate_task_event(struct perf_sched *sched,
1174 struct perf_evsel *evsel,
1175 struct perf_sample *sample,
1176 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001177{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001178 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001179 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001180 struct work_atoms *atoms;
1181 struct work_atom *atom;
1182 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001183 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001184
1185 /*
1186 * Only need to worry about migration when profiling one CPU.
1187 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001188 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001189 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001190
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001191 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001192 if (migrant == NULL)
1193 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001194 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001195 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001196 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001197 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001198 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001199 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001200 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001201 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001202 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001203 }
1204 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001205 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001206 }
1207
1208 BUG_ON(list_empty(&atoms->work_list));
1209
1210 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1211 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1212
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001213 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001214
1215 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001216 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001217 err = 0;
1218out_put:
1219 thread__put(migrant);
1220 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001221}
1222
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001223static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001224{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001225 int i;
1226 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001227 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001228 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001229
mingo39aeb522009-09-14 20:04:48 +02001230 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001231 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001232 /*
1233 * Ignore idle threads:
1234 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001235 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001236 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001237
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001238 sched->all_runtime += work_list->total_runtime;
1239 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001240
Josef Bacik2f80dd42015-05-22 09:18:40 -04001241 if (work_list->num_merged > 1)
1242 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1243 else
1244 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001245
mingo08f69e62009-09-14 18:30:44 +02001246 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001247 printf(" ");
1248
mingo39aeb522009-09-14 20:04:48 +02001249 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001250 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001251
Namhyung Kim99620a52016-10-24 11:02:45 +09001252 printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03001253 (double)work_list->total_runtime / NSEC_PER_MSEC,
1254 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1255 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001256 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001257}
1258
mingo39aeb522009-09-14 20:04:48 +02001259static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001260{
Jiri Olsa0014de12015-11-02 12:10:25 +01001261 if (l->thread == r->thread)
1262 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001263 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001264 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001265 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001266 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001267 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001268}
1269
mingo39aeb522009-09-14 20:04:48 +02001270static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001271{
1272 u64 avgl, avgr;
1273
1274 if (!l->nb_atoms)
1275 return -1;
1276
1277 if (!r->nb_atoms)
1278 return 1;
1279
1280 avgl = l->total_lat / l->nb_atoms;
1281 avgr = r->total_lat / r->nb_atoms;
1282
1283 if (avgl < avgr)
1284 return -1;
1285 if (avgl > avgr)
1286 return 1;
1287
1288 return 0;
1289}
1290
mingo39aeb522009-09-14 20:04:48 +02001291static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001292{
1293 if (l->max_lat < r->max_lat)
1294 return -1;
1295 if (l->max_lat > r->max_lat)
1296 return 1;
1297
1298 return 0;
1299}
1300
mingo39aeb522009-09-14 20:04:48 +02001301static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001302{
1303 if (l->nb_atoms < r->nb_atoms)
1304 return -1;
1305 if (l->nb_atoms > r->nb_atoms)
1306 return 1;
1307
1308 return 0;
1309}
1310
mingo39aeb522009-09-14 20:04:48 +02001311static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001312{
1313 if (l->total_runtime < r->total_runtime)
1314 return -1;
1315 if (l->total_runtime > r->total_runtime)
1316 return 1;
1317
1318 return 0;
1319}
1320
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001321static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001322{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001323 size_t i;
1324 static struct sort_dimension avg_sort_dimension = {
1325 .name = "avg",
1326 .cmp = avg_cmp,
1327 };
1328 static struct sort_dimension max_sort_dimension = {
1329 .name = "max",
1330 .cmp = max_cmp,
1331 };
1332 static struct sort_dimension pid_sort_dimension = {
1333 .name = "pid",
1334 .cmp = pid_cmp,
1335 };
1336 static struct sort_dimension runtime_sort_dimension = {
1337 .name = "runtime",
1338 .cmp = runtime_cmp,
1339 };
1340 static struct sort_dimension switch_sort_dimension = {
1341 .name = "switch",
1342 .cmp = switch_cmp,
1343 };
1344 struct sort_dimension *available_sorts[] = {
1345 &pid_sort_dimension,
1346 &avg_sort_dimension,
1347 &max_sort_dimension,
1348 &switch_sort_dimension,
1349 &runtime_sort_dimension,
1350 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001351
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001352 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001353 if (!strcmp(available_sorts[i]->name, tok)) {
1354 list_add_tail(&available_sorts[i]->list, list);
1355
1356 return 0;
1357 }
1358 }
1359
1360 return -1;
1361}
1362
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001363static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001364{
1365 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001366 struct rb_root *root = &sched->atom_root;
1367again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001368 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001369 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001370 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001371 if (!node)
1372 break;
1373
Josef Bacik2f80dd42015-05-22 09:18:40 -04001374 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001375 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001376 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001377 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001378 if (root == &sched->atom_root) {
1379 root = &sched->merged_atom_root;
1380 goto again;
1381 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001382}
1383
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001384static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001385 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001386 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001387 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001388{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001389 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001390
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001391 if (sched->tp_handler->wakeup_event)
1392 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001393
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001394 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001395}
1396
Jiri Olsaa151a372016-04-12 15:29:29 +02001397union map_priv {
1398 void *ptr;
1399 bool color;
1400};
1401
1402static bool thread__has_color(struct thread *thread)
1403{
1404 union map_priv priv = {
1405 .ptr = thread__priv(thread),
1406 };
1407
1408 return priv.color;
1409}
1410
1411static struct thread*
1412map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1413{
1414 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1415 union map_priv priv = {
1416 .color = false,
1417 };
1418
1419 if (!sched->map.color_pids || !thread || thread__priv(thread))
1420 return thread;
1421
1422 if (thread_map__has(sched->map.color_pids, tid))
1423 priv.color = true;
1424
1425 thread__set_priv(thread, priv.ptr);
1426 return thread;
1427}
1428
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001429static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1430 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001431{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001432 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1433 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001434 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001435 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001436 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001437 int i, this_cpu = sample->cpu;
1438 int cpus_nr;
1439 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001440 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001441 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001442
1443 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1444
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001445 if (this_cpu > sched->max_cpu)
1446 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001447
Jiri Olsa99623c62016-04-12 15:29:26 +02001448 if (sched->map.comp) {
1449 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1450 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1451 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1452 new_cpu = true;
1453 }
1454 } else
1455 cpus_nr = sched->max_cpu;
1456
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001457 timestamp0 = sched->cpu_last_switched[this_cpu];
1458 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001459 if (timestamp0)
1460 delta = timestamp - timestamp0;
1461 else
1462 delta = 0;
1463
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001464 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001465 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001466 return -1;
1467 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001468
Jiri Olsaa151a372016-04-12 15:29:29 +02001469 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001470 if (sched_in == NULL)
1471 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001472
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001473 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001474
1475 printf(" ");
1476
1477 new_shortname = 0;
1478 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001479 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1480 /*
1481 * Don't allocate a letter-number for swapper:0
1482 * as a shortname. Instead, we use '.' for it.
1483 */
1484 sched_in->shortname[0] = '.';
1485 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001486 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001487 sched_in->shortname[0] = sched->next_shortname1;
1488 sched_in->shortname[1] = sched->next_shortname2;
1489
1490 if (sched->next_shortname1 < 'Z') {
1491 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001492 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001493 sched->next_shortname1 = 'A';
1494 if (sched->next_shortname2 < '9')
1495 sched->next_shortname2++;
1496 else
1497 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001498 }
1499 }
1500 new_shortname = 1;
1501 }
1502
Jiri Olsa99623c62016-04-12 15:29:26 +02001503 for (i = 0; i < cpus_nr; i++) {
1504 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001505 struct thread *curr_thread = sched->curr_thread[cpu];
1506 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001507 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001508
1509 if (curr_thread && thread__has_color(curr_thread))
1510 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001511
Jiri Olsa73643bb2016-04-12 15:29:31 +02001512 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1513 continue;
1514
Jiri Olsacf294f22016-04-12 15:29:30 +02001515 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1516 cpu_color = COLOR_CPUS;
1517
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001518 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001519 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001520 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001521 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001522
Dongsheng6bcab4e2014-05-06 14:39:01 +09001523 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001524 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001525 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001526 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001527 }
1528
Jiri Olsa73643bb2016-04-12 15:29:31 +02001529 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1530 goto out;
1531
Namhyung Kim99620a52016-10-24 11:02:45 +09001532 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1533 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001534 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001535 const char *pid_color = color;
1536
1537 if (thread__has_color(sched_in))
1538 pid_color = COLOR_PIDS;
1539
1540 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001541 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001542 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001543
Jiri Olsa99623c62016-04-12 15:29:26 +02001544 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001545 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001546
Jiri Olsa73643bb2016-04-12 15:29:31 +02001547out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001548 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001549
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001550 thread__put(sched_in);
1551
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001552 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001553}
1554
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001555static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001556 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001557 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001558 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001559{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001560 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001561 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001562 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1563 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001564
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001565 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001566 /*
1567 * Are we trying to switch away a PID that is
1568 * not current?
1569 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001570 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001571 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001572 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001573
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001574 if (sched->tp_handler->switch_event)
1575 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001576
1577 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001578 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001579}
1580
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001581static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001582 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001583 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001584 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001585{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001586 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001587
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001588 if (sched->tp_handler->runtime_event)
1589 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001590
1591 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001592}
1593
David Aherncb627502013-08-07 22:50:47 -04001594static int perf_sched__process_fork_event(struct perf_tool *tool,
1595 union perf_event *event,
1596 struct perf_sample *sample,
1597 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001598{
1599 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1600
David Aherncb627502013-08-07 22:50:47 -04001601 /* run the fork event through the perf machineruy */
1602 perf_event__process_fork(tool, event, sample, machine);
1603
1604 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001605 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001606 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001607
1608 return 0;
1609}
1610
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001611static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001612 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001613 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001614 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001615{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001616 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001617
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001618 if (sched->tp_handler->migrate_task_event)
1619 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001620
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001621 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001622}
1623
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001624typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001625 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001626 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001627 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001628
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001629static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1630 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001631 struct perf_sample *sample,
1632 struct perf_evsel *evsel,
1633 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001634{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001635 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001636
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001637 if (evsel->handler != NULL) {
1638 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001639 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001640 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001641
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001642 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001643}
1644
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001645static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001646{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001647 const struct perf_evsel_str_handler handlers[] = {
1648 { "sched:sched_switch", process_sched_switch_event, },
1649 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1650 { "sched:sched_wakeup", process_sched_wakeup_event, },
1651 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001652 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1653 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001654 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001655 struct perf_data_file file = {
1656 .path = input_name,
1657 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001658 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001659 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001660 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001661
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001662 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001663 if (session == NULL) {
1664 pr_debug("No Memory for session\n");
1665 return -1;
1666 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001667
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001668 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001669
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001670 if (perf_session__set_tracepoints_handlers(session, handlers))
1671 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001672
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001673 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001674 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001675 if (err) {
1676 pr_err("Failed to process events, error %d", err);
1677 goto out_delete;
1678 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001679
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001680 sched->nr_events = session->evlist->stats.nr_events[0];
1681 sched->nr_lost_events = session->evlist->stats.total_lost;
1682 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001683 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001684
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001685 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001686out_delete:
1687 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001688 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001689}
1690
David Ahern49394a22016-11-16 15:06:29 +09001691/*
1692 * scheduling times are printed as msec.usec
1693 */
1694static inline void print_sched_time(unsigned long long nsecs, int width)
1695{
1696 unsigned long msecs;
1697 unsigned long usecs;
1698
1699 msecs = nsecs / NSEC_PER_MSEC;
1700 nsecs -= msecs * NSEC_PER_MSEC;
1701 usecs = nsecs / NSEC_PER_USEC;
1702 printf("%*lu.%03lu ", width, msecs, usecs);
1703}
1704
1705/*
1706 * returns runtime data for event, allocating memory for it the
1707 * first time it is used.
1708 */
1709static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1710{
1711 struct evsel_runtime *r = evsel->priv;
1712
1713 if (r == NULL) {
1714 r = zalloc(sizeof(struct evsel_runtime));
1715 evsel->priv = r;
1716 }
1717
1718 return r;
1719}
1720
1721/*
1722 * save last time event was seen per cpu
1723 */
1724static void perf_evsel__save_time(struct perf_evsel *evsel,
1725 u64 timestamp, u32 cpu)
1726{
1727 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1728
1729 if (r == NULL)
1730 return;
1731
1732 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1733 int i, n = __roundup_pow_of_two(cpu+1);
1734 void *p = r->last_time;
1735
1736 p = realloc(r->last_time, n * sizeof(u64));
1737 if (!p)
1738 return;
1739
1740 r->last_time = p;
1741 for (i = r->ncpu; i < n; ++i)
1742 r->last_time[i] = (u64) 0;
1743
1744 r->ncpu = n;
1745 }
1746
1747 r->last_time[cpu] = timestamp;
1748}
1749
1750/* returns last time this event was seen on the given cpu */
1751static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1752{
1753 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1754
1755 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1756 return 0;
1757
1758 return r->last_time[cpu];
1759}
1760
1761static int comm_width = 20;
1762
1763static char *timehist_get_commstr(struct thread *thread)
1764{
1765 static char str[32];
1766 const char *comm = thread__comm_str(thread);
1767 pid_t tid = thread->tid;
1768 pid_t pid = thread->pid_;
1769 int n;
1770
1771 if (pid == 0)
1772 n = scnprintf(str, sizeof(str), "%s", comm);
1773
1774 else if (tid != pid)
1775 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1776
1777 else
1778 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1779
1780 if (n > comm_width)
1781 comm_width = n;
1782
1783 return str;
1784}
1785
1786static void timehist_header(void)
1787{
1788 printf("%15s %6s ", "time", "cpu");
1789
1790 printf(" %-20s %9s %9s %9s",
1791 "task name", "wait time", "sch delay", "run time");
1792
1793 printf("\n");
1794
1795 /*
1796 * units row
1797 */
1798 printf("%15s %-6s ", "", "");
1799
1800 printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)");
1801
1802 /*
1803 * separator
1804 */
1805 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1806
1807 printf(" %.20s %.9s %.9s %.9s",
1808 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1809 graph_dotted_line);
1810
1811 printf("\n");
1812}
1813
David Ahernfc1469f2016-11-16 15:06:31 +09001814static void timehist_print_sample(struct perf_sched *sched,
1815 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001816 struct addr_location *al,
David Ahern49394a22016-11-16 15:06:29 +09001817 struct thread *thread)
1818{
1819 struct thread_runtime *tr = thread__priv(thread);
1820 char tstr[64];
1821
1822 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
1823 printf("%15s [%04d] ", tstr, sample->cpu);
1824
1825 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1826
1827 print_sched_time(tr->dt_wait, 6);
1828 print_sched_time(tr->dt_delay, 6);
1829 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001830
1831 if (sched->show_wakeups)
1832 printf(" %-*s", comm_width, "");
1833
David Ahern6c973c92016-11-16 15:06:32 +09001834 if (thread->tid == 0)
1835 goto out;
1836
1837 if (sched->show_callchain)
1838 printf(" ");
1839
1840 sample__fprintf_sym(sample, al, 0,
1841 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
1842 EVSEL__PRINT_CALLCHAIN_ARROW,
1843 &callchain_cursor, stdout);
1844
1845out:
David Ahern49394a22016-11-16 15:06:29 +09001846 printf("\n");
1847}
1848
1849/*
1850 * Explanation of delta-time stats:
1851 *
1852 * t = time of current schedule out event
1853 * tprev = time of previous sched out event
1854 * also time of schedule-in event for current task
1855 * last_time = time of last sched change event for current task
1856 * (i.e, time process was last scheduled out)
1857 * ready_to_run = time of wakeup for current task
1858 *
1859 * -----|------------|------------|------------|------
1860 * last ready tprev t
1861 * time to run
1862 *
1863 * |-------- dt_wait --------|
1864 * |- dt_delay -|-- dt_run --|
1865 *
1866 * dt_run = run time of current task
1867 * dt_wait = time between last schedule out event for task and tprev
1868 * represents time spent off the cpu
1869 * dt_delay = time between wakeup and schedule-in of task
1870 */
1871
1872static void timehist_update_runtime_stats(struct thread_runtime *r,
1873 u64 t, u64 tprev)
1874{
1875 r->dt_delay = 0;
1876 r->dt_wait = 0;
1877 r->dt_run = 0;
1878 if (tprev) {
1879 r->dt_run = t - tprev;
1880 if (r->ready_to_run) {
1881 if (r->ready_to_run > tprev)
1882 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1883 else
1884 r->dt_delay = tprev - r->ready_to_run;
1885 }
1886
1887 if (r->last_time > tprev)
1888 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
1889 else if (r->last_time)
1890 r->dt_wait = tprev - r->last_time;
1891 }
1892
1893 update_stats(&r->run_stats, r->dt_run);
1894 r->total_run_time += r->dt_run;
1895}
1896
David Ahern6c973c92016-11-16 15:06:32 +09001897static bool is_idle_sample(struct perf_sched *sched,
1898 struct perf_sample *sample,
1899 struct perf_evsel *evsel,
1900 struct machine *machine)
David Ahern49394a22016-11-16 15:06:29 +09001901{
David Ahern6c973c92016-11-16 15:06:32 +09001902 struct thread *thread;
1903 struct callchain_cursor *cursor = &callchain_cursor;
1904
David Ahern49394a22016-11-16 15:06:29 +09001905 /* pid 0 == swapper == idle task */
1906 if (sample->pid == 0)
1907 return true;
1908
1909 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) {
1910 if (perf_evsel__intval(evsel, sample, "prev_pid") == 0)
1911 return true;
1912 }
David Ahern6c973c92016-11-16 15:06:32 +09001913
1914 /* want main thread for process - has maps */
1915 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
1916 if (thread == NULL) {
1917 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
1918 return false;
1919 }
1920
1921 if (!symbol_conf.use_callchain || sample->callchain == NULL)
1922 return false;
1923
1924 if (thread__resolve_callchain(thread, cursor, evsel, sample,
1925 NULL, NULL, sched->max_stack) != 0) {
1926 if (verbose)
1927 error("Failed to resolve callchain. Skipping\n");
1928
1929 return false;
1930 }
1931 callchain_cursor_commit(cursor);
David Ahern49394a22016-11-16 15:06:29 +09001932 return false;
1933}
1934
1935/*
1936 * Track idle stats per cpu by maintaining a local thread
1937 * struct for the idle task on each cpu.
1938 */
1939static int init_idle_threads(int ncpu)
1940{
1941 int i;
1942
1943 idle_threads = zalloc(ncpu * sizeof(struct thread *));
1944 if (!idle_threads)
1945 return -ENOMEM;
1946
1947 idle_max_cpu = ncpu - 1;
1948
1949 /* allocate the actual thread struct if needed */
1950 for (i = 0; i < ncpu; ++i) {
1951 idle_threads[i] = thread__new(0, 0);
1952 if (idle_threads[i] == NULL)
1953 return -ENOMEM;
1954
1955 thread__set_comm(idle_threads[i], idle_comm, 0);
1956 }
1957
1958 return 0;
1959}
1960
1961static void free_idle_threads(void)
1962{
1963 int i;
1964
1965 if (idle_threads == NULL)
1966 return;
1967
1968 for (i = 0; i <= idle_max_cpu; ++i) {
1969 if ((idle_threads[i]))
1970 thread__delete(idle_threads[i]);
1971 }
1972
1973 free(idle_threads);
1974}
1975
1976static struct thread *get_idle_thread(int cpu)
1977{
1978 /*
1979 * expand/allocate array of pointers to local thread
1980 * structs if needed
1981 */
1982 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
1983 int i, j = __roundup_pow_of_two(cpu+1);
1984 void *p;
1985
1986 p = realloc(idle_threads, j * sizeof(struct thread *));
1987 if (!p)
1988 return NULL;
1989
1990 idle_threads = (struct thread **) p;
1991 i = idle_max_cpu ? idle_max_cpu + 1 : 0;
1992 for (; i < j; ++i)
1993 idle_threads[i] = NULL;
1994
1995 idle_max_cpu = j;
1996 }
1997
1998 /* allocate a new thread struct if needed */
1999 if (idle_threads[cpu] == NULL) {
2000 idle_threads[cpu] = thread__new(0, 0);
2001
2002 if (idle_threads[cpu]) {
2003 idle_threads[cpu]->tid = 0;
2004 thread__set_comm(idle_threads[cpu], idle_comm, 0);
2005 }
2006 }
2007
2008 return idle_threads[cpu];
2009}
2010
2011/*
2012 * handle runtime stats saved per thread
2013 */
2014static struct thread_runtime *thread__init_runtime(struct thread *thread)
2015{
2016 struct thread_runtime *r;
2017
2018 r = zalloc(sizeof(struct thread_runtime));
2019 if (!r)
2020 return NULL;
2021
2022 init_stats(&r->run_stats);
2023 thread__set_priv(thread, r);
2024
2025 return r;
2026}
2027
2028static struct thread_runtime *thread__get_runtime(struct thread *thread)
2029{
2030 struct thread_runtime *tr;
2031
2032 tr = thread__priv(thread);
2033 if (tr == NULL) {
2034 tr = thread__init_runtime(thread);
2035 if (tr == NULL)
2036 pr_debug("Failed to malloc memory for runtime data.\n");
2037 }
2038
2039 return tr;
2040}
2041
David Ahern6c973c92016-11-16 15:06:32 +09002042static struct thread *timehist_get_thread(struct perf_sched *sched,
2043 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002044 struct machine *machine,
2045 struct perf_evsel *evsel)
2046{
2047 struct thread *thread;
2048
David Ahern6c973c92016-11-16 15:06:32 +09002049 if (is_idle_sample(sched, sample, evsel, machine)) {
David Ahern49394a22016-11-16 15:06:29 +09002050 thread = get_idle_thread(sample->cpu);
2051 if (thread == NULL)
2052 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2053
2054 } else {
2055 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2056 if (thread == NULL) {
2057 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2058 sample->tid);
2059 }
2060 }
2061
2062 return thread;
2063}
2064
David Ahern52df1382016-11-16 15:06:30 +09002065static bool timehist_skip_sample(struct perf_sched *sched,
2066 struct thread *thread)
David Ahern49394a22016-11-16 15:06:29 +09002067{
2068 bool rc = false;
2069
David Ahern52df1382016-11-16 15:06:30 +09002070 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002071 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002072 sched->skipped_samples++;
2073 }
David Ahern49394a22016-11-16 15:06:29 +09002074
2075 return rc;
2076}
2077
David Ahernfc1469f2016-11-16 15:06:31 +09002078static void timehist_print_wakeup_event(struct perf_sched *sched,
2079 struct perf_sample *sample,
2080 struct machine *machine,
2081 struct thread *awakened)
2082{
2083 struct thread *thread;
2084 char tstr[64];
2085
2086 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2087 if (thread == NULL)
2088 return;
2089
2090 /* show wakeup unless both awakee and awaker are filtered */
2091 if (timehist_skip_sample(sched, thread) &&
2092 timehist_skip_sample(sched, awakened)) {
2093 return;
2094 }
2095
2096 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2097 printf("%15s [%04d] ", tstr, sample->cpu);
2098
2099 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2100
2101 /* dt spacer */
2102 printf(" %9s %9s %9s ", "", "", "");
2103
2104 printf("awakened: %s", timehist_get_commstr(awakened));
2105
2106 printf("\n");
2107}
2108
2109static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002110 union perf_event *event __maybe_unused,
2111 struct perf_evsel *evsel,
2112 struct perf_sample *sample,
2113 struct machine *machine)
2114{
David Ahernfc1469f2016-11-16 15:06:31 +09002115 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002116 struct thread *thread;
2117 struct thread_runtime *tr = NULL;
2118 /* want pid of awakened task not pid in sample */
2119 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2120
2121 thread = machine__findnew_thread(machine, 0, pid);
2122 if (thread == NULL)
2123 return -1;
2124
2125 tr = thread__get_runtime(thread);
2126 if (tr == NULL)
2127 return -1;
2128
2129 if (tr->ready_to_run == 0)
2130 tr->ready_to_run = sample->time;
2131
David Ahernfc1469f2016-11-16 15:06:31 +09002132 /* show wakeups if requested */
2133 if (sched->show_wakeups)
2134 timehist_print_wakeup_event(sched, sample, machine, thread);
2135
David Ahern49394a22016-11-16 15:06:29 +09002136 return 0;
2137}
2138
David Ahern52df1382016-11-16 15:06:30 +09002139static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002140 union perf_event *event,
2141 struct perf_evsel *evsel,
2142 struct perf_sample *sample,
2143 struct machine *machine)
2144{
David Ahernfc1469f2016-11-16 15:06:31 +09002145 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002146 struct addr_location al;
2147 struct thread *thread;
2148 struct thread_runtime *tr = NULL;
2149 u64 tprev;
2150 int rc = 0;
2151
2152 if (machine__resolve(machine, &al, sample) < 0) {
2153 pr_err("problem processing %d event. skipping it\n",
2154 event->header.type);
2155 rc = -1;
2156 goto out;
2157 }
2158
David Ahern6c973c92016-11-16 15:06:32 +09002159 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002160 if (thread == NULL) {
2161 rc = -1;
2162 goto out;
2163 }
2164
David Ahern52df1382016-11-16 15:06:30 +09002165 if (timehist_skip_sample(sched, thread))
David Ahern49394a22016-11-16 15:06:29 +09002166 goto out;
2167
2168 tr = thread__get_runtime(thread);
2169 if (tr == NULL) {
2170 rc = -1;
2171 goto out;
2172 }
2173
2174 tprev = perf_evsel__get_time(evsel, sample->cpu);
2175
2176 timehist_update_runtime_stats(tr, sample->time, tprev);
David Ahern52df1382016-11-16 15:06:30 +09002177 if (!sched->summary_only)
David Ahern6c973c92016-11-16 15:06:32 +09002178 timehist_print_sample(sched, sample, &al, thread);
David Ahern49394a22016-11-16 15:06:29 +09002179
2180out:
2181 if (tr) {
2182 /* time of this sched_switch event becomes last time task seen */
2183 tr->last_time = sample->time;
2184
2185 /* sched out event for task so reset ready to run time */
2186 tr->ready_to_run = 0;
2187 }
2188
2189 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2190
2191 return rc;
2192}
2193
2194static int timehist_sched_switch_event(struct perf_tool *tool,
2195 union perf_event *event,
2196 struct perf_evsel *evsel,
2197 struct perf_sample *sample,
2198 struct machine *machine __maybe_unused)
2199{
2200 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2201}
2202
2203static int process_lost(struct perf_tool *tool __maybe_unused,
2204 union perf_event *event,
2205 struct perf_sample *sample,
2206 struct machine *machine __maybe_unused)
2207{
2208 char tstr[64];
2209
2210 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2211 printf("%15s ", tstr);
2212 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2213
2214 return 0;
2215}
2216
2217
David Ahern52df1382016-11-16 15:06:30 +09002218static void print_thread_runtime(struct thread *t,
2219 struct thread_runtime *r)
2220{
2221 double mean = avg_stats(&r->run_stats);
2222 float stddev;
2223
2224 printf("%*s %5d %9" PRIu64 " ",
2225 comm_width, timehist_get_commstr(t), t->ppid,
2226 (u64) r->run_stats.n);
2227
2228 print_sched_time(r->total_run_time, 8);
2229 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2230 print_sched_time(r->run_stats.min, 6);
2231 printf(" ");
2232 print_sched_time((u64) mean, 6);
2233 printf(" ");
2234 print_sched_time(r->run_stats.max, 6);
2235 printf(" ");
2236 printf("%5.2f", stddev);
2237 printf("\n");
2238}
2239
2240struct total_run_stats {
2241 u64 sched_count;
2242 u64 task_count;
2243 u64 total_run_time;
2244};
2245
2246static int __show_thread_runtime(struct thread *t, void *priv)
2247{
2248 struct total_run_stats *stats = priv;
2249 struct thread_runtime *r;
2250
2251 if (thread__is_filtered(t))
2252 return 0;
2253
2254 r = thread__priv(t);
2255 if (r && r->run_stats.n) {
2256 stats->task_count++;
2257 stats->sched_count += r->run_stats.n;
2258 stats->total_run_time += r->total_run_time;
2259 print_thread_runtime(t, r);
2260 }
2261
2262 return 0;
2263}
2264
2265static int show_thread_runtime(struct thread *t, void *priv)
2266{
2267 if (t->dead)
2268 return 0;
2269
2270 return __show_thread_runtime(t, priv);
2271}
2272
2273static int show_deadthread_runtime(struct thread *t, void *priv)
2274{
2275 if (!t->dead)
2276 return 0;
2277
2278 return __show_thread_runtime(t, priv);
2279}
2280
2281static void timehist_print_summary(struct perf_sched *sched,
2282 struct perf_session *session)
2283{
2284 struct machine *m = &session->machines.host;
2285 struct total_run_stats totals;
2286 u64 task_count;
2287 struct thread *t;
2288 struct thread_runtime *r;
2289 int i;
2290
2291 memset(&totals, 0, sizeof(totals));
2292
2293 if (comm_width < 30)
2294 comm_width = 30;
2295
2296 printf("\nRuntime summary\n");
2297 printf("%*s parent sched-in ", comm_width, "comm");
2298 printf(" run-time min-run avg-run max-run stddev\n");
2299 printf("%*s (count) ", comm_width, "");
2300 printf(" (msec) (msec) (msec) (msec) %%\n");
2301 printf("%.105s\n", graph_dotted_line);
2302
2303 machine__for_each_thread(m, show_thread_runtime, &totals);
2304 task_count = totals.task_count;
2305 if (!task_count)
2306 printf("<no still running tasks>\n");
2307
2308 printf("\nTerminated tasks:\n");
2309 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2310 if (task_count == totals.task_count)
2311 printf("<no terminated tasks>\n");
2312
2313 /* CPU idle stats not tracked when samples were skipped */
2314 if (sched->skipped_samples)
2315 return;
2316
2317 printf("\nIdle stats:\n");
2318 for (i = 0; i <= idle_max_cpu; ++i) {
2319 t = idle_threads[i];
2320 if (!t)
2321 continue;
2322
2323 r = thread__priv(t);
2324 if (r && r->run_stats.n) {
2325 totals.sched_count += r->run_stats.n;
2326 printf(" CPU %2d idle for ", i);
2327 print_sched_time(r->total_run_time, 6);
2328 printf(" msec\n");
2329 } else
2330 printf(" CPU %2d idle entire time window\n", i);
2331 }
2332
2333 printf("\n"
2334 " Total number of unique tasks: %" PRIu64 "\n"
2335 "Total number of context switches: %" PRIu64 "\n"
2336 " Total run time (msec): ",
2337 totals.task_count, totals.sched_count);
2338
2339 print_sched_time(totals.total_run_time, 2);
2340 printf("\n");
2341}
2342
David Ahern49394a22016-11-16 15:06:29 +09002343typedef int (*sched_handler)(struct perf_tool *tool,
2344 union perf_event *event,
2345 struct perf_evsel *evsel,
2346 struct perf_sample *sample,
2347 struct machine *machine);
2348
2349static int perf_timehist__process_sample(struct perf_tool *tool,
2350 union perf_event *event,
2351 struct perf_sample *sample,
2352 struct perf_evsel *evsel,
2353 struct machine *machine)
2354{
2355 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2356 int err = 0;
2357 int this_cpu = sample->cpu;
2358
2359 if (this_cpu > sched->max_cpu)
2360 sched->max_cpu = this_cpu;
2361
2362 if (evsel->handler != NULL) {
2363 sched_handler f = evsel->handler;
2364
2365 err = f(tool, event, evsel, sample, machine);
2366 }
2367
2368 return err;
2369}
2370
David Ahern6c973c92016-11-16 15:06:32 +09002371static int timehist_check_attr(struct perf_sched *sched,
2372 struct perf_evlist *evlist)
2373{
2374 struct perf_evsel *evsel;
2375 struct evsel_runtime *er;
2376
2377 list_for_each_entry(evsel, &evlist->entries, node) {
2378 er = perf_evsel__get_runtime(evsel);
2379 if (er == NULL) {
2380 pr_err("Failed to allocate memory for evsel runtime data\n");
2381 return -1;
2382 }
2383
2384 if (sched->show_callchain &&
2385 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2386 pr_info("Samples do not have callchains.\n");
2387 sched->show_callchain = 0;
2388 symbol_conf.use_callchain = 0;
2389 }
2390 }
2391
2392 return 0;
2393}
2394
David Ahern49394a22016-11-16 15:06:29 +09002395static int perf_sched__timehist(struct perf_sched *sched)
2396{
2397 const struct perf_evsel_str_handler handlers[] = {
2398 { "sched:sched_switch", timehist_sched_switch_event, },
2399 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2400 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2401 };
2402 struct perf_data_file file = {
2403 .path = input_name,
2404 .mode = PERF_DATA_MODE_READ,
2405 };
2406
2407 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002408 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002409 int err = -1;
2410
2411 /*
2412 * event handlers for timehist option
2413 */
2414 sched->tool.sample = perf_timehist__process_sample;
2415 sched->tool.mmap = perf_event__process_mmap;
2416 sched->tool.comm = perf_event__process_comm;
2417 sched->tool.exit = perf_event__process_exit;
2418 sched->tool.fork = perf_event__process_fork;
2419 sched->tool.lost = process_lost;
2420 sched->tool.attr = perf_event__process_attr;
2421 sched->tool.tracing_data = perf_event__process_tracing_data;
2422 sched->tool.build_id = perf_event__process_build_id;
2423
2424 sched->tool.ordered_events = true;
2425 sched->tool.ordering_requires_timestamps = true;
2426
David Ahern6c973c92016-11-16 15:06:32 +09002427 symbol_conf.use_callchain = sched->show_callchain;
2428
David Ahern49394a22016-11-16 15:06:29 +09002429 session = perf_session__new(&file, false, &sched->tool);
2430 if (session == NULL)
2431 return -ENOMEM;
2432
David Ahern52df1382016-11-16 15:06:30 +09002433 evlist = session->evlist;
2434
David Ahern49394a22016-11-16 15:06:29 +09002435 symbol__init(&session->header.env);
2436
David Ahern6c973c92016-11-16 15:06:32 +09002437 if (timehist_check_attr(sched, evlist) != 0)
2438 goto out;
2439
David Ahern49394a22016-11-16 15:06:29 +09002440 setup_pager();
2441
2442 /* setup per-evsel handlers */
2443 if (perf_session__set_tracepoints_handlers(session, handlers))
2444 goto out;
2445
2446 if (!perf_session__has_traces(session, "record -R"))
2447 goto out;
2448
2449 /* pre-allocate struct for per-CPU idle stats */
2450 sched->max_cpu = session->header.env.nr_cpus_online;
2451 if (sched->max_cpu == 0)
2452 sched->max_cpu = 4;
2453 if (init_idle_threads(sched->max_cpu))
2454 goto out;
2455
David Ahern52df1382016-11-16 15:06:30 +09002456 /* summary_only implies summary option, but don't overwrite summary if set */
2457 if (sched->summary_only)
2458 sched->summary = sched->summary_only;
2459
2460 if (!sched->summary_only)
2461 timehist_header();
David Ahern49394a22016-11-16 15:06:29 +09002462
2463 err = perf_session__process_events(session);
2464 if (err) {
2465 pr_err("Failed to process events, error %d", err);
2466 goto out;
2467 }
2468
David Ahern52df1382016-11-16 15:06:30 +09002469 sched->nr_events = evlist->stats.nr_events[0];
2470 sched->nr_lost_events = evlist->stats.total_lost;
2471 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2472
2473 if (sched->summary)
2474 timehist_print_summary(sched, session);
2475
David Ahern49394a22016-11-16 15:06:29 +09002476out:
2477 free_idle_threads();
2478 perf_session__delete(session);
2479
2480 return err;
2481}
2482
2483
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002484static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002485{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002486 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002487 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002488 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2489 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002490 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002491 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002492 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002493 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2494 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002495 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002496 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002497 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002498 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2499 sched->nr_context_switch_bugs, sched->nr_timestamps);
2500 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002501 printf(" (due to lost events?)");
2502 printf("\n");
2503 }
2504}
2505
Josef Bacik2f80dd42015-05-22 09:18:40 -04002506static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2507{
2508 struct rb_node **new = &(root->rb_node), *parent = NULL;
2509 struct work_atoms *this;
2510 const char *comm = thread__comm_str(data->thread), *this_comm;
2511
2512 while (*new) {
2513 int cmp;
2514
2515 this = container_of(*new, struct work_atoms, node);
2516 parent = *new;
2517
2518 this_comm = thread__comm_str(this->thread);
2519 cmp = strcmp(comm, this_comm);
2520 if (cmp > 0) {
2521 new = &((*new)->rb_left);
2522 } else if (cmp < 0) {
2523 new = &((*new)->rb_right);
2524 } else {
2525 this->num_merged++;
2526 this->total_runtime += data->total_runtime;
2527 this->nb_atoms += data->nb_atoms;
2528 this->total_lat += data->total_lat;
2529 list_splice(&data->work_list, &this->work_list);
2530 if (this->max_lat < data->max_lat) {
2531 this->max_lat = data->max_lat;
2532 this->max_lat_at = data->max_lat_at;
2533 }
2534 zfree(&data);
2535 return;
2536 }
2537 }
2538
2539 data->num_merged++;
2540 rb_link_node(&data->node, parent, new);
2541 rb_insert_color(&data->node, root);
2542}
2543
2544static void perf_sched__merge_lat(struct perf_sched *sched)
2545{
2546 struct work_atoms *data;
2547 struct rb_node *node;
2548
2549 if (sched->skip_merge)
2550 return;
2551
2552 while ((node = rb_first(&sched->atom_root))) {
2553 rb_erase(node, &sched->atom_root);
2554 data = rb_entry(node, struct work_atoms, node);
2555 __merge_work_atoms(&sched->merged_atom_root, data);
2556 }
2557}
2558
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002559static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002560{
2561 struct rb_node *next;
2562
2563 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002564
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002565 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002566 return -1;
David Ahernad9def72013-08-07 22:50:44 -04002567
Josef Bacik2f80dd42015-05-22 09:18:40 -04002568 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002569 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002570
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002571 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
2572 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
2573 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002574
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002575 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002576
2577 while (next) {
2578 struct work_atoms *work_list;
2579
2580 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002581 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002582 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002583 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002584 }
2585
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002586 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002587 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03002588 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002589
2590 printf(" ---------------------------------------------------\n");
2591
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002592 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002593 printf("\n");
2594
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002595 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002596}
2597
Jiri Olsa99623c62016-04-12 15:29:26 +02002598static int setup_map_cpus(struct perf_sched *sched)
2599{
Jiri Olsa73643bb2016-04-12 15:29:31 +02002600 struct cpu_map *map;
2601
Jiri Olsa99623c62016-04-12 15:29:26 +02002602 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
2603
2604 if (sched->map.comp) {
2605 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02002606 if (!sched->map.comp_cpus)
2607 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02002608 }
2609
Jiri Olsa73643bb2016-04-12 15:29:31 +02002610 if (!sched->map.cpus_str)
2611 return 0;
2612
2613 map = cpu_map__new(sched->map.cpus_str);
2614 if (!map) {
2615 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
2616 return -1;
2617 }
2618
2619 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02002620 return 0;
2621}
2622
Jiri Olsaa151a372016-04-12 15:29:29 +02002623static int setup_color_pids(struct perf_sched *sched)
2624{
2625 struct thread_map *map;
2626
2627 if (!sched->map.color_pids_str)
2628 return 0;
2629
2630 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
2631 if (!map) {
2632 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
2633 return -1;
2634 }
2635
2636 sched->map.color_pids = map;
2637 return 0;
2638}
2639
Jiri Olsacf294f22016-04-12 15:29:30 +02002640static int setup_color_cpus(struct perf_sched *sched)
2641{
2642 struct cpu_map *map;
2643
2644 if (!sched->map.color_cpus_str)
2645 return 0;
2646
2647 map = cpu_map__new(sched->map.color_cpus_str);
2648 if (!map) {
2649 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
2650 return -1;
2651 }
2652
2653 sched->map.color_cpus = map;
2654 return 0;
2655}
2656
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002657static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002658{
Jiri Olsa99623c62016-04-12 15:29:26 +02002659 if (setup_map_cpus(sched))
2660 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02002661
Jiri Olsaa151a372016-04-12 15:29:29 +02002662 if (setup_color_pids(sched))
2663 return -1;
2664
Jiri Olsacf294f22016-04-12 15:29:30 +02002665 if (setup_color_cpus(sched))
2666 return -1;
2667
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002668 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002669 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002670 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002671 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002672 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002673}
2674
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002675static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002676{
2677 unsigned long i;
2678
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002679 calibrate_run_measurement_overhead(sched);
2680 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002681
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002682 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002683
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002684 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002685 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002686
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002687 printf("nr_run_events: %ld\n", sched->nr_run_events);
2688 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
2689 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002690
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002691 if (sched->targetless_wakeups)
2692 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
2693 if (sched->multitarget_wakeups)
2694 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
2695 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002696 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002697 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002698
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002699 print_task_traces(sched);
2700 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002701
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002702 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002703 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002704 for (i = 0; i < sched->replay_repeat; i++)
2705 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002706
2707 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002708}
2709
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002710static void setup_sorting(struct perf_sched *sched, const struct option *options,
2711 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002712{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002713 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002714
2715 for (tok = strtok_r(str, ", ", &tmp);
2716 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002717 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002718 usage_with_options_msg(usage_msg, options,
2719 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002720 }
2721 }
2722
2723 free(str);
2724
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002725 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002726}
2727
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002728static int __cmd_record(int argc, const char **argv)
2729{
2730 unsigned int rec_argc, i, j;
2731 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002732 const char * const record_args[] = {
2733 "record",
2734 "-a",
2735 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002736 "-m", "1024",
2737 "-c", "1",
2738 "-e", "sched:sched_switch",
2739 "-e", "sched:sched_stat_wait",
2740 "-e", "sched:sched_stat_sleep",
2741 "-e", "sched:sched_stat_iowait",
2742 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002743 "-e", "sched:sched_process_fork",
2744 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09002745 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002746 "-e", "sched:sched_migrate_task",
2747 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002748
2749 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
2750 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2751
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02002752 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11002753 return -ENOMEM;
2754
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002755 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2756 rec_argv[i] = strdup(record_args[i]);
2757
2758 for (j = 1; j < (unsigned int)argc; j++, i++)
2759 rec_argv[i] = argv[j];
2760
2761 BUG_ON(i != rec_argc);
2762
2763 return cmd_record(i, rec_argv, NULL);
2764}
2765
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002766int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002767{
Adrian Hunter8a39df82013-10-22 10:34:15 +03002768 const char default_sort_order[] = "avg, max, switch, runtime";
2769 struct perf_sched sched = {
2770 .tool = {
2771 .sample = perf_sched__process_tracepoint_sample,
2772 .comm = perf_event__process_comm,
2773 .lost = perf_event__process_lost,
2774 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002775 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002776 },
2777 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
2778 .sort_list = LIST_HEAD_INIT(sched.sort_list),
2779 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
2780 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002781 .sort_order = default_sort_order,
2782 .replay_repeat = 10,
2783 .profile_cpu = -1,
2784 .next_shortname1 = 'A',
2785 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04002786 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09002787 .show_callchain = 1,
2788 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002789 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09002790 const struct option sched_options[] = {
2791 OPT_STRING('i', "input", &input_name, "file",
2792 "input file name"),
2793 OPT_INCR('v', "verbose", &verbose,
2794 "be more verbose (show symbol address, etc)"),
2795 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2796 "dump raw trace in ASCII"),
2797 OPT_END()
2798 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002799 const struct option latency_options[] = {
2800 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
2801 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002802 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
2803 "CPU to profile on"),
2804 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2805 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04002806 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
2807 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002808 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002809 };
2810 const struct option replay_options[] = {
2811 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
2812 "repeat the workload replay N times (-1: infinite)"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002813 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2814 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08002815 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002816 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002817 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002818 const struct option map_options[] = {
2819 OPT_BOOLEAN(0, "compact", &sched.map.comp,
2820 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02002821 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
2822 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02002823 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
2824 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02002825 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
2826 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002827 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02002828 };
David Ahern49394a22016-11-16 15:06:29 +09002829 const struct option timehist_options[] = {
2830 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2831 "file", "vmlinux pathname"),
2832 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2833 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09002834 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
2835 "Display call chains if present (default on)"),
2836 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
2837 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09002838 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2839 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09002840 OPT_BOOLEAN('s', "summary", &sched.summary_only,
2841 "Show only syscall summary with statistics"),
2842 OPT_BOOLEAN('S', "with-summary", &sched.summary,
2843 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09002844 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Ahern49394a22016-11-16 15:06:29 +09002845 OPT_PARENT(sched_options)
2846 };
2847
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002848 const char * const latency_usage[] = {
2849 "perf sched latency [<options>]",
2850 NULL
2851 };
2852 const char * const replay_usage[] = {
2853 "perf sched replay [<options>]",
2854 NULL
2855 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002856 const char * const map_usage[] = {
2857 "perf sched map [<options>]",
2858 NULL
2859 };
David Ahern49394a22016-11-16 15:06:29 +09002860 const char * const timehist_usage[] = {
2861 "perf sched timehist [<options>]",
2862 NULL
2863 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002864 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09002865 "replay", "script",
2866 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002867 const char *sched_usage[] = {
2868 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002869 NULL
2870 };
2871 struct trace_sched_handler lat_ops = {
2872 .wakeup_event = latency_wakeup_event,
2873 .switch_event = latency_switch_event,
2874 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002875 .migrate_task_event = latency_migrate_task_event,
2876 };
2877 struct trace_sched_handler map_ops = {
2878 .switch_event = map_switch_event,
2879 };
2880 struct trace_sched_handler replay_ops = {
2881 .wakeup_event = replay_wakeup_event,
2882 .switch_event = replay_switch_event,
2883 .fork_event = replay_fork_event,
2884 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002885 unsigned int i;
2886
2887 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2888 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002889
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002890 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2891 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002892 if (!argc)
2893 usage_with_options(sched_usage, sched_options);
2894
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002895 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002896 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002897 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002898 if (!strcmp(argv[0], "script"))
2899 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002900
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002901 if (!strncmp(argv[0], "rec", 3)) {
2902 return __cmd_record(argc, argv);
2903 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002904 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002905 if (argc > 1) {
2906 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2907 if (argc)
2908 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002909 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002910 setup_sorting(&sched, latency_options, latency_usage);
2911 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002912 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002913 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002914 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002915 if (argc)
2916 usage_with_options(map_usage, map_options);
2917 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002918 sched.tp_handler = &map_ops;
2919 setup_sorting(&sched, latency_options, latency_usage);
2920 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002921 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002922 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002923 if (argc) {
2924 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2925 if (argc)
2926 usage_with_options(replay_usage, replay_options);
2927 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002928 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09002929 } else if (!strcmp(argv[0], "timehist")) {
2930 if (argc) {
2931 argc = parse_options(argc, argv, timehist_options,
2932 timehist_usage, 0);
2933 if (argc)
2934 usage_with_options(timehist_usage, timehist_options);
2935 }
David Ahernfc1469f2016-11-16 15:06:31 +09002936 if (sched.show_wakeups && sched.summary_only) {
2937 pr_err(" Error: -s and -w are mutually exclusive.\n");
2938 parse_options_usage(timehist_usage, timehist_options, "s", true);
2939 parse_options_usage(NULL, timehist_options, "w", true);
2940 return -EINVAL;
2941 }
2942
David Ahern49394a22016-11-16 15:06:29 +09002943 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002944 } else {
2945 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002946 }
2947
Ingo Molnarec156762009-09-11 12:12:54 +02002948 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002949}