blob: a8ac766021872afc06ac5bf672445984f2b195f9 [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"
David Ahern853b7402016-11-29 10:15:44 -070018#include "util/time-utils.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020019
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060020#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020021#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020022
Ingo Molnar0a02ad92009-09-11 12:12:54 +020023#include "util/debug.h"
24
David Ahern49394a22016-11-16 15:06:29 +090025#include <linux/log2.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020026#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020027#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020028
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020029#include <semaphore.h>
30#include <pthread.h>
31#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080032#include <api/fs/fs.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030033#include <linux/time64.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020034
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020035#define PR_SET_NAME 15 /* Set process name */
36#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020037#define COMM_LEN 20
38#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080039#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020040
mingo39aeb522009-09-14 20:04:48 +020041struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020042
43struct task_desc {
44 unsigned long nr;
45 unsigned long pid;
46 char comm[COMM_LEN];
47
48 unsigned long nr_events;
49 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020050 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020051
52 pthread_t thread;
53 sem_t sleep_sem;
54
55 sem_t ready_for_work;
56 sem_t work_done_sem;
57
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020058 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020059};
60
61enum sched_event_type {
62 SCHED_EVENT_RUN,
63 SCHED_EVENT_SLEEP,
64 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020065 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020066};
67
mingo39aeb522009-09-14 20:04:48 +020068struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020069 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030070 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020071 u64 timestamp;
72 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020073 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020074 sem_t *wait_sem;
75 struct task_desc *wakee;
76};
77
Dongshenge936e8e2014-05-05 16:05:54 +090078#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020079
Namhyung Kim941bdea2017-01-13 19:45:21 +090080/* task state bitmask, copied from include/linux/sched.h */
81#define TASK_RUNNING 0
82#define TASK_INTERRUPTIBLE 1
83#define TASK_UNINTERRUPTIBLE 2
84#define __TASK_STOPPED 4
85#define __TASK_TRACED 8
86/* in tsk->exit_state */
87#define EXIT_DEAD 16
88#define EXIT_ZOMBIE 32
89#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
90/* in tsk->state again */
91#define TASK_DEAD 64
92#define TASK_WAKEKILL 128
93#define TASK_WAKING 256
94#define TASK_PARKED 512
95
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020096enum thread_state {
97 THREAD_SLEEPING = 0,
98 THREAD_WAIT_CPU,
99 THREAD_SCHED_IN,
100 THREAD_IGNORE
101};
102
103struct work_atom {
104 struct list_head list;
105 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200106 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200107 u64 wake_up_time;
108 u64 sched_in_time;
109 u64 runtime;
110};
111
mingo39aeb522009-09-14 20:04:48 +0200112struct work_atoms {
113 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200114 struct thread *thread;
115 struct rb_node node;
116 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100117 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200118 u64 total_lat;
119 u64 nb_atoms;
120 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400121 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200122};
123
mingo39aeb522009-09-14 20:04:48 +0200124typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200125
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300126struct perf_sched;
127
128struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300129 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
130 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300131
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300132 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
133 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300134
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300135 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
136 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300137
David Aherncb627502013-08-07 22:50:47 -0400138 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
139 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
140 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300141
142 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300143 struct perf_evsel *evsel,
144 struct perf_sample *sample,
145 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300146};
147
Jiri Olsaa151a372016-04-12 15:29:29 +0200148#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200149#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200150
Jiri Olsa99623c62016-04-12 15:29:26 +0200151struct perf_sched_map {
152 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
153 int *comp_cpus;
154 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200155 struct thread_map *color_pids;
156 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200157 struct cpu_map *color_cpus;
158 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200159 struct cpu_map *cpus;
160 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200161};
162
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300163struct perf_sched {
164 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300165 const char *sort_order;
166 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800167 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300168 struct task_desc **tasks;
169 const struct trace_sched_handler *tp_handler;
170 pthread_mutex_t start_work_mutex;
171 pthread_mutex_t work_done_wait_mutex;
172 int profile_cpu;
173/*
174 * Track the current task - that way we can know whether there's any
175 * weird events, such as a task being switched away that is not current.
176 */
177 int max_cpu;
178 u32 curr_pid[MAX_CPUS];
179 struct thread *curr_thread[MAX_CPUS];
180 char next_shortname1;
181 char next_shortname2;
182 unsigned int replay_repeat;
183 unsigned long nr_run_events;
184 unsigned long nr_sleep_events;
185 unsigned long nr_wakeup_events;
186 unsigned long nr_sleep_corrections;
187 unsigned long nr_run_events_optimized;
188 unsigned long targetless_wakeups;
189 unsigned long multitarget_wakeups;
190 unsigned long nr_runs;
191 unsigned long nr_timestamps;
192 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300193 unsigned long nr_context_switch_bugs;
194 unsigned long nr_events;
195 unsigned long nr_lost_chunks;
196 unsigned long nr_lost_events;
197 u64 run_measurement_overhead;
198 u64 sleep_measurement_overhead;
199 u64 start_time;
200 u64 cpu_usage;
201 u64 runavg_cpu_usage;
202 u64 parent_cpu_usage;
203 u64 runavg_parent_cpu_usage;
204 u64 sum_runtime;
205 u64 sum_fluct;
206 u64 run_avg;
207 u64 all_runtime;
208 u64 all_count;
209 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400210 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300211 struct list_head sort_list, cmp_pid;
Yunlong Song939cda5212015-03-31 21:46:34 +0800212 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400213 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200214 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900215
216 /* options for timehist command */
217 bool summary;
218 bool summary_only;
Namhyung Kim699b5b92016-12-08 23:47:52 +0900219 bool idle_hist;
David Ahern6c973c92016-11-16 15:06:32 +0900220 bool show_callchain;
221 unsigned int max_stack;
David Aherna407b062016-11-16 15:06:33 +0900222 bool show_cpu_visual;
David Ahernfc1469f2016-11-16 15:06:31 +0900223 bool show_wakeups;
David Ahern350f54f2016-11-25 09:28:41 -0700224 bool show_migrations;
Namhyung Kim414e0502017-01-13 19:45:22 +0900225 bool show_state;
David Ahern52df1382016-11-16 15:06:30 +0900226 u64 skipped_samples;
David Ahern853b7402016-11-29 10:15:44 -0700227 const char *time_str;
228 struct perf_time_interval ptime;
Namhyung Kim9396c9c2016-12-22 15:03:50 +0900229 struct perf_time_interval hist_time;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300230};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200231
David Ahern49394a22016-11-16 15:06:29 +0900232/* per thread run time data */
233struct thread_runtime {
234 u64 last_time; /* time of previous sched in/out event */
235 u64 dt_run; /* run time */
Namhyung Kim941bdea2017-01-13 19:45:21 +0900236 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
237 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
238 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
David Ahern49394a22016-11-16 15:06:29 +0900239 u64 dt_delay; /* time between wakeup and sched-in */
240 u64 ready_to_run; /* time of wakeup */
241
242 struct stats run_stats;
243 u64 total_run_time;
David Ahern350f54f2016-11-25 09:28:41 -0700244
Namhyung Kim941bdea2017-01-13 19:45:21 +0900245 int last_state;
David Ahern350f54f2016-11-25 09:28:41 -0700246 u64 migrations;
David Ahern49394a22016-11-16 15:06:29 +0900247};
248
249/* per event run time data */
250struct evsel_runtime {
251 u64 *last_time; /* time this event was last seen per cpu */
252 u32 ncpu; /* highest cpu slot allocated */
253};
254
Namhyung Kim3bc2fa92016-12-08 23:47:51 +0900255/* per cpu idle time data */
256struct idle_thread_runtime {
257 struct thread_runtime tr;
258 struct thread *last_thread;
259 struct rb_root sorted_root;
260 struct callchain_root callchain;
261 struct callchain_cursor cursor;
262};
263
David Ahern49394a22016-11-16 15:06:29 +0900264/* track idle times per cpu */
265static struct thread **idle_threads;
266static int idle_max_cpu;
267static char idle_comm[] = "<idle>";
268
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200269static u64 get_nsecs(void)
270{
271 struct timespec ts;
272
273 clock_gettime(CLOCK_MONOTONIC, &ts);
274
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300275 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200276}
277
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300278static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200279{
280 u64 T0 = get_nsecs(), T1;
281
282 do {
283 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300284 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200285}
286
287static void sleep_nsecs(u64 nsecs)
288{
289 struct timespec ts;
290
291 ts.tv_nsec = nsecs % 999999999;
292 ts.tv_sec = nsecs / 999999999;
293
294 nanosleep(&ts, NULL);
295}
296
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300297static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200298{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300299 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200300 int i;
301
302 for (i = 0; i < 10; i++) {
303 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300304 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200305 T1 = get_nsecs();
306 delta = T1-T0;
307 min_delta = min(min_delta, delta);
308 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300309 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200310
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200311 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200312}
313
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300314static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200315{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300316 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200317 int i;
318
319 for (i = 0; i < 10; i++) {
320 T0 = get_nsecs();
321 sleep_nsecs(10000);
322 T1 = get_nsecs();
323 delta = T1-T0;
324 min_delta = min(min_delta, delta);
325 }
326 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300327 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200328
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200329 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200330}
331
mingo39aeb522009-09-14 20:04:48 +0200332static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200333get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200334{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200335 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200336 unsigned long idx = task->nr_events;
337 size_t size;
338
339 event->timestamp = timestamp;
340 event->nr = idx;
341
342 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200343 size = sizeof(struct sched_atom *) * task->nr_events;
344 task->atoms = realloc(task->atoms, size);
345 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200346
mingo39aeb522009-09-14 20:04:48 +0200347 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200348
349 return event;
350}
351
mingo39aeb522009-09-14 20:04:48 +0200352static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200353{
354 if (!task->nr_events)
355 return NULL;
356
mingo39aeb522009-09-14 20:04:48 +0200357 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200358}
359
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300360static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
361 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200362{
mingo39aeb522009-09-14 20:04:48 +0200363 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200364
365 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200366 * optimize an existing RUN event by merging this one
367 * to it:
368 */
Ingo Molnarec156762009-09-11 12:12:54 +0200369 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300370 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200371 curr_event->duration += duration;
372 return;
373 }
374
375 event = get_new_event(task, timestamp);
376
377 event->type = SCHED_EVENT_RUN;
378 event->duration = duration;
379
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300380 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200381}
382
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300383static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
384 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200385{
mingo39aeb522009-09-14 20:04:48 +0200386 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200387
388 event = get_new_event(task, timestamp);
389 event->type = SCHED_EVENT_WAKEUP;
390 event->wakee = wakee;
391
392 wakee_event = last_event(wakee);
393 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300394 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200395 return;
396 }
397 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300398 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200399 return;
400 }
401
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200402 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200403 sem_init(wakee_event->wait_sem, 0, 0);
404 wakee_event->specific_wait = 1;
405 event->wait_sem = wakee_event->wait_sem;
406
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300407 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200408}
409
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300410static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
411 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200412{
mingo39aeb522009-09-14 20:04:48 +0200413 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200414
415 event->type = SCHED_EVENT_SLEEP;
416
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300417 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200418}
419
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300420static struct task_desc *register_pid(struct perf_sched *sched,
421 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200422{
423 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800424 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200425
Yunlong Songcb06ac22015-03-31 21:46:30 +0800426 if (sched->pid_to_task == NULL) {
427 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
428 pid_max = MAX_PID;
429 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
430 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800431 if (pid >= (unsigned long)pid_max) {
432 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
433 sizeof(struct task_desc *))) == NULL);
434 while (pid >= (unsigned long)pid_max)
435 sched->pid_to_task[pid_max++] = NULL;
436 }
Ingo Molnarec156762009-09-11 12:12:54 +0200437
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300438 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200439
440 if (task)
441 return task;
442
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200443 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200444 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300445 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200446 strcpy(task->comm, comm);
447 /*
448 * every task starts in sleeping state - this gets ignored
449 * if there's no wakeup pointing to this sleep state:
450 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300451 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200452
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300453 sched->pid_to_task[pid] = task;
454 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800455 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300456 BUG_ON(!sched->tasks);
457 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200458
Ingo Molnarad236fd2009-09-11 12:12:54 +0200459 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300460 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200461
462 return task;
463}
464
465
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300466static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200467{
468 struct task_desc *task;
469 unsigned long i;
470
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300471 for (i = 0; i < sched->nr_tasks; i++) {
472 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200473 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200474 task->nr, task->comm, task->pid, task->nr_events);
475 }
476}
477
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300478static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200479{
480 struct task_desc *task1, *task2;
481 unsigned long i, j;
482
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300483 for (i = 0; i < sched->nr_tasks; i++) {
484 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200485 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300486 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200487 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300488 task2 = sched->tasks[j];
489 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200490 }
491}
492
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300493static void perf_sched__process_event(struct perf_sched *sched,
494 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200495{
496 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200497
mingo39aeb522009-09-14 20:04:48 +0200498 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200499 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300500 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200501 break;
502 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200503 if (atom->wait_sem)
504 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200505 BUG_ON(ret);
506 break;
507 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200508 if (atom->wait_sem)
509 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200510 BUG_ON(ret);
511 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200512 case SCHED_EVENT_MIGRATION:
513 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200514 default:
515 BUG_ON(1);
516 }
517}
518
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200519static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200520{
521 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200522 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200523 int err;
524
525 err = getrusage(RUSAGE_SELF, &ru);
526 BUG_ON(err);
527
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300528 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
529 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200530
531 return sum;
532}
533
Yunlong Song939cda5212015-03-31 21:46:34 +0800534static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200535{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800536 struct perf_event_attr attr;
Yunlong Song939cda5212015-03-31 21:46:34 +0800537 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800538 int fd;
Yunlong Song939cda5212015-03-31 21:46:34 +0800539 struct rlimit limit;
540 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800541
542 memset(&attr, 0, sizeof(attr));
543
544 attr.type = PERF_TYPE_SOFTWARE;
545 attr.config = PERF_COUNT_SW_TASK_CLOCK;
546
Yunlong Song939cda5212015-03-31 21:46:34 +0800547force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200548 fd = sys_perf_event_open(&attr, 0, -1, -1,
549 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800550
Yunlong Song1aff59b2015-03-31 21:46:33 +0800551 if (fd < 0) {
Yunlong Song939cda5212015-03-31 21:46:34 +0800552 if (errno == EMFILE) {
553 if (sched->force) {
554 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
555 limit.rlim_cur += sched->nr_tasks - cur_task;
556 if (limit.rlim_cur > limit.rlim_max) {
557 limit.rlim_max = limit.rlim_cur;
558 need_privilege = true;
559 }
560 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
561 if (need_privilege && errno == EPERM)
562 strcpy(info, "Need privilege\n");
563 } else
564 goto force_again;
565 } else
566 strcpy(info, "Have a try with -f option\n");
567 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900568 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda5212015-03-31 21:46:34 +0800569 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300570 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800571 exit(EXIT_FAILURE);
572 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800573 return fd;
574}
575
576static u64 get_cpu_usage_nsec_self(int fd)
577{
578 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200579 int ret;
580
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800581 ret = read(fd, &runtime, sizeof(runtime));
582 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200583
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800584 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200585}
586
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300587struct sched_thread_parms {
588 struct task_desc *task;
589 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800590 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300591};
592
Ingo Molnarec156762009-09-11 12:12:54 +0200593static void *thread_func(void *ctx)
594{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595 struct sched_thread_parms *parms = ctx;
596 struct task_desc *this_task = parms->task;
597 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200598 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200599 unsigned long i, ret;
600 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800601 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200602
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300603 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300604
Ingo Molnarec156762009-09-11 12:12:54 +0200605 sprintf(comm2, ":%s", this_task->comm);
606 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300607 if (fd < 0)
608 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200609again:
610 ret = sem_post(&this_task->ready_for_work);
611 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200613 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300614 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200615 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200616
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800617 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200618
619 for (i = 0; i < this_task->nr_events; i++) {
620 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300621 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200622 }
623
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800624 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200625 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200626 ret = sem_post(&this_task->work_done_sem);
627 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200628
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300629 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200630 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300631 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200632 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200633
634 goto again;
635}
636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200638{
639 struct task_desc *task;
640 pthread_attr_t attr;
641 unsigned long i;
642 int err;
643
644 err = pthread_attr_init(&attr);
645 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200646 err = pthread_attr_setstacksize(&attr,
647 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200648 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300649 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200650 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300651 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200652 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300653 for (i = 0; i < sched->nr_tasks; i++) {
654 struct sched_thread_parms *parms = malloc(sizeof(*parms));
655 BUG_ON(parms == NULL);
656 parms->task = task = sched->tasks[i];
657 parms->sched = sched;
Yunlong Song939cda5212015-03-31 21:46:34 +0800658 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200659 sem_init(&task->sleep_sem, 0, 0);
660 sem_init(&task->ready_for_work, 0, 0);
661 sem_init(&task->work_done_sem, 0, 0);
662 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300663 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200664 BUG_ON(err);
665 }
666}
667
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300668static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200669{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200670 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200671 struct task_desc *task;
672 unsigned long i, ret;
673
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300674 sched->start_time = get_nsecs();
675 sched->cpu_usage = 0;
676 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200677
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300678 for (i = 0; i < sched->nr_tasks; i++) {
679 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200680 ret = sem_wait(&task->ready_for_work);
681 BUG_ON(ret);
682 sem_init(&task->ready_for_work, 0, 0);
683 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200685 BUG_ON(ret);
686
687 cpu_usage_0 = get_cpu_usage_nsec_parent();
688
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200690
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300691 for (i = 0; i < sched->nr_tasks; i++) {
692 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200693 ret = sem_wait(&task->work_done_sem);
694 BUG_ON(ret);
695 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300696 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200697 task->cpu_usage = 0;
698 }
699
700 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300701 if (!sched->runavg_cpu_usage)
702 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800703 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 +0200704
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300705 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
706 if (!sched->runavg_parent_cpu_usage)
707 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800708 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
709 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200710
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300711 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200712 BUG_ON(ret);
713
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300714 for (i = 0; i < sched->nr_tasks; i++) {
715 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200716 sem_init(&task->sleep_sem, 0, 0);
717 task->curr_event = 0;
718 }
719}
720
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300721static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200722{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500723 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200724
725 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300726 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200727 T1 = get_nsecs();
728
729 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300730 sched->sum_runtime += delta;
731 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200732
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300733 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200734 if (delta < avg_delta)
735 fluct = avg_delta - delta;
736 else
737 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300738 sched->sum_fluct += fluct;
739 if (!sched->run_avg)
740 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800741 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200742
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300743 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200744
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300745 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200746
Ingo Molnarad236fd2009-09-11 12:12:54 +0200747 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300748 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200749
750#if 0
751 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200752 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300753 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200754 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200755 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300756 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
757 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200758#endif
759
Ingo Molnarad236fd2009-09-11 12:12:54 +0200760 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200761
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300762 if (sched->nr_sleep_corrections)
763 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
764 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200765}
766
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300767static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200768{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200769 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200770
771 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300772 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200773 T1 = get_nsecs();
774
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200775 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200776
777 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300778 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200779 T1 = get_nsecs();
780
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200781 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200782}
783
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300784static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300785replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300786 struct perf_evsel *evsel, struct perf_sample *sample,
787 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200788{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300789 const char *comm = perf_evsel__strval(evsel, sample, "comm");
790 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200791 struct task_desc *waker, *wakee;
792
793 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300794 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200795
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300796 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200797 }
798
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300799 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300800 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200801
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300802 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300803 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200804}
805
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300806static int replay_switch_event(struct perf_sched *sched,
807 struct perf_evsel *evsel,
808 struct perf_sample *sample,
809 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200810{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300811 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
812 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
813 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
814 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
815 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300816 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300817 u64 timestamp0, timestamp = sample->time;
818 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200819 s64 delta;
820
Ingo Molnarad236fd2009-09-11 12:12:54 +0200821 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300822 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200823
Ingo Molnarfbf94822009-09-11 12:12:54 +0200824 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300825 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200826
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300827 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200828 if (timestamp0)
829 delta = timestamp - timestamp0;
830 else
831 delta = 0;
832
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300833 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900834 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300835 return -1;
836 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200837
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300838 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
839 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200840
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300841 prev = register_pid(sched, prev_pid, prev_comm);
842 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200843
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300844 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200845
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300846 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300847 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300848
849 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200850}
851
David Aherncb627502013-08-07 22:50:47 -0400852static int replay_fork_event(struct perf_sched *sched,
853 union perf_event *event,
854 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200855{
David Aherncb627502013-08-07 22:50:47 -0400856 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300857
Adrian Hunter314add62013-08-27 11:23:03 +0300858 child = machine__findnew_thread(machine, event->fork.pid,
859 event->fork.tid);
860 parent = machine__findnew_thread(machine, event->fork.ppid,
861 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400862
863 if (child == NULL || parent == NULL) {
864 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
865 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300866 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200867 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300868
David Aherncb627502013-08-07 22:50:47 -0400869 if (verbose) {
870 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200871 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
872 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400873 }
874
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200875 register_pid(sched, parent->tid, thread__comm_str(parent));
876 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300877out_put:
878 thread__put(child);
879 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300880 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200881}
882
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200883struct sort_dimension {
884 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200885 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200886 struct list_head list;
887};
888
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200889static int
mingo39aeb522009-09-14 20:04:48 +0200890thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200891{
892 struct sort_dimension *sort;
893 int ret = 0;
894
Ingo Molnarb5fae122009-09-11 12:12:54 +0200895 BUG_ON(list_empty(list));
896
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200897 list_for_each_entry(sort, list, list) {
898 ret = sort->cmp(l, r);
899 if (ret)
900 return ret;
901 }
902
903 return ret;
904}
905
mingo39aeb522009-09-14 20:04:48 +0200906static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200907thread_atoms_search(struct rb_root *root, struct thread *thread,
908 struct list_head *sort_list)
909{
910 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200911 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200912
913 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200914 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200915 int cmp;
916
mingo39aeb522009-09-14 20:04:48 +0200917 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200918
919 cmp = thread_lat_cmp(sort_list, &key, atoms);
920 if (cmp > 0)
921 node = node->rb_left;
922 else if (cmp < 0)
923 node = node->rb_right;
924 else {
925 BUG_ON(thread != atoms->thread);
926 return atoms;
927 }
928 }
929 return NULL;
930}
931
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200932static void
mingo39aeb522009-09-14 20:04:48 +0200933__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200934 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935{
936 struct rb_node **new = &(root->rb_node), *parent = NULL;
937
938 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200939 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200940 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941
mingo39aeb522009-09-14 20:04:48 +0200942 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200943 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200944
945 cmp = thread_lat_cmp(sort_list, data, this);
946
947 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200949 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200950 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200951 }
952
953 rb_link_node(&data->node, parent, new);
954 rb_insert_color(&data->node, root);
955}
956
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300957static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200958{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200959 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300960 if (!atoms) {
961 pr_err("No memory at %s\n", __func__);
962 return -1;
963 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200964
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300965 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200966 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300967 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300968 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200969}
970
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300971static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200972{
973 const char *str = TASK_STATE_TO_CHAR_STR;
974
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300975 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200976}
977
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300978static int
mingo39aeb522009-09-14 20:04:48 +0200979add_sched_out_event(struct work_atoms *atoms,
980 char run_state,
981 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200983 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300984 if (!atom) {
985 pr_err("Non memory at %s", __func__);
986 return -1;
987 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200989 atom->sched_out_time = timestamp;
990
mingo39aeb522009-09-14 20:04:48 +0200991 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200992 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200993 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200994 }
995
mingo39aeb522009-09-14 20:04:48 +0200996 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300997 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200998}
999
1000static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001001add_runtime_event(struct work_atoms *atoms, u64 delta,
1002 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +02001003{
1004 struct work_atom *atom;
1005
1006 BUG_ON(list_empty(&atoms->work_list));
1007
1008 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1009
1010 atom->runtime += delta;
1011 atoms->total_runtime += delta;
1012}
1013
1014static void
1015add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001016{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001017 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001018 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001019
mingo39aeb522009-09-14 20:04:48 +02001020 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001021 return;
1022
mingo39aeb522009-09-14 20:04:48 +02001023 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001025 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001026 return;
1027
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001028 if (timestamp < atom->wake_up_time) {
1029 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030 return;
1031 }
1032
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001033 atom->state = THREAD_SCHED_IN;
1034 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001035
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001036 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001037 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001038 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001039 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001040 atoms->max_lat_at = timestamp;
1041 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001042 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043}
1044
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001045static int latency_switch_event(struct perf_sched *sched,
1046 struct perf_evsel *evsel,
1047 struct perf_sample *sample,
1048 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001049{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001050 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1051 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1052 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001053 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001054 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001055 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001056 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001057 s64 delta;
1058
mingo39aeb522009-09-14 20:04:48 +02001059 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001060
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001061 timestamp0 = sched->cpu_last_switched[cpu];
1062 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001063 if (timestamp0)
1064 delta = timestamp - timestamp0;
1065 else
1066 delta = 0;
1067
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001068 if (delta < 0) {
1069 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1070 return -1;
1071 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001072
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001073 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1074 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001075 if (sched_out == NULL || sched_in == NULL)
1076 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001077
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001078 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001079 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001080 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001081 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001082 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001083 if (!out_events) {
1084 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001085 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001086 }
mingo39aeb522009-09-14 20:04:48 +02001087 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001088 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001089 return -1;
mingo39aeb522009-09-14 20:04:48 +02001090
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001091 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001092 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001093 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001094 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001095 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001096 if (!in_events) {
1097 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001098 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001099 }
mingo39aeb522009-09-14 20:04:48 +02001100 /*
1101 * Take came in we have not heard about yet,
1102 * add in an initial atom in runnable state:
1103 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001104 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001105 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001106 }
1107 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001108 err = 0;
1109out_put:
1110 thread__put(sched_out);
1111 thread__put(sched_in);
1112 return err;
mingo39aeb522009-09-14 20:04:48 +02001113}
1114
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001115static int latency_runtime_event(struct perf_sched *sched,
1116 struct perf_evsel *evsel,
1117 struct perf_sample *sample,
1118 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001119{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001120 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1121 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001122 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001123 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001124 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001125 int cpu = sample->cpu, err = -1;
1126
1127 if (thread == NULL)
1128 return -1;
mingo39aeb522009-09-14 20:04:48 +02001129
1130 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001131 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001132 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001133 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001134 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001135 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001136 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001137 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001138 }
1139 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001140 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001141 }
1142
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001143 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001144 err = 0;
1145out_put:
1146 thread__put(thread);
1147 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001148}
1149
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001150static int latency_wakeup_event(struct perf_sched *sched,
1151 struct perf_evsel *evsel,
1152 struct perf_sample *sample,
1153 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001154{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001155 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001156 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001157 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001158 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001159 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001160 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001161
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001162 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001163 if (wakee == NULL)
1164 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001165 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001166 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001167 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001168 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001169 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001170 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001171 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001172 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001173 }
1174 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001175 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001176 }
1177
mingo39aeb522009-09-14 20:04:48 +02001178 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001179
mingo39aeb522009-09-14 20:04:48 +02001180 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001181
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001182 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001183 * As we do not guarantee the wakeup event happens when
1184 * task is out of run queue, also may happen when task is
1185 * on run queue and wakeup only change ->state to TASK_RUNNING,
1186 * then we should not set the ->wake_up_time when wake up a
1187 * task which is on run queue.
1188 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001189 * You WILL be missing events if you've recorded only
1190 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001191 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001192 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001193 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001194 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001195
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001196 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001197 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001198 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001199 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001200 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001201
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001202 atom->state = THREAD_WAIT_CPU;
1203 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001204out_ok:
1205 err = 0;
1206out_put:
1207 thread__put(wakee);
1208 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209}
1210
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001211static int latency_migrate_task_event(struct perf_sched *sched,
1212 struct perf_evsel *evsel,
1213 struct perf_sample *sample,
1214 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001215{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001216 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001217 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001218 struct work_atoms *atoms;
1219 struct work_atom *atom;
1220 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001221 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001222
1223 /*
1224 * Only need to worry about migration when profiling one CPU.
1225 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001226 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001227 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001228
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001229 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001230 if (migrant == NULL)
1231 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001232 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001233 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001234 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001235 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001236 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001237 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001238 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001239 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001240 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001241 }
1242 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001243 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001244 }
1245
1246 BUG_ON(list_empty(&atoms->work_list));
1247
1248 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1249 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1250
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001251 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001252
1253 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001254 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001255 err = 0;
1256out_put:
1257 thread__put(migrant);
1258 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001259}
1260
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001261static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001262{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001263 int i;
1264 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001265 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001266 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001267
mingo39aeb522009-09-14 20:04:48 +02001268 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001269 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001270 /*
1271 * Ignore idle threads:
1272 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001273 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001274 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001275
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001276 sched->all_runtime += work_list->total_runtime;
1277 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001278
Josef Bacik2f80dd42015-05-22 09:18:40 -04001279 if (work_list->num_merged > 1)
1280 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1281 else
1282 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001283
mingo08f69e62009-09-14 18:30:44 +02001284 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001285 printf(" ");
1286
mingo39aeb522009-09-14 20:04:48 +02001287 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001288 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001289
Namhyung Kim99620a52016-10-24 11:02:45 +09001290 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 -03001291 (double)work_list->total_runtime / NSEC_PER_MSEC,
1292 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1293 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001294 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001295}
1296
mingo39aeb522009-09-14 20:04:48 +02001297static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001298{
Jiri Olsa0014de12015-11-02 12:10:25 +01001299 if (l->thread == r->thread)
1300 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001301 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001302 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001303 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001304 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001305 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001306}
1307
mingo39aeb522009-09-14 20:04:48 +02001308static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001309{
1310 u64 avgl, avgr;
1311
1312 if (!l->nb_atoms)
1313 return -1;
1314
1315 if (!r->nb_atoms)
1316 return 1;
1317
1318 avgl = l->total_lat / l->nb_atoms;
1319 avgr = r->total_lat / r->nb_atoms;
1320
1321 if (avgl < avgr)
1322 return -1;
1323 if (avgl > avgr)
1324 return 1;
1325
1326 return 0;
1327}
1328
mingo39aeb522009-09-14 20:04:48 +02001329static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001330{
1331 if (l->max_lat < r->max_lat)
1332 return -1;
1333 if (l->max_lat > r->max_lat)
1334 return 1;
1335
1336 return 0;
1337}
1338
mingo39aeb522009-09-14 20:04:48 +02001339static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001340{
1341 if (l->nb_atoms < r->nb_atoms)
1342 return -1;
1343 if (l->nb_atoms > r->nb_atoms)
1344 return 1;
1345
1346 return 0;
1347}
1348
mingo39aeb522009-09-14 20:04:48 +02001349static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001350{
1351 if (l->total_runtime < r->total_runtime)
1352 return -1;
1353 if (l->total_runtime > r->total_runtime)
1354 return 1;
1355
1356 return 0;
1357}
1358
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001359static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001360{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001361 size_t i;
1362 static struct sort_dimension avg_sort_dimension = {
1363 .name = "avg",
1364 .cmp = avg_cmp,
1365 };
1366 static struct sort_dimension max_sort_dimension = {
1367 .name = "max",
1368 .cmp = max_cmp,
1369 };
1370 static struct sort_dimension pid_sort_dimension = {
1371 .name = "pid",
1372 .cmp = pid_cmp,
1373 };
1374 static struct sort_dimension runtime_sort_dimension = {
1375 .name = "runtime",
1376 .cmp = runtime_cmp,
1377 };
1378 static struct sort_dimension switch_sort_dimension = {
1379 .name = "switch",
1380 .cmp = switch_cmp,
1381 };
1382 struct sort_dimension *available_sorts[] = {
1383 &pid_sort_dimension,
1384 &avg_sort_dimension,
1385 &max_sort_dimension,
1386 &switch_sort_dimension,
1387 &runtime_sort_dimension,
1388 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001389
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001390 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001391 if (!strcmp(available_sorts[i]->name, tok)) {
1392 list_add_tail(&available_sorts[i]->list, list);
1393
1394 return 0;
1395 }
1396 }
1397
1398 return -1;
1399}
1400
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001401static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001402{
1403 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001404 struct rb_root *root = &sched->atom_root;
1405again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001406 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001407 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001408 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001409 if (!node)
1410 break;
1411
Josef Bacik2f80dd42015-05-22 09:18:40 -04001412 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001413 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001414 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001415 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001416 if (root == &sched->atom_root) {
1417 root = &sched->merged_atom_root;
1418 goto again;
1419 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001420}
1421
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001422static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001423 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001424 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001425 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001426{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001427 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001428
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001429 if (sched->tp_handler->wakeup_event)
1430 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001431
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001432 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001433}
1434
Jiri Olsaa151a372016-04-12 15:29:29 +02001435union map_priv {
1436 void *ptr;
1437 bool color;
1438};
1439
1440static bool thread__has_color(struct thread *thread)
1441{
1442 union map_priv priv = {
1443 .ptr = thread__priv(thread),
1444 };
1445
1446 return priv.color;
1447}
1448
1449static struct thread*
1450map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1451{
1452 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1453 union map_priv priv = {
1454 .color = false,
1455 };
1456
1457 if (!sched->map.color_pids || !thread || thread__priv(thread))
1458 return thread;
1459
1460 if (thread_map__has(sched->map.color_pids, tid))
1461 priv.color = true;
1462
1463 thread__set_priv(thread, priv.ptr);
1464 return thread;
1465}
1466
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001467static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1468 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001470 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1471 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001472 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001473 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001474 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001475 int i, this_cpu = sample->cpu;
1476 int cpus_nr;
1477 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001478 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001479 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001480
1481 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1482
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001483 if (this_cpu > sched->max_cpu)
1484 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001485
Jiri Olsa99623c62016-04-12 15:29:26 +02001486 if (sched->map.comp) {
1487 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1488 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1489 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1490 new_cpu = true;
1491 }
1492 } else
1493 cpus_nr = sched->max_cpu;
1494
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001495 timestamp0 = sched->cpu_last_switched[this_cpu];
1496 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001497 if (timestamp0)
1498 delta = timestamp - timestamp0;
1499 else
1500 delta = 0;
1501
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001502 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001503 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001504 return -1;
1505 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001506
Jiri Olsaa151a372016-04-12 15:29:29 +02001507 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001508 if (sched_in == NULL)
1509 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001510
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001511 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001512
1513 printf(" ");
1514
1515 new_shortname = 0;
1516 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001517 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1518 /*
1519 * Don't allocate a letter-number for swapper:0
1520 * as a shortname. Instead, we use '.' for it.
1521 */
1522 sched_in->shortname[0] = '.';
1523 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001524 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001525 sched_in->shortname[0] = sched->next_shortname1;
1526 sched_in->shortname[1] = sched->next_shortname2;
1527
1528 if (sched->next_shortname1 < 'Z') {
1529 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001530 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001531 sched->next_shortname1 = 'A';
1532 if (sched->next_shortname2 < '9')
1533 sched->next_shortname2++;
1534 else
1535 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001536 }
1537 }
1538 new_shortname = 1;
1539 }
1540
Jiri Olsa99623c62016-04-12 15:29:26 +02001541 for (i = 0; i < cpus_nr; i++) {
1542 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001543 struct thread *curr_thread = sched->curr_thread[cpu];
1544 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001545 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001546
1547 if (curr_thread && thread__has_color(curr_thread))
1548 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001549
Jiri Olsa73643bb2016-04-12 15:29:31 +02001550 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1551 continue;
1552
Jiri Olsacf294f22016-04-12 15:29:30 +02001553 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1554 cpu_color = COLOR_CPUS;
1555
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001556 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001557 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001558 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001559 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001560
Dongsheng6bcab4e2014-05-06 14:39:01 +09001561 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001562 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001563 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001564 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001565 }
1566
Jiri Olsa73643bb2016-04-12 15:29:31 +02001567 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1568 goto out;
1569
Namhyung Kim99620a52016-10-24 11:02:45 +09001570 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1571 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001572 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001573 const char *pid_color = color;
1574
1575 if (thread__has_color(sched_in))
1576 pid_color = COLOR_PIDS;
1577
1578 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001579 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001580 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001581
Jiri Olsa99623c62016-04-12 15:29:26 +02001582 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001583 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001584
Jiri Olsa73643bb2016-04-12 15:29:31 +02001585out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001586 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001587
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001588 thread__put(sched_in);
1589
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001590 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001591}
1592
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001593static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001594 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001595 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001596 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001597{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001598 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001599 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001600 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1601 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001602
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001603 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001604 /*
1605 * Are we trying to switch away a PID that is
1606 * not current?
1607 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001608 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001609 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001610 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001611
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001612 if (sched->tp_handler->switch_event)
1613 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001614
1615 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001616 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001617}
1618
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001619static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001620 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001621 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001622 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001623{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001624 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001625
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001626 if (sched->tp_handler->runtime_event)
1627 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001628
1629 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001630}
1631
David Aherncb627502013-08-07 22:50:47 -04001632static int perf_sched__process_fork_event(struct perf_tool *tool,
1633 union perf_event *event,
1634 struct perf_sample *sample,
1635 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001636{
1637 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1638
David Aherncb627502013-08-07 22:50:47 -04001639 /* run the fork event through the perf machineruy */
1640 perf_event__process_fork(tool, event, sample, machine);
1641
1642 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001643 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001644 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001645
1646 return 0;
1647}
1648
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001649static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001650 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001651 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001652 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001653{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001654 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001655
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001656 if (sched->tp_handler->migrate_task_event)
1657 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001658
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001659 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001660}
1661
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001662typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001663 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001664 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001665 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001666
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001667static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1668 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001669 struct perf_sample *sample,
1670 struct perf_evsel *evsel,
1671 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001672{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001673 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001674
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001675 if (evsel->handler != NULL) {
1676 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001677 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001678 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001679
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001680 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001681}
1682
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001683static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001684{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001685 const struct perf_evsel_str_handler handlers[] = {
1686 { "sched:sched_switch", process_sched_switch_event, },
1687 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1688 { "sched:sched_wakeup", process_sched_wakeup_event, },
1689 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001690 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1691 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001692 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001693 struct perf_data_file file = {
1694 .path = input_name,
1695 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001696 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001697 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001698 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001699
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001700 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001701 if (session == NULL) {
1702 pr_debug("No Memory for session\n");
1703 return -1;
1704 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001705
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001706 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001707
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001708 if (perf_session__set_tracepoints_handlers(session, handlers))
1709 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001710
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001711 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001712 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001713 if (err) {
1714 pr_err("Failed to process events, error %d", err);
1715 goto out_delete;
1716 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001717
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001718 sched->nr_events = session->evlist->stats.nr_events[0];
1719 sched->nr_lost_events = session->evlist->stats.total_lost;
1720 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001721 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001722
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001723 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001724out_delete:
1725 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001726 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001727}
1728
David Ahern49394a22016-11-16 15:06:29 +09001729/*
1730 * scheduling times are printed as msec.usec
1731 */
1732static inline void print_sched_time(unsigned long long nsecs, int width)
1733{
1734 unsigned long msecs;
1735 unsigned long usecs;
1736
1737 msecs = nsecs / NSEC_PER_MSEC;
1738 nsecs -= msecs * NSEC_PER_MSEC;
1739 usecs = nsecs / NSEC_PER_USEC;
1740 printf("%*lu.%03lu ", width, msecs, usecs);
1741}
1742
1743/*
1744 * returns runtime data for event, allocating memory for it the
1745 * first time it is used.
1746 */
1747static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1748{
1749 struct evsel_runtime *r = evsel->priv;
1750
1751 if (r == NULL) {
1752 r = zalloc(sizeof(struct evsel_runtime));
1753 evsel->priv = r;
1754 }
1755
1756 return r;
1757}
1758
1759/*
1760 * save last time event was seen per cpu
1761 */
1762static void perf_evsel__save_time(struct perf_evsel *evsel,
1763 u64 timestamp, u32 cpu)
1764{
1765 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1766
1767 if (r == NULL)
1768 return;
1769
1770 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1771 int i, n = __roundup_pow_of_two(cpu+1);
1772 void *p = r->last_time;
1773
1774 p = realloc(r->last_time, n * sizeof(u64));
1775 if (!p)
1776 return;
1777
1778 r->last_time = p;
1779 for (i = r->ncpu; i < n; ++i)
1780 r->last_time[i] = (u64) 0;
1781
1782 r->ncpu = n;
1783 }
1784
1785 r->last_time[cpu] = timestamp;
1786}
1787
1788/* returns last time this event was seen on the given cpu */
1789static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1790{
1791 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1792
1793 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1794 return 0;
1795
1796 return r->last_time[cpu];
1797}
1798
Namhyung Kim9b8087d2016-12-22 15:03:48 +09001799static int comm_width = 30;
David Ahern49394a22016-11-16 15:06:29 +09001800
1801static char *timehist_get_commstr(struct thread *thread)
1802{
1803 static char str[32];
1804 const char *comm = thread__comm_str(thread);
1805 pid_t tid = thread->tid;
1806 pid_t pid = thread->pid_;
1807 int n;
1808
1809 if (pid == 0)
1810 n = scnprintf(str, sizeof(str), "%s", comm);
1811
1812 else if (tid != pid)
1813 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1814
1815 else
1816 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1817
1818 if (n > comm_width)
1819 comm_width = n;
1820
1821 return str;
1822}
1823
David Aherna407b062016-11-16 15:06:33 +09001824static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001825{
David Aherna407b062016-11-16 15:06:33 +09001826 u32 ncpus = sched->max_cpu + 1;
1827 u32 i, j;
1828
David Ahern49394a22016-11-16 15:06:29 +09001829 printf("%15s %6s ", "time", "cpu");
1830
David Aherna407b062016-11-16 15:06:33 +09001831 if (sched->show_cpu_visual) {
1832 printf(" ");
1833 for (i = 0, j = 0; i < ncpus; ++i) {
1834 printf("%x", j++);
1835 if (j > 15)
1836 j = 0;
1837 }
1838 printf(" ");
1839 }
1840
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001841 printf(" %-*s %9s %9s %9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001842 "task name", "wait time", "sch delay", "run time");
1843
Namhyung Kim414e0502017-01-13 19:45:22 +09001844 if (sched->show_state)
1845 printf(" %s", "state");
1846
David Ahern49394a22016-11-16 15:06:29 +09001847 printf("\n");
1848
1849 /*
1850 * units row
1851 */
1852 printf("%15s %-6s ", "", "");
1853
David Aherna407b062016-11-16 15:06:33 +09001854 if (sched->show_cpu_visual)
1855 printf(" %*s ", ncpus, "");
1856
Namhyung Kim414e0502017-01-13 19:45:22 +09001857 printf(" %-*s %9s %9s %9s", comm_width,
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001858 "[tid/pid]", "(msec)", "(msec)", "(msec)");
David Ahern49394a22016-11-16 15:06:29 +09001859
Namhyung Kim414e0502017-01-13 19:45:22 +09001860 if (sched->show_state)
1861 printf(" %5s", "");
1862
1863 printf("\n");
1864
David Ahern49394a22016-11-16 15:06:29 +09001865 /*
1866 * separator
1867 */
1868 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1869
David Aherna407b062016-11-16 15:06:33 +09001870 if (sched->show_cpu_visual)
1871 printf(" %.*s ", ncpus, graph_dotted_line);
1872
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001873 printf(" %.*s %.9s %.9s %.9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001874 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1875 graph_dotted_line);
1876
Namhyung Kim414e0502017-01-13 19:45:22 +09001877 if (sched->show_state)
1878 printf(" %.5s", graph_dotted_line);
1879
David Ahern49394a22016-11-16 15:06:29 +09001880 printf("\n");
1881}
1882
Namhyung Kim414e0502017-01-13 19:45:22 +09001883static char task_state_char(struct thread *thread, int state)
1884{
1885 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1886 unsigned bit = state ? ffs(state) : 0;
1887
1888 /* 'I' for idle */
1889 if (thread->tid == 0)
1890 return 'I';
1891
1892 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
1893}
1894
David Ahernfc1469f2016-11-16 15:06:31 +09001895static void timehist_print_sample(struct perf_sched *sched,
1896 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001897 struct addr_location *al,
David Ahern853b7402016-11-29 10:15:44 -07001898 struct thread *thread,
Namhyung Kim414e0502017-01-13 19:45:22 +09001899 u64 t, int state)
David Ahern49394a22016-11-16 15:06:29 +09001900{
1901 struct thread_runtime *tr = thread__priv(thread);
David Aherna407b062016-11-16 15:06:33 +09001902 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09001903 char tstr[64];
Namhyung Kim941bdea2017-01-13 19:45:21 +09001904 u64 wait_time;
David Ahern49394a22016-11-16 15:06:29 +09001905
David Ahern853b7402016-11-29 10:15:44 -07001906 timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
David Ahern49394a22016-11-16 15:06:29 +09001907 printf("%15s [%04d] ", tstr, sample->cpu);
1908
David Aherna407b062016-11-16 15:06:33 +09001909 if (sched->show_cpu_visual) {
1910 u32 i;
1911 char c;
1912
1913 printf(" ");
1914 for (i = 0; i < max_cpus; ++i) {
1915 /* flag idle times with 'i'; others are sched events */
1916 if (i == sample->cpu)
1917 c = (thread->tid == 0) ? 'i' : 's';
1918 else
1919 c = ' ';
1920 printf("%c", c);
1921 }
1922 printf(" ");
1923 }
1924
David Ahern49394a22016-11-16 15:06:29 +09001925 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1926
Namhyung Kim941bdea2017-01-13 19:45:21 +09001927 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
1928 print_sched_time(wait_time, 6);
1929
David Ahern49394a22016-11-16 15:06:29 +09001930 print_sched_time(tr->dt_delay, 6);
1931 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001932
Namhyung Kim414e0502017-01-13 19:45:22 +09001933 if (sched->show_state)
1934 printf(" %5c ", task_state_char(thread, state));
1935
David Ahernfc1469f2016-11-16 15:06:31 +09001936 if (sched->show_wakeups)
1937 printf(" %-*s", comm_width, "");
1938
David Ahern6c973c92016-11-16 15:06:32 +09001939 if (thread->tid == 0)
1940 goto out;
1941
1942 if (sched->show_callchain)
1943 printf(" ");
1944
1945 sample__fprintf_sym(sample, al, 0,
1946 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09001947 EVSEL__PRINT_CALLCHAIN_ARROW |
1948 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09001949 &callchain_cursor, stdout);
1950
1951out:
David Ahern49394a22016-11-16 15:06:29 +09001952 printf("\n");
1953}
1954
1955/*
1956 * Explanation of delta-time stats:
1957 *
1958 * t = time of current schedule out event
1959 * tprev = time of previous sched out event
1960 * also time of schedule-in event for current task
1961 * last_time = time of last sched change event for current task
1962 * (i.e, time process was last scheduled out)
1963 * ready_to_run = time of wakeup for current task
1964 *
1965 * -----|------------|------------|------------|------
1966 * last ready tprev t
1967 * time to run
1968 *
1969 * |-------- dt_wait --------|
1970 * |- dt_delay -|-- dt_run --|
1971 *
1972 * dt_run = run time of current task
1973 * dt_wait = time between last schedule out event for task and tprev
1974 * represents time spent off the cpu
1975 * dt_delay = time between wakeup and schedule-in of task
1976 */
1977
1978static void timehist_update_runtime_stats(struct thread_runtime *r,
1979 u64 t, u64 tprev)
1980{
1981 r->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09001982 r->dt_sleep = 0;
1983 r->dt_iowait = 0;
1984 r->dt_preempt = 0;
David Ahern49394a22016-11-16 15:06:29 +09001985 r->dt_run = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09001986
David Ahern49394a22016-11-16 15:06:29 +09001987 if (tprev) {
1988 r->dt_run = t - tprev;
1989 if (r->ready_to_run) {
1990 if (r->ready_to_run > tprev)
1991 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1992 else
1993 r->dt_delay = tprev - r->ready_to_run;
1994 }
1995
1996 if (r->last_time > tprev)
1997 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
Namhyung Kim941bdea2017-01-13 19:45:21 +09001998 else if (r->last_time) {
1999 u64 dt_wait = tprev - r->last_time;
2000
2001 if (r->last_state == TASK_RUNNING)
2002 r->dt_preempt = dt_wait;
2003 else if (r->last_state == TASK_UNINTERRUPTIBLE)
2004 r->dt_iowait = dt_wait;
2005 else
2006 r->dt_sleep = dt_wait;
2007 }
David Ahern49394a22016-11-16 15:06:29 +09002008 }
2009
2010 update_stats(&r->run_stats, r->dt_run);
2011 r->total_run_time += r->dt_run;
2012}
2013
Namhyung Kim96039c72016-12-08 23:47:50 +09002014static bool is_idle_sample(struct perf_sample *sample,
2015 struct perf_evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09002016{
2017 /* pid 0 == swapper == idle task */
Namhyung Kim96039c72016-12-08 23:47:50 +09002018 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0)
2019 return perf_evsel__intval(evsel, sample, "prev_pid") == 0;
David Ahern49394a22016-11-16 15:06:29 +09002020
Namhyung Kim96039c72016-12-08 23:47:50 +09002021 return sample->pid == 0;
2022}
2023
2024static void save_task_callchain(struct perf_sched *sched,
2025 struct perf_sample *sample,
2026 struct perf_evsel *evsel,
2027 struct machine *machine)
2028{
2029 struct callchain_cursor *cursor = &callchain_cursor;
2030 struct thread *thread;
David Ahern6c973c92016-11-16 15:06:32 +09002031
2032 /* want main thread for process - has maps */
2033 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
2034 if (thread == NULL) {
2035 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
Namhyung Kim96039c72016-12-08 23:47:50 +09002036 return;
David Ahern6c973c92016-11-16 15:06:32 +09002037 }
2038
2039 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim96039c72016-12-08 23:47:50 +09002040 return;
David Ahern6c973c92016-11-16 15:06:32 +09002041
2042 if (thread__resolve_callchain(thread, cursor, evsel, sample,
Namhyung Kim8388deb2016-11-24 10:11:14 +09002043 NULL, NULL, sched->max_stack + 2) != 0) {
David Ahern6c973c92016-11-16 15:06:32 +09002044 if (verbose)
2045 error("Failed to resolve callchain. Skipping\n");
2046
Namhyung Kim96039c72016-12-08 23:47:50 +09002047 return;
David Ahern6c973c92016-11-16 15:06:32 +09002048 }
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002049
David Ahern6c973c92016-11-16 15:06:32 +09002050 callchain_cursor_commit(cursor);
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002051
2052 while (true) {
2053 struct callchain_cursor_node *node;
2054 struct symbol *sym;
2055
2056 node = callchain_cursor_current(cursor);
2057 if (node == NULL)
2058 break;
2059
2060 sym = node->sym;
2061 if (sym && sym->name) {
2062 if (!strcmp(sym->name, "schedule") ||
2063 !strcmp(sym->name, "__schedule") ||
2064 !strcmp(sym->name, "preempt_schedule"))
2065 sym->ignore = 1;
2066 }
2067
2068 callchain_cursor_advance(cursor);
2069 }
David Ahern49394a22016-11-16 15:06:29 +09002070}
2071
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002072static int init_idle_thread(struct thread *thread)
2073{
2074 struct idle_thread_runtime *itr;
2075
2076 thread__set_comm(thread, idle_comm, 0);
2077
2078 itr = zalloc(sizeof(*itr));
2079 if (itr == NULL)
2080 return -ENOMEM;
2081
2082 init_stats(&itr->tr.run_stats);
2083 callchain_init(&itr->callchain);
2084 callchain_cursor_reset(&itr->cursor);
2085 thread__set_priv(thread, itr);
2086
2087 return 0;
2088}
2089
David Ahern49394a22016-11-16 15:06:29 +09002090/*
2091 * Track idle stats per cpu by maintaining a local thread
2092 * struct for the idle task on each cpu.
2093 */
2094static int init_idle_threads(int ncpu)
2095{
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002096 int i, ret;
David Ahern49394a22016-11-16 15:06:29 +09002097
2098 idle_threads = zalloc(ncpu * sizeof(struct thread *));
2099 if (!idle_threads)
2100 return -ENOMEM;
2101
Namhyung Kimb3363522016-12-06 12:40:05 +09002102 idle_max_cpu = ncpu;
David Ahern49394a22016-11-16 15:06:29 +09002103
2104 /* allocate the actual thread struct if needed */
2105 for (i = 0; i < ncpu; ++i) {
2106 idle_threads[i] = thread__new(0, 0);
2107 if (idle_threads[i] == NULL)
2108 return -ENOMEM;
2109
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002110 ret = init_idle_thread(idle_threads[i]);
2111 if (ret < 0)
2112 return ret;
David Ahern49394a22016-11-16 15:06:29 +09002113 }
2114
2115 return 0;
2116}
2117
2118static void free_idle_threads(void)
2119{
2120 int i;
2121
2122 if (idle_threads == NULL)
2123 return;
2124
Namhyung Kimb3363522016-12-06 12:40:05 +09002125 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern49394a22016-11-16 15:06:29 +09002126 if ((idle_threads[i]))
2127 thread__delete(idle_threads[i]);
2128 }
2129
2130 free(idle_threads);
2131}
2132
2133static struct thread *get_idle_thread(int cpu)
2134{
2135 /*
2136 * expand/allocate array of pointers to local thread
2137 * structs if needed
2138 */
2139 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2140 int i, j = __roundup_pow_of_two(cpu+1);
2141 void *p;
2142
2143 p = realloc(idle_threads, j * sizeof(struct thread *));
2144 if (!p)
2145 return NULL;
2146
2147 idle_threads = (struct thread **) p;
Namhyung Kimb3363522016-12-06 12:40:05 +09002148 for (i = idle_max_cpu; i < j; ++i)
David Ahern49394a22016-11-16 15:06:29 +09002149 idle_threads[i] = NULL;
2150
2151 idle_max_cpu = j;
2152 }
2153
2154 /* allocate a new thread struct if needed */
2155 if (idle_threads[cpu] == NULL) {
2156 idle_threads[cpu] = thread__new(0, 0);
2157
2158 if (idle_threads[cpu]) {
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002159 if (init_idle_thread(idle_threads[cpu]) < 0)
2160 return NULL;
David Ahern49394a22016-11-16 15:06:29 +09002161 }
2162 }
2163
2164 return idle_threads[cpu];
2165}
2166
Namhyung Kim699b5b92016-12-08 23:47:52 +09002167static void save_idle_callchain(struct idle_thread_runtime *itr,
2168 struct perf_sample *sample)
2169{
2170 if (!symbol_conf.use_callchain || sample->callchain == NULL)
2171 return;
2172
2173 callchain_cursor__copy(&itr->cursor, &callchain_cursor);
2174}
2175
David Ahern49394a22016-11-16 15:06:29 +09002176/*
2177 * handle runtime stats saved per thread
2178 */
2179static struct thread_runtime *thread__init_runtime(struct thread *thread)
2180{
2181 struct thread_runtime *r;
2182
2183 r = zalloc(sizeof(struct thread_runtime));
2184 if (!r)
2185 return NULL;
2186
2187 init_stats(&r->run_stats);
2188 thread__set_priv(thread, r);
2189
2190 return r;
2191}
2192
2193static struct thread_runtime *thread__get_runtime(struct thread *thread)
2194{
2195 struct thread_runtime *tr;
2196
2197 tr = thread__priv(thread);
2198 if (tr == NULL) {
2199 tr = thread__init_runtime(thread);
2200 if (tr == NULL)
2201 pr_debug("Failed to malloc memory for runtime data.\n");
2202 }
2203
2204 return tr;
2205}
2206
David Ahern6c973c92016-11-16 15:06:32 +09002207static struct thread *timehist_get_thread(struct perf_sched *sched,
2208 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002209 struct machine *machine,
2210 struct perf_evsel *evsel)
2211{
2212 struct thread *thread;
2213
Namhyung Kim96039c72016-12-08 23:47:50 +09002214 if (is_idle_sample(sample, evsel)) {
David Ahern49394a22016-11-16 15:06:29 +09002215 thread = get_idle_thread(sample->cpu);
2216 if (thread == NULL)
2217 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2218
2219 } else {
Namhyung Kim5d92d962016-12-06 12:40:03 +09002220 /* there were samples with tid 0 but non-zero pid */
2221 thread = machine__findnew_thread(machine, sample->pid,
2222 sample->tid ?: sample->pid);
David Ahern49394a22016-11-16 15:06:29 +09002223 if (thread == NULL) {
2224 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2225 sample->tid);
2226 }
Namhyung Kim96039c72016-12-08 23:47:50 +09002227
2228 save_task_callchain(sched, sample, evsel, machine);
Namhyung Kim699b5b92016-12-08 23:47:52 +09002229 if (sched->idle_hist) {
2230 struct thread *idle;
2231 struct idle_thread_runtime *itr;
2232
2233 idle = get_idle_thread(sample->cpu);
2234 if (idle == NULL) {
2235 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2236 return NULL;
2237 }
2238
2239 itr = thread__priv(idle);
2240 if (itr == NULL)
2241 return NULL;
2242
2243 itr->last_thread = thread;
2244
2245 /* copy task callchain when entering to idle */
2246 if (perf_evsel__intval(evsel, sample, "next_pid") == 0)
2247 save_idle_callchain(itr, sample);
2248 }
David Ahern49394a22016-11-16 15:06:29 +09002249 }
2250
2251 return thread;
2252}
2253
David Ahern52df1382016-11-16 15:06:30 +09002254static bool timehist_skip_sample(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002255 struct thread *thread,
2256 struct perf_evsel *evsel,
2257 struct perf_sample *sample)
David Ahern49394a22016-11-16 15:06:29 +09002258{
2259 bool rc = false;
2260
David Ahern52df1382016-11-16 15:06:30 +09002261 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002262 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002263 sched->skipped_samples++;
2264 }
David Ahern49394a22016-11-16 15:06:29 +09002265
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002266 if (sched->idle_hist) {
2267 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch"))
2268 rc = true;
2269 else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 &&
2270 perf_evsel__intval(evsel, sample, "next_pid") != 0)
2271 rc = true;
2272 }
2273
David Ahern49394a22016-11-16 15:06:29 +09002274 return rc;
2275}
2276
David Ahernfc1469f2016-11-16 15:06:31 +09002277static void timehist_print_wakeup_event(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002278 struct perf_evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09002279 struct perf_sample *sample,
2280 struct machine *machine,
2281 struct thread *awakened)
2282{
2283 struct thread *thread;
2284 char tstr[64];
2285
2286 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2287 if (thread == NULL)
2288 return;
2289
2290 /* show wakeup unless both awakee and awaker are filtered */
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002291 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2292 timehist_skip_sample(sched, awakened, evsel, sample)) {
David Ahernfc1469f2016-11-16 15:06:31 +09002293 return;
2294 }
2295
2296 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2297 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002298 if (sched->show_cpu_visual)
2299 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002300
2301 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2302
2303 /* dt spacer */
2304 printf(" %9s %9s %9s ", "", "", "");
2305
2306 printf("awakened: %s", timehist_get_commstr(awakened));
2307
2308 printf("\n");
2309}
2310
2311static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002312 union perf_event *event __maybe_unused,
2313 struct perf_evsel *evsel,
2314 struct perf_sample *sample,
2315 struct machine *machine)
2316{
David Ahernfc1469f2016-11-16 15:06:31 +09002317 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002318 struct thread *thread;
2319 struct thread_runtime *tr = NULL;
2320 /* want pid of awakened task not pid in sample */
2321 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2322
2323 thread = machine__findnew_thread(machine, 0, pid);
2324 if (thread == NULL)
2325 return -1;
2326
2327 tr = thread__get_runtime(thread);
2328 if (tr == NULL)
2329 return -1;
2330
2331 if (tr->ready_to_run == 0)
2332 tr->ready_to_run = sample->time;
2333
David Ahernfc1469f2016-11-16 15:06:31 +09002334 /* show wakeups if requested */
David Ahern853b7402016-11-29 10:15:44 -07002335 if (sched->show_wakeups &&
2336 !perf_time__skip_sample(&sched->ptime, sample->time))
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002337 timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
David Ahernfc1469f2016-11-16 15:06:31 +09002338
David Ahern49394a22016-11-16 15:06:29 +09002339 return 0;
2340}
2341
David Ahern350f54f2016-11-25 09:28:41 -07002342static void timehist_print_migration_event(struct perf_sched *sched,
2343 struct perf_evsel *evsel,
2344 struct perf_sample *sample,
2345 struct machine *machine,
2346 struct thread *migrated)
2347{
2348 struct thread *thread;
2349 char tstr[64];
2350 u32 max_cpus = sched->max_cpu + 1;
2351 u32 ocpu, dcpu;
2352
2353 if (sched->summary_only)
2354 return;
2355
2356 max_cpus = sched->max_cpu + 1;
2357 ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
2358 dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
2359
2360 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2361 if (thread == NULL)
2362 return;
2363
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002364 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2365 timehist_skip_sample(sched, migrated, evsel, sample)) {
David Ahern350f54f2016-11-25 09:28:41 -07002366 return;
2367 }
2368
2369 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2370 printf("%15s [%04d] ", tstr, sample->cpu);
2371
2372 if (sched->show_cpu_visual) {
2373 u32 i;
2374 char c;
2375
2376 printf(" ");
2377 for (i = 0; i < max_cpus; ++i) {
2378 c = (i == sample->cpu) ? 'm' : ' ';
2379 printf("%c", c);
2380 }
2381 printf(" ");
2382 }
2383
2384 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2385
2386 /* dt spacer */
2387 printf(" %9s %9s %9s ", "", "", "");
2388
2389 printf("migrated: %s", timehist_get_commstr(migrated));
2390 printf(" cpu %d => %d", ocpu, dcpu);
2391
2392 printf("\n");
2393}
2394
2395static int timehist_migrate_task_event(struct perf_tool *tool,
2396 union perf_event *event __maybe_unused,
2397 struct perf_evsel *evsel,
2398 struct perf_sample *sample,
2399 struct machine *machine)
2400{
2401 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2402 struct thread *thread;
2403 struct thread_runtime *tr = NULL;
2404 /* want pid of migrated task not pid in sample */
2405 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2406
2407 thread = machine__findnew_thread(machine, 0, pid);
2408 if (thread == NULL)
2409 return -1;
2410
2411 tr = thread__get_runtime(thread);
2412 if (tr == NULL)
2413 return -1;
2414
2415 tr->migrations++;
2416
2417 /* show migrations if requested */
2418 timehist_print_migration_event(sched, evsel, sample, machine, thread);
2419
2420 return 0;
2421}
2422
David Ahern52df1382016-11-16 15:06:30 +09002423static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002424 union perf_event *event,
2425 struct perf_evsel *evsel,
2426 struct perf_sample *sample,
2427 struct machine *machine)
2428{
David Ahernfc1469f2016-11-16 15:06:31 +09002429 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern853b7402016-11-29 10:15:44 -07002430 struct perf_time_interval *ptime = &sched->ptime;
David Ahern49394a22016-11-16 15:06:29 +09002431 struct addr_location al;
2432 struct thread *thread;
2433 struct thread_runtime *tr = NULL;
David Ahern853b7402016-11-29 10:15:44 -07002434 u64 tprev, t = sample->time;
David Ahern49394a22016-11-16 15:06:29 +09002435 int rc = 0;
Namhyung Kim414e0502017-01-13 19:45:22 +09002436 int state = perf_evsel__intval(evsel, sample, "prev_state");
2437
David Ahern49394a22016-11-16 15:06:29 +09002438
2439 if (machine__resolve(machine, &al, sample) < 0) {
2440 pr_err("problem processing %d event. skipping it\n",
2441 event->header.type);
2442 rc = -1;
2443 goto out;
2444 }
2445
David Ahern6c973c92016-11-16 15:06:32 +09002446 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002447 if (thread == NULL) {
2448 rc = -1;
2449 goto out;
2450 }
2451
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002452 if (timehist_skip_sample(sched, thread, evsel, sample))
David Ahern49394a22016-11-16 15:06:29 +09002453 goto out;
2454
2455 tr = thread__get_runtime(thread);
2456 if (tr == NULL) {
2457 rc = -1;
2458 goto out;
2459 }
2460
2461 tprev = perf_evsel__get_time(evsel, sample->cpu);
2462
David Ahern853b7402016-11-29 10:15:44 -07002463 /*
2464 * If start time given:
2465 * - sample time is under window user cares about - skip sample
2466 * - tprev is under window user cares about - reset to start of window
2467 */
2468 if (ptime->start && ptime->start > t)
2469 goto out;
2470
Namhyung Kimbdd75722016-12-22 15:03:49 +09002471 if (tprev && ptime->start > tprev)
David Ahern853b7402016-11-29 10:15:44 -07002472 tprev = ptime->start;
2473
2474 /*
2475 * If end time given:
2476 * - previous sched event is out of window - we are done
2477 * - sample time is beyond window user cares about - reset it
2478 * to close out stats for time window interest
2479 */
2480 if (ptime->end) {
2481 if (tprev > ptime->end)
2482 goto out;
2483
2484 if (t > ptime->end)
2485 t = ptime->end;
2486 }
2487
Namhyung Kim07235f82016-12-08 23:47:54 +09002488 if (!sched->idle_hist || thread->tid == 0) {
2489 timehist_update_runtime_stats(tr, t, tprev);
2490
2491 if (sched->idle_hist) {
2492 struct idle_thread_runtime *itr = (void *)tr;
2493 struct thread_runtime *last_tr;
2494
2495 BUG_ON(thread->tid != 0);
2496
2497 if (itr->last_thread == NULL)
2498 goto out;
2499
2500 /* add current idle time as last thread's runtime */
2501 last_tr = thread__get_runtime(itr->last_thread);
2502 if (last_tr == NULL)
2503 goto out;
2504
2505 timehist_update_runtime_stats(last_tr, t, tprev);
2506 /*
2507 * remove delta time of last thread as it's not updated
2508 * and otherwise it will show an invalid value next
2509 * time. we only care total run time and run stat.
2510 */
2511 last_tr->dt_run = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002512 last_tr->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002513 last_tr->dt_sleep = 0;
2514 last_tr->dt_iowait = 0;
2515 last_tr->dt_preempt = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002516
Namhyung Kimba957eb2016-12-08 23:47:55 +09002517 if (itr->cursor.nr)
2518 callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2519
Namhyung Kim07235f82016-12-08 23:47:54 +09002520 itr->last_thread = NULL;
2521 }
2522 }
David Ahern853b7402016-11-29 10:15:44 -07002523
David Ahern52df1382016-11-16 15:06:30 +09002524 if (!sched->summary_only)
Namhyung Kim414e0502017-01-13 19:45:22 +09002525 timehist_print_sample(sched, sample, &al, thread, t, state);
David Ahern49394a22016-11-16 15:06:29 +09002526
2527out:
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002528 if (sched->hist_time.start == 0 && t >= ptime->start)
2529 sched->hist_time.start = t;
2530 if (ptime->end == 0 || t <= ptime->end)
2531 sched->hist_time.end = t;
2532
David Ahern49394a22016-11-16 15:06:29 +09002533 if (tr) {
2534 /* time of this sched_switch event becomes last time task seen */
2535 tr->last_time = sample->time;
2536
Namhyung Kim941bdea2017-01-13 19:45:21 +09002537 /* last state is used to determine where to account wait time */
Namhyung Kim414e0502017-01-13 19:45:22 +09002538 tr->last_state = state;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002539
David Ahern49394a22016-11-16 15:06:29 +09002540 /* sched out event for task so reset ready to run time */
2541 tr->ready_to_run = 0;
2542 }
2543
2544 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2545
2546 return rc;
2547}
2548
2549static int timehist_sched_switch_event(struct perf_tool *tool,
2550 union perf_event *event,
2551 struct perf_evsel *evsel,
2552 struct perf_sample *sample,
2553 struct machine *machine __maybe_unused)
2554{
2555 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2556}
2557
2558static int process_lost(struct perf_tool *tool __maybe_unused,
2559 union perf_event *event,
2560 struct perf_sample *sample,
2561 struct machine *machine __maybe_unused)
2562{
2563 char tstr[64];
2564
2565 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2566 printf("%15s ", tstr);
2567 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2568
2569 return 0;
2570}
2571
2572
David Ahern52df1382016-11-16 15:06:30 +09002573static void print_thread_runtime(struct thread *t,
2574 struct thread_runtime *r)
2575{
2576 double mean = avg_stats(&r->run_stats);
2577 float stddev;
2578
2579 printf("%*s %5d %9" PRIu64 " ",
2580 comm_width, timehist_get_commstr(t), t->ppid,
2581 (u64) r->run_stats.n);
2582
2583 print_sched_time(r->total_run_time, 8);
2584 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2585 print_sched_time(r->run_stats.min, 6);
2586 printf(" ");
2587 print_sched_time((u64) mean, 6);
2588 printf(" ");
2589 print_sched_time(r->run_stats.max, 6);
2590 printf(" ");
2591 printf("%5.2f", stddev);
David Ahern350f54f2016-11-25 09:28:41 -07002592 printf(" %5" PRIu64, r->migrations);
David Ahern52df1382016-11-16 15:06:30 +09002593 printf("\n");
2594}
2595
2596struct total_run_stats {
2597 u64 sched_count;
2598 u64 task_count;
2599 u64 total_run_time;
2600};
2601
2602static int __show_thread_runtime(struct thread *t, void *priv)
2603{
2604 struct total_run_stats *stats = priv;
2605 struct thread_runtime *r;
2606
2607 if (thread__is_filtered(t))
2608 return 0;
2609
2610 r = thread__priv(t);
2611 if (r && r->run_stats.n) {
2612 stats->task_count++;
2613 stats->sched_count += r->run_stats.n;
2614 stats->total_run_time += r->total_run_time;
2615 print_thread_runtime(t, r);
2616 }
2617
2618 return 0;
2619}
2620
2621static int show_thread_runtime(struct thread *t, void *priv)
2622{
2623 if (t->dead)
2624 return 0;
2625
2626 return __show_thread_runtime(t, priv);
2627}
2628
2629static int show_deadthread_runtime(struct thread *t, void *priv)
2630{
2631 if (!t->dead)
2632 return 0;
2633
2634 return __show_thread_runtime(t, priv);
2635}
2636
Namhyung Kimba957eb2016-12-08 23:47:55 +09002637static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2638{
2639 const char *sep = " <- ";
2640 struct callchain_list *chain;
2641 size_t ret = 0;
2642 char bf[1024];
2643 bool first;
2644
2645 if (node == NULL)
2646 return 0;
2647
2648 ret = callchain__fprintf_folded(fp, node->parent);
2649 first = (ret == 0);
2650
2651 list_for_each_entry(chain, &node->val, list) {
2652 if (chain->ip >= PERF_CONTEXT_MAX)
2653 continue;
2654 if (chain->ms.sym && chain->ms.sym->ignore)
2655 continue;
2656 ret += fprintf(fp, "%s%s", first ? "" : sep,
2657 callchain_list__sym_name(chain, bf, sizeof(bf),
2658 false));
2659 first = false;
2660 }
2661
2662 return ret;
2663}
2664
2665static size_t timehist_print_idlehist_callchain(struct rb_root *root)
2666{
2667 size_t ret = 0;
2668 FILE *fp = stdout;
2669 struct callchain_node *chain;
2670 struct rb_node *rb_node = rb_first(root);
2671
2672 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
2673 printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
2674 graph_dotted_line);
2675
2676 while (rb_node) {
2677 chain = rb_entry(rb_node, struct callchain_node, rb_node);
2678 rb_node = rb_next(rb_node);
2679
2680 ret += fprintf(fp, " ");
2681 print_sched_time(chain->hit, 12);
2682 ret += 16; /* print_sched_time returns 2nd arg + 4 */
2683 ret += fprintf(fp, " %8d ", chain->count);
2684 ret += callchain__fprintf_folded(fp, chain);
2685 ret += fprintf(fp, "\n");
2686 }
2687
2688 return ret;
2689}
2690
David Ahern52df1382016-11-16 15:06:30 +09002691static void timehist_print_summary(struct perf_sched *sched,
2692 struct perf_session *session)
2693{
2694 struct machine *m = &session->machines.host;
2695 struct total_run_stats totals;
2696 u64 task_count;
2697 struct thread *t;
2698 struct thread_runtime *r;
2699 int i;
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002700 u64 hist_time = sched->hist_time.end - sched->hist_time.start;
David Ahern52df1382016-11-16 15:06:30 +09002701
2702 memset(&totals, 0, sizeof(totals));
2703
Namhyung Kim07235f82016-12-08 23:47:54 +09002704 if (sched->idle_hist) {
2705 printf("\nIdle-time summary\n");
2706 printf("%*s parent sched-out ", comm_width, "comm");
2707 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
2708 } else {
2709 printf("\nRuntime summary\n");
2710 printf("%*s parent sched-in ", comm_width, "comm");
2711 printf(" run-time min-run avg-run max-run stddev migrations\n");
2712 }
David Ahern52df1382016-11-16 15:06:30 +09002713 printf("%*s (count) ", comm_width, "");
2714 printf(" (msec) (msec) (msec) (msec) %%\n");
David Ahern350f54f2016-11-25 09:28:41 -07002715 printf("%.117s\n", graph_dotted_line);
David Ahern52df1382016-11-16 15:06:30 +09002716
2717 machine__for_each_thread(m, show_thread_runtime, &totals);
2718 task_count = totals.task_count;
2719 if (!task_count)
2720 printf("<no still running tasks>\n");
2721
2722 printf("\nTerminated tasks:\n");
2723 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2724 if (task_count == totals.task_count)
2725 printf("<no terminated tasks>\n");
2726
2727 /* CPU idle stats not tracked when samples were skipped */
Namhyung Kim07235f82016-12-08 23:47:54 +09002728 if (sched->skipped_samples && !sched->idle_hist)
David Ahern52df1382016-11-16 15:06:30 +09002729 return;
2730
2731 printf("\nIdle stats:\n");
Namhyung Kimb3363522016-12-06 12:40:05 +09002732 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern52df1382016-11-16 15:06:30 +09002733 t = idle_threads[i];
2734 if (!t)
2735 continue;
2736
2737 r = thread__priv(t);
2738 if (r && r->run_stats.n) {
2739 totals.sched_count += r->run_stats.n;
2740 printf(" CPU %2d idle for ", i);
2741 print_sched_time(r->total_run_time, 6);
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002742 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
David Ahern52df1382016-11-16 15:06:30 +09002743 } else
2744 printf(" CPU %2d idle entire time window\n", i);
2745 }
2746
Namhyung Kimba957eb2016-12-08 23:47:55 +09002747 if (sched->idle_hist && symbol_conf.use_callchain) {
2748 callchain_param.mode = CHAIN_FOLDED;
2749 callchain_param.value = CCVAL_PERIOD;
2750
2751 callchain_register_param(&callchain_param);
2752
2753 printf("\nIdle stats by callchain:\n");
2754 for (i = 0; i < idle_max_cpu; ++i) {
2755 struct idle_thread_runtime *itr;
2756
2757 t = idle_threads[i];
2758 if (!t)
2759 continue;
2760
2761 itr = thread__priv(t);
2762 if (itr == NULL)
2763 continue;
2764
2765 callchain_param.sort(&itr->sorted_root, &itr->callchain,
2766 0, &callchain_param);
2767
2768 printf(" CPU %2d:", i);
2769 print_sched_time(itr->tr.total_run_time, 6);
2770 printf(" msec\n");
2771 timehist_print_idlehist_callchain(&itr->sorted_root);
2772 printf("\n");
2773 }
2774 }
2775
David Ahern52df1382016-11-16 15:06:30 +09002776 printf("\n"
2777 " Total number of unique tasks: %" PRIu64 "\n"
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002778 "Total number of context switches: %" PRIu64 "\n",
David Ahern52df1382016-11-16 15:06:30 +09002779 totals.task_count, totals.sched_count);
2780
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002781 printf(" Total run time (msec): ");
David Ahern52df1382016-11-16 15:06:30 +09002782 print_sched_time(totals.total_run_time, 2);
2783 printf("\n");
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002784
2785 printf(" Total scheduling time (msec): ");
2786 print_sched_time(hist_time, 2);
2787 printf(" (x %d)\n", sched->max_cpu);
David Ahern52df1382016-11-16 15:06:30 +09002788}
2789
David Ahern49394a22016-11-16 15:06:29 +09002790typedef int (*sched_handler)(struct perf_tool *tool,
2791 union perf_event *event,
2792 struct perf_evsel *evsel,
2793 struct perf_sample *sample,
2794 struct machine *machine);
2795
2796static int perf_timehist__process_sample(struct perf_tool *tool,
2797 union perf_event *event,
2798 struct perf_sample *sample,
2799 struct perf_evsel *evsel,
2800 struct machine *machine)
2801{
2802 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2803 int err = 0;
2804 int this_cpu = sample->cpu;
2805
2806 if (this_cpu > sched->max_cpu)
2807 sched->max_cpu = this_cpu;
2808
2809 if (evsel->handler != NULL) {
2810 sched_handler f = evsel->handler;
2811
2812 err = f(tool, event, evsel, sample, machine);
2813 }
2814
2815 return err;
2816}
2817
David Ahern6c973c92016-11-16 15:06:32 +09002818static int timehist_check_attr(struct perf_sched *sched,
2819 struct perf_evlist *evlist)
2820{
2821 struct perf_evsel *evsel;
2822 struct evsel_runtime *er;
2823
2824 list_for_each_entry(evsel, &evlist->entries, node) {
2825 er = perf_evsel__get_runtime(evsel);
2826 if (er == NULL) {
2827 pr_err("Failed to allocate memory for evsel runtime data\n");
2828 return -1;
2829 }
2830
2831 if (sched->show_callchain &&
2832 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2833 pr_info("Samples do not have callchains.\n");
2834 sched->show_callchain = 0;
2835 symbol_conf.use_callchain = 0;
2836 }
2837 }
2838
2839 return 0;
2840}
2841
David Ahern49394a22016-11-16 15:06:29 +09002842static int perf_sched__timehist(struct perf_sched *sched)
2843{
2844 const struct perf_evsel_str_handler handlers[] = {
2845 { "sched:sched_switch", timehist_sched_switch_event, },
2846 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2847 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2848 };
David Ahern350f54f2016-11-25 09:28:41 -07002849 const struct perf_evsel_str_handler migrate_handlers[] = {
2850 { "sched:sched_migrate_task", timehist_migrate_task_event, },
2851 };
David Ahern49394a22016-11-16 15:06:29 +09002852 struct perf_data_file file = {
2853 .path = input_name,
2854 .mode = PERF_DATA_MODE_READ,
Namhyung Kim6fa94252016-12-06 12:40:01 +09002855 .force = sched->force,
David Ahern49394a22016-11-16 15:06:29 +09002856 };
2857
2858 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002859 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002860 int err = -1;
2861
2862 /*
2863 * event handlers for timehist option
2864 */
2865 sched->tool.sample = perf_timehist__process_sample;
2866 sched->tool.mmap = perf_event__process_mmap;
2867 sched->tool.comm = perf_event__process_comm;
2868 sched->tool.exit = perf_event__process_exit;
2869 sched->tool.fork = perf_event__process_fork;
2870 sched->tool.lost = process_lost;
2871 sched->tool.attr = perf_event__process_attr;
2872 sched->tool.tracing_data = perf_event__process_tracing_data;
2873 sched->tool.build_id = perf_event__process_build_id;
2874
2875 sched->tool.ordered_events = true;
2876 sched->tool.ordering_requires_timestamps = true;
2877
David Ahern6c973c92016-11-16 15:06:32 +09002878 symbol_conf.use_callchain = sched->show_callchain;
2879
David Ahern49394a22016-11-16 15:06:29 +09002880 session = perf_session__new(&file, false, &sched->tool);
2881 if (session == NULL)
2882 return -ENOMEM;
2883
David Ahern52df1382016-11-16 15:06:30 +09002884 evlist = session->evlist;
2885
David Ahern49394a22016-11-16 15:06:29 +09002886 symbol__init(&session->header.env);
2887
David Ahern853b7402016-11-29 10:15:44 -07002888 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
2889 pr_err("Invalid time string\n");
2890 return -EINVAL;
2891 }
2892
David Ahern6c973c92016-11-16 15:06:32 +09002893 if (timehist_check_attr(sched, evlist) != 0)
2894 goto out;
2895
David Ahern49394a22016-11-16 15:06:29 +09002896 setup_pager();
2897
2898 /* setup per-evsel handlers */
2899 if (perf_session__set_tracepoints_handlers(session, handlers))
2900 goto out;
2901
David Ahernf45bf8d2016-11-29 13:39:48 -07002902 /* sched_switch event at a minimum needs to exist */
2903 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
2904 "sched:sched_switch")) {
2905 pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
David Ahern49394a22016-11-16 15:06:29 +09002906 goto out;
David Ahernf45bf8d2016-11-29 13:39:48 -07002907 }
David Ahern49394a22016-11-16 15:06:29 +09002908
David Ahern350f54f2016-11-25 09:28:41 -07002909 if (sched->show_migrations &&
2910 perf_session__set_tracepoints_handlers(session, migrate_handlers))
2911 goto out;
2912
David Ahern49394a22016-11-16 15:06:29 +09002913 /* pre-allocate struct for per-CPU idle stats */
2914 sched->max_cpu = session->header.env.nr_cpus_online;
2915 if (sched->max_cpu == 0)
2916 sched->max_cpu = 4;
2917 if (init_idle_threads(sched->max_cpu))
2918 goto out;
2919
David Ahern52df1382016-11-16 15:06:30 +09002920 /* summary_only implies summary option, but don't overwrite summary if set */
2921 if (sched->summary_only)
2922 sched->summary = sched->summary_only;
2923
2924 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09002925 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09002926
2927 err = perf_session__process_events(session);
2928 if (err) {
2929 pr_err("Failed to process events, error %d", err);
2930 goto out;
2931 }
2932
David Ahern52df1382016-11-16 15:06:30 +09002933 sched->nr_events = evlist->stats.nr_events[0];
2934 sched->nr_lost_events = evlist->stats.total_lost;
2935 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2936
2937 if (sched->summary)
2938 timehist_print_summary(sched, session);
2939
David Ahern49394a22016-11-16 15:06:29 +09002940out:
2941 free_idle_threads();
2942 perf_session__delete(session);
2943
2944 return err;
2945}
2946
2947
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002948static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002949{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002950 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002951 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002952 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2953 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002954 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002955 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002956 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002957 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2958 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002959 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002960 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002961 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002962 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2963 sched->nr_context_switch_bugs, sched->nr_timestamps);
2964 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002965 printf(" (due to lost events?)");
2966 printf("\n");
2967 }
2968}
2969
Josef Bacik2f80dd42015-05-22 09:18:40 -04002970static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2971{
2972 struct rb_node **new = &(root->rb_node), *parent = NULL;
2973 struct work_atoms *this;
2974 const char *comm = thread__comm_str(data->thread), *this_comm;
2975
2976 while (*new) {
2977 int cmp;
2978
2979 this = container_of(*new, struct work_atoms, node);
2980 parent = *new;
2981
2982 this_comm = thread__comm_str(this->thread);
2983 cmp = strcmp(comm, this_comm);
2984 if (cmp > 0) {
2985 new = &((*new)->rb_left);
2986 } else if (cmp < 0) {
2987 new = &((*new)->rb_right);
2988 } else {
2989 this->num_merged++;
2990 this->total_runtime += data->total_runtime;
2991 this->nb_atoms += data->nb_atoms;
2992 this->total_lat += data->total_lat;
2993 list_splice(&data->work_list, &this->work_list);
2994 if (this->max_lat < data->max_lat) {
2995 this->max_lat = data->max_lat;
2996 this->max_lat_at = data->max_lat_at;
2997 }
2998 zfree(&data);
2999 return;
3000 }
3001 }
3002
3003 data->num_merged++;
3004 rb_link_node(&data->node, parent, new);
3005 rb_insert_color(&data->node, root);
3006}
3007
3008static void perf_sched__merge_lat(struct perf_sched *sched)
3009{
3010 struct work_atoms *data;
3011 struct rb_node *node;
3012
3013 if (sched->skip_merge)
3014 return;
3015
3016 while ((node = rb_first(&sched->atom_root))) {
3017 rb_erase(node, &sched->atom_root);
3018 data = rb_entry(node, struct work_atoms, node);
3019 __merge_work_atoms(&sched->merged_atom_root, data);
3020 }
3021}
3022
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003023static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003024{
3025 struct rb_node *next;
3026
3027 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04003028
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003029 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003030 return -1;
David Ahernad9def72013-08-07 22:50:44 -04003031
Josef Bacik2f80dd42015-05-22 09:18:40 -04003032 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003033 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003034
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003035 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
3036 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
3037 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003038
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003039 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003040
3041 while (next) {
3042 struct work_atoms *work_list;
3043
3044 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003045 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003046 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003047 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003048 }
3049
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003050 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003051 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03003052 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003053
3054 printf(" ---------------------------------------------------\n");
3055
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003056 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003057 printf("\n");
3058
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003059 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003060}
3061
Jiri Olsa99623c62016-04-12 15:29:26 +02003062static int setup_map_cpus(struct perf_sched *sched)
3063{
Jiri Olsa73643bb2016-04-12 15:29:31 +02003064 struct cpu_map *map;
3065
Jiri Olsa99623c62016-04-12 15:29:26 +02003066 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
3067
3068 if (sched->map.comp) {
3069 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02003070 if (!sched->map.comp_cpus)
3071 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02003072 }
3073
Jiri Olsa73643bb2016-04-12 15:29:31 +02003074 if (!sched->map.cpus_str)
3075 return 0;
3076
3077 map = cpu_map__new(sched->map.cpus_str);
3078 if (!map) {
3079 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3080 return -1;
3081 }
3082
3083 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02003084 return 0;
3085}
3086
Jiri Olsaa151a372016-04-12 15:29:29 +02003087static int setup_color_pids(struct perf_sched *sched)
3088{
3089 struct thread_map *map;
3090
3091 if (!sched->map.color_pids_str)
3092 return 0;
3093
3094 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3095 if (!map) {
3096 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3097 return -1;
3098 }
3099
3100 sched->map.color_pids = map;
3101 return 0;
3102}
3103
Jiri Olsacf294f22016-04-12 15:29:30 +02003104static int setup_color_cpus(struct perf_sched *sched)
3105{
3106 struct cpu_map *map;
3107
3108 if (!sched->map.color_cpus_str)
3109 return 0;
3110
3111 map = cpu_map__new(sched->map.color_cpus_str);
3112 if (!map) {
3113 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3114 return -1;
3115 }
3116
3117 sched->map.color_cpus = map;
3118 return 0;
3119}
3120
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003121static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003122{
Jiri Olsa99623c62016-04-12 15:29:26 +02003123 if (setup_map_cpus(sched))
3124 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02003125
Jiri Olsaa151a372016-04-12 15:29:29 +02003126 if (setup_color_pids(sched))
3127 return -1;
3128
Jiri Olsacf294f22016-04-12 15:29:30 +02003129 if (setup_color_cpus(sched))
3130 return -1;
3131
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003132 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003133 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003134 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003135 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003136 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003137}
3138
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003139static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003140{
3141 unsigned long i;
3142
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003143 calibrate_run_measurement_overhead(sched);
3144 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003145
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003146 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003147
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003148 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003149 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003150
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003151 printf("nr_run_events: %ld\n", sched->nr_run_events);
3152 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
3153 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003154
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003155 if (sched->targetless_wakeups)
3156 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
3157 if (sched->multitarget_wakeups)
3158 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
3159 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003160 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003161 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003162
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003163 print_task_traces(sched);
3164 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003165
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003166 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003167 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003168 for (i = 0; i < sched->replay_repeat; i++)
3169 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003170
3171 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003172}
3173
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003174static void setup_sorting(struct perf_sched *sched, const struct option *options,
3175 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003176{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003177 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003178
3179 for (tok = strtok_r(str, ", ", &tmp);
3180 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003181 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003182 usage_with_options_msg(usage_msg, options,
3183 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003184 }
3185 }
3186
3187 free(str);
3188
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003189 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003190}
3191
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003192static int __cmd_record(int argc, const char **argv)
3193{
3194 unsigned int rec_argc, i, j;
3195 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003196 const char * const record_args[] = {
3197 "record",
3198 "-a",
3199 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003200 "-m", "1024",
3201 "-c", "1",
3202 "-e", "sched:sched_switch",
3203 "-e", "sched:sched_stat_wait",
3204 "-e", "sched:sched_stat_sleep",
3205 "-e", "sched:sched_stat_iowait",
3206 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003207 "-e", "sched:sched_process_fork",
3208 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09003209 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003210 "-e", "sched:sched_migrate_task",
3211 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003212
3213 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
3214 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3215
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02003216 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11003217 return -ENOMEM;
3218
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003219 for (i = 0; i < ARRAY_SIZE(record_args); i++)
3220 rec_argv[i] = strdup(record_args[i]);
3221
3222 for (j = 1; j < (unsigned int)argc; j++, i++)
3223 rec_argv[i] = argv[j];
3224
3225 BUG_ON(i != rec_argc);
3226
3227 return cmd_record(i, rec_argv, NULL);
3228}
3229
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003230int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003231{
Adrian Hunter8a39df82013-10-22 10:34:15 +03003232 const char default_sort_order[] = "avg, max, switch, runtime";
3233 struct perf_sched sched = {
3234 .tool = {
3235 .sample = perf_sched__process_tracepoint_sample,
3236 .comm = perf_event__process_comm,
3237 .lost = perf_event__process_lost,
3238 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003239 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003240 },
3241 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
3242 .sort_list = LIST_HEAD_INIT(sched.sort_list),
3243 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
3244 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003245 .sort_order = default_sort_order,
3246 .replay_repeat = 10,
3247 .profile_cpu = -1,
3248 .next_shortname1 = 'A',
3249 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04003250 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09003251 .show_callchain = 1,
3252 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003253 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09003254 const struct option sched_options[] = {
3255 OPT_STRING('i', "input", &input_name, "file",
3256 "input file name"),
3257 OPT_INCR('v', "verbose", &verbose,
3258 "be more verbose (show symbol address, etc)"),
3259 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3260 "dump raw trace in ASCII"),
Namhyung Kim6fa94252016-12-06 12:40:01 +09003261 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003262 OPT_END()
3263 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003264 const struct option latency_options[] = {
3265 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
3266 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003267 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
3268 "CPU to profile on"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04003269 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
3270 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003271 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003272 };
3273 const struct option replay_options[] = {
3274 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
3275 "repeat the workload replay N times (-1: infinite)"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003276 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003277 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003278 const struct option map_options[] = {
3279 OPT_BOOLEAN(0, "compact", &sched.map.comp,
3280 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02003281 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3282 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02003283 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3284 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02003285 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
3286 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003287 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02003288 };
David Ahern49394a22016-11-16 15:06:29 +09003289 const struct option timehist_options[] = {
3290 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3291 "file", "vmlinux pathname"),
3292 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3293 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09003294 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
3295 "Display call chains if present (default on)"),
3296 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
3297 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09003298 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
3299 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09003300 OPT_BOOLEAN('s', "summary", &sched.summary_only,
3301 "Show only syscall summary with statistics"),
3302 OPT_BOOLEAN('S', "with-summary", &sched.summary,
3303 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09003304 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Ahern350f54f2016-11-25 09:28:41 -07003305 OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
David Aherna407b062016-11-16 15:06:33 +09003306 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
Namhyung Kim07235f82016-12-08 23:47:54 +09003307 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
David Ahern853b7402016-11-29 10:15:44 -07003308 OPT_STRING(0, "time", &sched.time_str, "str",
3309 "Time span for analysis (start,stop)"),
Namhyung Kim414e0502017-01-13 19:45:22 +09003310 OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"),
David Ahern49394a22016-11-16 15:06:29 +09003311 OPT_PARENT(sched_options)
3312 };
3313
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003314 const char * const latency_usage[] = {
3315 "perf sched latency [<options>]",
3316 NULL
3317 };
3318 const char * const replay_usage[] = {
3319 "perf sched replay [<options>]",
3320 NULL
3321 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003322 const char * const map_usage[] = {
3323 "perf sched map [<options>]",
3324 NULL
3325 };
David Ahern49394a22016-11-16 15:06:29 +09003326 const char * const timehist_usage[] = {
3327 "perf sched timehist [<options>]",
3328 NULL
3329 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003330 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09003331 "replay", "script",
3332 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003333 const char *sched_usage[] = {
3334 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003335 NULL
3336 };
3337 struct trace_sched_handler lat_ops = {
3338 .wakeup_event = latency_wakeup_event,
3339 .switch_event = latency_switch_event,
3340 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003341 .migrate_task_event = latency_migrate_task_event,
3342 };
3343 struct trace_sched_handler map_ops = {
3344 .switch_event = map_switch_event,
3345 };
3346 struct trace_sched_handler replay_ops = {
3347 .wakeup_event = replay_wakeup_event,
3348 .switch_event = replay_switch_event,
3349 .fork_event = replay_fork_event,
3350 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03003351 unsigned int i;
3352
3353 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3354 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003355
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003356 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3357 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003358 if (!argc)
3359 usage_with_options(sched_usage, sched_options);
3360
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003361 /*
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003362 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003363 */
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003364 if (!strcmp(argv[0], "script"))
3365 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003366
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003367 if (!strncmp(argv[0], "rec", 3)) {
3368 return __cmd_record(argc, argv);
3369 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003370 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003371 if (argc > 1) {
3372 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3373 if (argc)
3374 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003375 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003376 setup_sorting(&sched, latency_options, latency_usage);
3377 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003378 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02003379 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02003380 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02003381 if (argc)
3382 usage_with_options(map_usage, map_options);
3383 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003384 sched.tp_handler = &map_ops;
3385 setup_sorting(&sched, latency_options, latency_usage);
3386 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003387 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003388 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003389 if (argc) {
3390 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3391 if (argc)
3392 usage_with_options(replay_usage, replay_options);
3393 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003394 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09003395 } else if (!strcmp(argv[0], "timehist")) {
3396 if (argc) {
3397 argc = parse_options(argc, argv, timehist_options,
3398 timehist_usage, 0);
3399 if (argc)
3400 usage_with_options(timehist_usage, timehist_options);
3401 }
David Ahernfc1469f2016-11-16 15:06:31 +09003402 if (sched.show_wakeups && sched.summary_only) {
3403 pr_err(" Error: -s and -w are mutually exclusive.\n");
3404 parse_options_usage(timehist_usage, timehist_options, "s", true);
3405 parse_options_usage(NULL, timehist_options, "w", true);
3406 return -EINVAL;
3407 }
3408
David Ahern49394a22016-11-16 15:06:29 +09003409 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003410 } else {
3411 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003412 }
3413
Ingo Molnarec156762009-09-11 12:12:54 +02003414 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003415}