blob: 079e67a3690478dd3755e6e1ecbc2c8a09f78aab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnar0a02ad92009-09-11 12:12:54 +02002#include "builtin.h"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02003#include "perf.h"
Arnaldo Carvalho de Melo91854f92019-08-29 14:59:50 -03004#include "perf-sys.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02005
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +01006#include "util/cpumap.h"
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02007#include "util/evlist.h"
Arnaldo Carvalho de Meloe3f42602011-11-16 17:02:54 -02008#include "util/evsel.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +02009#include "util/symbol.h"
10#include "util/thread.h"
11#include "util/header.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020012#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020013#include "util/tool.h"
Yann Droneaud57480d22014-06-30 22:28:47 +020014#include "util/cloexec.h"
Jiri Olsaa151a372016-04-12 15:29:29 +020015#include "util/thread_map.h"
Jiri Olsa8cd91192016-04-12 15:29:27 +020016#include "util/color.h"
David Ahern49394a22016-11-16 15:06:29 +090017#include "util/stat.h"
Arnaldo Carvalho de Melo6a9fa4e2019-06-25 17:31:26 -030018#include "util/string2.h"
David Ahern6c973c92016-11-16 15:06:32 +090019#include "util/callchain.h"
David Ahern853b7402016-11-29 10:15:44 -070020#include "util/time-utils.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020021
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030022#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060023#include <subcmd/parse-options.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020024#include "util/trace-event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020025
Ingo Molnar0a02ad92009-09-11 12:12:54 +020026#include "util/debug.h"
Arnaldo Carvalho de Melof12be042019-09-18 10:11:20 -030027#include "util/event.h"
Ingo Molnar0a02ad92009-09-11 12:12:54 +020028
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
David Ahern49394a22016-11-16 15:06:29 +090030#include <linux/log2.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030031#include <linux/zalloc.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020032#include <sys/prctl.h>
Markus Trippelsdorf7b78f132012-04-04 10:45:27 +020033#include <sys/resource.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030034#include <inttypes.h>
Ingo Molnar0a02ad92009-09-11 12:12:54 +020035
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020037#include <semaphore.h>
38#include <pthread.h>
39#include <math.h>
Yunlong Songcb06ac22015-03-31 21:46:30 +080040#include <api/fs/fs.h>
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010041#include <perf/cpumap.h>
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -030042#include <linux/time64.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053043#include <linux/err.h>
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +020044
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030045#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030046
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020047#define PR_SET_NAME 15 /* Set process name */
48#define MAX_CPUS 4096
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020049#define COMM_LEN 20
50#define SYM_LEN 129
Yunlong Songa35e27d2015-03-31 21:46:29 +080051#define MAX_PID 1024000
Ingo Molnarec156762009-09-11 12:12:54 +020052
mingo39aeb522009-09-14 20:04:48 +020053struct sched_atom;
Ingo Molnarec156762009-09-11 12:12:54 +020054
55struct task_desc {
56 unsigned long nr;
57 unsigned long pid;
58 char comm[COMM_LEN];
59
60 unsigned long nr_events;
61 unsigned long curr_event;
mingo39aeb522009-09-14 20:04:48 +020062 struct sched_atom **atoms;
Ingo Molnarec156762009-09-11 12:12:54 +020063
64 pthread_t thread;
65 sem_t sleep_sem;
66
67 sem_t ready_for_work;
68 sem_t work_done_sem;
69
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020070 u64 cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +020071};
72
73enum sched_event_type {
74 SCHED_EVENT_RUN,
75 SCHED_EVENT_SLEEP,
76 SCHED_EVENT_WAKEUP,
Mike Galbraith55ffb7a2009-10-10 14:46:04 +020077 SCHED_EVENT_MIGRATION,
Ingo Molnarec156762009-09-11 12:12:54 +020078};
79
mingo39aeb522009-09-14 20:04:48 +020080struct sched_atom {
Ingo Molnarec156762009-09-11 12:12:54 +020081 enum sched_event_type type;
Arnaldo Carvalho de Meloeed05fe2010-04-05 12:53:45 -030082 int specific_wait;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020083 u64 timestamp;
84 u64 duration;
Ingo Molnarec156762009-09-11 12:12:54 +020085 unsigned long nr;
Ingo Molnarec156762009-09-11 12:12:54 +020086 sem_t *wait_sem;
87 struct task_desc *wakee;
88};
89
Dongshenge936e8e2014-05-05 16:05:54 +090090#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +020091
Namhyung Kim941bdea2017-01-13 19:45:21 +090092/* task state bitmask, copied from include/linux/sched.h */
93#define TASK_RUNNING 0
94#define TASK_INTERRUPTIBLE 1
95#define TASK_UNINTERRUPTIBLE 2
96#define __TASK_STOPPED 4
97#define __TASK_TRACED 8
98/* in tsk->exit_state */
99#define EXIT_DEAD 16
100#define EXIT_ZOMBIE 32
101#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
102/* in tsk->state again */
103#define TASK_DEAD 64
104#define TASK_WAKEKILL 128
105#define TASK_WAKING 256
106#define TASK_PARKED 512
107
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200108enum thread_state {
109 THREAD_SLEEPING = 0,
110 THREAD_WAIT_CPU,
111 THREAD_SCHED_IN,
112 THREAD_IGNORE
113};
114
115struct work_atom {
116 struct list_head list;
117 enum thread_state state;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +0200118 u64 sched_out_time;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200119 u64 wake_up_time;
120 u64 sched_in_time;
121 u64 runtime;
122};
123
mingo39aeb522009-09-14 20:04:48 +0200124struct work_atoms {
125 struct list_head work_list;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200126 struct thread *thread;
127 struct rb_node node;
128 u64 max_lat;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +0100129 u64 max_lat_at;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200130 u64 total_lat;
131 u64 nb_atoms;
132 u64 total_runtime;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400133 int num_merged;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200134};
135
mingo39aeb522009-09-14 20:04:48 +0200136typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200137
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300138struct perf_sched;
139
140struct trace_sched_handler {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200141 int (*switch_event)(struct perf_sched *sched, struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300142 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300143
Jiri Olsa32dcd022019-07-21 13:23:51 +0200144 int (*runtime_event)(struct perf_sched *sched, struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300145 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300146
Jiri Olsa32dcd022019-07-21 13:23:51 +0200147 int (*wakeup_event)(struct perf_sched *sched, struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300148 struct perf_sample *sample, struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300149
David Aherncb627502013-08-07 22:50:47 -0400150 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
151 int (*fork_event)(struct perf_sched *sched, union perf_event *event,
152 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300153
154 int (*migrate_task_event)(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200155 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300156 struct perf_sample *sample,
157 struct machine *machine);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300158};
159
Jiri Olsaa151a372016-04-12 15:29:29 +0200160#define COLOR_PIDS PERF_COLOR_BLUE
Jiri Olsacf294f22016-04-12 15:29:30 +0200161#define COLOR_CPUS PERF_COLOR_BG_RED
Jiri Olsaa151a372016-04-12 15:29:29 +0200162
Jiri Olsa99623c62016-04-12 15:29:26 +0200163struct perf_sched_map {
164 DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
165 int *comp_cpus;
166 bool comp;
Jiri Olsa9749b902019-07-21 13:23:50 +0200167 struct perf_thread_map *color_pids;
Jiri Olsaa151a372016-04-12 15:29:29 +0200168 const char *color_pids_str;
Jiri Olsaf8548392019-07-21 13:23:49 +0200169 struct perf_cpu_map *color_cpus;
Jiri Olsacf294f22016-04-12 15:29:30 +0200170 const char *color_cpus_str;
Jiri Olsaf8548392019-07-21 13:23:49 +0200171 struct perf_cpu_map *cpus;
Jiri Olsa73643bb2016-04-12 15:29:31 +0200172 const char *cpus_str;
Jiri Olsa99623c62016-04-12 15:29:26 +0200173};
174
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300175struct perf_sched {
176 struct perf_tool tool;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300177 const char *sort_order;
178 unsigned long nr_tasks;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800179 struct task_desc **pid_to_task;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300180 struct task_desc **tasks;
181 const struct trace_sched_handler *tp_handler;
182 pthread_mutex_t start_work_mutex;
183 pthread_mutex_t work_done_wait_mutex;
184 int profile_cpu;
185/*
186 * Track the current task - that way we can know whether there's any
187 * weird events, such as a task being switched away that is not current.
188 */
189 int max_cpu;
190 u32 curr_pid[MAX_CPUS];
191 struct thread *curr_thread[MAX_CPUS];
192 char next_shortname1;
193 char next_shortname2;
194 unsigned int replay_repeat;
195 unsigned long nr_run_events;
196 unsigned long nr_sleep_events;
197 unsigned long nr_wakeup_events;
198 unsigned long nr_sleep_corrections;
199 unsigned long nr_run_events_optimized;
200 unsigned long targetless_wakeups;
201 unsigned long multitarget_wakeups;
202 unsigned long nr_runs;
203 unsigned long nr_timestamps;
204 unsigned long nr_unordered_timestamps;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300205 unsigned long nr_context_switch_bugs;
206 unsigned long nr_events;
207 unsigned long nr_lost_chunks;
208 unsigned long nr_lost_events;
209 u64 run_measurement_overhead;
210 u64 sleep_measurement_overhead;
211 u64 start_time;
212 u64 cpu_usage;
213 u64 runavg_cpu_usage;
214 u64 parent_cpu_usage;
215 u64 runavg_parent_cpu_usage;
216 u64 sum_runtime;
217 u64 sum_fluct;
218 u64 run_avg;
219 u64 all_runtime;
220 u64 all_count;
221 u64 cpu_last_switched[MAX_CPUS];
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800222 struct rb_root_cached atom_root, sorted_atom_root, merged_atom_root;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300223 struct list_head sort_list, cmp_pid;
Yunlong Song939cda52015-03-31 21:46:34 +0800224 bool force;
Josef Bacik2f80dd42015-05-22 09:18:40 -0400225 bool skip_merge;
Jiri Olsa99623c62016-04-12 15:29:26 +0200226 struct perf_sched_map map;
David Ahern52df1382016-11-16 15:06:30 +0900227
228 /* options for timehist command */
229 bool summary;
230 bool summary_only;
Namhyung Kim699b5b92016-12-08 23:47:52 +0900231 bool idle_hist;
David Ahern6c973c92016-11-16 15:06:32 +0900232 bool show_callchain;
233 unsigned int max_stack;
David Aherna407b062016-11-16 15:06:33 +0900234 bool show_cpu_visual;
David Ahernfc1469f2016-11-16 15:06:31 +0900235 bool show_wakeups;
Brendan Gregg292c4a82017-03-14 01:56:29 +0000236 bool show_next;
David Ahern350f54f2016-11-25 09:28:41 -0700237 bool show_migrations;
Namhyung Kim414e0502017-01-13 19:45:22 +0900238 bool show_state;
David Ahern52df1382016-11-16 15:06:30 +0900239 u64 skipped_samples;
David Ahern853b7402016-11-29 10:15:44 -0700240 const char *time_str;
241 struct perf_time_interval ptime;
Namhyung Kim9396c9c2016-12-22 15:03:50 +0900242 struct perf_time_interval hist_time;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300243};
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200244
David Ahern49394a22016-11-16 15:06:29 +0900245/* per thread run time data */
246struct thread_runtime {
247 u64 last_time; /* time of previous sched in/out event */
248 u64 dt_run; /* run time */
Namhyung Kim941bdea2017-01-13 19:45:21 +0900249 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
250 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
251 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
David Ahern49394a22016-11-16 15:06:29 +0900252 u64 dt_delay; /* time between wakeup and sched-in */
253 u64 ready_to_run; /* time of wakeup */
254
255 struct stats run_stats;
256 u64 total_run_time;
Namhyung Kim587782c2017-01-13 19:45:23 +0900257 u64 total_sleep_time;
258 u64 total_iowait_time;
259 u64 total_preempt_time;
260 u64 total_delay_time;
David Ahern350f54f2016-11-25 09:28:41 -0700261
Namhyung Kim941bdea2017-01-13 19:45:21 +0900262 int last_state;
Changbin Du8640da92018-03-06 11:37:36 +0800263
264 char shortname[3];
Changbin Du99a3c3a2018-03-06 11:37:37 +0800265 bool comm_changed;
266
David Ahern350f54f2016-11-25 09:28:41 -0700267 u64 migrations;
David Ahern49394a22016-11-16 15:06:29 +0900268};
269
270/* per event run time data */
271struct evsel_runtime {
272 u64 *last_time; /* time this event was last seen per cpu */
273 u32 ncpu; /* highest cpu slot allocated */
274};
275
Namhyung Kim3bc2fa92016-12-08 23:47:51 +0900276/* per cpu idle time data */
277struct idle_thread_runtime {
278 struct thread_runtime tr;
279 struct thread *last_thread;
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800280 struct rb_root_cached sorted_root;
Namhyung Kim3bc2fa92016-12-08 23:47:51 +0900281 struct callchain_root callchain;
282 struct callchain_cursor cursor;
283};
284
David Ahern49394a22016-11-16 15:06:29 +0900285/* track idle times per cpu */
286static struct thread **idle_threads;
287static int idle_max_cpu;
288static char idle_comm[] = "<idle>";
289
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200290static u64 get_nsecs(void)
291{
292 struct timespec ts;
293
294 clock_gettime(CLOCK_MONOTONIC, &ts);
295
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300296 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200297}
298
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300299static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200300{
301 u64 T0 = get_nsecs(), T1;
302
303 do {
304 T1 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300305 } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200306}
307
308static void sleep_nsecs(u64 nsecs)
309{
310 struct timespec ts;
311
312 ts.tv_nsec = nsecs % 999999999;
313 ts.tv_sec = nsecs / 999999999;
314
315 nanosleep(&ts, NULL);
316}
317
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300318static void calibrate_run_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200319{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300320 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200321 int i;
322
323 for (i = 0; i < 10; i++) {
324 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300325 burn_nsecs(sched, 0);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200326 T1 = get_nsecs();
327 delta = T1-T0;
328 min_delta = min(min_delta, delta);
329 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300330 sched->run_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200331
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200332 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200333}
334
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300335static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200336{
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300337 u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200338 int i;
339
340 for (i = 0; i < 10; i++) {
341 T0 = get_nsecs();
342 sleep_nsecs(10000);
343 T1 = get_nsecs();
344 delta = T1-T0;
345 min_delta = min(min_delta, delta);
346 }
347 min_delta -= 10000;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300348 sched->sleep_measurement_overhead = min_delta;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200349
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200350 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200351}
352
mingo39aeb522009-09-14 20:04:48 +0200353static struct sched_atom *
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200354get_new_event(struct task_desc *task, u64 timestamp)
Ingo Molnarec156762009-09-11 12:12:54 +0200355{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200356 struct sched_atom *event = zalloc(sizeof(*event));
Ingo Molnarec156762009-09-11 12:12:54 +0200357 unsigned long idx = task->nr_events;
358 size_t size;
359
360 event->timestamp = timestamp;
361 event->nr = idx;
362
363 task->nr_events++;
mingo39aeb522009-09-14 20:04:48 +0200364 size = sizeof(struct sched_atom *) * task->nr_events;
365 task->atoms = realloc(task->atoms, size);
366 BUG_ON(!task->atoms);
Ingo Molnarec156762009-09-11 12:12:54 +0200367
mingo39aeb522009-09-14 20:04:48 +0200368 task->atoms[idx] = event;
Ingo Molnarec156762009-09-11 12:12:54 +0200369
370 return event;
371}
372
mingo39aeb522009-09-14 20:04:48 +0200373static struct sched_atom *last_event(struct task_desc *task)
Ingo Molnarec156762009-09-11 12:12:54 +0200374{
375 if (!task->nr_events)
376 return NULL;
377
mingo39aeb522009-09-14 20:04:48 +0200378 return task->atoms[task->nr_events - 1];
Ingo Molnarec156762009-09-11 12:12:54 +0200379}
380
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300381static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
382 u64 timestamp, u64 duration)
Ingo Molnarec156762009-09-11 12:12:54 +0200383{
mingo39aeb522009-09-14 20:04:48 +0200384 struct sched_atom *event, *curr_event = last_event(task);
Ingo Molnarec156762009-09-11 12:12:54 +0200385
386 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200387 * optimize an existing RUN event by merging this one
388 * to it:
389 */
Ingo Molnarec156762009-09-11 12:12:54 +0200390 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300391 sched->nr_run_events_optimized++;
Ingo Molnarec156762009-09-11 12:12:54 +0200392 curr_event->duration += duration;
393 return;
394 }
395
396 event = get_new_event(task, timestamp);
397
398 event->type = SCHED_EVENT_RUN;
399 event->duration = duration;
400
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300401 sched->nr_run_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200402}
403
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300404static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
405 u64 timestamp, struct task_desc *wakee)
Ingo Molnarec156762009-09-11 12:12:54 +0200406{
mingo39aeb522009-09-14 20:04:48 +0200407 struct sched_atom *event, *wakee_event;
Ingo Molnarec156762009-09-11 12:12:54 +0200408
409 event = get_new_event(task, timestamp);
410 event->type = SCHED_EVENT_WAKEUP;
411 event->wakee = wakee;
412
413 wakee_event = last_event(wakee);
414 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300415 sched->targetless_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200416 return;
417 }
418 if (wakee_event->wait_sem) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300419 sched->multitarget_wakeups++;
Ingo Molnarec156762009-09-11 12:12:54 +0200420 return;
421 }
422
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200423 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
Ingo Molnarec156762009-09-11 12:12:54 +0200424 sem_init(wakee_event->wait_sem, 0, 0);
425 wakee_event->specific_wait = 1;
426 event->wait_sem = wakee_event->wait_sem;
427
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300428 sched->nr_wakeup_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200429}
430
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300431static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
432 u64 timestamp, u64 task_state __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200433{
mingo39aeb522009-09-14 20:04:48 +0200434 struct sched_atom *event = get_new_event(task, timestamp);
Ingo Molnarec156762009-09-11 12:12:54 +0200435
436 event->type = SCHED_EVENT_SLEEP;
437
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300438 sched->nr_sleep_events++;
Ingo Molnarec156762009-09-11 12:12:54 +0200439}
440
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300441static struct task_desc *register_pid(struct perf_sched *sched,
442 unsigned long pid, const char *comm)
Ingo Molnarec156762009-09-11 12:12:54 +0200443{
444 struct task_desc *task;
Yunlong Songcb06ac22015-03-31 21:46:30 +0800445 static int pid_max;
Ingo Molnarec156762009-09-11 12:12:54 +0200446
Yunlong Songcb06ac22015-03-31 21:46:30 +0800447 if (sched->pid_to_task == NULL) {
448 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
449 pid_max = MAX_PID;
450 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
451 }
Yunlong Song3a423a52015-03-31 21:46:31 +0800452 if (pid >= (unsigned long)pid_max) {
453 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
454 sizeof(struct task_desc *))) == NULL);
455 while (pid >= (unsigned long)pid_max)
456 sched->pid_to_task[pid_max++] = NULL;
457 }
Ingo Molnarec156762009-09-11 12:12:54 +0200458
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300459 task = sched->pid_to_task[pid];
Ingo Molnarec156762009-09-11 12:12:54 +0200460
461 if (task)
462 return task;
463
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200464 task = zalloc(sizeof(*task));
Ingo Molnarec156762009-09-11 12:12:54 +0200465 task->pid = pid;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300466 task->nr = sched->nr_tasks;
Ingo Molnarec156762009-09-11 12:12:54 +0200467 strcpy(task->comm, comm);
468 /*
469 * every task starts in sleeping state - this gets ignored
470 * if there's no wakeup pointing to this sleep state:
471 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300472 add_sched_event_sleep(sched, task, 0, 0);
Ingo Molnarec156762009-09-11 12:12:54 +0200473
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300474 sched->pid_to_task[pid] = task;
475 sched->nr_tasks++;
Yunlong Song0755bc42015-03-31 21:46:28 +0800476 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300477 BUG_ON(!sched->tasks);
478 sched->tasks[task->nr] = task;
Ingo Molnarec156762009-09-11 12:12:54 +0200479
Namhyung Kimbb963e12017-02-17 17:17:38 +0900480 if (verbose > 0)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300481 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
Ingo Molnarec156762009-09-11 12:12:54 +0200482
483 return task;
484}
485
486
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300487static void print_task_traces(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200488{
489 struct task_desc *task;
490 unsigned long i;
491
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300492 for (i = 0; i < sched->nr_tasks; i++) {
493 task = sched->tasks[i];
Ingo Molnarad236fd2009-09-11 12:12:54 +0200494 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
Ingo Molnarec156762009-09-11 12:12:54 +0200495 task->nr, task->comm, task->pid, task->nr_events);
496 }
497}
498
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300499static void add_cross_task_wakeups(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200500{
501 struct task_desc *task1, *task2;
502 unsigned long i, j;
503
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300504 for (i = 0; i < sched->nr_tasks; i++) {
505 task1 = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200506 j = i + 1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300507 if (j == sched->nr_tasks)
Ingo Molnarec156762009-09-11 12:12:54 +0200508 j = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300509 task2 = sched->tasks[j];
510 add_sched_event_wakeup(sched, task1, 0, task2);
Ingo Molnarec156762009-09-11 12:12:54 +0200511 }
512}
513
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300514static void perf_sched__process_event(struct perf_sched *sched,
515 struct sched_atom *atom)
Ingo Molnarec156762009-09-11 12:12:54 +0200516{
517 int ret = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200518
mingo39aeb522009-09-14 20:04:48 +0200519 switch (atom->type) {
Ingo Molnarec156762009-09-11 12:12:54 +0200520 case SCHED_EVENT_RUN:
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300521 burn_nsecs(sched, atom->duration);
Ingo Molnarec156762009-09-11 12:12:54 +0200522 break;
523 case SCHED_EVENT_SLEEP:
mingo39aeb522009-09-14 20:04:48 +0200524 if (atom->wait_sem)
525 ret = sem_wait(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200526 BUG_ON(ret);
527 break;
528 case SCHED_EVENT_WAKEUP:
mingo39aeb522009-09-14 20:04:48 +0200529 if (atom->wait_sem)
530 ret = sem_post(atom->wait_sem);
Ingo Molnarec156762009-09-11 12:12:54 +0200531 BUG_ON(ret);
532 break;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +0200533 case SCHED_EVENT_MIGRATION:
534 break;
Ingo Molnarec156762009-09-11 12:12:54 +0200535 default:
536 BUG_ON(1);
537 }
538}
539
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200540static u64 get_cpu_usage_nsec_parent(void)
Ingo Molnarec156762009-09-11 12:12:54 +0200541{
542 struct rusage ru;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200543 u64 sum;
Ingo Molnarec156762009-09-11 12:12:54 +0200544 int err;
545
546 err = getrusage(RUSAGE_SELF, &ru);
547 BUG_ON(err);
548
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300549 sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
550 sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
Ingo Molnarec156762009-09-11 12:12:54 +0200551
552 return sum;
553}
554
Yunlong Song939cda52015-03-31 21:46:34 +0800555static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
Ingo Molnarec156762009-09-11 12:12:54 +0200556{
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800557 struct perf_event_attr attr;
Yunlong Song939cda52015-03-31 21:46:34 +0800558 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800559 int fd;
Yunlong Song939cda52015-03-31 21:46:34 +0800560 struct rlimit limit;
561 bool need_privilege = false;
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800562
563 memset(&attr, 0, sizeof(attr));
564
565 attr.type = PERF_TYPE_SOFTWARE;
566 attr.config = PERF_COUNT_SW_TASK_CLOCK;
567
Yunlong Song939cda52015-03-31 21:46:34 +0800568force_again:
Yann Droneaud57480d22014-06-30 22:28:47 +0200569 fd = sys_perf_event_open(&attr, 0, -1, -1,
570 perf_event_open_cloexec_flag());
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800571
Yunlong Song1aff59b2015-03-31 21:46:33 +0800572 if (fd < 0) {
Yunlong Song939cda52015-03-31 21:46:34 +0800573 if (errno == EMFILE) {
574 if (sched->force) {
575 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
576 limit.rlim_cur += sched->nr_tasks - cur_task;
577 if (limit.rlim_cur > limit.rlim_max) {
578 limit.rlim_max = limit.rlim_cur;
579 need_privilege = true;
580 }
581 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
582 if (need_privilege && errno == EPERM)
583 strcpy(info, "Need privilege\n");
584 } else
585 goto force_again;
586 } else
587 strcpy(info, "Have a try with -f option\n");
588 }
Namhyung Kim60b7d142012-09-12 11:11:06 +0900589 pr_err("Error: sys_perf_event_open() syscall returned "
Yunlong Song939cda52015-03-31 21:46:34 +0800590 "with %d (%s)\n%s", fd,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300591 str_error_r(errno, sbuf, sizeof(sbuf)), info);
Yunlong Song1aff59b2015-03-31 21:46:33 +0800592 exit(EXIT_FAILURE);
593 }
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800594 return fd;
595}
596
597static u64 get_cpu_usage_nsec_self(int fd)
598{
599 u64 runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200600 int ret;
601
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800602 ret = read(fd, &runtime, sizeof(runtime));
603 BUG_ON(ret != sizeof(runtime));
Ingo Molnarec156762009-09-11 12:12:54 +0200604
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800605 return runtime;
Ingo Molnarec156762009-09-11 12:12:54 +0200606}
607
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300608struct sched_thread_parms {
609 struct task_desc *task;
610 struct perf_sched *sched;
Yunlong Song08097ab2015-03-31 21:46:32 +0800611 int fd;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300612};
613
Ingo Molnarec156762009-09-11 12:12:54 +0200614static void *thread_func(void *ctx)
615{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300616 struct sched_thread_parms *parms = ctx;
617 struct task_desc *this_task = parms->task;
618 struct perf_sched *sched = parms->sched;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200619 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200620 unsigned long i, ret;
621 char comm2[22];
Yunlong Song08097ab2015-03-31 21:46:32 +0800622 int fd = parms->fd;
Ingo Molnarec156762009-09-11 12:12:54 +0200623
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300624 zfree(&parms);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300625
Ingo Molnarec156762009-09-11 12:12:54 +0200626 sprintf(comm2, ":%s", this_task->comm);
627 prctl(PR_SET_NAME, comm2);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300628 if (fd < 0)
629 return NULL;
Ingo Molnarec156762009-09-11 12:12:54 +0200630again:
631 ret = sem_post(&this_task->ready_for_work);
632 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300633 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200634 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300635 ret = pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200636 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200637
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800638 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200639
640 for (i = 0; i < this_task->nr_events; i++) {
641 this_task->curr_event = i;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300642 perf_sched__process_event(sched, this_task->atoms[i]);
Ingo Molnarec156762009-09-11 12:12:54 +0200643 }
644
Xiao Guangrongc0c9e722009-12-09 17:51:30 +0800645 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
Ingo Molnarec156762009-09-11 12:12:54 +0200646 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
Ingo Molnarec156762009-09-11 12:12:54 +0200647 ret = sem_post(&this_task->work_done_sem);
648 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200649
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300650 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200651 BUG_ON(ret);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300652 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200653 BUG_ON(ret);
Ingo Molnarec156762009-09-11 12:12:54 +0200654
655 goto again;
656}
657
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300658static void create_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200659{
660 struct task_desc *task;
661 pthread_attr_t attr;
662 unsigned long i;
663 int err;
664
665 err = pthread_attr_init(&attr);
666 BUG_ON(err);
Jiri Pirko12f7e032011-01-10 14:14:23 -0200667 err = pthread_attr_setstacksize(&attr,
668 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
Ingo Molnarec156762009-09-11 12:12:54 +0200669 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300670 err = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200671 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300672 err = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200673 BUG_ON(err);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300674 for (i = 0; i < sched->nr_tasks; i++) {
675 struct sched_thread_parms *parms = malloc(sizeof(*parms));
676 BUG_ON(parms == NULL);
677 parms->task = task = sched->tasks[i];
678 parms->sched = sched;
Yunlong Song939cda52015-03-31 21:46:34 +0800679 parms->fd = self_open_counters(sched, i);
Ingo Molnarec156762009-09-11 12:12:54 +0200680 sem_init(&task->sleep_sem, 0, 0);
681 sem_init(&task->ready_for_work, 0, 0);
682 sem_init(&task->work_done_sem, 0, 0);
683 task->curr_event = 0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300684 err = pthread_create(&task->thread, &attr, thread_func, parms);
Ingo Molnarec156762009-09-11 12:12:54 +0200685 BUG_ON(err);
686 }
687}
688
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300689static void wait_for_tasks(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200690{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200691 u64 cpu_usage_0, cpu_usage_1;
Ingo Molnarec156762009-09-11 12:12:54 +0200692 struct task_desc *task;
693 unsigned long i, ret;
694
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300695 sched->start_time = get_nsecs();
696 sched->cpu_usage = 0;
697 pthread_mutex_unlock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200698
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300699 for (i = 0; i < sched->nr_tasks; i++) {
700 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200701 ret = sem_wait(&task->ready_for_work);
702 BUG_ON(ret);
703 sem_init(&task->ready_for_work, 0, 0);
704 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300705 ret = pthread_mutex_lock(&sched->work_done_wait_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200706 BUG_ON(ret);
707
708 cpu_usage_0 = get_cpu_usage_nsec_parent();
709
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300710 pthread_mutex_unlock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200711
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300712 for (i = 0; i < sched->nr_tasks; i++) {
713 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200714 ret = sem_wait(&task->work_done_sem);
715 BUG_ON(ret);
716 sem_init(&task->work_done_sem, 0, 0);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300717 sched->cpu_usage += task->cpu_usage;
Ingo Molnarec156762009-09-11 12:12:54 +0200718 task->cpu_usage = 0;
719 }
720
721 cpu_usage_1 = get_cpu_usage_nsec_parent();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300722 if (!sched->runavg_cpu_usage)
723 sched->runavg_cpu_usage = sched->cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800724 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 +0200725
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300726 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
727 if (!sched->runavg_parent_cpu_usage)
728 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800729 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
730 sched->parent_cpu_usage)/sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200731
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300732 ret = pthread_mutex_lock(&sched->start_work_mutex);
Ingo Molnarec156762009-09-11 12:12:54 +0200733 BUG_ON(ret);
734
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300735 for (i = 0; i < sched->nr_tasks; i++) {
736 task = sched->tasks[i];
Ingo Molnarec156762009-09-11 12:12:54 +0200737 sem_init(&task->sleep_sem, 0, 0);
738 task->curr_event = 0;
739 }
740}
741
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300742static void run_one_test(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200743{
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500744 u64 T0, T1, delta, avg_delta, fluct;
Ingo Molnarec156762009-09-11 12:12:54 +0200745
746 T0 = get_nsecs();
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300747 wait_for_tasks(sched);
Ingo Molnarec156762009-09-11 12:12:54 +0200748 T1 = get_nsecs();
749
750 delta = T1 - T0;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300751 sched->sum_runtime += delta;
752 sched->nr_runs++;
Ingo Molnarec156762009-09-11 12:12:54 +0200753
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300754 avg_delta = sched->sum_runtime / sched->nr_runs;
Ingo Molnarec156762009-09-11 12:12:54 +0200755 if (delta < avg_delta)
756 fluct = avg_delta - delta;
757 else
758 fluct = delta - avg_delta;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300759 sched->sum_fluct += fluct;
760 if (!sched->run_avg)
761 sched->run_avg = delta;
Yunlong Songff5f3bb2015-03-31 21:46:36 +0800762 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
Ingo Molnarec156762009-09-11 12:12:54 +0200763
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300764 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200765
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300766 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200767
Ingo Molnarad236fd2009-09-11 12:12:54 +0200768 printf("cpu: %0.2f / %0.2f",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300769 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200770
771#if 0
772 /*
Ingo Molnarfbf94822009-09-11 12:12:54 +0200773 * rusage statistics done by the parent, these are less
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300774 * accurate than the sched->sum_exec_runtime based statistics:
Ingo Molnarfbf94822009-09-11 12:12:54 +0200775 */
Ingo Molnarad236fd2009-09-11 12:12:54 +0200776 printf(" [%0.2f / %0.2f]",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300777 (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
778 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200779#endif
780
Ingo Molnarad236fd2009-09-11 12:12:54 +0200781 printf("\n");
Ingo Molnarec156762009-09-11 12:12:54 +0200782
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300783 if (sched->nr_sleep_corrections)
784 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
785 sched->nr_sleep_corrections = 0;
Ingo Molnarec156762009-09-11 12:12:54 +0200786}
787
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300788static void test_calibrations(struct perf_sched *sched)
Ingo Molnarec156762009-09-11 12:12:54 +0200789{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200790 u64 T0, T1;
Ingo Molnarec156762009-09-11 12:12:54 +0200791
792 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300793 burn_nsecs(sched, NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200794 T1 = get_nsecs();
795
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200796 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200797
798 T0 = get_nsecs();
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -0300799 sleep_nsecs(NSEC_PER_MSEC);
Ingo Molnarec156762009-09-11 12:12:54 +0200800 T1 = get_nsecs();
801
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200802 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
Ingo Molnarec156762009-09-11 12:12:54 +0200803}
804
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300805static int
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300806replay_wakeup_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200807 struct evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300808 struct machine *machine __maybe_unused)
Ingo Molnarec156762009-09-11 12:12:54 +0200809{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300810 const char *comm = perf_evsel__strval(evsel, sample, "comm");
811 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200812 struct task_desc *waker, *wakee;
813
Namhyung Kimbb963e12017-02-17 17:17:38 +0900814 if (verbose > 0) {
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300815 printf("sched_wakeup event %p\n", evsel);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200816
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300817 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200818 }
819
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300820 waker = register_pid(sched, sample->tid, "<unknown>");
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300821 wakee = register_pid(sched, pid, comm);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200822
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300823 add_sched_event_wakeup(sched, waker, sample->time, wakee);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300824 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200825}
826
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300827static int replay_switch_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200828 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300829 struct perf_sample *sample,
830 struct machine *machine __maybe_unused)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200831{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300832 const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"),
833 *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
834 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
835 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
836 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300837 struct task_desc *prev, __maybe_unused *next;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -0300838 u64 timestamp0, timestamp = sample->time;
839 int cpu = sample->cpu;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200840 s64 delta;
841
Namhyung Kimbb963e12017-02-17 17:17:38 +0900842 if (verbose > 0)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -0300843 printf("sched_switch event %p\n", evsel);
Ingo Molnarad236fd2009-09-11 12:12:54 +0200844
Ingo Molnarfbf94822009-09-11 12:12:54 +0200845 if (cpu >= MAX_CPUS || cpu < 0)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300846 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200847
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300848 timestamp0 = sched->cpu_last_switched[cpu];
Ingo Molnarfbf94822009-09-11 12:12:54 +0200849 if (timestamp0)
850 delta = timestamp - timestamp0;
851 else
852 delta = 0;
853
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300854 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +0900855 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300856 return -1;
857 }
Ingo Molnarfbf94822009-09-11 12:12:54 +0200858
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300859 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
860 prev_comm, prev_pid, next_comm, next_pid, delta);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200861
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300862 prev = register_pid(sched, prev_pid, prev_comm);
863 next = register_pid(sched, next_pid, next_comm);
Ingo Molnarfbf94822009-09-11 12:12:54 +0200864
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300865 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200866
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -0300867 add_sched_event_run(sched, prev, timestamp, delta);
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300868 add_sched_event_sleep(sched, prev, timestamp, prev_state);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300869
870 return 0;
Ingo Molnarfbf94822009-09-11 12:12:54 +0200871}
872
David Aherncb627502013-08-07 22:50:47 -0400873static int replay_fork_event(struct perf_sched *sched,
874 union perf_event *event,
875 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200876{
David Aherncb627502013-08-07 22:50:47 -0400877 struct thread *child, *parent;
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300878
Adrian Hunter314add62013-08-27 11:23:03 +0300879 child = machine__findnew_thread(machine, event->fork.pid,
880 event->fork.tid);
881 parent = machine__findnew_thread(machine, event->fork.ppid,
882 event->fork.ptid);
David Aherncb627502013-08-07 22:50:47 -0400883
884 if (child == NULL || parent == NULL) {
885 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
886 child, parent);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300887 goto out_put;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200888 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -0300889
Namhyung Kimbb963e12017-02-17 17:17:38 +0900890 if (verbose > 0) {
David Aherncb627502013-08-07 22:50:47 -0400891 printf("fork event\n");
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200892 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid);
893 printf("... child: %s/%d\n", thread__comm_str(child), child->tid);
David Aherncb627502013-08-07 22:50:47 -0400894 }
895
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200896 register_pid(sched, parent->tid, thread__comm_str(parent));
897 register_pid(sched, child->tid, thread__comm_str(child));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300898out_put:
899 thread__put(child);
900 thread__put(parent);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -0300901 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +0200902}
903
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200904struct sort_dimension {
905 const char *name;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200906 sort_fn_t cmp;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +0200907 struct list_head list;
908};
909
Changbin Du8640da92018-03-06 11:37:36 +0800910/*
911 * handle runtime stats saved per thread
912 */
913static struct thread_runtime *thread__init_runtime(struct thread *thread)
914{
915 struct thread_runtime *r;
916
917 r = zalloc(sizeof(struct thread_runtime));
918 if (!r)
919 return NULL;
920
921 init_stats(&r->run_stats);
922 thread__set_priv(thread, r);
923
924 return r;
925}
926
927static struct thread_runtime *thread__get_runtime(struct thread *thread)
928{
929 struct thread_runtime *tr;
930
931 tr = thread__priv(thread);
932 if (tr == NULL) {
933 tr = thread__init_runtime(thread);
934 if (tr == NULL)
935 pr_debug("Failed to malloc memory for runtime data.\n");
936 }
937
938 return tr;
939}
940
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200941static int
mingo39aeb522009-09-14 20:04:48 +0200942thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200943{
944 struct sort_dimension *sort;
945 int ret = 0;
946
Ingo Molnarb5fae122009-09-11 12:12:54 +0200947 BUG_ON(list_empty(list));
948
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200949 list_for_each_entry(sort, list, list) {
950 ret = sort->cmp(l, r);
951 if (ret)
952 return ret;
953 }
954
955 return ret;
956}
957
mingo39aeb522009-09-14 20:04:48 +0200958static struct work_atoms *
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800959thread_atoms_search(struct rb_root_cached *root, struct thread *thread,
Ingo Molnarb5fae122009-09-11 12:12:54 +0200960 struct list_head *sort_list)
961{
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800962 struct rb_node *node = root->rb_root.rb_node;
mingo39aeb522009-09-14 20:04:48 +0200963 struct work_atoms key = { .thread = thread };
Ingo Molnarb5fae122009-09-11 12:12:54 +0200964
965 while (node) {
mingo39aeb522009-09-14 20:04:48 +0200966 struct work_atoms *atoms;
Ingo Molnarb5fae122009-09-11 12:12:54 +0200967 int cmp;
968
mingo39aeb522009-09-14 20:04:48 +0200969 atoms = container_of(node, struct work_atoms, node);
Ingo Molnarb5fae122009-09-11 12:12:54 +0200970
971 cmp = thread_lat_cmp(sort_list, &key, atoms);
972 if (cmp > 0)
973 node = node->rb_left;
974 else if (cmp < 0)
975 node = node->rb_right;
976 else {
977 BUG_ON(thread != atoms->thread);
978 return atoms;
979 }
980 }
981 return NULL;
982}
983
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200984static void
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800985__thread_latency_insert(struct rb_root_cached *root, struct work_atoms *data,
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200986 struct list_head *sort_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200987{
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -0800988 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
989 bool leftmost = true;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200990
991 while (*new) {
mingo39aeb522009-09-14 20:04:48 +0200992 struct work_atoms *this;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200993 int cmp;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200994
mingo39aeb522009-09-14 20:04:48 +0200995 this = container_of(*new, struct work_atoms, node);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +0200996 parent = *new;
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +0200997
998 cmp = thread_lat_cmp(sort_list, data, this);
999
1000 if (cmp > 0)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001001 new = &((*new)->rb_left);
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001002 else {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001003 new = &((*new)->rb_right);
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001004 leftmost = false;
1005 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001006 }
1007
1008 rb_link_node(&data->node, parent, new);
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001009 rb_insert_color_cached(&data->node, root, leftmost);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001010}
1011
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001012static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001013{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -02001014 struct work_atoms *atoms = zalloc(sizeof(*atoms));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001015 if (!atoms) {
1016 pr_err("No memory at %s\n", __func__);
1017 return -1;
1018 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001019
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001020 atoms->thread = thread__get(thread);
mingo39aeb522009-09-14 20:04:48 +02001021 INIT_LIST_HEAD(&atoms->work_list);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001022 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001023 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001024}
1025
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001026static char sched_out_state(u64 prev_state)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001027{
1028 const char *str = TASK_STATE_TO_CHAR_STR;
1029
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001030 return str[prev_state];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001031}
1032
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001033static int
mingo39aeb522009-09-14 20:04:48 +02001034add_sched_out_event(struct work_atoms *atoms,
1035 char run_state,
1036 u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001037{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -02001038 struct work_atom *atom = zalloc(sizeof(*atom));
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001039 if (!atom) {
1040 pr_err("Non memory at %s", __func__);
1041 return -1;
1042 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001043
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001044 atom->sched_out_time = timestamp;
1045
mingo39aeb522009-09-14 20:04:48 +02001046 if (run_state == 'R') {
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001047 atom->state = THREAD_WAIT_CPU;
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001048 atom->wake_up_time = atom->sched_out_time;
Frederic Weisbeckerc6ced612009-09-13 00:46:19 +02001049 }
1050
mingo39aeb522009-09-14 20:04:48 +02001051 list_add_tail(&atom->list, &atoms->work_list);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001052 return 0;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001053}
1054
1055static void
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001056add_runtime_event(struct work_atoms *atoms, u64 delta,
1057 u64 timestamp __maybe_unused)
mingo39aeb522009-09-14 20:04:48 +02001058{
1059 struct work_atom *atom;
1060
1061 BUG_ON(list_empty(&atoms->work_list));
1062
1063 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1064
1065 atom->runtime += delta;
1066 atoms->total_runtime += delta;
1067}
1068
1069static void
1070add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001071{
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001072 struct work_atom *atom;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001073 u64 delta;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001074
mingo39aeb522009-09-14 20:04:48 +02001075 if (list_empty(&atoms->work_list))
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001076 return;
1077
mingo39aeb522009-09-14 20:04:48 +02001078 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001079
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001080 if (atom->state != THREAD_WAIT_CPU)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001081 return;
1082
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001083 if (timestamp < atom->wake_up_time) {
1084 atom->state = THREAD_IGNORE;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001085 return;
1086 }
1087
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001088 atom->state = THREAD_SCHED_IN;
1089 atom->sched_in_time = timestamp;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001090
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001091 delta = atom->sched_in_time - atom->wake_up_time;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001092 atoms->total_lat += delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001093 if (delta > atoms->max_lat) {
Frederic Weisbecker66685672009-09-13 01:56:25 +02001094 atoms->max_lat = delta;
Frederic Weisbecker3786310a2009-12-09 21:40:08 +01001095 atoms->max_lat_at = timestamp;
1096 }
Frederic Weisbecker66685672009-09-13 01:56:25 +02001097 atoms->nb_atoms++;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001098}
1099
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001100static int latency_switch_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001101 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001102 struct perf_sample *sample,
1103 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001104{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001105 const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1106 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1107 const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state");
mingo39aeb522009-09-14 20:04:48 +02001108 struct work_atoms *out_events, *in_events;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001109 struct thread *sched_out, *sched_in;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001110 u64 timestamp0, timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001111 int cpu = sample->cpu, err = -1;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001112 s64 delta;
1113
mingo39aeb522009-09-14 20:04:48 +02001114 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
Ingo Molnarea92ed52009-09-12 10:08:34 +02001115
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001116 timestamp0 = sched->cpu_last_switched[cpu];
1117 sched->cpu_last_switched[cpu] = timestamp;
Ingo Molnarea92ed52009-09-12 10:08:34 +02001118 if (timestamp0)
1119 delta = timestamp - timestamp0;
1120 else
1121 delta = 0;
1122
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001123 if (delta < 0) {
1124 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1125 return -1;
1126 }
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001127
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001128 sched_out = machine__findnew_thread(machine, -1, prev_pid);
1129 sched_in = machine__findnew_thread(machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001130 if (sched_out == NULL || sched_in == NULL)
1131 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001132
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001133 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001134 if (!out_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001135 if (thread_atoms_insert(sched, sched_out))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001136 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001137 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001138 if (!out_events) {
1139 pr_err("out-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001140 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001141 }
mingo39aeb522009-09-14 20:04:48 +02001142 }
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001143 if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001144 return -1;
mingo39aeb522009-09-14 20:04:48 +02001145
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001146 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
mingo39aeb522009-09-14 20:04:48 +02001147 if (!in_events) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001148 if (thread_atoms_insert(sched, sched_in))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001149 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001150 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001151 if (!in_events) {
1152 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001153 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001154 }
mingo39aeb522009-09-14 20:04:48 +02001155 /*
1156 * Take came in we have not heard about yet,
1157 * add in an initial atom in runnable state:
1158 */
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001159 if (add_sched_out_event(in_events, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001160 goto out_put;
mingo39aeb522009-09-14 20:04:48 +02001161 }
1162 add_sched_in_event(in_events, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001163 err = 0;
1164out_put:
1165 thread__put(sched_out);
1166 thread__put(sched_in);
1167 return err;
mingo39aeb522009-09-14 20:04:48 +02001168}
1169
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001170static int latency_runtime_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001171 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001172 struct perf_sample *sample,
1173 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001174{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001175 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
1176 const u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001177 struct thread *thread = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001178 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001179 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001180 int cpu = sample->cpu, err = -1;
1181
1182 if (thread == NULL)
1183 return -1;
mingo39aeb522009-09-14 20:04:48 +02001184
1185 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
mingo39aeb522009-09-14 20:04:48 +02001186 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001187 if (thread_atoms_insert(sched, thread))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001188 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001189 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001190 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001191 pr_err("in-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001192 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001193 }
1194 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001195 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001196 }
1197
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001198 add_runtime_event(atoms, runtime, timestamp);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001199 err = 0;
1200out_put:
1201 thread__put(thread);
1202 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001203}
1204
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001205static int latency_wakeup_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001206 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001207 struct perf_sample *sample,
1208 struct machine *machine)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001209{
Peter Zijlstra0680ee72014-05-12 20:19:46 +02001210 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
mingo39aeb522009-09-14 20:04:48 +02001211 struct work_atoms *atoms;
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001212 struct work_atom *atom;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001213 struct thread *wakee;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001214 u64 timestamp = sample->time;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001215 int err = -1;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001216
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001217 wakee = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001218 if (wakee == NULL)
1219 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001220 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Frederic Weisbecker17562202009-09-12 23:11:32 +02001221 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001222 if (thread_atoms_insert(sched, wakee))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001223 goto out_put;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001224 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001225 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001226 pr_err("wakeup-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001227 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001228 }
1229 if (add_sched_out_event(atoms, 'S', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001230 goto out_put;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001231 }
1232
mingo39aeb522009-09-14 20:04:48 +02001233 BUG_ON(list_empty(&atoms->work_list));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001234
mingo39aeb522009-09-14 20:04:48 +02001235 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001236
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001237 /*
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001238 * As we do not guarantee the wakeup event happens when
1239 * task is out of run queue, also may happen when task is
1240 * on run queue and wakeup only change ->state to TASK_RUNNING,
1241 * then we should not set the ->wake_up_time when wake up a
1242 * task which is on run queue.
1243 *
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001244 * You WILL be missing events if you've recorded only
1245 * one CPU, or are only looking at only one, so don't
Dongsheng Yang67d6259dd2014-05-13 10:38:21 +09001246 * skip in this case.
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001247 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001248 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001249 goto out_ok;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001250
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001251 sched->nr_timestamps++;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001252 if (atom->sched_out_time > timestamp) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001253 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001254 goto out_ok;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001255 }
Frederic Weisbeckeraa1ab9d2009-09-14 03:01:12 +02001256
Ingo Molnarb1ffe8f2009-09-11 12:12:54 +02001257 atom->state = THREAD_WAIT_CPU;
1258 atom->wake_up_time = timestamp;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001259out_ok:
1260 err = 0;
1261out_put:
1262 thread__put(wakee);
1263 return err;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001264}
1265
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001266static int latency_migrate_task_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001267 struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001268 struct perf_sample *sample,
1269 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001270{
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001271 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001272 u64 timestamp = sample->time;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001273 struct work_atoms *atoms;
1274 struct work_atom *atom;
1275 struct thread *migrant;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001276 int err = -1;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001277
1278 /*
1279 * Only need to worry about migration when profiling one CPU.
1280 */
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001281 if (sched->profile_cpu == -1)
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001282 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001283
Adrian Hunter1fcb8762014-07-14 13:02:25 +03001284 migrant = machine__findnew_thread(machine, -1, pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001285 if (migrant == NULL)
1286 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001287 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001288 if (!atoms) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001289 if (thread_atoms_insert(sched, migrant))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001290 goto out_put;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001291 register_pid(sched, migrant->tid, thread__comm_str(migrant));
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001292 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001293 if (!atoms) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001294 pr_err("migration-event: Internal tree error");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001295 goto out_put;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001296 }
1297 if (add_sched_out_event(atoms, 'R', timestamp))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001298 goto out_put;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001299 }
1300
1301 BUG_ON(list_empty(&atoms->work_list));
1302
1303 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1304 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1305
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001306 sched->nr_timestamps++;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001307
1308 if (atom->sched_out_time > timestamp)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001309 sched->nr_unordered_timestamps++;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001310 err = 0;
1311out_put:
1312 thread__put(migrant);
1313 return err;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001314}
1315
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001316static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001317{
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001318 int i;
1319 int ret;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001320 u64 avg;
Namhyung Kim99620a52016-10-24 11:02:45 +09001321 char max_lat_at[32];
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001322
mingo39aeb522009-09-14 20:04:48 +02001323 if (!work_list->nb_atoms)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001324 return;
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001325 /*
1326 * Ignore idle threads:
1327 */
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001328 if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
Ingo Molnarea57c4f2009-09-13 18:15:54 +02001329 return;
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001330
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001331 sched->all_runtime += work_list->total_runtime;
1332 sched->all_count += work_list->nb_atoms;
Frederic Weisbecker66685672009-09-13 01:56:25 +02001333
Josef Bacik2f80dd42015-05-22 09:18:40 -04001334 if (work_list->num_merged > 1)
1335 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged);
1336 else
1337 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001338
mingo08f69e62009-09-14 18:30:44 +02001339 for (i = 0; i < 24 - ret; i++)
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001340 printf(" ");
1341
mingo39aeb522009-09-14 20:04:48 +02001342 avg = work_list->total_lat / work_list->nb_atoms;
Namhyung Kim99620a52016-10-24 11:02:45 +09001343 timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at));
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001344
Namhyung Kim99620a52016-10-24 11:02:45 +09001345 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 -03001346 (double)work_list->total_runtime / NSEC_PER_MSEC,
1347 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
1348 (double)work_list->max_lat / NSEC_PER_MSEC,
Namhyung Kim99620a52016-10-24 11:02:45 +09001349 max_lat_at);
Frederic Weisbeckercdce9d72009-09-12 08:06:14 +02001350}
1351
mingo39aeb522009-09-14 20:04:48 +02001352static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001353{
Jiri Olsa0014de12015-11-02 12:10:25 +01001354 if (l->thread == r->thread)
1355 return 0;
Adrian Hunter38051232013-07-04 16:20:31 +03001356 if (l->thread->tid < r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001357 return -1;
Adrian Hunter38051232013-07-04 16:20:31 +03001358 if (l->thread->tid > r->thread->tid)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001359 return 1;
Jiri Olsa0014de12015-11-02 12:10:25 +01001360 return (int)(l->thread - r->thread);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001361}
1362
mingo39aeb522009-09-14 20:04:48 +02001363static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001364{
1365 u64 avgl, avgr;
1366
1367 if (!l->nb_atoms)
1368 return -1;
1369
1370 if (!r->nb_atoms)
1371 return 1;
1372
1373 avgl = l->total_lat / l->nb_atoms;
1374 avgr = r->total_lat / r->nb_atoms;
1375
1376 if (avgl < avgr)
1377 return -1;
1378 if (avgl > avgr)
1379 return 1;
1380
1381 return 0;
1382}
1383
mingo39aeb522009-09-14 20:04:48 +02001384static int max_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001385{
1386 if (l->max_lat < r->max_lat)
1387 return -1;
1388 if (l->max_lat > r->max_lat)
1389 return 1;
1390
1391 return 0;
1392}
1393
mingo39aeb522009-09-14 20:04:48 +02001394static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001395{
1396 if (l->nb_atoms < r->nb_atoms)
1397 return -1;
1398 if (l->nb_atoms > r->nb_atoms)
1399 return 1;
1400
1401 return 0;
1402}
1403
mingo39aeb522009-09-14 20:04:48 +02001404static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001405{
1406 if (l->total_runtime < r->total_runtime)
1407 return -1;
1408 if (l->total_runtime > r->total_runtime)
1409 return 1;
1410
1411 return 0;
1412}
1413
Randy Dunlapcbef79a2009-10-05 13:17:29 -07001414static int sort_dimension__add(const char *tok, struct list_head *list)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001415{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001416 size_t i;
1417 static struct sort_dimension avg_sort_dimension = {
1418 .name = "avg",
1419 .cmp = avg_cmp,
1420 };
1421 static struct sort_dimension max_sort_dimension = {
1422 .name = "max",
1423 .cmp = max_cmp,
1424 };
1425 static struct sort_dimension pid_sort_dimension = {
1426 .name = "pid",
1427 .cmp = pid_cmp,
1428 };
1429 static struct sort_dimension runtime_sort_dimension = {
1430 .name = "runtime",
1431 .cmp = runtime_cmp,
1432 };
1433 static struct sort_dimension switch_sort_dimension = {
1434 .name = "switch",
1435 .cmp = switch_cmp,
1436 };
1437 struct sort_dimension *available_sorts[] = {
1438 &pid_sort_dimension,
1439 &avg_sort_dimension,
1440 &max_sort_dimension,
1441 &switch_sort_dimension,
1442 &runtime_sort_dimension,
1443 };
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001444
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001445 for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001446 if (!strcmp(available_sorts[i]->name, tok)) {
1447 list_add_tail(&available_sorts[i]->list, list);
1448
1449 return 0;
1450 }
1451 }
1452
1453 return -1;
1454}
1455
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001456static void perf_sched__sort_lat(struct perf_sched *sched)
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001457{
1458 struct rb_node *node;
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001459 struct rb_root_cached *root = &sched->atom_root;
Josef Bacik2f80dd42015-05-22 09:18:40 -04001460again:
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001461 for (;;) {
mingo39aeb522009-09-14 20:04:48 +02001462 struct work_atoms *data;
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001463 node = rb_first_cached(root);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001464 if (!node)
1465 break;
1466
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08001467 rb_erase_cached(node, root);
mingo39aeb522009-09-14 20:04:48 +02001468 data = rb_entry(node, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001469 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001470 }
Josef Bacik2f80dd42015-05-22 09:18:40 -04001471 if (root == &sched->atom_root) {
1472 root = &sched->merged_atom_root;
1473 goto again;
1474 }
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02001475}
1476
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001477static int process_sched_wakeup_event(struct perf_tool *tool,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001478 struct evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001479 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001480 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001481{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001482 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001483
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001484 if (sched->tp_handler->wakeup_event)
1485 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001486
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001487 return 0;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001488}
1489
Jiri Olsaa151a372016-04-12 15:29:29 +02001490union map_priv {
1491 void *ptr;
1492 bool color;
1493};
1494
1495static bool thread__has_color(struct thread *thread)
1496{
1497 union map_priv priv = {
1498 .ptr = thread__priv(thread),
1499 };
1500
1501 return priv.color;
1502}
1503
1504static struct thread*
1505map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1506{
1507 struct thread *thread = machine__findnew_thread(machine, pid, tid);
1508 union map_priv priv = {
1509 .color = false,
1510 };
1511
1512 if (!sched->map.color_pids || !thread || thread__priv(thread))
1513 return thread;
1514
1515 if (thread_map__has(sched->map.color_pids, tid))
1516 priv.color = true;
1517
1518 thread__set_priv(thread, priv.ptr);
1519 return thread;
1520}
1521
Jiri Olsa32dcd022019-07-21 13:23:51 +02001522static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001523 struct perf_sample *sample, struct machine *machine)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001524{
Dongsheng Yang9d372ca2014-05-16 14:37:05 +09001525 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
1526 struct thread *sched_in;
Changbin Du8640da92018-03-06 11:37:36 +08001527 struct thread_runtime *tr;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001528 int new_shortname;
Arnaldo Carvalho de Melo7f7f8d02012-08-07 11:33:42 -03001529 u64 timestamp0, timestamp = sample->time;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001530 s64 delta;
Jiri Olsa99623c62016-04-12 15:29:26 +02001531 int i, this_cpu = sample->cpu;
1532 int cpus_nr;
1533 bool new_cpu = false;
Jiri Olsa8cd91192016-04-12 15:29:27 +02001534 const char *color = PERF_COLOR_NORMAL;
Namhyung Kim99620a52016-10-24 11:02:45 +09001535 char stimestamp[32];
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001536
1537 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1538
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001539 if (this_cpu > sched->max_cpu)
1540 sched->max_cpu = this_cpu;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001541
Jiri Olsa99623c62016-04-12 15:29:26 +02001542 if (sched->map.comp) {
1543 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
1544 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) {
1545 sched->map.comp_cpus[cpus_nr++] = this_cpu;
1546 new_cpu = true;
1547 }
1548 } else
1549 cpus_nr = sched->max_cpu;
1550
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001551 timestamp0 = sched->cpu_last_switched[this_cpu];
1552 sched->cpu_last_switched[this_cpu] = timestamp;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001553 if (timestamp0)
1554 delta = timestamp - timestamp0;
1555 else
1556 delta = 0;
1557
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001558 if (delta < 0) {
Namhyung Kim60b7d142012-09-12 11:11:06 +09001559 pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001560 return -1;
1561 }
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001562
Jiri Olsaa151a372016-04-12 15:29:29 +02001563 sched_in = map__findnew_thread(sched, machine, -1, next_pid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001564 if (sched_in == NULL)
1565 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001566
Changbin Du8640da92018-03-06 11:37:36 +08001567 tr = thread__get_runtime(sched_in);
1568 if (tr == NULL) {
1569 thread__put(sched_in);
1570 return -1;
1571 }
1572
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001573 sched->curr_thread[this_cpu] = thread__get(sched_in);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001574
1575 printf(" ");
1576
1577 new_shortname = 0;
Changbin Du8640da92018-03-06 11:37:36 +08001578 if (!tr->shortname[0]) {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001579 if (!strcmp(thread__comm_str(sched_in), "swapper")) {
1580 /*
1581 * Don't allocate a letter-number for swapper:0
1582 * as a shortname. Instead, we use '.' for it.
1583 */
Changbin Du8640da92018-03-06 11:37:36 +08001584 tr->shortname[0] = '.';
1585 tr->shortname[1] = ' ';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001586 } else {
Changbin Du8640da92018-03-06 11:37:36 +08001587 tr->shortname[0] = sched->next_shortname1;
1588 tr->shortname[1] = sched->next_shortname2;
Dongsheng6bcab4e2014-05-06 14:39:01 +09001589
1590 if (sched->next_shortname1 < 'Z') {
1591 sched->next_shortname1++;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001592 } else {
Dongsheng6bcab4e2014-05-06 14:39:01 +09001593 sched->next_shortname1 = 'A';
1594 if (sched->next_shortname2 < '9')
1595 sched->next_shortname2++;
1596 else
1597 sched->next_shortname2 = '0';
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001598 }
1599 }
1600 new_shortname = 1;
1601 }
1602
Jiri Olsa99623c62016-04-12 15:29:26 +02001603 for (i = 0; i < cpus_nr; i++) {
1604 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i;
Jiri Olsaa151a372016-04-12 15:29:29 +02001605 struct thread *curr_thread = sched->curr_thread[cpu];
Changbin Du8640da92018-03-06 11:37:36 +08001606 struct thread_runtime *curr_tr;
Jiri Olsaa151a372016-04-12 15:29:29 +02001607 const char *pid_color = color;
Jiri Olsacf294f22016-04-12 15:29:30 +02001608 const char *cpu_color = color;
Jiri Olsaa151a372016-04-12 15:29:29 +02001609
1610 if (curr_thread && thread__has_color(curr_thread))
1611 pid_color = COLOR_PIDS;
Jiri Olsa99623c62016-04-12 15:29:26 +02001612
Jiri Olsa73643bb2016-04-12 15:29:31 +02001613 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu))
1614 continue;
1615
Jiri Olsacf294f22016-04-12 15:29:30 +02001616 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu))
1617 cpu_color = COLOR_CPUS;
1618
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001619 if (cpu != this_cpu)
Namhyung Kim1208bb22016-10-24 11:02:43 +09001620 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001621 else
Jiri Olsacf294f22016-04-12 15:29:30 +02001622 color_fprintf(stdout, cpu_color, "*");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001623
Changbin Du8640da92018-03-06 11:37:36 +08001624 if (sched->curr_thread[cpu]) {
1625 curr_tr = thread__get_runtime(sched->curr_thread[cpu]);
1626 if (curr_tr == NULL) {
1627 thread__put(sched_in);
1628 return -1;
1629 }
1630 color_fprintf(stdout, pid_color, "%2s ", curr_tr->shortname);
1631 } else
Jiri Olsa8cd91192016-04-12 15:29:27 +02001632 color_fprintf(stdout, color, " ");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001633 }
1634
Jiri Olsa73643bb2016-04-12 15:29:31 +02001635 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu))
1636 goto out;
1637
Namhyung Kim99620a52016-10-24 11:02:45 +09001638 timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
1639 color_fprintf(stdout, color, " %12s secs ", stimestamp);
Changbin Du99a3c3a2018-03-06 11:37:37 +08001640 if (new_shortname || tr->comm_changed || (verbose > 0 && sched_in->tid)) {
Jiri Olsaa151a372016-04-12 15:29:29 +02001641 const char *pid_color = color;
1642
1643 if (thread__has_color(sched_in))
1644 pid_color = COLOR_PIDS;
1645
1646 color_fprintf(stdout, pid_color, "%s => %s:%d",
Changbin Du8640da92018-03-06 11:37:36 +08001647 tr->shortname, thread__comm_str(sched_in), sched_in->tid);
Changbin Du99a3c3a2018-03-06 11:37:37 +08001648 tr->comm_changed = false;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001649 }
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001650
Jiri Olsa99623c62016-04-12 15:29:26 +02001651 if (sched->map.comp && new_cpu)
Jiri Olsa8cd91192016-04-12 15:29:27 +02001652 color_fprintf(stdout, color, " (CPU %d)", this_cpu);
Jiri Olsa99623c62016-04-12 15:29:26 +02001653
Jiri Olsa73643bb2016-04-12 15:29:31 +02001654out:
Jiri Olsa8cd91192016-04-12 15:29:27 +02001655 color_fprintf(stdout, color, "\n");
Jiri Olsa99623c62016-04-12 15:29:26 +02001656
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001657 thread__put(sched_in);
1658
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001659 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02001660}
1661
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001662static int process_sched_switch_event(struct perf_tool *tool,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001663 struct evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001664 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001665 struct machine *machine)
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001666{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001667 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001668 int this_cpu = sample->cpu, err = 0;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001669 u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"),
1670 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001671
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001672 if (sched->curr_pid[this_cpu] != (u32)-1) {
Ingo Molnarc8a37752009-09-16 14:07:00 +02001673 /*
1674 * Are we trying to switch away a PID that is
1675 * not current?
1676 */
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001677 if (sched->curr_pid[this_cpu] != prev_pid)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001678 sched->nr_context_switch_bugs++;
Ingo Molnarc8a37752009-09-16 14:07:00 +02001679 }
Ingo Molnarc8a37752009-09-16 14:07:00 +02001680
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001681 if (sched->tp_handler->switch_event)
1682 err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001683
1684 sched->curr_pid[this_cpu] = next_pid;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001685 return err;
Frederic Weisbecker419ab0d2009-09-12 03:59:01 +02001686}
1687
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001688static int process_sched_runtime_event(struct perf_tool *tool,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001689 struct evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001690 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001691 struct machine *machine)
mingo39aeb522009-09-14 20:04:48 +02001692{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001693 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
mingo39aeb522009-09-14 20:04:48 +02001694
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001695 if (sched->tp_handler->runtime_event)
1696 return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001697
1698 return 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001699}
1700
David Aherncb627502013-08-07 22:50:47 -04001701static int perf_sched__process_fork_event(struct perf_tool *tool,
1702 union perf_event *event,
1703 struct perf_sample *sample,
1704 struct machine *machine)
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001705{
1706 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1707
David Aherncb627502013-08-07 22:50:47 -04001708 /* run the fork event through the perf machineruy */
1709 perf_event__process_fork(tool, event, sample, machine);
1710
1711 /* and then run additional processing needed for this command */
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001712 if (sched->tp_handler->fork_event)
David Aherncb627502013-08-07 22:50:47 -04001713 return sched->tp_handler->fork_event(sched, event, machine);
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001714
1715 return 0;
1716}
1717
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001718static int process_sched_migrate_task_event(struct perf_tool *tool,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001719 struct evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001720 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001721 struct machine *machine)
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001722{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03001723 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001724
Arnaldo Carvalho de Melo9ec3f4e2012-09-11 19:29:17 -03001725 if (sched->tp_handler->migrate_task_event)
1726 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001727
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001728 return 0;
Mike Galbraith55ffb7a2009-10-10 14:46:04 +02001729}
1730
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001731typedef int (*tracepoint_handler)(struct perf_tool *tool,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001732 struct evsel *evsel,
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001733 struct perf_sample *sample,
Arnaldo Carvalho de Melo4218e672012-09-11 13:18:47 -03001734 struct machine *machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001735
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001736static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1737 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001738 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001739 struct evsel *evsel,
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001740 struct machine *machine)
Ingo Molnarec156762009-09-11 12:12:54 +02001741{
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001742 int err = 0;
Ingo Molnarec156762009-09-11 12:12:54 +02001743
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03001744 if (evsel->handler != NULL) {
1745 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melo2b7fcbc2012-09-11 19:29:17 -03001746 err = f(tool, evsel, sample, machine);
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001747 }
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001748
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001749 return err;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001750}
1751
Changbin Du99a3c3a2018-03-06 11:37:37 +08001752static int perf_sched__process_comm(struct perf_tool *tool __maybe_unused,
1753 union perf_event *event,
1754 struct perf_sample *sample,
1755 struct machine *machine)
1756{
1757 struct thread *thread;
1758 struct thread_runtime *tr;
1759 int err;
1760
1761 err = perf_event__process_comm(tool, event, sample, machine);
1762 if (err)
1763 return err;
1764
1765 thread = machine__find_thread(machine, sample->pid, sample->tid);
1766 if (!thread) {
1767 pr_err("Internal error: can't find thread\n");
1768 return -1;
1769 }
1770
1771 tr = thread__get_runtime(thread);
1772 if (tr == NULL) {
1773 thread__put(thread);
1774 return -1;
1775 }
1776
1777 tr->comm_changed = true;
1778 thread__put(thread);
1779
1780 return 0;
1781}
1782
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001783static int perf_sched__read_events(struct perf_sched *sched)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001784{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001785 const struct evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001786 { "sched:sched_switch", process_sched_switch_event, },
1787 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1788 { "sched:sched_wakeup", process_sched_wakeup_event, },
1789 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001790 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1791 };
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001792 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001793 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001794 .path = input_name,
1795 .mode = PERF_DATA_MODE_READ,
1796 .force = sched->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001797 };
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001798 int rc = -1;
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001799
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001800 session = perf_session__new(&data, false, &sched->tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05301801 if (IS_ERR(session)) {
1802 pr_debug("Error creating perf session");
1803 return PTR_ERR(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001804 }
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -02001805
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001806 symbol__init(&session->header.env);
Namhyung Kim04934102014-08-12 15:40:41 +09001807
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001808 if (perf_session__set_tracepoints_handlers(session, handlers))
1809 goto out_delete;
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -02001810
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001811 if (perf_session__has_traces(session, "record -R")) {
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001812 int err = perf_session__process_events(session);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001813 if (err) {
1814 pr_err("Failed to process events, error %d", err);
1815 goto out_delete;
1816 }
Jiri Olsa4c09baf2011-08-08 23:03:34 +02001817
Arnaldo Carvalho de Melo75be9892015-02-14 14:50:11 -03001818 sched->nr_events = session->evlist->stats.nr_events[0];
1819 sched->nr_lost_events = session->evlist->stats.total_lost;
1820 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -03001821 }
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -02001822
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001823 rc = 0;
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03001824out_delete:
1825 perf_session__delete(session);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03001826 return rc;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02001827}
1828
David Ahern49394a22016-11-16 15:06:29 +09001829/*
1830 * scheduling times are printed as msec.usec
1831 */
1832static inline void print_sched_time(unsigned long long nsecs, int width)
1833{
1834 unsigned long msecs;
1835 unsigned long usecs;
1836
1837 msecs = nsecs / NSEC_PER_MSEC;
1838 nsecs -= msecs * NSEC_PER_MSEC;
1839 usecs = nsecs / NSEC_PER_USEC;
1840 printf("%*lu.%03lu ", width, msecs, usecs);
1841}
1842
1843/*
1844 * returns runtime data for event, allocating memory for it the
1845 * first time it is used.
1846 */
Jiri Olsa32dcd022019-07-21 13:23:51 +02001847static struct evsel_runtime *perf_evsel__get_runtime(struct evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09001848{
1849 struct evsel_runtime *r = evsel->priv;
1850
1851 if (r == NULL) {
1852 r = zalloc(sizeof(struct evsel_runtime));
1853 evsel->priv = r;
1854 }
1855
1856 return r;
1857}
1858
1859/*
1860 * save last time event was seen per cpu
1861 */
Jiri Olsa32dcd022019-07-21 13:23:51 +02001862static void perf_evsel__save_time(struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09001863 u64 timestamp, u32 cpu)
1864{
1865 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1866
1867 if (r == NULL)
1868 return;
1869
1870 if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
1871 int i, n = __roundup_pow_of_two(cpu+1);
1872 void *p = r->last_time;
1873
1874 p = realloc(r->last_time, n * sizeof(u64));
1875 if (!p)
1876 return;
1877
1878 r->last_time = p;
1879 for (i = r->ncpu; i < n; ++i)
1880 r->last_time[i] = (u64) 0;
1881
1882 r->ncpu = n;
1883 }
1884
1885 r->last_time[cpu] = timestamp;
1886}
1887
1888/* returns last time this event was seen on the given cpu */
Jiri Olsa32dcd022019-07-21 13:23:51 +02001889static u64 perf_evsel__get_time(struct evsel *evsel, u32 cpu)
David Ahern49394a22016-11-16 15:06:29 +09001890{
1891 struct evsel_runtime *r = perf_evsel__get_runtime(evsel);
1892
1893 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
1894 return 0;
1895
1896 return r->last_time[cpu];
1897}
1898
Namhyung Kim9b8087d2016-12-22 15:03:48 +09001899static int comm_width = 30;
David Ahern49394a22016-11-16 15:06:29 +09001900
1901static char *timehist_get_commstr(struct thread *thread)
1902{
1903 static char str[32];
1904 const char *comm = thread__comm_str(thread);
1905 pid_t tid = thread->tid;
1906 pid_t pid = thread->pid_;
1907 int n;
1908
1909 if (pid == 0)
1910 n = scnprintf(str, sizeof(str), "%s", comm);
1911
1912 else if (tid != pid)
1913 n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
1914
1915 else
1916 n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
1917
1918 if (n > comm_width)
1919 comm_width = n;
1920
1921 return str;
1922}
1923
David Aherna407b062016-11-16 15:06:33 +09001924static void timehist_header(struct perf_sched *sched)
David Ahern49394a22016-11-16 15:06:29 +09001925{
David Aherna407b062016-11-16 15:06:33 +09001926 u32 ncpus = sched->max_cpu + 1;
1927 u32 i, j;
1928
David Ahern49394a22016-11-16 15:06:29 +09001929 printf("%15s %6s ", "time", "cpu");
1930
David Aherna407b062016-11-16 15:06:33 +09001931 if (sched->show_cpu_visual) {
1932 printf(" ");
1933 for (i = 0, j = 0; i < ncpus; ++i) {
1934 printf("%x", j++);
1935 if (j > 15)
1936 j = 0;
1937 }
1938 printf(" ");
1939 }
1940
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001941 printf(" %-*s %9s %9s %9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001942 "task name", "wait time", "sch delay", "run time");
1943
Namhyung Kim414e0502017-01-13 19:45:22 +09001944 if (sched->show_state)
1945 printf(" %s", "state");
1946
David Ahern49394a22016-11-16 15:06:29 +09001947 printf("\n");
1948
1949 /*
1950 * units row
1951 */
1952 printf("%15s %-6s ", "", "");
1953
David Aherna407b062016-11-16 15:06:33 +09001954 if (sched->show_cpu_visual)
1955 printf(" %*s ", ncpus, "");
1956
Namhyung Kim414e0502017-01-13 19:45:22 +09001957 printf(" %-*s %9s %9s %9s", comm_width,
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001958 "[tid/pid]", "(msec)", "(msec)", "(msec)");
David Ahern49394a22016-11-16 15:06:29 +09001959
Namhyung Kim414e0502017-01-13 19:45:22 +09001960 if (sched->show_state)
1961 printf(" %5s", "");
1962
1963 printf("\n");
1964
David Ahern49394a22016-11-16 15:06:29 +09001965 /*
1966 * separator
1967 */
1968 printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
1969
David Aherna407b062016-11-16 15:06:33 +09001970 if (sched->show_cpu_visual)
1971 printf(" %.*s ", ncpus, graph_dotted_line);
1972
Namhyung Kim0e6758e2016-12-22 15:03:48 +09001973 printf(" %.*s %.9s %.9s %.9s", comm_width,
David Ahern49394a22016-11-16 15:06:29 +09001974 graph_dotted_line, graph_dotted_line, graph_dotted_line,
1975 graph_dotted_line);
1976
Namhyung Kim414e0502017-01-13 19:45:22 +09001977 if (sched->show_state)
1978 printf(" %.5s", graph_dotted_line);
1979
David Ahern49394a22016-11-16 15:06:29 +09001980 printf("\n");
1981}
1982
Namhyung Kim414e0502017-01-13 19:45:22 +09001983static char task_state_char(struct thread *thread, int state)
1984{
1985 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1986 unsigned bit = state ? ffs(state) : 0;
1987
1988 /* 'I' for idle */
1989 if (thread->tid == 0)
1990 return 'I';
1991
1992 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
1993}
1994
David Ahernfc1469f2016-11-16 15:06:31 +09001995static void timehist_print_sample(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001996 struct evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09001997 struct perf_sample *sample,
David Ahern6c973c92016-11-16 15:06:32 +09001998 struct addr_location *al,
David Ahern853b7402016-11-29 10:15:44 -07001999 struct thread *thread,
Namhyung Kim414e0502017-01-13 19:45:22 +09002000 u64 t, int state)
David Ahern49394a22016-11-16 15:06:29 +09002001{
2002 struct thread_runtime *tr = thread__priv(thread);
Brendan Gregg292c4a82017-03-14 01:56:29 +00002003 const char *next_comm = perf_evsel__strval(evsel, sample, "next_comm");
2004 const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid");
David Aherna407b062016-11-16 15:06:33 +09002005 u32 max_cpus = sched->max_cpu + 1;
David Ahern49394a22016-11-16 15:06:29 +09002006 char tstr[64];
Brendan Gregg292c4a82017-03-14 01:56:29 +00002007 char nstr[30];
Namhyung Kim941bdea2017-01-13 19:45:21 +09002008 u64 wait_time;
David Ahern49394a22016-11-16 15:06:29 +09002009
David Ahern853b7402016-11-29 10:15:44 -07002010 timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
David Ahern49394a22016-11-16 15:06:29 +09002011 printf("%15s [%04d] ", tstr, sample->cpu);
2012
David Aherna407b062016-11-16 15:06:33 +09002013 if (sched->show_cpu_visual) {
2014 u32 i;
2015 char c;
2016
2017 printf(" ");
2018 for (i = 0; i < max_cpus; ++i) {
2019 /* flag idle times with 'i'; others are sched events */
2020 if (i == sample->cpu)
2021 c = (thread->tid == 0) ? 'i' : 's';
2022 else
2023 c = ' ';
2024 printf("%c", c);
2025 }
2026 printf(" ");
2027 }
2028
David Ahern49394a22016-11-16 15:06:29 +09002029 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2030
Namhyung Kim941bdea2017-01-13 19:45:21 +09002031 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
2032 print_sched_time(wait_time, 6);
2033
David Ahern49394a22016-11-16 15:06:29 +09002034 print_sched_time(tr->dt_delay, 6);
2035 print_sched_time(tr->dt_run, 6);
David Ahernfc1469f2016-11-16 15:06:31 +09002036
Namhyung Kim414e0502017-01-13 19:45:22 +09002037 if (sched->show_state)
2038 printf(" %5c ", task_state_char(thread, state));
2039
Brendan Gregg292c4a82017-03-14 01:56:29 +00002040 if (sched->show_next) {
2041 snprintf(nstr, sizeof(nstr), "next: %s[%d]", next_comm, next_pid);
2042 printf(" %-*s", comm_width, nstr);
2043 }
2044
2045 if (sched->show_wakeups && !sched->show_next)
David Ahernfc1469f2016-11-16 15:06:31 +09002046 printf(" %-*s", comm_width, "");
2047
David Ahern6c973c92016-11-16 15:06:32 +09002048 if (thread->tid == 0)
2049 goto out;
2050
2051 if (sched->show_callchain)
2052 printf(" ");
2053
2054 sample__fprintf_sym(sample, al, 0,
2055 EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
Namhyung Kim2d9bbf62016-11-24 10:11:13 +09002056 EVSEL__PRINT_CALLCHAIN_ARROW |
2057 EVSEL__PRINT_SKIP_IGNORED,
David Ahern6c973c92016-11-16 15:06:32 +09002058 &callchain_cursor, stdout);
2059
2060out:
David Ahern49394a22016-11-16 15:06:29 +09002061 printf("\n");
2062}
2063
2064/*
2065 * Explanation of delta-time stats:
2066 *
2067 * t = time of current schedule out event
2068 * tprev = time of previous sched out event
2069 * also time of schedule-in event for current task
2070 * last_time = time of last sched change event for current task
2071 * (i.e, time process was last scheduled out)
2072 * ready_to_run = time of wakeup for current task
2073 *
2074 * -----|------------|------------|------------|------
2075 * last ready tprev t
2076 * time to run
2077 *
2078 * |-------- dt_wait --------|
2079 * |- dt_delay -|-- dt_run --|
2080 *
2081 * dt_run = run time of current task
2082 * dt_wait = time between last schedule out event for task and tprev
2083 * represents time spent off the cpu
2084 * dt_delay = time between wakeup and schedule-in of task
2085 */
2086
2087static void timehist_update_runtime_stats(struct thread_runtime *r,
2088 u64 t, u64 tprev)
2089{
2090 r->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002091 r->dt_sleep = 0;
2092 r->dt_iowait = 0;
2093 r->dt_preempt = 0;
David Ahern49394a22016-11-16 15:06:29 +09002094 r->dt_run = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002095
David Ahern49394a22016-11-16 15:06:29 +09002096 if (tprev) {
2097 r->dt_run = t - tprev;
2098 if (r->ready_to_run) {
2099 if (r->ready_to_run > tprev)
2100 pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
2101 else
2102 r->dt_delay = tprev - r->ready_to_run;
2103 }
2104
2105 if (r->last_time > tprev)
2106 pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
Namhyung Kim941bdea2017-01-13 19:45:21 +09002107 else if (r->last_time) {
2108 u64 dt_wait = tprev - r->last_time;
2109
2110 if (r->last_state == TASK_RUNNING)
2111 r->dt_preempt = dt_wait;
2112 else if (r->last_state == TASK_UNINTERRUPTIBLE)
2113 r->dt_iowait = dt_wait;
2114 else
2115 r->dt_sleep = dt_wait;
2116 }
David Ahern49394a22016-11-16 15:06:29 +09002117 }
2118
2119 update_stats(&r->run_stats, r->dt_run);
Namhyung Kim587782c2017-01-13 19:45:23 +09002120
2121 r->total_run_time += r->dt_run;
2122 r->total_delay_time += r->dt_delay;
2123 r->total_sleep_time += r->dt_sleep;
2124 r->total_iowait_time += r->dt_iowait;
2125 r->total_preempt_time += r->dt_preempt;
David Ahern49394a22016-11-16 15:06:29 +09002126}
2127
Namhyung Kim96039c72016-12-08 23:47:50 +09002128static bool is_idle_sample(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002129 struct evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09002130{
2131 /* pid 0 == swapper == idle task */
Namhyung Kim96039c72016-12-08 23:47:50 +09002132 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0)
2133 return perf_evsel__intval(evsel, sample, "prev_pid") == 0;
David Ahern49394a22016-11-16 15:06:29 +09002134
Namhyung Kim96039c72016-12-08 23:47:50 +09002135 return sample->pid == 0;
2136}
2137
2138static void save_task_callchain(struct perf_sched *sched,
2139 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002140 struct evsel *evsel,
Namhyung Kim96039c72016-12-08 23:47:50 +09002141 struct machine *machine)
2142{
2143 struct callchain_cursor *cursor = &callchain_cursor;
2144 struct thread *thread;
David Ahern6c973c92016-11-16 15:06:32 +09002145
2146 /* want main thread for process - has maps */
2147 thread = machine__findnew_thread(machine, sample->pid, sample->pid);
2148 if (thread == NULL) {
2149 pr_debug("Failed to get thread for pid %d.\n", sample->pid);
Namhyung Kim96039c72016-12-08 23:47:50 +09002150 return;
David Ahern6c973c92016-11-16 15:06:32 +09002151 }
2152
Arnaldo Carvalho de Melo4c505632018-05-28 16:07:56 -03002153 if (!sched->show_callchain || sample->callchain == NULL)
Namhyung Kim96039c72016-12-08 23:47:50 +09002154 return;
David Ahern6c973c92016-11-16 15:06:32 +09002155
2156 if (thread__resolve_callchain(thread, cursor, evsel, sample,
Namhyung Kim8388deb2016-11-24 10:11:14 +09002157 NULL, NULL, sched->max_stack + 2) != 0) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002158 if (verbose > 0)
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -03002159 pr_err("Failed to resolve callchain. Skipping\n");
David Ahern6c973c92016-11-16 15:06:32 +09002160
Namhyung Kim96039c72016-12-08 23:47:50 +09002161 return;
David Ahern6c973c92016-11-16 15:06:32 +09002162 }
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002163
David Ahern6c973c92016-11-16 15:06:32 +09002164 callchain_cursor_commit(cursor);
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002165
2166 while (true) {
2167 struct callchain_cursor_node *node;
2168 struct symbol *sym;
2169
2170 node = callchain_cursor_current(cursor);
2171 if (node == NULL)
2172 break;
2173
2174 sym = node->sym;
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03002175 if (sym) {
Namhyung Kimcdeb01b2016-11-24 10:11:12 +09002176 if (!strcmp(sym->name, "schedule") ||
2177 !strcmp(sym->name, "__schedule") ||
2178 !strcmp(sym->name, "preempt_schedule"))
2179 sym->ignore = 1;
2180 }
2181
2182 callchain_cursor_advance(cursor);
2183 }
David Ahern49394a22016-11-16 15:06:29 +09002184}
2185
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002186static int init_idle_thread(struct thread *thread)
2187{
2188 struct idle_thread_runtime *itr;
2189
2190 thread__set_comm(thread, idle_comm, 0);
2191
2192 itr = zalloc(sizeof(*itr));
2193 if (itr == NULL)
2194 return -ENOMEM;
2195
2196 init_stats(&itr->tr.run_stats);
2197 callchain_init(&itr->callchain);
2198 callchain_cursor_reset(&itr->cursor);
2199 thread__set_priv(thread, itr);
2200
2201 return 0;
2202}
2203
David Ahern49394a22016-11-16 15:06:29 +09002204/*
2205 * Track idle stats per cpu by maintaining a local thread
2206 * struct for the idle task on each cpu.
2207 */
2208static int init_idle_threads(int ncpu)
2209{
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002210 int i, ret;
David Ahern49394a22016-11-16 15:06:29 +09002211
2212 idle_threads = zalloc(ncpu * sizeof(struct thread *));
2213 if (!idle_threads)
2214 return -ENOMEM;
2215
Namhyung Kimb3363522016-12-06 12:40:05 +09002216 idle_max_cpu = ncpu;
David Ahern49394a22016-11-16 15:06:29 +09002217
2218 /* allocate the actual thread struct if needed */
2219 for (i = 0; i < ncpu; ++i) {
2220 idle_threads[i] = thread__new(0, 0);
2221 if (idle_threads[i] == NULL)
2222 return -ENOMEM;
2223
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002224 ret = init_idle_thread(idle_threads[i]);
2225 if (ret < 0)
2226 return ret;
David Ahern49394a22016-11-16 15:06:29 +09002227 }
2228
2229 return 0;
2230}
2231
2232static void free_idle_threads(void)
2233{
2234 int i;
2235
2236 if (idle_threads == NULL)
2237 return;
2238
Namhyung Kimb3363522016-12-06 12:40:05 +09002239 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern49394a22016-11-16 15:06:29 +09002240 if ((idle_threads[i]))
2241 thread__delete(idle_threads[i]);
2242 }
2243
2244 free(idle_threads);
2245}
2246
2247static struct thread *get_idle_thread(int cpu)
2248{
2249 /*
2250 * expand/allocate array of pointers to local thread
2251 * structs if needed
2252 */
2253 if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
2254 int i, j = __roundup_pow_of_two(cpu+1);
2255 void *p;
2256
2257 p = realloc(idle_threads, j * sizeof(struct thread *));
2258 if (!p)
2259 return NULL;
2260
2261 idle_threads = (struct thread **) p;
Namhyung Kimb3363522016-12-06 12:40:05 +09002262 for (i = idle_max_cpu; i < j; ++i)
David Ahern49394a22016-11-16 15:06:29 +09002263 idle_threads[i] = NULL;
2264
2265 idle_max_cpu = j;
2266 }
2267
2268 /* allocate a new thread struct if needed */
2269 if (idle_threads[cpu] == NULL) {
2270 idle_threads[cpu] = thread__new(0, 0);
2271
2272 if (idle_threads[cpu]) {
Namhyung Kim3bc2fa92016-12-08 23:47:51 +09002273 if (init_idle_thread(idle_threads[cpu]) < 0)
2274 return NULL;
David Ahern49394a22016-11-16 15:06:29 +09002275 }
2276 }
2277
2278 return idle_threads[cpu];
2279}
2280
Arnaldo Carvalho de Melo4c505632018-05-28 16:07:56 -03002281static void save_idle_callchain(struct perf_sched *sched,
2282 struct idle_thread_runtime *itr,
Namhyung Kim699b5b92016-12-08 23:47:52 +09002283 struct perf_sample *sample)
2284{
Arnaldo Carvalho de Melo4c505632018-05-28 16:07:56 -03002285 if (!sched->show_callchain || sample->callchain == NULL)
Namhyung Kim699b5b92016-12-08 23:47:52 +09002286 return;
2287
2288 callchain_cursor__copy(&itr->cursor, &callchain_cursor);
2289}
2290
David Ahern6c973c92016-11-16 15:06:32 +09002291static struct thread *timehist_get_thread(struct perf_sched *sched,
2292 struct perf_sample *sample,
David Ahern49394a22016-11-16 15:06:29 +09002293 struct machine *machine,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002294 struct evsel *evsel)
David Ahern49394a22016-11-16 15:06:29 +09002295{
2296 struct thread *thread;
2297
Namhyung Kim96039c72016-12-08 23:47:50 +09002298 if (is_idle_sample(sample, evsel)) {
David Ahern49394a22016-11-16 15:06:29 +09002299 thread = get_idle_thread(sample->cpu);
2300 if (thread == NULL)
2301 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2302
2303 } else {
Namhyung Kim5d92d962016-12-06 12:40:03 +09002304 /* there were samples with tid 0 but non-zero pid */
2305 thread = machine__findnew_thread(machine, sample->pid,
2306 sample->tid ?: sample->pid);
David Ahern49394a22016-11-16 15:06:29 +09002307 if (thread == NULL) {
2308 pr_debug("Failed to get thread for tid %d. skipping sample.\n",
2309 sample->tid);
2310 }
Namhyung Kim96039c72016-12-08 23:47:50 +09002311
2312 save_task_callchain(sched, sample, evsel, machine);
Namhyung Kim699b5b92016-12-08 23:47:52 +09002313 if (sched->idle_hist) {
2314 struct thread *idle;
2315 struct idle_thread_runtime *itr;
2316
2317 idle = get_idle_thread(sample->cpu);
2318 if (idle == NULL) {
2319 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2320 return NULL;
2321 }
2322
2323 itr = thread__priv(idle);
2324 if (itr == NULL)
2325 return NULL;
2326
2327 itr->last_thread = thread;
2328
2329 /* copy task callchain when entering to idle */
2330 if (perf_evsel__intval(evsel, sample, "next_pid") == 0)
Arnaldo Carvalho de Melo4c505632018-05-28 16:07:56 -03002331 save_idle_callchain(sched, itr, sample);
Namhyung Kim699b5b92016-12-08 23:47:52 +09002332 }
David Ahern49394a22016-11-16 15:06:29 +09002333 }
2334
2335 return thread;
2336}
2337
David Ahern52df1382016-11-16 15:06:30 +09002338static bool timehist_skip_sample(struct perf_sched *sched,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002339 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002340 struct evsel *evsel,
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002341 struct perf_sample *sample)
David Ahern49394a22016-11-16 15:06:29 +09002342{
2343 bool rc = false;
2344
David Ahern52df1382016-11-16 15:06:30 +09002345 if (thread__is_filtered(thread)) {
David Ahern49394a22016-11-16 15:06:29 +09002346 rc = true;
David Ahern52df1382016-11-16 15:06:30 +09002347 sched->skipped_samples++;
2348 }
David Ahern49394a22016-11-16 15:06:29 +09002349
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002350 if (sched->idle_hist) {
2351 if (strcmp(perf_evsel__name(evsel), "sched:sched_switch"))
2352 rc = true;
2353 else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 &&
2354 perf_evsel__intval(evsel, sample, "next_pid") != 0)
2355 rc = true;
2356 }
2357
David Ahern49394a22016-11-16 15:06:29 +09002358 return rc;
2359}
2360
David Ahernfc1469f2016-11-16 15:06:31 +09002361static void timehist_print_wakeup_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002362 struct evsel *evsel,
David Ahernfc1469f2016-11-16 15:06:31 +09002363 struct perf_sample *sample,
2364 struct machine *machine,
2365 struct thread *awakened)
2366{
2367 struct thread *thread;
2368 char tstr[64];
2369
2370 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2371 if (thread == NULL)
2372 return;
2373
2374 /* show wakeup unless both awakee and awaker are filtered */
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002375 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2376 timehist_skip_sample(sched, awakened, evsel, sample)) {
David Ahernfc1469f2016-11-16 15:06:31 +09002377 return;
2378 }
2379
2380 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2381 printf("%15s [%04d] ", tstr, sample->cpu);
David Aherna407b062016-11-16 15:06:33 +09002382 if (sched->show_cpu_visual)
2383 printf(" %*s ", sched->max_cpu + 1, "");
David Ahernfc1469f2016-11-16 15:06:31 +09002384
2385 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2386
2387 /* dt spacer */
2388 printf(" %9s %9s %9s ", "", "", "");
2389
2390 printf("awakened: %s", timehist_get_commstr(awakened));
2391
2392 printf("\n");
2393}
2394
2395static int timehist_sched_wakeup_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002396 union perf_event *event __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002397 struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09002398 struct perf_sample *sample,
2399 struct machine *machine)
2400{
David Ahernfc1469f2016-11-16 15:06:31 +09002401 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern49394a22016-11-16 15:06:29 +09002402 struct thread *thread;
2403 struct thread_runtime *tr = NULL;
2404 /* want pid of awakened task not pid in sample */
2405 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2406
2407 thread = machine__findnew_thread(machine, 0, pid);
2408 if (thread == NULL)
2409 return -1;
2410
2411 tr = thread__get_runtime(thread);
2412 if (tr == NULL)
2413 return -1;
2414
2415 if (tr->ready_to_run == 0)
2416 tr->ready_to_run = sample->time;
2417
David Ahernfc1469f2016-11-16 15:06:31 +09002418 /* show wakeups if requested */
David Ahern853b7402016-11-29 10:15:44 -07002419 if (sched->show_wakeups &&
2420 !perf_time__skip_sample(&sched->ptime, sample->time))
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002421 timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
David Ahernfc1469f2016-11-16 15:06:31 +09002422
David Ahern49394a22016-11-16 15:06:29 +09002423 return 0;
2424}
2425
David Ahern350f54f2016-11-25 09:28:41 -07002426static void timehist_print_migration_event(struct perf_sched *sched,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002427 struct evsel *evsel,
David Ahern350f54f2016-11-25 09:28:41 -07002428 struct perf_sample *sample,
2429 struct machine *machine,
2430 struct thread *migrated)
2431{
2432 struct thread *thread;
2433 char tstr[64];
2434 u32 max_cpus = sched->max_cpu + 1;
2435 u32 ocpu, dcpu;
2436
2437 if (sched->summary_only)
2438 return;
2439
2440 max_cpus = sched->max_cpu + 1;
2441 ocpu = perf_evsel__intval(evsel, sample, "orig_cpu");
2442 dcpu = perf_evsel__intval(evsel, sample, "dest_cpu");
2443
2444 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2445 if (thread == NULL)
2446 return;
2447
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002448 if (timehist_skip_sample(sched, thread, evsel, sample) &&
2449 timehist_skip_sample(sched, migrated, evsel, sample)) {
David Ahern350f54f2016-11-25 09:28:41 -07002450 return;
2451 }
2452
2453 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2454 printf("%15s [%04d] ", tstr, sample->cpu);
2455
2456 if (sched->show_cpu_visual) {
2457 u32 i;
2458 char c;
2459
2460 printf(" ");
2461 for (i = 0; i < max_cpus; ++i) {
2462 c = (i == sample->cpu) ? 'm' : ' ';
2463 printf("%c", c);
2464 }
2465 printf(" ");
2466 }
2467
2468 printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2469
2470 /* dt spacer */
2471 printf(" %9s %9s %9s ", "", "", "");
2472
2473 printf("migrated: %s", timehist_get_commstr(migrated));
2474 printf(" cpu %d => %d", ocpu, dcpu);
2475
2476 printf("\n");
2477}
2478
2479static int timehist_migrate_task_event(struct perf_tool *tool,
2480 union perf_event *event __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002481 struct evsel *evsel,
David Ahern350f54f2016-11-25 09:28:41 -07002482 struct perf_sample *sample,
2483 struct machine *machine)
2484{
2485 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2486 struct thread *thread;
2487 struct thread_runtime *tr = NULL;
2488 /* want pid of migrated task not pid in sample */
2489 const u32 pid = perf_evsel__intval(evsel, sample, "pid");
2490
2491 thread = machine__findnew_thread(machine, 0, pid);
2492 if (thread == NULL)
2493 return -1;
2494
2495 tr = thread__get_runtime(thread);
2496 if (tr == NULL)
2497 return -1;
2498
2499 tr->migrations++;
2500
2501 /* show migrations if requested */
2502 timehist_print_migration_event(sched, evsel, sample, machine, thread);
2503
2504 return 0;
2505}
2506
David Ahern52df1382016-11-16 15:06:30 +09002507static int timehist_sched_change_event(struct perf_tool *tool,
David Ahern49394a22016-11-16 15:06:29 +09002508 union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002509 struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09002510 struct perf_sample *sample,
2511 struct machine *machine)
2512{
David Ahernfc1469f2016-11-16 15:06:31 +09002513 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
David Ahern853b7402016-11-29 10:15:44 -07002514 struct perf_time_interval *ptime = &sched->ptime;
David Ahern49394a22016-11-16 15:06:29 +09002515 struct addr_location al;
2516 struct thread *thread;
2517 struct thread_runtime *tr = NULL;
David Ahern853b7402016-11-29 10:15:44 -07002518 u64 tprev, t = sample->time;
David Ahern49394a22016-11-16 15:06:29 +09002519 int rc = 0;
Namhyung Kim414e0502017-01-13 19:45:22 +09002520 int state = perf_evsel__intval(evsel, sample, "prev_state");
2521
David Ahern49394a22016-11-16 15:06:29 +09002522
2523 if (machine__resolve(machine, &al, sample) < 0) {
2524 pr_err("problem processing %d event. skipping it\n",
2525 event->header.type);
2526 rc = -1;
2527 goto out;
2528 }
2529
David Ahern6c973c92016-11-16 15:06:32 +09002530 thread = timehist_get_thread(sched, sample, machine, evsel);
David Ahern49394a22016-11-16 15:06:29 +09002531 if (thread == NULL) {
2532 rc = -1;
2533 goto out;
2534 }
2535
Namhyung Kima4b2b6f2016-12-08 23:47:53 +09002536 if (timehist_skip_sample(sched, thread, evsel, sample))
David Ahern49394a22016-11-16 15:06:29 +09002537 goto out;
2538
2539 tr = thread__get_runtime(thread);
2540 if (tr == NULL) {
2541 rc = -1;
2542 goto out;
2543 }
2544
2545 tprev = perf_evsel__get_time(evsel, sample->cpu);
2546
David Ahern853b7402016-11-29 10:15:44 -07002547 /*
2548 * If start time given:
2549 * - sample time is under window user cares about - skip sample
2550 * - tprev is under window user cares about - reset to start of window
2551 */
2552 if (ptime->start && ptime->start > t)
2553 goto out;
2554
Namhyung Kimbdd75722016-12-22 15:03:49 +09002555 if (tprev && ptime->start > tprev)
David Ahern853b7402016-11-29 10:15:44 -07002556 tprev = ptime->start;
2557
2558 /*
2559 * If end time given:
2560 * - previous sched event is out of window - we are done
2561 * - sample time is beyond window user cares about - reset it
2562 * to close out stats for time window interest
2563 */
2564 if (ptime->end) {
2565 if (tprev > ptime->end)
2566 goto out;
2567
2568 if (t > ptime->end)
2569 t = ptime->end;
2570 }
2571
Namhyung Kim07235f82016-12-08 23:47:54 +09002572 if (!sched->idle_hist || thread->tid == 0) {
2573 timehist_update_runtime_stats(tr, t, tprev);
2574
2575 if (sched->idle_hist) {
2576 struct idle_thread_runtime *itr = (void *)tr;
2577 struct thread_runtime *last_tr;
2578
2579 BUG_ON(thread->tid != 0);
2580
2581 if (itr->last_thread == NULL)
2582 goto out;
2583
2584 /* add current idle time as last thread's runtime */
2585 last_tr = thread__get_runtime(itr->last_thread);
2586 if (last_tr == NULL)
2587 goto out;
2588
2589 timehist_update_runtime_stats(last_tr, t, tprev);
2590 /*
2591 * remove delta time of last thread as it's not updated
2592 * and otherwise it will show an invalid value next
2593 * time. we only care total run time and run stat.
2594 */
2595 last_tr->dt_run = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002596 last_tr->dt_delay = 0;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002597 last_tr->dt_sleep = 0;
2598 last_tr->dt_iowait = 0;
2599 last_tr->dt_preempt = 0;
Namhyung Kim07235f82016-12-08 23:47:54 +09002600
Namhyung Kimba957eb2016-12-08 23:47:55 +09002601 if (itr->cursor.nr)
2602 callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2603
Namhyung Kim07235f82016-12-08 23:47:54 +09002604 itr->last_thread = NULL;
2605 }
2606 }
David Ahern853b7402016-11-29 10:15:44 -07002607
David Ahern52df1382016-11-16 15:06:30 +09002608 if (!sched->summary_only)
Brendan Gregg292c4a82017-03-14 01:56:29 +00002609 timehist_print_sample(sched, evsel, sample, &al, thread, t, state);
David Ahern49394a22016-11-16 15:06:29 +09002610
2611out:
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002612 if (sched->hist_time.start == 0 && t >= ptime->start)
2613 sched->hist_time.start = t;
2614 if (ptime->end == 0 || t <= ptime->end)
2615 sched->hist_time.end = t;
2616
David Ahern49394a22016-11-16 15:06:29 +09002617 if (tr) {
2618 /* time of this sched_switch event becomes last time task seen */
2619 tr->last_time = sample->time;
2620
Namhyung Kim941bdea2017-01-13 19:45:21 +09002621 /* last state is used to determine where to account wait time */
Namhyung Kim414e0502017-01-13 19:45:22 +09002622 tr->last_state = state;
Namhyung Kim941bdea2017-01-13 19:45:21 +09002623
David Ahern49394a22016-11-16 15:06:29 +09002624 /* sched out event for task so reset ready to run time */
2625 tr->ready_to_run = 0;
2626 }
2627
2628 perf_evsel__save_time(evsel, sample->time, sample->cpu);
2629
2630 return rc;
2631}
2632
2633static int timehist_sched_switch_event(struct perf_tool *tool,
2634 union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002635 struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09002636 struct perf_sample *sample,
2637 struct machine *machine __maybe_unused)
2638{
2639 return timehist_sched_change_event(tool, event, evsel, sample, machine);
2640}
2641
2642static int process_lost(struct perf_tool *tool __maybe_unused,
2643 union perf_event *event,
2644 struct perf_sample *sample,
2645 struct machine *machine __maybe_unused)
2646{
2647 char tstr[64];
2648
2649 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2650 printf("%15s ", tstr);
Jiri Olsa5290ed62019-08-25 20:17:46 +02002651 printf("lost %" PRI_lu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
David Ahern49394a22016-11-16 15:06:29 +09002652
2653 return 0;
2654}
2655
2656
David Ahern52df1382016-11-16 15:06:30 +09002657static void print_thread_runtime(struct thread *t,
2658 struct thread_runtime *r)
2659{
2660 double mean = avg_stats(&r->run_stats);
2661 float stddev;
2662
2663 printf("%*s %5d %9" PRIu64 " ",
2664 comm_width, timehist_get_commstr(t), t->ppid,
2665 (u64) r->run_stats.n);
2666
2667 print_sched_time(r->total_run_time, 8);
2668 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
2669 print_sched_time(r->run_stats.min, 6);
2670 printf(" ");
2671 print_sched_time((u64) mean, 6);
2672 printf(" ");
2673 print_sched_time(r->run_stats.max, 6);
2674 printf(" ");
2675 printf("%5.2f", stddev);
David Ahern350f54f2016-11-25 09:28:41 -07002676 printf(" %5" PRIu64, r->migrations);
David Ahern52df1382016-11-16 15:06:30 +09002677 printf("\n");
2678}
2679
Namhyung Kim587782c2017-01-13 19:45:23 +09002680static void print_thread_waittime(struct thread *t,
2681 struct thread_runtime *r)
2682{
2683 printf("%*s %5d %9" PRIu64 " ",
2684 comm_width, timehist_get_commstr(t), t->ppid,
2685 (u64) r->run_stats.n);
2686
2687 print_sched_time(r->total_run_time, 8);
2688 print_sched_time(r->total_sleep_time, 6);
2689 printf(" ");
2690 print_sched_time(r->total_iowait_time, 6);
2691 printf(" ");
2692 print_sched_time(r->total_preempt_time, 6);
2693 printf(" ");
2694 print_sched_time(r->total_delay_time, 6);
2695 printf("\n");
2696}
2697
David Ahern52df1382016-11-16 15:06:30 +09002698struct total_run_stats {
Namhyung Kim587782c2017-01-13 19:45:23 +09002699 struct perf_sched *sched;
David Ahern52df1382016-11-16 15:06:30 +09002700 u64 sched_count;
2701 u64 task_count;
2702 u64 total_run_time;
2703};
2704
2705static int __show_thread_runtime(struct thread *t, void *priv)
2706{
2707 struct total_run_stats *stats = priv;
2708 struct thread_runtime *r;
2709
2710 if (thread__is_filtered(t))
2711 return 0;
2712
2713 r = thread__priv(t);
2714 if (r && r->run_stats.n) {
2715 stats->task_count++;
2716 stats->sched_count += r->run_stats.n;
2717 stats->total_run_time += r->total_run_time;
Namhyung Kim587782c2017-01-13 19:45:23 +09002718
2719 if (stats->sched->show_state)
2720 print_thread_waittime(t, r);
2721 else
2722 print_thread_runtime(t, r);
David Ahern52df1382016-11-16 15:06:30 +09002723 }
2724
2725 return 0;
2726}
2727
2728static int show_thread_runtime(struct thread *t, void *priv)
2729{
2730 if (t->dead)
2731 return 0;
2732
2733 return __show_thread_runtime(t, priv);
2734}
2735
2736static int show_deadthread_runtime(struct thread *t, void *priv)
2737{
2738 if (!t->dead)
2739 return 0;
2740
2741 return __show_thread_runtime(t, priv);
2742}
2743
Namhyung Kimba957eb2016-12-08 23:47:55 +09002744static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2745{
2746 const char *sep = " <- ";
2747 struct callchain_list *chain;
2748 size_t ret = 0;
2749 char bf[1024];
2750 bool first;
2751
2752 if (node == NULL)
2753 return 0;
2754
2755 ret = callchain__fprintf_folded(fp, node->parent);
2756 first = (ret == 0);
2757
2758 list_for_each_entry(chain, &node->val, list) {
2759 if (chain->ip >= PERF_CONTEXT_MAX)
2760 continue;
2761 if (chain->ms.sym && chain->ms.sym->ignore)
2762 continue;
2763 ret += fprintf(fp, "%s%s", first ? "" : sep,
2764 callchain_list__sym_name(chain, bf, sizeof(bf),
2765 false));
2766 first = false;
2767 }
2768
2769 return ret;
2770}
2771
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08002772static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)
Namhyung Kimba957eb2016-12-08 23:47:55 +09002773{
2774 size_t ret = 0;
2775 FILE *fp = stdout;
2776 struct callchain_node *chain;
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08002777 struct rb_node *rb_node = rb_first_cached(root);
Namhyung Kimba957eb2016-12-08 23:47:55 +09002778
2779 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
2780 printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
2781 graph_dotted_line);
2782
2783 while (rb_node) {
2784 chain = rb_entry(rb_node, struct callchain_node, rb_node);
2785 rb_node = rb_next(rb_node);
2786
2787 ret += fprintf(fp, " ");
2788 print_sched_time(chain->hit, 12);
2789 ret += 16; /* print_sched_time returns 2nd arg + 4 */
2790 ret += fprintf(fp, " %8d ", chain->count);
2791 ret += callchain__fprintf_folded(fp, chain);
2792 ret += fprintf(fp, "\n");
2793 }
2794
2795 return ret;
2796}
2797
David Ahern52df1382016-11-16 15:06:30 +09002798static void timehist_print_summary(struct perf_sched *sched,
2799 struct perf_session *session)
2800{
2801 struct machine *m = &session->machines.host;
2802 struct total_run_stats totals;
2803 u64 task_count;
2804 struct thread *t;
2805 struct thread_runtime *r;
2806 int i;
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002807 u64 hist_time = sched->hist_time.end - sched->hist_time.start;
David Ahern52df1382016-11-16 15:06:30 +09002808
2809 memset(&totals, 0, sizeof(totals));
Namhyung Kim587782c2017-01-13 19:45:23 +09002810 totals.sched = sched;
David Ahern52df1382016-11-16 15:06:30 +09002811
Namhyung Kim07235f82016-12-08 23:47:54 +09002812 if (sched->idle_hist) {
2813 printf("\nIdle-time summary\n");
2814 printf("%*s parent sched-out ", comm_width, "comm");
2815 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
Namhyung Kim587782c2017-01-13 19:45:23 +09002816 } else if (sched->show_state) {
2817 printf("\nWait-time summary\n");
2818 printf("%*s parent sched-in ", comm_width, "comm");
2819 printf(" run-time sleep iowait preempt delay\n");
Namhyung Kim07235f82016-12-08 23:47:54 +09002820 } else {
2821 printf("\nRuntime summary\n");
2822 printf("%*s parent sched-in ", comm_width, "comm");
2823 printf(" run-time min-run avg-run max-run stddev migrations\n");
2824 }
David Ahern52df1382016-11-16 15:06:30 +09002825 printf("%*s (count) ", comm_width, "");
Namhyung Kim587782c2017-01-13 19:45:23 +09002826 printf(" (msec) (msec) (msec) (msec) %s\n",
2827 sched->show_state ? "(msec)" : "%");
David Ahern350f54f2016-11-25 09:28:41 -07002828 printf("%.117s\n", graph_dotted_line);
David Ahern52df1382016-11-16 15:06:30 +09002829
2830 machine__for_each_thread(m, show_thread_runtime, &totals);
2831 task_count = totals.task_count;
2832 if (!task_count)
2833 printf("<no still running tasks>\n");
2834
2835 printf("\nTerminated tasks:\n");
2836 machine__for_each_thread(m, show_deadthread_runtime, &totals);
2837 if (task_count == totals.task_count)
2838 printf("<no terminated tasks>\n");
2839
2840 /* CPU idle stats not tracked when samples were skipped */
Namhyung Kim07235f82016-12-08 23:47:54 +09002841 if (sched->skipped_samples && !sched->idle_hist)
David Ahern52df1382016-11-16 15:06:30 +09002842 return;
2843
2844 printf("\nIdle stats:\n");
Namhyung Kimb3363522016-12-06 12:40:05 +09002845 for (i = 0; i < idle_max_cpu; ++i) {
David Ahern52df1382016-11-16 15:06:30 +09002846 t = idle_threads[i];
2847 if (!t)
2848 continue;
2849
2850 r = thread__priv(t);
2851 if (r && r->run_stats.n) {
2852 totals.sched_count += r->run_stats.n;
2853 printf(" CPU %2d idle for ", i);
2854 print_sched_time(r->total_run_time, 6);
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002855 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
David Ahern52df1382016-11-16 15:06:30 +09002856 } else
2857 printf(" CPU %2d idle entire time window\n", i);
2858 }
2859
Arnaldo Carvalho de Melo4c505632018-05-28 16:07:56 -03002860 if (sched->idle_hist && sched->show_callchain) {
Namhyung Kimba957eb2016-12-08 23:47:55 +09002861 callchain_param.mode = CHAIN_FOLDED;
2862 callchain_param.value = CCVAL_PERIOD;
2863
2864 callchain_register_param(&callchain_param);
2865
2866 printf("\nIdle stats by callchain:\n");
2867 for (i = 0; i < idle_max_cpu; ++i) {
2868 struct idle_thread_runtime *itr;
2869
2870 t = idle_threads[i];
2871 if (!t)
2872 continue;
2873
2874 itr = thread__priv(t);
2875 if (itr == NULL)
2876 continue;
2877
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08002878 callchain_param.sort(&itr->sorted_root.rb_root, &itr->callchain,
Namhyung Kimba957eb2016-12-08 23:47:55 +09002879 0, &callchain_param);
2880
2881 printf(" CPU %2d:", i);
2882 print_sched_time(itr->tr.total_run_time, 6);
2883 printf(" msec\n");
2884 timehist_print_idlehist_callchain(&itr->sorted_root);
2885 printf("\n");
2886 }
2887 }
2888
David Ahern52df1382016-11-16 15:06:30 +09002889 printf("\n"
2890 " Total number of unique tasks: %" PRIu64 "\n"
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002891 "Total number of context switches: %" PRIu64 "\n",
David Ahern52df1382016-11-16 15:06:30 +09002892 totals.task_count, totals.sched_count);
2893
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002894 printf(" Total run time (msec): ");
David Ahern52df1382016-11-16 15:06:30 +09002895 print_sched_time(totals.total_run_time, 2);
2896 printf("\n");
Namhyung Kim9396c9c2016-12-22 15:03:50 +09002897
2898 printf(" Total scheduling time (msec): ");
2899 print_sched_time(hist_time, 2);
2900 printf(" (x %d)\n", sched->max_cpu);
David Ahern52df1382016-11-16 15:06:30 +09002901}
2902
David Ahern49394a22016-11-16 15:06:29 +09002903typedef int (*sched_handler)(struct perf_tool *tool,
2904 union perf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002905 struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09002906 struct perf_sample *sample,
2907 struct machine *machine);
2908
2909static int perf_timehist__process_sample(struct perf_tool *tool,
2910 union perf_event *event,
2911 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002912 struct evsel *evsel,
David Ahern49394a22016-11-16 15:06:29 +09002913 struct machine *machine)
2914{
2915 struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2916 int err = 0;
2917 int this_cpu = sample->cpu;
2918
2919 if (this_cpu > sched->max_cpu)
2920 sched->max_cpu = this_cpu;
2921
2922 if (evsel->handler != NULL) {
2923 sched_handler f = evsel->handler;
2924
2925 err = f(tool, event, evsel, sample, machine);
2926 }
2927
2928 return err;
2929}
2930
David Ahern6c973c92016-11-16 15:06:32 +09002931static int timehist_check_attr(struct perf_sched *sched,
Jiri Olsa63503db2019-07-21 13:23:52 +02002932 struct evlist *evlist)
David Ahern6c973c92016-11-16 15:06:32 +09002933{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002934 struct evsel *evsel;
David Ahern6c973c92016-11-16 15:06:32 +09002935 struct evsel_runtime *er;
2936
Jiri Olsace9036a2019-07-21 13:24:23 +02002937 list_for_each_entry(evsel, &evlist->core.entries, core.node) {
David Ahern6c973c92016-11-16 15:06:32 +09002938 er = perf_evsel__get_runtime(evsel);
2939 if (er == NULL) {
2940 pr_err("Failed to allocate memory for evsel runtime data\n");
2941 return -1;
2942 }
2943
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002944 if (sched->show_callchain && !evsel__has_callchain(evsel)) {
David Ahern6c973c92016-11-16 15:06:32 +09002945 pr_info("Samples do not have callchains.\n");
2946 sched->show_callchain = 0;
2947 symbol_conf.use_callchain = 0;
2948 }
2949 }
2950
2951 return 0;
2952}
2953
David Ahern49394a22016-11-16 15:06:29 +09002954static int perf_sched__timehist(struct perf_sched *sched)
2955{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002956 const struct evsel_str_handler handlers[] = {
David Ahern49394a22016-11-16 15:06:29 +09002957 { "sched:sched_switch", timehist_sched_switch_event, },
2958 { "sched:sched_wakeup", timehist_sched_wakeup_event, },
2959 { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
2960 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02002961 const struct evsel_str_handler migrate_handlers[] = {
David Ahern350f54f2016-11-25 09:28:41 -07002962 { "sched:sched_migrate_task", timehist_migrate_task_event, },
2963 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002964 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01002965 .path = input_name,
2966 .mode = PERF_DATA_MODE_READ,
2967 .force = sched->force,
David Ahern49394a22016-11-16 15:06:29 +09002968 };
2969
2970 struct perf_session *session;
Jiri Olsa63503db2019-07-21 13:23:52 +02002971 struct evlist *evlist;
David Ahern49394a22016-11-16 15:06:29 +09002972 int err = -1;
2973
2974 /*
2975 * event handlers for timehist option
2976 */
2977 sched->tool.sample = perf_timehist__process_sample;
2978 sched->tool.mmap = perf_event__process_mmap;
2979 sched->tool.comm = perf_event__process_comm;
2980 sched->tool.exit = perf_event__process_exit;
2981 sched->tool.fork = perf_event__process_fork;
2982 sched->tool.lost = process_lost;
2983 sched->tool.attr = perf_event__process_attr;
2984 sched->tool.tracing_data = perf_event__process_tracing_data;
2985 sched->tool.build_id = perf_event__process_build_id;
2986
2987 sched->tool.ordered_events = true;
2988 sched->tool.ordering_requires_timestamps = true;
2989
David Ahern6c973c92016-11-16 15:06:32 +09002990 symbol_conf.use_callchain = sched->show_callchain;
2991
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002992 session = perf_session__new(&data, false, &sched->tool);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +05302993 if (IS_ERR(session))
2994 return PTR_ERR(session);
David Ahern49394a22016-11-16 15:06:29 +09002995
David Ahern52df1382016-11-16 15:06:30 +09002996 evlist = session->evlist;
2997
David Ahern49394a22016-11-16 15:06:29 +09002998 symbol__init(&session->header.env);
2999
David Ahern853b7402016-11-29 10:15:44 -07003000 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
3001 pr_err("Invalid time string\n");
3002 return -EINVAL;
3003 }
3004
David Ahern6c973c92016-11-16 15:06:32 +09003005 if (timehist_check_attr(sched, evlist) != 0)
3006 goto out;
3007
David Ahern49394a22016-11-16 15:06:29 +09003008 setup_pager();
3009
3010 /* setup per-evsel handlers */
3011 if (perf_session__set_tracepoints_handlers(session, handlers))
3012 goto out;
3013
David Ahernf45bf8d2016-11-29 13:39:48 -07003014 /* sched_switch event at a minimum needs to exist */
3015 if (!perf_evlist__find_tracepoint_by_name(session->evlist,
3016 "sched:sched_switch")) {
3017 pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
David Ahern49394a22016-11-16 15:06:29 +09003018 goto out;
David Ahernf45bf8d2016-11-29 13:39:48 -07003019 }
David Ahern49394a22016-11-16 15:06:29 +09003020
David Ahern350f54f2016-11-25 09:28:41 -07003021 if (sched->show_migrations &&
3022 perf_session__set_tracepoints_handlers(session, migrate_handlers))
3023 goto out;
3024
David Ahern49394a22016-11-16 15:06:29 +09003025 /* pre-allocate struct for per-CPU idle stats */
3026 sched->max_cpu = session->header.env.nr_cpus_online;
3027 if (sched->max_cpu == 0)
3028 sched->max_cpu = 4;
3029 if (init_idle_threads(sched->max_cpu))
3030 goto out;
3031
David Ahern52df1382016-11-16 15:06:30 +09003032 /* summary_only implies summary option, but don't overwrite summary if set */
3033 if (sched->summary_only)
3034 sched->summary = sched->summary_only;
3035
3036 if (!sched->summary_only)
David Aherna407b062016-11-16 15:06:33 +09003037 timehist_header(sched);
David Ahern49394a22016-11-16 15:06:29 +09003038
3039 err = perf_session__process_events(session);
3040 if (err) {
3041 pr_err("Failed to process events, error %d", err);
3042 goto out;
3043 }
3044
David Ahern52df1382016-11-16 15:06:30 +09003045 sched->nr_events = evlist->stats.nr_events[0];
3046 sched->nr_lost_events = evlist->stats.total_lost;
3047 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
3048
3049 if (sched->summary)
3050 timehist_print_summary(sched, session);
3051
David Ahern49394a22016-11-16 15:06:29 +09003052out:
3053 free_idle_threads();
3054 perf_session__delete(session);
3055
3056 return err;
3057}
3058
3059
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003060static void print_bad_events(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003061{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003062 if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003063 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003064 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
3065 sched->nr_unordered_timestamps, sched->nr_timestamps);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003066 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003067 if (sched->nr_lost_events && sched->nr_events) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003068 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003069 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
3070 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003071 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003072 if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003073 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003074 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
3075 sched->nr_context_switch_bugs, sched->nr_timestamps);
3076 if (sched->nr_lost_events)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003077 printf(" (due to lost events?)");
3078 printf("\n");
3079 }
3080}
3081
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003082static void __merge_work_atoms(struct rb_root_cached *root, struct work_atoms *data)
Josef Bacik2f80dd42015-05-22 09:18:40 -04003083{
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003084 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
Josef Bacik2f80dd42015-05-22 09:18:40 -04003085 struct work_atoms *this;
3086 const char *comm = thread__comm_str(data->thread), *this_comm;
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003087 bool leftmost = true;
Josef Bacik2f80dd42015-05-22 09:18:40 -04003088
3089 while (*new) {
3090 int cmp;
3091
3092 this = container_of(*new, struct work_atoms, node);
3093 parent = *new;
3094
3095 this_comm = thread__comm_str(this->thread);
3096 cmp = strcmp(comm, this_comm);
3097 if (cmp > 0) {
3098 new = &((*new)->rb_left);
3099 } else if (cmp < 0) {
3100 new = &((*new)->rb_right);
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003101 leftmost = false;
Josef Bacik2f80dd42015-05-22 09:18:40 -04003102 } else {
3103 this->num_merged++;
3104 this->total_runtime += data->total_runtime;
3105 this->nb_atoms += data->nb_atoms;
3106 this->total_lat += data->total_lat;
3107 list_splice(&data->work_list, &this->work_list);
3108 if (this->max_lat < data->max_lat) {
3109 this->max_lat = data->max_lat;
3110 this->max_lat_at = data->max_lat_at;
3111 }
3112 zfree(&data);
3113 return;
3114 }
3115 }
3116
3117 data->num_merged++;
3118 rb_link_node(&data->node, parent, new);
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003119 rb_insert_color_cached(&data->node, root, leftmost);
Josef Bacik2f80dd42015-05-22 09:18:40 -04003120}
3121
3122static void perf_sched__merge_lat(struct perf_sched *sched)
3123{
3124 struct work_atoms *data;
3125 struct rb_node *node;
3126
3127 if (sched->skip_merge)
3128 return;
3129
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003130 while ((node = rb_first_cached(&sched->atom_root))) {
3131 rb_erase_cached(node, &sched->atom_root);
Josef Bacik2f80dd42015-05-22 09:18:40 -04003132 data = rb_entry(node, struct work_atoms, node);
3133 __merge_work_atoms(&sched->merged_atom_root, data);
3134 }
3135}
3136
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003137static int perf_sched__lat(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003138{
3139 struct rb_node *next;
3140
3141 setup_pager();
David Ahernad9def72013-08-07 22:50:44 -04003142
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003143 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003144 return -1;
David Ahernad9def72013-08-07 22:50:44 -04003145
Josef Bacik2f80dd42015-05-22 09:18:40 -04003146 perf_sched__merge_lat(sched);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003147 perf_sched__sort_lat(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003148
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003149 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
3150 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
3151 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003152
Davidlohr Buesocb4c13a2018-12-06 11:18:19 -08003153 next = rb_first_cached(&sched->sorted_atom_root);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003154
3155 while (next) {
3156 struct work_atoms *work_list;
3157
3158 work_list = rb_entry(next, struct work_atoms, node);
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003159 output_lat_thread(sched, work_list);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003160 next = rb_next(next);
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003161 thread__zput(work_list->thread);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003162 }
3163
Ramkumar Ramachandra80790e02014-03-17 10:18:21 -04003164 printf(" -----------------------------------------------------------------------------------------------------------------\n");
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003165 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
Arnaldo Carvalho de Melo4fc76e42016-08-08 12:23:49 -03003166 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003167
3168 printf(" ---------------------------------------------------\n");
3169
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003170 print_bad_events(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003171 printf("\n");
3172
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003173 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003174}
3175
Jiri Olsa99623c62016-04-12 15:29:26 +02003176static int setup_map_cpus(struct perf_sched *sched)
3177{
Jiri Olsaf8548392019-07-21 13:23:49 +02003178 struct perf_cpu_map *map;
Jiri Olsa73643bb2016-04-12 15:29:31 +02003179
Jiri Olsa99623c62016-04-12 15:29:26 +02003180 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
3181
3182 if (sched->map.comp) {
3183 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int));
Jiri Olsacf294f22016-04-12 15:29:30 +02003184 if (!sched->map.comp_cpus)
3185 return -1;
Jiri Olsa99623c62016-04-12 15:29:26 +02003186 }
3187
Jiri Olsa73643bb2016-04-12 15:29:31 +02003188 if (!sched->map.cpus_str)
3189 return 0;
3190
Jiri Olsa9c3516d2019-07-21 13:24:30 +02003191 map = perf_cpu_map__new(sched->map.cpus_str);
Jiri Olsa73643bb2016-04-12 15:29:31 +02003192 if (!map) {
3193 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3194 return -1;
3195 }
3196
3197 sched->map.cpus = map;
Jiri Olsa99623c62016-04-12 15:29:26 +02003198 return 0;
3199}
3200
Jiri Olsaa151a372016-04-12 15:29:29 +02003201static int setup_color_pids(struct perf_sched *sched)
3202{
Jiri Olsa9749b902019-07-21 13:23:50 +02003203 struct perf_thread_map *map;
Jiri Olsaa151a372016-04-12 15:29:29 +02003204
3205 if (!sched->map.color_pids_str)
3206 return 0;
3207
3208 map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3209 if (!map) {
3210 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3211 return -1;
3212 }
3213
3214 sched->map.color_pids = map;
3215 return 0;
3216}
3217
Jiri Olsacf294f22016-04-12 15:29:30 +02003218static int setup_color_cpus(struct perf_sched *sched)
3219{
Jiri Olsaf8548392019-07-21 13:23:49 +02003220 struct perf_cpu_map *map;
Jiri Olsacf294f22016-04-12 15:29:30 +02003221
3222 if (!sched->map.color_cpus_str)
3223 return 0;
3224
Jiri Olsa9c3516d2019-07-21 13:24:30 +02003225 map = perf_cpu_map__new(sched->map.color_cpus_str);
Jiri Olsacf294f22016-04-12 15:29:30 +02003226 if (!map) {
3227 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3228 return -1;
3229 }
3230
3231 sched->map.color_cpus = map;
3232 return 0;
3233}
3234
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003235static int perf_sched__map(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003236{
Jiri Olsa99623c62016-04-12 15:29:26 +02003237 if (setup_map_cpus(sched))
3238 return -1;
Ingo Molnar40749d02009-09-17 18:24:55 +02003239
Jiri Olsaa151a372016-04-12 15:29:29 +02003240 if (setup_color_pids(sched))
3241 return -1;
3242
Jiri Olsacf294f22016-04-12 15:29:30 +02003243 if (setup_color_cpus(sched))
3244 return -1;
3245
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003246 setup_pager();
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003247 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003248 return -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003249 print_bad_events(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003250 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003251}
3252
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003253static int perf_sched__replay(struct perf_sched *sched)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003254{
3255 unsigned long i;
3256
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003257 calibrate_run_measurement_overhead(sched);
3258 calibrate_sleep_measurement_overhead(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003259
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003260 test_calibrations(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003261
Arnaldo Carvalho de Meloae536ac2015-03-02 22:28:41 -03003262 if (perf_sched__read_events(sched))
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003263 return -1;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003264
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003265 printf("nr_run_events: %ld\n", sched->nr_run_events);
3266 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
3267 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003268
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003269 if (sched->targetless_wakeups)
3270 printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
3271 if (sched->multitarget_wakeups)
3272 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
3273 if (sched->nr_run_events_optimized)
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003274 printf("run atoms optimized: %ld\n",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003275 sched->nr_run_events_optimized);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003276
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003277 print_task_traces(sched);
3278 add_cross_task_wakeups(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003279
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003280 create_tasks(sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003281 printf("------------------------------------------------------------\n");
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003282 for (i = 0; i < sched->replay_repeat; i++)
3283 run_one_test(sched);
Arnaldo Carvalho de Meloa116e052012-09-08 22:53:06 -03003284
3285 return 0;
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003286}
3287
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003288static void setup_sorting(struct perf_sched *sched, const struct option *options,
3289 const char * const usage_msg[])
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003290{
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003291 char *tmp, *tok, *str = strdup(sched->sort_order);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003292
3293 for (tok = strtok_r(str, ", ", &tmp);
3294 tok; tok = strtok_r(NULL, ", ", &tmp)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003295 if (sort_dimension__add(tok, &sched->sort_list) < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003296 usage_with_options_msg(usage_msg, options,
3297 "Unknown --sort key: `%s'", tok);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003298 }
3299 }
3300
3301 free(str);
3302
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003303 sort_dimension__add("pid", &sched->cmp_pid);
Frederic Weisbeckerdaa1d7a2009-09-13 03:36:29 +02003304}
3305
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003306static int __cmd_record(int argc, const char **argv)
3307{
3308 unsigned int rec_argc, i, j;
3309 const char **rec_argv;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003310 const char * const record_args[] = {
3311 "record",
3312 "-a",
3313 "-R",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003314 "-m", "1024",
3315 "-c", "1",
3316 "-e", "sched:sched_switch",
3317 "-e", "sched:sched_stat_wait",
3318 "-e", "sched:sched_stat_sleep",
3319 "-e", "sched:sched_stat_iowait",
3320 "-e", "sched:sched_stat_runtime",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003321 "-e", "sched:sched_process_fork",
3322 "-e", "sched:sched_wakeup",
Dongsheng7fff9592014-05-05 16:05:53 +09003323 "-e", "sched:sched_wakeup_new",
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003324 "-e", "sched:sched_migrate_task",
3325 };
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003326
3327 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
3328 rec_argv = calloc(rec_argc + 1, sizeof(char *));
3329
Arnaldo Carvalho de Meloe462dc52011-01-10 10:48:47 -02003330 if (rec_argv == NULL)
Chris Samuelce47dc52010-11-13 13:35:06 +11003331 return -ENOMEM;
3332
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003333 for (i = 0; i < ARRAY_SIZE(record_args); i++)
3334 rec_argv[i] = strdup(record_args[i]);
3335
3336 for (j = 1; j < (unsigned int)argc; j++, i++)
3337 rec_argv[i] = argv[j];
3338
3339 BUG_ON(i != rec_argc);
3340
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003341 return cmd_record(i, rec_argv);
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003342}
3343
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003344int cmd_sched(int argc, const char **argv)
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003345{
Rasmus Villemoes49b8e2b2018-11-03 00:06:23 +01003346 static const char default_sort_order[] = "avg, max, switch, runtime";
Adrian Hunter8a39df82013-10-22 10:34:15 +03003347 struct perf_sched sched = {
3348 .tool = {
3349 .sample = perf_sched__process_tracepoint_sample,
Changbin Du99a3c3a2018-03-06 11:37:37 +08003350 .comm = perf_sched__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303351 .namespaces = perf_event__process_namespaces,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003352 .lost = perf_event__process_lost,
3353 .fork = perf_sched__process_fork_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003354 .ordered_events = true,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003355 },
3356 .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
3357 .sort_list = LIST_HEAD_INIT(sched.sort_list),
3358 .start_work_mutex = PTHREAD_MUTEX_INITIALIZER,
3359 .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003360 .sort_order = default_sort_order,
3361 .replay_repeat = 10,
3362 .profile_cpu = -1,
3363 .next_shortname1 = 'A',
3364 .next_shortname2 = '0',
Josef Bacik2f80dd42015-05-22 09:18:40 -04003365 .skip_merge = 0,
David Ahern6c973c92016-11-16 15:06:32 +09003366 .show_callchain = 1,
3367 .max_stack = 5,
Adrian Hunter8a39df82013-10-22 10:34:15 +03003368 };
Namhyung Kim77f02f42016-10-24 12:00:03 +09003369 const struct option sched_options[] = {
3370 OPT_STRING('i', "input", &input_name, "file",
3371 "input file name"),
3372 OPT_INCR('v', "verbose", &verbose,
3373 "be more verbose (show symbol address, etc)"),
3374 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3375 "dump raw trace in ASCII"),
Namhyung Kim6fa94252016-12-06 12:40:01 +09003376 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003377 OPT_END()
3378 };
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003379 const struct option latency_options[] = {
3380 OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
3381 "sort by key(s): runtime, switch, avg, max"),
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003382 OPT_INTEGER('C', "CPU", &sched.profile_cpu,
3383 "CPU to profile on"),
Josef Bacik2f80dd42015-05-22 09:18:40 -04003384 OPT_BOOLEAN('p', "pids", &sched.skip_merge,
3385 "latency stats per pid instead of per comm"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003386 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003387 };
3388 const struct option replay_options[] = {
3389 OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
3390 "repeat the workload replay N times (-1: infinite)"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003391 OPT_PARENT(sched_options)
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003392 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003393 const struct option map_options[] = {
3394 OPT_BOOLEAN(0, "compact", &sched.map.comp,
3395 "map output in compact mode"),
Jiri Olsaa151a372016-04-12 15:29:29 +02003396 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3397 "highlight given pids in map"),
Jiri Olsacf294f22016-04-12 15:29:30 +02003398 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3399 "highlight given CPUs in map"),
Jiri Olsa73643bb2016-04-12 15:29:31 +02003400 OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
3401 "display given CPUs in map"),
Namhyung Kim77f02f42016-10-24 12:00:03 +09003402 OPT_PARENT(sched_options)
Jiri Olsa99623c62016-04-12 15:29:26 +02003403 };
David Ahern49394a22016-11-16 15:06:29 +09003404 const struct option timehist_options[] = {
3405 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3406 "file", "vmlinux pathname"),
3407 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3408 "file", "kallsyms pathname"),
David Ahern6c973c92016-11-16 15:06:32 +09003409 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
3410 "Display call chains if present (default on)"),
3411 OPT_UINTEGER(0, "max-stack", &sched.max_stack,
3412 "Maximum number of functions to display backtrace."),
David Ahern49394a22016-11-16 15:06:29 +09003413 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
3414 "Look for files with symbols relative to this directory"),
David Ahern52df1382016-11-16 15:06:30 +09003415 OPT_BOOLEAN('s', "summary", &sched.summary_only,
3416 "Show only syscall summary with statistics"),
3417 OPT_BOOLEAN('S', "with-summary", &sched.summary,
3418 "Show all syscalls and summary with statistics"),
David Ahernfc1469f2016-11-16 15:06:31 +09003419 OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
Brendan Gregg292c4a82017-03-14 01:56:29 +00003420 OPT_BOOLEAN('n', "next", &sched.show_next, "Show next task"),
David Ahern350f54f2016-11-25 09:28:41 -07003421 OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
David Aherna407b062016-11-16 15:06:33 +09003422 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
Namhyung Kim07235f82016-12-08 23:47:54 +09003423 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
David Ahern853b7402016-11-29 10:15:44 -07003424 OPT_STRING(0, "time", &sched.time_str, "str",
3425 "Time span for analysis (start,stop)"),
Namhyung Kim414e0502017-01-13 19:45:22 +09003426 OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"),
David Ahern0f59d7a2017-09-01 10:49:12 -07003427 OPT_STRING('p', "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3428 "analyze events only for given process id(s)"),
3429 OPT_STRING('t', "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3430 "analyze events only for given thread id(s)"),
David Ahern49394a22016-11-16 15:06:29 +09003431 OPT_PARENT(sched_options)
3432 };
3433
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003434 const char * const latency_usage[] = {
3435 "perf sched latency [<options>]",
3436 NULL
3437 };
3438 const char * const replay_usage[] = {
3439 "perf sched replay [<options>]",
3440 NULL
3441 };
Jiri Olsa99623c62016-04-12 15:29:26 +02003442 const char * const map_usage[] = {
3443 "perf sched map [<options>]",
3444 NULL
3445 };
David Ahern49394a22016-11-16 15:06:29 +09003446 const char * const timehist_usage[] = {
3447 "perf sched timehist [<options>]",
3448 NULL
3449 };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003450 const char *const sched_subcommands[] = { "record", "latency", "map",
David Ahern49394a22016-11-16 15:06:29 +09003451 "replay", "script",
3452 "timehist", NULL };
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003453 const char *sched_usage[] = {
3454 NULL,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003455 NULL
3456 };
3457 struct trace_sched_handler lat_ops = {
3458 .wakeup_event = latency_wakeup_event,
3459 .switch_event = latency_switch_event,
3460 .runtime_event = latency_runtime_event,
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003461 .migrate_task_event = latency_migrate_task_event,
3462 };
3463 struct trace_sched_handler map_ops = {
3464 .switch_event = map_switch_event,
3465 };
3466 struct trace_sched_handler replay_ops = {
3467 .wakeup_event = replay_wakeup_event,
3468 .switch_event = replay_switch_event,
3469 .fork_event = replay_fork_event,
3470 };
Adrian Hunter156a2b02013-10-22 10:34:16 +03003471 unsigned int i;
3472
3473 for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3474 sched.curr_pid[i] = -1;
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003475
Ramkumar Ramachandraa83edb22014-03-14 23:17:54 -04003476 argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3477 sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003478 if (!argc)
3479 usage_with_options(sched_usage, sched_options);
3480
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003481 /*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003482 * Aliased to 'perf script' for now:
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003483 */
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003484 if (!strcmp(argv[0], "script"))
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003485 return cmd_script(argc, argv);
Xiao Guangrongc0777c52009-12-07 12:04:49 +08003486
Ingo Molnar1fc35b22009-09-13 09:44:29 +02003487 if (!strncmp(argv[0], "rec", 3)) {
3488 return __cmd_record(argc, argv);
3489 } else if (!strncmp(argv[0], "lat", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003490 sched.tp_handler = &lat_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003491 if (argc > 1) {
3492 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3493 if (argc)
3494 usage_with_options(latency_usage, latency_options);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003495 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003496 setup_sorting(&sched, latency_options, latency_usage);
3497 return perf_sched__lat(&sched);
Ingo Molnar0ec04e12009-09-16 17:40:48 +02003498 } else if (!strcmp(argv[0], "map")) {
Jiri Olsa99623c62016-04-12 15:29:26 +02003499 if (argc) {
Jiri Olsaa151a372016-04-12 15:29:29 +02003500 argc = parse_options(argc, argv, map_options, map_usage, 0);
Jiri Olsa99623c62016-04-12 15:29:26 +02003501 if (argc)
3502 usage_with_options(map_usage, map_options);
3503 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003504 sched.tp_handler = &map_ops;
3505 setup_sorting(&sched, latency_options, latency_usage);
3506 return perf_sched__map(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003507 } else if (!strncmp(argv[0], "rep", 3)) {
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003508 sched.tp_handler = &replay_ops;
Ingo Molnarf2858d82009-09-11 12:12:54 +02003509 if (argc) {
3510 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3511 if (argc)
3512 usage_with_options(replay_usage, replay_options);
3513 }
Arnaldo Carvalho de Melo0e9b07e2012-09-11 17:29:27 -03003514 return perf_sched__replay(&sched);
David Ahern49394a22016-11-16 15:06:29 +09003515 } else if (!strcmp(argv[0], "timehist")) {
3516 if (argc) {
3517 argc = parse_options(argc, argv, timehist_options,
3518 timehist_usage, 0);
3519 if (argc)
3520 usage_with_options(timehist_usage, timehist_options);
3521 }
Brendan Gregg292c4a82017-03-14 01:56:29 +00003522 if ((sched.show_wakeups || sched.show_next) &&
3523 sched.summary_only) {
3524 pr_err(" Error: -s and -[n|w] are mutually exclusive.\n");
David Ahernfc1469f2016-11-16 15:06:31 +09003525 parse_options_usage(timehist_usage, timehist_options, "s", true);
Brendan Gregg292c4a82017-03-14 01:56:29 +00003526 if (sched.show_wakeups)
3527 parse_options_usage(NULL, timehist_options, "w", true);
3528 if (sched.show_next)
3529 parse_options_usage(NULL, timehist_options, "n", true);
David Ahernfc1469f2016-11-16 15:06:31 +09003530 return -EINVAL;
3531 }
3532
David Ahern49394a22016-11-16 15:06:29 +09003533 return perf_sched__timehist(&sched);
Ingo Molnarf2858d82009-09-11 12:12:54 +02003534 } else {
3535 usage_with_options(sched_usage, sched_options);
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003536 }
3537
Ingo Molnarec156762009-09-11 12:12:54 +02003538 return 0;
Ingo Molnar0a02ad92009-09-11 12:12:54 +02003539}