blob: 43fcc13e402d6d3ae9c190498752dd2541894780 [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 Aherna407b062016-11-16 15:06:33 +0900204 bool show_cpu_visual;
David Ahernfc1469f2016-11-16 15:06:31 +0900205 bool show_wakeups;
David Ahern52df1382016-11-16 15:06:30 +0900206 u64 skipped_samples;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300207};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200208
David Ahern49394a22016-11-16 15:06:29 +0900209/* per thread run time data */
210struct thread_runtime {
211 u64 last_time; /* time of previous sched in/out event */
212 u64 dt_run; /* run time */
213 u64 dt_wait; /* time between CPU access (off cpu) */
214 u64 dt_delay; /* time between wakeup and sched-in */
215 u64 ready_to_run; /* time of wakeup */
216
217 struct stats run_stats;
218 u64 total_run_time;
219};
220
221/* per event run time data */
222struct evsel_runtime {
223 u64 *last_time; /* time this event was last seen per cpu */
224 u32 ncpu; /* highest cpu slot allocated */
225};
226
227/* track idle times per cpu */
228static struct thread **idle_threads;
229static int idle_max_cpu;
230static char idle_comm[] = "<idle>";
231
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200232static u64 get_nsecs(void)
233{
234 struct timespec ts;
235
236 clock_gettime(CLOCK_MONOTONIC, &ts);
237
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300238 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200239}
240
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300241static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200242{
243 u64 T0 = get_nsecs(), T1;
244
245 do {
246 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300247 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200248}
249
250static void sleep_nsecs(u64 nsecs)
251{
252 struct timespec ts;
253
254 ts.tv_nsec = nsecs % 999999999;
255 ts.tv_sec = nsecs / 999999999;
256
257 nanosleep(&ts, NULL);
258}
259
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300260static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200261{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300262 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200263 int i;
264
265 for (i = 0; i < 10; i++) {
266 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300267 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200268 T1 = get_nsecs();
269 delta = T1-T0;
270 min_delta = min(min_delta, delta);
271 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300272 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200273
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200274 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200275}
276
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300277static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200278{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300279 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200280 int i;
281
282 for (i = 0; i < 10; i++) {
283 T0 = get_nsecs();
284 sleep_nsecs(10000);
285 T1 = get_nsecs();
286 delta = T1-T0;
287 min_delta = min(min_delta, delta);
288 }
289 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300290 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200291
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200292 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200293}
294
mingo39aeb522009-09-14 20:04:48 +0200295static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200296get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200297{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200298 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200299 unsigned long idx = task->nr_events;
300 size_t size;
301
302 event->timestamp = timestamp;
303 event->nr = idx;
304
305 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200306 size = sizeof(struct sched_atom *) * task->nr_events;
307 task->atoms = realloc(task->atoms, size);
308 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200309
mingo39aeb522009-09-14 20:04:48 +0200310 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200311
312 return event;
313}
314
mingo39aeb522009-09-14 20:04:48 +0200315static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200316{
317 if (!task->nr_events)
318 return NULL;
319
mingo39aeb522009-09-14 20:04:48 +0200320 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200321}
322
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300323static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
324 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200325{
mingo39aeb522009-09-14 20:04:48 +0200326 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200327
328 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200329 * optimize an existing RUN event by merging this one
330 * to it:
331 */
Ingo Molnarec156762009-09-11 12:12:54 +0200332 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300333 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200334 curr_event->duration += duration;
335 return;
336 }
337
338 event = get_new_event(task, timestamp);
339
340 event->type = SCHED_EVENT_RUN;
341 event->duration = duration;
342
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300343 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200344}
345
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300346static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
347 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200348{
mingo39aeb522009-09-14 20:04:48 +0200349 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200350
351 event = get_new_event(task, timestamp);
352 event->type = SCHED_EVENT_WAKEUP;
353 event->wakee = wakee;
354
355 wakee_event = last_event(wakee);
356 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300357 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200358 return;
359 }
360 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300361 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200362 return;
363 }
364
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200365 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200366 sem_init(wakee_event->wait_sem, 0, 0);
367 wakee_event->specific_wait = 1;
368 event->wait_sem = wakee_event->wait_sem;
369
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300370 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200371}
372
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300373static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
374 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200375{
mingo39aeb522009-09-14 20:04:48 +0200376 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200377
378 event->type = SCHED_EVENT_SLEEP;
379
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300380 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200381}
382
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300383static struct task_desc *register_pid(struct perf_sched *sched,
384 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200385{
386 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800387 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200388
Yunlong Songcb06ac22015-03-31 21:46:30 +0800389 if (sched->pid_to_task == NULL) {
390 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
391 pid_max = MAX_PID;
392 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
393 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800394 if (pid >= (unsigned long)pid_max) {
395 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
396 sizeof(struct task_desc *))) == NULL);
397 while (pid >= (unsigned long)pid_max)
398 sched->pid_to_task[pid_max++] = NULL;
399 }
Ingo Molnarec156762009-09-11 12:12:54 +0200400
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300401 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200402
403 if (task)
404 return task;
405
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200406 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200407 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300408 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200409 strcpy(task->comm, comm);
410 /*
411 * every task starts in sleeping state - this gets ignored
412 * if there's no wakeup pointing to this sleep state:
413 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300414 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200415
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300416 sched->pid_to_task[pid] = task;
417 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800418 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300419 BUG_ON(!sched->tasks);
420 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200421
Ingo Molnarad236fd2009-09-11 12:12:54 +0200422 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300423 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200424
425 return task;
426}
427
428
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300429static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200430{
431 struct task_desc *task;
432 unsigned long i;
433
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300434 for (i = 0; i < sched->nr_tasks; i++) {
435 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200436 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200437 task->nr, task->comm, task->pid, task->nr_events);
438 }
439}
440
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300441static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200442{
443 struct task_desc *task1, *task2;
444 unsigned long i, j;
445
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300446 for (i = 0; i < sched->nr_tasks; i++) {
447 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200448 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300449 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200450 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300451 task2 = sched->tasks[j];
452 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200453 }
454}
455
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300456static void perf_sched__process_event(struct perf_sched *sched,
457 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200458{
459 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200460
mingo39aeb522009-09-14 20:04:48 +0200461 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200462 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300463 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200464 break;
465 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200466 if (atom->wait_sem)
467 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200468 BUG_ON(ret);
469 break;
470 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200471 if (atom->wait_sem)
472 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200473 BUG_ON(ret);
474 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200475 case SCHED_EVENT_MIGRATION:
476 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200477 default:
478 BUG_ON(1);
479 }
480}
481
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200482static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200483{
484 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200485 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200486 int err;
487
488 err = getrusage(RUSAGE_SELF, &ru);
489 BUG_ON(err);
490
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300491 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
492 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200493
494 return sum;
495}
496
Yunlong Song939cda52015-03-31 21:46:34 +0800497static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200498{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800499 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800500 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800501 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800502 struct rlimit limit;
503 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800504
505 memset(&attr, 0, sizeof(attr));
506
507 attr.type = PERF_TYPE_SOFTWARE;
508 attr.config = PERF_COUNT_SW_TASK_CLOCK;
509
Yunlong Song939cda52015-03-31 21:46:34 +0800510force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200511 fd = sys_perf_event_open(&attr, 0, -1, -1,
512 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800513
Yunlong Song1aff59b2015-03-31 21:46:33 +0800514 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800515 if (errno == EMFILE) {
516 if (sched->force) {
517 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
518 limit.rlim_cur += sched->nr_tasks - cur_task;
519 if (limit.rlim_cur > limit.rlim_max) {
520 limit.rlim_max = limit.rlim_cur;
521 need_privilege = true;
522 }
523 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
524 if (need_privilege && errno == EPERM)
525 strcpy(info, "Need privilege\n");
526 } else
527 goto force_again;
528 } else
529 strcpy(info, "Have a try with -f option\n");
530 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900531 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800532 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300533 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800534 exit(EXIT_FAILURE);
535 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800536 return fd;
537}
538
539static u64 get_cpu_usage_nsec_self(int fd)
540{
541 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200542 int ret;
543
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800544 ret = read(fd, &runtime, sizeof(runtime));
545 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200546
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800547 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200548}
549
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300550struct sched_thread_parms {
551 struct task_desc *task;
552 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800553 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300554};
555
Ingo Molnarec156762009-09-11 12:12:54 +0200556static void *thread_func(void *ctx)
557{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300558 struct sched_thread_parms *parms = ctx;
559 struct task_desc *this_task = parms->task;
560 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200561 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200562 unsigned long i, ret;
563 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800564 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200565
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300566 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300567
Ingo Molnarec156762009-09-11 12:12:54 +0200568 sprintf(comm2, ":%s", this_task->comm);
569 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300570 if (fd < 0)
571 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200572again:
573 ret = sem_post(&this_task->ready_for_work);
574 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300575 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200576 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300577 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200578 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200579
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800580 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200581
582 for (i = 0; i < this_task->nr_events; i++) {
583 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300584 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200585 }
586
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800587 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200588 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200589 ret = sem_post(&this_task->work_done_sem);
590 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200591
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300592 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200593 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300594 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200595 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200596
597 goto again;
598}
599
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300600static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200601{
602 struct task_desc *task;
603 pthread_attr_t attr;
604 unsigned long i;
605 int err;
606
607 err = pthread_attr_init(&attr);
608 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200609 err = pthread_attr_setstacksize(&attr,
610 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200611 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200613 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300614 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200615 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300616 for (i = 0; i < sched->nr_tasks; i++) {
617 struct sched_thread_parms *parms = malloc(sizeof(*parms));
618 BUG_ON(parms == NULL);
619 parms->task = task = sched->tasks[i];
620 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800621 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200622 sem_init(&task->sleep_sem, 0, 0);
623 sem_init(&task->ready_for_work, 0, 0);
624 sem_init(&task->work_done_sem, 0, 0);
625 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300626 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200627 BUG_ON(err);
628 }
629}
630
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300631static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200632{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200633 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200634 struct task_desc *task;
635 unsigned long i, ret;
636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637 sched->start_time = get_nsecs();
638 sched->cpu_usage = 0;
639 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200640
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300641 for (i = 0; i < sched->nr_tasks; i++) {
642 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200643 ret = sem_wait(&task->ready_for_work);
644 BUG_ON(ret);
645 sem_init(&task->ready_for_work, 0, 0);
646 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300647 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200648 BUG_ON(ret);
649
650 cpu_usage_0 = get_cpu_usage_nsec_parent();
651
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300652 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200653
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300654 for (i = 0; i < sched->nr_tasks; i++) {
655 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200656 ret = sem_wait(&task->work_done_sem);
657 BUG_ON(ret);
658 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300659 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200660 task->cpu_usage = 0;
661 }
662
663 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300664 if (!sched->runavg_cpu_usage)
665 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800666 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 +0200667
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300668 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
669 if (!sched->runavg_parent_cpu_usage)
670 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800671 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
672 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200673
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300674 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200675 BUG_ON(ret);
676
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300677 for (i = 0; i < sched->nr_tasks; i++) {
678 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200679 sem_init(&task->sleep_sem, 0, 0);
680 task->curr_event = 0;
681 }
682}
683
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200685{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500686 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200687
688 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200690 T1 = get_nsecs();
691
692 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300693 sched->sum_runtime += delta;
694 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200695
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300696 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200697 if (delta < avg_delta)
698 fluct = avg_delta - delta;
699 else
700 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300701 sched->sum_fluct += fluct;
702 if (!sched->run_avg)
703 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800704 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200705
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300706 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200707
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300708 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200709
Ingo Molnarad236fd2009-09-11 12:12:54 +0200710 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300711 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200712
713#if 0
714 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200715 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300716 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200717 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200718 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300719 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
720 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200721#endif
722
Ingo Molnarad236fd2009-09-11 12:12:54 +0200723 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200724
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300725 if (sched->nr_sleep_corrections)
726 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
727 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200728}
729
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300730static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200731{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200732 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200733
734 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300735 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200736 T1 = get_nsecs();
737
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200738 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200739
740 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300741 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200742 T1 = get_nsecs();
743
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200744 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200745}
746
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300747static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300748replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300749 struct perf_evsel *evsel, struct perf_sample *sample,
750 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200751{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300752 const char *comm = perf_evsel__strval(evsel, sample, "comm");
753 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200754 struct task_desc *waker, *wakee;
755
756 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300757 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200758
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300759 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200760 }
761
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300762 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300763 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200764
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300765 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300766 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200767}
768
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300769static int replay_switch_event(struct perf_sched *sched,
770 struct perf_evsel *evsel,
771 struct perf_sample *sample,
772 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200773{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300774 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
775 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
776 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
777 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
778 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300779 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300780 u64 timestamp0, timestamp = sample->time;
781 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200782 s64 delta;
783
Ingo Molnarad236fd2009-09-11 12:12:54 +0200784 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300785 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200786
Ingo Molnarfbf94822009-09-11 12:12:54 +0200787 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300788 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200789
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300790 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200791 if (timestamp0)
792 delta = timestamp - timestamp0;
793 else
794 delta = 0;
795
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300796 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900797 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300798 return -1;
799 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200800
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300801 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
802 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200803
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300804 prev = register_pid(sched, prev_pid, prev_comm);
805 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200806
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300807 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200808
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300809 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300810 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300811
812 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200813}
814
David Aherncb627502013-08-07 22:50:47 -0400815static int replay_fork_event(struct perf_sched *sched,
816 union perf_event *event,
817 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200818{
David Aherncb627502013-08-07 22:50:47 -0400819 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300820
Adrian Hunter314add62013-08-27 11:23:03 +0300821 child = machine__findnew_thread(machine, event->fork.pid,
822 event->fork.tid);
823 parent = machine__findnew_thread(machine, event->fork.ppid,
824 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400825
826 if (child == NULL || parent == NULL) {
827 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
828 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300829 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200830 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300831
David Aherncb627502013-08-07 22:50:47 -0400832 if (verbose) {
833 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200834 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
835 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400836 }
837
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200838 register_pid(sched, parent->tid, thread__comm_str(parent));
839 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300840out_put:
841 thread__put(child);
842 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300843 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200844}
845
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200846struct sort_dimension {
847 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200848 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200849 struct list_head list;
850};
851
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200852static int
mingo39aeb522009-09-14 20:04:48 +0200853thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200854{
855 struct sort_dimension *sort;
856 int ret = 0;
857
Ingo Molnarb5fae122009-09-11 12:12:54 +0200858 BUG_ON(list_empty(list));
859
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200860 list_for_each_entry(sort, list, list) {
861 ret = sort->cmp(l, r);
862 if (ret)
863 return ret;
864 }
865
866 return ret;
867}
868
mingo39aeb522009-09-14 20:04:48 +0200869static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200870thread_atoms_search(struct rb_root *root, struct thread *thread,
871 struct list_head *sort_list)
872{
873 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200874 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200875
876 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200877 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200878 int cmp;
879
mingo39aeb522009-09-14 20:04:48 +0200880 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200881
882 cmp = thread_lat_cmp(sort_list, &key, atoms);
883 if (cmp > 0)
884 node = node->rb_left;
885 else if (cmp < 0)
886 node = node->rb_right;
887 else {
888 BUG_ON(thread != atoms->thread);
889 return atoms;
890 }
891 }
892 return NULL;
893}
894
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200895static void
mingo39aeb522009-09-14 20:04:48 +0200896__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200897 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200898{
899 struct rb_node **new = &(root->rb_node), *parent = NULL;
900
901 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200902 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200903 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200904
mingo39aeb522009-09-14 20:04:48 +0200905 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200906 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200907
908 cmp = thread_lat_cmp(sort_list, data, this);
909
910 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200911 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200912 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200913 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200914 }
915
916 rb_link_node(&data->node, parent, new);
917 rb_insert_color(&data->node, root);
918}
919
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300920static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200921{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200922 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300923 if (!atoms) {
924 pr_err("No memory at %s\n", __func__);
925 return -1;
926 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200927
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300928 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200929 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300930 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300931 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200932}
933
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300934static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935{
936 const char *str = TASK_STATE_TO_CHAR_STR;
937
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300938 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200939}
940
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300941static int
mingo39aeb522009-09-14 20:04:48 +0200942add_sched_out_event(struct work_atoms *atoms,
943 char run_state,
944 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200945{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200946 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300947 if (!atom) {
948 pr_err("Non memory at %s", __func__);
949 return -1;
950 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200952 atom->sched_out_time = timestamp;
953
mingo39aeb522009-09-14 20:04:48 +0200954 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200955 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200956 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200957 }
958
mingo39aeb522009-09-14 20:04:48 +0200959 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300960 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200961}
962
963static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300964add_runtime_event(struct work_atoms *atoms, u64 delta,
965 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200966{
967 struct work_atom *atom;
968
969 BUG_ON(list_empty(&atoms->work_list));
970
971 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
972
973 atom->runtime += delta;
974 atoms->total_runtime += delta;
975}
976
977static void
978add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200979{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200980 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200981 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982
mingo39aeb522009-09-14 20:04:48 +0200983 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200984 return;
985
mingo39aeb522009-09-14 20:04:48 +0200986 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200988 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200989 return;
990
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200991 if (timestamp < atom->wake_up_time) {
992 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200993 return;
994 }
995
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200996 atom->state = THREAD_SCHED_IN;
997 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200998
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200999 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001000 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001001 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001002 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001003 atoms->max_lat_at = timestamp;
1004 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001005 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001006}
1007
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001008static int latency_switch_event(struct perf_sched *sched,
1009 struct perf_evsel *evsel,
1010 struct perf_sample *sample,
1011 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001012{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001013 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1014 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1015 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001016 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001017 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001018 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001019 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001020 s64 delta;
1021
mingo39aeb522009-09-14 20:04:48 +02001022 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001023
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001024 timestamp0 = sched->cpu_last_switched[cpu];
1025 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001026 if (timestamp0)
1027 delta = timestamp - timestamp0;
1028 else
1029 delta = 0;
1030
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001031 if (delta < 0) {
1032 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1033 return -1;
1034 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001035
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001036 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1037 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001038 if (sched_out == NULL || sched_in == NULL)
1039 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001040
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001041 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001042 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001043 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001044 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001045 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001046 if (!out_events) {
1047 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001048 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001049 }
mingo39aeb522009-09-14 20:04:48 +02001050 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001051 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001052 return -1;
mingo39aeb522009-09-14 20:04:48 +02001053
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001054 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001055 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001056 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001057 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001058 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001059 if (!in_events) {
1060 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001061 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001062 }
mingo39aeb522009-09-14 20:04:48 +02001063 /*
1064 * Take came in we have not heard about yet,
1065 * add in an initial atom in runnable state:
1066 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001067 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001068 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001069 }
1070 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001071 err = 0;
1072out_put:
1073 thread__put(sched_out);
1074 thread__put(sched_in);
1075 return err;
mingo39aeb522009-09-14 20:04:48 +02001076}
1077
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001078static int latency_runtime_event(struct perf_sched *sched,
1079 struct perf_evsel *evsel,
1080 struct perf_sample *sample,
1081 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001082{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001083 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1084 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001085 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001086 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001087 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001088 int cpu = sample->cpu, err = -1;
1089
1090 if (thread == NULL)
1091 return -1;
mingo39aeb522009-09-14 20:04:48 +02001092
1093 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001094 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001095 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001096 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001097 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001098 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001099 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001100 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001101 }
1102 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001103 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001104 }
1105
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001106 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001107 err = 0;
1108out_put:
1109 thread__put(thread);
1110 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001111}
1112
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001113static int latency_wakeup_event(struct perf_sched *sched,
1114 struct perf_evsel *evsel,
1115 struct perf_sample *sample,
1116 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001117{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001118 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001119 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001120 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001121 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001122 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001123 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001125 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001126 if (wakee == NULL)
1127 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001128 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001129 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001130 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001131 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001132 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001133 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001134 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001135 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001136 }
1137 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001138 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001139 }
1140
mingo39aeb522009-09-14 20:04:48 +02001141 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142
mingo39aeb522009-09-14 20:04:48 +02001143 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001144
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001145 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001146 * As we do not guarantee the wakeup event happens when
1147 * task is out of run queue, also may happen when task is
1148 * on run queue and wakeup only change ->state to TASK_RUNNING,
1149 * then we should not set the ->wake_up_time when wake up a
1150 * task which is on run queue.
1151 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001152 * You WILL be missing events if you've recorded only
1153 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001154 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001155 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001156 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001157 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001158
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001160 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001161 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001162 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001163 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001164
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001165 atom->state = THREAD_WAIT_CPU;
1166 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001167out_ok:
1168 err = 0;
1169out_put:
1170 thread__put(wakee);
1171 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001172}
1173
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001174static int latency_migrate_task_event(struct perf_sched *sched,
1175 struct perf_evsel *evsel,
1176 struct perf_sample *sample,
1177 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001178{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001179 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001180 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001181 struct work_atoms *atoms;
1182 struct work_atom *atom;
1183 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001184 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001185
1186 /*
1187 * Only need to worry about migration when profiling one CPU.
1188 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001189 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001190 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001191
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001192 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001193 if (migrant == NULL)
1194 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001195 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001196 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001197 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001198 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001199 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001200 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001201 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001202 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001203 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001204 }
1205 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001206 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001207 }
1208
1209 BUG_ON(list_empty(&atoms->work_list));
1210
1211 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1212 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1213
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001214 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001215
1216 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001217 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001218 err = 0;
1219out_put:
1220 thread__put(migrant);
1221 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001222}
1223
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001224static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001225{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001226 int i;
1227 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001228 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001229 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001230
mingo39aeb522009-09-14 20:04:48 +02001231 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001232 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001233 /*
1234 * Ignore idle threads:
1235 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001236 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001237 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001238
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001239 sched->all_runtime += work_list->total_runtime;
1240 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001241
Josef Bacik2f80dd42015-05-22 09:18:40 -04001242 if (work_list->num_merged > 1)
1243 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1244 else
1245 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001246
mingo08f69e62009-09-14 18:30:44 +02001247 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001248 printf(" ");
1249
mingo39aeb522009-09-14 20:04:48 +02001250 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001251 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001252
Namhyung Kim99620a52016-10-24 11:02:45 +09001253 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 -03001254 (double)work_list->total_runtime / NSEC_PER_MSEC,
1255 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1256 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001257 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001258}
1259
mingo39aeb522009-09-14 20:04:48 +02001260static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001261{
Jiri Olsa0014de12015-11-02 12:10:25 +01001262 if (l->thread == r->thread)
1263 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001264 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001266 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001267 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001268 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001269}
1270
mingo39aeb522009-09-14 20:04:48 +02001271static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001272{
1273 u64 avgl, avgr;
1274
1275 if (!l->nb_atoms)
1276 return -1;
1277
1278 if (!r->nb_atoms)
1279 return 1;
1280
1281 avgl = l->total_lat / l->nb_atoms;
1282 avgr = r->total_lat / r->nb_atoms;
1283
1284 if (avgl < avgr)
1285 return -1;
1286 if (avgl > avgr)
1287 return 1;
1288
1289 return 0;
1290}
1291
mingo39aeb522009-09-14 20:04:48 +02001292static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001293{
1294 if (l->max_lat < r->max_lat)
1295 return -1;
1296 if (l->max_lat > r->max_lat)
1297 return 1;
1298
1299 return 0;
1300}
1301
mingo39aeb522009-09-14 20:04:48 +02001302static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001303{
1304 if (l->nb_atoms < r->nb_atoms)
1305 return -1;
1306 if (l->nb_atoms > r->nb_atoms)
1307 return 1;
1308
1309 return 0;
1310}
1311
mingo39aeb522009-09-14 20:04:48 +02001312static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001313{
1314 if (l->total_runtime < r->total_runtime)
1315 return -1;
1316 if (l->total_runtime > r->total_runtime)
1317 return 1;
1318
1319 return 0;
1320}
1321
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001322static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001323{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001324 size_t i;
1325 static struct sort_dimension avg_sort_dimension = {
1326 .name = "avg",
1327 .cmp = avg_cmp,
1328 };
1329 static struct sort_dimension max_sort_dimension = {
1330 .name = "max",
1331 .cmp = max_cmp,
1332 };
1333 static struct sort_dimension pid_sort_dimension = {
1334 .name = "pid",
1335 .cmp = pid_cmp,
1336 };
1337 static struct sort_dimension runtime_sort_dimension = {
1338 .name = "runtime",
1339 .cmp = runtime_cmp,
1340 };
1341 static struct sort_dimension switch_sort_dimension = {
1342 .name = "switch",
1343 .cmp = switch_cmp,
1344 };
1345 struct sort_dimension *available_sorts[] = {
1346 &pid_sort_dimension,
1347 &avg_sort_dimension,
1348 &max_sort_dimension,
1349 &switch_sort_dimension,
1350 &runtime_sort_dimension,
1351 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001352
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001353 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001354 if (!strcmp(available_sorts[i]->name, tok)) {
1355 list_add_tail(&available_sorts[i]->list, list);
1356
1357 return 0;
1358 }
1359 }
1360
1361 return -1;
1362}
1363
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001364static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001365{
1366 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001367 struct rb_root *root = &sched->atom_root;
1368again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001369 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001370 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001371 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001372 if (!node)
1373 break;
1374
Josef Bacik2f80dd42015-05-22 09:18:40 -04001375 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001376 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001377 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001378 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001379 if (root == &sched->atom_root) {
1380 root = &sched->merged_atom_root;
1381 goto again;
1382 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001383}
1384
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001385static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001386 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001387 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001388 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001389{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001390 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001391
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001392 if (sched->tp_handler->wakeup_event)
1393 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001394
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001395 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001396}
1397
Jiri Olsaa151a372016-04-12 15:29:29 +02001398union map_priv {
1399 void *ptr;
1400 bool color;
1401};
1402
1403static bool thread__has_color(struct thread *thread)
1404{
1405 union map_priv priv = {
1406 .ptr = thread__priv(thread),
1407 };
1408
1409 return priv.color;
1410}
1411
1412static struct thread*
1413map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1414{
1415 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1416 union map_priv priv = {
1417 .color = false,
1418 };
1419
1420 if (!sched->map.color_pids || !thread || thread__priv(thread))
1421 return thread;
1422
1423 if (thread_map__has(sched->map.color_pids, tid))
1424 priv.color = true;
1425
1426 thread__set_priv(thread, priv.ptr);
1427 return thread;
1428}
1429
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001430static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1431 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001433 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1434 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001435 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001436 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001437 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001438 int i, this_cpu = sample->cpu;
1439 int cpus_nr;
1440 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001441 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001442 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001443
1444 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1445
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001446 if (this_cpu > sched->max_cpu)
1447 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001448
Jiri Olsa99623c62016-04-12 15:29:26 +02001449 if (sched->map.comp) {
1450 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1451 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1452 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1453 new_cpu = true;
1454 }
1455 } else
1456 cpus_nr = sched->max_cpu;
1457
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001458 timestamp0 = sched->cpu_last_switched[this_cpu];
1459 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001460 if (timestamp0)
1461 delta = timestamp - timestamp0;
1462 else
1463 delta = 0;
1464
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001465 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001466 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001467 return -1;
1468 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469
Jiri Olsaa151a372016-04-12 15:29:29 +02001470 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001471 if (sched_in == NULL)
1472 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001473
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001474 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001475
1476 printf(" ");
1477
1478 new_shortname = 0;
1479 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001480 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1481 /*
1482 * Don't allocate a letter-number for swapper:0
1483 * as a shortname. Instead, we use '.' for it.
1484 */
1485 sched_in->shortname[0] = '.';
1486 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001487 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001488 sched_in->shortname[0] = sched->next_shortname1;
1489 sched_in->shortname[1] = sched->next_shortname2;
1490
1491 if (sched->next_shortname1 < 'Z') {
1492 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001493 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001494 sched->next_shortname1 = 'A';
1495 if (sched->next_shortname2 < '9')
1496 sched->next_shortname2++;
1497 else
1498 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001499 }
1500 }
1501 new_shortname = 1;
1502 }
1503
Jiri Olsa99623c62016-04-12 15:29:26 +02001504 for (i = 0; i < cpus_nr; i++) {
1505 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001506 struct thread *curr_thread = sched->curr_thread[cpu];
1507 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001508 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001509
1510 if (curr_thread && thread__has_color(curr_thread))
1511 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001512
Jiri Olsa73643bb2016-04-12 15:29:31 +02001513 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1514 continue;
1515
Jiri Olsacf294f22016-04-12 15:29:30 +02001516 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1517 cpu_color = COLOR_CPUS;
1518
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001519 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001520 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001521 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001522 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001523
Dongsheng6bcab4e2014-05-06 14:39:01 +09001524 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001525 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001526 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001527 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001528 }
1529
Jiri Olsa73643bb2016-04-12 15:29:31 +02001530 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1531 goto out;
1532
Namhyung Kim99620a52016-10-24 11:02:45 +09001533 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1534 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001535 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001536 const char *pid_color = color;
1537
1538 if (thread__has_color(sched_in))
1539 pid_color = COLOR_PIDS;
1540
1541 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001542 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001543 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001544
Jiri Olsa99623c62016-04-12 15:29:26 +02001545 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001546 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001547
Jiri Olsa73643bb2016-04-12 15:29:31 +02001548out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001549 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001550
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001551 thread__put(sched_in);
1552
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001553 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001554}
1555
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001556static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001557 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001558 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001559 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001560{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001561 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001562 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001563 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1564 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001565
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001566 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001567 /*
1568 * Are we trying to switch away a PID that is
1569 * not current?
1570 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001571 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001572 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001573 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001574
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001575 if (sched->tp_handler->switch_event)
1576 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001577
1578 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001579 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001580}
1581
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001582static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001583 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001584 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001585 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001586{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001587 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001588
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001589 if (sched->tp_handler->runtime_event)
1590 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001591
1592 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001593}
1594
David Aherncb627502013-08-07 22:50:47 -04001595static int perf_sched__process_fork_event(struct perf_tool *tool,
1596 union perf_event *event,
1597 struct perf_sample *sample,
1598 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001599{
1600 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1601
David Aherncb627502013-08-07 22:50:47 -04001602 /* run the fork event through the perf machineruy */
1603 perf_event__process_fork(tool, event, sample, machine);
1604
1605 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001606 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001607 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001608
1609 return 0;
1610}
1611
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001612static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001613 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001614 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001615 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001616{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001617 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001618
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001619 if (sched->tp_handler->migrate_task_event)
1620 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001621
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001622 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001623}
1624
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001625typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001626 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001627 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001628 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001629
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001630static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1631 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001632 struct perf_sample *sample,
1633 struct perf_evsel *evsel,
1634 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001635{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001636 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001637
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001638 if (evsel->handler != NULL) {
1639 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001640 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001641 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001642
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001643 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001644}
1645
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001646static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001647{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001648 const struct perf_evsel_str_handler handlers[] = {
1649 { "sched:sched_switch", process_sched_switch_event, },
1650 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1651 { "sched:sched_wakeup", process_sched_wakeup_event, },
1652 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001653 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1654 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001655 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001656 struct perf_data_file file = {
1657 .path = input_name,
1658 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001659 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001660 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001661 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001662
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001663 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001664 if (session == NULL) {
1665 pr_debug("No Memory for session\n");
1666 return -1;
1667 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001668
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001669 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001670
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001671 if (perf_session__set_tracepoints_handlers(session, handlers))
1672 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001673
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001674 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001675 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001676 if (err) {
1677 pr_err("Failed to process events, error %d", err);
1678 goto out_delete;
1679 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001680
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001681 sched->nr_events = session->evlist->stats.nr_events[0];
1682 sched->nr_lost_events = session->evlist->stats.total_lost;
1683 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001684 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001685
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001686 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001687out_delete:
1688 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001689 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001690}
1691
David Ahern49394a22016-11-16 15:06:29 +09001692/*
1693 * scheduling times are printed as msec.usec
1694 */
1695static inline void print_sched_time(unsigned long long nsecs, int width)
1696{
1697 unsigned long msecs;
1698 unsigned long usecs;
1699
1700 msecs = nsecs / NSEC_PER_MSEC;
1701 nsecs -= msecs * NSEC_PER_MSEC;
1702 usecs = nsecs / NSEC_PER_USEC;
1703 printf("%*lu.%03lu ", width, msecs, usecs);
1704}
1705
1706/*
1707 * returns runtime data for event, allocating memory for it the
1708 * first time it is used.
1709 */
1710static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1711{
1712 struct evsel_runtime *r = evsel->priv;
1713
1714 if (r == NULL) {
1715 r = zalloc(sizeof(struct evsel_runtime));
1716 evsel->priv = r;
1717 }
1718
1719 return r;
1720}
1721
1722/*
1723 * save last time event was seen per cpu
1724 */
1725static void perf_evsel__save_time(struct perf_evsel *evsel,
1726 u64 timestamp, u32 cpu)
1727{
1728 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1729
1730 if (r == NULL)
1731 return;
1732
1733 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1734 int i, n = __roundup_pow_of_two(cpu+1);
1735 void *p = r->last_time;
1736
1737 p = realloc(r->last_time, n * sizeof(u64));
1738 if (!p)
1739 return;
1740
1741 r->last_time = p;
1742 for (i = r->ncpu; i < n; ++i)
1743 r->last_time[i] = (u64) 0;
1744
1745 r->ncpu = n;
1746 }
1747
1748 r->last_time[cpu] = timestamp;
1749}
1750
1751/* returns last time this event was seen on the given cpu */
1752static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1753{
1754 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1755
1756 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1757 return 0;
1758
1759 return r->last_time[cpu];
1760}
1761
1762static int comm_width = 20;
1763
1764static char *timehist_get_commstr(struct thread *thread)
1765{
1766 static char str[32];
1767 const char *comm = thread__comm_str(thread);
1768 pid_t tid = thread->tid;
1769 pid_t pid = thread->pid_;
1770 int n;
1771
1772 if (pid == 0)
1773 n = scnprintf(str, sizeof(str), "%s", comm);
1774
1775 else if (tid != pid)
1776 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1777
1778 else
1779 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1780
1781 if (n > comm_width)
1782 comm_width = n;
1783
1784 return str;
1785}
1786
David Aherna407b062016-11-16 15:06:33 +09001787static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001788{
David Aherna407b062016-11-16 15:06:33 +09001789 u32 ncpus = sched->max_cpu + 1;
1790 u32 i, j;
1791
David Ahern49394a22016-11-16 15:06:29 +09001792 printf("%15s %6s ", "time", "cpu");
1793
David Aherna407b062016-11-16 15:06:33 +09001794 if (sched->show_cpu_visual) {
1795 printf(" ");
1796 for (i = 0, j = 0; i < ncpus; ++i) {
1797 printf("%x", j++);
1798 if (j > 15)
1799 j = 0;
1800 }
1801 printf(" ");
1802 }
1803
David Ahern49394a22016-11-16 15:06:29 +09001804 printf(" %-20s %9s %9s %9s",
1805 "task name", "wait time", "sch delay", "run time");
1806
1807 printf("\n");
1808
1809 /*
1810 * units row
1811 */
1812 printf("%15s %-6s ", "", "");
1813
David Aherna407b062016-11-16 15:06:33 +09001814 if (sched->show_cpu_visual)
1815 printf(" %*s ", ncpus, "");
1816
David Ahern49394a22016-11-16 15:06:29 +09001817 printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)");
1818
1819 /*
1820 * separator
1821 */
1822 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1823
David Aherna407b062016-11-16 15:06:33 +09001824 if (sched->show_cpu_visual)
1825 printf(" %.*s ", ncpus, graph_dotted_line);
1826
David Ahern49394a22016-11-16 15:06:29 +09001827 printf(" %.20s %.9s %.9s %.9s",
1828 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1829 graph_dotted_line);
1830
1831 printf("\n");
1832}
1833
David Ahernfc1469f2016-11-16 15:06:31 +09001834static void timehist_print_sample(struct perf_sched *sched,
1835 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001836 struct addr_location *al,
David Ahern49394a22016-11-16 15:06:29 +09001837 struct thread *thread)
1838{
1839 struct thread_runtime *tr = thread__priv(thread);
David Aherna407b062016-11-16 15:06:33 +09001840 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09001841 char tstr[64];
1842
1843 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
1844 printf("%15s [%04d] ", tstr, sample->cpu);
1845
David Aherna407b062016-11-16 15:06:33 +09001846 if (sched->show_cpu_visual) {
1847 u32 i;
1848 char c;
1849
1850 printf(" ");
1851 for (i = 0; i < max_cpus; ++i) {
1852 /* flag idle times with 'i'; others are sched events */
1853 if (i == sample->cpu)
1854 c = (thread->tid == 0) ? 'i' : 's';
1855 else
1856 c = ' ';
1857 printf("%c", c);
1858 }
1859 printf(" ");
1860 }
1861
David Ahern49394a22016-11-16 15:06:29 +09001862 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1863
1864 print_sched_time(tr->dt_wait, 6);
1865 print_sched_time(tr->dt_delay, 6);
1866 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001867
1868 if (sched->show_wakeups)
1869 printf(" %-*s", comm_width, "");
1870
David Ahern6c973c92016-11-16 15:06:32 +09001871 if (thread->tid == 0)
1872 goto out;
1873
1874 if (sched->show_callchain)
1875 printf(" ");
1876
1877 sample__fprintf_sym(sample, al, 0,
1878 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09001879 EVSEL__PRINT_CALLCHAIN_ARROW |
1880 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09001881 &callchain_cursor, stdout);
1882
1883out:
David Ahern49394a22016-11-16 15:06:29 +09001884 printf("\n");
1885}
1886
1887/*
1888 * Explanation of delta-time stats:
1889 *
1890 * t = time of current schedule out event
1891 * tprev = time of previous sched out event
1892 * also time of schedule-in event for current task
1893 * last_time = time of last sched change event for current task
1894 * (i.e, time process was last scheduled out)
1895 * ready_to_run = time of wakeup for current task
1896 *
1897 * -----|------------|------------|------------|------
1898 * last ready tprev t
1899 * time to run
1900 *
1901 * |-------- dt_wait --------|
1902 * |- dt_delay -|-- dt_run --|
1903 *
1904 * dt_run = run time of current task
1905 * dt_wait = time between last schedule out event for task and tprev
1906 * represents time spent off the cpu
1907 * dt_delay = time between wakeup and schedule-in of task
1908 */
1909
1910static void timehist_update_runtime_stats(struct thread_runtime *r,
1911 u64 t, u64 tprev)
1912{
1913 r->dt_delay = 0;
1914 r->dt_wait = 0;
1915 r->dt_run = 0;
1916 if (tprev) {
1917 r->dt_run = t - tprev;
1918 if (r->ready_to_run) {
1919 if (r->ready_to_run > tprev)
1920 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1921 else
1922 r->dt_delay = tprev - r->ready_to_run;
1923 }
1924
1925 if (r->last_time > tprev)
1926 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
1927 else if (r->last_time)
1928 r->dt_wait = tprev - r->last_time;
1929 }
1930
1931 update_stats(&r->run_stats, r->dt_run);
1932 r->total_run_time += r->dt_run;
1933}
1934
David Ahern6c973c92016-11-16 15:06:32 +09001935static bool is_idle_sample(struct perf_sched *sched,
1936 struct perf_sample *sample,
1937 struct perf_evsel *evsel,
1938 struct machine *machine)
David Ahern49394a22016-11-16 15:06:29 +09001939{
David Ahern6c973c92016-11-16 15:06:32 +09001940 struct thread *thread;
1941 struct callchain_cursor *cursor = &callchain_cursor;
1942
David Ahern49394a22016-11-16 15:06:29 +09001943 /* pid 0 == swapper == idle task */
1944 if (sample->pid == 0)
1945 return true;
1946
1947 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) {
1948 if (perf_evsel__intval(evsel, sample, "prev_pid") == 0)
1949 return true;
1950 }
David Ahern6c973c92016-11-16 15:06:32 +09001951
1952 /* want main thread for process - has maps */
1953 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
1954 if (thread == NULL) {
1955 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
1956 return false;
1957 }
1958
1959 if (!symbol_conf.use_callchain || sample->callchain == NULL)
1960 return false;
1961
1962 if (thread__resolve_callchain(thread, cursor, evsel, sample,
1963 NULL, NULL, sched->max_stack) != 0) {
1964 if (verbose)
1965 error("Failed to resolve callchain. Skipping\n");
1966
1967 return false;
1968 }
1969 callchain_cursor_commit(cursor);
David Ahern49394a22016-11-16 15:06:29 +09001970 return false;
1971}
1972
1973/*
1974 * Track idle stats per cpu by maintaining a local thread
1975 * struct for the idle task on each cpu.
1976 */
1977static int init_idle_threads(int ncpu)
1978{
1979 int i;
1980
1981 idle_threads = zalloc(ncpu * sizeof(struct thread *));
1982 if (!idle_threads)
1983 return -ENOMEM;
1984
1985 idle_max_cpu = ncpu - 1;
1986
1987 /* allocate the actual thread struct if needed */
1988 for (i = 0; i < ncpu; ++i) {
1989 idle_threads[i] = thread__new(0, 0);
1990 if (idle_threads[i] == NULL)
1991 return -ENOMEM;
1992
1993 thread__set_comm(idle_threads[i], idle_comm, 0);
1994 }
1995
1996 return 0;
1997}
1998
1999static void free_idle_threads(void)
2000{
2001 int i;
2002
2003 if (idle_threads == NULL)
2004 return;
2005
2006 for (i = 0; i <= idle_max_cpu; ++i) {
2007 if ((idle_threads[i]))
2008 thread__delete(idle_threads[i]);
2009 }
2010
2011 free(idle_threads);
2012}
2013
2014static struct thread *get_idle_thread(int cpu)
2015{
2016 /*
2017 * expand/allocate array of pointers to local thread
2018 * structs if needed
2019 */
2020 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2021 int i, j = __roundup_pow_of_two(cpu+1);
2022 void *p;
2023
2024 p = realloc(idle_threads, j * sizeof(struct thread *));
2025 if (!p)
2026 return NULL;
2027
2028 idle_threads = (struct thread **) p;
2029 i = idle_max_cpu ? idle_max_cpu + 1 : 0;
2030 for (; i < j; ++i)
2031 idle_threads[i] = NULL;
2032
2033 idle_max_cpu = j;
2034 }
2035
2036 /* allocate a new thread struct if needed */
2037 if (idle_threads[cpu] == NULL) {
2038 idle_threads[cpu] = thread__new(0, 0);
2039
2040 if (idle_threads[cpu]) {
2041 idle_threads[cpu]->tid = 0;
2042 thread__set_comm(idle_threads[cpu], idle_comm, 0);
2043 }
2044 }
2045
2046 return idle_threads[cpu];
2047}
2048
2049/*
2050 * handle runtime stats saved per thread
2051 */
2052static struct thread_runtime *thread__init_runtime(struct thread *thread)
2053{
2054 struct thread_runtime *r;
2055
2056 r = zalloc(sizeof(struct thread_runtime));
2057 if (!r)
2058 return NULL;
2059
2060 init_stats(&r->run_stats);
2061 thread__set_priv(thread, r);
2062
2063 return r;
2064}
2065
2066static struct thread_runtime *thread__get_runtime(struct thread *thread)
2067{
2068 struct thread_runtime *tr;
2069
2070 tr = thread__priv(thread);
2071 if (tr == NULL) {
2072 tr = thread__init_runtime(thread);
2073 if (tr == NULL)
2074 pr_debug("Failed to malloc memory for runtime data.\n");
2075 }
2076
2077 return tr;
2078}
2079
David Ahern6c973c92016-11-16 15:06:32 +09002080static struct thread *timehist_get_thread(struct perf_sched *sched,
2081 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002082 struct machine *machine,
2083 struct perf_evsel *evsel)
2084{
2085 struct thread *thread;
2086
David Ahern6c973c92016-11-16 15:06:32 +09002087 if (is_idle_sample(sched, sample, evsel, machine)) {
David Ahern49394a22016-11-16 15:06:29 +09002088 thread = get_idle_thread(sample->cpu);
2089 if (thread == NULL)
2090 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2091
2092 } else {
2093 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2094 if (thread == NULL) {
2095 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2096 sample->tid);
2097 }
2098 }
2099
2100 return thread;
2101}
2102
David Ahern52df1382016-11-16 15:06:30 +09002103static bool timehist_skip_sample(struct perf_sched *sched,
2104 struct thread *thread)
David Ahern49394a22016-11-16 15:06:29 +09002105{
2106 bool rc = false;
2107
David Ahern52df1382016-11-16 15:06:30 +09002108 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002109 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002110 sched->skipped_samples++;
2111 }
David Ahern49394a22016-11-16 15:06:29 +09002112
2113 return rc;
2114}
2115
David Ahernfc1469f2016-11-16 15:06:31 +09002116static void timehist_print_wakeup_event(struct perf_sched *sched,
2117 struct perf_sample *sample,
2118 struct machine *machine,
2119 struct thread *awakened)
2120{
2121 struct thread *thread;
2122 char tstr[64];
2123
2124 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2125 if (thread == NULL)
2126 return;
2127
2128 /* show wakeup unless both awakee and awaker are filtered */
2129 if (timehist_skip_sample(sched, thread) &&
2130 timehist_skip_sample(sched, awakened)) {
2131 return;
2132 }
2133
2134 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2135 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002136 if (sched->show_cpu_visual)
2137 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002138
2139 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2140
2141 /* dt spacer */
2142 printf(" %9s %9s %9s ", "", "", "");
2143
2144 printf("awakened: %s", timehist_get_commstr(awakened));
2145
2146 printf("\n");
2147}
2148
2149static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002150 union perf_event *event __maybe_unused,
2151 struct perf_evsel *evsel,
2152 struct perf_sample *sample,
2153 struct machine *machine)
2154{
David Ahernfc1469f2016-11-16 15:06:31 +09002155 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002156 struct thread *thread;
2157 struct thread_runtime *tr = NULL;
2158 /* want pid of awakened task not pid in sample */
2159 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2160
2161 thread = machine__findnew_thread(machine, 0, pid);
2162 if (thread == NULL)
2163 return -1;
2164
2165 tr = thread__get_runtime(thread);
2166 if (tr == NULL)
2167 return -1;
2168
2169 if (tr->ready_to_run == 0)
2170 tr->ready_to_run = sample->time;
2171
David Ahernfc1469f2016-11-16 15:06:31 +09002172 /* show wakeups if requested */
2173 if (sched->show_wakeups)
2174 timehist_print_wakeup_event(sched, sample, machine, thread);
2175
David Ahern49394a22016-11-16 15:06:29 +09002176 return 0;
2177}
2178
David Ahern52df1382016-11-16 15:06:30 +09002179static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002180 union perf_event *event,
2181 struct perf_evsel *evsel,
2182 struct perf_sample *sample,
2183 struct machine *machine)
2184{
David Ahernfc1469f2016-11-16 15:06:31 +09002185 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002186 struct addr_location al;
2187 struct thread *thread;
2188 struct thread_runtime *tr = NULL;
2189 u64 tprev;
2190 int rc = 0;
2191
2192 if (machine__resolve(machine, &al, sample) < 0) {
2193 pr_err("problem processing %d event. skipping it\n",
2194 event->header.type);
2195 rc = -1;
2196 goto out;
2197 }
2198
David Ahern6c973c92016-11-16 15:06:32 +09002199 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002200 if (thread == NULL) {
2201 rc = -1;
2202 goto out;
2203 }
2204
David Ahern52df1382016-11-16 15:06:30 +09002205 if (timehist_skip_sample(sched, thread))
David Ahern49394a22016-11-16 15:06:29 +09002206 goto out;
2207
2208 tr = thread__get_runtime(thread);
2209 if (tr == NULL) {
2210 rc = -1;
2211 goto out;
2212 }
2213
2214 tprev = perf_evsel__get_time(evsel, sample->cpu);
2215
2216 timehist_update_runtime_stats(tr, sample->time, tprev);
David Ahern52df1382016-11-16 15:06:30 +09002217 if (!sched->summary_only)
David Ahern6c973c92016-11-16 15:06:32 +09002218 timehist_print_sample(sched, sample, &al, thread);
David Ahern49394a22016-11-16 15:06:29 +09002219
2220out:
2221 if (tr) {
2222 /* time of this sched_switch event becomes last time task seen */
2223 tr->last_time = sample->time;
2224
2225 /* sched out event for task so reset ready to run time */
2226 tr->ready_to_run = 0;
2227 }
2228
2229 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2230
2231 return rc;
2232}
2233
2234static int timehist_sched_switch_event(struct perf_tool *tool,
2235 union perf_event *event,
2236 struct perf_evsel *evsel,
2237 struct perf_sample *sample,
2238 struct machine *machine __maybe_unused)
2239{
2240 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2241}
2242
2243static int process_lost(struct perf_tool *tool __maybe_unused,
2244 union perf_event *event,
2245 struct perf_sample *sample,
2246 struct machine *machine __maybe_unused)
2247{
2248 char tstr[64];
2249
2250 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2251 printf("%15s ", tstr);
2252 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2253
2254 return 0;
2255}
2256
2257
David Ahern52df1382016-11-16 15:06:30 +09002258static void print_thread_runtime(struct thread *t,
2259 struct thread_runtime *r)
2260{
2261 double mean = avg_stats(&r->run_stats);
2262 float stddev;
2263
2264 printf("%*s %5d %9" PRIu64 " ",
2265 comm_width, timehist_get_commstr(t), t->ppid,
2266 (u64) r->run_stats.n);
2267
2268 print_sched_time(r->total_run_time, 8);
2269 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2270 print_sched_time(r->run_stats.min, 6);
2271 printf(" ");
2272 print_sched_time((u64) mean, 6);
2273 printf(" ");
2274 print_sched_time(r->run_stats.max, 6);
2275 printf(" ");
2276 printf("%5.2f", stddev);
2277 printf("\n");
2278}
2279
2280struct total_run_stats {
2281 u64 sched_count;
2282 u64 task_count;
2283 u64 total_run_time;
2284};
2285
2286static int __show_thread_runtime(struct thread *t, void *priv)
2287{
2288 struct total_run_stats *stats = priv;
2289 struct thread_runtime *r;
2290
2291 if (thread__is_filtered(t))
2292 return 0;
2293
2294 r = thread__priv(t);
2295 if (r && r->run_stats.n) {
2296 stats->task_count++;
2297 stats->sched_count += r->run_stats.n;
2298 stats->total_run_time += r->total_run_time;
2299 print_thread_runtime(t, r);
2300 }
2301
2302 return 0;
2303}
2304
2305static int show_thread_runtime(struct thread *t, void *priv)
2306{
2307 if (t->dead)
2308 return 0;
2309
2310 return __show_thread_runtime(t, priv);
2311}
2312
2313static int show_deadthread_runtime(struct thread *t, void *priv)
2314{
2315 if (!t->dead)
2316 return 0;
2317
2318 return __show_thread_runtime(t, priv);
2319}
2320
2321static void timehist_print_summary(struct perf_sched *sched,
2322 struct perf_session *session)
2323{
2324 struct machine *m = &session->machines.host;
2325 struct total_run_stats totals;
2326 u64 task_count;
2327 struct thread *t;
2328 struct thread_runtime *r;
2329 int i;
2330
2331 memset(&totals, 0, sizeof(totals));
2332
2333 if (comm_width < 30)
2334 comm_width = 30;
2335
2336 printf("\nRuntime summary\n");
2337 printf("%*s parent sched-in ", comm_width, "comm");
2338 printf(" run-time min-run avg-run max-run stddev\n");
2339 printf("%*s (count) ", comm_width, "");
2340 printf(" (msec) (msec) (msec) (msec) %%\n");
2341 printf("%.105s\n", graph_dotted_line);
2342
2343 machine__for_each_thread(m, show_thread_runtime, &totals);
2344 task_count = totals.task_count;
2345 if (!task_count)
2346 printf("<no still running tasks>\n");
2347
2348 printf("\nTerminated tasks:\n");
2349 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2350 if (task_count == totals.task_count)
2351 printf("<no terminated tasks>\n");
2352
2353 /* CPU idle stats not tracked when samples were skipped */
2354 if (sched->skipped_samples)
2355 return;
2356
2357 printf("\nIdle stats:\n");
2358 for (i = 0; i <= idle_max_cpu; ++i) {
2359 t = idle_threads[i];
2360 if (!t)
2361 continue;
2362
2363 r = thread__priv(t);
2364 if (r && r->run_stats.n) {
2365 totals.sched_count += r->run_stats.n;
2366 printf(" CPU %2d idle for ", i);
2367 print_sched_time(r->total_run_time, 6);
2368 printf(" msec\n");
2369 } else
2370 printf(" CPU %2d idle entire time window\n", i);
2371 }
2372
2373 printf("\n"
2374 " Total number of unique tasks: %" PRIu64 "\n"
2375 "Total number of context switches: %" PRIu64 "\n"
2376 " Total run time (msec): ",
2377 totals.task_count, totals.sched_count);
2378
2379 print_sched_time(totals.total_run_time, 2);
2380 printf("\n");
2381}
2382
David Ahern49394a22016-11-16 15:06:29 +09002383typedef int (*sched_handler)(struct perf_tool *tool,
2384 union perf_event *event,
2385 struct perf_evsel *evsel,
2386 struct perf_sample *sample,
2387 struct machine *machine);
2388
2389static int perf_timehist__process_sample(struct perf_tool *tool,
2390 union perf_event *event,
2391 struct perf_sample *sample,
2392 struct perf_evsel *evsel,
2393 struct machine *machine)
2394{
2395 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2396 int err = 0;
2397 int this_cpu = sample->cpu;
2398
2399 if (this_cpu > sched->max_cpu)
2400 sched->max_cpu = this_cpu;
2401
2402 if (evsel->handler != NULL) {
2403 sched_handler f = evsel->handler;
2404
2405 err = f(tool, event, evsel, sample, machine);
2406 }
2407
2408 return err;
2409}
2410
David Ahern6c973c92016-11-16 15:06:32 +09002411static int timehist_check_attr(struct perf_sched *sched,
2412 struct perf_evlist *evlist)
2413{
2414 struct perf_evsel *evsel;
2415 struct evsel_runtime *er;
2416
2417 list_for_each_entry(evsel, &evlist->entries, node) {
2418 er = perf_evsel__get_runtime(evsel);
2419 if (er == NULL) {
2420 pr_err("Failed to allocate memory for evsel runtime data\n");
2421 return -1;
2422 }
2423
2424 if (sched->show_callchain &&
2425 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2426 pr_info("Samples do not have callchains.\n");
2427 sched->show_callchain = 0;
2428 symbol_conf.use_callchain = 0;
2429 }
2430 }
2431
2432 return 0;
2433}
2434
David Ahern49394a22016-11-16 15:06:29 +09002435static int perf_sched__timehist(struct perf_sched *sched)
2436{
2437 const struct perf_evsel_str_handler handlers[] = {
2438 { "sched:sched_switch", timehist_sched_switch_event, },
2439 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2440 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2441 };
2442 struct perf_data_file file = {
2443 .path = input_name,
2444 .mode = PERF_DATA_MODE_READ,
2445 };
2446
2447 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002448 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002449 int err = -1;
2450
2451 /*
2452 * event handlers for timehist option
2453 */
2454 sched->tool.sample = perf_timehist__process_sample;
2455 sched->tool.mmap = perf_event__process_mmap;
2456 sched->tool.comm = perf_event__process_comm;
2457 sched->tool.exit = perf_event__process_exit;
2458 sched->tool.fork = perf_event__process_fork;
2459 sched->tool.lost = process_lost;
2460 sched->tool.attr = perf_event__process_attr;
2461 sched->tool.tracing_data = perf_event__process_tracing_data;
2462 sched->tool.build_id = perf_event__process_build_id;
2463
2464 sched->tool.ordered_events = true;
2465 sched->tool.ordering_requires_timestamps = true;
2466
David Ahern6c973c92016-11-16 15:06:32 +09002467 symbol_conf.use_callchain = sched->show_callchain;
2468
David Ahern49394a22016-11-16 15:06:29 +09002469 session = perf_session__new(&file, false, &sched->tool);
2470 if (session == NULL)
2471 return -ENOMEM;
2472
David Ahern52df1382016-11-16 15:06:30 +09002473 evlist = session->evlist;
2474
David Ahern49394a22016-11-16 15:06:29 +09002475 symbol__init(&session->header.env);
2476
David Ahern6c973c92016-11-16 15:06:32 +09002477 if (timehist_check_attr(sched, evlist) != 0)
2478 goto out;
2479
David Ahern49394a22016-11-16 15:06:29 +09002480 setup_pager();
2481
2482 /* setup per-evsel handlers */
2483 if (perf_session__set_tracepoints_handlers(session, handlers))
2484 goto out;
2485
2486 if (!perf_session__has_traces(session, "record -R"))
2487 goto out;
2488
2489 /* pre-allocate struct for per-CPU idle stats */
2490 sched->max_cpu = session->header.env.nr_cpus_online;
2491 if (sched->max_cpu == 0)
2492 sched->max_cpu = 4;
2493 if (init_idle_threads(sched->max_cpu))
2494 goto out;
2495
David Ahern52df1382016-11-16 15:06:30 +09002496 /* summary_only implies summary option, but don't overwrite summary if set */
2497 if (sched->summary_only)
2498 sched->summary = sched->summary_only;
2499
2500 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09002501 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09002502
2503 err = perf_session__process_events(session);
2504 if (err) {
2505 pr_err("Failed to process events, error %d", err);
2506 goto out;
2507 }
2508
David Ahern52df1382016-11-16 15:06:30 +09002509 sched->nr_events = evlist->stats.nr_events[0];
2510 sched->nr_lost_events = evlist->stats.total_lost;
2511 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2512
2513 if (sched->summary)
2514 timehist_print_summary(sched, session);
2515
David Ahern49394a22016-11-16 15:06:29 +09002516out:
2517 free_idle_threads();
2518 perf_session__delete(session);
2519
2520 return err;
2521}
2522
2523
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002524static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002525{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002526 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002527 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002528 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2529 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002530 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002531 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002532 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002533 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2534 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002535 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002536 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002537 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002538 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2539 sched->nr_context_switch_bugs, sched->nr_timestamps);
2540 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002541 printf(" (due to lost events?)");
2542 printf("\n");
2543 }
2544}
2545
Josef Bacik2f80dd42015-05-22 09:18:40 -04002546static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2547{
2548 struct rb_node **new = &(root->rb_node), *parent = NULL;
2549 struct work_atoms *this;
2550 const char *comm = thread__comm_str(data->thread), *this_comm;
2551
2552 while (*new) {
2553 int cmp;
2554
2555 this = container_of(*new, struct work_atoms, node);
2556 parent = *new;
2557
2558 this_comm = thread__comm_str(this->thread);
2559 cmp = strcmp(comm, this_comm);
2560 if (cmp > 0) {
2561 new = &((*new)->rb_left);
2562 } else if (cmp < 0) {
2563 new = &((*new)->rb_right);
2564 } else {
2565 this->num_merged++;
2566 this->total_runtime += data->total_runtime;
2567 this->nb_atoms += data->nb_atoms;
2568 this->total_lat += data->total_lat;
2569 list_splice(&data->work_list, &this->work_list);
2570 if (this->max_lat < data->max_lat) {
2571 this->max_lat = data->max_lat;
2572 this->max_lat_at = data->max_lat_at;
2573 }
2574 zfree(&data);
2575 return;
2576 }
2577 }
2578
2579 data->num_merged++;
2580 rb_link_node(&data->node, parent, new);
2581 rb_insert_color(&data->node, root);
2582}
2583
2584static void perf_sched__merge_lat(struct perf_sched *sched)
2585{
2586 struct work_atoms *data;
2587 struct rb_node *node;
2588
2589 if (sched->skip_merge)
2590 return;
2591
2592 while ((node = rb_first(&sched->atom_root))) {
2593 rb_erase(node, &sched->atom_root);
2594 data = rb_entry(node, struct work_atoms, node);
2595 __merge_work_atoms(&sched->merged_atom_root, data);
2596 }
2597}
2598
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002599static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002600{
2601 struct rb_node *next;
2602
2603 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002604
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002605 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002606 return -1;
David Ahernad9def72013-08-07 22:50:44 -04002607
Josef Bacik2f80dd42015-05-22 09:18:40 -04002608 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002609 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002610
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002611 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
2612 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
2613 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002614
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002615 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002616
2617 while (next) {
2618 struct work_atoms *work_list;
2619
2620 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002621 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002622 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002623 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002624 }
2625
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002626 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002627 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03002628 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002629
2630 printf(" ---------------------------------------------------\n");
2631
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002632 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002633 printf("\n");
2634
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002635 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002636}
2637
Jiri Olsa99623c62016-04-12 15:29:26 +02002638static int setup_map_cpus(struct perf_sched *sched)
2639{
Jiri Olsa73643bb2016-04-12 15:29:31 +02002640 struct cpu_map *map;
2641
Jiri Olsa99623c62016-04-12 15:29:26 +02002642 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
2643
2644 if (sched->map.comp) {
2645 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02002646 if (!sched->map.comp_cpus)
2647 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02002648 }
2649
Jiri Olsa73643bb2016-04-12 15:29:31 +02002650 if (!sched->map.cpus_str)
2651 return 0;
2652
2653 map = cpu_map__new(sched->map.cpus_str);
2654 if (!map) {
2655 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
2656 return -1;
2657 }
2658
2659 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02002660 return 0;
2661}
2662
Jiri Olsaa151a372016-04-12 15:29:29 +02002663static int setup_color_pids(struct perf_sched *sched)
2664{
2665 struct thread_map *map;
2666
2667 if (!sched->map.color_pids_str)
2668 return 0;
2669
2670 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
2671 if (!map) {
2672 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
2673 return -1;
2674 }
2675
2676 sched->map.color_pids = map;
2677 return 0;
2678}
2679
Jiri Olsacf294f22016-04-12 15:29:30 +02002680static int setup_color_cpus(struct perf_sched *sched)
2681{
2682 struct cpu_map *map;
2683
2684 if (!sched->map.color_cpus_str)
2685 return 0;
2686
2687 map = cpu_map__new(sched->map.color_cpus_str);
2688 if (!map) {
2689 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
2690 return -1;
2691 }
2692
2693 sched->map.color_cpus = map;
2694 return 0;
2695}
2696
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002697static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002698{
Jiri Olsa99623c62016-04-12 15:29:26 +02002699 if (setup_map_cpus(sched))
2700 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02002701
Jiri Olsaa151a372016-04-12 15:29:29 +02002702 if (setup_color_pids(sched))
2703 return -1;
2704
Jiri Olsacf294f22016-04-12 15:29:30 +02002705 if (setup_color_cpus(sched))
2706 return -1;
2707
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002708 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002709 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002710 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002711 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002712 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002713}
2714
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002715static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002716{
2717 unsigned long i;
2718
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002719 calibrate_run_measurement_overhead(sched);
2720 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002721
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002722 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002723
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002724 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002725 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002727 printf("nr_run_events: %ld\n", sched->nr_run_events);
2728 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
2729 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002730
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002731 if (sched->targetless_wakeups)
2732 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
2733 if (sched->multitarget_wakeups)
2734 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
2735 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002736 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002737 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002738
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002739 print_task_traces(sched);
2740 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002741
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002742 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002743 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002744 for (i = 0; i < sched->replay_repeat; i++)
2745 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002746
2747 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002748}
2749
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002750static void setup_sorting(struct perf_sched *sched, const struct option *options,
2751 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002752{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002753 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002754
2755 for (tok = strtok_r(str, ", ", &tmp);
2756 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002757 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002758 usage_with_options_msg(usage_msg, options,
2759 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002760 }
2761 }
2762
2763 free(str);
2764
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002765 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002766}
2767
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002768static int __cmd_record(int argc, const char **argv)
2769{
2770 unsigned int rec_argc, i, j;
2771 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002772 const char * const record_args[] = {
2773 "record",
2774 "-a",
2775 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002776 "-m", "1024",
2777 "-c", "1",
2778 "-e", "sched:sched_switch",
2779 "-e", "sched:sched_stat_wait",
2780 "-e", "sched:sched_stat_sleep",
2781 "-e", "sched:sched_stat_iowait",
2782 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002783 "-e", "sched:sched_process_fork",
2784 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09002785 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002786 "-e", "sched:sched_migrate_task",
2787 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002788
2789 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
2790 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2791
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02002792 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11002793 return -ENOMEM;
2794
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002795 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2796 rec_argv[i] = strdup(record_args[i]);
2797
2798 for (j = 1; j < (unsigned int)argc; j++, i++)
2799 rec_argv[i] = argv[j];
2800
2801 BUG_ON(i != rec_argc);
2802
2803 return cmd_record(i, rec_argv, NULL);
2804}
2805
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002806int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002807{
Adrian Hunter8a39df82013-10-22 10:34:15 +03002808 const char default_sort_order[] = "avg, max, switch, runtime";
2809 struct perf_sched sched = {
2810 .tool = {
2811 .sample = perf_sched__process_tracepoint_sample,
2812 .comm = perf_event__process_comm,
2813 .lost = perf_event__process_lost,
2814 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002815 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002816 },
2817 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
2818 .sort_list = LIST_HEAD_INIT(sched.sort_list),
2819 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
2820 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002821 .sort_order = default_sort_order,
2822 .replay_repeat = 10,
2823 .profile_cpu = -1,
2824 .next_shortname1 = 'A',
2825 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04002826 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09002827 .show_callchain = 1,
2828 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002829 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09002830 const struct option sched_options[] = {
2831 OPT_STRING('i', "input", &input_name, "file",
2832 "input file name"),
2833 OPT_INCR('v', "verbose", &verbose,
2834 "be more verbose (show symbol address, etc)"),
2835 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2836 "dump raw trace in ASCII"),
2837 OPT_END()
2838 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002839 const struct option latency_options[] = {
2840 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
2841 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002842 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
2843 "CPU to profile on"),
2844 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2845 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04002846 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
2847 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002848 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002849 };
2850 const struct option replay_options[] = {
2851 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
2852 "repeat the workload replay N times (-1: infinite)"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002853 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2854 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08002855 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002856 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002857 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002858 const struct option map_options[] = {
2859 OPT_BOOLEAN(0, "compact", &sched.map.comp,
2860 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02002861 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
2862 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02002863 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
2864 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02002865 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
2866 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002867 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02002868 };
David Ahern49394a22016-11-16 15:06:29 +09002869 const struct option timehist_options[] = {
2870 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2871 "file", "vmlinux pathname"),
2872 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2873 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09002874 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
2875 "Display call chains if present (default on)"),
2876 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
2877 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09002878 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2879 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09002880 OPT_BOOLEAN('s', "summary", &sched.summary_only,
2881 "Show only syscall summary with statistics"),
2882 OPT_BOOLEAN('S', "with-summary", &sched.summary,
2883 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09002884 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Aherna407b062016-11-16 15:06:33 +09002885 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
David Ahern49394a22016-11-16 15:06:29 +09002886 OPT_PARENT(sched_options)
2887 };
2888
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002889 const char * const latency_usage[] = {
2890 "perf sched latency [<options>]",
2891 NULL
2892 };
2893 const char * const replay_usage[] = {
2894 "perf sched replay [<options>]",
2895 NULL
2896 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002897 const char * const map_usage[] = {
2898 "perf sched map [<options>]",
2899 NULL
2900 };
David Ahern49394a22016-11-16 15:06:29 +09002901 const char * const timehist_usage[] = {
2902 "perf sched timehist [<options>]",
2903 NULL
2904 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002905 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09002906 "replay", "script",
2907 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002908 const char *sched_usage[] = {
2909 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002910 NULL
2911 };
2912 struct trace_sched_handler lat_ops = {
2913 .wakeup_event = latency_wakeup_event,
2914 .switch_event = latency_switch_event,
2915 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002916 .migrate_task_event = latency_migrate_task_event,
2917 };
2918 struct trace_sched_handler map_ops = {
2919 .switch_event = map_switch_event,
2920 };
2921 struct trace_sched_handler replay_ops = {
2922 .wakeup_event = replay_wakeup_event,
2923 .switch_event = replay_switch_event,
2924 .fork_event = replay_fork_event,
2925 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002926 unsigned int i;
2927
2928 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2929 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002930
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002931 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2932 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002933 if (!argc)
2934 usage_with_options(sched_usage, sched_options);
2935
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002936 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002937 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002938 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002939 if (!strcmp(argv[0], "script"))
2940 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002941
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002942 if (!strncmp(argv[0], "rec", 3)) {
2943 return __cmd_record(argc, argv);
2944 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002945 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002946 if (argc > 1) {
2947 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2948 if (argc)
2949 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002950 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002951 setup_sorting(&sched, latency_options, latency_usage);
2952 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002953 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002954 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002955 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002956 if (argc)
2957 usage_with_options(map_usage, map_options);
2958 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002959 sched.tp_handler = &map_ops;
2960 setup_sorting(&sched, latency_options, latency_usage);
2961 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002962 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002963 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002964 if (argc) {
2965 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2966 if (argc)
2967 usage_with_options(replay_usage, replay_options);
2968 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002969 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09002970 } else if (!strcmp(argv[0], "timehist")) {
2971 if (argc) {
2972 argc = parse_options(argc, argv, timehist_options,
2973 timehist_usage, 0);
2974 if (argc)
2975 usage_with_options(timehist_usage, timehist_options);
2976 }
David Ahernfc1469f2016-11-16 15:06:31 +09002977 if (sched.show_wakeups && sched.summary_only) {
2978 pr_err(" Error: -s and -w are mutually exclusive.\n");
2979 parse_options_usage(timehist_usage, timehist_options, "s", true);
2980 parse_options_usage(NULL, timehist_options, "w", true);
2981 return -EINVAL;
2982 }
2983
David Ahern49394a22016-11-16 15:06:29 +09002984 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002985 } else {
2986 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002987 }
2988
Ingo Molnarec156762009-09-11 12:12:54 +02002989 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002990}