blob: 1e7d81ad5ec650c350982b0b7b2714d9ca4b5a1e [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 Song939cda5212015-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;
201 u64 skipped_samples;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300202};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200203
David Ahern49394a22016-11-16 15:06:29 +0900204/* per thread run time data */
205struct thread_runtime {
206 u64 last_time; /* time of previous sched in/out event */
207 u64 dt_run; /* run time */
208 u64 dt_wait; /* time between CPU access (off cpu) */
209 u64 dt_delay; /* time between wakeup and sched-in */
210 u64 ready_to_run; /* time of wakeup */
211
212 struct stats run_stats;
213 u64 total_run_time;
214};
215
216/* per event run time data */
217struct evsel_runtime {
218 u64 *last_time; /* time this event was last seen per cpu */
219 u32 ncpu; /* highest cpu slot allocated */
220};
221
222/* track idle times per cpu */
223static struct thread **idle_threads;
224static int idle_max_cpu;
225static char idle_comm[] = "<idle>";
226
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200227static u64 get_nsecs(void)
228{
229 struct timespec ts;
230
231 clock_gettime(CLOCK_MONOTONIC, &ts);
232
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300233 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200234}
235
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300236static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200237{
238 u64 T0 = get_nsecs(), T1;
239
240 do {
241 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300242 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200243}
244
245static void sleep_nsecs(u64 nsecs)
246{
247 struct timespec ts;
248
249 ts.tv_nsec = nsecs % 999999999;
250 ts.tv_sec = nsecs / 999999999;
251
252 nanosleep(&ts, NULL);
253}
254
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300255static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200256{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300257 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200258 int i;
259
260 for (i = 0; i < 10; i++) {
261 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300262 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200263 T1 = get_nsecs();
264 delta = T1-T0;
265 min_delta = min(min_delta, delta);
266 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300267 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200268
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200269 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200270}
271
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300272static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200273{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300274 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200275 int i;
276
277 for (i = 0; i < 10; i++) {
278 T0 = get_nsecs();
279 sleep_nsecs(10000);
280 T1 = get_nsecs();
281 delta = T1-T0;
282 min_delta = min(min_delta, delta);
283 }
284 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300285 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200286
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200287 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200288}
289
mingo39aeb522009-09-14 20:04:48 +0200290static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200291get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200292{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200293 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200294 unsigned long idx = task->nr_events;
295 size_t size;
296
297 event->timestamp = timestamp;
298 event->nr = idx;
299
300 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200301 size = sizeof(struct sched_atom *) * task->nr_events;
302 task->atoms = realloc(task->atoms, size);
303 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200304
mingo39aeb522009-09-14 20:04:48 +0200305 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200306
307 return event;
308}
309
mingo39aeb522009-09-14 20:04:48 +0200310static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200311{
312 if (!task->nr_events)
313 return NULL;
314
mingo39aeb522009-09-14 20:04:48 +0200315 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200316}
317
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300318static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
319 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200320{
mingo39aeb522009-09-14 20:04:48 +0200321 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200322
323 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200324 * optimize an existing RUN event by merging this one
325 * to it:
326 */
Ingo Molnarec156762009-09-11 12:12:54 +0200327 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300328 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200329 curr_event->duration += duration;
330 return;
331 }
332
333 event = get_new_event(task, timestamp);
334
335 event->type = SCHED_EVENT_RUN;
336 event->duration = duration;
337
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300338 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200339}
340
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300341static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
342 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200343{
mingo39aeb522009-09-14 20:04:48 +0200344 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200345
346 event = get_new_event(task, timestamp);
347 event->type = SCHED_EVENT_WAKEUP;
348 event->wakee = wakee;
349
350 wakee_event = last_event(wakee);
351 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300352 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200353 return;
354 }
355 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300356 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200357 return;
358 }
359
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200360 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200361 sem_init(wakee_event->wait_sem, 0, 0);
362 wakee_event->specific_wait = 1;
363 event->wait_sem = wakee_event->wait_sem;
364
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300365 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200366}
367
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300368static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
369 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200370{
mingo39aeb522009-09-14 20:04:48 +0200371 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200372
373 event->type = SCHED_EVENT_SLEEP;
374
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300375 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200376}
377
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300378static struct task_desc *register_pid(struct perf_sched *sched,
379 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200380{
381 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800382 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200383
Yunlong Songcb06ac22015-03-31 21:46:30 +0800384 if (sched->pid_to_task == NULL) {
385 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
386 pid_max = MAX_PID;
387 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
388 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800389 if (pid >= (unsigned long)pid_max) {
390 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
391 sizeof(struct task_desc *))) == NULL);
392 while (pid >= (unsigned long)pid_max)
393 sched->pid_to_task[pid_max++] = NULL;
394 }
Ingo Molnarec156762009-09-11 12:12:54 +0200395
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300396 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200397
398 if (task)
399 return task;
400
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200401 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200402 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300403 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200404 strcpy(task->comm, comm);
405 /*
406 * every task starts in sleeping state - this gets ignored
407 * if there's no wakeup pointing to this sleep state:
408 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300409 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200410
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300411 sched->pid_to_task[pid] = task;
412 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800413 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300414 BUG_ON(!sched->tasks);
415 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200416
Ingo Molnarad236fd2009-09-11 12:12:54 +0200417 if (verbose)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300418 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200419
420 return task;
421}
422
423
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300424static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200425{
426 struct task_desc *task;
427 unsigned long i;
428
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300429 for (i = 0; i < sched->nr_tasks; i++) {
430 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200431 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200432 task->nr, task->comm, task->pid, task->nr_events);
433 }
434}
435
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300436static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200437{
438 struct task_desc *task1, *task2;
439 unsigned long i, j;
440
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300441 for (i = 0; i < sched->nr_tasks; i++) {
442 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200443 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300444 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200445 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300446 task2 = sched->tasks[j];
447 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200448 }
449}
450
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300451static void perf_sched__process_event(struct perf_sched *sched,
452 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200453{
454 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200455
mingo39aeb522009-09-14 20:04:48 +0200456 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200457 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300458 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200459 break;
460 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200461 if (atom->wait_sem)
462 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200463 BUG_ON(ret);
464 break;
465 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200466 if (atom->wait_sem)
467 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200468 BUG_ON(ret);
469 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200470 case SCHED_EVENT_MIGRATION:
471 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200472 default:
473 BUG_ON(1);
474 }
475}
476
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200477static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200478{
479 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200480 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200481 int err;
482
483 err = getrusage(RUSAGE_SELF, &ru);
484 BUG_ON(err);
485
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300486 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
487 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200488
489 return sum;
490}
491
Yunlong Song939cda5212015-03-31 21:46:34 +0800492static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200493{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800494 struct perf_event_attr attr;
Yunlong Song939cda5212015-03-31 21:46:34 +0800495 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800496 int fd;
Yunlong Song939cda5212015-03-31 21:46:34 +0800497 struct rlimit limit;
498 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800499
500 memset(&attr, 0, sizeof(attr));
501
502 attr.type = PERF_TYPE_SOFTWARE;
503 attr.config = PERF_COUNT_SW_TASK_CLOCK;
504
Yunlong Song939cda5212015-03-31 21:46:34 +0800505force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200506 fd = sys_perf_event_open(&attr, 0, -1, -1,
507 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800508
Yunlong Song1aff59b2015-03-31 21:46:33 +0800509 if (fd < 0) {
Yunlong Song939cda5212015-03-31 21:46:34 +0800510 if (errno == EMFILE) {
511 if (sched->force) {
512 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
513 limit.rlim_cur += sched->nr_tasks - cur_task;
514 if (limit.rlim_cur > limit.rlim_max) {
515 limit.rlim_max = limit.rlim_cur;
516 need_privilege = true;
517 }
518 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
519 if (need_privilege && errno == EPERM)
520 strcpy(info, "Need privilege\n");
521 } else
522 goto force_again;
523 } else
524 strcpy(info, "Have a try with -f option\n");
525 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900526 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda5212015-03-31 21:46:34 +0800527 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300528 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800529 exit(EXIT_FAILURE);
530 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800531 return fd;
532}
533
534static u64 get_cpu_usage_nsec_self(int fd)
535{
536 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200537 int ret;
538
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800539 ret = read(fd, &runtime, sizeof(runtime));
540 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200541
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800542 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200543}
544
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300545struct sched_thread_parms {
546 struct task_desc *task;
547 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800548 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300549};
550
Ingo Molnarec156762009-09-11 12:12:54 +0200551static void *thread_func(void *ctx)
552{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300553 struct sched_thread_parms *parms = ctx;
554 struct task_desc *this_task = parms->task;
555 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200556 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200557 unsigned long i, ret;
558 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800559 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200560
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300561 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300562
Ingo Molnarec156762009-09-11 12:12:54 +0200563 sprintf(comm2, ":%s", this_task->comm);
564 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300565 if (fd < 0)
566 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200567again:
568 ret = sem_post(&this_task->ready_for_work);
569 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300570 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200571 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300572 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200573 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200574
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800575 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200576
577 for (i = 0; i < this_task->nr_events; i++) {
578 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300579 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200580 }
581
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800582 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200583 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200584 ret = sem_post(&this_task->work_done_sem);
585 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200586
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300587 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200588 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300589 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200590 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200591
592 goto again;
593}
594
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300595static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200596{
597 struct task_desc *task;
598 pthread_attr_t attr;
599 unsigned long i;
600 int err;
601
602 err = pthread_attr_init(&attr);
603 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200604 err = pthread_attr_setstacksize(&attr,
605 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200606 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300607 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200608 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300609 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200610 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300611 for (i = 0; i < sched->nr_tasks; i++) {
612 struct sched_thread_parms *parms = malloc(sizeof(*parms));
613 BUG_ON(parms == NULL);
614 parms->task = task = sched->tasks[i];
615 parms->sched = sched;
Yunlong Song939cda5212015-03-31 21:46:34 +0800616 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200617 sem_init(&task->sleep_sem, 0, 0);
618 sem_init(&task->ready_for_work, 0, 0);
619 sem_init(&task->work_done_sem, 0, 0);
620 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300621 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200622 BUG_ON(err);
623 }
624}
625
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300626static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200627{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200628 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200629 struct task_desc *task;
630 unsigned long i, ret;
631
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300632 sched->start_time = get_nsecs();
633 sched->cpu_usage = 0;
634 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200635
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300636 for (i = 0; i < sched->nr_tasks; i++) {
637 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200638 ret = sem_wait(&task->ready_for_work);
639 BUG_ON(ret);
640 sem_init(&task->ready_for_work, 0, 0);
641 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300642 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200643 BUG_ON(ret);
644
645 cpu_usage_0 = get_cpu_usage_nsec_parent();
646
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300647 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200648
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300649 for (i = 0; i < sched->nr_tasks; i++) {
650 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200651 ret = sem_wait(&task->work_done_sem);
652 BUG_ON(ret);
653 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300654 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200655 task->cpu_usage = 0;
656 }
657
658 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300659 if (!sched->runavg_cpu_usage)
660 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800661 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 +0200662
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300663 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
664 if (!sched->runavg_parent_cpu_usage)
665 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800666 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
667 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200668
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300669 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200670 BUG_ON(ret);
671
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300672 for (i = 0; i < sched->nr_tasks; i++) {
673 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200674 sem_init(&task->sleep_sem, 0, 0);
675 task->curr_event = 0;
676 }
677}
678
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300679static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200680{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500681 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200682
683 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200685 T1 = get_nsecs();
686
687 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300688 sched->sum_runtime += delta;
689 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200690
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300691 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200692 if (delta < avg_delta)
693 fluct = avg_delta - delta;
694 else
695 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300696 sched->sum_fluct += fluct;
697 if (!sched->run_avg)
698 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800699 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200700
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300701 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200702
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300703 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200704
Ingo Molnarad236fd2009-09-11 12:12:54 +0200705 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300706 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200707
708#if 0
709 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200710 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300711 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200712 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200713 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300714 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
715 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200716#endif
717
Ingo Molnarad236fd2009-09-11 12:12:54 +0200718 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200719
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300720 if (sched->nr_sleep_corrections)
721 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
722 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200723}
724
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300725static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200726{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200727 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200728
729 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300730 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200731 T1 = get_nsecs();
732
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200733 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200734
735 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300736 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200737 T1 = get_nsecs();
738
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200739 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200740}
741
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300742static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300743replay_wakeup_event(struct perf_sched *sched,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300744 struct perf_evsel *evsel, struct perf_sample *sample,
745 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200746{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300747 const char *comm = perf_evsel__strval(evsel, sample, "comm");
748 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200749 struct task_desc *waker, *wakee;
750
751 if (verbose) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300752 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200753
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300754 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200755 }
756
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300757 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300758 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200759
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300760 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300761 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200762}
763
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300764static int replay_switch_event(struct perf_sched *sched,
765 struct perf_evsel *evsel,
766 struct perf_sample *sample,
767 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200768{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300769 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
770 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
771 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
772 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
773 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300774 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300775 u64 timestamp0, timestamp = sample->time;
776 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200777 s64 delta;
778
Ingo Molnarad236fd2009-09-11 12:12:54 +0200779 if (verbose)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300780 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200781
Ingo Molnarfbf94822009-09-11 12:12:54 +0200782 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300783 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200784
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300785 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200786 if (timestamp0)
787 delta = timestamp - timestamp0;
788 else
789 delta = 0;
790
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300791 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900792 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300793 return -1;
794 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200795
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300796 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
797 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200798
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300799 prev = register_pid(sched, prev_pid, prev_comm);
800 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200801
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300802 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200803
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300804 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300805 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300806
807 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200808}
809
David Aherncb627502013-08-07 22:50:47 -0400810static int replay_fork_event(struct perf_sched *sched,
811 union perf_event *event,
812 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200813{
David Aherncb627502013-08-07 22:50:47 -0400814 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300815
Adrian Hunter314add62013-08-27 11:23:03 +0300816 child = machine__findnew_thread(machine, event->fork.pid,
817 event->fork.tid);
818 parent = machine__findnew_thread(machine, event->fork.ppid,
819 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400820
821 if (child == NULL || parent == NULL) {
822 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
823 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300824 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200825 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300826
David Aherncb627502013-08-07 22:50:47 -0400827 if (verbose) {
828 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200829 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
830 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400831 }
832
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200833 register_pid(sched, parent->tid, thread__comm_str(parent));
834 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300835out_put:
836 thread__put(child);
837 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300838 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200839}
840
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200841struct sort_dimension {
842 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200843 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200844 struct list_head list;
845};
846
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200847static int
mingo39aeb522009-09-14 20:04:48 +0200848thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200849{
850 struct sort_dimension *sort;
851 int ret = 0;
852
Ingo Molnarb5fae122009-09-11 12:12:54 +0200853 BUG_ON(list_empty(list));
854
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200855 list_for_each_entry(sort, list, list) {
856 ret = sort->cmp(l, r);
857 if (ret)
858 return ret;
859 }
860
861 return ret;
862}
863
mingo39aeb522009-09-14 20:04:48 +0200864static struct work_atoms *
Ingo Molnarb5fae122009-09-11 12:12:54 +0200865thread_atoms_search(struct rb_root *root, struct thread *thread,
866 struct list_head *sort_list)
867{
868 struct rb_node *node = root->rb_node;
mingo39aeb522009-09-14 20:04:48 +0200869 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200870
871 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200872 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200873 int cmp;
874
mingo39aeb522009-09-14 20:04:48 +0200875 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200876
877 cmp = thread_lat_cmp(sort_list, &key, atoms);
878 if (cmp > 0)
879 node = node->rb_left;
880 else if (cmp < 0)
881 node = node->rb_right;
882 else {
883 BUG_ON(thread != atoms->thread);
884 return atoms;
885 }
886 }
887 return NULL;
888}
889
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200890static void
mingo39aeb522009-09-14 20:04:48 +0200891__thread_latency_insert(struct rb_root *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200892 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200893{
894 struct rb_node **new = &(root->rb_node), *parent = NULL;
895
896 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200897 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200898 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200899
mingo39aeb522009-09-14 20:04:48 +0200900 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200901 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200902
903 cmp = thread_lat_cmp(sort_list, data, this);
904
905 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200906 new = &((*new)->rb_left);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200907 else
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200908 new = &((*new)->rb_right);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200909 }
910
911 rb_link_node(&data->node, parent, new);
912 rb_insert_color(&data->node, root);
913}
914
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300915static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200916{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200917 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300918 if (!atoms) {
919 pr_err("No memory at %s\n", __func__);
920 return -1;
921 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200922
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300923 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +0200924 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300925 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300926 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200927}
928
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300929static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200930{
931 const char *str = TASK_STATE_TO_CHAR_STR;
932
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300933 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200934}
935
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300936static int
mingo39aeb522009-09-14 20:04:48 +0200937add_sched_out_event(struct work_atoms *atoms,
938 char run_state,
939 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200940{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200941 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300942 if (!atom) {
943 pr_err("Non memory at %s", __func__);
944 return -1;
945 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200946
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200947 atom->sched_out_time = timestamp;
948
mingo39aeb522009-09-14 20:04:48 +0200949 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200950 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200951 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +0200952 }
953
mingo39aeb522009-09-14 20:04:48 +0200954 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300955 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200956}
957
958static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300959add_runtime_event(struct work_atoms *atoms, u64 delta,
960 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +0200961{
962 struct work_atom *atom;
963
964 BUG_ON(list_empty(&atoms->work_list));
965
966 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
967
968 atom->runtime += delta;
969 atoms->total_runtime += delta;
970}
971
972static void
973add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200974{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200975 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200976 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200977
mingo39aeb522009-09-14 20:04:48 +0200978 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200979 return;
980
mingo39aeb522009-09-14 20:04:48 +0200981 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200982
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200983 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200984 return;
985
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200986 if (timestamp < atom->wake_up_time) {
987 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200988 return;
989 }
990
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200991 atom->state = THREAD_SCHED_IN;
992 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200993
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200994 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +0200995 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100996 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +0200997 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100998 atoms->max_lat_at = timestamp;
999 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001000 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001001}
1002
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001003static int latency_switch_event(struct perf_sched *sched,
1004 struct perf_evsel *evsel,
1005 struct perf_sample *sample,
1006 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001007{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001008 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1009 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1010 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001011 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001012 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001013 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001014 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001015 s64 delta;
1016
mingo39aeb522009-09-14 20:04:48 +02001017 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001018
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001019 timestamp0 = sched->cpu_last_switched[cpu];
1020 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001021 if (timestamp0)
1022 delta = timestamp - timestamp0;
1023 else
1024 delta = 0;
1025
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001026 if (delta < 0) {
1027 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1028 return -1;
1029 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001030
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001031 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1032 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001033 if (sched_out == NULL || sched_in == NULL)
1034 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001035
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001036 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001037 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001038 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001039 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001040 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001041 if (!out_events) {
1042 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001043 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001044 }
mingo39aeb522009-09-14 20:04:48 +02001045 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001046 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001047 return -1;
mingo39aeb522009-09-14 20:04:48 +02001048
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001049 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001050 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001051 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001052 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001053 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001054 if (!in_events) {
1055 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001056 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001057 }
mingo39aeb522009-09-14 20:04:48 +02001058 /*
1059 * Take came in we have not heard about yet,
1060 * add in an initial atom in runnable state:
1061 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001062 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001063 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001064 }
1065 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001066 err = 0;
1067out_put:
1068 thread__put(sched_out);
1069 thread__put(sched_in);
1070 return err;
mingo39aeb522009-09-14 20:04:48 +02001071}
1072
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001073static int latency_runtime_event(struct perf_sched *sched,
1074 struct perf_evsel *evsel,
1075 struct perf_sample *sample,
1076 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001077{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001078 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1079 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001080 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001081 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001082 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001083 int cpu = sample->cpu, err = -1;
1084
1085 if (thread == NULL)
1086 return -1;
mingo39aeb522009-09-14 20:04:48 +02001087
1088 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001089 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001090 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001091 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001092 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001093 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001094 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001095 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001096 }
1097 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001098 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001099 }
1100
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001101 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001102 err = 0;
1103out_put:
1104 thread__put(thread);
1105 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001106}
1107
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001108static int latency_wakeup_event(struct perf_sched *sched,
1109 struct perf_evsel *evsel,
1110 struct perf_sample *sample,
1111 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001112{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001113 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001114 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001115 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001116 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001117 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001118 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001119
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001120 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001121 if (wakee == NULL)
1122 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001123 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001124 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001125 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001126 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001127 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001128 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001129 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001130 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001131 }
1132 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001133 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001134 }
1135
mingo39aeb522009-09-14 20:04:48 +02001136 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001137
mingo39aeb522009-09-14 20:04:48 +02001138 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001139
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001140 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001141 * As we do not guarantee the wakeup event happens when
1142 * task is out of run queue, also may happen when task is
1143 * on run queue and wakeup only change ->state to TASK_RUNNING,
1144 * then we should not set the ->wake_up_time when wake up a
1145 * task which is on run queue.
1146 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001147 * You WILL be missing events if you've recorded only
1148 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001149 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001150 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001151 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001152 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001153
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001154 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001155 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001156 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001157 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001158 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001159
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001160 atom->state = THREAD_WAIT_CPU;
1161 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001162out_ok:
1163 err = 0;
1164out_put:
1165 thread__put(wakee);
1166 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001167}
1168
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001169static int latency_migrate_task_event(struct perf_sched *sched,
1170 struct perf_evsel *evsel,
1171 struct perf_sample *sample,
1172 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001173{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001174 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001175 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001176 struct work_atoms *atoms;
1177 struct work_atom *atom;
1178 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001179 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001180
1181 /*
1182 * Only need to worry about migration when profiling one CPU.
1183 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001184 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001185 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001186
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001187 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001188 if (migrant == NULL)
1189 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001190 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001191 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001192 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001193 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001194 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001195 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001196 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001197 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001198 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001199 }
1200 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001201 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001202 }
1203
1204 BUG_ON(list_empty(&atoms->work_list));
1205
1206 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1207 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1208
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001209 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001210
1211 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001212 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001213 err = 0;
1214out_put:
1215 thread__put(migrant);
1216 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001217}
1218
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001219static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001220{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001221 int i;
1222 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001223 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001224 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001225
mingo39aeb522009-09-14 20:04:48 +02001226 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001227 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001228 /*
1229 * Ignore idle threads:
1230 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001231 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001232 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001233
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001234 sched->all_runtime += work_list->total_runtime;
1235 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001236
Josef Bacik2f80dd42015-05-22 09:18:40 -04001237 if (work_list->num_merged > 1)
1238 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1239 else
1240 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001241
mingo08f69e62009-09-14 18:30:44 +02001242 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001243 printf(" ");
1244
mingo39aeb522009-09-14 20:04:48 +02001245 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001246 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001247
Namhyung Kim99620a52016-10-24 11:02:45 +09001248 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 -03001249 (double)work_list->total_runtime / NSEC_PER_MSEC,
1250 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1251 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001252 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001253}
1254
mingo39aeb522009-09-14 20:04:48 +02001255static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001256{
Jiri Olsa0014de12015-11-02 12:10:25 +01001257 if (l->thread == r->thread)
1258 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001259 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001260 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001261 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001262 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001263 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001264}
1265
mingo39aeb522009-09-14 20:04:48 +02001266static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001267{
1268 u64 avgl, avgr;
1269
1270 if (!l->nb_atoms)
1271 return -1;
1272
1273 if (!r->nb_atoms)
1274 return 1;
1275
1276 avgl = l->total_lat / l->nb_atoms;
1277 avgr = r->total_lat / r->nb_atoms;
1278
1279 if (avgl < avgr)
1280 return -1;
1281 if (avgl > avgr)
1282 return 1;
1283
1284 return 0;
1285}
1286
mingo39aeb522009-09-14 20:04:48 +02001287static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001288{
1289 if (l->max_lat < r->max_lat)
1290 return -1;
1291 if (l->max_lat > r->max_lat)
1292 return 1;
1293
1294 return 0;
1295}
1296
mingo39aeb522009-09-14 20:04:48 +02001297static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001298{
1299 if (l->nb_atoms < r->nb_atoms)
1300 return -1;
1301 if (l->nb_atoms > r->nb_atoms)
1302 return 1;
1303
1304 return 0;
1305}
1306
mingo39aeb522009-09-14 20:04:48 +02001307static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001308{
1309 if (l->total_runtime < r->total_runtime)
1310 return -1;
1311 if (l->total_runtime > r->total_runtime)
1312 return 1;
1313
1314 return 0;
1315}
1316
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001317static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001318{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001319 size_t i;
1320 static struct sort_dimension avg_sort_dimension = {
1321 .name = "avg",
1322 .cmp = avg_cmp,
1323 };
1324 static struct sort_dimension max_sort_dimension = {
1325 .name = "max",
1326 .cmp = max_cmp,
1327 };
1328 static struct sort_dimension pid_sort_dimension = {
1329 .name = "pid",
1330 .cmp = pid_cmp,
1331 };
1332 static struct sort_dimension runtime_sort_dimension = {
1333 .name = "runtime",
1334 .cmp = runtime_cmp,
1335 };
1336 static struct sort_dimension switch_sort_dimension = {
1337 .name = "switch",
1338 .cmp = switch_cmp,
1339 };
1340 struct sort_dimension *available_sorts[] = {
1341 &pid_sort_dimension,
1342 &avg_sort_dimension,
1343 &max_sort_dimension,
1344 &switch_sort_dimension,
1345 &runtime_sort_dimension,
1346 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001347
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001348 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001349 if (!strcmp(available_sorts[i]->name, tok)) {
1350 list_add_tail(&available_sorts[i]->list, list);
1351
1352 return 0;
1353 }
1354 }
1355
1356 return -1;
1357}
1358
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001359static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001360{
1361 struct rb_node *node;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001362 struct rb_root *root = &sched->atom_root;
1363again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001364 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001365 struct work_atoms *data;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001366 node = rb_first(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001367 if (!node)
1368 break;
1369
Josef Bacik2f80dd42015-05-22 09:18:40 -04001370 rb_erase(node, root);
mingo39aeb522009-09-14 20:04:48 +02001371 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001372 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001373 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001374 if (root == &sched->atom_root) {
1375 root = &sched->merged_atom_root;
1376 goto again;
1377 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001378}
1379
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001380static int process_sched_wakeup_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001381 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001382 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001383 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001384{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001385 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001386
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001387 if (sched->tp_handler->wakeup_event)
1388 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001389
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001390 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001391}
1392
Jiri Olsaa151a372016-04-12 15:29:29 +02001393union map_priv {
1394 void *ptr;
1395 bool color;
1396};
1397
1398static bool thread__has_color(struct thread *thread)
1399{
1400 union map_priv priv = {
1401 .ptr = thread__priv(thread),
1402 };
1403
1404 return priv.color;
1405}
1406
1407static struct thread*
1408map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1409{
1410 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1411 union map_priv priv = {
1412 .color = false,
1413 };
1414
1415 if (!sched->map.color_pids || !thread || thread__priv(thread))
1416 return thread;
1417
1418 if (thread_map__has(sched->map.color_pids, tid))
1419 priv.color = true;
1420
1421 thread__set_priv(thread, priv.ptr);
1422 return thread;
1423}
1424
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001425static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
1426 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001427{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001428 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1429 struct thread *sched_in;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001430 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001431 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001432 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001433 int i, this_cpu = sample->cpu;
1434 int cpus_nr;
1435 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001436 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001437 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001438
1439 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1440
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001441 if (this_cpu > sched->max_cpu)
1442 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001443
Jiri Olsa99623c62016-04-12 15:29:26 +02001444 if (sched->map.comp) {
1445 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1446 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1447 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1448 new_cpu = true;
1449 }
1450 } else
1451 cpus_nr = sched->max_cpu;
1452
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001453 timestamp0 = sched->cpu_last_switched[this_cpu];
1454 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001455 if (timestamp0)
1456 delta = timestamp - timestamp0;
1457 else
1458 delta = 0;
1459
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001460 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001461 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001462 return -1;
1463 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001464
Jiri Olsaa151a372016-04-12 15:29:29 +02001465 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001466 if (sched_in == NULL)
1467 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001468
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001469 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001470
1471 printf(" ");
1472
1473 new_shortname = 0;
1474 if (!sched_in->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001475 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1476 /*
1477 * Don't allocate a letter-number for swapper:0
1478 * as a shortname. Instead, we use '.' for it.
1479 */
1480 sched_in->shortname[0] = '.';
1481 sched_in->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001482 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001483 sched_in->shortname[0] = sched->next_shortname1;
1484 sched_in->shortname[1] = sched->next_shortname2;
1485
1486 if (sched->next_shortname1 < 'Z') {
1487 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001488 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001489 sched->next_shortname1 = 'A';
1490 if (sched->next_shortname2 < '9')
1491 sched->next_shortname2++;
1492 else
1493 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001494 }
1495 }
1496 new_shortname = 1;
1497 }
1498
Jiri Olsa99623c62016-04-12 15:29:26 +02001499 for (i = 0; i < cpus_nr; i++) {
1500 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001501 struct thread *curr_thread = sched->curr_thread[cpu];
1502 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001503 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001504
1505 if (curr_thread && thread__has_color(curr_thread))
1506 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001507
Jiri Olsa73643bb2016-04-12 15:29:31 +02001508 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1509 continue;
1510
Jiri Olsacf294f22016-04-12 15:29:30 +02001511 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1512 cpu_color = COLOR_CPUS;
1513
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001514 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001515 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001516 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001517 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001518
Dongsheng6bcab4e2014-05-06 14:39:01 +09001519 if (sched->curr_thread[cpu])
Jiri Olsaa151a372016-04-12 15:29:29 +02001520 color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname);
Dongsheng6bcab4e2014-05-06 14:39:01 +09001521 else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001522 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001523 }
1524
Jiri Olsa73643bb2016-04-12 15:29:31 +02001525 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1526 goto out;
1527
Namhyung Kim99620a52016-10-24 11:02:45 +09001528 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1529 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Namhyung Kime107f122016-10-24 11:02:44 +09001530 if (new_shortname || (verbose && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001531 const char *pid_color = color;
1532
1533 if (thread__has_color(sched_in))
1534 pid_color = COLOR_PIDS;
1535
1536 color_fprintf(stdout, pid_color, "%s => %s:%d",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001537 sched_in->shortname, thread__comm_str(sched_in), sched_in->tid);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001538 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001539
Jiri Olsa99623c62016-04-12 15:29:26 +02001540 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001541 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001542
Jiri Olsa73643bb2016-04-12 15:29:31 +02001543out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001544 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001545
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001546 thread__put(sched_in);
1547
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001548 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001549}
1550
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001551static int process_sched_switch_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001552 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001553 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001554 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001555{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001556 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001557 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001558 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1559 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001560
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001561 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001562 /*
1563 * Are we trying to switch away a PID that is
1564 * not current?
1565 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001566 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001567 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001568 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001569
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001570 if (sched->tp_handler->switch_event)
1571 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001572
1573 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001574 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001575}
1576
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001577static int process_sched_runtime_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001578 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001579 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001580 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001581{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001582 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001583
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001584 if (sched->tp_handler->runtime_event)
1585 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001586
1587 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001588}
1589
David Aherncb627502013-08-07 22:50:47 -04001590static int perf_sched__process_fork_event(struct perf_tool *tool,
1591 union perf_event *event,
1592 struct perf_sample *sample,
1593 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001594{
1595 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1596
David Aherncb627502013-08-07 22:50:47 -04001597 /* run the fork event through the perf machineruy */
1598 perf_event__process_fork(tool, event, sample, machine);
1599
1600 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001601 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001602 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001603
1604 return 0;
1605}
1606
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001607static int process_sched_migrate_task_event(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001608 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001609 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001610 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001611{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001612 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001613
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001614 if (sched->tp_handler->migrate_task_event)
1615 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001616
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001617 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001618}
1619
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001620typedef int (*tracepoint_handler)(struct perf_tool *tool,
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001621 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001622 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001623 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001624
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001625static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1626 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001627 struct perf_sample *sample,
1628 struct perf_evsel *evsel,
1629 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001630{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001631 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001632
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001633 if (evsel->handler != NULL) {
1634 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001635 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001636 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001637
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001638 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001639}
1640
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001641static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001642{
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001643 const struct perf_evsel_str_handler handlers[] = {
1644 { "sched:sched_switch", process_sched_switch_event, },
1645 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1646 { "sched:sched_wakeup", process_sched_wakeup_event, },
1647 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001648 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1649 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001650 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001651 struct perf_data_file file = {
1652 .path = input_name,
1653 .mode = PERF_DATA_MODE_READ,
Yunlong Songf0dd3302015-03-31 21:46:35 +08001654 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001655 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001656 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001657
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001658 session = perf_session__new(&file, false, &sched->tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001659 if (session == NULL) {
1660 pr_debug("No Memory for session\n");
1661 return -1;
1662 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001663
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001664 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001665
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001666 if (perf_session__set_tracepoints_handlers(session, handlers))
1667 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001668
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001669 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001670 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001671 if (err) {
1672 pr_err("Failed to process events, error %d", err);
1673 goto out_delete;
1674 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001675
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001676 sched->nr_events = session->evlist->stats.nr_events[0];
1677 sched->nr_lost_events = session->evlist->stats.total_lost;
1678 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001679 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001680
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001681 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001682out_delete:
1683 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001684 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001685}
1686
David Ahern49394a22016-11-16 15:06:29 +09001687/*
1688 * scheduling times are printed as msec.usec
1689 */
1690static inline void print_sched_time(unsigned long long nsecs, int width)
1691{
1692 unsigned long msecs;
1693 unsigned long usecs;
1694
1695 msecs = nsecs / NSEC_PER_MSEC;
1696 nsecs -= msecs * NSEC_PER_MSEC;
1697 usecs = nsecs / NSEC_PER_USEC;
1698 printf("%*lu.%03lu ", width, msecs, usecs);
1699}
1700
1701/*
1702 * returns runtime data for event, allocating memory for it the
1703 * first time it is used.
1704 */
1705static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel)
1706{
1707 struct evsel_runtime *r = evsel->priv;
1708
1709 if (r == NULL) {
1710 r = zalloc(sizeof(struct evsel_runtime));
1711 evsel->priv = r;
1712 }
1713
1714 return r;
1715}
1716
1717/*
1718 * save last time event was seen per cpu
1719 */
1720static void perf_evsel__save_time(struct perf_evsel *evsel,
1721 u64 timestamp, u32 cpu)
1722{
1723 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1724
1725 if (r == NULL)
1726 return;
1727
1728 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1729 int i, n = __roundup_pow_of_two(cpu+1);
1730 void *p = r->last_time;
1731
1732 p = realloc(r->last_time, n * sizeof(u64));
1733 if (!p)
1734 return;
1735
1736 r->last_time = p;
1737 for (i = r->ncpu; i < n; ++i)
1738 r->last_time[i] = (u64) 0;
1739
1740 r->ncpu = n;
1741 }
1742
1743 r->last_time[cpu] = timestamp;
1744}
1745
1746/* returns last time this event was seen on the given cpu */
1747static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu)
1748{
1749 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1750
1751 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1752 return 0;
1753
1754 return r->last_time[cpu];
1755}
1756
1757static int comm_width = 20;
1758
1759static char *timehist_get_commstr(struct thread *thread)
1760{
1761 static char str[32];
1762 const char *comm = thread__comm_str(thread);
1763 pid_t tid = thread->tid;
1764 pid_t pid = thread->pid_;
1765 int n;
1766
1767 if (pid == 0)
1768 n = scnprintf(str, sizeof(str), "%s", comm);
1769
1770 else if (tid != pid)
1771 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1772
1773 else
1774 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1775
1776 if (n > comm_width)
1777 comm_width = n;
1778
1779 return str;
1780}
1781
1782static void timehist_header(void)
1783{
1784 printf("%15s %6s ", "time", "cpu");
1785
1786 printf(" %-20s %9s %9s %9s",
1787 "task name", "wait time", "sch delay", "run time");
1788
1789 printf("\n");
1790
1791 /*
1792 * units row
1793 */
1794 printf("%15s %-6s ", "", "");
1795
1796 printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)");
1797
1798 /*
1799 * separator
1800 */
1801 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1802
1803 printf(" %.20s %.9s %.9s %.9s",
1804 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1805 graph_dotted_line);
1806
1807 printf("\n");
1808}
1809
1810static void timehist_print_sample(struct perf_sample *sample,
1811 struct thread *thread)
1812{
1813 struct thread_runtime *tr = thread__priv(thread);
1814 char tstr[64];
1815
1816 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
1817 printf("%15s [%04d] ", tstr, sample->cpu);
1818
1819 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
1820
1821 print_sched_time(tr->dt_wait, 6);
1822 print_sched_time(tr->dt_delay, 6);
1823 print_sched_time(tr->dt_run, 6);
1824 printf("\n");
1825}
1826
1827/*
1828 * Explanation of delta-time stats:
1829 *
1830 * t = time of current schedule out event
1831 * tprev = time of previous sched out event
1832 * also time of schedule-in event for current task
1833 * last_time = time of last sched change event for current task
1834 * (i.e, time process was last scheduled out)
1835 * ready_to_run = time of wakeup for current task
1836 *
1837 * -----|------------|------------|------------|------
1838 * last ready tprev t
1839 * time to run
1840 *
1841 * |-------- dt_wait --------|
1842 * |- dt_delay -|-- dt_run --|
1843 *
1844 * dt_run = run time of current task
1845 * dt_wait = time between last schedule out event for task and tprev
1846 * represents time spent off the cpu
1847 * dt_delay = time between wakeup and schedule-in of task
1848 */
1849
1850static void timehist_update_runtime_stats(struct thread_runtime *r,
1851 u64 t, u64 tprev)
1852{
1853 r->dt_delay = 0;
1854 r->dt_wait = 0;
1855 r->dt_run = 0;
1856 if (tprev) {
1857 r->dt_run = t - tprev;
1858 if (r->ready_to_run) {
1859 if (r->ready_to_run > tprev)
1860 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
1861 else
1862 r->dt_delay = tprev - r->ready_to_run;
1863 }
1864
1865 if (r->last_time > tprev)
1866 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
1867 else if (r->last_time)
1868 r->dt_wait = tprev - r->last_time;
1869 }
1870
1871 update_stats(&r->run_stats, r->dt_run);
1872 r->total_run_time += r->dt_run;
1873}
1874
1875static bool is_idle_sample(struct perf_sample *sample,
1876 struct perf_evsel *evsel)
1877{
1878 /* pid 0 == swapper == idle task */
1879 if (sample->pid == 0)
1880 return true;
1881
1882 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) {
1883 if (perf_evsel__intval(evsel, sample, "prev_pid") == 0)
1884 return true;
1885 }
1886 return false;
1887}
1888
1889/*
1890 * Track idle stats per cpu by maintaining a local thread
1891 * struct for the idle task on each cpu.
1892 */
1893static int init_idle_threads(int ncpu)
1894{
1895 int i;
1896
1897 idle_threads = zalloc(ncpu * sizeof(struct thread *));
1898 if (!idle_threads)
1899 return -ENOMEM;
1900
1901 idle_max_cpu = ncpu - 1;
1902
1903 /* allocate the actual thread struct if needed */
1904 for (i = 0; i < ncpu; ++i) {
1905 idle_threads[i] = thread__new(0, 0);
1906 if (idle_threads[i] == NULL)
1907 return -ENOMEM;
1908
1909 thread__set_comm(idle_threads[i], idle_comm, 0);
1910 }
1911
1912 return 0;
1913}
1914
1915static void free_idle_threads(void)
1916{
1917 int i;
1918
1919 if (idle_threads == NULL)
1920 return;
1921
1922 for (i = 0; i <= idle_max_cpu; ++i) {
1923 if ((idle_threads[i]))
1924 thread__delete(idle_threads[i]);
1925 }
1926
1927 free(idle_threads);
1928}
1929
1930static struct thread *get_idle_thread(int cpu)
1931{
1932 /*
1933 * expand/allocate array of pointers to local thread
1934 * structs if needed
1935 */
1936 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
1937 int i, j = __roundup_pow_of_two(cpu+1);
1938 void *p;
1939
1940 p = realloc(idle_threads, j * sizeof(struct thread *));
1941 if (!p)
1942 return NULL;
1943
1944 idle_threads = (struct thread **) p;
1945 i = idle_max_cpu ? idle_max_cpu + 1 : 0;
1946 for (; i < j; ++i)
1947 idle_threads[i] = NULL;
1948
1949 idle_max_cpu = j;
1950 }
1951
1952 /* allocate a new thread struct if needed */
1953 if (idle_threads[cpu] == NULL) {
1954 idle_threads[cpu] = thread__new(0, 0);
1955
1956 if (idle_threads[cpu]) {
1957 idle_threads[cpu]->tid = 0;
1958 thread__set_comm(idle_threads[cpu], idle_comm, 0);
1959 }
1960 }
1961
1962 return idle_threads[cpu];
1963}
1964
1965/*
1966 * handle runtime stats saved per thread
1967 */
1968static struct thread_runtime *thread__init_runtime(struct thread *thread)
1969{
1970 struct thread_runtime *r;
1971
1972 r = zalloc(sizeof(struct thread_runtime));
1973 if (!r)
1974 return NULL;
1975
1976 init_stats(&r->run_stats);
1977 thread__set_priv(thread, r);
1978
1979 return r;
1980}
1981
1982static struct thread_runtime *thread__get_runtime(struct thread *thread)
1983{
1984 struct thread_runtime *tr;
1985
1986 tr = thread__priv(thread);
1987 if (tr == NULL) {
1988 tr = thread__init_runtime(thread);
1989 if (tr == NULL)
1990 pr_debug("Failed to malloc memory for runtime data.\n");
1991 }
1992
1993 return tr;
1994}
1995
1996static struct thread *timehist_get_thread(struct perf_sample *sample,
1997 struct machine *machine,
1998 struct perf_evsel *evsel)
1999{
2000 struct thread *thread;
2001
2002 if (is_idle_sample(sample, evsel)) {
2003 thread = get_idle_thread(sample->cpu);
2004 if (thread == NULL)
2005 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2006
2007 } else {
2008 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2009 if (thread == NULL) {
2010 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2011 sample->tid);
2012 }
2013 }
2014
2015 return thread;
2016}
2017
David Ahern52df1382016-11-16 15:06:30 +09002018static bool timehist_skip_sample(struct perf_sched *sched,
2019 struct thread *thread)
David Ahern49394a22016-11-16 15:06:29 +09002020{
2021 bool rc = false;
2022
David Ahern52df1382016-11-16 15:06:30 +09002023 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002024 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002025 sched->skipped_samples++;
2026 }
David Ahern49394a22016-11-16 15:06:29 +09002027
2028 return rc;
2029}
2030
2031static int timehist_sched_wakeup_event(struct perf_tool *tool __maybe_unused,
2032 union perf_event *event __maybe_unused,
2033 struct perf_evsel *evsel,
2034 struct perf_sample *sample,
2035 struct machine *machine)
2036{
2037 struct thread *thread;
2038 struct thread_runtime *tr = NULL;
2039 /* want pid of awakened task not pid in sample */
2040 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2041
2042 thread = machine__findnew_thread(machine, 0, pid);
2043 if (thread == NULL)
2044 return -1;
2045
2046 tr = thread__get_runtime(thread);
2047 if (tr == NULL)
2048 return -1;
2049
2050 if (tr->ready_to_run == 0)
2051 tr->ready_to_run = sample->time;
2052
2053 return 0;
2054}
2055
David Ahern52df1382016-11-16 15:06:30 +09002056static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002057 union perf_event *event,
2058 struct perf_evsel *evsel,
2059 struct perf_sample *sample,
2060 struct machine *machine)
2061{
2062 struct addr_location al;
2063 struct thread *thread;
2064 struct thread_runtime *tr = NULL;
2065 u64 tprev;
2066 int rc = 0;
David Ahern52df1382016-11-16 15:06:30 +09002067 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002068
2069 if (machine__resolve(machine, &al, sample) < 0) {
2070 pr_err("problem processing %d event. skipping it\n",
2071 event->header.type);
2072 rc = -1;
2073 goto out;
2074 }
2075
2076 thread = timehist_get_thread(sample, machine, evsel);
2077 if (thread == NULL) {
2078 rc = -1;
2079 goto out;
2080 }
2081
David Ahern52df1382016-11-16 15:06:30 +09002082 if (timehist_skip_sample(sched, thread))
David Ahern49394a22016-11-16 15:06:29 +09002083 goto out;
2084
2085 tr = thread__get_runtime(thread);
2086 if (tr == NULL) {
2087 rc = -1;
2088 goto out;
2089 }
2090
2091 tprev = perf_evsel__get_time(evsel, sample->cpu);
2092
2093 timehist_update_runtime_stats(tr, sample->time, tprev);
David Ahern52df1382016-11-16 15:06:30 +09002094 if (!sched->summary_only)
2095 timehist_print_sample(sample, thread);
David Ahern49394a22016-11-16 15:06:29 +09002096
2097out:
2098 if (tr) {
2099 /* time of this sched_switch event becomes last time task seen */
2100 tr->last_time = sample->time;
2101
2102 /* sched out event for task so reset ready to run time */
2103 tr->ready_to_run = 0;
2104 }
2105
2106 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2107
2108 return rc;
2109}
2110
2111static int timehist_sched_switch_event(struct perf_tool *tool,
2112 union perf_event *event,
2113 struct perf_evsel *evsel,
2114 struct perf_sample *sample,
2115 struct machine *machine __maybe_unused)
2116{
2117 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2118}
2119
2120static int process_lost(struct perf_tool *tool __maybe_unused,
2121 union perf_event *event,
2122 struct perf_sample *sample,
2123 struct machine *machine __maybe_unused)
2124{
2125 char tstr[64];
2126
2127 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2128 printf("%15s ", tstr);
2129 printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
2130
2131 return 0;
2132}
2133
2134
David Ahern52df1382016-11-16 15:06:30 +09002135static void print_thread_runtime(struct thread *t,
2136 struct thread_runtime *r)
2137{
2138 double mean = avg_stats(&r->run_stats);
2139 float stddev;
2140
2141 printf("%*s %5d %9" PRIu64 " ",
2142 comm_width, timehist_get_commstr(t), t->ppid,
2143 (u64) r->run_stats.n);
2144
2145 print_sched_time(r->total_run_time, 8);
2146 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2147 print_sched_time(r->run_stats.min, 6);
2148 printf(" ");
2149 print_sched_time((u64) mean, 6);
2150 printf(" ");
2151 print_sched_time(r->run_stats.max, 6);
2152 printf(" ");
2153 printf("%5.2f", stddev);
2154 printf("\n");
2155}
2156
2157struct total_run_stats {
2158 u64 sched_count;
2159 u64 task_count;
2160 u64 total_run_time;
2161};
2162
2163static int __show_thread_runtime(struct thread *t, void *priv)
2164{
2165 struct total_run_stats *stats = priv;
2166 struct thread_runtime *r;
2167
2168 if (thread__is_filtered(t))
2169 return 0;
2170
2171 r = thread__priv(t);
2172 if (r && r->run_stats.n) {
2173 stats->task_count++;
2174 stats->sched_count += r->run_stats.n;
2175 stats->total_run_time += r->total_run_time;
2176 print_thread_runtime(t, r);
2177 }
2178
2179 return 0;
2180}
2181
2182static int show_thread_runtime(struct thread *t, void *priv)
2183{
2184 if (t->dead)
2185 return 0;
2186
2187 return __show_thread_runtime(t, priv);
2188}
2189
2190static int show_deadthread_runtime(struct thread *t, void *priv)
2191{
2192 if (!t->dead)
2193 return 0;
2194
2195 return __show_thread_runtime(t, priv);
2196}
2197
2198static void timehist_print_summary(struct perf_sched *sched,
2199 struct perf_session *session)
2200{
2201 struct machine *m = &session->machines.host;
2202 struct total_run_stats totals;
2203 u64 task_count;
2204 struct thread *t;
2205 struct thread_runtime *r;
2206 int i;
2207
2208 memset(&totals, 0, sizeof(totals));
2209
2210 if (comm_width < 30)
2211 comm_width = 30;
2212
2213 printf("\nRuntime summary\n");
2214 printf("%*s parent sched-in ", comm_width, "comm");
2215 printf(" run-time min-run avg-run max-run stddev\n");
2216 printf("%*s (count) ", comm_width, "");
2217 printf(" (msec) (msec) (msec) (msec) %%\n");
2218 printf("%.105s\n", graph_dotted_line);
2219
2220 machine__for_each_thread(m, show_thread_runtime, &totals);
2221 task_count = totals.task_count;
2222 if (!task_count)
2223 printf("<no still running tasks>\n");
2224
2225 printf("\nTerminated tasks:\n");
2226 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2227 if (task_count == totals.task_count)
2228 printf("<no terminated tasks>\n");
2229
2230 /* CPU idle stats not tracked when samples were skipped */
2231 if (sched->skipped_samples)
2232 return;
2233
2234 printf("\nIdle stats:\n");
2235 for (i = 0; i <= idle_max_cpu; ++i) {
2236 t = idle_threads[i];
2237 if (!t)
2238 continue;
2239
2240 r = thread__priv(t);
2241 if (r && r->run_stats.n) {
2242 totals.sched_count += r->run_stats.n;
2243 printf(" CPU %2d idle for ", i);
2244 print_sched_time(r->total_run_time, 6);
2245 printf(" msec\n");
2246 } else
2247 printf(" CPU %2d idle entire time window\n", i);
2248 }
2249
2250 printf("\n"
2251 " Total number of unique tasks: %" PRIu64 "\n"
2252 "Total number of context switches: %" PRIu64 "\n"
2253 " Total run time (msec): ",
2254 totals.task_count, totals.sched_count);
2255
2256 print_sched_time(totals.total_run_time, 2);
2257 printf("\n");
2258}
2259
David Ahern49394a22016-11-16 15:06:29 +09002260typedef int (*sched_handler)(struct perf_tool *tool,
2261 union perf_event *event,
2262 struct perf_evsel *evsel,
2263 struct perf_sample *sample,
2264 struct machine *machine);
2265
2266static int perf_timehist__process_sample(struct perf_tool *tool,
2267 union perf_event *event,
2268 struct perf_sample *sample,
2269 struct perf_evsel *evsel,
2270 struct machine *machine)
2271{
2272 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2273 int err = 0;
2274 int this_cpu = sample->cpu;
2275
2276 if (this_cpu > sched->max_cpu)
2277 sched->max_cpu = this_cpu;
2278
2279 if (evsel->handler != NULL) {
2280 sched_handler f = evsel->handler;
2281
2282 err = f(tool, event, evsel, sample, machine);
2283 }
2284
2285 return err;
2286}
2287
2288static int perf_sched__timehist(struct perf_sched *sched)
2289{
2290 const struct perf_evsel_str_handler handlers[] = {
2291 { "sched:sched_switch", timehist_sched_switch_event, },
2292 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2293 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2294 };
2295 struct perf_data_file file = {
2296 .path = input_name,
2297 .mode = PERF_DATA_MODE_READ,
2298 };
2299
2300 struct perf_session *session;
David Ahern52df1382016-11-16 15:06:30 +09002301 struct perf_evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002302 int err = -1;
2303
2304 /*
2305 * event handlers for timehist option
2306 */
2307 sched->tool.sample = perf_timehist__process_sample;
2308 sched->tool.mmap = perf_event__process_mmap;
2309 sched->tool.comm = perf_event__process_comm;
2310 sched->tool.exit = perf_event__process_exit;
2311 sched->tool.fork = perf_event__process_fork;
2312 sched->tool.lost = process_lost;
2313 sched->tool.attr = perf_event__process_attr;
2314 sched->tool.tracing_data = perf_event__process_tracing_data;
2315 sched->tool.build_id = perf_event__process_build_id;
2316
2317 sched->tool.ordered_events = true;
2318 sched->tool.ordering_requires_timestamps = true;
2319
2320 session = perf_session__new(&file, false, &sched->tool);
2321 if (session == NULL)
2322 return -ENOMEM;
2323
David Ahern52df1382016-11-16 15:06:30 +09002324 evlist = session->evlist;
2325
David Ahern49394a22016-11-16 15:06:29 +09002326 symbol__init(&session->header.env);
2327
2328 setup_pager();
2329
2330 /* setup per-evsel handlers */
2331 if (perf_session__set_tracepoints_handlers(session, handlers))
2332 goto out;
2333
2334 if (!perf_session__has_traces(session, "record -R"))
2335 goto out;
2336
2337 /* pre-allocate struct for per-CPU idle stats */
2338 sched->max_cpu = session->header.env.nr_cpus_online;
2339 if (sched->max_cpu == 0)
2340 sched->max_cpu = 4;
2341 if (init_idle_threads(sched->max_cpu))
2342 goto out;
2343
David Ahern52df1382016-11-16 15:06:30 +09002344 /* summary_only implies summary option, but don't overwrite summary if set */
2345 if (sched->summary_only)
2346 sched->summary = sched->summary_only;
2347
2348 if (!sched->summary_only)
2349 timehist_header();
David Ahern49394a22016-11-16 15:06:29 +09002350
2351 err = perf_session__process_events(session);
2352 if (err) {
2353 pr_err("Failed to process events, error %d", err);
2354 goto out;
2355 }
2356
David Ahern52df1382016-11-16 15:06:30 +09002357 sched->nr_events = evlist->stats.nr_events[0];
2358 sched->nr_lost_events = evlist->stats.total_lost;
2359 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
2360
2361 if (sched->summary)
2362 timehist_print_summary(sched, session);
2363
David Ahern49394a22016-11-16 15:06:29 +09002364out:
2365 free_idle_threads();
2366 perf_session__delete(session);
2367
2368 return err;
2369}
2370
2371
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002372static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002373{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002374 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002375 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002376 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
2377 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002378 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002379 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002380 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002381 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
2382 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002383 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002384 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002385 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002386 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
2387 sched->nr_context_switch_bugs, sched->nr_timestamps);
2388 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002389 printf(" (due to lost events?)");
2390 printf("\n");
2391 }
2392}
2393
Josef Bacik2f80dd42015-05-22 09:18:40 -04002394static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data)
2395{
2396 struct rb_node **new = &(root->rb_node), *parent = NULL;
2397 struct work_atoms *this;
2398 const char *comm = thread__comm_str(data->thread), *this_comm;
2399
2400 while (*new) {
2401 int cmp;
2402
2403 this = container_of(*new, struct work_atoms, node);
2404 parent = *new;
2405
2406 this_comm = thread__comm_str(this->thread);
2407 cmp = strcmp(comm, this_comm);
2408 if (cmp > 0) {
2409 new = &((*new)->rb_left);
2410 } else if (cmp < 0) {
2411 new = &((*new)->rb_right);
2412 } else {
2413 this->num_merged++;
2414 this->total_runtime += data->total_runtime;
2415 this->nb_atoms += data->nb_atoms;
2416 this->total_lat += data->total_lat;
2417 list_splice(&data->work_list, &this->work_list);
2418 if (this->max_lat < data->max_lat) {
2419 this->max_lat = data->max_lat;
2420 this->max_lat_at = data->max_lat_at;
2421 }
2422 zfree(&data);
2423 return;
2424 }
2425 }
2426
2427 data->num_merged++;
2428 rb_link_node(&data->node, parent, new);
2429 rb_insert_color(&data->node, root);
2430}
2431
2432static void perf_sched__merge_lat(struct perf_sched *sched)
2433{
2434 struct work_atoms *data;
2435 struct rb_node *node;
2436
2437 if (sched->skip_merge)
2438 return;
2439
2440 while ((node = rb_first(&sched->atom_root))) {
2441 rb_erase(node, &sched->atom_root);
2442 data = rb_entry(node, struct work_atoms, node);
2443 __merge_work_atoms(&sched->merged_atom_root, data);
2444 }
2445}
2446
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002447static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002448{
2449 struct rb_node *next;
2450
2451 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04002452
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002453 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002454 return -1;
David Ahernad9def72013-08-07 22:50:44 -04002455
Josef Bacik2f80dd42015-05-22 09:18:40 -04002456 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002457 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002458
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002459 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
2460 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
2461 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002462
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002463 next = rb_first(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002464
2465 while (next) {
2466 struct work_atoms *work_list;
2467
2468 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002469 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002470 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002471 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002472 }
2473
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04002474 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002475 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03002476 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002477
2478 printf(" ---------------------------------------------------\n");
2479
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002480 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002481 printf("\n");
2482
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002483 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002484}
2485
Jiri Olsa99623c62016-04-12 15:29:26 +02002486static int setup_map_cpus(struct perf_sched *sched)
2487{
Jiri Olsa73643bb2016-04-12 15:29:31 +02002488 struct cpu_map *map;
2489
Jiri Olsa99623c62016-04-12 15:29:26 +02002490 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
2491
2492 if (sched->map.comp) {
2493 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02002494 if (!sched->map.comp_cpus)
2495 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02002496 }
2497
Jiri Olsa73643bb2016-04-12 15:29:31 +02002498 if (!sched->map.cpus_str)
2499 return 0;
2500
2501 map = cpu_map__new(sched->map.cpus_str);
2502 if (!map) {
2503 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
2504 return -1;
2505 }
2506
2507 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02002508 return 0;
2509}
2510
Jiri Olsaa151a372016-04-12 15:29:29 +02002511static int setup_color_pids(struct perf_sched *sched)
2512{
2513 struct thread_map *map;
2514
2515 if (!sched->map.color_pids_str)
2516 return 0;
2517
2518 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
2519 if (!map) {
2520 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
2521 return -1;
2522 }
2523
2524 sched->map.color_pids = map;
2525 return 0;
2526}
2527
Jiri Olsacf294f22016-04-12 15:29:30 +02002528static int setup_color_cpus(struct perf_sched *sched)
2529{
2530 struct cpu_map *map;
2531
2532 if (!sched->map.color_cpus_str)
2533 return 0;
2534
2535 map = cpu_map__new(sched->map.color_cpus_str);
2536 if (!map) {
2537 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
2538 return -1;
2539 }
2540
2541 sched->map.color_cpus = map;
2542 return 0;
2543}
2544
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002545static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002546{
Jiri Olsa99623c62016-04-12 15:29:26 +02002547 if (setup_map_cpus(sched))
2548 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02002549
Jiri Olsaa151a372016-04-12 15:29:29 +02002550 if (setup_color_pids(sched))
2551 return -1;
2552
Jiri Olsacf294f22016-04-12 15:29:30 +02002553 if (setup_color_cpus(sched))
2554 return -1;
2555
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002556 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002557 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002558 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002559 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002560 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002561}
2562
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002563static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002564{
2565 unsigned long i;
2566
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002567 calibrate_run_measurement_overhead(sched);
2568 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002569
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002570 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002571
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03002572 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002573 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002574
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002575 printf("nr_run_events: %ld\n", sched->nr_run_events);
2576 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
2577 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002578
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002579 if (sched->targetless_wakeups)
2580 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
2581 if (sched->multitarget_wakeups)
2582 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
2583 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002584 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002585 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002586
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002587 print_task_traces(sched);
2588 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002589
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002590 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002591 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002592 for (i = 0; i < sched->replay_repeat; i++)
2593 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03002594
2595 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002596}
2597
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002598static void setup_sorting(struct perf_sched *sched, const struct option *options,
2599 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002600{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002601 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002602
2603 for (tok = strtok_r(str, ", ", &tmp);
2604 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002605 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002606 usage_with_options_msg(usage_msg, options,
2607 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002608 }
2609 }
2610
2611 free(str);
2612
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002613 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02002614}
2615
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002616static int __cmd_record(int argc, const char **argv)
2617{
2618 unsigned int rec_argc, i, j;
2619 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002620 const char * const record_args[] = {
2621 "record",
2622 "-a",
2623 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002624 "-m", "1024",
2625 "-c", "1",
2626 "-e", "sched:sched_switch",
2627 "-e", "sched:sched_stat_wait",
2628 "-e", "sched:sched_stat_sleep",
2629 "-e", "sched:sched_stat_iowait",
2630 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002631 "-e", "sched:sched_process_fork",
2632 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09002633 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002634 "-e", "sched:sched_migrate_task",
2635 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002636
2637 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
2638 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2639
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02002640 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11002641 return -ENOMEM;
2642
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002643 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2644 rec_argv[i] = strdup(record_args[i]);
2645
2646 for (j = 1; j < (unsigned int)argc; j++, i++)
2647 rec_argv[i] = argv[j];
2648
2649 BUG_ON(i != rec_argc);
2650
2651 return cmd_record(i, rec_argv, NULL);
2652}
2653
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002654int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002655{
Adrian Hunter8a39df82013-10-22 10:34:15 +03002656 const char default_sort_order[] = "avg, max, switch, runtime";
2657 struct perf_sched sched = {
2658 .tool = {
2659 .sample = perf_sched__process_tracepoint_sample,
2660 .comm = perf_event__process_comm,
2661 .lost = perf_event__process_lost,
2662 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002663 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002664 },
2665 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
2666 .sort_list = LIST_HEAD_INIT(sched.sort_list),
2667 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
2668 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002669 .sort_order = default_sort_order,
2670 .replay_repeat = 10,
2671 .profile_cpu = -1,
2672 .next_shortname1 = 'A',
2673 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04002674 .skip_merge = 0,
Adrian Hunter8a39df82013-10-22 10:34:15 +03002675 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09002676 const struct option sched_options[] = {
2677 OPT_STRING('i', "input", &input_name, "file",
2678 "input file name"),
2679 OPT_INCR('v', "verbose", &verbose,
2680 "be more verbose (show symbol address, etc)"),
2681 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2682 "dump raw trace in ASCII"),
2683 OPT_END()
2684 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002685 const struct option latency_options[] = {
2686 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
2687 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002688 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
2689 "CPU to profile on"),
2690 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2691 "dump raw trace in ASCII"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04002692 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
2693 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002694 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002695 };
2696 const struct option replay_options[] = {
2697 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
2698 "repeat the workload replay N times (-1: infinite)"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002699 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2700 "dump raw trace in ASCII"),
Yunlong Song939cda5212015-03-31 21:46:34 +08002701 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002702 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002703 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002704 const struct option map_options[] = {
2705 OPT_BOOLEAN(0, "compact", &sched.map.comp,
2706 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02002707 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
2708 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02002709 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
2710 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02002711 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
2712 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09002713 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02002714 };
David Ahern49394a22016-11-16 15:06:29 +09002715 const struct option timehist_options[] = {
2716 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2717 "file", "vmlinux pathname"),
2718 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2719 "file", "kallsyms pathname"),
2720 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2721 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09002722 OPT_BOOLEAN('s', "summary", &sched.summary_only,
2723 "Show only syscall summary with statistics"),
2724 OPT_BOOLEAN('S', "with-summary", &sched.summary,
2725 "Show all syscalls and summary with statistics"),
David Ahern49394a22016-11-16 15:06:29 +09002726 OPT_PARENT(sched_options)
2727 };
2728
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002729 const char * const latency_usage[] = {
2730 "perf sched latency [<options>]",
2731 NULL
2732 };
2733 const char * const replay_usage[] = {
2734 "perf sched replay [<options>]",
2735 NULL
2736 };
Jiri Olsa99623c62016-04-12 15:29:26 +02002737 const char * const map_usage[] = {
2738 "perf sched map [<options>]",
2739 NULL
2740 };
David Ahern49394a22016-11-16 15:06:29 +09002741 const char * const timehist_usage[] = {
2742 "perf sched timehist [<options>]",
2743 NULL
2744 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002745 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09002746 "replay", "script",
2747 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002748 const char *sched_usage[] = {
2749 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002750 NULL
2751 };
2752 struct trace_sched_handler lat_ops = {
2753 .wakeup_event = latency_wakeup_event,
2754 .switch_event = latency_switch_event,
2755 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002756 .migrate_task_event = latency_migrate_task_event,
2757 };
2758 struct trace_sched_handler map_ops = {
2759 .switch_event = map_switch_event,
2760 };
2761 struct trace_sched_handler replay_ops = {
2762 .wakeup_event = replay_wakeup_event,
2763 .switch_event = replay_switch_event,
2764 .fork_event = replay_fork_event,
2765 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03002766 unsigned int i;
2767
2768 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
2769 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002770
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04002771 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
2772 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002773 if (!argc)
2774 usage_with_options(sched_usage, sched_options);
2775
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002776 /*
Ingo Molnar133dc4c32010-11-16 18:45:39 +01002777 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002778 */
Ingo Molnar133dc4c32010-11-16 18:45:39 +01002779 if (!strcmp(argv[0], "script"))
2780 return cmd_script(argc, argv, prefix);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08002781
Ingo Molnar1fc35b22009-09-13 09:44:29 +02002782 if (!strncmp(argv[0], "rec", 3)) {
2783 return __cmd_record(argc, argv);
2784 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002785 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002786 if (argc > 1) {
2787 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
2788 if (argc)
2789 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002790 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002791 setup_sorting(&sched, latency_options, latency_usage);
2792 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02002793 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02002794 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02002795 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02002796 if (argc)
2797 usage_with_options(map_usage, map_options);
2798 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002799 sched.tp_handler = &map_ops;
2800 setup_sorting(&sched, latency_options, latency_usage);
2801 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002802 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002803 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02002804 if (argc) {
2805 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
2806 if (argc)
2807 usage_with_options(replay_usage, replay_options);
2808 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03002809 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09002810 } else if (!strcmp(argv[0], "timehist")) {
2811 if (argc) {
2812 argc = parse_options(argc, argv, timehist_options,
2813 timehist_usage, 0);
2814 if (argc)
2815 usage_with_options(timehist_usage, timehist_options);
2816 }
2817 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02002818 } else {
2819 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002820 }
2821
Ingo Molnarec156762009-09-11 12:12:54 +02002822 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002823}