blob: 4f9e7cba4ebfd0015d776a0b4102e1af14ece60d [file] [log] [blame]
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001#include "builtin.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02002#include "perf.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003
4#include "util/util.h"
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02005#include "util/evlist.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02006#include "util/cache.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02007#include "util/evsel.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02008#include "util/symbol.h"
9#include "util/thread.h"
10#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Yann Droneaud57480d22014-06-30 22:28:47 +020013#include "util/cloexec.h"
Jiri Olsaa151a372016-04-12 15:29:29 +020014#include "util/thread_map.h"
Jiri Olsa8cd91192016-04-12 15:29:27 +020015#include "util/color.h"
David Ahern49394a22016-11-16 15:06:29 +090016#include "util/stat.h"
David Ahern6c973c92016-11-16 15:06:32 +090017#include "util/callchain.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020018
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020020#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021
Ingo Molnar0a02ad92009-09-11 12:12:54 +020022#include "util/debug.h"
23
David Ahern49394a22016-11-16 15:06:29 +090024#include <linux/log2.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020025#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020026#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020027
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020028#include <semaphore.h>
29#include <pthread.h>
30#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080031#include <api/fs/fs.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030032#include <linux/time64.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020033
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020034#define PR_SET_NAME 15 /* Set process name */
35#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020036#define COMM_LEN 20
37#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080038#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020039
mingo39aeb522009-09-14 20:04:48 +020040struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020041
42struct task_desc {
43 unsigned long nr;
44 unsigned long pid;
45 char comm[COMM_LEN];
46
47 unsigned long nr_events;
48 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020049 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020050
51 pthread_t thread;
52 sem_t sleep_sem;
53
54 sem_t ready_for_work;
55 sem_t work_done_sem;
56
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020057 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020058};
59
60enum sched_event_type {
61 SCHED_EVENT_RUN,
62 SCHED_EVENT_SLEEP,
63 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020064 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020065};
66
mingo39aeb522009-09-14 20:04:48 +020067struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020068 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030069 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020070 u64 timestamp;
71 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020072 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020073 sem_t *wait_sem;
74 struct task_desc *wakee;
75};
76
Dongshenge936e8e2014-05-05 16:05:54 +090077#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020078
79enum thread_state {
80 THREAD_SLEEPING = 0,
81 THREAD_WAIT_CPU,
82 THREAD_SCHED_IN,
83 THREAD_IGNORE
84};
85
86struct work_atom {
87 struct list_head list;
88 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020089 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020090 u64 wake_up_time;
91 u64 sched_in_time;
92 u64 runtime;
93};
94
mingo39aeb522009-09-14 20:04:48 +020095struct work_atoms {
96 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020097 struct thread *thread;
98 struct rb_node node;
99 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100100 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200101 u64 total_lat;
102 u64 nb_atoms;
103 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400104 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200105};
106
mingo39aeb522009-09-14 20:04:48 +0200107typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200108
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300109struct perf_sched;
110
111struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300112 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
113 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300114
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300115 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
116 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300117
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300118 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
119 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300120
David Aherncb627502013-08-07 22:50:47 -0400121 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
122 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
123 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300124
125 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300126 struct perf_evsel *evsel,
127 struct perf_sample *sample,
128 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300129};
130
Jiri Olsaa151a372016-04-12 15:29:29 +0200131#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200132#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200133
Jiri Olsa99623c62016-04-12 15:29:26 +0200134struct perf_sched_map {
135 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
136 int *comp_cpus;
137 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200138 struct thread_map *color_pids;
139 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200140 struct cpu_map *color_cpus;
141 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200142 struct cpu_map *cpus;
143 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200144};
145
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300146struct perf_sched {
147 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300148 const char *sort_order;
149 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800150 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300151 struct task_desc **tasks;
152 const struct trace_sched_handler *tp_handler;
153 pthread_mutex_t start_work_mutex;
154 pthread_mutex_t work_done_wait_mutex;
155 int profile_cpu;
156/*
157 * Track the current task - that way we can know whether there's any
158 * weird events, such as a task being switched away that is not current.
159 */
160 int max_cpu;
161 u32 curr_pid[MAX_CPUS];
162 struct thread *curr_thread[MAX_CPUS];
163 char next_shortname1;
164 char next_shortname2;
165 unsigned int replay_repeat;
166 unsigned long nr_run_events;
167 unsigned long nr_sleep_events;
168 unsigned long nr_wakeup_events;
169 unsigned long nr_sleep_corrections;
170 unsigned long nr_run_events_optimized;
171 unsigned long targetless_wakeups;
172 unsigned long multitarget_wakeups;
173 unsigned long nr_runs;
174 unsigned long nr_timestamps;
175 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300176 unsigned long nr_context_switch_bugs;
177 unsigned long nr_events;
178 unsigned long nr_lost_chunks;
179 unsigned long nr_lost_events;
180 u64 run_measurement_overhead;
181 u64 sleep_measurement_overhead;
182 u64 start_time;
183 u64 cpu_usage;
184 u64 runavg_cpu_usage;
185 u64 parent_cpu_usage;
186 u64 runavg_parent_cpu_usage;
187 u64 sum_runtime;
188 u64 sum_fluct;
189 u64 run_avg;
190 u64 all_runtime;
191 u64 all_count;
192 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400193 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300194 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800195 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400196 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200197 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900198
199 /* options for timehist command */
200 bool summary;
201 bool summary_only;
David Ahern6c973c92016-11-16 15:06:32 +0900202 bool show_callchain;
203 unsigned int max_stack;
David Aherna407b062016-11-16 15:06:33 +0900204 bool show_cpu_visual;
David Ahernfc1469f2016-11-16 15:06:31 +0900205 bool show_wakeups;
David Ahern350f54f2016-11-25 09:28:41 -0700206 bool show_migrations;
David Ahern52df1382016-11-16 15:06:30 +0900207 u64 skipped_samples;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300208};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200209
David Ahern49394a22016-11-16 15:06:29 +0900210/* per thread run time data */
211struct thread_runtime {
212 u64 last_time; /* time of previous sched in/out event */
213 u64 dt_run; /* run time */
214 u64 dt_wait; /* time between CPU access (off cpu) */
215 u64 dt_delay; /* time between wakeup and sched-in */
216 u64 ready_to_run; /* time of wakeup */
217
218 struct stats run_stats;
219 u64 total_run_time;
David Ahern350f54f2016-11-25 09:28:41 -0700220
221 u64 migrations;
David Ahern49394a22016-11-16 15:06:29 +0900222};
223
224/* per event run time data */
225struct evsel_runtime {
226 u64 *last_time; /* time this event was last seen per cpu */
227 u32 ncpu; /* highest cpu slot allocated */
228};
229
230/* track idle times per cpu */
231static struct thread **idle_threads;
232static int idle_max_cpu;
233static char idle_comm[] = "<idle>";
234
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200235static u64 get_nsecs(void)
236{
237 struct timespec ts;
238
239 clock_gettime(CLOCK_MONOTONIC, &ts);
240
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300241 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200242}
243
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300244static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200245{
246 u64 T0 = get_nsecs(), T1;
247
248 do {
249 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300250 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200251}
252
253static void sleep_nsecs(u64 nsecs)
254{
255 struct timespec ts;
256
257 ts.tv_nsec = nsecs % 999999999;
258 ts.tv_sec = nsecs / 999999999;
259
260 nanosleep(&ts, NULL);
261}
262
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300263static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200264{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300265 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200266 int i;
267
268 for (i = 0; i < 10; i++) {
269 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300270 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200271 T1 = get_nsecs();
272 delta = T1-T0;
273 min_delta = min(min_delta, delta);
274 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300275 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200276
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200277 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200278}
279
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300280static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200281{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300282 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200283 int i;
284
285 for (i = 0; i < 10; i++) {
286 T0 = get_nsecs();
287 sleep_nsecs(10000);
288 T1 = get_nsecs();
289 delta = T1-T0;
290 min_delta = min(min_delta, delta);
291 }
292 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300293 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200294
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200295 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200296}
297
mingo39aeb522009-09-14 20:04:48 +0200298static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200299get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200300{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200301 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200302 unsigned long idx = task->nr_events;
303 size_t size;
304
305 event->timestamp = timestamp;
306 event->nr = idx;
307
308 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200309 size = sizeof(struct sched_atom *) * task->nr_events;
310 task->atoms = realloc(task->atoms, size);
311 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200312
mingo39aeb522009-09-14 20:04:48 +0200313 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200314
315 return event;
316}
317
mingo39aeb522009-09-14 20:04:48 +0200318static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200319{
320 if (!task->nr_events)
321 return NULL;
322
mingo39aeb522009-09-14 20:04:48 +0200323 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200324}
325
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300326static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
327 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200328{
mingo39aeb522009-09-14 20:04:48 +0200329 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200330
331 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200332 * optimize an existing RUN event by merging this one
333 * to it:
334 */
Ingo Molnarec156762009-09-11 12:12:54 +0200335 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300336 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200337 curr_event->duration += duration;
338 return;
339 }
340
341 event = get_new_event(task, timestamp);
342
343 event->type = SCHED_EVENT_RUN;
344 event->duration = duration;
345
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300346 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200347}
348
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300349static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
350 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200351{
mingo39aeb522009-09-14 20:04:48 +0200352 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200353
354 event = get_new_event(task, timestamp);
355 event->type = SCHED_EVENT_WAKEUP;
356 event->wakee = wakee;
357
358 wakee_event = last_event(wakee);
359 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300360 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200361 return;
362 }
363 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300364 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200365 return;
366 }
367
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200368 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200369 sem_init(wakee_event->wait_sem, 0, 0);
370 wakee_event->specific_wait = 1;
371 event->wait_sem = wakee_event->wait_sem;
372
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300373 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200374}
375
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300376static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
377 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200378{
mingo39aeb522009-09-14 20:04:48 +0200379 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200380
381 event->type = SCHED_EVENT_SLEEP;
382
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300383 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200384}
385
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300386static struct task_desc *register_pid(struct perf_sched *sched,
387 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200388{
389 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800390 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200391
Yunlong Songcb06ac22015-03-31 21:46:30 +0800392 if (sched->pid_to_task == NULL) {
393 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
394 pid_max = MAX_PID;
395 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
396 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800397 if (pid >= (unsigned long)pid_max) {
398 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
399 sizeof(struct task_desc *))) == NULL);
400 while (pid >= (unsigned long)pid_max)
401 sched->pid_to_task[pid_max++] = NULL;
402 }
Ingo Molnarec156762009-09-11 12:12:54 +0200403
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300404 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200405
406 if (task)
407 return task;
408
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200409 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200410 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300411 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200412 strcpy(task->comm, comm);
413 /*
414 * every task starts in sleeping state - this gets ignored
415 * if there's no wakeup pointing to this sleep state:
416 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300417 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200418
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300419 sched->pid_to_task[pid] = task;
420 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800421 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300422 BUG_ON(!sched->tasks);
423 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200424
Ingo Molnarad236fd2009-09-11 12:12:54 +0200425 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300426 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200427
428 return task;
429}
430
431
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300432static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200433{
434 struct task_desc *task;
435 unsigned long i;
436
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300437 for (i = 0; i < sched->nr_tasks; i++) {
438 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200439 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200440 task->nr, task->comm, task->pid, task->nr_events);
441 }
442}
443
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300444static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200445{
446 struct task_desc *task1, *task2;
447 unsigned long i, j;
448
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300449 for (i = 0; i < sched->nr_tasks; i++) {
450 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200451 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300452 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200453 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300454 task2 = sched->tasks[j];
455 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200456 }
457}
458
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300459static void perf_sched__process_event(struct perf_sched *sched,
460 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200461{
462 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200463
mingo39aeb522009-09-14 20:04:48 +0200464 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200465 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300466 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200467 break;
468 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200469 if (atom->wait_sem)
470 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200471 BUG_ON(ret);
472 break;
473 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200474 if (atom->wait_sem)
475 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200476 BUG_ON(ret);
477 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200478 case SCHED_EVENT_MIGRATION:
479 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200480 default:
481 BUG_ON(1);
482 }
483}
484
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200485static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200486{
487 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200488 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200489 int err;
490
491 err = getrusage(RUSAGE_SELF, &ru);
492 BUG_ON(err);
493
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300494 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
495 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200496
497 return sum;
498}
499
Yunlong Song939cda52015-03-31 21:46:34 +0800500static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200501{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800502 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800503 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800504 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800505 struct rlimit limit;
506 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800507
508 memset(&attr, 0, sizeof(attr));
509
510 attr.type = PERF_TYPE_SOFTWARE;
511 attr.config = PERF_COUNT_SW_TASK_CLOCK;
512
Yunlong Song939cda52015-03-31 21:46:34 +0800513force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200514 fd = sys_perf_event_open(&attr, 0, -1, -1,
515 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800516
Yunlong Song1aff59b2015-03-31 21:46:33 +0800517 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800518 if (errno == EMFILE) {
519 if (sched->force) {
520 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
521 limit.rlim_cur += sched->nr_tasks - cur_task;
522 if (limit.rlim_cur > limit.rlim_max) {
523 limit.rlim_max = limit.rlim_cur;
524 need_privilege = true;
525 }
526 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
527 if (need_privilege && errno == EPERM)
528 strcpy(info, "Need privilege\n");
529 } else
530 goto force_again;
531 } else
532 strcpy(info, "Have a try with -f option\n");
533 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900534 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800535 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300536 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800537 exit(EXIT_FAILURE);
538 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800539 return fd;
540}
541
542static u64 get_cpu_usage_nsec_self(int fd)
543{
544 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200545 int ret;
546
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800547 ret = read(fd, &runtime, sizeof(runtime));
548 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200549
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800550 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200551}
552
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553struct sched_thread_parms {
554 struct task_desc *task;
555 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800556 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300557};
558
Ingo Molnarec156762009-09-11 12:12:54 +0200559static void *thread_func(void *ctx)
560{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300561 struct sched_thread_parms *parms = ctx;
562 struct task_desc *this_task = parms->task;
563 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200564 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200565 unsigned long i, ret;
566 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800567 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200568
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300569 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300570
Ingo Molnarec156762009-09-11 12:12:54 +0200571 sprintf(comm2, ":%s", this_task->comm);
572 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300573 if (fd < 0)
574 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200575again:
576 ret = sem_post(&this_task->ready_for_work);
577 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300578 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200579 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200581 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200582
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800583 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200584
585 for (i = 0; i < this_task->nr_events; i++) {
586 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300587 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200588 }
589
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800590 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200591 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200592 ret = sem_post(&this_task->work_done_sem);
593 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200594
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200596 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300597 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200598 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200599
600 goto again;
601}
602
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300603static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200604{
605 struct task_desc *task;
606 pthread_attr_t attr;
607 unsigned long i;
608 int err;
609
610 err = pthread_attr_init(&attr);
611 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200612 err = pthread_attr_setstacksize(&attr,
613 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200614 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300615 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200616 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300617 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200618 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300619 for (i = 0; i < sched->nr_tasks; i++) {
620 struct sched_thread_parms *parms = malloc(sizeof(*parms));
621 BUG_ON(parms == NULL);
622 parms->task = task = sched->tasks[i];
623 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800624 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200625 sem_init(&task->sleep_sem, 0, 0);
626 sem_init(&task->ready_for_work, 0, 0);
627 sem_init(&task->work_done_sem, 0, 0);
628 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300629 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200630 BUG_ON(err);
631 }
632}
633
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300634static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200635{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200636 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200637 struct task_desc *task;
638 unsigned long i, ret;
639
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300640 sched->start_time = get_nsecs();
641 sched->cpu_usage = 0;
642 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200643
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300644 for (i = 0; i < sched->nr_tasks; i++) {
645 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200646 ret = sem_wait(&task->ready_for_work);
647 BUG_ON(ret);
648 sem_init(&task->ready_for_work, 0, 0);
649 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200651 BUG_ON(ret);
652
653 cpu_usage_0 = get_cpu_usage_nsec_parent();
654
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200656
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300657 for (i = 0; i < sched->nr_tasks; i++) {
658 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200659 ret = sem_wait(&task->work_done_sem);
660 BUG_ON(ret);
661 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300662 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200663 task->cpu_usage = 0;
664 }
665
666 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300667 if (!sched->runavg_cpu_usage)
668 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800669 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 +0200670
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300671 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
672 if (!sched->runavg_parent_cpu_usage)
673 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800674 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
675 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200676
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300677 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200678 BUG_ON(ret);
679
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300680 for (i = 0; i < sched->nr_tasks; i++) {
681 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200682 sem_init(&task->sleep_sem, 0, 0);
683 task->curr_event = 0;
684 }
685}
686
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300687static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200688{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500689 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200690
691 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300692 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200693 T1 = get_nsecs();
694
695 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300696 sched->sum_runtime += delta;
697 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200698
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300699 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200700 if (delta < avg_delta)
701 fluct = avg_delta - delta;
702 else
703 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300704 sched->sum_fluct += fluct;
705 if (!sched->run_avg)
706 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800707 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200708
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300709 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200710
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300711 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200712
Ingo Molnarad236fd2009-09-11 12:12:54 +0200713 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300714 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200715
716#if 0
717 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200718 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300719 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200720 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200721 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300722 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
723 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200724#endif
725
Ingo Molnarad236fd2009-09-11 12:12:54 +0200726 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200727
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300728 if (sched->nr_sleep_corrections)
729 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
730 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200731}
732
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300733static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200734{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200735 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200736
737 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300738 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200739 T1 = get_nsecs();
740
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200741 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200742
743 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300744 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200745 T1 = get_nsecs();
746
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200747 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200748}
749
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300750static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300751replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300752 struct perf_evsel *evsel, struct perf_sample *sample,
753 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200754{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300755 const char *comm = perf_evsel__strval(evsel, sample, "comm");
756 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200757 struct task_desc *waker, *wakee;
758
759 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300760 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200761
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300762 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200763 }
764
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300765 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300766 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200767
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300768 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300769 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200770}
771
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300772static int replay_switch_event(struct perf_sched *sched,
773 struct perf_evsel *evsel,
774 struct perf_sample *sample,
775 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200776{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300777 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
778 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
779 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
780 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
781 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300782 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300783 u64 timestamp0, timestamp = sample->time;
784 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200785 s64 delta;
786
Ingo Molnarad236fd2009-09-11 12:12:54 +0200787 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300788 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200789
Ingo Molnarfbf94822009-09-11 12:12:54 +0200790 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300791 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200792
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300793 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200794 if (timestamp0)
795 delta = timestamp - timestamp0;
796 else
797 delta = 0;
798
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300799 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900800 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300801 return -1;
802 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200803
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300804 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
805 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200806
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300807 prev = register_pid(sched, prev_pid, prev_comm);
808 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200809
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300810 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200811
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300812 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300813 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300814
815 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200816}
817
David Aherncb627502013-08-07 22:50:47 -0400818static int replay_fork_event(struct perf_sched *sched,
819 union perf_event *event,
820 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200821{
David Aherncb627502013-08-07 22:50:47 -0400822 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300823
Adrian Hunter314add62013-08-27 11:23:03 +0300824 child = machine__findnew_thread(machine, event->fork.pid,
825 event->fork.tid);
826 parent = machine__findnew_thread(machine, event->fork.ppid,
827 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400828
829 if (child == NULL || parent == NULL) {
830 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
831 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300832 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200833 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300834
David Aherncb627502013-08-07 22:50:47 -0400835 if (verbose) {
836 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200837 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
838 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400839 }
840
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200841 register_pid(sched, parent->tid, thread__comm_str(parent));
842 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300843out_put:
844 thread__put(child);
845 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300846 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200847}
848
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200849struct sort_dimension {
850 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200851 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200852 struct list_head list;
853};
854
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200855static int
mingo39aeb522009-09-14 20:04:48 +0200856thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200857{
858 struct sort_dimension *sort;
859 int ret = 0;
860
Ingo Molnarb5fae122009-09-11 12:12:54 +0200861 BUG_ON(list_empty(list));
862
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200863 list_for_each_entry(sort, list, list) {
864 ret = sort->cmp(l, r);
865 if (ret)
866 return ret;
867 }
868
869 return ret;
870}
871
mingo39aeb522009-09-14 20:04:48 +0200872static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200873thread_atoms_search(struct rb_root *root, struct thread *thread,
874 struct list_head *sort_list)
875{
876 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200877 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200878
879 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200880 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200881 int cmp;
882
mingo39aeb522009-09-14 20:04:48 +0200883 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200884
885 cmp = thread_lat_cmp(sort_list, &key, atoms);
886 if (cmp > 0)
887 node = node->rb_left;
888 else if (cmp < 0)
889 node = node->rb_right;
890 else {
891 BUG_ON(thread != atoms->thread);
892 return atoms;
893 }
894 }
895 return NULL;
896}
897
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200898static void
mingo39aeb522009-09-14 20:04:48 +0200899__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200900 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200901{
902 struct rb_node **new = &(root->rb_node), *parent = NULL;
903
904 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200905 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200906 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907
mingo39aeb522009-09-14 20:04:48 +0200908 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200909 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200910
911 cmp = thread_lat_cmp(sort_list, data, this);
912
913 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200914 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200915 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200916 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200917 }
918
919 rb_link_node(&data->node, parent, new);
920 rb_insert_color(&data->node, root);
921}
922
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300923static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200924{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200925 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300926 if (!atoms) {
927 pr_err("No memory at %s\n", __func__);
928 return -1;
929 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300931 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200932 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300933 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300934 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935}
936
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300937static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200938{
939 const char *str = TASK_STATE_TO_CHAR_STR;
940
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300941 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200942}
943
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300944static int
mingo39aeb522009-09-14 20:04:48 +0200945add_sched_out_event(struct work_atoms *atoms,
946 char run_state,
947 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200948{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200949 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300950 if (!atom) {
951 pr_err("Non memory at %s", __func__);
952 return -1;
953 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200954
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200955 atom->sched_out_time = timestamp;
956
mingo39aeb522009-09-14 20:04:48 +0200957 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200958 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200959 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200960 }
961
mingo39aeb522009-09-14 20:04:48 +0200962 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300963 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200964}
965
966static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300967add_runtime_event(struct work_atoms *atoms, u64 delta,
968 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200969{
970 struct work_atom *atom;
971
972 BUG_ON(list_empty(&atoms->work_list));
973
974 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
975
976 atom->runtime += delta;
977 atoms->total_runtime += delta;
978}
979
980static void
981add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200983 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200984 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200985
mingo39aeb522009-09-14 20:04:48 +0200986 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987 return;
988
mingo39aeb522009-09-14 20:04:48 +0200989 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200990
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200991 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200992 return;
993
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200994 if (timestamp < atom->wake_up_time) {
995 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200996 return;
997 }
998
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200999 atom->state = THREAD_SCHED_IN;
1000 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001001
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001002 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001003 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001004 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001005 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001006 atoms->max_lat_at = timestamp;
1007 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001008 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001009}
1010
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001011static int latency_switch_event(struct perf_sched *sched,
1012 struct perf_evsel *evsel,
1013 struct perf_sample *sample,
1014 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001015{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001016 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1017 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1018 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001019 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001020 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001021 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001022 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001023 s64 delta;
1024
mingo39aeb522009-09-14 20:04:48 +02001025 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001026
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001027 timestamp0 = sched->cpu_last_switched[cpu];
1028 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001029 if (timestamp0)
1030 delta = timestamp - timestamp0;
1031 else
1032 delta = 0;
1033
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001034 if (delta < 0) {
1035 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1036 return -1;
1037 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001038
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001039 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1040 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001041 if (sched_out == NULL || sched_in == NULL)
1042 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001044 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001045 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001046 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001047 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001048 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001049 if (!out_events) {
1050 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001051 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001052 }
mingo39aeb522009-09-14 20:04:48 +02001053 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001054 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001055 return -1;
mingo39aeb522009-09-14 20:04:48 +02001056
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001057 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001058 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001059 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001060 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001061 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001062 if (!in_events) {
1063 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001064 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001065 }
mingo39aeb522009-09-14 20:04:48 +02001066 /*
1067 * Take came in we have not heard about yet,
1068 * add in an initial atom in runnable state:
1069 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001070 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001071 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001072 }
1073 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001074 err = 0;
1075out_put:
1076 thread__put(sched_out);
1077 thread__put(sched_in);
1078 return err;
mingo39aeb522009-09-14 20:04:48 +02001079}
1080
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001081static int latency_runtime_event(struct perf_sched *sched,
1082 struct perf_evsel *evsel,
1083 struct perf_sample *sample,
1084 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001085{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001086 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1087 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001088 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001089 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001090 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001091 int cpu = sample->cpu, err = -1;
1092
1093 if (thread == NULL)
1094 return -1;
mingo39aeb522009-09-14 20:04:48 +02001095
1096 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001097 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001098 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001099 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001100 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001101 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001102 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001103 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001104 }
1105 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001106 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001107 }
1108
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001109 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001110 err = 0;
1111out_put:
1112 thread__put(thread);
1113 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001114}
1115
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001116static int latency_wakeup_event(struct perf_sched *sched,
1117 struct perf_evsel *evsel,
1118 struct perf_sample *sample,
1119 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001121 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001122 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001123 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001124 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001125 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001126 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001127
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001128 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001129 if (wakee == NULL)
1130 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001131 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001132 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001133 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001134 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001135 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001136 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001137 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001138 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001139 }
1140 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001141 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001142 }
1143
mingo39aeb522009-09-14 20:04:48 +02001144 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001145
mingo39aeb522009-09-14 20:04:48 +02001146 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001147
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001148 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001149 * As we do not guarantee the wakeup event happens when
1150 * task is out of run queue, also may happen when task is
1151 * on run queue and wakeup only change ->state to TASK_RUNNING,
1152 * then we should not set the ->wake_up_time when wake up a
1153 * task which is on run queue.
1154 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001155 * You WILL be missing events if you've recorded only
1156 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001157 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001158 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001159 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001160 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001161
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001162 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001163 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001164 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001165 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001166 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001167
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001168 atom->state = THREAD_WAIT_CPU;
1169 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001170out_ok:
1171 err = 0;
1172out_put:
1173 thread__put(wakee);
1174 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001175}
1176
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001177static int latency_migrate_task_event(struct perf_sched *sched,
1178 struct perf_evsel *evsel,
1179 struct perf_sample *sample,
1180 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001181{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001182 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001183 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001184 struct work_atoms *atoms;
1185 struct work_atom *atom;
1186 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001187 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001188
1189 /*
1190 * Only need to worry about migration when profiling one CPU.
1191 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001192 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001193 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001194
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001195 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001196 if (migrant == NULL)
1197 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001198 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001199 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001200 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001201 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001202 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001203 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001204 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001205 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001206 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001207 }
1208 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001209 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001210 }
1211
1212 BUG_ON(list_empty(&atoms->work_list));
1213
1214 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1215 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1216
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001217 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001218
1219 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001220 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001221 err = 0;
1222out_put:
1223 thread__put(migrant);
1224 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001225}
1226
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001227static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001228{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001229 int i;
1230 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001231 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001232 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001233
mingo39aeb522009-09-14 20:04:48 +02001234 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001235 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001236 /*
1237 * Ignore idle threads:
1238 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001239 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001240 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001241
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001242 sched->all_runtime += work_list->total_runtime;
1243 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001244
Josef Bacik2f80dd42015-05-22 09:18:40 -04001245 if (work_list->num_merged > 1)
1246 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1247 else
1248 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001249
mingo08f69e62009-09-14 18:30:44 +02001250 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001251 printf(" ");
1252
mingo39aeb522009-09-14 20:04:48 +02001253 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001254 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001255
Namhyung Kim99620a52016-10-24 11:02:45 +09001256 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 -03001257 (double)work_list->total_runtime / NSEC_PER_MSEC,
1258 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1259 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001260 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001261}
1262
mingo39aeb522009-09-14 20:04:48 +02001263static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001264{
Jiri Olsa0014de12015-11-02 12:10:25 +01001265 if (l->thread == r->thread)
1266 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001267 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001268 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001269 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001270 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001271 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001272}
1273
mingo39aeb522009-09-14 20:04:48 +02001274static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001275{
1276 u64 avgl, avgr;
1277
1278 if (!l->nb_atoms)
1279 return -1;
1280
1281 if (!r->nb_atoms)
1282 return 1;
1283
1284 avgl = l->total_lat / l->nb_atoms;
1285 avgr = r->total_lat / r->nb_atoms;
1286
1287 if (avgl < avgr)
1288 return -1;
1289 if (avgl > avgr)
1290 return 1;
1291
1292 return 0;
1293}
1294
mingo39aeb522009-09-14 20:04:48 +02001295static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001296{
1297 if (l->max_lat < r->max_lat)
1298 return -1;
1299 if (l->max_lat > r->max_lat)
1300 return 1;
1301
1302 return 0;
1303}
1304
mingo39aeb522009-09-14 20:04:48 +02001305static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001306{
1307 if (l->nb_atoms < r->nb_atoms)
1308 return -1;
1309 if (l->nb_atoms > r->nb_atoms)
1310 return 1;
1311
1312 return 0;
1313}
1314
mingo39aeb522009-09-14 20:04:48 +02001315static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001316{
1317 if (l->total_runtime < r->total_runtime)
1318 return -1;
1319 if (l->total_runtime > r->total_runtime)
1320 return 1;
1321
1322 return 0;
1323}
1324
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001325static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001326{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001327 size_t i;
1328 static struct sort_dimension avg_sort_dimension = {
1329 .name = "avg",
1330 .cmp = avg_cmp,
1331 };
1332 static struct sort_dimension max_sort_dimension = {
1333 .name = "max",
1334 .cmp = max_cmp,
1335 };
1336 static struct sort_dimension pid_sort_dimension = {
1337 .name = "pid",
1338 .cmp = pid_cmp,
1339 };
1340 static struct sort_dimension runtime_sort_dimension = {
1341 .name = "runtime",
1342 .cmp = runtime_cmp,
1343 };
1344 static struct sort_dimension switch_sort_dimension = {
1345 .name = "switch",
1346 .cmp = switch_cmp,
1347 };
1348 struct sort_dimension *available_sorts[] = {
1349 &pid_sort_dimension,
1350 &avg_sort_dimension,
1351 &max_sort_dimension,
1352 &switch_sort_dimension,
1353 &runtime_sort_dimension,
1354 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001355
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001356 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001357 if (!strcmp(available_sorts[i]->name, tok)) {
1358 list_add_tail(&available_sorts[i]->list, list);
1359
1360 return 0;
1361 }
1362 }
1363
1364 return -1;
1365}
1366
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001367static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001368{
1369 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001370 struct rb_root *root = &sched->atom_root;
1371again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001372 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001373 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001374 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001375 if (!node)
1376 break;
1377
Josef Bacik2f80dd42015-05-22 09:18:40 -04001378 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001379 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001380 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001381 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001382 if (root == &sched->atom_root) {
1383 root = &sched->merged_atom_root;
1384 goto again;
1385 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001386}
1387
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001388static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001389 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001390 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001391 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001392{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001393 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001394
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001395 if (sched->tp_handler->wakeup_event)
1396 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001397
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001398 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001399}
1400
Jiri Olsaa151a372016-04-12 15:29:29 +02001401union map_priv {
1402 void *ptr;
1403 bool color;
1404};
1405
1406static bool thread__has_color(struct thread *thread)
1407{
1408 union map_priv priv = {
1409 .ptr = thread__priv(thread),
1410 };
1411
1412 return priv.color;
1413}
1414
1415static struct thread*
1416map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1417{
1418 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1419 union map_priv priv = {
1420 .color = false,
1421 };
1422
1423 if (!sched->map.color_pids || !thread || thread__priv(thread))
1424 return thread;
1425
1426 if (thread_map__has(sched->map.color_pids, tid))
1427 priv.color = true;
1428
1429 thread__set_priv(thread, priv.ptr);
1430 return thread;
1431}
1432
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001433static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1434 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001435{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001436 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1437 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001438 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001439 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001440 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001441 int i, this_cpu = sample->cpu;
1442 int cpus_nr;
1443 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001444 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001445 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001446
1447 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1448
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001449 if (this_cpu > sched->max_cpu)
1450 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001451
Jiri Olsa99623c62016-04-12 15:29:26 +02001452 if (sched->map.comp) {
1453 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1454 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1455 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1456 new_cpu = true;
1457 }
1458 } else
1459 cpus_nr = sched->max_cpu;
1460
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001461 timestamp0 = sched->cpu_last_switched[this_cpu];
1462 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001463 if (timestamp0)
1464 delta = timestamp - timestamp0;
1465 else
1466 delta = 0;
1467
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001468 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001469 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001470 return -1;
1471 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001472
Jiri Olsaa151a372016-04-12 15:29:29 +02001473 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001474 if (sched_in == NULL)
1475 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001476
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001477 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001478
1479 printf(" ");
1480
1481 new_shortname = 0;
1482 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001483 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1484 /*
1485 * Don't allocate a letter-number for swapper:0
1486 * as a shortname. Instead, we use '.' for it.
1487 */
1488 sched_in->shortname[0] = '.';
1489 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001490 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001491 sched_in->shortname[0] = sched->next_shortname1;
1492 sched_in->shortname[1] = sched->next_shortname2;
1493
1494 if (sched->next_shortname1 < 'Z') {
1495 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001496 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001497 sched->next_shortname1 = 'A';
1498 if (sched->next_shortname2 < '9')
1499 sched->next_shortname2++;
1500 else
1501 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001502 }
1503 }
1504 new_shortname = 1;
1505 }
1506
Jiri Olsa99623c62016-04-12 15:29:26 +02001507 for (i = 0; i < cpus_nr; i++) {
1508 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001509 struct thread *curr_thread = sched->curr_thread[cpu];
1510 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001511 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001512
1513 if (curr_thread && thread__has_color(curr_thread))
1514 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001515
Jiri Olsa73643bb2016-04-12 15:29:31 +02001516 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1517 continue;
1518
Jiri Olsacf294f22016-04-12 15:29:30 +02001519 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1520 cpu_color = COLOR_CPUS;
1521
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001522 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001523 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001524 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001525 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001526
Dongsheng6bcab4e2014-05-06 14:39:01 +09001527 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001528 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001529 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001530 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001531 }
1532
Jiri Olsa73643bb2016-04-12 15:29:31 +02001533 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1534 goto out;
1535
Namhyung Kim99620a52016-10-24 11:02:45 +09001536 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1537 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001538 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001539 const char *pid_color = color;
1540
1541 if (thread__has_color(sched_in))
1542 pid_color = COLOR_PIDS;
1543
1544 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001545 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001546 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001547
Jiri Olsa99623c62016-04-12 15:29:26 +02001548 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001549 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001550
Jiri Olsa73643bb2016-04-12 15:29:31 +02001551out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001552 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001553
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001554 thread__put(sched_in);
1555
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001556 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001557}
1558
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001559static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001560 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001561 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001562 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001563{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001564 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001565 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001566 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1567 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001568
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001569 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001570 /*
1571 * Are we trying to switch away a PID that is
1572 * not current?
1573 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001574 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001575 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001576 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001577
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001578 if (sched->tp_handler->switch_event)
1579 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001580
1581 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001582 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001583}
1584
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001585static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001586 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001587 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001588 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001589{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001590 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001591
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001592 if (sched->tp_handler->runtime_event)
1593 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001594
1595 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001596}
1597
David Aherncb627502013-08-07 22:50:47 -04001598static int perf_sched__process_fork_event(struct perf_tool *tool,
1599 union perf_event *event,
1600 struct perf_sample *sample,
1601 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001602{
1603 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1604
David Aherncb627502013-08-07 22:50:47 -04001605 /* run the fork event through the perf machineruy */
1606 perf_event__process_fork(tool, event, sample, machine);
1607
1608 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001609 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001610 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001611
1612 return 0;
1613}
1614
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001615static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001616 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001617 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001618 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001619{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001620 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001621
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001622 if (sched->tp_handler->migrate_task_event)
1623 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001624
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001625 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001626}
1627
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001628typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001629 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001630 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001631 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001632
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001633static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1634 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001635 struct perf_sample *sample,
1636 struct perf_evsel *evsel,
1637 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001638{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001639 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001640
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001641 if (evsel->handler != NULL) {
1642 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001643 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001644 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001645
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001646 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001647}
1648
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001649static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001650{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001651 const struct perf_evsel_str_handler handlers[] = {
1652 { "sched:sched_switch", process_sched_switch_event, },
1653 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1654 { "sched:sched_wakeup", process_sched_wakeup_event, },
1655 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001656 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1657 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001658 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001659 struct perf_data_file file = {
1660 .path = input_name,
1661 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001662 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001663 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001664 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001665
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001666 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001667 if (session == NULL) {
1668 pr_debug("No Memory for session\n");
1669 return -1;
1670 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001671
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001672 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001673
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001674 if (perf_session__set_tracepoints_handlers(session, handlers))
1675 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001676
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001677 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001678 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001679 if (err) {
1680 pr_err("Failed to process events, error %d", err);
1681 goto out_delete;
1682 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001683
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001684 sched->nr_events = session->evlist->stats.nr_events[0];
1685 sched->nr_lost_events = session->evlist->stats.total_lost;
1686 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001687 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001688
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001689 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001690out_delete:
1691 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001692 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001693}
1694
David Ahern49394a22016-11-16 15:06:29 +09001695/*
1696 * scheduling times are printed as msec.usec
1697 */
1698static inline void print_sched_time(unsigned long long nsecs, int width)
1699{
1700 unsigned long msecs;
1701 unsigned long usecs;
1702
1703 msecs = nsecs / NSEC_PER_MSEC;
1704 nsecs -= msecs * NSEC_PER_MSEC;
1705 usecs = nsecs / NSEC_PER_USEC;
1706 printf("%*lu.%03lu ", width, msecs, usecs);
1707}
1708
1709/*
1710 * returns runtime data for event, allocating memory for it the
1711 * first time it is used.
1712 */
1713static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1714{
1715 struct evsel_runtime *r = evsel->priv;
1716
1717 if (r == NULL) {
1718 r = zalloc(sizeof(struct evsel_runtime));
1719 evsel->priv = r;
1720 }
1721
1722 return r;
1723}
1724
1725/*
1726 * save last time event was seen per cpu
1727 */
1728static void perf_evsel__save_time(struct perf_evsel *evsel,
1729 u64 timestamp, u32 cpu)
1730{
1731 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1732
1733 if (r == NULL)
1734 return;
1735
1736 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1737 int i, n = __roundup_pow_of_two(cpu+1);
1738 void *p = r->last_time;
1739
1740 p = realloc(r->last_time, n * sizeof(u64));
1741 if (!p)
1742 return;
1743
1744 r->last_time = p;
1745 for (i = r->ncpu; i < n; ++i)
1746 r->last_time[i] = (u64) 0;
1747
1748 r->ncpu = n;
1749 }
1750
1751 r->last_time[cpu] = timestamp;
1752}
1753
1754/* returns last time this event was seen on the given cpu */
1755static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1756{
1757 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1758
1759 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1760 return 0;
1761
1762 return r->last_time[cpu];
1763}
1764
1765static int comm_width = 20;
1766
1767static char *timehist_get_commstr(struct thread *thread)
1768{
1769 static char str[32];
1770 const char *comm = thread__comm_str(thread);
1771 pid_t tid = thread->tid;
1772 pid_t pid = thread->pid_;
1773 int n;
1774
1775 if (pid == 0)
1776 n = scnprintf(str, sizeof(str), "%s", comm);
1777
1778 else if (tid != pid)
1779 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1780
1781 else
1782 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1783
1784 if (n > comm_width)
1785 comm_width = n;
1786
1787 return str;
1788}
1789
David Aherna407b062016-11-16 15:06:33 +09001790static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001791{
David Aherna407b062016-11-16 15:06:33 +09001792 u32 ncpus = sched->max_cpu + 1;
1793 u32 i, j;
1794
David Ahern49394a22016-11-16 15:06:29 +09001795 printf("%15s %6s ", "time", "cpu");
1796
David Aherna407b062016-11-16 15:06:33 +09001797 if (sched->show_cpu_visual) {
1798 printf(" ");
1799 for (i = 0, j = 0; i < ncpus; ++i) {
1800 printf("%x", j++);
1801 if (j > 15)
1802 j = 0;
1803 }
1804 printf(" ");
1805 }
1806
David Ahern49394a22016-11-16 15:06:29 +09001807 printf(" %-20s %9s %9s %9s",
1808 "task name", "wait time", "sch delay", "run time");
1809
1810 printf("\n");
1811
1812 /*
1813 * units row
1814 */
1815 printf("%15s %-6s ", "", "");
1816
David Aherna407b062016-11-16 15:06:33 +09001817 if (sched->show_cpu_visual)
1818 printf(" %*s ", ncpus, "");
1819
David Ahern49394a22016-11-16 15:06:29 +09001820 printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)");
1821
1822 /*
1823 * separator
1824 */
1825 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1826
David Aherna407b062016-11-16 15:06:33 +09001827 if (sched->show_cpu_visual)
1828 printf(" %.*s ", ncpus, graph_dotted_line);
1829
David Ahern49394a22016-11-16 15:06:29 +09001830 printf(" %.20s %.9s %.9s %.9s",
1831 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1832 graph_dotted_line);
1833
1834 printf("\n");
1835}
1836
David Ahernfc1469f2016-11-16 15:06:31 +09001837static void timehist_print_sample(struct perf_sched *sched,
1838 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001839 struct addr_location *al,
David Ahern49394a22016-11-16 15:06:29 +09001840 struct thread *thread)
1841{
1842 struct thread_runtime *tr = thread__priv(thread);
David Aherna407b062016-11-16 15:06:33 +09001843 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09001844 char tstr[64];
1845
1846 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
1847 printf("%15s [%04d] ", tstr, sample->cpu);
1848
David Aherna407b062016-11-16 15:06:33 +09001849 if (sched->show_cpu_visual) {
1850 u32 i;
1851 char c;
1852
1853 printf(" ");
1854 for (i = 0; i < max_cpus; ++i) {
1855 /* flag idle times with 'i'; others are sched events */
1856 if (i == sample->cpu)
1857 c = (thread->tid == 0) ? 'i' : 's';
1858 else
1859 c = ' ';
1860 printf("%c", c);
1861 }
1862 printf(" ");
1863 }
1864
David Ahern49394a22016-11-16 15:06:29 +09001865 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1866
1867 print_sched_time(tr->dt_wait, 6);
1868 print_sched_time(tr->dt_delay, 6);
1869 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001870
1871 if (sched->show_wakeups)
1872 printf(" %-*s", comm_width, "");
1873
David Ahern6c973c92016-11-16 15:06:32 +09001874 if (thread->tid == 0)
1875 goto out;
1876
1877 if (sched->show_callchain)
1878 printf(" ");
1879
1880 sample__fprintf_sym(sample, al, 0,
1881 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09001882 EVSEL__PRINT_CALLCHAIN_ARROW |
1883 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09001884 &callchain_cursor, stdout);
1885
1886out:
David Ahern49394a22016-11-16 15:06:29 +09001887 printf("\n");
1888}
1889
1890/*
1891 * Explanation of delta-time stats:
1892 *
1893 * t = time of current schedule out event
1894 * tprev = time of previous sched out event
1895 * also time of schedule-in event for current task
1896 * last_time = time of last sched change event for current task
1897 * (i.e, time process was last scheduled out)
1898 * ready_to_run = time of wakeup for current task
1899 *
1900 * -----|------------|------------|------------|------
1901 * last ready tprev t
1902 * time to run
1903 *
1904 * |-------- dt_wait --------|
1905 * |- dt_delay -|-- dt_run --|
1906 *
1907 * dt_run = run time of current task
1908 * dt_wait = time between last schedule out event for task and tprev
1909 * represents time spent off the cpu
1910 * dt_delay = time between wakeup and schedule-in of task
1911 */
1912
1913static void timehist_update_runtime_stats(struct thread_runtime *r,
1914 u64 t, u64 tprev)
1915{
1916 r->dt_delay = 0;
1917 r->dt_wait = 0;
1918 r->dt_run = 0;
1919 if (tprev) {
1920 r->dt_run = t - tprev;
1921 if (r->ready_to_run) {
1922 if (r->ready_to_run > tprev)
1923 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1924 else
1925 r->dt_delay = tprev - r->ready_to_run;
1926 }
1927
1928 if (r->last_time > tprev)
1929 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
1930 else if (r->last_time)
1931 r->dt_wait = tprev - r->last_time;
1932 }
1933
1934 update_stats(&r->run_stats, r->dt_run);
1935 r->total_run_time += r->dt_run;
1936}
1937
David Ahern6c973c92016-11-16 15:06:32 +09001938static bool is_idle_sample(struct perf_sched *sched,
1939 struct perf_sample *sample,
1940 struct perf_evsel *evsel,
1941 struct machine *machine)
David Ahern49394a22016-11-16 15:06:29 +09001942{
David Ahern6c973c92016-11-16 15:06:32 +09001943 struct thread *thread;
1944 struct callchain_cursor *cursor = &callchain_cursor;
1945
David Ahern49394a22016-11-16 15:06:29 +09001946 /* pid 0 == swapper == idle task */
1947 if (sample->pid == 0)
1948 return true;
1949
1950 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) {
1951 if (perf_evsel__intval(evsel, sample, "prev_pid") == 0)
1952 return true;
1953 }
David Ahern6c973c92016-11-16 15:06:32 +09001954
1955 /* want main thread for process - has maps */
1956 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
1957 if (thread == NULL) {
1958 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
1959 return false;
1960 }
1961
1962 if (!symbol_conf.use_callchain || sample->callchain == NULL)
1963 return false;
1964
1965 if (thread__resolve_callchain(thread, cursor, evsel, sample,
Namhyung Kim8388deb2016-11-24 10:11:14 +09001966 NULL, NULL, sched->max_stack + 2) != 0) {
David Ahern6c973c92016-11-16 15:06:32 +09001967 if (verbose)
1968 error("Failed to resolve callchain. Skipping\n");
1969
1970 return false;
1971 }
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09001972
David Ahern6c973c92016-11-16 15:06:32 +09001973 callchain_cursor_commit(cursor);
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09001974
1975 while (true) {
1976 struct callchain_cursor_node *node;
1977 struct symbol *sym;
1978
1979 node = callchain_cursor_current(cursor);
1980 if (node == NULL)
1981 break;
1982
1983 sym = node->sym;
1984 if (sym && sym->name) {
1985 if (!strcmp(sym->name, "schedule") ||
1986 !strcmp(sym->name, "__schedule") ||
1987 !strcmp(sym->name, "preempt_schedule"))
1988 sym->ignore = 1;
1989 }
1990
1991 callchain_cursor_advance(cursor);
1992 }
1993
David Ahern49394a22016-11-16 15:06:29 +09001994 return false;
1995}
1996
1997/*
1998 * Track idle stats per cpu by maintaining a local thread
1999 * struct for the idle task on each cpu.
2000 */
2001static int init_idle_threads(int ncpu)
2002{
2003 int i;
2004
2005 idle_threads = zalloc(ncpu * sizeof(struct thread *));
2006 if (!idle_threads)
2007 return -ENOMEM;
2008
2009 idle_max_cpu = ncpu - 1;
2010
2011 /* allocate the actual thread struct if needed */
2012 for (i = 0; i < ncpu; ++i) {
2013 idle_threads[i] = thread__new(0, 0);
2014 if (idle_threads[i] == NULL)
2015 return -ENOMEM;
2016
2017 thread__set_comm(idle_threads[i], idle_comm, 0);
2018 }
2019
2020 return 0;
2021}
2022
2023static void free_idle_threads(void)
2024{
2025 int i;
2026
2027 if (idle_threads == NULL)
2028 return;
2029
2030 for (i = 0; i <= idle_max_cpu; ++i) {
2031 if ((idle_threads[i]))
2032 thread__delete(idle_threads[i]);
2033 }
2034
2035 free(idle_threads);
2036}
2037
2038static struct thread *get_idle_thread(int cpu)
2039{
2040 /*
2041 * expand/allocate array of pointers to local thread
2042 * structs if needed
2043 */
2044 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2045 int i, j = __roundup_pow_of_two(cpu+1);
2046 void *p;
2047
2048 p = realloc(idle_threads, j * sizeof(struct thread *));
2049 if (!p)
2050 return NULL;
2051
2052 idle_threads = (struct thread **) p;
2053 i = idle_max_cpu ? idle_max_cpu + 1 : 0;
2054 for (; i < j; ++i)
2055 idle_threads[i] = NULL;
2056
2057 idle_max_cpu = j;
2058 }
2059
2060 /* allocate a new thread struct if needed */
2061 if (idle_threads[cpu] == NULL) {
2062 idle_threads[cpu] = thread__new(0, 0);
2063
2064 if (idle_threads[cpu]) {
2065 idle_threads[cpu]->tid = 0;
2066 thread__set_comm(idle_threads[cpu], idle_comm, 0);
2067 }
2068 }
2069
2070 return idle_threads[cpu];
2071}
2072
2073/*
2074 * handle runtime stats saved per thread
2075 */
2076static struct thread_runtime *thread__init_runtime(struct thread *thread)
2077{
2078 struct thread_runtime *r;
2079
2080 r = zalloc(sizeof(struct thread_runtime));
2081 if (!r)
2082 return NULL;
2083
2084 init_stats(&r->run_stats);
2085 thread__set_priv(thread, r);
2086
2087 return r;
2088}
2089
2090static struct thread_runtime *thread__get_runtime(struct thread *thread)
2091{
2092 struct thread_runtime *tr;
2093
2094 tr = thread__priv(thread);
2095 if (tr == NULL) {
2096 tr = thread__init_runtime(thread);
2097 if (tr == NULL)
2098 pr_debug("Failed to malloc memory for runtime data.\n");
2099 }
2100
2101 return tr;
2102}
2103
David Ahern6c973c92016-11-16 15:06:32 +09002104static struct thread *timehist_get_thread(struct perf_sched *sched,
2105 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002106 struct machine *machine,
2107 struct perf_evsel *evsel)
2108{
2109 struct thread *thread;
2110
David Ahern6c973c92016-11-16 15:06:32 +09002111 if (is_idle_sample(sched, sample, evsel, machine)) {
David Ahern49394a22016-11-16 15:06:29 +09002112 thread = get_idle_thread(sample->cpu);
2113 if (thread == NULL)
2114 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2115
2116 } else {
2117 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2118 if (thread == NULL) {
2119 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2120 sample->tid);
2121 }
2122 }
2123
2124 return thread;
2125}
2126
David Ahern52df1382016-11-16 15:06:30 +09002127static bool timehist_skip_sample(struct perf_sched *sched,
2128 struct thread *thread)
David Ahern49394a22016-11-16 15:06:29 +09002129{
2130 bool rc = false;
2131
David Ahern52df1382016-11-16 15:06:30 +09002132 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002133 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002134 sched->skipped_samples++;
2135 }
David Ahern49394a22016-11-16 15:06:29 +09002136
2137 return rc;
2138}
2139
David Ahernfc1469f2016-11-16 15:06:31 +09002140static void timehist_print_wakeup_event(struct perf_sched *sched,
2141 struct perf_sample *sample,
2142 struct machine *machine,
2143 struct thread *awakened)
2144{
2145 struct thread *thread;
2146 char tstr[64];
2147
2148 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2149 if (thread == NULL)
2150 return;
2151
2152 /* show wakeup unless both awakee and awaker are filtered */
2153 if (timehist_skip_sample(sched, thread) &&
2154 timehist_skip_sample(sched, awakened)) {
2155 return;
2156 }
2157
2158 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2159 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002160 if (sched->show_cpu_visual)
2161 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002162
2163 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2164
2165 /* dt spacer */
2166 printf(" %9s %9s %9s ", "", "", "");
2167
2168 printf("awakened: %s", timehist_get_commstr(awakened));
2169
2170 printf("\n");
2171}
2172
2173static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002174 union perf_event *event __maybe_unused,
2175 struct perf_evsel *evsel,
2176 struct perf_sample *sample,
2177 struct machine *machine)
2178{
David Ahernfc1469f2016-11-16 15:06:31 +09002179 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002180 struct thread *thread;
2181 struct thread_runtime *tr = NULL;
2182 /* want pid of awakened task not pid in sample */
2183 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2184
2185 thread = machine__findnew_thread(machine, 0, pid);
2186 if (thread == NULL)
2187 return -1;
2188
2189 tr = thread__get_runtime(thread);
2190 if (tr == NULL)
2191 return -1;
2192
2193 if (tr->ready_to_run == 0)
2194 tr->ready_to_run = sample->time;
2195
David Ahernfc1469f2016-11-16 15:06:31 +09002196 /* show wakeups if requested */
2197 if (sched->show_wakeups)
2198 timehist_print_wakeup_event(sched, sample, machine, thread);
2199
David Ahern49394a22016-11-16 15:06:29 +09002200 return 0;
2201}
2202
David Ahern350f54f2016-11-25 09:28:41 -07002203static void timehist_print_migration_event(struct perf_sched *sched,
2204 struct perf_evsel *evsel,
2205 struct perf_sample *sample,
2206 struct machine *machine,
2207 struct thread *migrated)
2208{
2209 struct thread *thread;
2210 char tstr[64];
2211 u32 max_cpus = sched->max_cpu + 1;
2212 u32 ocpu, dcpu;
2213
2214 if (sched->summary_only)
2215 return;
2216
2217 max_cpus = sched->max_cpu + 1;
2218 ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
2219 dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
2220
2221 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2222 if (thread == NULL)
2223 return;
2224
2225 if (timehist_skip_sample(sched, thread) &&
2226 timehist_skip_sample(sched, migrated)) {
2227 return;
2228 }
2229
2230 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2231 printf("%15s [%04d] ", tstr, sample->cpu);
2232
2233 if (sched->show_cpu_visual) {
2234 u32 i;
2235 char c;
2236
2237 printf(" ");
2238 for (i = 0; i < max_cpus; ++i) {
2239 c = (i == sample->cpu) ? 'm' : ' ';
2240 printf("%c", c);
2241 }
2242 printf(" ");
2243 }
2244
2245 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2246
2247 /* dt spacer */
2248 printf(" %9s %9s %9s ", "", "", "");
2249
2250 printf("migrated: %s", timehist_get_commstr(migrated));
2251 printf(" cpu %d => %d", ocpu, dcpu);
2252
2253 printf("\n");
2254}
2255
2256static int timehist_migrate_task_event(struct perf_tool *tool,
2257 union perf_event *event __maybe_unused,
2258 struct perf_evsel *evsel,
2259 struct perf_sample *sample,
2260 struct machine *machine)
2261{
2262 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2263 struct thread *thread;
2264 struct thread_runtime *tr = NULL;
2265 /* want pid of migrated task not pid in sample */
2266 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2267
2268 thread = machine__findnew_thread(machine, 0, pid);
2269 if (thread == NULL)
2270 return -1;
2271
2272 tr = thread__get_runtime(thread);
2273 if (tr == NULL)
2274 return -1;
2275
2276 tr->migrations++;
2277
2278 /* show migrations if requested */
2279 timehist_print_migration_event(sched, evsel, sample, machine, thread);
2280
2281 return 0;
2282}
2283
David Ahern52df1382016-11-16 15:06:30 +09002284static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002285 union perf_event *event,
2286 struct perf_evsel *evsel,
2287 struct perf_sample *sample,
2288 struct machine *machine)
2289{
David Ahernfc1469f2016-11-16 15:06:31 +09002290 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002291 struct addr_location al;
2292 struct thread *thread;
2293 struct thread_runtime *tr = NULL;
2294 u64 tprev;
2295 int rc = 0;
2296
2297 if (machine__resolve(machine, &al, sample) < 0) {
2298 pr_err("problem processing %d event. skipping it\n",
2299 event->header.type);
2300 rc = -1;
2301 goto out;
2302 }
2303
David Ahern6c973c92016-11-16 15:06:32 +09002304 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002305 if (thread == NULL) {
2306 rc = -1;
2307 goto out;
2308 }
2309
David Ahern52df1382016-11-16 15:06:30 +09002310 if (timehist_skip_sample(sched, thread))
David Ahern49394a22016-11-16 15:06:29 +09002311 goto out;
2312
2313 tr = thread__get_runtime(thread);
2314 if (tr == NULL) {
2315 rc = -1;
2316 goto out;
2317 }
2318
2319 tprev = perf_evsel__get_time(evsel, sample->cpu);
2320
2321 timehist_update_runtime_stats(tr, sample->time, tprev);
David Ahern52df1382016-11-16 15:06:30 +09002322 if (!sched->summary_only)
David Ahern6c973c92016-11-16 15:06:32 +09002323 timehist_print_sample(sched, sample, &al, thread);
David Ahern49394a22016-11-16 15:06:29 +09002324
2325out:
2326 if (tr) {
2327 /* time of this sched_switch event becomes last time task seen */
2328 tr->last_time = sample->time;
2329
2330 /* sched out event for task so reset ready to run time */
2331 tr->ready_to_run = 0;
2332 }
2333
2334 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2335
2336 return rc;
2337}
2338
2339static int timehist_sched_switch_event(struct perf_tool *tool,
2340 union perf_event *event,
2341 struct perf_evsel *evsel,
2342 struct perf_sample *sample,
2343 struct machine *machine __maybe_unused)
2344{
2345 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2346}
2347
2348static int process_lost(struct perf_tool *tool __maybe_unused,
2349 union perf_event *event,
2350 struct perf_sample *sample,
2351 struct machine *machine __maybe_unused)
2352{
2353 char tstr[64];
2354
2355 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2356 printf("%15s ", tstr);
2357 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2358
2359 return 0;
2360}
2361
2362
David Ahern52df1382016-11-16 15:06:30 +09002363static void print_thread_runtime(struct thread *t,
2364 struct thread_runtime *r)
2365{
2366 double mean = avg_stats(&r->run_stats);
2367 float stddev;
2368
2369 printf("%*s %5d %9" PRIu64 " ",
2370 comm_width, timehist_get_commstr(t), t->ppid,
2371 (u64) r->run_stats.n);
2372
2373 print_sched_time(r->total_run_time, 8);
2374 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2375 print_sched_time(r->run_stats.min, 6);
2376 printf(" ");
2377 print_sched_time((u64) mean, 6);
2378 printf(" ");
2379 print_sched_time(r->run_stats.max, 6);
2380 printf(" ");
2381 printf("%5.2f", stddev);
David Ahern350f54f2016-11-25 09:28:41 -07002382 printf(" %5" PRIu64, r->migrations);
David Ahern52df1382016-11-16 15:06:30 +09002383 printf("\n");
2384}
2385
2386struct total_run_stats {
2387 u64 sched_count;
2388 u64 task_count;
2389 u64 total_run_time;
2390};
2391
2392static int __show_thread_runtime(struct thread *t, void *priv)
2393{
2394 struct total_run_stats *stats = priv;
2395 struct thread_runtime *r;
2396
2397 if (thread__is_filtered(t))
2398 return 0;
2399
2400 r = thread__priv(t);
2401 if (r && r->run_stats.n) {
2402 stats->task_count++;
2403 stats->sched_count += r->run_stats.n;
2404 stats->total_run_time += r->total_run_time;
2405 print_thread_runtime(t, r);
2406 }
2407
2408 return 0;
2409}
2410
2411static int show_thread_runtime(struct thread *t, void *priv)
2412{
2413 if (t->dead)
2414 return 0;
2415
2416 return __show_thread_runtime(t, priv);
2417}
2418
2419static int show_deadthread_runtime(struct thread *t, void *priv)
2420{
2421 if (!t->dead)
2422 return 0;
2423
2424 return __show_thread_runtime(t, priv);
2425}
2426
2427static void timehist_print_summary(struct perf_sched *sched,
2428 struct perf_session *session)
2429{
2430 struct machine *m = &session->machines.host;
2431 struct total_run_stats totals;
2432 u64 task_count;
2433 struct thread *t;
2434 struct thread_runtime *r;
2435 int i;
2436
2437 memset(&totals, 0, sizeof(totals));
2438
2439 if (comm_width < 30)
2440 comm_width = 30;
2441
2442 printf("\nRuntime summary\n");
2443 printf("%*s parent sched-in ", comm_width, "comm");
David Ahern350f54f2016-11-25 09:28:41 -07002444 printf(" run-time min-run avg-run max-run stddev migrations\n");
David Ahern52df1382016-11-16 15:06:30 +09002445 printf("%*s (count) ", comm_width, "");
2446 printf(" (msec) (msec) (msec) (msec) %%\n");
David Ahern350f54f2016-11-25 09:28:41 -07002447 printf("%.117s\n", graph_dotted_line);
David Ahern52df1382016-11-16 15:06:30 +09002448
2449 machine__for_each_thread(m, show_thread_runtime, &totals);
2450 task_count = totals.task_count;
2451 if (!task_count)
2452 printf("<no still running tasks>\n");
2453
2454 printf("\nTerminated tasks:\n");
2455 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2456 if (task_count == totals.task_count)
2457 printf("<no terminated tasks>\n");
2458
2459 /* CPU idle stats not tracked when samples were skipped */
2460 if (sched->skipped_samples)
2461 return;
2462
2463 printf("\nIdle stats:\n");
2464 for (i = 0; i <= idle_max_cpu; ++i) {
2465 t = idle_threads[i];
2466 if (!t)
2467 continue;
2468
2469 r = thread__priv(t);
2470 if (r && r->run_stats.n) {
2471 totals.sched_count += r->run_stats.n;
2472 printf(" CPU %2d idle for ", i);
2473 print_sched_time(r->total_run_time, 6);
2474 printf(" msec\n");
2475 } else
2476 printf(" CPU %2d idle entire time window\n", i);
2477 }
2478
2479 printf("\n"
2480 " Total number of unique tasks: %" PRIu64 "\n"
2481 "Total number of context switches: %" PRIu64 "\n"
2482 " Total run time (msec): ",
2483 totals.task_count, totals.sched_count);
2484
2485 print_sched_time(totals.total_run_time, 2);
2486 printf("\n");
2487}
2488
David Ahern49394a22016-11-16 15:06:29 +09002489typedef int (*sched_handler)(struct perf_tool *tool,
2490 union perf_event *event,
2491 struct perf_evsel *evsel,
2492 struct perf_sample *sample,
2493 struct machine *machine);
2494
2495static int perf_timehist__process_sample(struct perf_tool *tool,
2496 union perf_event *event,
2497 struct perf_sample *sample,
2498 struct perf_evsel *evsel,
2499 struct machine *machine)
2500{
2501 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2502 int err = 0;
2503 int this_cpu = sample->cpu;
2504
2505 if (this_cpu > sched->max_cpu)
2506 sched->max_cpu = this_cpu;
2507
2508 if (evsel->handler != NULL) {
2509 sched_handler f = evsel->handler;
2510
2511 err = f(tool, event, evsel, sample, machine);
2512 }
2513
2514 return err;
2515}
2516
David Ahern6c973c92016-11-16 15:06:32 +09002517static int timehist_check_attr(struct perf_sched *sched,
2518 struct perf_evlist *evlist)
2519{
2520 struct perf_evsel *evsel;
2521 struct evsel_runtime *er;
2522
2523 list_for_each_entry(evsel, &evlist->entries, node) {
2524 er = perf_evsel__get_runtime(evsel);
2525 if (er == NULL) {
2526 pr_err("Failed to allocate memory for evsel runtime data\n");
2527 return -1;
2528 }
2529
2530 if (sched->show_callchain &&
2531 !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
2532 pr_info("Samples do not have callchains.\n");
2533 sched->show_callchain = 0;
2534 symbol_conf.use_callchain = 0;
2535 }
2536 }
2537
2538 return 0;
2539}
2540
David Ahern49394a22016-11-16 15:06:29 +09002541static int perf_sched__timehist(struct perf_sched *sched)
2542{
2543 const struct perf_evsel_str_handler handlers[] = {
2544 { "sched:sched_switch", timehist_sched_switch_event, },
2545 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2546 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2547 };
David Ahern350f54f2016-11-25 09:28:41 -07002548 const struct perf_evsel_str_handler migrate_handlers[] = {
2549 { "sched:sched_migrate_task", timehist_migrate_task_event, },
2550 };
David Ahern49394a22016-11-16 15:06:29 +09002551 struct perf_data_file file = {
2552 .path = input_name,
2553 .mode = PERF_DATA_MODE_READ,
2554 };
2555
2556 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002557 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002558 int err = -1;
2559
2560 /*
2561 * event handlers for timehist option
2562 */
2563 sched->tool.sample = perf_timehist__process_sample;
2564 sched->tool.mmap = perf_event__process_mmap;
2565 sched->tool.comm = perf_event__process_comm;
2566 sched->tool.exit = perf_event__process_exit;
2567 sched->tool.fork = perf_event__process_fork;
2568 sched->tool.lost = process_lost;
2569 sched->tool.attr = perf_event__process_attr;
2570 sched->tool.tracing_data = perf_event__process_tracing_data;
2571 sched->tool.build_id = perf_event__process_build_id;
2572
2573 sched->tool.ordered_events = true;
2574 sched->tool.ordering_requires_timestamps = true;
2575
David Ahern6c973c92016-11-16 15:06:32 +09002576 symbol_conf.use_callchain = sched->show_callchain;
2577
David Ahern49394a22016-11-16 15:06:29 +09002578 session = perf_session__new(&file, false, &sched->tool);
2579 if (session == NULL)
2580 return -ENOMEM;
2581
David Ahern52df1382016-11-16 15:06:30 +09002582 evlist = session->evlist;
2583
David Ahern49394a22016-11-16 15:06:29 +09002584 symbol__init(&session->header.env);
2585
David Ahern6c973c92016-11-16 15:06:32 +09002586 if (timehist_check_attr(sched, evlist) != 0)
2587 goto out;
2588
David Ahern49394a22016-11-16 15:06:29 +09002589 setup_pager();
2590
2591 /* setup per-evsel handlers */
2592 if (perf_session__set_tracepoints_handlers(session, handlers))
2593 goto out;
2594
2595 if (!perf_session__has_traces(session, "record -R"))
2596 goto out;
2597
David Ahern350f54f2016-11-25 09:28:41 -07002598 if (sched->show_migrations &&
2599 perf_session__set_tracepoints_handlers(session, migrate_handlers))
2600 goto out;
2601
David Ahern49394a22016-11-16 15:06:29 +09002602 /* pre-allocate struct for per-CPU idle stats */
2603 sched->max_cpu = session->header.env.nr_cpus_online;
2604 if (sched->max_cpu == 0)
2605 sched->max_cpu = 4;
2606 if (init_idle_threads(sched->max_cpu))
2607 goto out;
2608
David Ahern52df1382016-11-16 15:06:30 +09002609 /* summary_only implies summary option, but don't overwrite summary if set */
2610 if (sched->summary_only)
2611 sched->summary = sched->summary_only;
2612
2613 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09002614 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09002615
2616 err = perf_session__process_events(session);
2617 if (err) {
2618 pr_err("Failed to process events, error %d", err);
2619 goto out;
2620 }
2621
David Ahern52df1382016-11-16 15:06:30 +09002622 sched->nr_events = evlist->stats.nr_events[0];
2623 sched->nr_lost_events = evlist->stats.total_lost;
2624 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2625
2626 if (sched->summary)
2627 timehist_print_summary(sched, session);
2628
David Ahern49394a22016-11-16 15:06:29 +09002629out:
2630 free_idle_threads();
2631 perf_session__delete(session);
2632
2633 return err;
2634}
2635
2636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002637static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002638{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002639 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002640 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002641 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2642 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002643 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002644 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002645 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002646 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2647 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002648 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002649 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002650 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002651 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2652 sched->nr_context_switch_bugs, sched->nr_timestamps);
2653 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002654 printf(" (due to lost events?)");
2655 printf("\n");
2656 }
2657}
2658
Josef Bacik2f80dd42015-05-22 09:18:40 -04002659static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2660{
2661 struct rb_node **new = &(root->rb_node), *parent = NULL;
2662 struct work_atoms *this;
2663 const char *comm = thread__comm_str(data->thread), *this_comm;
2664
2665 while (*new) {
2666 int cmp;
2667
2668 this = container_of(*new, struct work_atoms, node);
2669 parent = *new;
2670
2671 this_comm = thread__comm_str(this->thread);
2672 cmp = strcmp(comm, this_comm);
2673 if (cmp > 0) {
2674 new = &((*new)->rb_left);
2675 } else if (cmp < 0) {
2676 new = &((*new)->rb_right);
2677 } else {
2678 this->num_merged++;
2679 this->total_runtime += data->total_runtime;
2680 this->nb_atoms += data->nb_atoms;
2681 this->total_lat += data->total_lat;
2682 list_splice(&data->work_list, &this->work_list);
2683 if (this->max_lat < data->max_lat) {
2684 this->max_lat = data->max_lat;
2685 this->max_lat_at = data->max_lat_at;
2686 }
2687 zfree(&data);
2688 return;
2689 }
2690 }
2691
2692 data->num_merged++;
2693 rb_link_node(&data->node, parent, new);
2694 rb_insert_color(&data->node, root);
2695}
2696
2697static void perf_sched__merge_lat(struct perf_sched *sched)
2698{
2699 struct work_atoms *data;
2700 struct rb_node *node;
2701
2702 if (sched->skip_merge)
2703 return;
2704
2705 while ((node = rb_first(&sched->atom_root))) {
2706 rb_erase(node, &sched->atom_root);
2707 data = rb_entry(node, struct work_atoms, node);
2708 __merge_work_atoms(&sched->merged_atom_root, data);
2709 }
2710}
2711
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002712static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002713{
2714 struct rb_node *next;
2715
2716 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002717
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002718 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002719 return -1;
David Ahernad9def72013-08-07 22:50:44 -04002720
Josef Bacik2f80dd42015-05-22 09:18:40 -04002721 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002722 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002723
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002724 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
2725 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
2726 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002727
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002728 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002729
2730 while (next) {
2731 struct work_atoms *work_list;
2732
2733 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002734 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002735 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002736 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002737 }
2738
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002739 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002740 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03002741 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002742
2743 printf(" ---------------------------------------------------\n");
2744
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002745 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002746 printf("\n");
2747
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002748 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002749}
2750
Jiri Olsa99623c62016-04-12 15:29:26 +02002751static int setup_map_cpus(struct perf_sched *sched)
2752{
Jiri Olsa73643bb2016-04-12 15:29:31 +02002753 struct cpu_map *map;
2754
Jiri Olsa99623c62016-04-12 15:29:26 +02002755 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
2756
2757 if (sched->map.comp) {
2758 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02002759 if (!sched->map.comp_cpus)
2760 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02002761 }
2762
Jiri Olsa73643bb2016-04-12 15:29:31 +02002763 if (!sched->map.cpus_str)
2764 return 0;
2765
2766 map = cpu_map__new(sched->map.cpus_str);
2767 if (!map) {
2768 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
2769 return -1;
2770 }
2771
2772 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02002773 return 0;
2774}
2775
Jiri Olsaa151a372016-04-12 15:29:29 +02002776static int setup_color_pids(struct perf_sched *sched)
2777{
2778 struct thread_map *map;
2779
2780 if (!sched->map.color_pids_str)
2781 return 0;
2782
2783 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
2784 if (!map) {
2785 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
2786 return -1;
2787 }
2788
2789 sched->map.color_pids = map;
2790 return 0;
2791}
2792
Jiri Olsacf294f22016-04-12 15:29:30 +02002793static int setup_color_cpus(struct perf_sched *sched)
2794{
2795 struct cpu_map *map;
2796
2797 if (!sched->map.color_cpus_str)
2798 return 0;
2799
2800 map = cpu_map__new(sched->map.color_cpus_str);
2801 if (!map) {
2802 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
2803 return -1;
2804 }
2805
2806 sched->map.color_cpus = map;
2807 return 0;
2808}
2809
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002810static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002811{
Jiri Olsa99623c62016-04-12 15:29:26 +02002812 if (setup_map_cpus(sched))
2813 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02002814
Jiri Olsaa151a372016-04-12 15:29:29 +02002815 if (setup_color_pids(sched))
2816 return -1;
2817
Jiri Olsacf294f22016-04-12 15:29:30 +02002818 if (setup_color_cpus(sched))
2819 return -1;
2820
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002821 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002822 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002823 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002824 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002825 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002826}
2827
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002828static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002829{
2830 unsigned long i;
2831
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002832 calibrate_run_measurement_overhead(sched);
2833 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002834
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002835 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002836
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002837 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002838 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002839
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002840 printf("nr_run_events: %ld\n", sched->nr_run_events);
2841 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
2842 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002843
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002844 if (sched->targetless_wakeups)
2845 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
2846 if (sched->multitarget_wakeups)
2847 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
2848 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002849 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002850 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002851
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002852 print_task_traces(sched);
2853 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002854
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002855 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002856 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002857 for (i = 0; i < sched->replay_repeat; i++)
2858 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002859
2860 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002861}
2862
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002863static void setup_sorting(struct perf_sched *sched, const struct option *options,
2864 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002865{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002866 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002867
2868 for (tok = strtok_r(str, ", ", &tmp);
2869 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002870 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002871 usage_with_options_msg(usage_msg, options,
2872 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002873 }
2874 }
2875
2876 free(str);
2877
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002878 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002879}
2880
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002881static int __cmd_record(int argc, const char **argv)
2882{
2883 unsigned int rec_argc, i, j;
2884 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002885 const char * const record_args[] = {
2886 "record",
2887 "-a",
2888 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002889 "-m", "1024",
2890 "-c", "1",
2891 "-e", "sched:sched_switch",
2892 "-e", "sched:sched_stat_wait",
2893 "-e", "sched:sched_stat_sleep",
2894 "-e", "sched:sched_stat_iowait",
2895 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002896 "-e", "sched:sched_process_fork",
2897 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09002898 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002899 "-e", "sched:sched_migrate_task",
2900 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002901
2902 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
2903 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2904
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02002905 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11002906 return -ENOMEM;
2907
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002908 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2909 rec_argv[i] = strdup(record_args[i]);
2910
2911 for (j = 1; j < (unsigned int)argc; j++, i++)
2912 rec_argv[i] = argv[j];
2913
2914 BUG_ON(i != rec_argc);
2915
2916 return cmd_record(i, rec_argv, NULL);
2917}
2918
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002919int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002920{
Adrian Hunter8a39df82013-10-22 10:34:15 +03002921 const char default_sort_order[] = "avg, max, switch, runtime";
2922 struct perf_sched sched = {
2923 .tool = {
2924 .sample = perf_sched__process_tracepoint_sample,
2925 .comm = perf_event__process_comm,
2926 .lost = perf_event__process_lost,
2927 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002928 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002929 },
2930 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
2931 .sort_list = LIST_HEAD_INIT(sched.sort_list),
2932 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
2933 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002934 .sort_order = default_sort_order,
2935 .replay_repeat = 10,
2936 .profile_cpu = -1,
2937 .next_shortname1 = 'A',
2938 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04002939 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09002940 .show_callchain = 1,
2941 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002942 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09002943 const struct option sched_options[] = {
2944 OPT_STRING('i', "input", &input_name, "file",
2945 "input file name"),
2946 OPT_INCR('v', "verbose", &verbose,
2947 "be more verbose (show symbol address, etc)"),
2948 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2949 "dump raw trace in ASCII"),
2950 OPT_END()
2951 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002952 const struct option latency_options[] = {
2953 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
2954 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002955 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
2956 "CPU to profile on"),
2957 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2958 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04002959 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
2960 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002961 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002962 };
2963 const struct option replay_options[] = {
2964 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
2965 "repeat the workload replay N times (-1: infinite)"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002966 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2967 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08002968 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002969 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002970 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002971 const struct option map_options[] = {
2972 OPT_BOOLEAN(0, "compact", &sched.map.comp,
2973 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02002974 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
2975 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02002976 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
2977 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02002978 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
2979 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002980 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02002981 };
David Ahern49394a22016-11-16 15:06:29 +09002982 const struct option timehist_options[] = {
2983 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2984 "file", "vmlinux pathname"),
2985 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2986 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09002987 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
2988 "Display call chains if present (default on)"),
2989 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
2990 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09002991 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2992 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09002993 OPT_BOOLEAN('s', "summary", &sched.summary_only,
2994 "Show only syscall summary with statistics"),
2995 OPT_BOOLEAN('S', "with-summary", &sched.summary,
2996 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09002997 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Ahern350f54f2016-11-25 09:28:41 -07002998 OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
David Aherna407b062016-11-16 15:06:33 +09002999 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
David Ahern49394a22016-11-16 15:06:29 +09003000 OPT_PARENT(sched_options)
3001 };
3002
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003003 const char * const latency_usage[] = {
3004 "perf sched latency [<options>]",
3005 NULL
3006 };
3007 const char * const replay_usage[] = {
3008 "perf sched replay [<options>]",
3009 NULL
3010 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003011 const char * const map_usage[] = {
3012 "perf sched map [<options>]",
3013 NULL
3014 };
David Ahern49394a22016-11-16 15:06:29 +09003015 const char * const timehist_usage[] = {
3016 "perf sched timehist [<options>]",
3017 NULL
3018 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003019 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09003020 "replay", "script",
3021 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003022 const char *sched_usage[] = {
3023 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003024 NULL
3025 };
3026 struct trace_sched_handler lat_ops = {
3027 .wakeup_event = latency_wakeup_event,
3028 .switch_event = latency_switch_event,
3029 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003030 .migrate_task_event = latency_migrate_task_event,
3031 };
3032 struct trace_sched_handler map_ops = {
3033 .switch_event = map_switch_event,
3034 };
3035 struct trace_sched_handler replay_ops = {
3036 .wakeup_event = replay_wakeup_event,
3037 .switch_event = replay_switch_event,
3038 .fork_event = replay_fork_event,
3039 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03003040 unsigned int i;
3041
3042 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3043 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003044
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003045 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3046 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003047 if (!argc)
3048 usage_with_options(sched_usage, sched_options);
3049
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003050 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003051 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003052 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003053 if (!strcmp(argv[0], "script"))
3054 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003055
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003056 if (!strncmp(argv[0], "rec", 3)) {
3057 return __cmd_record(argc, argv);
3058 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003059 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003060 if (argc > 1) {
3061 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3062 if (argc)
3063 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003064 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003065 setup_sorting(&sched, latency_options, latency_usage);
3066 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003067 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02003068 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02003069 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02003070 if (argc)
3071 usage_with_options(map_usage, map_options);
3072 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003073 sched.tp_handler = &map_ops;
3074 setup_sorting(&sched, latency_options, latency_usage);
3075 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003076 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003077 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003078 if (argc) {
3079 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3080 if (argc)
3081 usage_with_options(replay_usage, replay_options);
3082 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003083 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09003084 } else if (!strcmp(argv[0], "timehist")) {
3085 if (argc) {
3086 argc = parse_options(argc, argv, timehist_options,
3087 timehist_usage, 0);
3088 if (argc)
3089 usage_with_options(timehist_usage, timehist_options);
3090 }
David Ahernfc1469f2016-11-16 15:06:31 +09003091 if (sched.show_wakeups && sched.summary_only) {
3092 pr_err(" Error: -s and -w are mutually exclusive.\n");
3093 parse_options_usage(timehist_usage, timehist_options, "s", true);
3094 parse_options_usage(NULL, timehist_options, "w", true);
3095 return -EINVAL;
3096 }
3097
David Ahern49394a22016-11-16 15:06:29 +09003098 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003099 } else {
3100 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003101 }
3102
Ingo Molnarec156762009-09-11 12:12:54 +02003103 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003104}