blob: 8fb7bcc2cb76b990be1dc2715e6cb9d573fa536b [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"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020017
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060018#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020019#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020020
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021#include "util/debug.h"
22
David Ahern49394a22016-11-16 15:06:29 +090023#include <linux/log2.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020024#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020025#include <sys/resource.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020026
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020027#include <semaphore.h>
28#include <pthread.h>
29#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080030#include <api/fs/fs.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030031#include <linux/time64.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020032
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020033#define PR_SET_NAME 15 /* Set process name */
34#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020035#define COMM_LEN 20
36#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080037#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020038
mingo39aeb522009-09-14 20:04:48 +020039struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020040
41struct task_desc {
42 unsigned long nr;
43 unsigned long pid;
44 char comm[COMM_LEN];
45
46 unsigned long nr_events;
47 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020048 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020049
50 pthread_t thread;
51 sem_t sleep_sem;
52
53 sem_t ready_for_work;
54 sem_t work_done_sem;
55
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020056 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020057};
58
59enum sched_event_type {
60 SCHED_EVENT_RUN,
61 SCHED_EVENT_SLEEP,
62 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020063 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020064};
65
mingo39aeb522009-09-14 20:04:48 +020066struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020067 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030068 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020069 u64 timestamp;
70 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020071 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020072 sem_t *wait_sem;
73 struct task_desc *wakee;
74};
75
Dongshenge936e8e2014-05-05 16:05:54 +090076#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020077
78enum thread_state {
79 THREAD_SLEEPING = 0,
80 THREAD_WAIT_CPU,
81 THREAD_SCHED_IN,
82 THREAD_IGNORE
83};
84
85struct work_atom {
86 struct list_head list;
87 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +020088 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020089 u64 wake_up_time;
90 u64 sched_in_time;
91 u64 runtime;
92};
93
mingo39aeb522009-09-14 20:04:48 +020094struct work_atoms {
95 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020096 struct thread *thread;
97 struct rb_node node;
98 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +010099 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200100 u64 total_lat;
101 u64 nb_atoms;
102 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400103 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200104};
105
mingo39aeb522009-09-14 20:04:48 +0200106typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200107
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300108struct perf_sched;
109
110struct trace_sched_handler {
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300111 int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel,
112 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300113
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300114 int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel,
115 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300116
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300117 int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel,
118 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300119
David Aherncb627502013-08-07 22:50:47 -0400120 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
121 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
122 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300123
124 int (*migrate_task_event)(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300125 struct perf_evsel *evsel,
126 struct perf_sample *sample,
127 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300128};
129
Jiri Olsaa151a372016-04-12 15:29:29 +0200130#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200131#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200132
Jiri Olsa99623c62016-04-12 15:29:26 +0200133struct perf_sched_map {
134 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
135 int *comp_cpus;
136 bool comp;
Jiri Olsaa151a372016-04-12 15:29:29 +0200137 struct thread_map *color_pids;
138 const char *color_pids_str;
Jiri Olsacf294f22016-04-12 15:29:30 +0200139 struct cpu_map *color_cpus;
140 const char *color_cpus_str;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200141 struct cpu_map *cpus;
142 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200143};
144
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300145struct perf_sched {
146 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300147 const char *sort_order;
148 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800149 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300150 struct task_desc **tasks;
151 const struct trace_sched_handler *tp_handler;
152 pthread_mutex_t start_work_mutex;
153 pthread_mutex_t work_done_wait_mutex;
154 int profile_cpu;
155/*
156 * Track the current task - that way we can know whether there's any
157 * weird events, such as a task being switched away that is not current.
158 */
159 int max_cpu;
160 u32 curr_pid[MAX_CPUS];
161 struct thread *curr_thread[MAX_CPUS];
162 char next_shortname1;
163 char next_shortname2;
164 unsigned int replay_repeat;
165 unsigned long nr_run_events;
166 unsigned long nr_sleep_events;
167 unsigned long nr_wakeup_events;
168 unsigned long nr_sleep_corrections;
169 unsigned long nr_run_events_optimized;
170 unsigned long targetless_wakeups;
171 unsigned long multitarget_wakeups;
172 unsigned long nr_runs;
173 unsigned long nr_timestamps;
174 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300175 unsigned long nr_context_switch_bugs;
176 unsigned long nr_events;
177 unsigned long nr_lost_chunks;
178 unsigned long nr_lost_events;
179 u64 run_measurement_overhead;
180 u64 sleep_measurement_overhead;
181 u64 start_time;
182 u64 cpu_usage;
183 u64 runavg_cpu_usage;
184 u64 parent_cpu_usage;
185 u64 runavg_parent_cpu_usage;
186 u64 sum_runtime;
187 u64 sum_fluct;
188 u64 run_avg;
189 u64 all_runtime;
190 u64 all_count;
191 u64 cpu_last_switched[MAX_CPUS];
Josef Bacik2f80dd42015-05-22 09:18:40 -0400192 struct rb_root atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300193 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800194 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400195 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200196 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900197
198 /* options for timehist command */
199 bool summary;
200 bool summary_only;
David Ahernfc1469f2016-11-16 15:06:31 +0900201 bool show_wakeups;
David Ahern52df1382016-11-16 15:06:30 +0900202 u64 skipped_samples;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300203};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200204
David Ahern49394a22016-11-16 15:06:29 +0900205/* per thread run time data */
206struct thread_runtime {
207 u64 last_time; /* time of previous sched in/out event */
208 u64 dt_run; /* run time */
209 u64 dt_wait; /* time between CPU access (off cpu) */
210 u64 dt_delay; /* time between wakeup and sched-in */
211 u64 ready_to_run; /* time of wakeup */
212
213 struct stats run_stats;
214 u64 total_run_time;
215};
216
217/* per event run time data */
218struct evsel_runtime {
219 u64 *last_time; /* time this event was last seen per cpu */
220 u32 ncpu; /* highest cpu slot allocated */
221};
222
223/* track idle times per cpu */
224static struct thread **idle_threads;
225static int idle_max_cpu;
226static char idle_comm[] = "<idle>";
227
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200228static u64 get_nsecs(void)
229{
230 struct timespec ts;
231
232 clock_gettime(CLOCK_MONOTONIC, &ts);
233
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300234 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200235}
236
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300237static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200238{
239 u64 T0 = get_nsecs(), T1;
240
241 do {
242 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300243 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200244}
245
246static void sleep_nsecs(u64 nsecs)
247{
248 struct timespec ts;
249
250 ts.tv_nsec = nsecs % 999999999;
251 ts.tv_sec = nsecs / 999999999;
252
253 nanosleep(&ts, NULL);
254}
255
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300256static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200257{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300258 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200259 int i;
260
261 for (i = 0; i < 10; i++) {
262 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300263 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200264 T1 = get_nsecs();
265 delta = T1-T0;
266 min_delta = min(min_delta, delta);
267 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300268 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200269
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200270 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200271}
272
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300273static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200274{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300275 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200276 int i;
277
278 for (i = 0; i < 10; i++) {
279 T0 = get_nsecs();
280 sleep_nsecs(10000);
281 T1 = get_nsecs();
282 delta = T1-T0;
283 min_delta = min(min_delta, delta);
284 }
285 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300286 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200287
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200288 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200289}
290
mingo39aeb522009-09-14 20:04:48 +0200291static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200292get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200293{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200294 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200295 unsigned long idx = task->nr_events;
296 size_t size;
297
298 event->timestamp = timestamp;
299 event->nr = idx;
300
301 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200302 size = sizeof(struct sched_atom *) * task->nr_events;
303 task->atoms = realloc(task->atoms, size);
304 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200305
mingo39aeb522009-09-14 20:04:48 +0200306 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200307
308 return event;
309}
310
mingo39aeb522009-09-14 20:04:48 +0200311static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200312{
313 if (!task->nr_events)
314 return NULL;
315
mingo39aeb522009-09-14 20:04:48 +0200316 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200317}
318
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300319static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
320 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200321{
mingo39aeb522009-09-14 20:04:48 +0200322 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200323
324 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200325 * optimize an existing RUN event by merging this one
326 * to it:
327 */
Ingo Molnarec156762009-09-11 12:12:54 +0200328 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300329 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200330 curr_event->duration += duration;
331 return;
332 }
333
334 event = get_new_event(task, timestamp);
335
336 event->type = SCHED_EVENT_RUN;
337 event->duration = duration;
338
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300339 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200340}
341
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300342static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
343 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200344{
mingo39aeb522009-09-14 20:04:48 +0200345 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200346
347 event = get_new_event(task, timestamp);
348 event->type = SCHED_EVENT_WAKEUP;
349 event->wakee = wakee;
350
351 wakee_event = last_event(wakee);
352 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300353 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200354 return;
355 }
356 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300357 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200358 return;
359 }
360
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200361 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200362 sem_init(wakee_event->wait_sem, 0, 0);
363 wakee_event->specific_wait = 1;
364 event->wait_sem = wakee_event->wait_sem;
365
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300366 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200367}
368
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300369static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
370 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200371{
mingo39aeb522009-09-14 20:04:48 +0200372 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200373
374 event->type = SCHED_EVENT_SLEEP;
375
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300376 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200377}
378
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300379static struct task_desc *register_pid(struct perf_sched *sched,
380 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200381{
382 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800383 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200384
Yunlong Songcb06ac22015-03-31 21:46:30 +0800385 if (sched->pid_to_task == NULL) {
386 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
387 pid_max = MAX_PID;
388 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
389 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800390 if (pid >= (unsigned long)pid_max) {
391 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
392 sizeof(struct task_desc *))) == NULL);
393 while (pid >= (unsigned long)pid_max)
394 sched->pid_to_task[pid_max++] = NULL;
395 }
Ingo Molnarec156762009-09-11 12:12:54 +0200396
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300397 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200398
399 if (task)
400 return task;
401
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200402 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200403 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300404 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200405 strcpy(task->comm, comm);
406 /*
407 * every task starts in sleeping state - this gets ignored
408 * if there's no wakeup pointing to this sleep state:
409 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300410 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200411
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300412 sched->pid_to_task[pid] = task;
413 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800414 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415 BUG_ON(!sched->tasks);
416 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200417
Ingo Molnarad236fd2009-09-11 12:12:54 +0200418 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300419 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200420
421 return task;
422}
423
424
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300425static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200426{
427 struct task_desc *task;
428 unsigned long i;
429
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300430 for (i = 0; i < sched->nr_tasks; i++) {
431 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200432 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200433 task->nr, task->comm, task->pid, task->nr_events);
434 }
435}
436
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300437static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200438{
439 struct task_desc *task1, *task2;
440 unsigned long i, j;
441
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300442 for (i = 0; i < sched->nr_tasks; i++) {
443 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200444 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300445 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200446 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300447 task2 = sched->tasks[j];
448 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200449 }
450}
451
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300452static void perf_sched__process_event(struct perf_sched *sched,
453 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200454{
455 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200456
mingo39aeb522009-09-14 20:04:48 +0200457 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200458 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300459 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200460 break;
461 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200462 if (atom->wait_sem)
463 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200464 BUG_ON(ret);
465 break;
466 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200467 if (atom->wait_sem)
468 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200469 BUG_ON(ret);
470 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200471 case SCHED_EVENT_MIGRATION:
472 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200473 default:
474 BUG_ON(1);
475 }
476}
477
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200478static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200479{
480 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200481 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200482 int err;
483
484 err = getrusage(RUSAGE_SELF, &ru);
485 BUG_ON(err);
486
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300487 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
488 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200489
490 return sum;
491}
492
Yunlong Song939cda52015-03-31 21:46:34 +0800493static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200494{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800495 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800496 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800497 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800498 struct rlimit limit;
499 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800500
501 memset(&attr, 0, sizeof(attr));
502
503 attr.type = PERF_TYPE_SOFTWARE;
504 attr.config = PERF_COUNT_SW_TASK_CLOCK;
505
Yunlong Song939cda52015-03-31 21:46:34 +0800506force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200507 fd = sys_perf_event_open(&attr, 0, -1, -1,
508 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800509
Yunlong Song1aff59b2015-03-31 21:46:33 +0800510 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800511 if (errno == EMFILE) {
512 if (sched->force) {
513 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
514 limit.rlim_cur += sched->nr_tasks - cur_task;
515 if (limit.rlim_cur > limit.rlim_max) {
516 limit.rlim_max = limit.rlim_cur;
517 need_privilege = true;
518 }
519 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
520 if (need_privilege && errno == EPERM)
521 strcpy(info, "Need privilege\n");
522 } else
523 goto force_again;
524 } else
525 strcpy(info, "Have a try with -f option\n");
526 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900527 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800528 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300529 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800530 exit(EXIT_FAILURE);
531 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800532 return fd;
533}
534
535static u64 get_cpu_usage_nsec_self(int fd)
536{
537 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200538 int ret;
539
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800540 ret = read(fd, &runtime, sizeof(runtime));
541 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200542
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800543 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200544}
545
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300546struct sched_thread_parms {
547 struct task_desc *task;
548 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800549 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300550};
551
Ingo Molnarec156762009-09-11 12:12:54 +0200552static void *thread_func(void *ctx)
553{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300554 struct sched_thread_parms *parms = ctx;
555 struct task_desc *this_task = parms->task;
556 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200557 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200558 unsigned long i, ret;
559 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800560 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200561
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300562 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300563
Ingo Molnarec156762009-09-11 12:12:54 +0200564 sprintf(comm2, ":%s", this_task->comm);
565 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300566 if (fd < 0)
567 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200568again:
569 ret = sem_post(&this_task->ready_for_work);
570 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300571 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200572 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300573 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200574 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200575
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800576 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200577
578 for (i = 0; i < this_task->nr_events; i++) {
579 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300580 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200581 }
582
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800583 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200584 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200585 ret = sem_post(&this_task->work_done_sem);
586 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200587
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300588 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200589 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300590 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200591 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200592
593 goto again;
594}
595
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300596static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200597{
598 struct task_desc *task;
599 pthread_attr_t attr;
600 unsigned long i;
601 int err;
602
603 err = pthread_attr_init(&attr);
604 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200605 err = pthread_attr_setstacksize(&attr,
606 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200607 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300608 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200609 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300610 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200611 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612 for (i = 0; i < sched->nr_tasks; i++) {
613 struct sched_thread_parms *parms = malloc(sizeof(*parms));
614 BUG_ON(parms == NULL);
615 parms->task = task = sched->tasks[i];
616 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800617 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200618 sem_init(&task->sleep_sem, 0, 0);
619 sem_init(&task->ready_for_work, 0, 0);
620 sem_init(&task->work_done_sem, 0, 0);
621 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300622 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200623 BUG_ON(err);
624 }
625}
626
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300627static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200628{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200629 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200630 struct task_desc *task;
631 unsigned long i, ret;
632
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300633 sched->start_time = get_nsecs();
634 sched->cpu_usage = 0;
635 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200636
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300637 for (i = 0; i < sched->nr_tasks; i++) {
638 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200639 ret = sem_wait(&task->ready_for_work);
640 BUG_ON(ret);
641 sem_init(&task->ready_for_work, 0, 0);
642 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300643 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200644 BUG_ON(ret);
645
646 cpu_usage_0 = get_cpu_usage_nsec_parent();
647
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300648 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200649
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 for (i = 0; i < sched->nr_tasks; i++) {
651 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200652 ret = sem_wait(&task->work_done_sem);
653 BUG_ON(ret);
654 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300655 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200656 task->cpu_usage = 0;
657 }
658
659 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300660 if (!sched->runavg_cpu_usage)
661 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800662 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 +0200663
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300664 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
665 if (!sched->runavg_parent_cpu_usage)
666 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800667 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
668 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200669
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200671 BUG_ON(ret);
672
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300673 for (i = 0; i < sched->nr_tasks; i++) {
674 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200675 sem_init(&task->sleep_sem, 0, 0);
676 task->curr_event = 0;
677 }
678}
679
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300680static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200681{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500682 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200683
684 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300685 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200686 T1 = get_nsecs();
687
688 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689 sched->sum_runtime += delta;
690 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200691
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300692 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200693 if (delta < avg_delta)
694 fluct = avg_delta - delta;
695 else
696 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300697 sched->sum_fluct += fluct;
698 if (!sched->run_avg)
699 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800700 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200701
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300702 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200703
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300704 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200705
Ingo Molnarad236fd2009-09-11 12:12:54 +0200706 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300707 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200708
709#if 0
710 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200711 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300712 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200713 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200714 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300715 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
716 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200717#endif
718
Ingo Molnarad236fd2009-09-11 12:12:54 +0200719 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200720
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300721 if (sched->nr_sleep_corrections)
722 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
723 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200724}
725
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300726static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200727{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200728 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200729
730 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300731 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200732 T1 = get_nsecs();
733
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200734 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200735
736 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300737 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200738 T1 = get_nsecs();
739
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200740 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200741}
742
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300743static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300744replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300745 struct perf_evsel *evsel, struct perf_sample *sample,
746 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200747{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300748 const char *comm = perf_evsel__strval(evsel, sample, "comm");
749 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200750 struct task_desc *waker, *wakee;
751
752 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300753 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200754
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300755 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200756 }
757
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300758 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300759 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200760
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300761 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300762 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200763}
764
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300765static int replay_switch_event(struct perf_sched *sched,
766 struct perf_evsel *evsel,
767 struct perf_sample *sample,
768 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200769{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300770 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
771 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
772 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
773 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
774 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300775 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300776 u64 timestamp0, timestamp = sample->time;
777 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200778 s64 delta;
779
Ingo Molnarad236fd2009-09-11 12:12:54 +0200780 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300781 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200782
Ingo Molnarfbf94822009-09-11 12:12:54 +0200783 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300784 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200785
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300786 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200787 if (timestamp0)
788 delta = timestamp - timestamp0;
789 else
790 delta = 0;
791
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300792 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900793 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300794 return -1;
795 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200796
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300797 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
798 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200799
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300800 prev = register_pid(sched, prev_pid, prev_comm);
801 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200802
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300803 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200804
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300805 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300806 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300807
808 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200809}
810
David Aherncb627502013-08-07 22:50:47 -0400811static int replay_fork_event(struct perf_sched *sched,
812 union perf_event *event,
813 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200814{
David Aherncb627502013-08-07 22:50:47 -0400815 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300816
Adrian Hunter314add62013-08-27 11:23:03 +0300817 child = machine__findnew_thread(machine, event->fork.pid,
818 event->fork.tid);
819 parent = machine__findnew_thread(machine, event->fork.ppid,
820 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400821
822 if (child == NULL || parent == NULL) {
823 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
824 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300825 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200826 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300827
David Aherncb627502013-08-07 22:50:47 -0400828 if (verbose) {
829 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200830 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
831 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400832 }
833
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200834 register_pid(sched, parent->tid, thread__comm_str(parent));
835 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300836out_put:
837 thread__put(child);
838 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300839 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200840}
841
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200842struct sort_dimension {
843 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200844 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200845 struct list_head list;
846};
847
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200848static int
mingo39aeb522009-09-14 20:04:48 +0200849thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200850{
851 struct sort_dimension *sort;
852 int ret = 0;
853
Ingo Molnarb5fae122009-09-11 12:12:54 +0200854 BUG_ON(list_empty(list));
855
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200856 list_for_each_entry(sort, list, list) {
857 ret = sort->cmp(l, r);
858 if (ret)
859 return ret;
860 }
861
862 return ret;
863}
864
mingo39aeb522009-09-14 20:04:48 +0200865static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200866thread_atoms_search(struct rb_root *root, struct thread *thread,
867 struct list_head *sort_list)
868{
869 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200870 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200871
872 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200873 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200874 int cmp;
875
mingo39aeb522009-09-14 20:04:48 +0200876 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200877
878 cmp = thread_lat_cmp(sort_list, &key, atoms);
879 if (cmp > 0)
880 node = node->rb_left;
881 else if (cmp < 0)
882 node = node->rb_right;
883 else {
884 BUG_ON(thread != atoms->thread);
885 return atoms;
886 }
887 }
888 return NULL;
889}
890
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200891static void
mingo39aeb522009-09-14 20:04:48 +0200892__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200893 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200894{
895 struct rb_node **new = &(root->rb_node), *parent = NULL;
896
897 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200898 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200899 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200900
mingo39aeb522009-09-14 20:04:48 +0200901 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200902 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200903
904 cmp = thread_lat_cmp(sort_list, data, this);
905
906 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200908 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200909 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200910 }
911
912 rb_link_node(&data->node, parent, new);
913 rb_insert_color(&data->node, root);
914}
915
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300916static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200917{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200918 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300919 if (!atoms) {
920 pr_err("No memory at %s\n", __func__);
921 return -1;
922 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200923
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300924 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200925 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300926 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300927 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200928}
929
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300930static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200931{
932 const char *str = TASK_STATE_TO_CHAR_STR;
933
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300934 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200935}
936
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300937static int
mingo39aeb522009-09-14 20:04:48 +0200938add_sched_out_event(struct work_atoms *atoms,
939 char run_state,
940 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200941{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200942 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300943 if (!atom) {
944 pr_err("Non memory at %s", __func__);
945 return -1;
946 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200947
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200948 atom->sched_out_time = timestamp;
949
mingo39aeb522009-09-14 20:04:48 +0200950 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200951 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200952 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200953 }
954
mingo39aeb522009-09-14 20:04:48 +0200955 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300956 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200957}
958
959static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300960add_runtime_event(struct work_atoms *atoms, u64 delta,
961 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200962{
963 struct work_atom *atom;
964
965 BUG_ON(list_empty(&atoms->work_list));
966
967 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
968
969 atom->runtime += delta;
970 atoms->total_runtime += delta;
971}
972
973static void
974add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200975{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200976 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200977 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200978
mingo39aeb522009-09-14 20:04:48 +0200979 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200980 return;
981
mingo39aeb522009-09-14 20:04:48 +0200982 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200983
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200984 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200985 return;
986
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200987 if (timestamp < atom->wake_up_time) {
988 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200989 return;
990 }
991
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200992 atom->state = THREAD_SCHED_IN;
993 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200994
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200995 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200996 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100997 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200998 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100999 atoms->max_lat_at = timestamp;
1000 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001001 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001002}
1003
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001004static int latency_switch_event(struct perf_sched *sched,
1005 struct perf_evsel *evsel,
1006 struct perf_sample *sample,
1007 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001008{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001009 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1010 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1011 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001012 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001013 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001014 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001015 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001016 s64 delta;
1017
mingo39aeb522009-09-14 20:04:48 +02001018 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001019
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001020 timestamp0 = sched->cpu_last_switched[cpu];
1021 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001022 if (timestamp0)
1023 delta = timestamp - timestamp0;
1024 else
1025 delta = 0;
1026
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001027 if (delta < 0) {
1028 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1029 return -1;
1030 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001031
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001032 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1033 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001034 if (sched_out == NULL || sched_in == NULL)
1035 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001036
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001037 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001038 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001039 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001040 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001041 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001042 if (!out_events) {
1043 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001044 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001045 }
mingo39aeb522009-09-14 20:04:48 +02001046 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001047 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001048 return -1;
mingo39aeb522009-09-14 20:04:48 +02001049
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001050 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001051 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001052 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001053 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001054 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001055 if (!in_events) {
1056 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001057 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001058 }
mingo39aeb522009-09-14 20:04:48 +02001059 /*
1060 * Take came in we have not heard about yet,
1061 * add in an initial atom in runnable state:
1062 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001063 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001064 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001065 }
1066 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001067 err = 0;
1068out_put:
1069 thread__put(sched_out);
1070 thread__put(sched_in);
1071 return err;
mingo39aeb522009-09-14 20:04:48 +02001072}
1073
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001074static int latency_runtime_event(struct perf_sched *sched,
1075 struct perf_evsel *evsel,
1076 struct perf_sample *sample,
1077 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001078{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001079 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1080 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001081 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001082 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001083 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001084 int cpu = sample->cpu, err = -1;
1085
1086 if (thread == NULL)
1087 return -1;
mingo39aeb522009-09-14 20:04:48 +02001088
1089 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001090 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001091 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001092 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001093 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001094 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001095 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001096 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001097 }
1098 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001099 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001100 }
1101
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001102 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001103 err = 0;
1104out_put:
1105 thread__put(thread);
1106 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001107}
1108
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001109static int latency_wakeup_event(struct perf_sched *sched,
1110 struct perf_evsel *evsel,
1111 struct perf_sample *sample,
1112 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001113{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001114 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001115 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001116 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001117 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001118 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001119 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001120
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001121 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001122 if (wakee == NULL)
1123 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001124 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001125 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001126 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001127 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001128 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001129 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001130 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001131 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001132 }
1133 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001134 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001135 }
1136
mingo39aeb522009-09-14 20:04:48 +02001137 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001138
mingo39aeb522009-09-14 20:04:48 +02001139 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001140
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001141 /*
Dongsheng Yang67d62592014-05-13 10:38:21 +09001142 * As we do not guarantee the wakeup event happens when
1143 * task is out of run queue, also may happen when task is
1144 * on run queue and wakeup only change ->state to TASK_RUNNING,
1145 * then we should not set the ->wake_up_time when wake up a
1146 * task which is on run queue.
1147 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001148 * You WILL be missing events if you've recorded only
1149 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d62592014-05-13 10:38:21 +09001150 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001151 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001152 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001153 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001154
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001155 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001156 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001157 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001158 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001159 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001160
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001161 atom->state = THREAD_WAIT_CPU;
1162 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001163out_ok:
1164 err = 0;
1165out_put:
1166 thread__put(wakee);
1167 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001168}
1169
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001170static int latency_migrate_task_event(struct perf_sched *sched,
1171 struct perf_evsel *evsel,
1172 struct perf_sample *sample,
1173 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001174{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001175 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001176 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001177 struct work_atoms *atoms;
1178 struct work_atom *atom;
1179 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001180 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001181
1182 /*
1183 * Only need to worry about migration when profiling one CPU.
1184 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001185 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001186 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001187
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001188 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001189 if (migrant == NULL)
1190 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001191 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001192 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001193 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001194 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001195 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001196 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001197 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001198 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001199 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001200 }
1201 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001202 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001203 }
1204
1205 BUG_ON(list_empty(&atoms->work_list));
1206
1207 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1208 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1209
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001210 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001211
1212 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001213 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001214 err = 0;
1215out_put:
1216 thread__put(migrant);
1217 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001218}
1219
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001220static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001221{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001222 int i;
1223 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001224 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001225 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001226
mingo39aeb522009-09-14 20:04:48 +02001227 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001228 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001229 /*
1230 * Ignore idle threads:
1231 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001232 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001233 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001234
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001235 sched->all_runtime += work_list->total_runtime;
1236 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001237
Josef Bacik2f80dd42015-05-22 09:18:40 -04001238 if (work_list->num_merged > 1)
1239 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1240 else
1241 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001242
mingo08f69e62009-09-14 18:30:44 +02001243 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001244 printf(" ");
1245
mingo39aeb522009-09-14 20:04:48 +02001246 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001247 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001248
Namhyung Kim99620a52016-10-24 11:02:45 +09001249 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 -03001250 (double)work_list->total_runtime / NSEC_PER_MSEC,
1251 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1252 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001253 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001254}
1255
mingo39aeb522009-09-14 20:04:48 +02001256static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001257{
Jiri Olsa0014de12015-11-02 12:10:25 +01001258 if (l->thread == r->thread)
1259 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001260 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001261 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001262 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001263 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001264 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001265}
1266
mingo39aeb522009-09-14 20:04:48 +02001267static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001268{
1269 u64 avgl, avgr;
1270
1271 if (!l->nb_atoms)
1272 return -1;
1273
1274 if (!r->nb_atoms)
1275 return 1;
1276
1277 avgl = l->total_lat / l->nb_atoms;
1278 avgr = r->total_lat / r->nb_atoms;
1279
1280 if (avgl < avgr)
1281 return -1;
1282 if (avgl > avgr)
1283 return 1;
1284
1285 return 0;
1286}
1287
mingo39aeb522009-09-14 20:04:48 +02001288static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001289{
1290 if (l->max_lat < r->max_lat)
1291 return -1;
1292 if (l->max_lat > r->max_lat)
1293 return 1;
1294
1295 return 0;
1296}
1297
mingo39aeb522009-09-14 20:04:48 +02001298static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001299{
1300 if (l->nb_atoms < r->nb_atoms)
1301 return -1;
1302 if (l->nb_atoms > r->nb_atoms)
1303 return 1;
1304
1305 return 0;
1306}
1307
mingo39aeb522009-09-14 20:04:48 +02001308static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001309{
1310 if (l->total_runtime < r->total_runtime)
1311 return -1;
1312 if (l->total_runtime > r->total_runtime)
1313 return 1;
1314
1315 return 0;
1316}
1317
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001318static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001319{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001320 size_t i;
1321 static struct sort_dimension avg_sort_dimension = {
1322 .name = "avg",
1323 .cmp = avg_cmp,
1324 };
1325 static struct sort_dimension max_sort_dimension = {
1326 .name = "max",
1327 .cmp = max_cmp,
1328 };
1329 static struct sort_dimension pid_sort_dimension = {
1330 .name = "pid",
1331 .cmp = pid_cmp,
1332 };
1333 static struct sort_dimension runtime_sort_dimension = {
1334 .name = "runtime",
1335 .cmp = runtime_cmp,
1336 };
1337 static struct sort_dimension switch_sort_dimension = {
1338 .name = "switch",
1339 .cmp = switch_cmp,
1340 };
1341 struct sort_dimension *available_sorts[] = {
1342 &pid_sort_dimension,
1343 &avg_sort_dimension,
1344 &max_sort_dimension,
1345 &switch_sort_dimension,
1346 &runtime_sort_dimension,
1347 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001348
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001349 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001350 if (!strcmp(available_sorts[i]->name, tok)) {
1351 list_add_tail(&available_sorts[i]->list, list);
1352
1353 return 0;
1354 }
1355 }
1356
1357 return -1;
1358}
1359
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001360static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001361{
1362 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001363 struct rb_root *root = &sched->atom_root;
1364again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001365 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001366 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001367 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001368 if (!node)
1369 break;
1370
Josef Bacik2f80dd42015-05-22 09:18:40 -04001371 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001372 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001373 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001374 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001375 if (root == &sched->atom_root) {
1376 root = &sched->merged_atom_root;
1377 goto again;
1378 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001379}
1380
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001381static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001382 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001383 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001384 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001385{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001386 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001387
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001388 if (sched->tp_handler->wakeup_event)
1389 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001390
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001391 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001392}
1393
Jiri Olsaa151a372016-04-12 15:29:29 +02001394union map_priv {
1395 void *ptr;
1396 bool color;
1397};
1398
1399static bool thread__has_color(struct thread *thread)
1400{
1401 union map_priv priv = {
1402 .ptr = thread__priv(thread),
1403 };
1404
1405 return priv.color;
1406}
1407
1408static struct thread*
1409map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1410{
1411 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1412 union map_priv priv = {
1413 .color = false,
1414 };
1415
1416 if (!sched->map.color_pids || !thread || thread__priv(thread))
1417 return thread;
1418
1419 if (thread_map__has(sched->map.color_pids, tid))
1420 priv.color = true;
1421
1422 thread__set_priv(thread, priv.ptr);
1423 return thread;
1424}
1425
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001426static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1427 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001428{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001429 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1430 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001431 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001432 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001433 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001434 int i, this_cpu = sample->cpu;
1435 int cpus_nr;
1436 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001437 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001438 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001439
1440 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1441
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001442 if (this_cpu > sched->max_cpu)
1443 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001444
Jiri Olsa99623c62016-04-12 15:29:26 +02001445 if (sched->map.comp) {
1446 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1447 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1448 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1449 new_cpu = true;
1450 }
1451 } else
1452 cpus_nr = sched->max_cpu;
1453
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001454 timestamp0 = sched->cpu_last_switched[this_cpu];
1455 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001456 if (timestamp0)
1457 delta = timestamp - timestamp0;
1458 else
1459 delta = 0;
1460
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001461 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001462 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001463 return -1;
1464 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001465
Jiri Olsaa151a372016-04-12 15:29:29 +02001466 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001467 if (sched_in == NULL)
1468 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001469
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001470 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001471
1472 printf(" ");
1473
1474 new_shortname = 0;
1475 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001476 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1477 /*
1478 * Don't allocate a letter-number for swapper:0
1479 * as a shortname. Instead, we use '.' for it.
1480 */
1481 sched_in->shortname[0] = '.';
1482 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001483 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001484 sched_in->shortname[0] = sched->next_shortname1;
1485 sched_in->shortname[1] = sched->next_shortname2;
1486
1487 if (sched->next_shortname1 < 'Z') {
1488 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001489 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001490 sched->next_shortname1 = 'A';
1491 if (sched->next_shortname2 < '9')
1492 sched->next_shortname2++;
1493 else
1494 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001495 }
1496 }
1497 new_shortname = 1;
1498 }
1499
Jiri Olsa99623c62016-04-12 15:29:26 +02001500 for (i = 0; i < cpus_nr; i++) {
1501 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001502 struct thread *curr_thread = sched->curr_thread[cpu];
1503 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001504 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001505
1506 if (curr_thread && thread__has_color(curr_thread))
1507 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001508
Jiri Olsa73643bb2016-04-12 15:29:31 +02001509 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1510 continue;
1511
Jiri Olsacf294f22016-04-12 15:29:30 +02001512 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1513 cpu_color = COLOR_CPUS;
1514
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001515 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001516 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001517 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001518 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001519
Dongsheng6bcab4e2014-05-06 14:39:01 +09001520 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001521 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001522 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001523 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001524 }
1525
Jiri Olsa73643bb2016-04-12 15:29:31 +02001526 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1527 goto out;
1528
Namhyung Kim99620a52016-10-24 11:02:45 +09001529 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1530 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001531 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001532 const char *pid_color = color;
1533
1534 if (thread__has_color(sched_in))
1535 pid_color = COLOR_PIDS;
1536
1537 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001538 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001539 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001540
Jiri Olsa99623c62016-04-12 15:29:26 +02001541 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001542 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001543
Jiri Olsa73643bb2016-04-12 15:29:31 +02001544out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001545 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001546
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001547 thread__put(sched_in);
1548
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001549 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001550}
1551
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001552static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001553 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001554 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001555 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001556{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001557 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001558 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001559 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1560 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001561
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001562 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001563 /*
1564 * Are we trying to switch away a PID that is
1565 * not current?
1566 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001567 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001568 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001569 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001570
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001571 if (sched->tp_handler->switch_event)
1572 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001573
1574 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001575 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001576}
1577
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001578static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001579 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001580 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001581 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001582{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001583 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001584
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001585 if (sched->tp_handler->runtime_event)
1586 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001587
1588 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001589}
1590
David Aherncb627502013-08-07 22:50:47 -04001591static int perf_sched__process_fork_event(struct perf_tool *tool,
1592 union perf_event *event,
1593 struct perf_sample *sample,
1594 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001595{
1596 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1597
David Aherncb627502013-08-07 22:50:47 -04001598 /* run the fork event through the perf machineruy */
1599 perf_event__process_fork(tool, event, sample, machine);
1600
1601 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001602 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001603 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001604
1605 return 0;
1606}
1607
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001608static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001609 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001610 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001611 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001612{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001613 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001614
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001615 if (sched->tp_handler->migrate_task_event)
1616 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001617
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001618 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001619}
1620
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001621typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001622 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001623 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001624 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001625
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001626static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1627 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001628 struct perf_sample *sample,
1629 struct perf_evsel *evsel,
1630 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001631{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001632 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001633
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001634 if (evsel->handler != NULL) {
1635 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001636 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001637 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001638
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001639 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001640}
1641
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001642static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001643{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001644 const struct perf_evsel_str_handler handlers[] = {
1645 { "sched:sched_switch", process_sched_switch_event, },
1646 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1647 { "sched:sched_wakeup", process_sched_wakeup_event, },
1648 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001649 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1650 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001651 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001652 struct perf_data_file file = {
1653 .path = input_name,
1654 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001655 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001656 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001657 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001658
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001659 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001660 if (session == NULL) {
1661 pr_debug("No Memory for session\n");
1662 return -1;
1663 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001664
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001665 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001666
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001667 if (perf_session__set_tracepoints_handlers(session, handlers))
1668 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001669
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001670 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001671 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001672 if (err) {
1673 pr_err("Failed to process events, error %d", err);
1674 goto out_delete;
1675 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001676
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001677 sched->nr_events = session->evlist->stats.nr_events[0];
1678 sched->nr_lost_events = session->evlist->stats.total_lost;
1679 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001680 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001681
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001682 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001683out_delete:
1684 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001685 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001686}
1687
David Ahern49394a22016-11-16 15:06:29 +09001688/*
1689 * scheduling times are printed as msec.usec
1690 */
1691static inline void print_sched_time(unsigned long long nsecs, int width)
1692{
1693 unsigned long msecs;
1694 unsigned long usecs;
1695
1696 msecs = nsecs / NSEC_PER_MSEC;
1697 nsecs -= msecs * NSEC_PER_MSEC;
1698 usecs = nsecs / NSEC_PER_USEC;
1699 printf("%*lu.%03lu ", width, msecs, usecs);
1700}
1701
1702/*
1703 * returns runtime data for event, allocating memory for it the
1704 * first time it is used.
1705 */
1706static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1707{
1708 struct evsel_runtime *r = evsel->priv;
1709
1710 if (r == NULL) {
1711 r = zalloc(sizeof(struct evsel_runtime));
1712 evsel->priv = r;
1713 }
1714
1715 return r;
1716}
1717
1718/*
1719 * save last time event was seen per cpu
1720 */
1721static void perf_evsel__save_time(struct perf_evsel *evsel,
1722 u64 timestamp, u32 cpu)
1723{
1724 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1725
1726 if (r == NULL)
1727 return;
1728
1729 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1730 int i, n = __roundup_pow_of_two(cpu+1);
1731 void *p = r->last_time;
1732
1733 p = realloc(r->last_time, n * sizeof(u64));
1734 if (!p)
1735 return;
1736
1737 r->last_time = p;
1738 for (i = r->ncpu; i < n; ++i)
1739 r->last_time[i] = (u64) 0;
1740
1741 r->ncpu = n;
1742 }
1743
1744 r->last_time[cpu] = timestamp;
1745}
1746
1747/* returns last time this event was seen on the given cpu */
1748static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1749{
1750 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1751
1752 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1753 return 0;
1754
1755 return r->last_time[cpu];
1756}
1757
1758static int comm_width = 20;
1759
1760static char *timehist_get_commstr(struct thread *thread)
1761{
1762 static char str[32];
1763 const char *comm = thread__comm_str(thread);
1764 pid_t tid = thread->tid;
1765 pid_t pid = thread->pid_;
1766 int n;
1767
1768 if (pid == 0)
1769 n = scnprintf(str, sizeof(str), "%s", comm);
1770
1771 else if (tid != pid)
1772 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1773
1774 else
1775 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1776
1777 if (n > comm_width)
1778 comm_width = n;
1779
1780 return str;
1781}
1782
1783static void timehist_header(void)
1784{
1785 printf("%15s %6s ", "time", "cpu");
1786
1787 printf(" %-20s %9s %9s %9s",
1788 "task name", "wait time", "sch delay", "run time");
1789
1790 printf("\n");
1791
1792 /*
1793 * units row
1794 */
1795 printf("%15s %-6s ", "", "");
1796
1797 printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)");
1798
1799 /*
1800 * separator
1801 */
1802 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1803
1804 printf(" %.20s %.9s %.9s %.9s",
1805 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1806 graph_dotted_line);
1807
1808 printf("\n");
1809}
1810
David Ahernfc1469f2016-11-16 15:06:31 +09001811static void timehist_print_sample(struct perf_sched *sched,
1812 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09001813 struct thread *thread)
1814{
1815 struct thread_runtime *tr = thread__priv(thread);
1816 char tstr[64];
1817
1818 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
1819 printf("%15s [%04d] ", tstr, sample->cpu);
1820
1821 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1822
1823 print_sched_time(tr->dt_wait, 6);
1824 print_sched_time(tr->dt_delay, 6);
1825 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09001826
1827 if (sched->show_wakeups)
1828 printf(" %-*s", comm_width, "");
1829
David Ahern49394a22016-11-16 15:06:29 +09001830 printf("\n");
1831}
1832
1833/*
1834 * Explanation of delta-time stats:
1835 *
1836 * t = time of current schedule out event
1837 * tprev = time of previous sched out event
1838 * also time of schedule-in event for current task
1839 * last_time = time of last sched change event for current task
1840 * (i.e, time process was last scheduled out)
1841 * ready_to_run = time of wakeup for current task
1842 *
1843 * -----|------------|------------|------------|------
1844 * last ready tprev t
1845 * time to run
1846 *
1847 * |-------- dt_wait --------|
1848 * |- dt_delay -|-- dt_run --|
1849 *
1850 * dt_run = run time of current task
1851 * dt_wait = time between last schedule out event for task and tprev
1852 * represents time spent off the cpu
1853 * dt_delay = time between wakeup and schedule-in of task
1854 */
1855
1856static void timehist_update_runtime_stats(struct thread_runtime *r,
1857 u64 t, u64 tprev)
1858{
1859 r->dt_delay = 0;
1860 r->dt_wait = 0;
1861 r->dt_run = 0;
1862 if (tprev) {
1863 r->dt_run = t - tprev;
1864 if (r->ready_to_run) {
1865 if (r->ready_to_run > tprev)
1866 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1867 else
1868 r->dt_delay = tprev - r->ready_to_run;
1869 }
1870
1871 if (r->last_time > tprev)
1872 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
1873 else if (r->last_time)
1874 r->dt_wait = tprev - r->last_time;
1875 }
1876
1877 update_stats(&r->run_stats, r->dt_run);
1878 r->total_run_time += r->dt_run;
1879}
1880
1881static bool is_idle_sample(struct perf_sample *sample,
1882 struct perf_evsel *evsel)
1883{
1884 /* pid 0 == swapper == idle task */
1885 if (sample->pid == 0)
1886 return true;
1887
1888 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) {
1889 if (perf_evsel__intval(evsel, sample, "prev_pid") == 0)
1890 return true;
1891 }
1892 return false;
1893}
1894
1895/*
1896 * Track idle stats per cpu by maintaining a local thread
1897 * struct for the idle task on each cpu.
1898 */
1899static int init_idle_threads(int ncpu)
1900{
1901 int i;
1902
1903 idle_threads = zalloc(ncpu * sizeof(struct thread *));
1904 if (!idle_threads)
1905 return -ENOMEM;
1906
1907 idle_max_cpu = ncpu - 1;
1908
1909 /* allocate the actual thread struct if needed */
1910 for (i = 0; i < ncpu; ++i) {
1911 idle_threads[i] = thread__new(0, 0);
1912 if (idle_threads[i] == NULL)
1913 return -ENOMEM;
1914
1915 thread__set_comm(idle_threads[i], idle_comm, 0);
1916 }
1917
1918 return 0;
1919}
1920
1921static void free_idle_threads(void)
1922{
1923 int i;
1924
1925 if (idle_threads == NULL)
1926 return;
1927
1928 for (i = 0; i <= idle_max_cpu; ++i) {
1929 if ((idle_threads[i]))
1930 thread__delete(idle_threads[i]);
1931 }
1932
1933 free(idle_threads);
1934}
1935
1936static struct thread *get_idle_thread(int cpu)
1937{
1938 /*
1939 * expand/allocate array of pointers to local thread
1940 * structs if needed
1941 */
1942 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
1943 int i, j = __roundup_pow_of_two(cpu+1);
1944 void *p;
1945
1946 p = realloc(idle_threads, j * sizeof(struct thread *));
1947 if (!p)
1948 return NULL;
1949
1950 idle_threads = (struct thread **) p;
1951 i = idle_max_cpu ? idle_max_cpu + 1 : 0;
1952 for (; i < j; ++i)
1953 idle_threads[i] = NULL;
1954
1955 idle_max_cpu = j;
1956 }
1957
1958 /* allocate a new thread struct if needed */
1959 if (idle_threads[cpu] == NULL) {
1960 idle_threads[cpu] = thread__new(0, 0);
1961
1962 if (idle_threads[cpu]) {
1963 idle_threads[cpu]->tid = 0;
1964 thread__set_comm(idle_threads[cpu], idle_comm, 0);
1965 }
1966 }
1967
1968 return idle_threads[cpu];
1969}
1970
1971/*
1972 * handle runtime stats saved per thread
1973 */
1974static struct thread_runtime *thread__init_runtime(struct thread *thread)
1975{
1976 struct thread_runtime *r;
1977
1978 r = zalloc(sizeof(struct thread_runtime));
1979 if (!r)
1980 return NULL;
1981
1982 init_stats(&r->run_stats);
1983 thread__set_priv(thread, r);
1984
1985 return r;
1986}
1987
1988static struct thread_runtime *thread__get_runtime(struct thread *thread)
1989{
1990 struct thread_runtime *tr;
1991
1992 tr = thread__priv(thread);
1993 if (tr == NULL) {
1994 tr = thread__init_runtime(thread);
1995 if (tr == NULL)
1996 pr_debug("Failed to malloc memory for runtime data.\n");
1997 }
1998
1999 return tr;
2000}
2001
2002static struct thread *timehist_get_thread(struct perf_sample *sample,
2003 struct machine *machine,
2004 struct perf_evsel *evsel)
2005{
2006 struct thread *thread;
2007
2008 if (is_idle_sample(sample, evsel)) {
2009 thread = get_idle_thread(sample->cpu);
2010 if (thread == NULL)
2011 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2012
2013 } else {
2014 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2015 if (thread == NULL) {
2016 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2017 sample->tid);
2018 }
2019 }
2020
2021 return thread;
2022}
2023
David Ahern52df1382016-11-16 15:06:30 +09002024static bool timehist_skip_sample(struct perf_sched *sched,
2025 struct thread *thread)
David Ahern49394a22016-11-16 15:06:29 +09002026{
2027 bool rc = false;
2028
David Ahern52df1382016-11-16 15:06:30 +09002029 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002030 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002031 sched->skipped_samples++;
2032 }
David Ahern49394a22016-11-16 15:06:29 +09002033
2034 return rc;
2035}
2036
David Ahernfc1469f2016-11-16 15:06:31 +09002037static void timehist_print_wakeup_event(struct perf_sched *sched,
2038 struct perf_sample *sample,
2039 struct machine *machine,
2040 struct thread *awakened)
2041{
2042 struct thread *thread;
2043 char tstr[64];
2044
2045 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2046 if (thread == NULL)
2047 return;
2048
2049 /* show wakeup unless both awakee and awaker are filtered */
2050 if (timehist_skip_sample(sched, thread) &&
2051 timehist_skip_sample(sched, awakened)) {
2052 return;
2053 }
2054
2055 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2056 printf("%15s [%04d] ", tstr, sample->cpu);
2057
2058 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2059
2060 /* dt spacer */
2061 printf(" %9s %9s %9s ", "", "", "");
2062
2063 printf("awakened: %s", timehist_get_commstr(awakened));
2064
2065 printf("\n");
2066}
2067
2068static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002069 union perf_event *event __maybe_unused,
2070 struct perf_evsel *evsel,
2071 struct perf_sample *sample,
2072 struct machine *machine)
2073{
David Ahernfc1469f2016-11-16 15:06:31 +09002074 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002075 struct thread *thread;
2076 struct thread_runtime *tr = NULL;
2077 /* want pid of awakened task not pid in sample */
2078 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2079
2080 thread = machine__findnew_thread(machine, 0, pid);
2081 if (thread == NULL)
2082 return -1;
2083
2084 tr = thread__get_runtime(thread);
2085 if (tr == NULL)
2086 return -1;
2087
2088 if (tr->ready_to_run == 0)
2089 tr->ready_to_run = sample->time;
2090
David Ahernfc1469f2016-11-16 15:06:31 +09002091 /* show wakeups if requested */
2092 if (sched->show_wakeups)
2093 timehist_print_wakeup_event(sched, sample, machine, thread);
2094
David Ahern49394a22016-11-16 15:06:29 +09002095 return 0;
2096}
2097
David Ahern52df1382016-11-16 15:06:30 +09002098static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002099 union perf_event *event,
2100 struct perf_evsel *evsel,
2101 struct perf_sample *sample,
2102 struct machine *machine)
2103{
David Ahernfc1469f2016-11-16 15:06:31 +09002104 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002105 struct addr_location al;
2106 struct thread *thread;
2107 struct thread_runtime *tr = NULL;
2108 u64 tprev;
2109 int rc = 0;
2110
2111 if (machine__resolve(machine, &al, sample) < 0) {
2112 pr_err("problem processing %d event. skipping it\n",
2113 event->header.type);
2114 rc = -1;
2115 goto out;
2116 }
2117
2118 thread = timehist_get_thread(sample, machine, evsel);
2119 if (thread == NULL) {
2120 rc = -1;
2121 goto out;
2122 }
2123
David Ahern52df1382016-11-16 15:06:30 +09002124 if (timehist_skip_sample(sched, thread))
David Ahern49394a22016-11-16 15:06:29 +09002125 goto out;
2126
2127 tr = thread__get_runtime(thread);
2128 if (tr == NULL) {
2129 rc = -1;
2130 goto out;
2131 }
2132
2133 tprev = perf_evsel__get_time(evsel, sample->cpu);
2134
2135 timehist_update_runtime_stats(tr, sample->time, tprev);
David Ahern52df1382016-11-16 15:06:30 +09002136 if (!sched->summary_only)
David Ahernfc1469f2016-11-16 15:06:31 +09002137 timehist_print_sample(sched, sample, thread);
David Ahern49394a22016-11-16 15:06:29 +09002138
2139out:
2140 if (tr) {
2141 /* time of this sched_switch event becomes last time task seen */
2142 tr->last_time = sample->time;
2143
2144 /* sched out event for task so reset ready to run time */
2145 tr->ready_to_run = 0;
2146 }
2147
2148 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2149
2150 return rc;
2151}
2152
2153static int timehist_sched_switch_event(struct perf_tool *tool,
2154 union perf_event *event,
2155 struct perf_evsel *evsel,
2156 struct perf_sample *sample,
2157 struct machine *machine __maybe_unused)
2158{
2159 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2160}
2161
2162static int process_lost(struct perf_tool *tool __maybe_unused,
2163 union perf_event *event,
2164 struct perf_sample *sample,
2165 struct machine *machine __maybe_unused)
2166{
2167 char tstr[64];
2168
2169 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2170 printf("%15s ", tstr);
2171 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2172
2173 return 0;
2174}
2175
2176
David Ahern52df1382016-11-16 15:06:30 +09002177static void print_thread_runtime(struct thread *t,
2178 struct thread_runtime *r)
2179{
2180 double mean = avg_stats(&r->run_stats);
2181 float stddev;
2182
2183 printf("%*s %5d %9" PRIu64 " ",
2184 comm_width, timehist_get_commstr(t), t->ppid,
2185 (u64) r->run_stats.n);
2186
2187 print_sched_time(r->total_run_time, 8);
2188 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2189 print_sched_time(r->run_stats.min, 6);
2190 printf(" ");
2191 print_sched_time((u64) mean, 6);
2192 printf(" ");
2193 print_sched_time(r->run_stats.max, 6);
2194 printf(" ");
2195 printf("%5.2f", stddev);
2196 printf("\n");
2197}
2198
2199struct total_run_stats {
2200 u64 sched_count;
2201 u64 task_count;
2202 u64 total_run_time;
2203};
2204
2205static int __show_thread_runtime(struct thread *t, void *priv)
2206{
2207 struct total_run_stats *stats = priv;
2208 struct thread_runtime *r;
2209
2210 if (thread__is_filtered(t))
2211 return 0;
2212
2213 r = thread__priv(t);
2214 if (r && r->run_stats.n) {
2215 stats->task_count++;
2216 stats->sched_count += r->run_stats.n;
2217 stats->total_run_time += r->total_run_time;
2218 print_thread_runtime(t, r);
2219 }
2220
2221 return 0;
2222}
2223
2224static int show_thread_runtime(struct thread *t, void *priv)
2225{
2226 if (t->dead)
2227 return 0;
2228
2229 return __show_thread_runtime(t, priv);
2230}
2231
2232static int show_deadthread_runtime(struct thread *t, void *priv)
2233{
2234 if (!t->dead)
2235 return 0;
2236
2237 return __show_thread_runtime(t, priv);
2238}
2239
2240static void timehist_print_summary(struct perf_sched *sched,
2241 struct perf_session *session)
2242{
2243 struct machine *m = &session->machines.host;
2244 struct total_run_stats totals;
2245 u64 task_count;
2246 struct thread *t;
2247 struct thread_runtime *r;
2248 int i;
2249
2250 memset(&totals, 0, sizeof(totals));
2251
2252 if (comm_width < 30)
2253 comm_width = 30;
2254
2255 printf("\nRuntime summary\n");
2256 printf("%*s parent sched-in ", comm_width, "comm");
2257 printf(" run-time min-run avg-run max-run stddev\n");
2258 printf("%*s (count) ", comm_width, "");
2259 printf(" (msec) (msec) (msec) (msec) %%\n");
2260 printf("%.105s\n", graph_dotted_line);
2261
2262 machine__for_each_thread(m, show_thread_runtime, &totals);
2263 task_count = totals.task_count;
2264 if (!task_count)
2265 printf("<no still running tasks>\n");
2266
2267 printf("\nTerminated tasks:\n");
2268 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2269 if (task_count == totals.task_count)
2270 printf("<no terminated tasks>\n");
2271
2272 /* CPU idle stats not tracked when samples were skipped */
2273 if (sched->skipped_samples)
2274 return;
2275
2276 printf("\nIdle stats:\n");
2277 for (i = 0; i <= idle_max_cpu; ++i) {
2278 t = idle_threads[i];
2279 if (!t)
2280 continue;
2281
2282 r = thread__priv(t);
2283 if (r && r->run_stats.n) {
2284 totals.sched_count += r->run_stats.n;
2285 printf(" CPU %2d idle for ", i);
2286 print_sched_time(r->total_run_time, 6);
2287 printf(" msec\n");
2288 } else
2289 printf(" CPU %2d idle entire time window\n", i);
2290 }
2291
2292 printf("\n"
2293 " Total number of unique tasks: %" PRIu64 "\n"
2294 "Total number of context switches: %" PRIu64 "\n"
2295 " Total run time (msec): ",
2296 totals.task_count, totals.sched_count);
2297
2298 print_sched_time(totals.total_run_time, 2);
2299 printf("\n");
2300}
2301
David Ahern49394a22016-11-16 15:06:29 +09002302typedef int (*sched_handler)(struct perf_tool *tool,
2303 union perf_event *event,
2304 struct perf_evsel *evsel,
2305 struct perf_sample *sample,
2306 struct machine *machine);
2307
2308static int perf_timehist__process_sample(struct perf_tool *tool,
2309 union perf_event *event,
2310 struct perf_sample *sample,
2311 struct perf_evsel *evsel,
2312 struct machine *machine)
2313{
2314 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2315 int err = 0;
2316 int this_cpu = sample->cpu;
2317
2318 if (this_cpu > sched->max_cpu)
2319 sched->max_cpu = this_cpu;
2320
2321 if (evsel->handler != NULL) {
2322 sched_handler f = evsel->handler;
2323
2324 err = f(tool, event, evsel, sample, machine);
2325 }
2326
2327 return err;
2328}
2329
2330static int perf_sched__timehist(struct perf_sched *sched)
2331{
2332 const struct perf_evsel_str_handler handlers[] = {
2333 { "sched:sched_switch", timehist_sched_switch_event, },
2334 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2335 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2336 };
2337 struct perf_data_file file = {
2338 .path = input_name,
2339 .mode = PERF_DATA_MODE_READ,
2340 };
2341
2342 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002343 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002344 int err = -1;
2345
2346 /*
2347 * event handlers for timehist option
2348 */
2349 sched->tool.sample = perf_timehist__process_sample;
2350 sched->tool.mmap = perf_event__process_mmap;
2351 sched->tool.comm = perf_event__process_comm;
2352 sched->tool.exit = perf_event__process_exit;
2353 sched->tool.fork = perf_event__process_fork;
2354 sched->tool.lost = process_lost;
2355 sched->tool.attr = perf_event__process_attr;
2356 sched->tool.tracing_data = perf_event__process_tracing_data;
2357 sched->tool.build_id = perf_event__process_build_id;
2358
2359 sched->tool.ordered_events = true;
2360 sched->tool.ordering_requires_timestamps = true;
2361
2362 session = perf_session__new(&file, false, &sched->tool);
2363 if (session == NULL)
2364 return -ENOMEM;
2365
David Ahern52df1382016-11-16 15:06:30 +09002366 evlist = session->evlist;
2367
David Ahern49394a22016-11-16 15:06:29 +09002368 symbol__init(&session->header.env);
2369
2370 setup_pager();
2371
2372 /* setup per-evsel handlers */
2373 if (perf_session__set_tracepoints_handlers(session, handlers))
2374 goto out;
2375
2376 if (!perf_session__has_traces(session, "record -R"))
2377 goto out;
2378
2379 /* pre-allocate struct for per-CPU idle stats */
2380 sched->max_cpu = session->header.env.nr_cpus_online;
2381 if (sched->max_cpu == 0)
2382 sched->max_cpu = 4;
2383 if (init_idle_threads(sched->max_cpu))
2384 goto out;
2385
David Ahern52df1382016-11-16 15:06:30 +09002386 /* summary_only implies summary option, but don't overwrite summary if set */
2387 if (sched->summary_only)
2388 sched->summary = sched->summary_only;
2389
2390 if (!sched->summary_only)
2391 timehist_header();
David Ahern49394a22016-11-16 15:06:29 +09002392
2393 err = perf_session__process_events(session);
2394 if (err) {
2395 pr_err("Failed to process events, error %d", err);
2396 goto out;
2397 }
2398
David Ahern52df1382016-11-16 15:06:30 +09002399 sched->nr_events = evlist->stats.nr_events[0];
2400 sched->nr_lost_events = evlist->stats.total_lost;
2401 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2402
2403 if (sched->summary)
2404 timehist_print_summary(sched, session);
2405
David Ahern49394a22016-11-16 15:06:29 +09002406out:
2407 free_idle_threads();
2408 perf_session__delete(session);
2409
2410 return err;
2411}
2412
2413
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002414static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002415{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002416 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002417 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002418 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2419 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002420 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002421 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002422 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002423 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2424 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002425 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002426 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002427 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002428 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2429 sched->nr_context_switch_bugs, sched->nr_timestamps);
2430 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002431 printf(" (due to lost events?)");
2432 printf("\n");
2433 }
2434}
2435
Josef Bacik2f80dd42015-05-22 09:18:40 -04002436static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2437{
2438 struct rb_node **new = &(root->rb_node), *parent = NULL;
2439 struct work_atoms *this;
2440 const char *comm = thread__comm_str(data->thread), *this_comm;
2441
2442 while (*new) {
2443 int cmp;
2444
2445 this = container_of(*new, struct work_atoms, node);
2446 parent = *new;
2447
2448 this_comm = thread__comm_str(this->thread);
2449 cmp = strcmp(comm, this_comm);
2450 if (cmp > 0) {
2451 new = &((*new)->rb_left);
2452 } else if (cmp < 0) {
2453 new = &((*new)->rb_right);
2454 } else {
2455 this->num_merged++;
2456 this->total_runtime += data->total_runtime;
2457 this->nb_atoms += data->nb_atoms;
2458 this->total_lat += data->total_lat;
2459 list_splice(&data->work_list, &this->work_list);
2460 if (this->max_lat < data->max_lat) {
2461 this->max_lat = data->max_lat;
2462 this->max_lat_at = data->max_lat_at;
2463 }
2464 zfree(&data);
2465 return;
2466 }
2467 }
2468
2469 data->num_merged++;
2470 rb_link_node(&data->node, parent, new);
2471 rb_insert_color(&data->node, root);
2472}
2473
2474static void perf_sched__merge_lat(struct perf_sched *sched)
2475{
2476 struct work_atoms *data;
2477 struct rb_node *node;
2478
2479 if (sched->skip_merge)
2480 return;
2481
2482 while ((node = rb_first(&sched->atom_root))) {
2483 rb_erase(node, &sched->atom_root);
2484 data = rb_entry(node, struct work_atoms, node);
2485 __merge_work_atoms(&sched->merged_atom_root, data);
2486 }
2487}
2488
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002489static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002490{
2491 struct rb_node *next;
2492
2493 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002494
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002495 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002496 return -1;
David Ahernad9def72013-08-07 22:50:44 -04002497
Josef Bacik2f80dd42015-05-22 09:18:40 -04002498 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002499 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002500
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002501 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
2502 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
2503 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002504
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002505 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002506
2507 while (next) {
2508 struct work_atoms *work_list;
2509
2510 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002511 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002512 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002513 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002514 }
2515
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002516 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002517 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03002518 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002519
2520 printf(" ---------------------------------------------------\n");
2521
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002522 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002523 printf("\n");
2524
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002525 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002526}
2527
Jiri Olsa99623c62016-04-12 15:29:26 +02002528static int setup_map_cpus(struct perf_sched *sched)
2529{
Jiri Olsa73643bb2016-04-12 15:29:31 +02002530 struct cpu_map *map;
2531
Jiri Olsa99623c62016-04-12 15:29:26 +02002532 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
2533
2534 if (sched->map.comp) {
2535 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02002536 if (!sched->map.comp_cpus)
2537 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02002538 }
2539
Jiri Olsa73643bb2016-04-12 15:29:31 +02002540 if (!sched->map.cpus_str)
2541 return 0;
2542
2543 map = cpu_map__new(sched->map.cpus_str);
2544 if (!map) {
2545 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
2546 return -1;
2547 }
2548
2549 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02002550 return 0;
2551}
2552
Jiri Olsaa151a372016-04-12 15:29:29 +02002553static int setup_color_pids(struct perf_sched *sched)
2554{
2555 struct thread_map *map;
2556
2557 if (!sched->map.color_pids_str)
2558 return 0;
2559
2560 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
2561 if (!map) {
2562 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
2563 return -1;
2564 }
2565
2566 sched->map.color_pids = map;
2567 return 0;
2568}
2569
Jiri Olsacf294f22016-04-12 15:29:30 +02002570static int setup_color_cpus(struct perf_sched *sched)
2571{
2572 struct cpu_map *map;
2573
2574 if (!sched->map.color_cpus_str)
2575 return 0;
2576
2577 map = cpu_map__new(sched->map.color_cpus_str);
2578 if (!map) {
2579 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
2580 return -1;
2581 }
2582
2583 sched->map.color_cpus = map;
2584 return 0;
2585}
2586
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002587static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002588{
Jiri Olsa99623c62016-04-12 15:29:26 +02002589 if (setup_map_cpus(sched))
2590 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02002591
Jiri Olsaa151a372016-04-12 15:29:29 +02002592 if (setup_color_pids(sched))
2593 return -1;
2594
Jiri Olsacf294f22016-04-12 15:29:30 +02002595 if (setup_color_cpus(sched))
2596 return -1;
2597
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002598 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002599 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002600 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002601 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002602 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002603}
2604
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002605static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002606{
2607 unsigned long i;
2608
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002609 calibrate_run_measurement_overhead(sched);
2610 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002611
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002612 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002613
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002614 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002615 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002616
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002617 printf("nr_run_events: %ld\n", sched->nr_run_events);
2618 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
2619 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002620
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002621 if (sched->targetless_wakeups)
2622 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
2623 if (sched->multitarget_wakeups)
2624 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
2625 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002626 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002627 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002628
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002629 print_task_traces(sched);
2630 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002631
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002632 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002633 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002634 for (i = 0; i < sched->replay_repeat; i++)
2635 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002636
2637 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002638}
2639
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002640static void setup_sorting(struct perf_sched *sched, const struct option *options,
2641 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002642{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002643 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002644
2645 for (tok = strtok_r(str, ", ", &tmp);
2646 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002647 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002648 usage_with_options_msg(usage_msg, options,
2649 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002650 }
2651 }
2652
2653 free(str);
2654
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002655 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002656}
2657
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002658static int __cmd_record(int argc, const char **argv)
2659{
2660 unsigned int rec_argc, i, j;
2661 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002662 const char * const record_args[] = {
2663 "record",
2664 "-a",
2665 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002666 "-m", "1024",
2667 "-c", "1",
2668 "-e", "sched:sched_switch",
2669 "-e", "sched:sched_stat_wait",
2670 "-e", "sched:sched_stat_sleep",
2671 "-e", "sched:sched_stat_iowait",
2672 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002673 "-e", "sched:sched_process_fork",
2674 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09002675 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002676 "-e", "sched:sched_migrate_task",
2677 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002678
2679 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
2680 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2681
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02002682 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11002683 return -ENOMEM;
2684
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002685 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2686 rec_argv[i] = strdup(record_args[i]);
2687
2688 for (j = 1; j < (unsigned int)argc; j++, i++)
2689 rec_argv[i] = argv[j];
2690
2691 BUG_ON(i != rec_argc);
2692
2693 return cmd_record(i, rec_argv, NULL);
2694}
2695
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002696int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002697{
Adrian Hunter8a39df82013-10-22 10:34:15 +03002698 const char default_sort_order[] = "avg, max, switch, runtime";
2699 struct perf_sched sched = {
2700 .tool = {
2701 .sample = perf_sched__process_tracepoint_sample,
2702 .comm = perf_event__process_comm,
2703 .lost = perf_event__process_lost,
2704 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002705 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002706 },
2707 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
2708 .sort_list = LIST_HEAD_INIT(sched.sort_list),
2709 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
2710 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002711 .sort_order = default_sort_order,
2712 .replay_repeat = 10,
2713 .profile_cpu = -1,
2714 .next_shortname1 = 'A',
2715 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04002716 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002717 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09002718 const struct option sched_options[] = {
2719 OPT_STRING('i', "input", &input_name, "file",
2720 "input file name"),
2721 OPT_INCR('v', "verbose", &verbose,
2722 "be more verbose (show symbol address, etc)"),
2723 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2724 "dump raw trace in ASCII"),
2725 OPT_END()
2726 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002727 const struct option latency_options[] = {
2728 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
2729 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002730 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
2731 "CPU to profile on"),
2732 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2733 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04002734 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
2735 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002736 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002737 };
2738 const struct option replay_options[] = {
2739 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
2740 "repeat the workload replay N times (-1: infinite)"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002741 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2742 "dump raw trace in ASCII"),
Yunlong Song939cda52015-03-31 21:46:34 +08002743 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002744 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002745 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002746 const struct option map_options[] = {
2747 OPT_BOOLEAN(0, "compact", &sched.map.comp,
2748 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02002749 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
2750 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02002751 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
2752 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02002753 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
2754 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002755 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02002756 };
David Ahern49394a22016-11-16 15:06:29 +09002757 const struct option timehist_options[] = {
2758 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2759 "file", "vmlinux pathname"),
2760 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2761 "file", "kallsyms pathname"),
2762 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2763 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09002764 OPT_BOOLEAN('s', "summary", &sched.summary_only,
2765 "Show only syscall summary with statistics"),
2766 OPT_BOOLEAN('S', "with-summary", &sched.summary,
2767 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09002768 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
David Ahern49394a22016-11-16 15:06:29 +09002769 OPT_PARENT(sched_options)
2770 };
2771
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002772 const char * const latency_usage[] = {
2773 "perf sched latency [<options>]",
2774 NULL
2775 };
2776 const char * const replay_usage[] = {
2777 "perf sched replay [<options>]",
2778 NULL
2779 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002780 const char * const map_usage[] = {
2781 "perf sched map [<options>]",
2782 NULL
2783 };
David Ahern49394a22016-11-16 15:06:29 +09002784 const char * const timehist_usage[] = {
2785 "perf sched timehist [<options>]",
2786 NULL
2787 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002788 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09002789 "replay", "script",
2790 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002791 const char *sched_usage[] = {
2792 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002793 NULL
2794 };
2795 struct trace_sched_handler lat_ops = {
2796 .wakeup_event = latency_wakeup_event,
2797 .switch_event = latency_switch_event,
2798 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002799 .migrate_task_event = latency_migrate_task_event,
2800 };
2801 struct trace_sched_handler map_ops = {
2802 .switch_event = map_switch_event,
2803 };
2804 struct trace_sched_handler replay_ops = {
2805 .wakeup_event = replay_wakeup_event,
2806 .switch_event = replay_switch_event,
2807 .fork_event = replay_fork_event,
2808 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002809 unsigned int i;
2810
2811 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2812 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002813
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002814 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2815 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002816 if (!argc)
2817 usage_with_options(sched_usage, sched_options);
2818
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002819 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002820 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002821 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002822 if (!strcmp(argv[0], "script"))
2823 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002824
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002825 if (!strncmp(argv[0], "rec", 3)) {
2826 return __cmd_record(argc, argv);
2827 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002828 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002829 if (argc > 1) {
2830 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2831 if (argc)
2832 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002833 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002834 setup_sorting(&sched, latency_options, latency_usage);
2835 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002836 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002837 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002838 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002839 if (argc)
2840 usage_with_options(map_usage, map_options);
2841 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002842 sched.tp_handler = &map_ops;
2843 setup_sorting(&sched, latency_options, latency_usage);
2844 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002845 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002846 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002847 if (argc) {
2848 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2849 if (argc)
2850 usage_with_options(replay_usage, replay_options);
2851 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002852 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09002853 } else if (!strcmp(argv[0], "timehist")) {
2854 if (argc) {
2855 argc = parse_options(argc, argv, timehist_options,
2856 timehist_usage, 0);
2857 if (argc)
2858 usage_with_options(timehist_usage, timehist_options);
2859 }
David Ahernfc1469f2016-11-16 15:06:31 +09002860 if (sched.show_wakeups && sched.summary_only) {
2861 pr_err(" Error: -s and -w are mutually exclusive.\n");
2862 parse_options_usage(timehist_usage, timehist_options, "s", true);
2863 parse_options_usage(NULL, timehist_options, "w", true);
2864 return -EINVAL;
2865 }
2866
David Ahern49394a22016-11-16 15:06:29 +09002867 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002868 } else {
2869 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002870 }
2871
Ingo Molnarec156762009-09-11 12:12:54 +02002872 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002873}