blob: 5cd60882c8d924b90c67fae6aff89d29378bef72 [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
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030025#include <linux/kernel.h>
David Ahern49394a22016-11-16 15:06:29 +090026#include <linux/log2.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020027#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020028#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020029
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020030#include <semaphore.h>
31#include <pthread.h>
32#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080033#include <api/fs/fs.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030034#include <linux/time64.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020035
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020036#define PR_SET_NAME 15 /* Set process name */
37#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020038#define COMM_LEN 20
39#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080040#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020041
mingo39aeb522009-09-14 20:04:48 +020042struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020043
44struct task_desc {
45 unsigned long nr;
46 unsigned long pid;
47 char comm[COMM_LEN];
48
49 unsigned long nr_events;
50 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020051 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020052
53 pthread_t thread;
54 sem_t sleep_sem;
55
56 sem_t ready_for_work;
57 sem_t work_done_sem;
58
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020059 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020060};
61
62enum sched_event_type {
63 SCHED_EVENT_RUN,
64 SCHED_EVENT_SLEEP,
65 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020066 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020067};
68
mingo39aeb522009-09-14 20:04:48 +020069struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020070 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030071 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020072 u64 timestamp;
73 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020074 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020075 sem_t *wait_sem;
76 struct task_desc *wakee;
77};
78
Dongshenge936e8e2014-05-05 16:05:54 +090079#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020080
Namhyung Kim941bdea2017-01-13 19:45:21 +090081/* task state bitmask, copied from include/linux/sched.h */
82#define TASK_RUNNING 0
83#define TASK_INTERRUPTIBLE 1
84#define TASK_UNINTERRUPTIBLE 2
85#define __TASK_STOPPED 4
86#define __TASK_TRACED 8
87/* in tsk->exit_state */
88#define EXIT_DEAD 16
89#define EXIT_ZOMBIE 32
90#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
91/* in tsk->state again */
92#define TASK_DEAD 64
93#define TASK_WAKEKILL 128
94#define TASK_WAKING 256
95#define TASK_PARKED 512
96
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097enum thread_state {
98 THREAD_SLEEPING = 0,
99 THREAD_WAIT_CPU,
100 THREAD_SCHED_IN,
101 THREAD_IGNORE
102};
103
104struct work_atom {
105 struct list_head list;
106 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200107 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200108 u64 wake_up_time;
109 u64 sched_in_time;
110 u64 runtime;
111};
112
mingo39aeb522009-09-14 20:04:48 +0200113struct work_atoms {
114 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200115 struct thread *thread;
116 struct rb_node node;
117 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100118 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200119 u64 total_lat;
120 u64 nb_atoms;
121 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400122 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200123};
124
mingo39aeb522009-09-14 20:04:48 +0200125typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200126
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300127struct perf_sched;
128
129struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300130 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
131 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300132
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300133 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
134 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300135
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300136 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
137 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300138
David Aherncb627502013-08-07 22:50:47 -0400139 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
140 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
141 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300142
143 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300144 struct perf_evsel *evsel,
145 struct perf_sample *sample,
146 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300147};
148
Jiri Olsaa151a372016-04-12 15:29:29 +0200149#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200150#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200151
Jiri Olsa99623c62016-04-12 15:29:26 +0200152struct perf_sched_map {
153 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
154 int *comp_cpus;
155 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200156 struct thread_map *color_pids;
157 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200158 struct cpu_map *color_cpus;
159 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200160 struct cpu_map *cpus;
161 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200162};
163
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300164struct perf_sched {
165 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300166 const char *sort_order;
167 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800168 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300169 struct task_desc **tasks;
170 const struct trace_sched_handler *tp_handler;
171 pthread_mutex_t start_work_mutex;
172 pthread_mutex_t work_done_wait_mutex;
173 int profile_cpu;
174/*
175 * Track the current task - that way we can know whether there's any
176 * weird events, such as a task being switched away that is not current.
177 */
178 int max_cpu;
179 u32 curr_pid[MAX_CPUS];
180 struct thread *curr_thread[MAX_CPUS];
181 char next_shortname1;
182 char next_shortname2;
183 unsigned int replay_repeat;
184 unsigned long nr_run_events;
185 unsigned long nr_sleep_events;
186 unsigned long nr_wakeup_events;
187 unsigned long nr_sleep_corrections;
188 unsigned long nr_run_events_optimized;
189 unsigned long targetless_wakeups;
190 unsigned long multitarget_wakeups;
191 unsigned long nr_runs;
192 unsigned long nr_timestamps;
193 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300194 unsigned long nr_context_switch_bugs;
195 unsigned long nr_events;
196 unsigned long nr_lost_chunks;
197 unsigned long nr_lost_events;
198 u64 run_measurement_overhead;
199 u64 sleep_measurement_overhead;
200 u64 start_time;
201 u64 cpu_usage;
202 u64 runavg_cpu_usage;
203 u64 parent_cpu_usage;
204 u64 runavg_parent_cpu_usage;
205 u64 sum_runtime;
206 u64 sum_fluct;
207 u64 run_avg;
208 u64 all_runtime;
209 u64 all_count;
210 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400211 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300212 struct list_head sort_list, cmp_pid;
Yunlong Song939cda5212015-03-31 21:46:34 +0800213 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400214 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200215 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900216
217 /* options for timehist command */
218 bool summary;
219 bool summary_only;
Namhyung Kim699b5b92016-12-08 23:47:52 +0900220 bool idle_hist;
David Ahern6c973c92016-11-16 15:06:32 +0900221 bool show_callchain;
222 unsigned int max_stack;
David Aherna407b062016-11-16 15:06:33 +0900223 bool show_cpu_visual;
David Ahernfc1469f2016-11-16 15:06:31 +0900224 bool show_wakeups;
Brendan Gregg292c4a82017-03-14 01:56:29 +0000225 bool show_next;
David Ahern350f54f2016-11-25 09:28:41 -0700226 bool show_migrations;
Namhyung Kim414e0502017-01-13 19:45:22 +0900227 bool show_state;
David Ahern52df1382016-11-16 15:06:30 +0900228 u64 skipped_samples;
David Ahern853b7402016-11-29 10:15:44 -0700229 const char *time_str;
230 struct perf_time_interval ptime;
Namhyung Kim9396c9c2016-12-22 15:03:50 +0900231 struct perf_time_interval hist_time;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300232};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200233
David Ahern49394a22016-11-16 15:06:29 +0900234/* per thread run time data */
235struct thread_runtime {
236 u64 last_time; /* time of previous sched in/out event */
237 u64 dt_run; /* run time */
Namhyung Kim941bdea2017-01-13 19:45:21 +0900238 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
239 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
240 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
David Ahern49394a22016-11-16 15:06:29 +0900241 u64 dt_delay; /* time between wakeup and sched-in */
242 u64 ready_to_run; /* time of wakeup */
243
244 struct stats run_stats;
245 u64 total_run_time;
Namhyung Kim587782c2017-01-13 19:45:23 +0900246 u64 total_sleep_time;
247 u64 total_iowait_time;
248 u64 total_preempt_time;
249 u64 total_delay_time;
David Ahern350f54f2016-11-25 09:28:41 -0700250
Namhyung Kim941bdea2017-01-13 19:45:21 +0900251 int last_state;
David Ahern350f54f2016-11-25 09:28:41 -0700252 u64 migrations;
David Ahern49394a22016-11-16 15:06:29 +0900253};
254
255/* per event run time data */
256struct evsel_runtime {
257 u64 *last_time; /* time this event was last seen per cpu */
258 u32 ncpu; /* highest cpu slot allocated */
259};
260
Namhyung Kim3bc2fa92016-12-08 23:47:51 +0900261/* per cpu idle time data */
262struct idle_thread_runtime {
263 struct thread_runtime tr;
264 struct thread *last_thread;
265 struct rb_root sorted_root;
266 struct callchain_root callchain;
267 struct callchain_cursor cursor;
268};
269
David Ahern49394a22016-11-16 15:06:29 +0900270/* track idle times per cpu */
271static struct thread **idle_threads;
272static int idle_max_cpu;
273static char idle_comm[] = "<idle>";
274
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200275static u64 get_nsecs(void)
276{
277 struct timespec ts;
278
279 clock_gettime(CLOCK_MONOTONIC, &ts);
280
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300281 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200282}
283
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300284static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200285{
286 u64 T0 = get_nsecs(), T1;
287
288 do {
289 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300290 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200291}
292
293static void sleep_nsecs(u64 nsecs)
294{
295 struct timespec ts;
296
297 ts.tv_nsec = nsecs % 999999999;
298 ts.tv_sec = nsecs / 999999999;
299
300 nanosleep(&ts, NULL);
301}
302
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300303static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200304{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300305 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200306 int i;
307
308 for (i = 0; i < 10; i++) {
309 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300310 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200311 T1 = get_nsecs();
312 delta = T1-T0;
313 min_delta = min(min_delta, delta);
314 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300315 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200316
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200317 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200318}
319
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300320static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200321{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300322 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200323 int i;
324
325 for (i = 0; i < 10; i++) {
326 T0 = get_nsecs();
327 sleep_nsecs(10000);
328 T1 = get_nsecs();
329 delta = T1-T0;
330 min_delta = min(min_delta, delta);
331 }
332 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300333 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200334
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200335 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200336}
337
mingo39aeb522009-09-14 20:04:48 +0200338static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200339get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200340{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200341 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200342 unsigned long idx = task->nr_events;
343 size_t size;
344
345 event->timestamp = timestamp;
346 event->nr = idx;
347
348 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200349 size = sizeof(struct sched_atom *) * task->nr_events;
350 task->atoms = realloc(task->atoms, size);
351 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200352
mingo39aeb522009-09-14 20:04:48 +0200353 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200354
355 return event;
356}
357
mingo39aeb522009-09-14 20:04:48 +0200358static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200359{
360 if (!task->nr_events)
361 return NULL;
362
mingo39aeb522009-09-14 20:04:48 +0200363 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200364}
365
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300366static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
367 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200368{
mingo39aeb522009-09-14 20:04:48 +0200369 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200370
371 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200372 * optimize an existing RUN event by merging this one
373 * to it:
374 */
Ingo Molnarec156762009-09-11 12:12:54 +0200375 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300376 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200377 curr_event->duration += duration;
378 return;
379 }
380
381 event = get_new_event(task, timestamp);
382
383 event->type = SCHED_EVENT_RUN;
384 event->duration = duration;
385
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300386 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200387}
388
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300389static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
390 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200391{
mingo39aeb522009-09-14 20:04:48 +0200392 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200393
394 event = get_new_event(task, timestamp);
395 event->type = SCHED_EVENT_WAKEUP;
396 event->wakee = wakee;
397
398 wakee_event = last_event(wakee);
399 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300400 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200401 return;
402 }
403 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300404 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200405 return;
406 }
407
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200408 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200409 sem_init(wakee_event->wait_sem, 0, 0);
410 wakee_event->specific_wait = 1;
411 event->wait_sem = wakee_event->wait_sem;
412
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300413 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200414}
415
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300416static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
417 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200418{
mingo39aeb522009-09-14 20:04:48 +0200419 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200420
421 event->type = SCHED_EVENT_SLEEP;
422
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300423 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200424}
425
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300426static struct task_desc *register_pid(struct perf_sched *sched,
427 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200428{
429 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800430 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200431
Yunlong Songcb06ac22015-03-31 21:46:30 +0800432 if (sched->pid_to_task == NULL) {
433 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
434 pid_max = MAX_PID;
435 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
436 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800437 if (pid >= (unsigned long)pid_max) {
438 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
439 sizeof(struct task_desc *))) == NULL);
440 while (pid >= (unsigned long)pid_max)
441 sched->pid_to_task[pid_max++] = NULL;
442 }
Ingo Molnarec156762009-09-11 12:12:54 +0200443
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300444 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200445
446 if (task)
447 return task;
448
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200449 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200450 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300451 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200452 strcpy(task->comm, comm);
453 /*
454 * every task starts in sleeping state - this gets ignored
455 * if there's no wakeup pointing to this sleep state:
456 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300457 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200458
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300459 sched->pid_to_task[pid] = task;
460 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800461 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300462 BUG_ON(!sched->tasks);
463 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200464
Namhyung Kimbb963e12017-02-17 17:17:38 +0900465 if (verbose > 0)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300466 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200467
468 return task;
469}
470
471
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300472static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200473{
474 struct task_desc *task;
475 unsigned long i;
476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300477 for (i = 0; i < sched->nr_tasks; i++) {
478 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200479 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200480 task->nr, task->comm, task->pid, task->nr_events);
481 }
482}
483
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300484static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200485{
486 struct task_desc *task1, *task2;
487 unsigned long i, j;
488
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300489 for (i = 0; i < sched->nr_tasks; i++) {
490 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200491 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300492 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200493 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300494 task2 = sched->tasks[j];
495 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200496 }
497}
498
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300499static void perf_sched__process_event(struct perf_sched *sched,
500 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200501{
502 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200503
mingo39aeb522009-09-14 20:04:48 +0200504 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200505 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300506 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200507 break;
508 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200509 if (atom->wait_sem)
510 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200511 BUG_ON(ret);
512 break;
513 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200514 if (atom->wait_sem)
515 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200516 BUG_ON(ret);
517 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200518 case SCHED_EVENT_MIGRATION:
519 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200520 default:
521 BUG_ON(1);
522 }
523}
524
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200525static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200526{
527 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200528 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200529 int err;
530
531 err = getrusage(RUSAGE_SELF, &ru);
532 BUG_ON(err);
533
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300534 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
535 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200536
537 return sum;
538}
539
Yunlong Song939cda5212015-03-31 21:46:34 +0800540static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200541{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800542 struct perf_event_attr attr;
Yunlong Song939cda5212015-03-31 21:46:34 +0800543 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800544 int fd;
Yunlong Song939cda5212015-03-31 21:46:34 +0800545 struct rlimit limit;
546 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800547
548 memset(&attr, 0, sizeof(attr));
549
550 attr.type = PERF_TYPE_SOFTWARE;
551 attr.config = PERF_COUNT_SW_TASK_CLOCK;
552
Yunlong Song939cda5212015-03-31 21:46:34 +0800553force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200554 fd = sys_perf_event_open(&attr, 0, -1, -1,
555 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800556
Yunlong Song1aff59b2015-03-31 21:46:33 +0800557 if (fd < 0) {
Yunlong Song939cda5212015-03-31 21:46:34 +0800558 if (errno == EMFILE) {
559 if (sched->force) {
560 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
561 limit.rlim_cur += sched->nr_tasks - cur_task;
562 if (limit.rlim_cur > limit.rlim_max) {
563 limit.rlim_max = limit.rlim_cur;
564 need_privilege = true;
565 }
566 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
567 if (need_privilege && errno == EPERM)
568 strcpy(info, "Need privilege\n");
569 } else
570 goto force_again;
571 } else
572 strcpy(info, "Have a try with -f option\n");
573 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900574 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda5212015-03-31 21:46:34 +0800575 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300576 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800577 exit(EXIT_FAILURE);
578 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800579 return fd;
580}
581
582static u64 get_cpu_usage_nsec_self(int fd)
583{
584 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200585 int ret;
586
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800587 ret = read(fd, &runtime, sizeof(runtime));
588 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200589
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800590 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200591}
592
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300593struct sched_thread_parms {
594 struct task_desc *task;
595 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800596 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300597};
598
Ingo Molnarec156762009-09-11 12:12:54 +0200599static void *thread_func(void *ctx)
600{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300601 struct sched_thread_parms *parms = ctx;
602 struct task_desc *this_task = parms->task;
603 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200604 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200605 unsigned long i, ret;
606 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800607 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200608
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300609 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300610
Ingo Molnarec156762009-09-11 12:12:54 +0200611 sprintf(comm2, ":%s", this_task->comm);
612 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300613 if (fd < 0)
614 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200615again:
616 ret = sem_post(&this_task->ready_for_work);
617 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300618 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200619 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300620 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200621 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200622
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800623 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200624
625 for (i = 0; i < this_task->nr_events; i++) {
626 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200628 }
629
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800630 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200631 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200632 ret = sem_post(&this_task->work_done_sem);
633 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200634
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300635 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200636 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200638 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200639
640 goto again;
641}
642
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200644{
645 struct task_desc *task;
646 pthread_attr_t attr;
647 unsigned long i;
648 int err;
649
650 err = pthread_attr_init(&attr);
651 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200652 err = pthread_attr_setstacksize(&attr,
653 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200654 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200656 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300657 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200658 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300659 for (i = 0; i < sched->nr_tasks; i++) {
660 struct sched_thread_parms *parms = malloc(sizeof(*parms));
661 BUG_ON(parms == NULL);
662 parms->task = task = sched->tasks[i];
663 parms->sched = sched;
Yunlong Song939cda5212015-03-31 21:46:34 +0800664 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200665 sem_init(&task->sleep_sem, 0, 0);
666 sem_init(&task->ready_for_work, 0, 0);
667 sem_init(&task->work_done_sem, 0, 0);
668 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300669 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200670 BUG_ON(err);
671 }
672}
673
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300674static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200675{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200676 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200677 struct task_desc *task;
678 unsigned long i, ret;
679
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300680 sched->start_time = get_nsecs();
681 sched->cpu_usage = 0;
682 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200683
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684 for (i = 0; i < sched->nr_tasks; i++) {
685 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200686 ret = sem_wait(&task->ready_for_work);
687 BUG_ON(ret);
688 sem_init(&task->ready_for_work, 0, 0);
689 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300690 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200691 BUG_ON(ret);
692
693 cpu_usage_0 = get_cpu_usage_nsec_parent();
694
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300695 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200696
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300697 for (i = 0; i < sched->nr_tasks; i++) {
698 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200699 ret = sem_wait(&task->work_done_sem);
700 BUG_ON(ret);
701 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300702 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200703 task->cpu_usage = 0;
704 }
705
706 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300707 if (!sched->runavg_cpu_usage)
708 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800709 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 +0200710
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300711 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
712 if (!sched->runavg_parent_cpu_usage)
713 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800714 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
715 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200716
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300717 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200718 BUG_ON(ret);
719
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300720 for (i = 0; i < sched->nr_tasks; i++) {
721 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200722 sem_init(&task->sleep_sem, 0, 0);
723 task->curr_event = 0;
724 }
725}
726
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300727static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200728{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500729 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200730
731 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300732 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200733 T1 = get_nsecs();
734
735 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300736 sched->sum_runtime += delta;
737 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200738
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300739 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200740 if (delta < avg_delta)
741 fluct = avg_delta - delta;
742 else
743 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300744 sched->sum_fluct += fluct;
745 if (!sched->run_avg)
746 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800747 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200748
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300749 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200750
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300751 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200752
Ingo Molnarad236fd2009-09-11 12:12:54 +0200753 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300754 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200755
756#if 0
757 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200758 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300759 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200760 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200761 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300762 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
763 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200764#endif
765
Ingo Molnarad236fd2009-09-11 12:12:54 +0200766 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200767
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300768 if (sched->nr_sleep_corrections)
769 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
770 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200771}
772
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300773static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200774{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200775 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200776
777 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300778 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200779 T1 = get_nsecs();
780
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200781 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200782
783 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300784 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200785 T1 = get_nsecs();
786
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200787 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200788}
789
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300790static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300791replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300792 struct perf_evsel *evsel, struct perf_sample *sample,
793 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200794{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300795 const char *comm = perf_evsel__strval(evsel, sample, "comm");
796 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200797 struct task_desc *waker, *wakee;
798
Namhyung Kimbb963e12017-02-17 17:17:38 +0900799 if (verbose > 0) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300800 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200801
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300802 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200803 }
804
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300805 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300806 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200807
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300808 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300809 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200810}
811
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300812static int replay_switch_event(struct perf_sched *sched,
813 struct perf_evsel *evsel,
814 struct perf_sample *sample,
815 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200816{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300817 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
818 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
819 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
820 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
821 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300822 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300823 u64 timestamp0, timestamp = sample->time;
824 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200825 s64 delta;
826
Namhyung Kimbb963e12017-02-17 17:17:38 +0900827 if (verbose > 0)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300828 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200829
Ingo Molnarfbf94822009-09-11 12:12:54 +0200830 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300831 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200832
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300833 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200834 if (timestamp0)
835 delta = timestamp - timestamp0;
836 else
837 delta = 0;
838
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300839 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900840 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300841 return -1;
842 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200843
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300844 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
845 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200846
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300847 prev = register_pid(sched, prev_pid, prev_comm);
848 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200849
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300850 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200851
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300852 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300853 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300854
855 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200856}
857
David Aherncb627502013-08-07 22:50:47 -0400858static int replay_fork_event(struct perf_sched *sched,
859 union perf_event *event,
860 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200861{
David Aherncb627502013-08-07 22:50:47 -0400862 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300863
Adrian Hunter314add62013-08-27 11:23:03 +0300864 child = machine__findnew_thread(machine, event->fork.pid,
865 event->fork.tid);
866 parent = machine__findnew_thread(machine, event->fork.ppid,
867 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400868
869 if (child == NULL || parent == NULL) {
870 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
871 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300872 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200873 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300874
Namhyung Kimbb963e12017-02-17 17:17:38 +0900875 if (verbose > 0) {
David Aherncb627502013-08-07 22:50:47 -0400876 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200877 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
878 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400879 }
880
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200881 register_pid(sched, parent->tid, thread__comm_str(parent));
882 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300883out_put:
884 thread__put(child);
885 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300886 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200887}
888
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200889struct sort_dimension {
890 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200891 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200892 struct list_head list;
893};
894
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200895static int
mingo39aeb522009-09-14 20:04:48 +0200896thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200897{
898 struct sort_dimension *sort;
899 int ret = 0;
900
Ingo Molnarb5fae122009-09-11 12:12:54 +0200901 BUG_ON(list_empty(list));
902
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200903 list_for_each_entry(sort, list, list) {
904 ret = sort->cmp(l, r);
905 if (ret)
906 return ret;
907 }
908
909 return ret;
910}
911
mingo39aeb522009-09-14 20:04:48 +0200912static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200913thread_atoms_search(struct rb_root *root, struct thread *thread,
914 struct list_head *sort_list)
915{
916 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200917 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200918
919 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200920 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200921 int cmp;
922
mingo39aeb522009-09-14 20:04:48 +0200923 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200924
925 cmp = thread_lat_cmp(sort_list, &key, atoms);
926 if (cmp > 0)
927 node = node->rb_left;
928 else if (cmp < 0)
929 node = node->rb_right;
930 else {
931 BUG_ON(thread != atoms->thread);
932 return atoms;
933 }
934 }
935 return NULL;
936}
937
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938static void
mingo39aeb522009-09-14 20:04:48 +0200939__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200940 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941{
942 struct rb_node **new = &(root->rb_node), *parent = NULL;
943
944 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200945 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200946 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947
mingo39aeb522009-09-14 20:04:48 +0200948 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200949 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200950
951 cmp = thread_lat_cmp(sort_list, data, this);
952
953 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200955 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200956 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957 }
958
959 rb_link_node(&data->node, parent, new);
960 rb_insert_color(&data->node, root);
961}
962
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300963static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200964{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200965 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300966 if (!atoms) {
967 pr_err("No memory at %s\n", __func__);
968 return -1;
969 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200970
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300971 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200972 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300973 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300974 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200975}
976
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300977static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978{
979 const char *str = TASK_STATE_TO_CHAR_STR;
980
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300981 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982}
983
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300984static int
mingo39aeb522009-09-14 20:04:48 +0200985add_sched_out_event(struct work_atoms *atoms,
986 char run_state,
987 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200989 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300990 if (!atom) {
991 pr_err("Non memory at %s", __func__);
992 return -1;
993 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200994
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200995 atom->sched_out_time = timestamp;
996
mingo39aeb522009-09-14 20:04:48 +0200997 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200998 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200999 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +02001000 }
1001
mingo39aeb522009-09-14 20:04:48 +02001002 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001003 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001004}
1005
1006static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001007add_runtime_event(struct work_atoms *atoms, u64 delta,
1008 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +02001009{
1010 struct work_atom *atom;
1011
1012 BUG_ON(list_empty(&atoms->work_list));
1013
1014 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1015
1016 atom->runtime += delta;
1017 atoms->total_runtime += delta;
1018}
1019
1020static void
1021add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001022{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001023 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001024 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001025
mingo39aeb522009-09-14 20:04:48 +02001026 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027 return;
1028
mingo39aeb522009-09-14 20:04:48 +02001029 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001031 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001032 return;
1033
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001034 if (timestamp < atom->wake_up_time) {
1035 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001036 return;
1037 }
1038
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001039 atom->state = THREAD_SCHED_IN;
1040 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001041
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001042 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001043 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001044 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001045 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001046 atoms->max_lat_at = timestamp;
1047 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001048 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001049}
1050
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001051static int latency_switch_event(struct perf_sched *sched,
1052 struct perf_evsel *evsel,
1053 struct perf_sample *sample,
1054 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001055{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001056 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1057 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1058 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001059 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001060 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001061 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001062 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001063 s64 delta;
1064
mingo39aeb522009-09-14 20:04:48 +02001065 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001066
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001067 timestamp0 = sched->cpu_last_switched[cpu];
1068 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001069 if (timestamp0)
1070 delta = timestamp - timestamp0;
1071 else
1072 delta = 0;
1073
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001074 if (delta < 0) {
1075 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1076 return -1;
1077 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001078
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001079 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1080 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001081 if (sched_out == NULL || sched_in == NULL)
1082 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001083
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001084 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001085 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001086 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001087 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001088 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001089 if (!out_events) {
1090 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001091 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001092 }
mingo39aeb522009-09-14 20:04:48 +02001093 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001094 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001095 return -1;
mingo39aeb522009-09-14 20:04:48 +02001096
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001097 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001098 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001099 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001100 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001101 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001102 if (!in_events) {
1103 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001104 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001105 }
mingo39aeb522009-09-14 20:04:48 +02001106 /*
1107 * Take came in we have not heard about yet,
1108 * add in an initial atom in runnable state:
1109 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001110 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001111 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001112 }
1113 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001114 err = 0;
1115out_put:
1116 thread__put(sched_out);
1117 thread__put(sched_in);
1118 return err;
mingo39aeb522009-09-14 20:04:48 +02001119}
1120
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001121static int latency_runtime_event(struct perf_sched *sched,
1122 struct perf_evsel *evsel,
1123 struct perf_sample *sample,
1124 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001125{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001126 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1127 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001128 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001129 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001130 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001131 int cpu = sample->cpu, err = -1;
1132
1133 if (thread == NULL)
1134 return -1;
mingo39aeb522009-09-14 20:04:48 +02001135
1136 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001137 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001138 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001139 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001140 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001141 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001142 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001143 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001144 }
1145 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001146 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001147 }
1148
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001149 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001150 err = 0;
1151out_put:
1152 thread__put(thread);
1153 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001154}
1155
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001156static int latency_wakeup_event(struct perf_sched *sched,
1157 struct perf_evsel *evsel,
1158 struct perf_sample *sample,
1159 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001160{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001161 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001162 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001163 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001164 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001165 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001166 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001167
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001168 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001169 if (wakee == NULL)
1170 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001171 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001172 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001173 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001174 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001175 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001176 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001177 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001178 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001179 }
1180 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001181 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001182 }
1183
mingo39aeb522009-09-14 20:04:48 +02001184 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001185
mingo39aeb522009-09-14 20:04:48 +02001186 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001187
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001188 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001189 * As we do not guarantee the wakeup event happens when
1190 * task is out of run queue, also may happen when task is
1191 * on run queue and wakeup only change ->state to TASK_RUNNING,
1192 * then we should not set the ->wake_up_time when wake up a
1193 * task which is on run queue.
1194 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001195 * You WILL be missing events if you've recorded only
1196 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001197 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001198 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001199 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001200 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001201
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001202 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001203 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001204 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001205 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001206 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001207
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001208 atom->state = THREAD_WAIT_CPU;
1209 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001210out_ok:
1211 err = 0;
1212out_put:
1213 thread__put(wakee);
1214 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001215}
1216
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001217static int latency_migrate_task_event(struct perf_sched *sched,
1218 struct perf_evsel *evsel,
1219 struct perf_sample *sample,
1220 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001221{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001222 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001223 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001224 struct work_atoms *atoms;
1225 struct work_atom *atom;
1226 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001227 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001228
1229 /*
1230 * Only need to worry about migration when profiling one CPU.
1231 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001232 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001233 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001234
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001235 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001236 if (migrant == NULL)
1237 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001238 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001239 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001240 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001241 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001242 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001243 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001244 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001245 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001246 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001247 }
1248 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001249 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001250 }
1251
1252 BUG_ON(list_empty(&atoms->work_list));
1253
1254 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1255 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1256
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001257 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001258
1259 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001260 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001261 err = 0;
1262out_put:
1263 thread__put(migrant);
1264 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001265}
1266
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001267static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001268{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001269 int i;
1270 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001271 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001272 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001273
mingo39aeb522009-09-14 20:04:48 +02001274 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001275 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001276 /*
1277 * Ignore idle threads:
1278 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001279 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001280 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001281
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001282 sched->all_runtime += work_list->total_runtime;
1283 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001284
Josef Bacik2f80dd42015-05-22 09:18:40 -04001285 if (work_list->num_merged > 1)
1286 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1287 else
1288 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001289
mingo08f69e62009-09-14 18:30:44 +02001290 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001291 printf(" ");
1292
mingo39aeb522009-09-14 20:04:48 +02001293 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001294 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001295
Namhyung Kim99620a52016-10-24 11:02:45 +09001296 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 -03001297 (double)work_list->total_runtime / NSEC_PER_MSEC,
1298 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1299 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001300 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001301}
1302
mingo39aeb522009-09-14 20:04:48 +02001303static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001304{
Jiri Olsa0014de12015-11-02 12:10:25 +01001305 if (l->thread == r->thread)
1306 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001307 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001308 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001309 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001310 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001311 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001312}
1313
mingo39aeb522009-09-14 20:04:48 +02001314static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001315{
1316 u64 avgl, avgr;
1317
1318 if (!l->nb_atoms)
1319 return -1;
1320
1321 if (!r->nb_atoms)
1322 return 1;
1323
1324 avgl = l->total_lat / l->nb_atoms;
1325 avgr = r->total_lat / r->nb_atoms;
1326
1327 if (avgl < avgr)
1328 return -1;
1329 if (avgl > avgr)
1330 return 1;
1331
1332 return 0;
1333}
1334
mingo39aeb522009-09-14 20:04:48 +02001335static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001336{
1337 if (l->max_lat < r->max_lat)
1338 return -1;
1339 if (l->max_lat > r->max_lat)
1340 return 1;
1341
1342 return 0;
1343}
1344
mingo39aeb522009-09-14 20:04:48 +02001345static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001346{
1347 if (l->nb_atoms < r->nb_atoms)
1348 return -1;
1349 if (l->nb_atoms > r->nb_atoms)
1350 return 1;
1351
1352 return 0;
1353}
1354
mingo39aeb522009-09-14 20:04:48 +02001355static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001356{
1357 if (l->total_runtime < r->total_runtime)
1358 return -1;
1359 if (l->total_runtime > r->total_runtime)
1360 return 1;
1361
1362 return 0;
1363}
1364
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001365static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001366{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001367 size_t i;
1368 static struct sort_dimension avg_sort_dimension = {
1369 .name = "avg",
1370 .cmp = avg_cmp,
1371 };
1372 static struct sort_dimension max_sort_dimension = {
1373 .name = "max",
1374 .cmp = max_cmp,
1375 };
1376 static struct sort_dimension pid_sort_dimension = {
1377 .name = "pid",
1378 .cmp = pid_cmp,
1379 };
1380 static struct sort_dimension runtime_sort_dimension = {
1381 .name = "runtime",
1382 .cmp = runtime_cmp,
1383 };
1384 static struct sort_dimension switch_sort_dimension = {
1385 .name = "switch",
1386 .cmp = switch_cmp,
1387 };
1388 struct sort_dimension *available_sorts[] = {
1389 &pid_sort_dimension,
1390 &avg_sort_dimension,
1391 &max_sort_dimension,
1392 &switch_sort_dimension,
1393 &runtime_sort_dimension,
1394 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001395
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001396 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001397 if (!strcmp(available_sorts[i]->name, tok)) {
1398 list_add_tail(&available_sorts[i]->list, list);
1399
1400 return 0;
1401 }
1402 }
1403
1404 return -1;
1405}
1406
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001407static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001408{
1409 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001410 struct rb_root *root = &sched->atom_root;
1411again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001412 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001413 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001414 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001415 if (!node)
1416 break;
1417
Josef Bacik2f80dd42015-05-22 09:18:40 -04001418 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001419 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001420 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001421 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001422 if (root == &sched->atom_root) {
1423 root = &sched->merged_atom_root;
1424 goto again;
1425 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001426}
1427
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001428static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001429 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001430 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001431 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001432{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001433 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001434
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001435 if (sched->tp_handler->wakeup_event)
1436 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001437
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001438 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001439}
1440
Jiri Olsaa151a372016-04-12 15:29:29 +02001441union map_priv {
1442 void *ptr;
1443 bool color;
1444};
1445
1446static bool thread__has_color(struct thread *thread)
1447{
1448 union map_priv priv = {
1449 .ptr = thread__priv(thread),
1450 };
1451
1452 return priv.color;
1453}
1454
1455static struct thread*
1456map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1457{
1458 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1459 union map_priv priv = {
1460 .color = false,
1461 };
1462
1463 if (!sched->map.color_pids || !thread || thread__priv(thread))
1464 return thread;
1465
1466 if (thread_map__has(sched->map.color_pids, tid))
1467 priv.color = true;
1468
1469 thread__set_priv(thread, priv.ptr);
1470 return thread;
1471}
1472
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001473static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1474 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001475{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001476 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1477 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001478 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001479 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001480 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001481 int i, this_cpu = sample->cpu;
1482 int cpus_nr;
1483 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001484 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001485 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001486
1487 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1488
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001489 if (this_cpu > sched->max_cpu)
1490 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001491
Jiri Olsa99623c62016-04-12 15:29:26 +02001492 if (sched->map.comp) {
1493 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1494 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1495 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1496 new_cpu = true;
1497 }
1498 } else
1499 cpus_nr = sched->max_cpu;
1500
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001501 timestamp0 = sched->cpu_last_switched[this_cpu];
1502 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001503 if (timestamp0)
1504 delta = timestamp - timestamp0;
1505 else
1506 delta = 0;
1507
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001508 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001509 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001510 return -1;
1511 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001512
Jiri Olsaa151a372016-04-12 15:29:29 +02001513 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001514 if (sched_in == NULL)
1515 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001516
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001517 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001518
1519 printf(" ");
1520
1521 new_shortname = 0;
1522 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001523 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1524 /*
1525 * Don't allocate a letter-number for swapper:0
1526 * as a shortname. Instead, we use '.' for it.
1527 */
1528 sched_in->shortname[0] = '.';
1529 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001530 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001531 sched_in->shortname[0] = sched->next_shortname1;
1532 sched_in->shortname[1] = sched->next_shortname2;
1533
1534 if (sched->next_shortname1 < 'Z') {
1535 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001536 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001537 sched->next_shortname1 = 'A';
1538 if (sched->next_shortname2 < '9')
1539 sched->next_shortname2++;
1540 else
1541 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001542 }
1543 }
1544 new_shortname = 1;
1545 }
1546
Jiri Olsa99623c62016-04-12 15:29:26 +02001547 for (i = 0; i < cpus_nr; i++) {
1548 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001549 struct thread *curr_thread = sched->curr_thread[cpu];
1550 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001551 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001552
1553 if (curr_thread && thread__has_color(curr_thread))
1554 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001555
Jiri Olsa73643bb2016-04-12 15:29:31 +02001556 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1557 continue;
1558
Jiri Olsacf294f22016-04-12 15:29:30 +02001559 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1560 cpu_color = COLOR_CPUS;
1561
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001562 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001563 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001564 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001565 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001566
Dongsheng6bcab4e2014-05-06 14:39:01 +09001567 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001568 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001569 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001570 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001571 }
1572
Jiri Olsa73643bb2016-04-12 15:29:31 +02001573 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1574 goto out;
1575
Namhyung Kim99620a52016-10-24 11:02:45 +09001576 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1577 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kimbb963e12017-02-17 17:17:38 +09001578 if (new_shortname || (verbose > 0 && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001579 const char *pid_color = color;
1580
1581 if (thread__has_color(sched_in))
1582 pid_color = COLOR_PIDS;
1583
1584 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001585 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001586 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001587
Jiri Olsa99623c62016-04-12 15:29:26 +02001588 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001589 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001590
Jiri Olsa73643bb2016-04-12 15:29:31 +02001591out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001592 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001593
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001594 thread__put(sched_in);
1595
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001596 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001597}
1598
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001599static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001600 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001601 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001602 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001603{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001604 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001605 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001606 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1607 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001608
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001609 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001610 /*
1611 * Are we trying to switch away a PID that is
1612 * not current?
1613 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001614 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001615 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001616 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001617
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001618 if (sched->tp_handler->switch_event)
1619 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001620
1621 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001622 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001623}
1624
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001625static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001626 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001627 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001628 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001629{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001630 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001631
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001632 if (sched->tp_handler->runtime_event)
1633 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001634
1635 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001636}
1637
David Aherncb627502013-08-07 22:50:47 -04001638static int perf_sched__process_fork_event(struct perf_tool *tool,
1639 union perf_event *event,
1640 struct perf_sample *sample,
1641 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001642{
1643 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1644
David Aherncb627502013-08-07 22:50:47 -04001645 /* run the fork event through the perf machineruy */
1646 perf_event__process_fork(tool, event, sample, machine);
1647
1648 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001649 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001650 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001651
1652 return 0;
1653}
1654
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001655static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001656 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001657 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001658 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001659{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001660 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001661
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001662 if (sched->tp_handler->migrate_task_event)
1663 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001664
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001665 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001666}
1667
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001668typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001669 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001670 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001671 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001672
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001673static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1674 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001675 struct perf_sample *sample,
1676 struct perf_evsel *evsel,
1677 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001678{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001679 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001680
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001681 if (evsel->handler != NULL) {
1682 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001683 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001684 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001685
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001686 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001687}
1688
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001689static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001690{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001691 const struct perf_evsel_str_handler handlers[] = {
1692 { "sched:sched_switch", process_sched_switch_event, },
1693 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1694 { "sched:sched_wakeup", process_sched_wakeup_event, },
1695 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001696 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1697 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001698 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001699 struct perf_data_file file = {
1700 .path = input_name,
1701 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001702 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001703 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001704 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001705
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001706 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001707 if (session == NULL) {
1708 pr_debug("No Memory for session\n");
1709 return -1;
1710 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001711
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001712 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001713
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001714 if (perf_session__set_tracepoints_handlers(session, handlers))
1715 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001716
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001717 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001718 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001719 if (err) {
1720 pr_err("Failed to process events, error %d", err);
1721 goto out_delete;
1722 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001723
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001724 sched->nr_events = session->evlist->stats.nr_events[0];
1725 sched->nr_lost_events = session->evlist->stats.total_lost;
1726 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001727 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001728
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001729 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001730out_delete:
1731 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001732 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001733}
1734
David Ahern49394a22016-11-16 15:06:29 +09001735/*
1736 * scheduling times are printed as msec.usec
1737 */
1738static inline void print_sched_time(unsigned long long nsecs, int width)
1739{
1740 unsigned long msecs;
1741 unsigned long usecs;
1742
1743 msecs = nsecs / NSEC_PER_MSEC;
1744 nsecs -= msecs * NSEC_PER_MSEC;
1745 usecs = nsecs / NSEC_PER_USEC;
1746 printf("%*lu.%03lu ", width, msecs, usecs);
1747}
1748
1749/*
1750 * returns runtime data for event, allocating memory for it the
1751 * first time it is used.
1752 */
1753static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1754{
1755 struct evsel_runtime *r = evsel->priv;
1756
1757 if (r == NULL) {
1758 r = zalloc(sizeof(struct evsel_runtime));
1759 evsel->priv = r;
1760 }
1761
1762 return r;
1763}
1764
1765/*
1766 * save last time event was seen per cpu
1767 */
1768static void perf_evsel__save_time(struct perf_evsel *evsel,
1769 u64 timestamp, u32 cpu)
1770{
1771 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1772
1773 if (r == NULL)
1774 return;
1775
1776 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1777 int i, n = __roundup_pow_of_two(cpu+1);
1778 void *p = r->last_time;
1779
1780 p = realloc(r->last_time, n * sizeof(u64));
1781 if (!p)
1782 return;
1783
1784 r->last_time = p;
1785 for (i = r->ncpu; i < n; ++i)
1786 r->last_time[i] = (u64) 0;
1787
1788 r->ncpu = n;
1789 }
1790
1791 r->last_time[cpu] = timestamp;
1792}
1793
1794/* returns last time this event was seen on the given cpu */
1795static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1796{
1797 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1798
1799 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1800 return 0;
1801
1802 return r->last_time[cpu];
1803}
1804
Namhyung Kim9b8087d2016-12-22 15:03:48 +09001805static int comm_width = 30;
David Ahern49394a22016-11-16 15:06:29 +09001806
1807static char *timehist_get_commstr(struct thread *thread)
1808{
1809 static char str[32];
1810 const char *comm = thread__comm_str(thread);
1811 pid_t tid = thread->tid;
1812 pid_t pid = thread->pid_;
1813 int n;
1814
1815 if (pid == 0)
1816 n = scnprintf(str, sizeof(str), "%s", comm);
1817
1818 else if (tid != pid)
1819 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1820
1821 else
1822 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1823
1824 if (n > comm_width)
1825 comm_width = n;
1826
1827 return str;
1828}
1829
David Aherna407b062016-11-16 15:06:33 +09001830static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001831{
David Aherna407b062016-11-16 15:06:33 +09001832 u32 ncpus = sched->max_cpu + 1;
1833 u32 i, j;
1834
David Ahern49394a22016-11-16 15:06:29 +09001835 printf("%15s %6s ", "time", "cpu");
1836
David Aherna407b062016-11-16 15:06:33 +09001837 if (sched->show_cpu_visual) {
1838 printf(" ");
1839 for (i = 0, j = 0; i < ncpus; ++i) {
1840 printf("%x", j++);
1841 if (j > 15)
1842 j = 0;
1843 }
1844 printf(" ");
1845 }
1846
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001847 printf(" %-*s %9s %9s %9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001848 "task name", "wait time", "sch delay", "run time");
1849
Namhyung Kim414e0502017-01-13 19:45:22 +09001850 if (sched->show_state)
1851 printf(" %s", "state");
1852
David Ahern49394a22016-11-16 15:06:29 +09001853 printf("\n");
1854
1855 /*
1856 * units row
1857 */
1858 printf("%15s %-6s ", "", "");
1859
David Aherna407b062016-11-16 15:06:33 +09001860 if (sched->show_cpu_visual)
1861 printf(" %*s ", ncpus, "");
1862
Namhyung Kim414e0502017-01-13 19:45:22 +09001863 printf(" %-*s %9s %9s %9s", comm_width,
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001864 "[tid/pid]", "(msec)", "(msec)", "(msec)");
David Ahern49394a22016-11-16 15:06:29 +09001865
Namhyung Kim414e0502017-01-13 19:45:22 +09001866 if (sched->show_state)
1867 printf(" %5s", "");
1868
1869 printf("\n");
1870
David Ahern49394a22016-11-16 15:06:29 +09001871 /*
1872 * separator
1873 */
1874 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1875
David Aherna407b062016-11-16 15:06:33 +09001876 if (sched->show_cpu_visual)
1877 printf(" %.*s ", ncpus, graph_dotted_line);
1878
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001879 printf(" %.*s %.9s %.9s %.9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001880 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1881 graph_dotted_line);
1882
Namhyung Kim414e0502017-01-13 19:45:22 +09001883 if (sched->show_state)
1884 printf(" %.5s", graph_dotted_line);
1885
David Ahern49394a22016-11-16 15:06:29 +09001886 printf("\n");
1887}
1888
Namhyung Kim414e0502017-01-13 19:45:22 +09001889static char task_state_char(struct thread *thread, int state)
1890{
1891 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1892 unsigned bit = state ? ffs(state) : 0;
1893
1894 /* 'I' for idle */
1895 if (thread->tid == 0)
1896 return 'I';
1897
1898 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
1899}
1900
David Ahernfc1469f2016-11-16 15:06:31 +09001901static void timehist_print_sample(struct perf_sched *sched,
Brendan Gregg292c4a82017-03-14 01:56:29 +00001902 struct perf_evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09001903 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001904 struct addr_location *al,
David Ahern853b7402016-11-29 10:15:44 -07001905 struct thread *thread,
Namhyung Kim414e0502017-01-13 19:45:22 +09001906 u64 t, int state)
David Ahern49394a22016-11-16 15:06:29 +09001907{
1908 struct thread_runtime *tr = thread__priv(thread);
Brendan Gregg292c4a82017-03-14 01:56:29 +00001909 const char *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
1910 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
David Aherna407b062016-11-16 15:06:33 +09001911 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09001912 char tstr[64];
Brendan Gregg292c4a82017-03-14 01:56:29 +00001913 char nstr[30];
Namhyung Kim941bdea2017-01-13 19:45:21 +09001914 u64 wait_time;
David Ahern49394a22016-11-16 15:06:29 +09001915
David Ahern853b7402016-11-29 10:15:44 -07001916 timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
David Ahern49394a22016-11-16 15:06:29 +09001917 printf("%15s [%04d] ", tstr, sample->cpu);
1918
David Aherna407b062016-11-16 15:06:33 +09001919 if (sched->show_cpu_visual) {
1920 u32 i;
1921 char c;
1922
1923 printf(" ");
1924 for (i = 0; i < max_cpus; ++i) {
1925 /* flag idle times with 'i'; others are sched events */
1926 if (i == sample->cpu)
1927 c = (thread->tid == 0) ? 'i' : 's';
1928 else
1929 c = ' ';
1930 printf("%c", c);
1931 }
1932 printf(" ");
1933 }
1934
David Ahern49394a22016-11-16 15:06:29 +09001935 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1936
Namhyung Kim941bdea2017-01-13 19:45:21 +09001937 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
1938 print_sched_time(wait_time, 6);
1939
David Ahern49394a22016-11-16 15:06:29 +09001940 print_sched_time(tr->dt_delay, 6);
1941 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001942
Namhyung Kim414e0502017-01-13 19:45:22 +09001943 if (sched->show_state)
1944 printf(" %5c ", task_state_char(thread, state));
1945
Brendan Gregg292c4a82017-03-14 01:56:29 +00001946 if (sched->show_next) {
1947 snprintf(nstr, sizeof(nstr), "next: %s[%d]", next_comm, next_pid);
1948 printf(" %-*s", comm_width, nstr);
1949 }
1950
1951 if (sched->show_wakeups && !sched->show_next)
David Ahernfc1469f2016-11-16 15:06:31 +09001952 printf(" %-*s", comm_width, "");
1953
David Ahern6c973c92016-11-16 15:06:32 +09001954 if (thread->tid == 0)
1955 goto out;
1956
1957 if (sched->show_callchain)
1958 printf(" ");
1959
1960 sample__fprintf_sym(sample, al, 0,
1961 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09001962 EVSEL__PRINT_CALLCHAIN_ARROW |
1963 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09001964 &callchain_cursor, stdout);
1965
1966out:
David Ahern49394a22016-11-16 15:06:29 +09001967 printf("\n");
1968}
1969
1970/*
1971 * Explanation of delta-time stats:
1972 *
1973 * t = time of current schedule out event
1974 * tprev = time of previous sched out event
1975 * also time of schedule-in event for current task
1976 * last_time = time of last sched change event for current task
1977 * (i.e, time process was last scheduled out)
1978 * ready_to_run = time of wakeup for current task
1979 *
1980 * -----|------------|------------|------------|------
1981 * last ready tprev t
1982 * time to run
1983 *
1984 * |-------- dt_wait --------|
1985 * |- dt_delay -|-- dt_run --|
1986 *
1987 * dt_run = run time of current task
1988 * dt_wait = time between last schedule out event for task and tprev
1989 * represents time spent off the cpu
1990 * dt_delay = time between wakeup and schedule-in of task
1991 */
1992
1993static void timehist_update_runtime_stats(struct thread_runtime *r,
1994 u64 t, u64 tprev)
1995{
1996 r->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09001997 r->dt_sleep = 0;
1998 r->dt_iowait = 0;
1999 r->dt_preempt = 0;
David Ahern49394a22016-11-16 15:06:29 +09002000 r->dt_run = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002001
David Ahern49394a22016-11-16 15:06:29 +09002002 if (tprev) {
2003 r->dt_run = t - tprev;
2004 if (r->ready_to_run) {
2005 if (r->ready_to_run > tprev)
2006 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
2007 else
2008 r->dt_delay = tprev - r->ready_to_run;
2009 }
2010
2011 if (r->last_time > tprev)
2012 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
Namhyung Kim941bdea2017-01-13 19:45:21 +09002013 else if (r->last_time) {
2014 u64 dt_wait = tprev - r->last_time;
2015
2016 if (r->last_state == TASK_RUNNING)
2017 r->dt_preempt = dt_wait;
2018 else if (r->last_state == TASK_UNINTERRUPTIBLE)
2019 r->dt_iowait = dt_wait;
2020 else
2021 r->dt_sleep = dt_wait;
2022 }
David Ahern49394a22016-11-16 15:06:29 +09002023 }
2024
2025 update_stats(&r->run_stats, r->dt_run);
Namhyung Kim587782c2017-01-13 19:45:23 +09002026
2027 r->total_run_time += r->dt_run;
2028 r->total_delay_time += r->dt_delay;
2029 r->total_sleep_time += r->dt_sleep;
2030 r->total_iowait_time += r->dt_iowait;
2031 r->total_preempt_time += r->dt_preempt;
David Ahern49394a22016-11-16 15:06:29 +09002032}
2033
Namhyung Kim96039c72016-12-08 23:47:50 +09002034static bool is_idle_sample(struct perf_sample *sample,
2035 struct perf_evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09002036{
2037 /* pid 0 == swapper == idle task */
Namhyung Kim96039c72016-12-08 23:47:50 +09002038 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0)
2039 return perf_evsel__intval(evsel, sample, "prev_pid") == 0;
David Ahern49394a22016-11-16 15:06:29 +09002040
Namhyung Kim96039c72016-12-08 23:47:50 +09002041 return sample->pid == 0;
2042}
2043
2044static void save_task_callchain(struct perf_sched *sched,
2045 struct perf_sample *sample,
2046 struct perf_evsel *evsel,
2047 struct machine *machine)
2048{
2049 struct callchain_cursor *cursor = &callchain_cursor;
2050 struct thread *thread;
David Ahern6c973c92016-11-16 15:06:32 +09002051
2052 /* want main thread for process - has maps */
2053 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
2054 if (thread == NULL) {
2055 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
Namhyung Kim96039c72016-12-08 23:47:50 +09002056 return;
David Ahern6c973c92016-11-16 15:06:32 +09002057 }
2058
2059 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim96039c72016-12-08 23:47:50 +09002060 return;
David Ahern6c973c92016-11-16 15:06:32 +09002061
2062 if (thread__resolve_callchain(thread, cursor, evsel, sample,
Namhyung Kim8388deb2016-11-24 10:11:14 +09002063 NULL, NULL, sched->max_stack + 2) != 0) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002064 if (verbose > 0)
David Ahern6c973c92016-11-16 15:06:32 +09002065 error("Failed to resolve callchain. Skipping\n");
2066
Namhyung Kim96039c72016-12-08 23:47:50 +09002067 return;
David Ahern6c973c92016-11-16 15:06:32 +09002068 }
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002069
David Ahern6c973c92016-11-16 15:06:32 +09002070 callchain_cursor_commit(cursor);
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002071
2072 while (true) {
2073 struct callchain_cursor_node *node;
2074 struct symbol *sym;
2075
2076 node = callchain_cursor_current(cursor);
2077 if (node == NULL)
2078 break;
2079
2080 sym = node->sym;
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03002081 if (sym) {
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002082 if (!strcmp(sym->name, "schedule") ||
2083 !strcmp(sym->name, "__schedule") ||
2084 !strcmp(sym->name, "preempt_schedule"))
2085 sym->ignore = 1;
2086 }
2087
2088 callchain_cursor_advance(cursor);
2089 }
David Ahern49394a22016-11-16 15:06:29 +09002090}
2091
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002092static int init_idle_thread(struct thread *thread)
2093{
2094 struct idle_thread_runtime *itr;
2095
2096 thread__set_comm(thread, idle_comm, 0);
2097
2098 itr = zalloc(sizeof(*itr));
2099 if (itr == NULL)
2100 return -ENOMEM;
2101
2102 init_stats(&itr->tr.run_stats);
2103 callchain_init(&itr->callchain);
2104 callchain_cursor_reset(&itr->cursor);
2105 thread__set_priv(thread, itr);
2106
2107 return 0;
2108}
2109
David Ahern49394a22016-11-16 15:06:29 +09002110/*
2111 * Track idle stats per cpu by maintaining a local thread
2112 * struct for the idle task on each cpu.
2113 */
2114static int init_idle_threads(int ncpu)
2115{
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002116 int i, ret;
David Ahern49394a22016-11-16 15:06:29 +09002117
2118 idle_threads = zalloc(ncpu * sizeof(struct thread *));
2119 if (!idle_threads)
2120 return -ENOMEM;
2121
Namhyung Kimb3363522016-12-06 12:40:05 +09002122 idle_max_cpu = ncpu;
David Ahern49394a22016-11-16 15:06:29 +09002123
2124 /* allocate the actual thread struct if needed */
2125 for (i = 0; i < ncpu; ++i) {
2126 idle_threads[i] = thread__new(0, 0);
2127 if (idle_threads[i] == NULL)
2128 return -ENOMEM;
2129
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002130 ret = init_idle_thread(idle_threads[i]);
2131 if (ret < 0)
2132 return ret;
David Ahern49394a22016-11-16 15:06:29 +09002133 }
2134
2135 return 0;
2136}
2137
2138static void free_idle_threads(void)
2139{
2140 int i;
2141
2142 if (idle_threads == NULL)
2143 return;
2144
Namhyung Kimb3363522016-12-06 12:40:05 +09002145 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern49394a22016-11-16 15:06:29 +09002146 if ((idle_threads[i]))
2147 thread__delete(idle_threads[i]);
2148 }
2149
2150 free(idle_threads);
2151}
2152
2153static struct thread *get_idle_thread(int cpu)
2154{
2155 /*
2156 * expand/allocate array of pointers to local thread
2157 * structs if needed
2158 */
2159 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2160 int i, j = __roundup_pow_of_two(cpu+1);
2161 void *p;
2162
2163 p = realloc(idle_threads, j * sizeof(struct thread *));
2164 if (!p)
2165 return NULL;
2166
2167 idle_threads = (struct thread **) p;
Namhyung Kimb3363522016-12-06 12:40:05 +09002168 for (i = idle_max_cpu; i < j; ++i)
David Ahern49394a22016-11-16 15:06:29 +09002169 idle_threads[i] = NULL;
2170
2171 idle_max_cpu = j;
2172 }
2173
2174 /* allocate a new thread struct if needed */
2175 if (idle_threads[cpu] == NULL) {
2176 idle_threads[cpu] = thread__new(0, 0);
2177
2178 if (idle_threads[cpu]) {
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002179 if (init_idle_thread(idle_threads[cpu]) < 0)
2180 return NULL;
David Ahern49394a22016-11-16 15:06:29 +09002181 }
2182 }
2183
2184 return idle_threads[cpu];
2185}
2186
Namhyung Kim699b5b92016-12-08 23:47:52 +09002187static void save_idle_callchain(struct idle_thread_runtime *itr,
2188 struct perf_sample *sample)
2189{
2190 if (!symbol_conf.use_callchain || sample->callchain == NULL)
2191 return;
2192
2193 callchain_cursor__copy(&itr->cursor, &callchain_cursor);
2194}
2195
David Ahern49394a22016-11-16 15:06:29 +09002196/*
2197 * handle runtime stats saved per thread
2198 */
2199static struct thread_runtime *thread__init_runtime(struct thread *thread)
2200{
2201 struct thread_runtime *r;
2202
2203 r = zalloc(sizeof(struct thread_runtime));
2204 if (!r)
2205 return NULL;
2206
2207 init_stats(&r->run_stats);
2208 thread__set_priv(thread, r);
2209
2210 return r;
2211}
2212
2213static struct thread_runtime *thread__get_runtime(struct thread *thread)
2214{
2215 struct thread_runtime *tr;
2216
2217 tr = thread__priv(thread);
2218 if (tr == NULL) {
2219 tr = thread__init_runtime(thread);
2220 if (tr == NULL)
2221 pr_debug("Failed to malloc memory for runtime data.\n");
2222 }
2223
2224 return tr;
2225}
2226
David Ahern6c973c92016-11-16 15:06:32 +09002227static struct thread *timehist_get_thread(struct perf_sched *sched,
2228 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002229 struct machine *machine,
2230 struct perf_evsel *evsel)
2231{
2232 struct thread *thread;
2233
Namhyung Kim96039c72016-12-08 23:47:50 +09002234 if (is_idle_sample(sample, evsel)) {
David Ahern49394a22016-11-16 15:06:29 +09002235 thread = get_idle_thread(sample->cpu);
2236 if (thread == NULL)
2237 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2238
2239 } else {
Namhyung Kim5d92d962016-12-06 12:40:03 +09002240 /* there were samples with tid 0 but non-zero pid */
2241 thread = machine__findnew_thread(machine, sample->pid,
2242 sample->tid ?: sample->pid);
David Ahern49394a22016-11-16 15:06:29 +09002243 if (thread == NULL) {
2244 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2245 sample->tid);
2246 }
Namhyung Kim96039c72016-12-08 23:47:50 +09002247
2248 save_task_callchain(sched, sample, evsel, machine);
Namhyung Kim699b5b92016-12-08 23:47:52 +09002249 if (sched->idle_hist) {
2250 struct thread *idle;
2251 struct idle_thread_runtime *itr;
2252
2253 idle = get_idle_thread(sample->cpu);
2254 if (idle == NULL) {
2255 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2256 return NULL;
2257 }
2258
2259 itr = thread__priv(idle);
2260 if (itr == NULL)
2261 return NULL;
2262
2263 itr->last_thread = thread;
2264
2265 /* copy task callchain when entering to idle */
2266 if (perf_evsel__intval(evsel, sample, "next_pid") == 0)
2267 save_idle_callchain(itr, sample);
2268 }
David Ahern49394a22016-11-16 15:06:29 +09002269 }
2270
2271 return thread;
2272}
2273
David Ahern52df1382016-11-16 15:06:30 +09002274static bool timehist_skip_sample(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002275 struct thread *thread,
2276 struct perf_evsel *evsel,
2277 struct perf_sample *sample)
David Ahern49394a22016-11-16 15:06:29 +09002278{
2279 bool rc = false;
2280
David Ahern52df1382016-11-16 15:06:30 +09002281 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002282 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002283 sched->skipped_samples++;
2284 }
David Ahern49394a22016-11-16 15:06:29 +09002285
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002286 if (sched->idle_hist) {
2287 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch"))
2288 rc = true;
2289 else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 &&
2290 perf_evsel__intval(evsel, sample, "next_pid") != 0)
2291 rc = true;
2292 }
2293
David Ahern49394a22016-11-16 15:06:29 +09002294 return rc;
2295}
2296
David Ahernfc1469f2016-11-16 15:06:31 +09002297static void timehist_print_wakeup_event(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002298 struct perf_evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09002299 struct perf_sample *sample,
2300 struct machine *machine,
2301 struct thread *awakened)
2302{
2303 struct thread *thread;
2304 char tstr[64];
2305
2306 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2307 if (thread == NULL)
2308 return;
2309
2310 /* show wakeup unless both awakee and awaker are filtered */
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002311 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2312 timehist_skip_sample(sched, awakened, evsel, sample)) {
David Ahernfc1469f2016-11-16 15:06:31 +09002313 return;
2314 }
2315
2316 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2317 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002318 if (sched->show_cpu_visual)
2319 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002320
2321 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2322
2323 /* dt spacer */
2324 printf(" %9s %9s %9s ", "", "", "");
2325
2326 printf("awakened: %s", timehist_get_commstr(awakened));
2327
2328 printf("\n");
2329}
2330
2331static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002332 union perf_event *event __maybe_unused,
2333 struct perf_evsel *evsel,
2334 struct perf_sample *sample,
2335 struct machine *machine)
2336{
David Ahernfc1469f2016-11-16 15:06:31 +09002337 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002338 struct thread *thread;
2339 struct thread_runtime *tr = NULL;
2340 /* want pid of awakened task not pid in sample */
2341 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2342
2343 thread = machine__findnew_thread(machine, 0, pid);
2344 if (thread == NULL)
2345 return -1;
2346
2347 tr = thread__get_runtime(thread);
2348 if (tr == NULL)
2349 return -1;
2350
2351 if (tr->ready_to_run == 0)
2352 tr->ready_to_run = sample->time;
2353
David Ahernfc1469f2016-11-16 15:06:31 +09002354 /* show wakeups if requested */
David Ahern853b7402016-11-29 10:15:44 -07002355 if (sched->show_wakeups &&
2356 !perf_time__skip_sample(&sched->ptime, sample->time))
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002357 timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
David Ahernfc1469f2016-11-16 15:06:31 +09002358
David Ahern49394a22016-11-16 15:06:29 +09002359 return 0;
2360}
2361
David Ahern350f54f2016-11-25 09:28:41 -07002362static void timehist_print_migration_event(struct perf_sched *sched,
2363 struct perf_evsel *evsel,
2364 struct perf_sample *sample,
2365 struct machine *machine,
2366 struct thread *migrated)
2367{
2368 struct thread *thread;
2369 char tstr[64];
2370 u32 max_cpus = sched->max_cpu + 1;
2371 u32 ocpu, dcpu;
2372
2373 if (sched->summary_only)
2374 return;
2375
2376 max_cpus = sched->max_cpu + 1;
2377 ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
2378 dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
2379
2380 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2381 if (thread == NULL)
2382 return;
2383
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002384 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2385 timehist_skip_sample(sched, migrated, evsel, sample)) {
David Ahern350f54f2016-11-25 09:28:41 -07002386 return;
2387 }
2388
2389 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2390 printf("%15s [%04d] ", tstr, sample->cpu);
2391
2392 if (sched->show_cpu_visual) {
2393 u32 i;
2394 char c;
2395
2396 printf(" ");
2397 for (i = 0; i < max_cpus; ++i) {
2398 c = (i == sample->cpu) ? 'm' : ' ';
2399 printf("%c", c);
2400 }
2401 printf(" ");
2402 }
2403
2404 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2405
2406 /* dt spacer */
2407 printf(" %9s %9s %9s ", "", "", "");
2408
2409 printf("migrated: %s", timehist_get_commstr(migrated));
2410 printf(" cpu %d => %d", ocpu, dcpu);
2411
2412 printf("\n");
2413}
2414
2415static int timehist_migrate_task_event(struct perf_tool *tool,
2416 union perf_event *event __maybe_unused,
2417 struct perf_evsel *evsel,
2418 struct perf_sample *sample,
2419 struct machine *machine)
2420{
2421 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2422 struct thread *thread;
2423 struct thread_runtime *tr = NULL;
2424 /* want pid of migrated task not pid in sample */
2425 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2426
2427 thread = machine__findnew_thread(machine, 0, pid);
2428 if (thread == NULL)
2429 return -1;
2430
2431 tr = thread__get_runtime(thread);
2432 if (tr == NULL)
2433 return -1;
2434
2435 tr->migrations++;
2436
2437 /* show migrations if requested */
2438 timehist_print_migration_event(sched, evsel, sample, machine, thread);
2439
2440 return 0;
2441}
2442
David Ahern52df1382016-11-16 15:06:30 +09002443static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002444 union perf_event *event,
2445 struct perf_evsel *evsel,
2446 struct perf_sample *sample,
2447 struct machine *machine)
2448{
David Ahernfc1469f2016-11-16 15:06:31 +09002449 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern853b7402016-11-29 10:15:44 -07002450 struct perf_time_interval *ptime = &sched->ptime;
David Ahern49394a22016-11-16 15:06:29 +09002451 struct addr_location al;
2452 struct thread *thread;
2453 struct thread_runtime *tr = NULL;
David Ahern853b7402016-11-29 10:15:44 -07002454 u64 tprev, t = sample->time;
David Ahern49394a22016-11-16 15:06:29 +09002455 int rc = 0;
Namhyung Kim414e0502017-01-13 19:45:22 +09002456 int state = perf_evsel__intval(evsel, sample, "prev_state");
2457
David Ahern49394a22016-11-16 15:06:29 +09002458
2459 if (machine__resolve(machine, &al, sample) < 0) {
2460 pr_err("problem processing %d event. skipping it\n",
2461 event->header.type);
2462 rc = -1;
2463 goto out;
2464 }
2465
David Ahern6c973c92016-11-16 15:06:32 +09002466 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002467 if (thread == NULL) {
2468 rc = -1;
2469 goto out;
2470 }
2471
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002472 if (timehist_skip_sample(sched, thread, evsel, sample))
David Ahern49394a22016-11-16 15:06:29 +09002473 goto out;
2474
2475 tr = thread__get_runtime(thread);
2476 if (tr == NULL) {
2477 rc = -1;
2478 goto out;
2479 }
2480
2481 tprev = perf_evsel__get_time(evsel, sample->cpu);
2482
David Ahern853b7402016-11-29 10:15:44 -07002483 /*
2484 * If start time given:
2485 * - sample time is under window user cares about - skip sample
2486 * - tprev is under window user cares about - reset to start of window
2487 */
2488 if (ptime->start && ptime->start > t)
2489 goto out;
2490
Namhyung Kimbdd75722016-12-22 15:03:49 +09002491 if (tprev && ptime->start > tprev)
David Ahern853b7402016-11-29 10:15:44 -07002492 tprev = ptime->start;
2493
2494 /*
2495 * If end time given:
2496 * - previous sched event is out of window - we are done
2497 * - sample time is beyond window user cares about - reset it
2498 * to close out stats for time window interest
2499 */
2500 if (ptime->end) {
2501 if (tprev > ptime->end)
2502 goto out;
2503
2504 if (t > ptime->end)
2505 t = ptime->end;
2506 }
2507
Namhyung Kim07235f82016-12-08 23:47:54 +09002508 if (!sched->idle_hist || thread->tid == 0) {
2509 timehist_update_runtime_stats(tr, t, tprev);
2510
2511 if (sched->idle_hist) {
2512 struct idle_thread_runtime *itr = (void *)tr;
2513 struct thread_runtime *last_tr;
2514
2515 BUG_ON(thread->tid != 0);
2516
2517 if (itr->last_thread == NULL)
2518 goto out;
2519
2520 /* add current idle time as last thread's runtime */
2521 last_tr = thread__get_runtime(itr->last_thread);
2522 if (last_tr == NULL)
2523 goto out;
2524
2525 timehist_update_runtime_stats(last_tr, t, tprev);
2526 /*
2527 * remove delta time of last thread as it's not updated
2528 * and otherwise it will show an invalid value next
2529 * time. we only care total run time and run stat.
2530 */
2531 last_tr->dt_run = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002532 last_tr->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002533 last_tr->dt_sleep = 0;
2534 last_tr->dt_iowait = 0;
2535 last_tr->dt_preempt = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002536
Namhyung Kimba957eb2016-12-08 23:47:55 +09002537 if (itr->cursor.nr)
2538 callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2539
Namhyung Kim07235f82016-12-08 23:47:54 +09002540 itr->last_thread = NULL;
2541 }
2542 }
David Ahern853b7402016-11-29 10:15:44 -07002543
David Ahern52df1382016-11-16 15:06:30 +09002544 if (!sched->summary_only)
Brendan Gregg292c4a82017-03-14 01:56:29 +00002545 timehist_print_sample(sched, evsel, sample, &al, thread, t, state);
David Ahern49394a22016-11-16 15:06:29 +09002546
2547out:
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002548 if (sched->hist_time.start == 0 && t >= ptime->start)
2549 sched->hist_time.start = t;
2550 if (ptime->end == 0 || t <= ptime->end)
2551 sched->hist_time.end = t;
2552
David Ahern49394a22016-11-16 15:06:29 +09002553 if (tr) {
2554 /* time of this sched_switch event becomes last time task seen */
2555 tr->last_time = sample->time;
2556
Namhyung Kim941bdea2017-01-13 19:45:21 +09002557 /* last state is used to determine where to account wait time */
Namhyung Kim414e0502017-01-13 19:45:22 +09002558 tr->last_state = state;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002559
David Ahern49394a22016-11-16 15:06:29 +09002560 /* sched out event for task so reset ready to run time */
2561 tr->ready_to_run = 0;
2562 }
2563
2564 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2565
2566 return rc;
2567}
2568
2569static int timehist_sched_switch_event(struct perf_tool *tool,
2570 union perf_event *event,
2571 struct perf_evsel *evsel,
2572 struct perf_sample *sample,
2573 struct machine *machine __maybe_unused)
2574{
2575 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2576}
2577
2578static int process_lost(struct perf_tool *tool __maybe_unused,
2579 union perf_event *event,
2580 struct perf_sample *sample,
2581 struct machine *machine __maybe_unused)
2582{
2583 char tstr[64];
2584
2585 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2586 printf("%15s ", tstr);
2587 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2588
2589 return 0;
2590}
2591
2592
David Ahern52df1382016-11-16 15:06:30 +09002593static void print_thread_runtime(struct thread *t,
2594 struct thread_runtime *r)
2595{
2596 double mean = avg_stats(&r->run_stats);
2597 float stddev;
2598
2599 printf("%*s %5d %9" PRIu64 " ",
2600 comm_width, timehist_get_commstr(t), t->ppid,
2601 (u64) r->run_stats.n);
2602
2603 print_sched_time(r->total_run_time, 8);
2604 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2605 print_sched_time(r->run_stats.min, 6);
2606 printf(" ");
2607 print_sched_time((u64) mean, 6);
2608 printf(" ");
2609 print_sched_time(r->run_stats.max, 6);
2610 printf(" ");
2611 printf("%5.2f", stddev);
David Ahern350f54f2016-11-25 09:28:41 -07002612 printf(" %5" PRIu64, r->migrations);
David Ahern52df1382016-11-16 15:06:30 +09002613 printf("\n");
2614}
2615
Namhyung Kim587782c2017-01-13 19:45:23 +09002616static void print_thread_waittime(struct thread *t,
2617 struct thread_runtime *r)
2618{
2619 printf("%*s %5d %9" PRIu64 " ",
2620 comm_width, timehist_get_commstr(t), t->ppid,
2621 (u64) r->run_stats.n);
2622
2623 print_sched_time(r->total_run_time, 8);
2624 print_sched_time(r->total_sleep_time, 6);
2625 printf(" ");
2626 print_sched_time(r->total_iowait_time, 6);
2627 printf(" ");
2628 print_sched_time(r->total_preempt_time, 6);
2629 printf(" ");
2630 print_sched_time(r->total_delay_time, 6);
2631 printf("\n");
2632}
2633
David Ahern52df1382016-11-16 15:06:30 +09002634struct total_run_stats {
Namhyung Kim587782c2017-01-13 19:45:23 +09002635 struct perf_sched *sched;
David Ahern52df1382016-11-16 15:06:30 +09002636 u64 sched_count;
2637 u64 task_count;
2638 u64 total_run_time;
2639};
2640
2641static int __show_thread_runtime(struct thread *t, void *priv)
2642{
2643 struct total_run_stats *stats = priv;
2644 struct thread_runtime *r;
2645
2646 if (thread__is_filtered(t))
2647 return 0;
2648
2649 r = thread__priv(t);
2650 if (r && r->run_stats.n) {
2651 stats->task_count++;
2652 stats->sched_count += r->run_stats.n;
2653 stats->total_run_time += r->total_run_time;
Namhyung Kim587782c2017-01-13 19:45:23 +09002654
2655 if (stats->sched->show_state)
2656 print_thread_waittime(t, r);
2657 else
2658 print_thread_runtime(t, r);
David Ahern52df1382016-11-16 15:06:30 +09002659 }
2660
2661 return 0;
2662}
2663
2664static int show_thread_runtime(struct thread *t, void *priv)
2665{
2666 if (t->dead)
2667 return 0;
2668
2669 return __show_thread_runtime(t, priv);
2670}
2671
2672static int show_deadthread_runtime(struct thread *t, void *priv)
2673{
2674 if (!t->dead)
2675 return 0;
2676
2677 return __show_thread_runtime(t, priv);
2678}
2679
Namhyung Kimba957eb2016-12-08 23:47:55 +09002680static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2681{
2682 const char *sep = " <- ";
2683 struct callchain_list *chain;
2684 size_t ret = 0;
2685 char bf[1024];
2686 bool first;
2687
2688 if (node == NULL)
2689 return 0;
2690
2691 ret = callchain__fprintf_folded(fp, node->parent);
2692 first = (ret == 0);
2693
2694 list_for_each_entry(chain, &node->val, list) {
2695 if (chain->ip >= PERF_CONTEXT_MAX)
2696 continue;
2697 if (chain->ms.sym && chain->ms.sym->ignore)
2698 continue;
2699 ret += fprintf(fp, "%s%s", first ? "" : sep,
2700 callchain_list__sym_name(chain, bf, sizeof(bf),
2701 false));
2702 first = false;
2703 }
2704
2705 return ret;
2706}
2707
2708static size_t timehist_print_idlehist_callchain(struct rb_root *root)
2709{
2710 size_t ret = 0;
2711 FILE *fp = stdout;
2712 struct callchain_node *chain;
2713 struct rb_node *rb_node = rb_first(root);
2714
2715 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
2716 printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
2717 graph_dotted_line);
2718
2719 while (rb_node) {
2720 chain = rb_entry(rb_node, struct callchain_node, rb_node);
2721 rb_node = rb_next(rb_node);
2722
2723 ret += fprintf(fp, " ");
2724 print_sched_time(chain->hit, 12);
2725 ret += 16; /* print_sched_time returns 2nd arg + 4 */
2726 ret += fprintf(fp, " %8d ", chain->count);
2727 ret += callchain__fprintf_folded(fp, chain);
2728 ret += fprintf(fp, "\n");
2729 }
2730
2731 return ret;
2732}
2733
David Ahern52df1382016-11-16 15:06:30 +09002734static void timehist_print_summary(struct perf_sched *sched,
2735 struct perf_session *session)
2736{
2737 struct machine *m = &session->machines.host;
2738 struct total_run_stats totals;
2739 u64 task_count;
2740 struct thread *t;
2741 struct thread_runtime *r;
2742 int i;
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002743 u64 hist_time = sched->hist_time.end - sched->hist_time.start;
David Ahern52df1382016-11-16 15:06:30 +09002744
2745 memset(&totals, 0, sizeof(totals));
Namhyung Kim587782c2017-01-13 19:45:23 +09002746 totals.sched = sched;
David Ahern52df1382016-11-16 15:06:30 +09002747
Namhyung Kim07235f82016-12-08 23:47:54 +09002748 if (sched->idle_hist) {
2749 printf("\nIdle-time summary\n");
2750 printf("%*s parent sched-out ", comm_width, "comm");
2751 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
Namhyung Kim587782c2017-01-13 19:45:23 +09002752 } else if (sched->show_state) {
2753 printf("\nWait-time summary\n");
2754 printf("%*s parent sched-in ", comm_width, "comm");
2755 printf(" run-time sleep iowait preempt delay\n");
Namhyung Kim07235f82016-12-08 23:47:54 +09002756 } else {
2757 printf("\nRuntime summary\n");
2758 printf("%*s parent sched-in ", comm_width, "comm");
2759 printf(" run-time min-run avg-run max-run stddev migrations\n");
2760 }
David Ahern52df1382016-11-16 15:06:30 +09002761 printf("%*s (count) ", comm_width, "");
Namhyung Kim587782c2017-01-13 19:45:23 +09002762 printf(" (msec) (msec) (msec) (msec) %s\n",
2763 sched->show_state ? "(msec)" : "%");
David Ahern350f54f2016-11-25 09:28:41 -07002764 printf("%.117s\n", graph_dotted_line);
David Ahern52df1382016-11-16 15:06:30 +09002765
2766 machine__for_each_thread(m, show_thread_runtime, &totals);
2767 task_count = totals.task_count;
2768 if (!task_count)
2769 printf("<no still running tasks>\n");
2770
2771 printf("\nTerminated tasks:\n");
2772 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2773 if (task_count == totals.task_count)
2774 printf("<no terminated tasks>\n");
2775
2776 /* CPU idle stats not tracked when samples were skipped */
Namhyung Kim07235f82016-12-08 23:47:54 +09002777 if (sched->skipped_samples && !sched->idle_hist)
David Ahern52df1382016-11-16 15:06:30 +09002778 return;
2779
2780 printf("\nIdle stats:\n");
Namhyung Kimb3363522016-12-06 12:40:05 +09002781 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern52df1382016-11-16 15:06:30 +09002782 t = idle_threads[i];
2783 if (!t)
2784 continue;
2785
2786 r = thread__priv(t);
2787 if (r && r->run_stats.n) {
2788 totals.sched_count += r->run_stats.n;
2789 printf(" CPU %2d idle for ", i);
2790 print_sched_time(r->total_run_time, 6);
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002791 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
David Ahern52df1382016-11-16 15:06:30 +09002792 } else
2793 printf(" CPU %2d idle entire time window\n", i);
2794 }
2795
Namhyung Kimba957eb2016-12-08 23:47:55 +09002796 if (sched->idle_hist && symbol_conf.use_callchain) {
2797 callchain_param.mode = CHAIN_FOLDED;
2798 callchain_param.value = CCVAL_PERIOD;
2799
2800 callchain_register_param(&callchain_param);
2801
2802 printf("\nIdle stats by callchain:\n");
2803 for (i = 0; i < idle_max_cpu; ++i) {
2804 struct idle_thread_runtime *itr;
2805
2806 t = idle_threads[i];
2807 if (!t)
2808 continue;
2809
2810 itr = thread__priv(t);
2811 if (itr == NULL)
2812 continue;
2813
2814 callchain_param.sort(&itr->sorted_root, &itr->callchain,
2815 0, &callchain_param);
2816
2817 printf(" CPU %2d:", i);
2818 print_sched_time(itr->tr.total_run_time, 6);
2819 printf(" msec\n");
2820 timehist_print_idlehist_callchain(&itr->sorted_root);
2821 printf("\n");
2822 }
2823 }
2824
David Ahern52df1382016-11-16 15:06:30 +09002825 printf("\n"
2826 " Total number of unique tasks: %" PRIu64 "\n"
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002827 "Total number of context switches: %" PRIu64 "\n",
David Ahern52df1382016-11-16 15:06:30 +09002828 totals.task_count, totals.sched_count);
2829
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002830 printf(" Total run time (msec): ");
David Ahern52df1382016-11-16 15:06:30 +09002831 print_sched_time(totals.total_run_time, 2);
2832 printf("\n");
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002833
2834 printf(" Total scheduling time (msec): ");
2835 print_sched_time(hist_time, 2);
2836 printf(" (x %d)\n", sched->max_cpu);
David Ahern52df1382016-11-16 15:06:30 +09002837}
2838
David Ahern49394a22016-11-16 15:06:29 +09002839typedef int (*sched_handler)(struct perf_tool *tool,
2840 union perf_event *event,
2841 struct perf_evsel *evsel,
2842 struct perf_sample *sample,
2843 struct machine *machine);
2844
2845static int perf_timehist__process_sample(struct perf_tool *tool,
2846 union perf_event *event,
2847 struct perf_sample *sample,
2848 struct perf_evsel *evsel,
2849 struct machine *machine)
2850{
2851 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2852 int err = 0;
2853 int this_cpu = sample->cpu;
2854
2855 if (this_cpu > sched->max_cpu)
2856 sched->max_cpu = this_cpu;
2857
2858 if (evsel->handler != NULL) {
2859 sched_handler f = evsel->handler;
2860
2861 err = f(tool, event, evsel, sample, machine);
2862 }
2863
2864 return err;
2865}
2866
David Ahern6c973c92016-11-16 15:06:32 +09002867static int timehist_check_attr(struct perf_sched *sched,
2868 struct perf_evlist *evlist)
2869{
2870 struct perf_evsel *evsel;
2871 struct evsel_runtime *er;
2872
2873 list_for_each_entry(evsel, &evlist->entries, node) {
2874 er = perf_evsel__get_runtime(evsel);
2875 if (er == NULL) {
2876 pr_err("Failed to allocate memory for evsel runtime data\n");
2877 return -1;
2878 }
2879
2880 if (sched->show_callchain &&
2881 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2882 pr_info("Samples do not have callchains.\n");
2883 sched->show_callchain = 0;
2884 symbol_conf.use_callchain = 0;
2885 }
2886 }
2887
2888 return 0;
2889}
2890
David Ahern49394a22016-11-16 15:06:29 +09002891static int perf_sched__timehist(struct perf_sched *sched)
2892{
2893 const struct perf_evsel_str_handler handlers[] = {
2894 { "sched:sched_switch", timehist_sched_switch_event, },
2895 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2896 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2897 };
David Ahern350f54f2016-11-25 09:28:41 -07002898 const struct perf_evsel_str_handler migrate_handlers[] = {
2899 { "sched:sched_migrate_task", timehist_migrate_task_event, },
2900 };
David Ahern49394a22016-11-16 15:06:29 +09002901 struct perf_data_file file = {
2902 .path = input_name,
2903 .mode = PERF_DATA_MODE_READ,
Namhyung Kim6fa94252016-12-06 12:40:01 +09002904 .force = sched->force,
David Ahern49394a22016-11-16 15:06:29 +09002905 };
2906
2907 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002908 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002909 int err = -1;
2910
2911 /*
2912 * event handlers for timehist option
2913 */
2914 sched->tool.sample = perf_timehist__process_sample;
2915 sched->tool.mmap = perf_event__process_mmap;
2916 sched->tool.comm = perf_event__process_comm;
2917 sched->tool.exit = perf_event__process_exit;
2918 sched->tool.fork = perf_event__process_fork;
2919 sched->tool.lost = process_lost;
2920 sched->tool.attr = perf_event__process_attr;
2921 sched->tool.tracing_data = perf_event__process_tracing_data;
2922 sched->tool.build_id = perf_event__process_build_id;
2923
2924 sched->tool.ordered_events = true;
2925 sched->tool.ordering_requires_timestamps = true;
2926
David Ahern6c973c92016-11-16 15:06:32 +09002927 symbol_conf.use_callchain = sched->show_callchain;
2928
David Ahern49394a22016-11-16 15:06:29 +09002929 session = perf_session__new(&file, false, &sched->tool);
2930 if (session == NULL)
2931 return -ENOMEM;
2932
David Ahern52df1382016-11-16 15:06:30 +09002933 evlist = session->evlist;
2934
David Ahern49394a22016-11-16 15:06:29 +09002935 symbol__init(&session->header.env);
2936
David Ahern853b7402016-11-29 10:15:44 -07002937 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
2938 pr_err("Invalid time string\n");
2939 return -EINVAL;
2940 }
2941
David Ahern6c973c92016-11-16 15:06:32 +09002942 if (timehist_check_attr(sched, evlist) != 0)
2943 goto out;
2944
David Ahern49394a22016-11-16 15:06:29 +09002945 setup_pager();
2946
2947 /* setup per-evsel handlers */
2948 if (perf_session__set_tracepoints_handlers(session, handlers))
2949 goto out;
2950
David Ahernf45bf8d2016-11-29 13:39:48 -07002951 /* sched_switch event at a minimum needs to exist */
2952 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
2953 "sched:sched_switch")) {
2954 pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
David Ahern49394a22016-11-16 15:06:29 +09002955 goto out;
David Ahernf45bf8d2016-11-29 13:39:48 -07002956 }
David Ahern49394a22016-11-16 15:06:29 +09002957
David Ahern350f54f2016-11-25 09:28:41 -07002958 if (sched->show_migrations &&
2959 perf_session__set_tracepoints_handlers(session, migrate_handlers))
2960 goto out;
2961
David Ahern49394a22016-11-16 15:06:29 +09002962 /* pre-allocate struct for per-CPU idle stats */
2963 sched->max_cpu = session->header.env.nr_cpus_online;
2964 if (sched->max_cpu == 0)
2965 sched->max_cpu = 4;
2966 if (init_idle_threads(sched->max_cpu))
2967 goto out;
2968
David Ahern52df1382016-11-16 15:06:30 +09002969 /* summary_only implies summary option, but don't overwrite summary if set */
2970 if (sched->summary_only)
2971 sched->summary = sched->summary_only;
2972
2973 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09002974 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09002975
2976 err = perf_session__process_events(session);
2977 if (err) {
2978 pr_err("Failed to process events, error %d", err);
2979 goto out;
2980 }
2981
David Ahern52df1382016-11-16 15:06:30 +09002982 sched->nr_events = evlist->stats.nr_events[0];
2983 sched->nr_lost_events = evlist->stats.total_lost;
2984 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2985
2986 if (sched->summary)
2987 timehist_print_summary(sched, session);
2988
David Ahern49394a22016-11-16 15:06:29 +09002989out:
2990 free_idle_threads();
2991 perf_session__delete(session);
2992
2993 return err;
2994}
2995
2996
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002997static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002998{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002999 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003000 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003001 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
3002 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003003 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003004 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003005 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003006 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
3007 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003008 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003009 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003010 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003011 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
3012 sched->nr_context_switch_bugs, sched->nr_timestamps);
3013 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003014 printf(" (due to lost events?)");
3015 printf("\n");
3016 }
3017}
3018
Josef Bacik2f80dd42015-05-22 09:18:40 -04003019static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
3020{
3021 struct rb_node **new = &(root->rb_node), *parent = NULL;
3022 struct work_atoms *this;
3023 const char *comm = thread__comm_str(data->thread), *this_comm;
3024
3025 while (*new) {
3026 int cmp;
3027
3028 this = container_of(*new, struct work_atoms, node);
3029 parent = *new;
3030
3031 this_comm = thread__comm_str(this->thread);
3032 cmp = strcmp(comm, this_comm);
3033 if (cmp > 0) {
3034 new = &((*new)->rb_left);
3035 } else if (cmp < 0) {
3036 new = &((*new)->rb_right);
3037 } else {
3038 this->num_merged++;
3039 this->total_runtime += data->total_runtime;
3040 this->nb_atoms += data->nb_atoms;
3041 this->total_lat += data->total_lat;
3042 list_splice(&data->work_list, &this->work_list);
3043 if (this->max_lat < data->max_lat) {
3044 this->max_lat = data->max_lat;
3045 this->max_lat_at = data->max_lat_at;
3046 }
3047 zfree(&data);
3048 return;
3049 }
3050 }
3051
3052 data->num_merged++;
3053 rb_link_node(&data->node, parent, new);
3054 rb_insert_color(&data->node, root);
3055}
3056
3057static void perf_sched__merge_lat(struct perf_sched *sched)
3058{
3059 struct work_atoms *data;
3060 struct rb_node *node;
3061
3062 if (sched->skip_merge)
3063 return;
3064
3065 while ((node = rb_first(&sched->atom_root))) {
3066 rb_erase(node, &sched->atom_root);
3067 data = rb_entry(node, struct work_atoms, node);
3068 __merge_work_atoms(&sched->merged_atom_root, data);
3069 }
3070}
3071
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003072static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003073{
3074 struct rb_node *next;
3075
3076 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04003077
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003078 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003079 return -1;
David Ahernad9def72013-08-07 22:50:44 -04003080
Josef Bacik2f80dd42015-05-22 09:18:40 -04003081 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003082 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003083
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003084 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
3085 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
3086 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003087
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003088 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003089
3090 while (next) {
3091 struct work_atoms *work_list;
3092
3093 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003094 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003095 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003096 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003097 }
3098
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003099 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003100 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03003101 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003102
3103 printf(" ---------------------------------------------------\n");
3104
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003105 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003106 printf("\n");
3107
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003108 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003109}
3110
Jiri Olsa99623c62016-04-12 15:29:26 +02003111static int setup_map_cpus(struct perf_sched *sched)
3112{
Jiri Olsa73643bb2016-04-12 15:29:31 +02003113 struct cpu_map *map;
3114
Jiri Olsa99623c62016-04-12 15:29:26 +02003115 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
3116
3117 if (sched->map.comp) {
3118 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02003119 if (!sched->map.comp_cpus)
3120 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02003121 }
3122
Jiri Olsa73643bb2016-04-12 15:29:31 +02003123 if (!sched->map.cpus_str)
3124 return 0;
3125
3126 map = cpu_map__new(sched->map.cpus_str);
3127 if (!map) {
3128 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3129 return -1;
3130 }
3131
3132 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02003133 return 0;
3134}
3135
Jiri Olsaa151a372016-04-12 15:29:29 +02003136static int setup_color_pids(struct perf_sched *sched)
3137{
3138 struct thread_map *map;
3139
3140 if (!sched->map.color_pids_str)
3141 return 0;
3142
3143 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3144 if (!map) {
3145 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3146 return -1;
3147 }
3148
3149 sched->map.color_pids = map;
3150 return 0;
3151}
3152
Jiri Olsacf294f22016-04-12 15:29:30 +02003153static int setup_color_cpus(struct perf_sched *sched)
3154{
3155 struct cpu_map *map;
3156
3157 if (!sched->map.color_cpus_str)
3158 return 0;
3159
3160 map = cpu_map__new(sched->map.color_cpus_str);
3161 if (!map) {
3162 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3163 return -1;
3164 }
3165
3166 sched->map.color_cpus = map;
3167 return 0;
3168}
3169
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003170static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003171{
Jiri Olsa99623c62016-04-12 15:29:26 +02003172 if (setup_map_cpus(sched))
3173 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02003174
Jiri Olsaa151a372016-04-12 15:29:29 +02003175 if (setup_color_pids(sched))
3176 return -1;
3177
Jiri Olsacf294f22016-04-12 15:29:30 +02003178 if (setup_color_cpus(sched))
3179 return -1;
3180
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003181 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003182 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003183 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003184 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003185 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003186}
3187
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003188static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003189{
3190 unsigned long i;
3191
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003192 calibrate_run_measurement_overhead(sched);
3193 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003194
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003195 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003196
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003197 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003198 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003199
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003200 printf("nr_run_events: %ld\n", sched->nr_run_events);
3201 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
3202 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003203
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003204 if (sched->targetless_wakeups)
3205 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
3206 if (sched->multitarget_wakeups)
3207 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
3208 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003209 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003210 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003211
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003212 print_task_traces(sched);
3213 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003214
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003215 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003216 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003217 for (i = 0; i < sched->replay_repeat; i++)
3218 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003219
3220 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003221}
3222
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003223static void setup_sorting(struct perf_sched *sched, const struct option *options,
3224 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003225{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003226 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003227
3228 for (tok = strtok_r(str, ", ", &tmp);
3229 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003230 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003231 usage_with_options_msg(usage_msg, options,
3232 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003233 }
3234 }
3235
3236 free(str);
3237
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003238 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003239}
3240
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003241static int __cmd_record(int argc, const char **argv)
3242{
3243 unsigned int rec_argc, i, j;
3244 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003245 const char * const record_args[] = {
3246 "record",
3247 "-a",
3248 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003249 "-m", "1024",
3250 "-c", "1",
3251 "-e", "sched:sched_switch",
3252 "-e", "sched:sched_stat_wait",
3253 "-e", "sched:sched_stat_sleep",
3254 "-e", "sched:sched_stat_iowait",
3255 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003256 "-e", "sched:sched_process_fork",
3257 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09003258 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003259 "-e", "sched:sched_migrate_task",
3260 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003261
3262 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
3263 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3264
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02003265 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11003266 return -ENOMEM;
3267
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003268 for (i = 0; i < ARRAY_SIZE(record_args); i++)
3269 rec_argv[i] = strdup(record_args[i]);
3270
3271 for (j = 1; j < (unsigned int)argc; j++, i++)
3272 rec_argv[i] = argv[j];
3273
3274 BUG_ON(i != rec_argc);
3275
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003276 return cmd_record(i, rec_argv);
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003277}
3278
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003279int cmd_sched(int argc, const char **argv)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003280{
Adrian Hunter8a39df82013-10-22 10:34:15 +03003281 const char default_sort_order[] = "avg, max, switch, runtime";
3282 struct perf_sched sched = {
3283 .tool = {
3284 .sample = perf_sched__process_tracepoint_sample,
3285 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303286 .namespaces = perf_event__process_namespaces,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003287 .lost = perf_event__process_lost,
3288 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003289 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003290 },
3291 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
3292 .sort_list = LIST_HEAD_INIT(sched.sort_list),
3293 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
3294 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003295 .sort_order = default_sort_order,
3296 .replay_repeat = 10,
3297 .profile_cpu = -1,
3298 .next_shortname1 = 'A',
3299 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04003300 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09003301 .show_callchain = 1,
3302 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003303 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09003304 const struct option sched_options[] = {
3305 OPT_STRING('i', "input", &input_name, "file",
3306 "input file name"),
3307 OPT_INCR('v', "verbose", &verbose,
3308 "be more verbose (show symbol address, etc)"),
3309 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3310 "dump raw trace in ASCII"),
Namhyung Kim6fa94252016-12-06 12:40:01 +09003311 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003312 OPT_END()
3313 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003314 const struct option latency_options[] = {
3315 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
3316 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003317 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
3318 "CPU to profile on"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04003319 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
3320 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003321 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003322 };
3323 const struct option replay_options[] = {
3324 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
3325 "repeat the workload replay N times (-1: infinite)"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003326 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003327 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003328 const struct option map_options[] = {
3329 OPT_BOOLEAN(0, "compact", &sched.map.comp,
3330 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02003331 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3332 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02003333 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3334 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02003335 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
3336 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003337 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02003338 };
David Ahern49394a22016-11-16 15:06:29 +09003339 const struct option timehist_options[] = {
3340 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3341 "file", "vmlinux pathname"),
3342 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3343 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09003344 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
3345 "Display call chains if present (default on)"),
3346 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
3347 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09003348 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
3349 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09003350 OPT_BOOLEAN('s', "summary", &sched.summary_only,
3351 "Show only syscall summary with statistics"),
3352 OPT_BOOLEAN('S', "with-summary", &sched.summary,
3353 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09003354 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
Brendan Gregg292c4a82017-03-14 01:56:29 +00003355 OPT_BOOLEAN('n', "next", &sched.show_next, "Show next task"),
David Ahern350f54f2016-11-25 09:28:41 -07003356 OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
David Aherna407b062016-11-16 15:06:33 +09003357 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
Namhyung Kim07235f82016-12-08 23:47:54 +09003358 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
David Ahern853b7402016-11-29 10:15:44 -07003359 OPT_STRING(0, "time", &sched.time_str, "str",
3360 "Time span for analysis (start,stop)"),
Namhyung Kim414e0502017-01-13 19:45:22 +09003361 OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"),
David Ahern49394a22016-11-16 15:06:29 +09003362 OPT_PARENT(sched_options)
3363 };
3364
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003365 const char * const latency_usage[] = {
3366 "perf sched latency [<options>]",
3367 NULL
3368 };
3369 const char * const replay_usage[] = {
3370 "perf sched replay [<options>]",
3371 NULL
3372 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003373 const char * const map_usage[] = {
3374 "perf sched map [<options>]",
3375 NULL
3376 };
David Ahern49394a22016-11-16 15:06:29 +09003377 const char * const timehist_usage[] = {
3378 "perf sched timehist [<options>]",
3379 NULL
3380 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003381 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09003382 "replay", "script",
3383 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003384 const char *sched_usage[] = {
3385 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003386 NULL
3387 };
3388 struct trace_sched_handler lat_ops = {
3389 .wakeup_event = latency_wakeup_event,
3390 .switch_event = latency_switch_event,
3391 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003392 .migrate_task_event = latency_migrate_task_event,
3393 };
3394 struct trace_sched_handler map_ops = {
3395 .switch_event = map_switch_event,
3396 };
3397 struct trace_sched_handler replay_ops = {
3398 .wakeup_event = replay_wakeup_event,
3399 .switch_event = replay_switch_event,
3400 .fork_event = replay_fork_event,
3401 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03003402 unsigned int i;
3403
3404 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3405 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003406
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003407 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3408 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003409 if (!argc)
3410 usage_with_options(sched_usage, sched_options);
3411
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003412 /*
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003413 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003414 */
Ingo Molnar133dc4c32010-11-16 18:45:39 +01003415 if (!strcmp(argv[0], "script"))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003416 return cmd_script(argc, argv);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003417
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003418 if (!strncmp(argv[0], "rec", 3)) {
3419 return __cmd_record(argc, argv);
3420 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003421 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003422 if (argc > 1) {
3423 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3424 if (argc)
3425 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003426 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003427 setup_sorting(&sched, latency_options, latency_usage);
3428 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003429 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02003430 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02003431 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02003432 if (argc)
3433 usage_with_options(map_usage, map_options);
3434 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003435 sched.tp_handler = &map_ops;
3436 setup_sorting(&sched, latency_options, latency_usage);
3437 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003438 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003439 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003440 if (argc) {
3441 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3442 if (argc)
3443 usage_with_options(replay_usage, replay_options);
3444 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003445 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09003446 } else if (!strcmp(argv[0], "timehist")) {
3447 if (argc) {
3448 argc = parse_options(argc, argv, timehist_options,
3449 timehist_usage, 0);
3450 if (argc)
3451 usage_with_options(timehist_usage, timehist_options);
3452 }
Brendan Gregg292c4a82017-03-14 01:56:29 +00003453 if ((sched.show_wakeups || sched.show_next) &&
3454 sched.summary_only) {
3455 pr_err(" Error: -s and -[n|w] are mutually exclusive.\n");
David Ahernfc1469f2016-11-16 15:06:31 +09003456 parse_options_usage(timehist_usage, timehist_options, "s", true);
Brendan Gregg292c4a82017-03-14 01:56:29 +00003457 if (sched.show_wakeups)
3458 parse_options_usage(NULL, timehist_options, "w", true);
3459 if (sched.show_next)
3460 parse_options_usage(NULL, timehist_options, "n", true);
David Ahernfc1469f2016-11-16 15:06:31 +09003461 return -EINVAL;
3462 }
3463
David Ahern49394a22016-11-16 15:06:29 +09003464 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003465 } else {
3466 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003467 }
3468
Ingo Molnarec156762009-09-11 12:12:54 +02003469 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003470}