blob: 6d3c3e84881ae88c07a1bb6c42d3ad574f90a3f9 [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;
David Ahern52df1382016-11-16 15:06:30 +0900225 u64 skipped_samples;
David Ahern853b7402016-11-29 10:15:44 -0700226 const char *time_str;
227 struct perf_time_interval ptime;
Namhyung Kim9396c9c2016-12-22 15:03:50 +0900228 struct perf_time_interval hist_time;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300229};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200230
David Ahern49394a22016-11-16 15:06:29 +0900231/* per thread run time data */
232struct thread_runtime {
233 u64 last_time; /* time of previous sched in/out event */
234 u64 dt_run; /* run time */
Namhyung Kim941bdea2017-01-13 19:45:21 +0900235 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
236 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
237 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
David Ahern49394a22016-11-16 15:06:29 +0900238 u64 dt_delay; /* time between wakeup and sched-in */
239 u64 ready_to_run; /* time of wakeup */
240
241 struct stats run_stats;
242 u64 total_run_time;
David Ahern350f54f2016-11-25 09:28:41 -0700243
Namhyung Kim941bdea2017-01-13 19:45:21 +0900244 int last_state;
David Ahern350f54f2016-11-25 09:28:41 -0700245 u64 migrations;
David Ahern49394a22016-11-16 15:06:29 +0900246};
247
248/* per event run time data */
249struct evsel_runtime {
250 u64 *last_time; /* time this event was last seen per cpu */
251 u32 ncpu; /* highest cpu slot allocated */
252};
253
Namhyung Kim3bc2fa92016-12-08 23:47:51 +0900254/* per cpu idle time data */
255struct idle_thread_runtime {
256 struct thread_runtime tr;
257 struct thread *last_thread;
258 struct rb_root sorted_root;
259 struct callchain_root callchain;
260 struct callchain_cursor cursor;
261};
262
David Ahern49394a22016-11-16 15:06:29 +0900263/* track idle times per cpu */
264static struct thread **idle_threads;
265static int idle_max_cpu;
266static char idle_comm[] = "<idle>";
267
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200268static u64 get_nsecs(void)
269{
270 struct timespec ts;
271
272 clock_gettime(CLOCK_MONOTONIC, &ts);
273
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300274 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200275}
276
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300277static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200278{
279 u64 T0 = get_nsecs(), T1;
280
281 do {
282 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300283 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200284}
285
286static void sleep_nsecs(u64 nsecs)
287{
288 struct timespec ts;
289
290 ts.tv_nsec = nsecs % 999999999;
291 ts.tv_sec = nsecs / 999999999;
292
293 nanosleep(&ts, NULL);
294}
295
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300296static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200297{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300298 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200299 int i;
300
301 for (i = 0; i < 10; i++) {
302 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300303 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200304 T1 = get_nsecs();
305 delta = T1-T0;
306 min_delta = min(min_delta, delta);
307 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300308 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200309
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200310 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200311}
312
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300313static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200314{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300315 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200316 int i;
317
318 for (i = 0; i < 10; i++) {
319 T0 = get_nsecs();
320 sleep_nsecs(10000);
321 T1 = get_nsecs();
322 delta = T1-T0;
323 min_delta = min(min_delta, delta);
324 }
325 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300326 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200327
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200328 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200329}
330
mingo39aeb522009-09-14 20:04:48 +0200331static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200332get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200333{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200334 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200335 unsigned long idx = task->nr_events;
336 size_t size;
337
338 event->timestamp = timestamp;
339 event->nr = idx;
340
341 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200342 size = sizeof(struct sched_atom *) * task->nr_events;
343 task->atoms = realloc(task->atoms, size);
344 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200345
mingo39aeb522009-09-14 20:04:48 +0200346 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200347
348 return event;
349}
350
mingo39aeb522009-09-14 20:04:48 +0200351static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200352{
353 if (!task->nr_events)
354 return NULL;
355
mingo39aeb522009-09-14 20:04:48 +0200356 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200357}
358
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300359static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
360 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200361{
mingo39aeb522009-09-14 20:04:48 +0200362 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200363
364 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200365 * optimize an existing RUN event by merging this one
366 * to it:
367 */
Ingo Molnarec156762009-09-11 12:12:54 +0200368 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300369 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200370 curr_event->duration += duration;
371 return;
372 }
373
374 event = get_new_event(task, timestamp);
375
376 event->type = SCHED_EVENT_RUN;
377 event->duration = duration;
378
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300379 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200380}
381
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300382static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
383 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200384{
mingo39aeb522009-09-14 20:04:48 +0200385 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200386
387 event = get_new_event(task, timestamp);
388 event->type = SCHED_EVENT_WAKEUP;
389 event->wakee = wakee;
390
391 wakee_event = last_event(wakee);
392 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300393 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200394 return;
395 }
396 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300397 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200398 return;
399 }
400
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200401 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200402 sem_init(wakee_event->wait_sem, 0, 0);
403 wakee_event->specific_wait = 1;
404 event->wait_sem = wakee_event->wait_sem;
405
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300406 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200407}
408
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300409static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
410 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200411{
mingo39aeb522009-09-14 20:04:48 +0200412 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200413
414 event->type = SCHED_EVENT_SLEEP;
415
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300416 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200417}
418
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300419static struct task_desc *register_pid(struct perf_sched *sched,
420 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200421{
422 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800423 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200424
Yunlong Songcb06ac22015-03-31 21:46:30 +0800425 if (sched->pid_to_task == NULL) {
426 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
427 pid_max = MAX_PID;
428 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
429 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800430 if (pid >= (unsigned long)pid_max) {
431 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
432 sizeof(struct task_desc *))) == NULL);
433 while (pid >= (unsigned long)pid_max)
434 sched->pid_to_task[pid_max++] = NULL;
435 }
Ingo Molnarec156762009-09-11 12:12:54 +0200436
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300437 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200438
439 if (task)
440 return task;
441
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200442 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200443 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300444 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200445 strcpy(task->comm, comm);
446 /*
447 * every task starts in sleeping state - this gets ignored
448 * if there's no wakeup pointing to this sleep state:
449 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300450 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200451
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300452 sched->pid_to_task[pid] = task;
453 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800454 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300455 BUG_ON(!sched->tasks);
456 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200457
Ingo Molnarad236fd2009-09-11 12:12:54 +0200458 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300459 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200460
461 return task;
462}
463
464
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300465static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200466{
467 struct task_desc *task;
468 unsigned long i;
469
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300470 for (i = 0; i < sched->nr_tasks; i++) {
471 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200472 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200473 task->nr, task->comm, task->pid, task->nr_events);
474 }
475}
476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300477static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200478{
479 struct task_desc *task1, *task2;
480 unsigned long i, j;
481
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300482 for (i = 0; i < sched->nr_tasks; i++) {
483 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200484 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300485 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200486 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300487 task2 = sched->tasks[j];
488 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200489 }
490}
491
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300492static void perf_sched__process_event(struct perf_sched *sched,
493 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200494{
495 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200496
mingo39aeb522009-09-14 20:04:48 +0200497 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200498 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300499 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200500 break;
501 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200502 if (atom->wait_sem)
503 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200504 BUG_ON(ret);
505 break;
506 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200507 if (atom->wait_sem)
508 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200509 BUG_ON(ret);
510 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200511 case SCHED_EVENT_MIGRATION:
512 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200513 default:
514 BUG_ON(1);
515 }
516}
517
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200518static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200519{
520 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200521 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200522 int err;
523
524 err = getrusage(RUSAGE_SELF, &ru);
525 BUG_ON(err);
526
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300527 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
528 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200529
530 return sum;
531}
532
Yunlong Song939cda5212015-03-31 21:46:34 +0800533static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200534{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800535 struct perf_event_attr attr;
Yunlong Song939cda5212015-03-31 21:46:34 +0800536 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800537 int fd;
Yunlong Song939cda5212015-03-31 21:46:34 +0800538 struct rlimit limit;
539 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800540
541 memset(&attr, 0, sizeof(attr));
542
543 attr.type = PERF_TYPE_SOFTWARE;
544 attr.config = PERF_COUNT_SW_TASK_CLOCK;
545
Yunlong Song939cda5212015-03-31 21:46:34 +0800546force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200547 fd = sys_perf_event_open(&attr, 0, -1, -1,
548 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800549
Yunlong Song1aff59b2015-03-31 21:46:33 +0800550 if (fd < 0) {
Yunlong Song939cda5212015-03-31 21:46:34 +0800551 if (errno == EMFILE) {
552 if (sched->force) {
553 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
554 limit.rlim_cur += sched->nr_tasks - cur_task;
555 if (limit.rlim_cur > limit.rlim_max) {
556 limit.rlim_max = limit.rlim_cur;
557 need_privilege = true;
558 }
559 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
560 if (need_privilege && errno == EPERM)
561 strcpy(info, "Need privilege\n");
562 } else
563 goto force_again;
564 } else
565 strcpy(info, "Have a try with -f option\n");
566 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900567 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda5212015-03-31 21:46:34 +0800568 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300569 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800570 exit(EXIT_FAILURE);
571 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800572 return fd;
573}
574
575static u64 get_cpu_usage_nsec_self(int fd)
576{
577 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200578 int ret;
579
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800580 ret = read(fd, &runtime, sizeof(runtime));
581 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200582
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800583 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200584}
585
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300586struct sched_thread_parms {
587 struct task_desc *task;
588 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800589 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590};
591
Ingo Molnarec156762009-09-11 12:12:54 +0200592static void *thread_func(void *ctx)
593{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300594 struct sched_thread_parms *parms = ctx;
595 struct task_desc *this_task = parms->task;
596 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200597 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200598 unsigned long i, ret;
599 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800600 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200601
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300602 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300603
Ingo Molnarec156762009-09-11 12:12:54 +0200604 sprintf(comm2, ":%s", this_task->comm);
605 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300606 if (fd < 0)
607 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200608again:
609 ret = sem_post(&this_task->ready_for_work);
610 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200612 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300613 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200614 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200615
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800616 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200617
618 for (i = 0; i < this_task->nr_events; i++) {
619 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300620 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200621 }
622
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800623 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200624 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200625 ret = sem_post(&this_task->work_done_sem);
626 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200627
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300628 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200629 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300630 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200631 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200632
633 goto again;
634}
635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300636static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200637{
638 struct task_desc *task;
639 pthread_attr_t attr;
640 unsigned long i;
641 int err;
642
643 err = pthread_attr_init(&attr);
644 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200645 err = pthread_attr_setstacksize(&attr,
646 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200647 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300648 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200649 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200651 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300652 for (i = 0; i < sched->nr_tasks; i++) {
653 struct sched_thread_parms *parms = malloc(sizeof(*parms));
654 BUG_ON(parms == NULL);
655 parms->task = task = sched->tasks[i];
656 parms->sched = sched;
Yunlong Song939cda5212015-03-31 21:46:34 +0800657 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200658 sem_init(&task->sleep_sem, 0, 0);
659 sem_init(&task->ready_for_work, 0, 0);
660 sem_init(&task->work_done_sem, 0, 0);
661 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300662 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200663 BUG_ON(err);
664 }
665}
666
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300667static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200668{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200669 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200670 struct task_desc *task;
671 unsigned long i, ret;
672
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 sched->start_time = get_nsecs();
674 sched->cpu_usage = 0;
675 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200676
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300677 for (i = 0; i < sched->nr_tasks; i++) {
678 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200679 ret = sem_wait(&task->ready_for_work);
680 BUG_ON(ret);
681 sem_init(&task->ready_for_work, 0, 0);
682 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300683 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200684 BUG_ON(ret);
685
686 cpu_usage_0 = get_cpu_usage_nsec_parent();
687
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300688 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200689
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300690 for (i = 0; i < sched->nr_tasks; i++) {
691 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200692 ret = sem_wait(&task->work_done_sem);
693 BUG_ON(ret);
694 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300695 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200696 task->cpu_usage = 0;
697 }
698
699 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300700 if (!sched->runavg_cpu_usage)
701 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800702 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 +0200703
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300704 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
705 if (!sched->runavg_parent_cpu_usage)
706 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800707 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
708 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200709
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300710 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200711 BUG_ON(ret);
712
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300713 for (i = 0; i < sched->nr_tasks; i++) {
714 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200715 sem_init(&task->sleep_sem, 0, 0);
716 task->curr_event = 0;
717 }
718}
719
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300720static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200721{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500722 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200723
724 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300725 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200726 T1 = get_nsecs();
727
728 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300729 sched->sum_runtime += delta;
730 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200731
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300732 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200733 if (delta < avg_delta)
734 fluct = avg_delta - delta;
735 else
736 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300737 sched->sum_fluct += fluct;
738 if (!sched->run_avg)
739 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800740 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200741
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300742 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200743
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300744 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200745
Ingo Molnarad236fd2009-09-11 12:12:54 +0200746 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300747 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200748
749#if 0
750 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200751 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300752 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200753 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200754 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300755 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
756 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200757#endif
758
Ingo Molnarad236fd2009-09-11 12:12:54 +0200759 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200760
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300761 if (sched->nr_sleep_corrections)
762 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
763 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200764}
765
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300766static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200767{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200768 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200769
770 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300771 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200772 T1 = get_nsecs();
773
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200774 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200775
776 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300777 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200778 T1 = get_nsecs();
779
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200780 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200781}
782
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300783static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300784replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300785 struct perf_evsel *evsel, struct perf_sample *sample,
786 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200787{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300788 const char *comm = perf_evsel__strval(evsel, sample, "comm");
789 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200790 struct task_desc *waker, *wakee;
791
792 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300793 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200794
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300795 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200796 }
797
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300798 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300799 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200800
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300801 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300802 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200803}
804
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300805static int replay_switch_event(struct perf_sched *sched,
806 struct perf_evsel *evsel,
807 struct perf_sample *sample,
808 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200809{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300810 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
811 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
812 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
813 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
814 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300815 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300816 u64 timestamp0, timestamp = sample->time;
817 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200818 s64 delta;
819
Ingo Molnarad236fd2009-09-11 12:12:54 +0200820 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300821 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200822
Ingo Molnarfbf94822009-09-11 12:12:54 +0200823 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300824 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200825
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300826 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200827 if (timestamp0)
828 delta = timestamp - timestamp0;
829 else
830 delta = 0;
831
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300832 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900833 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300834 return -1;
835 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200836
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300837 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
838 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200839
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300840 prev = register_pid(sched, prev_pid, prev_comm);
841 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200842
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300843 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200844
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300845 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300846 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300847
848 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200849}
850
David Aherncb627502013-08-07 22:50:47 -0400851static int replay_fork_event(struct perf_sched *sched,
852 union perf_event *event,
853 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200854{
David Aherncb627502013-08-07 22:50:47 -0400855 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300856
Adrian Hunter314add62013-08-27 11:23:03 +0300857 child = machine__findnew_thread(machine, event->fork.pid,
858 event->fork.tid);
859 parent = machine__findnew_thread(machine, event->fork.ppid,
860 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400861
862 if (child == NULL || parent == NULL) {
863 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
864 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300865 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200866 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300867
David Aherncb627502013-08-07 22:50:47 -0400868 if (verbose) {
869 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200870 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
871 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400872 }
873
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200874 register_pid(sched, parent->tid, thread__comm_str(parent));
875 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300876out_put:
877 thread__put(child);
878 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300879 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200880}
881
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200882struct sort_dimension {
883 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200884 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200885 struct list_head list;
886};
887
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200888static int
mingo39aeb522009-09-14 20:04:48 +0200889thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200890{
891 struct sort_dimension *sort;
892 int ret = 0;
893
Ingo Molnarb5fae122009-09-11 12:12:54 +0200894 BUG_ON(list_empty(list));
895
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200896 list_for_each_entry(sort, list, list) {
897 ret = sort->cmp(l, r);
898 if (ret)
899 return ret;
900 }
901
902 return ret;
903}
904
mingo39aeb522009-09-14 20:04:48 +0200905static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200906thread_atoms_search(struct rb_root *root, struct thread *thread,
907 struct list_head *sort_list)
908{
909 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200910 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200911
912 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200913 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200914 int cmp;
915
mingo39aeb522009-09-14 20:04:48 +0200916 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200917
918 cmp = thread_lat_cmp(sort_list, &key, atoms);
919 if (cmp > 0)
920 node = node->rb_left;
921 else if (cmp < 0)
922 node = node->rb_right;
923 else {
924 BUG_ON(thread != atoms->thread);
925 return atoms;
926 }
927 }
928 return NULL;
929}
930
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200931static void
mingo39aeb522009-09-14 20:04:48 +0200932__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200933 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200934{
935 struct rb_node **new = &(root->rb_node), *parent = NULL;
936
937 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200938 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200939 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200940
mingo39aeb522009-09-14 20:04:48 +0200941 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200942 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200943
944 cmp = thread_lat_cmp(sort_list, data, this);
945
946 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200949 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200950 }
951
952 rb_link_node(&data->node, parent, new);
953 rb_insert_color(&data->node, root);
954}
955
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300956static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200958 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300959 if (!atoms) {
960 pr_err("No memory at %s\n", __func__);
961 return -1;
962 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200963
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300964 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200965 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300966 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300967 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200968}
969
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300970static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200971{
972 const char *str = TASK_STATE_TO_CHAR_STR;
973
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300974 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200975}
976
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300977static int
mingo39aeb522009-09-14 20:04:48 +0200978add_sched_out_event(struct work_atoms *atoms,
979 char run_state,
980 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200981{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200982 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300983 if (!atom) {
984 pr_err("Non memory at %s", __func__);
985 return -1;
986 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200988 atom->sched_out_time = timestamp;
989
mingo39aeb522009-09-14 20:04:48 +0200990 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200991 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200992 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200993 }
994
mingo39aeb522009-09-14 20:04:48 +0200995 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300996 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200997}
998
999static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001000add_runtime_event(struct work_atoms *atoms, u64 delta,
1001 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +02001002{
1003 struct work_atom *atom;
1004
1005 BUG_ON(list_empty(&atoms->work_list));
1006
1007 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1008
1009 atom->runtime += delta;
1010 atoms->total_runtime += delta;
1011}
1012
1013static void
1014add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001015{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001016 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001017 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001018
mingo39aeb522009-09-14 20:04:48 +02001019 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001020 return;
1021
mingo39aeb522009-09-14 20:04:48 +02001022 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001023
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001024 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001025 return;
1026
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001027 if (timestamp < atom->wake_up_time) {
1028 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001029 return;
1030 }
1031
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001032 atom->state = THREAD_SCHED_IN;
1033 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001034
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001035 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001036 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001037 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001038 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001039 atoms->max_lat_at = timestamp;
1040 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001041 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001042}
1043
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001044static int latency_switch_event(struct perf_sched *sched,
1045 struct perf_evsel *evsel,
1046 struct perf_sample *sample,
1047 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001048{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001049 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1050 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1051 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001052 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001053 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001054 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001055 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001056 s64 delta;
1057
mingo39aeb522009-09-14 20:04:48 +02001058 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001059
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001060 timestamp0 = sched->cpu_last_switched[cpu];
1061 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001062 if (timestamp0)
1063 delta = timestamp - timestamp0;
1064 else
1065 delta = 0;
1066
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001067 if (delta < 0) {
1068 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1069 return -1;
1070 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001071
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001072 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1073 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001074 if (sched_out == NULL || sched_in == NULL)
1075 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001076
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001077 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001078 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001079 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001080 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001081 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001082 if (!out_events) {
1083 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001084 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001085 }
mingo39aeb522009-09-14 20:04:48 +02001086 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001087 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001088 return -1;
mingo39aeb522009-09-14 20:04:48 +02001089
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001090 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001091 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001093 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001094 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 if (!in_events) {
1096 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001097 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001098 }
mingo39aeb522009-09-14 20:04:48 +02001099 /*
1100 * Take came in we have not heard about yet,
1101 * add in an initial atom in runnable state:
1102 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001103 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001104 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001105 }
1106 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001107 err = 0;
1108out_put:
1109 thread__put(sched_out);
1110 thread__put(sched_in);
1111 return err;
mingo39aeb522009-09-14 20:04:48 +02001112}
1113
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001114static int latency_runtime_event(struct perf_sched *sched,
1115 struct perf_evsel *evsel,
1116 struct perf_sample *sample,
1117 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001118{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001119 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1120 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001121 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001122 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001123 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001124 int cpu = sample->cpu, err = -1;
1125
1126 if (thread == NULL)
1127 return -1;
mingo39aeb522009-09-14 20:04:48 +02001128
1129 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001130 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001131 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001132 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001133 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001134 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001135 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001136 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001137 }
1138 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001139 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001140 }
1141
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001142 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001143 err = 0;
1144out_put:
1145 thread__put(thread);
1146 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001147}
1148
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001149static int latency_wakeup_event(struct perf_sched *sched,
1150 struct perf_evsel *evsel,
1151 struct perf_sample *sample,
1152 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001153{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001154 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001155 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001156 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001157 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001158 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001159 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001160
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001161 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001162 if (wakee == NULL)
1163 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001164 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001165 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001166 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001167 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001168 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001169 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001170 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001171 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001172 }
1173 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001174 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001175 }
1176
mingo39aeb522009-09-14 20:04:48 +02001177 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001178
mingo39aeb522009-09-14 20:04:48 +02001179 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001180
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001181 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001182 * As we do not guarantee the wakeup event happens when
1183 * task is out of run queue, also may happen when task is
1184 * on run queue and wakeup only change ->state to TASK_RUNNING,
1185 * then we should not set the ->wake_up_time when wake up a
1186 * task which is on run queue.
1187 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001188 * You WILL be missing events if you've recorded only
1189 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001190 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001191 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001192 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001193 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001194
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001195 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001196 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001197 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001198 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001199 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001200
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001201 atom->state = THREAD_WAIT_CPU;
1202 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001203out_ok:
1204 err = 0;
1205out_put:
1206 thread__put(wakee);
1207 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001208}
1209
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001210static int latency_migrate_task_event(struct perf_sched *sched,
1211 struct perf_evsel *evsel,
1212 struct perf_sample *sample,
1213 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001214{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001215 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001216 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001217 struct work_atoms *atoms;
1218 struct work_atom *atom;
1219 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001220 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001221
1222 /*
1223 * Only need to worry about migration when profiling one CPU.
1224 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001225 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001226 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001227
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001228 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001229 if (migrant == NULL)
1230 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001231 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001232 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001233 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001234 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001235 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001236 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001237 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001238 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001239 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001240 }
1241 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001242 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001243 }
1244
1245 BUG_ON(list_empty(&atoms->work_list));
1246
1247 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1248 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1249
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001250 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001251
1252 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001253 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001254 err = 0;
1255out_put:
1256 thread__put(migrant);
1257 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001258}
1259
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001260static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001261{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001262 int i;
1263 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001264 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001265 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001266
mingo39aeb522009-09-14 20:04:48 +02001267 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001268 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001269 /*
1270 * Ignore idle threads:
1271 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001272 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001273 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001274
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001275 sched->all_runtime += work_list->total_runtime;
1276 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001277
Josef Bacik2f80dd42015-05-22 09:18:40 -04001278 if (work_list->num_merged > 1)
1279 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1280 else
1281 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001282
mingo08f69e62009-09-14 18:30:44 +02001283 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001284 printf(" ");
1285
mingo39aeb522009-09-14 20:04:48 +02001286 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001287 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001288
Namhyung Kim99620a52016-10-24 11:02:45 +09001289 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 -03001290 (double)work_list->total_runtime / NSEC_PER_MSEC,
1291 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1292 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001293 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001294}
1295
mingo39aeb522009-09-14 20:04:48 +02001296static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001297{
Jiri Olsa0014de12015-11-02 12:10:25 +01001298 if (l->thread == r->thread)
1299 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001300 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001301 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001302 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001303 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001304 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001305}
1306
mingo39aeb522009-09-14 20:04:48 +02001307static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001308{
1309 u64 avgl, avgr;
1310
1311 if (!l->nb_atoms)
1312 return -1;
1313
1314 if (!r->nb_atoms)
1315 return 1;
1316
1317 avgl = l->total_lat / l->nb_atoms;
1318 avgr = r->total_lat / r->nb_atoms;
1319
1320 if (avgl < avgr)
1321 return -1;
1322 if (avgl > avgr)
1323 return 1;
1324
1325 return 0;
1326}
1327
mingo39aeb522009-09-14 20:04:48 +02001328static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001329{
1330 if (l->max_lat < r->max_lat)
1331 return -1;
1332 if (l->max_lat > r->max_lat)
1333 return 1;
1334
1335 return 0;
1336}
1337
mingo39aeb522009-09-14 20:04:48 +02001338static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001339{
1340 if (l->nb_atoms < r->nb_atoms)
1341 return -1;
1342 if (l->nb_atoms > r->nb_atoms)
1343 return 1;
1344
1345 return 0;
1346}
1347
mingo39aeb522009-09-14 20:04:48 +02001348static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001349{
1350 if (l->total_runtime < r->total_runtime)
1351 return -1;
1352 if (l->total_runtime > r->total_runtime)
1353 return 1;
1354
1355 return 0;
1356}
1357
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001358static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001359{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001360 size_t i;
1361 static struct sort_dimension avg_sort_dimension = {
1362 .name = "avg",
1363 .cmp = avg_cmp,
1364 };
1365 static struct sort_dimension max_sort_dimension = {
1366 .name = "max",
1367 .cmp = max_cmp,
1368 };
1369 static struct sort_dimension pid_sort_dimension = {
1370 .name = "pid",
1371 .cmp = pid_cmp,
1372 };
1373 static struct sort_dimension runtime_sort_dimension = {
1374 .name = "runtime",
1375 .cmp = runtime_cmp,
1376 };
1377 static struct sort_dimension switch_sort_dimension = {
1378 .name = "switch",
1379 .cmp = switch_cmp,
1380 };
1381 struct sort_dimension *available_sorts[] = {
1382 &pid_sort_dimension,
1383 &avg_sort_dimension,
1384 &max_sort_dimension,
1385 &switch_sort_dimension,
1386 &runtime_sort_dimension,
1387 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001388
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001389 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001390 if (!strcmp(available_sorts[i]->name, tok)) {
1391 list_add_tail(&available_sorts[i]->list, list);
1392
1393 return 0;
1394 }
1395 }
1396
1397 return -1;
1398}
1399
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001400static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001401{
1402 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001403 struct rb_root *root = &sched->atom_root;
1404again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001405 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001406 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001407 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001408 if (!node)
1409 break;
1410
Josef Bacik2f80dd42015-05-22 09:18:40 -04001411 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001412 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001413 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001414 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001415 if (root == &sched->atom_root) {
1416 root = &sched->merged_atom_root;
1417 goto again;
1418 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001419}
1420
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001421static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001422 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001423 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001424 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001425{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001426 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001427
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001428 if (sched->tp_handler->wakeup_event)
1429 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001430
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001431 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001432}
1433
Jiri Olsaa151a372016-04-12 15:29:29 +02001434union map_priv {
1435 void *ptr;
1436 bool color;
1437};
1438
1439static bool thread__has_color(struct thread *thread)
1440{
1441 union map_priv priv = {
1442 .ptr = thread__priv(thread),
1443 };
1444
1445 return priv.color;
1446}
1447
1448static struct thread*
1449map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1450{
1451 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1452 union map_priv priv = {
1453 .color = false,
1454 };
1455
1456 if (!sched->map.color_pids || !thread || thread__priv(thread))
1457 return thread;
1458
1459 if (thread_map__has(sched->map.color_pids, tid))
1460 priv.color = true;
1461
1462 thread__set_priv(thread, priv.ptr);
1463 return thread;
1464}
1465
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001466static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1467 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001468{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001469 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1470 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001471 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001472 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001473 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001474 int i, this_cpu = sample->cpu;
1475 int cpus_nr;
1476 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001477 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001478 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001479
1480 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1481
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001482 if (this_cpu > sched->max_cpu)
1483 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001484
Jiri Olsa99623c62016-04-12 15:29:26 +02001485 if (sched->map.comp) {
1486 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1487 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1488 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1489 new_cpu = true;
1490 }
1491 } else
1492 cpus_nr = sched->max_cpu;
1493
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001494 timestamp0 = sched->cpu_last_switched[this_cpu];
1495 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001496 if (timestamp0)
1497 delta = timestamp - timestamp0;
1498 else
1499 delta = 0;
1500
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001501 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001502 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001503 return -1;
1504 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001505
Jiri Olsaa151a372016-04-12 15:29:29 +02001506 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001507 if (sched_in == NULL)
1508 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001509
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001510 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001511
1512 printf(" ");
1513
1514 new_shortname = 0;
1515 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001516 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1517 /*
1518 * Don't allocate a letter-number for swapper:0
1519 * as a shortname. Instead, we use '.' for it.
1520 */
1521 sched_in->shortname[0] = '.';
1522 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001523 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001524 sched_in->shortname[0] = sched->next_shortname1;
1525 sched_in->shortname[1] = sched->next_shortname2;
1526
1527 if (sched->next_shortname1 < 'Z') {
1528 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001529 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001530 sched->next_shortname1 = 'A';
1531 if (sched->next_shortname2 < '9')
1532 sched->next_shortname2++;
1533 else
1534 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001535 }
1536 }
1537 new_shortname = 1;
1538 }
1539
Jiri Olsa99623c62016-04-12 15:29:26 +02001540 for (i = 0; i < cpus_nr; i++) {
1541 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001542 struct thread *curr_thread = sched->curr_thread[cpu];
1543 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001544 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001545
1546 if (curr_thread && thread__has_color(curr_thread))
1547 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001548
Jiri Olsa73643bb2016-04-12 15:29:31 +02001549 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1550 continue;
1551
Jiri Olsacf294f22016-04-12 15:29:30 +02001552 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1553 cpu_color = COLOR_CPUS;
1554
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001555 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001556 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001557 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001558 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001559
Dongsheng6bcab4e2014-05-06 14:39:01 +09001560 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001561 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001562 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001563 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001564 }
1565
Jiri Olsa73643bb2016-04-12 15:29:31 +02001566 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1567 goto out;
1568
Namhyung Kim99620a52016-10-24 11:02:45 +09001569 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1570 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001571 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001572 const char *pid_color = color;
1573
1574 if (thread__has_color(sched_in))
1575 pid_color = COLOR_PIDS;
1576
1577 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001578 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001579 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001580
Jiri Olsa99623c62016-04-12 15:29:26 +02001581 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001582 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001583
Jiri Olsa73643bb2016-04-12 15:29:31 +02001584out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001585 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001586
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001587 thread__put(sched_in);
1588
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001589 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001590}
1591
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001592static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001593 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001594 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001595 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001596{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001597 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001598 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001599 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1600 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001601
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001602 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001603 /*
1604 * Are we trying to switch away a PID that is
1605 * not current?
1606 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001607 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001608 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001609 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001610
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001611 if (sched->tp_handler->switch_event)
1612 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001613
1614 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001615 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001616}
1617
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001618static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001619 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001620 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001621 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001622{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001623 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001624
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001625 if (sched->tp_handler->runtime_event)
1626 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001627
1628 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001629}
1630
David Aherncb627502013-08-07 22:50:47 -04001631static int perf_sched__process_fork_event(struct perf_tool *tool,
1632 union perf_event *event,
1633 struct perf_sample *sample,
1634 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001635{
1636 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1637
David Aherncb627502013-08-07 22:50:47 -04001638 /* run the fork event through the perf machineruy */
1639 perf_event__process_fork(tool, event, sample, machine);
1640
1641 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001642 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001643 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001644
1645 return 0;
1646}
1647
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001648static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001649 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001650 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001651 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001652{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001653 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001654
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001655 if (sched->tp_handler->migrate_task_event)
1656 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001657
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001658 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001659}
1660
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001661typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001662 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001663 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001664 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001665
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001666static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1667 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001668 struct perf_sample *sample,
1669 struct perf_evsel *evsel,
1670 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001671{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001672 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001673
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001674 if (evsel->handler != NULL) {
1675 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001676 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001677 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001678
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001679 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001680}
1681
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001682static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001683{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001684 const struct perf_evsel_str_handler handlers[] = {
1685 { "sched:sched_switch", process_sched_switch_event, },
1686 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1687 { "sched:sched_wakeup", process_sched_wakeup_event, },
1688 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001689 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1690 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001691 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001692 struct perf_data_file file = {
1693 .path = input_name,
1694 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001695 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001696 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001697 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001698
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001699 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001700 if (session == NULL) {
1701 pr_debug("No Memory for session\n");
1702 return -1;
1703 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001704
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001705 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001706
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001707 if (perf_session__set_tracepoints_handlers(session, handlers))
1708 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001709
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001710 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001711 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001712 if (err) {
1713 pr_err("Failed to process events, error %d", err);
1714 goto out_delete;
1715 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001716
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001717 sched->nr_events = session->evlist->stats.nr_events[0];
1718 sched->nr_lost_events = session->evlist->stats.total_lost;
1719 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001720 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001721
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001722 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001723out_delete:
1724 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001725 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001726}
1727
David Ahern49394a22016-11-16 15:06:29 +09001728/*
1729 * scheduling times are printed as msec.usec
1730 */
1731static inline void print_sched_time(unsigned long long nsecs, int width)
1732{
1733 unsigned long msecs;
1734 unsigned long usecs;
1735
1736 msecs = nsecs / NSEC_PER_MSEC;
1737 nsecs -= msecs * NSEC_PER_MSEC;
1738 usecs = nsecs / NSEC_PER_USEC;
1739 printf("%*lu.%03lu ", width, msecs, usecs);
1740}
1741
1742/*
1743 * returns runtime data for event, allocating memory for it the
1744 * first time it is used.
1745 */
1746static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1747{
1748 struct evsel_runtime *r = evsel->priv;
1749
1750 if (r == NULL) {
1751 r = zalloc(sizeof(struct evsel_runtime));
1752 evsel->priv = r;
1753 }
1754
1755 return r;
1756}
1757
1758/*
1759 * save last time event was seen per cpu
1760 */
1761static void perf_evsel__save_time(struct perf_evsel *evsel,
1762 u64 timestamp, u32 cpu)
1763{
1764 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1765
1766 if (r == NULL)
1767 return;
1768
1769 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1770 int i, n = __roundup_pow_of_two(cpu+1);
1771 void *p = r->last_time;
1772
1773 p = realloc(r->last_time, n * sizeof(u64));
1774 if (!p)
1775 return;
1776
1777 r->last_time = p;
1778 for (i = r->ncpu; i < n; ++i)
1779 r->last_time[i] = (u64) 0;
1780
1781 r->ncpu = n;
1782 }
1783
1784 r->last_time[cpu] = timestamp;
1785}
1786
1787/* returns last time this event was seen on the given cpu */
1788static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1789{
1790 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1791
1792 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1793 return 0;
1794
1795 return r->last_time[cpu];
1796}
1797
Namhyung Kim9b8087d2016-12-22 15:03:48 +09001798static int comm_width = 30;
David Ahern49394a22016-11-16 15:06:29 +09001799
1800static char *timehist_get_commstr(struct thread *thread)
1801{
1802 static char str[32];
1803 const char *comm = thread__comm_str(thread);
1804 pid_t tid = thread->tid;
1805 pid_t pid = thread->pid_;
1806 int n;
1807
1808 if (pid == 0)
1809 n = scnprintf(str, sizeof(str), "%s", comm);
1810
1811 else if (tid != pid)
1812 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1813
1814 else
1815 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1816
1817 if (n > comm_width)
1818 comm_width = n;
1819
1820 return str;
1821}
1822
David Aherna407b062016-11-16 15:06:33 +09001823static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001824{
David Aherna407b062016-11-16 15:06:33 +09001825 u32 ncpus = sched->max_cpu + 1;
1826 u32 i, j;
1827
David Ahern49394a22016-11-16 15:06:29 +09001828 printf("%15s %6s ", "time", "cpu");
1829
David Aherna407b062016-11-16 15:06:33 +09001830 if (sched->show_cpu_visual) {
1831 printf(" ");
1832 for (i = 0, j = 0; i < ncpus; ++i) {
1833 printf("%x", j++);
1834 if (j > 15)
1835 j = 0;
1836 }
1837 printf(" ");
1838 }
1839
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001840 printf(" %-*s %9s %9s %9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001841 "task name", "wait time", "sch delay", "run time");
1842
1843 printf("\n");
1844
1845 /*
1846 * units row
1847 */
1848 printf("%15s %-6s ", "", "");
1849
David Aherna407b062016-11-16 15:06:33 +09001850 if (sched->show_cpu_visual)
1851 printf(" %*s ", ncpus, "");
1852
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001853 printf(" %-*s %9s %9s %9s\n", comm_width,
1854 "[tid/pid]", "(msec)", "(msec)", "(msec)");
David Ahern49394a22016-11-16 15:06:29 +09001855
1856 /*
1857 * separator
1858 */
1859 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1860
David Aherna407b062016-11-16 15:06:33 +09001861 if (sched->show_cpu_visual)
1862 printf(" %.*s ", ncpus, graph_dotted_line);
1863
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001864 printf(" %.*s %.9s %.9s %.9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001865 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1866 graph_dotted_line);
1867
1868 printf("\n");
1869}
1870
David Ahernfc1469f2016-11-16 15:06:31 +09001871static void timehist_print_sample(struct perf_sched *sched,
1872 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001873 struct addr_location *al,
David Ahern853b7402016-11-29 10:15:44 -07001874 struct thread *thread,
1875 u64 t)
David Ahern49394a22016-11-16 15:06:29 +09001876{
1877 struct thread_runtime *tr = thread__priv(thread);
David Aherna407b062016-11-16 15:06:33 +09001878 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09001879 char tstr[64];
Namhyung Kim941bdea2017-01-13 19:45:21 +09001880 u64 wait_time;
David Ahern49394a22016-11-16 15:06:29 +09001881
David Ahern853b7402016-11-29 10:15:44 -07001882 timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
David Ahern49394a22016-11-16 15:06:29 +09001883 printf("%15s [%04d] ", tstr, sample->cpu);
1884
David Aherna407b062016-11-16 15:06:33 +09001885 if (sched->show_cpu_visual) {
1886 u32 i;
1887 char c;
1888
1889 printf(" ");
1890 for (i = 0; i < max_cpus; ++i) {
1891 /* flag idle times with 'i'; others are sched events */
1892 if (i == sample->cpu)
1893 c = (thread->tid == 0) ? 'i' : 's';
1894 else
1895 c = ' ';
1896 printf("%c", c);
1897 }
1898 printf(" ");
1899 }
1900
David Ahern49394a22016-11-16 15:06:29 +09001901 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1902
Namhyung Kim941bdea2017-01-13 19:45:21 +09001903 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
1904 print_sched_time(wait_time, 6);
1905
David Ahern49394a22016-11-16 15:06:29 +09001906 print_sched_time(tr->dt_delay, 6);
1907 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001908
1909 if (sched->show_wakeups)
1910 printf(" %-*s", comm_width, "");
1911
David Ahern6c973c92016-11-16 15:06:32 +09001912 if (thread->tid == 0)
1913 goto out;
1914
1915 if (sched->show_callchain)
1916 printf(" ");
1917
1918 sample__fprintf_sym(sample, al, 0,
1919 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09001920 EVSEL__PRINT_CALLCHAIN_ARROW |
1921 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09001922 &callchain_cursor, stdout);
1923
1924out:
David Ahern49394a22016-11-16 15:06:29 +09001925 printf("\n");
1926}
1927
1928/*
1929 * Explanation of delta-time stats:
1930 *
1931 * t = time of current schedule out event
1932 * tprev = time of previous sched out event
1933 * also time of schedule-in event for current task
1934 * last_time = time of last sched change event for current task
1935 * (i.e, time process was last scheduled out)
1936 * ready_to_run = time of wakeup for current task
1937 *
1938 * -----|------------|------------|------------|------
1939 * last ready tprev t
1940 * time to run
1941 *
1942 * |-------- dt_wait --------|
1943 * |- dt_delay -|-- dt_run --|
1944 *
1945 * dt_run = run time of current task
1946 * dt_wait = time between last schedule out event for task and tprev
1947 * represents time spent off the cpu
1948 * dt_delay = time between wakeup and schedule-in of task
1949 */
1950
1951static void timehist_update_runtime_stats(struct thread_runtime *r,
1952 u64 t, u64 tprev)
1953{
1954 r->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09001955 r->dt_sleep = 0;
1956 r->dt_iowait = 0;
1957 r->dt_preempt = 0;
David Ahern49394a22016-11-16 15:06:29 +09001958 r->dt_run = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09001959
David Ahern49394a22016-11-16 15:06:29 +09001960 if (tprev) {
1961 r->dt_run = t - tprev;
1962 if (r->ready_to_run) {
1963 if (r->ready_to_run > tprev)
1964 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1965 else
1966 r->dt_delay = tprev - r->ready_to_run;
1967 }
1968
1969 if (r->last_time > tprev)
1970 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
Namhyung Kim941bdea2017-01-13 19:45:21 +09001971 else if (r->last_time) {
1972 u64 dt_wait = tprev - r->last_time;
1973
1974 if (r->last_state == TASK_RUNNING)
1975 r->dt_preempt = dt_wait;
1976 else if (r->last_state == TASK_UNINTERRUPTIBLE)
1977 r->dt_iowait = dt_wait;
1978 else
1979 r->dt_sleep = dt_wait;
1980 }
David Ahern49394a22016-11-16 15:06:29 +09001981 }
1982
1983 update_stats(&r->run_stats, r->dt_run);
1984 r->total_run_time += r->dt_run;
1985}
1986
Namhyung Kim96039c72016-12-08 23:47:50 +09001987static bool is_idle_sample(struct perf_sample *sample,
1988 struct perf_evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09001989{
1990 /* pid 0 == swapper == idle task */
Namhyung Kim96039c72016-12-08 23:47:50 +09001991 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0)
1992 return perf_evsel__intval(evsel, sample, "prev_pid") == 0;
David Ahern49394a22016-11-16 15:06:29 +09001993
Namhyung Kim96039c72016-12-08 23:47:50 +09001994 return sample->pid == 0;
1995}
1996
1997static void save_task_callchain(struct perf_sched *sched,
1998 struct perf_sample *sample,
1999 struct perf_evsel *evsel,
2000 struct machine *machine)
2001{
2002 struct callchain_cursor *cursor = &callchain_cursor;
2003 struct thread *thread;
David Ahern6c973c92016-11-16 15:06:32 +09002004
2005 /* want main thread for process - has maps */
2006 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
2007 if (thread == NULL) {
2008 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
Namhyung Kim96039c72016-12-08 23:47:50 +09002009 return;
David Ahern6c973c92016-11-16 15:06:32 +09002010 }
2011
2012 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim96039c72016-12-08 23:47:50 +09002013 return;
David Ahern6c973c92016-11-16 15:06:32 +09002014
2015 if (thread__resolve_callchain(thread, cursor, evsel, sample,
Namhyung Kim8388deb2016-11-24 10:11:14 +09002016 NULL, NULL, sched->max_stack + 2) != 0) {
David Ahern6c973c92016-11-16 15:06:32 +09002017 if (verbose)
2018 error("Failed to resolve callchain. Skipping\n");
2019
Namhyung Kim96039c72016-12-08 23:47:50 +09002020 return;
David Ahern6c973c92016-11-16 15:06:32 +09002021 }
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002022
David Ahern6c973c92016-11-16 15:06:32 +09002023 callchain_cursor_commit(cursor);
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002024
2025 while (true) {
2026 struct callchain_cursor_node *node;
2027 struct symbol *sym;
2028
2029 node = callchain_cursor_current(cursor);
2030 if (node == NULL)
2031 break;
2032
2033 sym = node->sym;
2034 if (sym && sym->name) {
2035 if (!strcmp(sym->name, "schedule") ||
2036 !strcmp(sym->name, "__schedule") ||
2037 !strcmp(sym->name, "preempt_schedule"))
2038 sym->ignore = 1;
2039 }
2040
2041 callchain_cursor_advance(cursor);
2042 }
David Ahern49394a22016-11-16 15:06:29 +09002043}
2044
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002045static int init_idle_thread(struct thread *thread)
2046{
2047 struct idle_thread_runtime *itr;
2048
2049 thread__set_comm(thread, idle_comm, 0);
2050
2051 itr = zalloc(sizeof(*itr));
2052 if (itr == NULL)
2053 return -ENOMEM;
2054
2055 init_stats(&itr->tr.run_stats);
2056 callchain_init(&itr->callchain);
2057 callchain_cursor_reset(&itr->cursor);
2058 thread__set_priv(thread, itr);
2059
2060 return 0;
2061}
2062
David Ahern49394a22016-11-16 15:06:29 +09002063/*
2064 * Track idle stats per cpu by maintaining a local thread
2065 * struct for the idle task on each cpu.
2066 */
2067static int init_idle_threads(int ncpu)
2068{
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002069 int i, ret;
David Ahern49394a22016-11-16 15:06:29 +09002070
2071 idle_threads = zalloc(ncpu * sizeof(struct thread *));
2072 if (!idle_threads)
2073 return -ENOMEM;
2074
Namhyung Kimb3363522016-12-06 12:40:05 +09002075 idle_max_cpu = ncpu;
David Ahern49394a22016-11-16 15:06:29 +09002076
2077 /* allocate the actual thread struct if needed */
2078 for (i = 0; i < ncpu; ++i) {
2079 idle_threads[i] = thread__new(0, 0);
2080 if (idle_threads[i] == NULL)
2081 return -ENOMEM;
2082
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002083 ret = init_idle_thread(idle_threads[i]);
2084 if (ret < 0)
2085 return ret;
David Ahern49394a22016-11-16 15:06:29 +09002086 }
2087
2088 return 0;
2089}
2090
2091static void free_idle_threads(void)
2092{
2093 int i;
2094
2095 if (idle_threads == NULL)
2096 return;
2097
Namhyung Kimb3363522016-12-06 12:40:05 +09002098 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern49394a22016-11-16 15:06:29 +09002099 if ((idle_threads[i]))
2100 thread__delete(idle_threads[i]);
2101 }
2102
2103 free(idle_threads);
2104}
2105
2106static struct thread *get_idle_thread(int cpu)
2107{
2108 /*
2109 * expand/allocate array of pointers to local thread
2110 * structs if needed
2111 */
2112 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2113 int i, j = __roundup_pow_of_two(cpu+1);
2114 void *p;
2115
2116 p = realloc(idle_threads, j * sizeof(struct thread *));
2117 if (!p)
2118 return NULL;
2119
2120 idle_threads = (struct thread **) p;
Namhyung Kimb3363522016-12-06 12:40:05 +09002121 for (i = idle_max_cpu; i < j; ++i)
David Ahern49394a22016-11-16 15:06:29 +09002122 idle_threads[i] = NULL;
2123
2124 idle_max_cpu = j;
2125 }
2126
2127 /* allocate a new thread struct if needed */
2128 if (idle_threads[cpu] == NULL) {
2129 idle_threads[cpu] = thread__new(0, 0);
2130
2131 if (idle_threads[cpu]) {
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002132 if (init_idle_thread(idle_threads[cpu]) < 0)
2133 return NULL;
David Ahern49394a22016-11-16 15:06:29 +09002134 }
2135 }
2136
2137 return idle_threads[cpu];
2138}
2139
Namhyung Kim699b5b92016-12-08 23:47:52 +09002140static void save_idle_callchain(struct idle_thread_runtime *itr,
2141 struct perf_sample *sample)
2142{
2143 if (!symbol_conf.use_callchain || sample->callchain == NULL)
2144 return;
2145
2146 callchain_cursor__copy(&itr->cursor, &callchain_cursor);
2147}
2148
David Ahern49394a22016-11-16 15:06:29 +09002149/*
2150 * handle runtime stats saved per thread
2151 */
2152static struct thread_runtime *thread__init_runtime(struct thread *thread)
2153{
2154 struct thread_runtime *r;
2155
2156 r = zalloc(sizeof(struct thread_runtime));
2157 if (!r)
2158 return NULL;
2159
2160 init_stats(&r->run_stats);
2161 thread__set_priv(thread, r);
2162
2163 return r;
2164}
2165
2166static struct thread_runtime *thread__get_runtime(struct thread *thread)
2167{
2168 struct thread_runtime *tr;
2169
2170 tr = thread__priv(thread);
2171 if (tr == NULL) {
2172 tr = thread__init_runtime(thread);
2173 if (tr == NULL)
2174 pr_debug("Failed to malloc memory for runtime data.\n");
2175 }
2176
2177 return tr;
2178}
2179
David Ahern6c973c92016-11-16 15:06:32 +09002180static struct thread *timehist_get_thread(struct perf_sched *sched,
2181 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002182 struct machine *machine,
2183 struct perf_evsel *evsel)
2184{
2185 struct thread *thread;
2186
Namhyung Kim96039c72016-12-08 23:47:50 +09002187 if (is_idle_sample(sample, evsel)) {
David Ahern49394a22016-11-16 15:06:29 +09002188 thread = get_idle_thread(sample->cpu);
2189 if (thread == NULL)
2190 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2191
2192 } else {
Namhyung Kim5d92d962016-12-06 12:40:03 +09002193 /* there were samples with tid 0 but non-zero pid */
2194 thread = machine__findnew_thread(machine, sample->pid,
2195 sample->tid ?: sample->pid);
David Ahern49394a22016-11-16 15:06:29 +09002196 if (thread == NULL) {
2197 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2198 sample->tid);
2199 }
Namhyung Kim96039c72016-12-08 23:47:50 +09002200
2201 save_task_callchain(sched, sample, evsel, machine);
Namhyung Kim699b5b92016-12-08 23:47:52 +09002202 if (sched->idle_hist) {
2203 struct thread *idle;
2204 struct idle_thread_runtime *itr;
2205
2206 idle = get_idle_thread(sample->cpu);
2207 if (idle == NULL) {
2208 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2209 return NULL;
2210 }
2211
2212 itr = thread__priv(idle);
2213 if (itr == NULL)
2214 return NULL;
2215
2216 itr->last_thread = thread;
2217
2218 /* copy task callchain when entering to idle */
2219 if (perf_evsel__intval(evsel, sample, "next_pid") == 0)
2220 save_idle_callchain(itr, sample);
2221 }
David Ahern49394a22016-11-16 15:06:29 +09002222 }
2223
2224 return thread;
2225}
2226
David Ahern52df1382016-11-16 15:06:30 +09002227static bool timehist_skip_sample(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002228 struct thread *thread,
2229 struct perf_evsel *evsel,
2230 struct perf_sample *sample)
David Ahern49394a22016-11-16 15:06:29 +09002231{
2232 bool rc = false;
2233
David Ahern52df1382016-11-16 15:06:30 +09002234 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002235 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002236 sched->skipped_samples++;
2237 }
David Ahern49394a22016-11-16 15:06:29 +09002238
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002239 if (sched->idle_hist) {
2240 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch"))
2241 rc = true;
2242 else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 &&
2243 perf_evsel__intval(evsel, sample, "next_pid") != 0)
2244 rc = true;
2245 }
2246
David Ahern49394a22016-11-16 15:06:29 +09002247 return rc;
2248}
2249
David Ahernfc1469f2016-11-16 15:06:31 +09002250static void timehist_print_wakeup_event(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002251 struct perf_evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09002252 struct perf_sample *sample,
2253 struct machine *machine,
2254 struct thread *awakened)
2255{
2256 struct thread *thread;
2257 char tstr[64];
2258
2259 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2260 if (thread == NULL)
2261 return;
2262
2263 /* show wakeup unless both awakee and awaker are filtered */
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002264 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2265 timehist_skip_sample(sched, awakened, evsel, sample)) {
David Ahernfc1469f2016-11-16 15:06:31 +09002266 return;
2267 }
2268
2269 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2270 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002271 if (sched->show_cpu_visual)
2272 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002273
2274 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2275
2276 /* dt spacer */
2277 printf(" %9s %9s %9s ", "", "", "");
2278
2279 printf("awakened: %s", timehist_get_commstr(awakened));
2280
2281 printf("\n");
2282}
2283
2284static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002285 union perf_event *event __maybe_unused,
2286 struct perf_evsel *evsel,
2287 struct perf_sample *sample,
2288 struct machine *machine)
2289{
David Ahernfc1469f2016-11-16 15:06:31 +09002290 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002291 struct thread *thread;
2292 struct thread_runtime *tr = NULL;
2293 /* want pid of awakened task not pid in sample */
2294 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2295
2296 thread = machine__findnew_thread(machine, 0, pid);
2297 if (thread == NULL)
2298 return -1;
2299
2300 tr = thread__get_runtime(thread);
2301 if (tr == NULL)
2302 return -1;
2303
2304 if (tr->ready_to_run == 0)
2305 tr->ready_to_run = sample->time;
2306
David Ahernfc1469f2016-11-16 15:06:31 +09002307 /* show wakeups if requested */
David Ahern853b7402016-11-29 10:15:44 -07002308 if (sched->show_wakeups &&
2309 !perf_time__skip_sample(&sched->ptime, sample->time))
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002310 timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
David Ahernfc1469f2016-11-16 15:06:31 +09002311
David Ahern49394a22016-11-16 15:06:29 +09002312 return 0;
2313}
2314
David Ahern350f54f2016-11-25 09:28:41 -07002315static void timehist_print_migration_event(struct perf_sched *sched,
2316 struct perf_evsel *evsel,
2317 struct perf_sample *sample,
2318 struct machine *machine,
2319 struct thread *migrated)
2320{
2321 struct thread *thread;
2322 char tstr[64];
2323 u32 max_cpus = sched->max_cpu + 1;
2324 u32 ocpu, dcpu;
2325
2326 if (sched->summary_only)
2327 return;
2328
2329 max_cpus = sched->max_cpu + 1;
2330 ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
2331 dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
2332
2333 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2334 if (thread == NULL)
2335 return;
2336
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002337 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2338 timehist_skip_sample(sched, migrated, evsel, sample)) {
David Ahern350f54f2016-11-25 09:28:41 -07002339 return;
2340 }
2341
2342 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2343 printf("%15s [%04d] ", tstr, sample->cpu);
2344
2345 if (sched->show_cpu_visual) {
2346 u32 i;
2347 char c;
2348
2349 printf(" ");
2350 for (i = 0; i < max_cpus; ++i) {
2351 c = (i == sample->cpu) ? 'm' : ' ';
2352 printf("%c", c);
2353 }
2354 printf(" ");
2355 }
2356
2357 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2358
2359 /* dt spacer */
2360 printf(" %9s %9s %9s ", "", "", "");
2361
2362 printf("migrated: %s", timehist_get_commstr(migrated));
2363 printf(" cpu %d => %d", ocpu, dcpu);
2364
2365 printf("\n");
2366}
2367
2368static int timehist_migrate_task_event(struct perf_tool *tool,
2369 union perf_event *event __maybe_unused,
2370 struct perf_evsel *evsel,
2371 struct perf_sample *sample,
2372 struct machine *machine)
2373{
2374 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2375 struct thread *thread;
2376 struct thread_runtime *tr = NULL;
2377 /* want pid of migrated task not pid in sample */
2378 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2379
2380 thread = machine__findnew_thread(machine, 0, pid);
2381 if (thread == NULL)
2382 return -1;
2383
2384 tr = thread__get_runtime(thread);
2385 if (tr == NULL)
2386 return -1;
2387
2388 tr->migrations++;
2389
2390 /* show migrations if requested */
2391 timehist_print_migration_event(sched, evsel, sample, machine, thread);
2392
2393 return 0;
2394}
2395
David Ahern52df1382016-11-16 15:06:30 +09002396static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002397 union perf_event *event,
2398 struct perf_evsel *evsel,
2399 struct perf_sample *sample,
2400 struct machine *machine)
2401{
David Ahernfc1469f2016-11-16 15:06:31 +09002402 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern853b7402016-11-29 10:15:44 -07002403 struct perf_time_interval *ptime = &sched->ptime;
David Ahern49394a22016-11-16 15:06:29 +09002404 struct addr_location al;
2405 struct thread *thread;
2406 struct thread_runtime *tr = NULL;
David Ahern853b7402016-11-29 10:15:44 -07002407 u64 tprev, t = sample->time;
David Ahern49394a22016-11-16 15:06:29 +09002408 int rc = 0;
2409
2410 if (machine__resolve(machine, &al, sample) < 0) {
2411 pr_err("problem processing %d event. skipping it\n",
2412 event->header.type);
2413 rc = -1;
2414 goto out;
2415 }
2416
David Ahern6c973c92016-11-16 15:06:32 +09002417 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002418 if (thread == NULL) {
2419 rc = -1;
2420 goto out;
2421 }
2422
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002423 if (timehist_skip_sample(sched, thread, evsel, sample))
David Ahern49394a22016-11-16 15:06:29 +09002424 goto out;
2425
2426 tr = thread__get_runtime(thread);
2427 if (tr == NULL) {
2428 rc = -1;
2429 goto out;
2430 }
2431
2432 tprev = perf_evsel__get_time(evsel, sample->cpu);
2433
David Ahern853b7402016-11-29 10:15:44 -07002434 /*
2435 * If start time given:
2436 * - sample time is under window user cares about - skip sample
2437 * - tprev is under window user cares about - reset to start of window
2438 */
2439 if (ptime->start && ptime->start > t)
2440 goto out;
2441
Namhyung Kimbdd75722016-12-22 15:03:49 +09002442 if (tprev && ptime->start > tprev)
David Ahern853b7402016-11-29 10:15:44 -07002443 tprev = ptime->start;
2444
2445 /*
2446 * If end time given:
2447 * - previous sched event is out of window - we are done
2448 * - sample time is beyond window user cares about - reset it
2449 * to close out stats for time window interest
2450 */
2451 if (ptime->end) {
2452 if (tprev > ptime->end)
2453 goto out;
2454
2455 if (t > ptime->end)
2456 t = ptime->end;
2457 }
2458
Namhyung Kim07235f82016-12-08 23:47:54 +09002459 if (!sched->idle_hist || thread->tid == 0) {
2460 timehist_update_runtime_stats(tr, t, tprev);
2461
2462 if (sched->idle_hist) {
2463 struct idle_thread_runtime *itr = (void *)tr;
2464 struct thread_runtime *last_tr;
2465
2466 BUG_ON(thread->tid != 0);
2467
2468 if (itr->last_thread == NULL)
2469 goto out;
2470
2471 /* add current idle time as last thread's runtime */
2472 last_tr = thread__get_runtime(itr->last_thread);
2473 if (last_tr == NULL)
2474 goto out;
2475
2476 timehist_update_runtime_stats(last_tr, t, tprev);
2477 /*
2478 * remove delta time of last thread as it's not updated
2479 * and otherwise it will show an invalid value next
2480 * time. we only care total run time and run stat.
2481 */
2482 last_tr->dt_run = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002483 last_tr->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002484 last_tr->dt_sleep = 0;
2485 last_tr->dt_iowait = 0;
2486 last_tr->dt_preempt = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002487
Namhyung Kimba957eb2016-12-08 23:47:55 +09002488 if (itr->cursor.nr)
2489 callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2490
Namhyung Kim07235f82016-12-08 23:47:54 +09002491 itr->last_thread = NULL;
2492 }
2493 }
David Ahern853b7402016-11-29 10:15:44 -07002494
David Ahern52df1382016-11-16 15:06:30 +09002495 if (!sched->summary_only)
David Ahern853b7402016-11-29 10:15:44 -07002496 timehist_print_sample(sched, sample, &al, thread, t);
David Ahern49394a22016-11-16 15:06:29 +09002497
2498out:
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002499 if (sched->hist_time.start == 0 && t >= ptime->start)
2500 sched->hist_time.start = t;
2501 if (ptime->end == 0 || t <= ptime->end)
2502 sched->hist_time.end = t;
2503
David Ahern49394a22016-11-16 15:06:29 +09002504 if (tr) {
2505 /* time of this sched_switch event becomes last time task seen */
2506 tr->last_time = sample->time;
2507
Namhyung Kim941bdea2017-01-13 19:45:21 +09002508 /* last state is used to determine where to account wait time */
2509 tr->last_state = perf_evsel__intval(evsel, sample, "prev_state");
2510
David Ahern49394a22016-11-16 15:06:29 +09002511 /* sched out event for task so reset ready to run time */
2512 tr->ready_to_run = 0;
2513 }
2514
2515 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2516
2517 return rc;
2518}
2519
2520static int timehist_sched_switch_event(struct perf_tool *tool,
2521 union perf_event *event,
2522 struct perf_evsel *evsel,
2523 struct perf_sample *sample,
2524 struct machine *machine __maybe_unused)
2525{
2526 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2527}
2528
2529static int process_lost(struct perf_tool *tool __maybe_unused,
2530 union perf_event *event,
2531 struct perf_sample *sample,
2532 struct machine *machine __maybe_unused)
2533{
2534 char tstr[64];
2535
2536 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2537 printf("%15s ", tstr);
2538 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2539
2540 return 0;
2541}
2542
2543
David Ahern52df1382016-11-16 15:06:30 +09002544static void print_thread_runtime(struct thread *t,
2545 struct thread_runtime *r)
2546{
2547 double mean = avg_stats(&r->run_stats);
2548 float stddev;
2549
2550 printf("%*s %5d %9" PRIu64 " ",
2551 comm_width, timehist_get_commstr(t), t->ppid,
2552 (u64) r->run_stats.n);
2553
2554 print_sched_time(r->total_run_time, 8);
2555 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2556 print_sched_time(r->run_stats.min, 6);
2557 printf(" ");
2558 print_sched_time((u64) mean, 6);
2559 printf(" ");
2560 print_sched_time(r->run_stats.max, 6);
2561 printf(" ");
2562 printf("%5.2f", stddev);
David Ahern350f54f2016-11-25 09:28:41 -07002563 printf(" %5" PRIu64, r->migrations);
David Ahern52df1382016-11-16 15:06:30 +09002564 printf("\n");
2565}
2566
2567struct total_run_stats {
2568 u64 sched_count;
2569 u64 task_count;
2570 u64 total_run_time;
2571};
2572
2573static int __show_thread_runtime(struct thread *t, void *priv)
2574{
2575 struct total_run_stats *stats = priv;
2576 struct thread_runtime *r;
2577
2578 if (thread__is_filtered(t))
2579 return 0;
2580
2581 r = thread__priv(t);
2582 if (r && r->run_stats.n) {
2583 stats->task_count++;
2584 stats->sched_count += r->run_stats.n;
2585 stats->total_run_time += r->total_run_time;
2586 print_thread_runtime(t, r);
2587 }
2588
2589 return 0;
2590}
2591
2592static int show_thread_runtime(struct thread *t, void *priv)
2593{
2594 if (t->dead)
2595 return 0;
2596
2597 return __show_thread_runtime(t, priv);
2598}
2599
2600static int show_deadthread_runtime(struct thread *t, void *priv)
2601{
2602 if (!t->dead)
2603 return 0;
2604
2605 return __show_thread_runtime(t, priv);
2606}
2607
Namhyung Kimba957eb2016-12-08 23:47:55 +09002608static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2609{
2610 const char *sep = " <- ";
2611 struct callchain_list *chain;
2612 size_t ret = 0;
2613 char bf[1024];
2614 bool first;
2615
2616 if (node == NULL)
2617 return 0;
2618
2619 ret = callchain__fprintf_folded(fp, node->parent);
2620 first = (ret == 0);
2621
2622 list_for_each_entry(chain, &node->val, list) {
2623 if (chain->ip >= PERF_CONTEXT_MAX)
2624 continue;
2625 if (chain->ms.sym && chain->ms.sym->ignore)
2626 continue;
2627 ret += fprintf(fp, "%s%s", first ? "" : sep,
2628 callchain_list__sym_name(chain, bf, sizeof(bf),
2629 false));
2630 first = false;
2631 }
2632
2633 return ret;
2634}
2635
2636static size_t timehist_print_idlehist_callchain(struct rb_root *root)
2637{
2638 size_t ret = 0;
2639 FILE *fp = stdout;
2640 struct callchain_node *chain;
2641 struct rb_node *rb_node = rb_first(root);
2642
2643 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
2644 printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
2645 graph_dotted_line);
2646
2647 while (rb_node) {
2648 chain = rb_entry(rb_node, struct callchain_node, rb_node);
2649 rb_node = rb_next(rb_node);
2650
2651 ret += fprintf(fp, " ");
2652 print_sched_time(chain->hit, 12);
2653 ret += 16; /* print_sched_time returns 2nd arg + 4 */
2654 ret += fprintf(fp, " %8d ", chain->count);
2655 ret += callchain__fprintf_folded(fp, chain);
2656 ret += fprintf(fp, "\n");
2657 }
2658
2659 return ret;
2660}
2661
David Ahern52df1382016-11-16 15:06:30 +09002662static void timehist_print_summary(struct perf_sched *sched,
2663 struct perf_session *session)
2664{
2665 struct machine *m = &session->machines.host;
2666 struct total_run_stats totals;
2667 u64 task_count;
2668 struct thread *t;
2669 struct thread_runtime *r;
2670 int i;
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002671 u64 hist_time = sched->hist_time.end - sched->hist_time.start;
David Ahern52df1382016-11-16 15:06:30 +09002672
2673 memset(&totals, 0, sizeof(totals));
2674
Namhyung Kim07235f82016-12-08 23:47:54 +09002675 if (sched->idle_hist) {
2676 printf("\nIdle-time summary\n");
2677 printf("%*s parent sched-out ", comm_width, "comm");
2678 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
2679 } else {
2680 printf("\nRuntime summary\n");
2681 printf("%*s parent sched-in ", comm_width, "comm");
2682 printf(" run-time min-run avg-run max-run stddev migrations\n");
2683 }
David Ahern52df1382016-11-16 15:06:30 +09002684 printf("%*s (count) ", comm_width, "");
2685 printf(" (msec) (msec) (msec) (msec) %%\n");
David Ahern350f54f2016-11-25 09:28:41 -07002686 printf("%.117s\n", graph_dotted_line);
David Ahern52df1382016-11-16 15:06:30 +09002687
2688 machine__for_each_thread(m, show_thread_runtime, &totals);
2689 task_count = totals.task_count;
2690 if (!task_count)
2691 printf("<no still running tasks>\n");
2692
2693 printf("\nTerminated tasks:\n");
2694 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2695 if (task_count == totals.task_count)
2696 printf("<no terminated tasks>\n");
2697
2698 /* CPU idle stats not tracked when samples were skipped */
Namhyung Kim07235f82016-12-08 23:47:54 +09002699 if (sched->skipped_samples && !sched->idle_hist)
David Ahern52df1382016-11-16 15:06:30 +09002700 return;
2701
2702 printf("\nIdle stats:\n");
Namhyung Kimb3363522016-12-06 12:40:05 +09002703 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern52df1382016-11-16 15:06:30 +09002704 t = idle_threads[i];
2705 if (!t)
2706 continue;
2707
2708 r = thread__priv(t);
2709 if (r && r->run_stats.n) {
2710 totals.sched_count += r->run_stats.n;
2711 printf(" CPU %2d idle for ", i);
2712 print_sched_time(r->total_run_time, 6);
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002713 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
David Ahern52df1382016-11-16 15:06:30 +09002714 } else
2715 printf(" CPU %2d idle entire time window\n", i);
2716 }
2717
Namhyung Kimba957eb2016-12-08 23:47:55 +09002718 if (sched->idle_hist && symbol_conf.use_callchain) {
2719 callchain_param.mode = CHAIN_FOLDED;
2720 callchain_param.value = CCVAL_PERIOD;
2721
2722 callchain_register_param(&callchain_param);
2723
2724 printf("\nIdle stats by callchain:\n");
2725 for (i = 0; i < idle_max_cpu; ++i) {
2726 struct idle_thread_runtime *itr;
2727
2728 t = idle_threads[i];
2729 if (!t)
2730 continue;
2731
2732 itr = thread__priv(t);
2733 if (itr == NULL)
2734 continue;
2735
2736 callchain_param.sort(&itr->sorted_root, &itr->callchain,
2737 0, &callchain_param);
2738
2739 printf(" CPU %2d:", i);
2740 print_sched_time(itr->tr.total_run_time, 6);
2741 printf(" msec\n");
2742 timehist_print_idlehist_callchain(&itr->sorted_root);
2743 printf("\n");
2744 }
2745 }
2746
David Ahern52df1382016-11-16 15:06:30 +09002747 printf("\n"
2748 " Total number of unique tasks: %" PRIu64 "\n"
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002749 "Total number of context switches: %" PRIu64 "\n",
David Ahern52df1382016-11-16 15:06:30 +09002750 totals.task_count, totals.sched_count);
2751
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002752 printf(" Total run time (msec): ");
David Ahern52df1382016-11-16 15:06:30 +09002753 print_sched_time(totals.total_run_time, 2);
2754 printf("\n");
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002755
2756 printf(" Total scheduling time (msec): ");
2757 print_sched_time(hist_time, 2);
2758 printf(" (x %d)\n", sched->max_cpu);
David Ahern52df1382016-11-16 15:06:30 +09002759}
2760
David Ahern49394a22016-11-16 15:06:29 +09002761typedef int (*sched_handler)(struct perf_tool *tool,
2762 union perf_event *event,
2763 struct perf_evsel *evsel,
2764 struct perf_sample *sample,
2765 struct machine *machine);
2766
2767static int perf_timehist__process_sample(struct perf_tool *tool,
2768 union perf_event *event,
2769 struct perf_sample *sample,
2770 struct perf_evsel *evsel,
2771 struct machine *machine)
2772{
2773 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2774 int err = 0;
2775 int this_cpu = sample->cpu;
2776
2777 if (this_cpu > sched->max_cpu)
2778 sched->max_cpu = this_cpu;
2779
2780 if (evsel->handler != NULL) {
2781 sched_handler f = evsel->handler;
2782
2783 err = f(tool, event, evsel, sample, machine);
2784 }
2785
2786 return err;
2787}
2788
David Ahern6c973c92016-11-16 15:06:32 +09002789static int timehist_check_attr(struct perf_sched *sched,
2790 struct perf_evlist *evlist)
2791{
2792 struct perf_evsel *evsel;
2793 struct evsel_runtime *er;
2794
2795 list_for_each_entry(evsel, &evlist->entries, node) {
2796 er = perf_evsel__get_runtime(evsel);
2797 if (er == NULL) {
2798 pr_err("Failed to allocate memory for evsel runtime data\n");
2799 return -1;
2800 }
2801
2802 if (sched->show_callchain &&
2803 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2804 pr_info("Samples do not have callchains.\n");
2805 sched->show_callchain = 0;
2806 symbol_conf.use_callchain = 0;
2807 }
2808 }
2809
2810 return 0;
2811}
2812
David Ahern49394a22016-11-16 15:06:29 +09002813static int perf_sched__timehist(struct perf_sched *sched)
2814{
2815 const struct perf_evsel_str_handler handlers[] = {
2816 { "sched:sched_switch", timehist_sched_switch_event, },
2817 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2818 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2819 };
David Ahern350f54f2016-11-25 09:28:41 -07002820 const struct perf_evsel_str_handler migrate_handlers[] = {
2821 { "sched:sched_migrate_task", timehist_migrate_task_event, },
2822 };
David Ahern49394a22016-11-16 15:06:29 +09002823 struct perf_data_file file = {
2824 .path = input_name,
2825 .mode = PERF_DATA_MODE_READ,
Namhyung Kim6fa94252016-12-06 12:40:01 +09002826 .force = sched->force,
David Ahern49394a22016-11-16 15:06:29 +09002827 };
2828
2829 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002830 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002831 int err = -1;
2832
2833 /*
2834 * event handlers for timehist option
2835 */
2836 sched->tool.sample = perf_timehist__process_sample;
2837 sched->tool.mmap = perf_event__process_mmap;
2838 sched->tool.comm = perf_event__process_comm;
2839 sched->tool.exit = perf_event__process_exit;
2840 sched->tool.fork = perf_event__process_fork;
2841 sched->tool.lost = process_lost;
2842 sched->tool.attr = perf_event__process_attr;
2843 sched->tool.tracing_data = perf_event__process_tracing_data;
2844 sched->tool.build_id = perf_event__process_build_id;
2845
2846 sched->tool.ordered_events = true;
2847 sched->tool.ordering_requires_timestamps = true;
2848
David Ahern6c973c92016-11-16 15:06:32 +09002849 symbol_conf.use_callchain = sched->show_callchain;
2850
David Ahern49394a22016-11-16 15:06:29 +09002851 session = perf_session__new(&file, false, &sched->tool);
2852 if (session == NULL)
2853 return -ENOMEM;
2854
David Ahern52df1382016-11-16 15:06:30 +09002855 evlist = session->evlist;
2856
David Ahern49394a22016-11-16 15:06:29 +09002857 symbol__init(&session->header.env);
2858
David Ahern853b7402016-11-29 10:15:44 -07002859 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
2860 pr_err("Invalid time string\n");
2861 return -EINVAL;
2862 }
2863
David Ahern6c973c92016-11-16 15:06:32 +09002864 if (timehist_check_attr(sched, evlist) != 0)
2865 goto out;
2866
David Ahern49394a22016-11-16 15:06:29 +09002867 setup_pager();
2868
2869 /* setup per-evsel handlers */
2870 if (perf_session__set_tracepoints_handlers(session, handlers))
2871 goto out;
2872
David Ahernf45bf8d2016-11-29 13:39:48 -07002873 /* sched_switch event at a minimum needs to exist */
2874 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
2875 "sched:sched_switch")) {
2876 pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
David Ahern49394a22016-11-16 15:06:29 +09002877 goto out;
David Ahernf45bf8d2016-11-29 13:39:48 -07002878 }
David Ahern49394a22016-11-16 15:06:29 +09002879
David Ahern350f54f2016-11-25 09:28:41 -07002880 if (sched->show_migrations &&
2881 perf_session__set_tracepoints_handlers(session, migrate_handlers))
2882 goto out;
2883
David Ahern49394a22016-11-16 15:06:29 +09002884 /* pre-allocate struct for per-CPU idle stats */
2885 sched->max_cpu = session->header.env.nr_cpus_online;
2886 if (sched->max_cpu == 0)
2887 sched->max_cpu = 4;
2888 if (init_idle_threads(sched->max_cpu))
2889 goto out;
2890
David Ahern52df1382016-11-16 15:06:30 +09002891 /* summary_only implies summary option, but don't overwrite summary if set */
2892 if (sched->summary_only)
2893 sched->summary = sched->summary_only;
2894
2895 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09002896 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09002897
2898 err = perf_session__process_events(session);
2899 if (err) {
2900 pr_err("Failed to process events, error %d", err);
2901 goto out;
2902 }
2903
David Ahern52df1382016-11-16 15:06:30 +09002904 sched->nr_events = evlist->stats.nr_events[0];
2905 sched->nr_lost_events = evlist->stats.total_lost;
2906 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2907
2908 if (sched->summary)
2909 timehist_print_summary(sched, session);
2910
David Ahern49394a22016-11-16 15:06:29 +09002911out:
2912 free_idle_threads();
2913 perf_session__delete(session);
2914
2915 return err;
2916}
2917
2918
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002919static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002920{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002921 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002922 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002923 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2924 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002925 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002926 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002927 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002928 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2929 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002930 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002931 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002932 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002933 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2934 sched->nr_context_switch_bugs, sched->nr_timestamps);
2935 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002936 printf(" (due to lost events?)");
2937 printf("\n");
2938 }
2939}
2940
Josef Bacik2f80dd42015-05-22 09:18:40 -04002941static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2942{
2943 struct rb_node **new = &(root->rb_node), *parent = NULL;
2944 struct work_atoms *this;
2945 const char *comm = thread__comm_str(data->thread), *this_comm;
2946
2947 while (*new) {
2948 int cmp;
2949
2950 this = container_of(*new, struct work_atoms, node);
2951 parent = *new;
2952
2953 this_comm = thread__comm_str(this->thread);
2954 cmp = strcmp(comm, this_comm);
2955 if (cmp > 0) {
2956 new = &((*new)->rb_left);
2957 } else if (cmp < 0) {
2958 new = &((*new)->rb_right);
2959 } else {
2960 this->num_merged++;
2961 this->total_runtime += data->total_runtime;
2962 this->nb_atoms += data->nb_atoms;
2963 this->total_lat += data->total_lat;
2964 list_splice(&data->work_list, &this->work_list);
2965 if (this->max_lat < data->max_lat) {
2966 this->max_lat = data->max_lat;
2967 this->max_lat_at = data->max_lat_at;
2968 }
2969 zfree(&data);
2970 return;
2971 }
2972 }
2973
2974 data->num_merged++;
2975 rb_link_node(&data->node, parent, new);
2976 rb_insert_color(&data->node, root);
2977}
2978
2979static void perf_sched__merge_lat(struct perf_sched *sched)
2980{
2981 struct work_atoms *data;
2982 struct rb_node *node;
2983
2984 if (sched->skip_merge)
2985 return;
2986
2987 while ((node = rb_first(&sched->atom_root))) {
2988 rb_erase(node, &sched->atom_root);
2989 data = rb_entry(node, struct work_atoms, node);
2990 __merge_work_atoms(&sched->merged_atom_root, data);
2991 }
2992}
2993
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002994static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002995{
2996 struct rb_node *next;
2997
2998 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002999
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003000 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003001 return -1;
David Ahernad9def72013-08-07 22:50:44 -04003002
Josef Bacik2f80dd42015-05-22 09:18:40 -04003003 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003004 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003005
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003006 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
3007 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
3008 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003009
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003010 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003011
3012 while (next) {
3013 struct work_atoms *work_list;
3014
3015 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003016 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003017 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003018 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003019 }
3020
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003021 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003022 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03003023 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003024
3025 printf(" ---------------------------------------------------\n");
3026
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003027 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003028 printf("\n");
3029
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003030 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003031}
3032
Jiri Olsa99623c62016-04-12 15:29:26 +02003033static int setup_map_cpus(struct perf_sched *sched)
3034{
Jiri Olsa73643bb2016-04-12 15:29:31 +02003035 struct cpu_map *map;
3036
Jiri Olsa99623c62016-04-12 15:29:26 +02003037 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
3038
3039 if (sched->map.comp) {
3040 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02003041 if (!sched->map.comp_cpus)
3042 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02003043 }
3044
Jiri Olsa73643bb2016-04-12 15:29:31 +02003045 if (!sched->map.cpus_str)
3046 return 0;
3047
3048 map = cpu_map__new(sched->map.cpus_str);
3049 if (!map) {
3050 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3051 return -1;
3052 }
3053
3054 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02003055 return 0;
3056}
3057
Jiri Olsaa151a372016-04-12 15:29:29 +02003058static int setup_color_pids(struct perf_sched *sched)
3059{
3060 struct thread_map *map;
3061
3062 if (!sched->map.color_pids_str)
3063 return 0;
3064
3065 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3066 if (!map) {
3067 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3068 return -1;
3069 }
3070
3071 sched->map.color_pids = map;
3072 return 0;
3073}
3074
Jiri Olsacf294f22016-04-12 15:29:30 +02003075static int setup_color_cpus(struct perf_sched *sched)
3076{
3077 struct cpu_map *map;
3078
3079 if (!sched->map.color_cpus_str)
3080 return 0;
3081
3082 map = cpu_map__new(sched->map.color_cpus_str);
3083 if (!map) {
3084 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3085 return -1;
3086 }
3087
3088 sched->map.color_cpus = map;
3089 return 0;
3090}
3091
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003092static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003093{
Jiri Olsa99623c62016-04-12 15:29:26 +02003094 if (setup_map_cpus(sched))
3095 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02003096
Jiri Olsaa151a372016-04-12 15:29:29 +02003097 if (setup_color_pids(sched))
3098 return -1;
3099
Jiri Olsacf294f22016-04-12 15:29:30 +02003100 if (setup_color_cpus(sched))
3101 return -1;
3102
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003103 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003104 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003105 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003106 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003107 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003108}
3109
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003110static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003111{
3112 unsigned long i;
3113
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003114 calibrate_run_measurement_overhead(sched);
3115 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003116
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003117 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003118
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003119 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003120 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003121
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003122 printf("nr_run_events: %ld\n", sched->nr_run_events);
3123 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
3124 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003125
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003126 if (sched->targetless_wakeups)
3127 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
3128 if (sched->multitarget_wakeups)
3129 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
3130 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003131 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003132 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003133
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003134 print_task_traces(sched);
3135 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003136
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003137 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003138 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003139 for (i = 0; i < sched->replay_repeat; i++)
3140 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003141
3142 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003143}
3144
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003145static void setup_sorting(struct perf_sched *sched, const struct option *options,
3146 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003147{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003148 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003149
3150 for (tok = strtok_r(str, ", ", &tmp);
3151 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003152 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003153 usage_with_options_msg(usage_msg, options,
3154 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003155 }
3156 }
3157
3158 free(str);
3159
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003160 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003161}
3162
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003163static int __cmd_record(int argc, const char **argv)
3164{
3165 unsigned int rec_argc, i, j;
3166 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003167 const char * const record_args[] = {
3168 "record",
3169 "-a",
3170 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003171 "-m", "1024",
3172 "-c", "1",
3173 "-e", "sched:sched_switch",
3174 "-e", "sched:sched_stat_wait",
3175 "-e", "sched:sched_stat_sleep",
3176 "-e", "sched:sched_stat_iowait",
3177 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003178 "-e", "sched:sched_process_fork",
3179 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09003180 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003181 "-e", "sched:sched_migrate_task",
3182 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003183
3184 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
3185 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3186
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02003187 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11003188 return -ENOMEM;
3189
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003190 for (i = 0; i < ARRAY_SIZE(record_args); i++)
3191 rec_argv[i] = strdup(record_args[i]);
3192
3193 for (j = 1; j < (unsigned int)argc; j++, i++)
3194 rec_argv[i] = argv[j];
3195
3196 BUG_ON(i != rec_argc);
3197
3198 return cmd_record(i, rec_argv, NULL);
3199}
3200
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003201int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003202{
Adrian Hunter8a39df82013-10-22 10:34:15 +03003203 const char default_sort_order[] = "avg, max, switch, runtime";
3204 struct perf_sched sched = {
3205 .tool = {
3206 .sample = perf_sched__process_tracepoint_sample,
3207 .comm = perf_event__process_comm,
3208 .lost = perf_event__process_lost,
3209 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003210 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003211 },
3212 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
3213 .sort_list = LIST_HEAD_INIT(sched.sort_list),
3214 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
3215 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003216 .sort_order = default_sort_order,
3217 .replay_repeat = 10,
3218 .profile_cpu = -1,
3219 .next_shortname1 = 'A',
3220 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04003221 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09003222 .show_callchain = 1,
3223 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003224 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09003225 const struct option sched_options[] = {
3226 OPT_STRING('i', "input", &input_name, "file",
3227 "input file name"),
3228 OPT_INCR('v', "verbose", &verbose,
3229 "be more verbose (show symbol address, etc)"),
3230 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3231 "dump raw trace in ASCII"),
Namhyung Kim6fa94252016-12-06 12:40:01 +09003232 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003233 OPT_END()
3234 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003235 const struct option latency_options[] = {
3236 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
3237 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003238 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
3239 "CPU to profile on"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04003240 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
3241 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003242 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003243 };
3244 const struct option replay_options[] = {
3245 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
3246 "repeat the workload replay N times (-1: infinite)"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003247 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003248 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003249 const struct option map_options[] = {
3250 OPT_BOOLEAN(0, "compact", &sched.map.comp,
3251 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02003252 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3253 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02003254 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3255 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02003256 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
3257 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003258 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02003259 };
David Ahern49394a22016-11-16 15:06:29 +09003260 const struct option timehist_options[] = {
3261 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3262 "file", "vmlinux pathname"),
3263 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3264 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09003265 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
3266 "Display call chains if present (default on)"),
3267 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
3268 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09003269 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
3270 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09003271 OPT_BOOLEAN('s', "summary", &sched.summary_only,
3272 "Show only syscall summary with statistics"),
3273 OPT_BOOLEAN('S', "with-summary", &sched.summary,
3274 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09003275 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Ahern350f54f2016-11-25 09:28:41 -07003276 OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
David Aherna407b062016-11-16 15:06:33 +09003277 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
Namhyung Kim07235f82016-12-08 23:47:54 +09003278 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
David Ahern853b7402016-11-29 10:15:44 -07003279 OPT_STRING(0, "time", &sched.time_str, "str",
3280 "Time span for analysis (start,stop)"),
David Ahern49394a22016-11-16 15:06:29 +09003281 OPT_PARENT(sched_options)
3282 };
3283
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003284 const char * const latency_usage[] = {
3285 "perf sched latency [<options>]",
3286 NULL
3287 };
3288 const char * const replay_usage[] = {
3289 "perf sched replay [<options>]",
3290 NULL
3291 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003292 const char * const map_usage[] = {
3293 "perf sched map [<options>]",
3294 NULL
3295 };
David Ahern49394a22016-11-16 15:06:29 +09003296 const char * const timehist_usage[] = {
3297 "perf sched timehist [<options>]",
3298 NULL
3299 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003300 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09003301 "replay", "script",
3302 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003303 const char *sched_usage[] = {
3304 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003305 NULL
3306 };
3307 struct trace_sched_handler lat_ops = {
3308 .wakeup_event = latency_wakeup_event,
3309 .switch_event = latency_switch_event,
3310 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003311 .migrate_task_event = latency_migrate_task_event,
3312 };
3313 struct trace_sched_handler map_ops = {
3314 .switch_event = map_switch_event,
3315 };
3316 struct trace_sched_handler replay_ops = {
3317 .wakeup_event = replay_wakeup_event,
3318 .switch_event = replay_switch_event,
3319 .fork_event = replay_fork_event,
3320 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03003321 unsigned int i;
3322
3323 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3324 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003325
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003326 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3327 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003328 if (!argc)
3329 usage_with_options(sched_usage, sched_options);
3330
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003331 /*
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003332 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003333 */
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003334 if (!strcmp(argv[0], "script"))
3335 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003336
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003337 if (!strncmp(argv[0], "rec", 3)) {
3338 return __cmd_record(argc, argv);
3339 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003340 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003341 if (argc > 1) {
3342 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3343 if (argc)
3344 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003345 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003346 setup_sorting(&sched, latency_options, latency_usage);
3347 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003348 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02003349 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02003350 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02003351 if (argc)
3352 usage_with_options(map_usage, map_options);
3353 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003354 sched.tp_handler = &map_ops;
3355 setup_sorting(&sched, latency_options, latency_usage);
3356 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003357 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003358 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003359 if (argc) {
3360 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3361 if (argc)
3362 usage_with_options(replay_usage, replay_options);
3363 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003364 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09003365 } else if (!strcmp(argv[0], "timehist")) {
3366 if (argc) {
3367 argc = parse_options(argc, argv, timehist_options,
3368 timehist_usage, 0);
3369 if (argc)
3370 usage_with_options(timehist_usage, timehist_options);
3371 }
David Ahernfc1469f2016-11-16 15:06:31 +09003372 if (sched.show_wakeups && sched.summary_only) {
3373 pr_err(" Error: -s and -w are mutually exclusive.\n");
3374 parse_options_usage(timehist_usage, timehist_options, "s", true);
3375 parse_options_usage(NULL, timehist_options, "w", true);
3376 return -EINVAL;
3377 }
3378
David Ahern49394a22016-11-16 15:06:29 +09003379 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003380 } else {
3381 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003382 }
3383
Ingo Molnarec156762009-09-11 12:12:54 +02003384 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003385}