xref: /openbmc/linux/tools/perf/builtin-sched.c (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20a02ad93SIngo Molnar #include "builtin.h"
391854f9aSArnaldo Carvalho de Melo #include "perf-sys.h"
40a02ad93SIngo Molnar 
587ffb6c6SArnaldo Carvalho de Melo #include "util/cpumap.h"
6ee29be62SArnaldo Carvalho de Melo #include "util/evlist.h"
7e3f42609SArnaldo Carvalho de Melo #include "util/evsel.h"
8ca125277SArnaldo Carvalho de Melo #include "util/evsel_fprintf.h"
90bd14ac2SIan Rogers #include "util/mutex.h"
100a02ad93SIngo Molnar #include "util/symbol.h"
110a02ad93SIngo Molnar #include "util/thread.h"
120a02ad93SIngo Molnar #include "util/header.h"
1394c744b6SArnaldo Carvalho de Melo #include "util/session.h"
1445694aa7SArnaldo Carvalho de Melo #include "util/tool.h"
1557480d2cSYann Droneaud #include "util/cloexec.h"
16a151a37aSJiri Olsa #include "util/thread_map.h"
178cd91195SJiri Olsa #include "util/color.h"
1849394a2aSDavid Ahern #include "util/stat.h"
196a9fa4e3SArnaldo Carvalho de Melo #include "util/string2.h"
206c973c90SDavid Ahern #include "util/callchain.h"
21853b7407SDavid Ahern #include "util/time-utils.h"
220a02ad93SIngo Molnar 
23fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
244b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
25b1ffe8f3SIngo Molnar #include "util/trace-event.h"
260a02ad93SIngo Molnar 
270a02ad93SIngo Molnar #include "util/debug.h"
28f12be047SArnaldo Carvalho de Melo #include "util/event.h"
29f12ad272SIan Rogers #include "util/util.h"
300a02ad93SIngo Molnar 
31877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
3249394a2aSDavid Ahern #include <linux/log2.h>
337f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
34b1ffe8f3SIngo Molnar #include <sys/prctl.h>
357b78f136SMarkus Trippelsdorf #include <sys/resource.h>
36fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
370a02ad93SIngo Molnar 
38a43783aeSArnaldo Carvalho de Melo #include <errno.h>
39b1ffe8f3SIngo Molnar #include <semaphore.h>
40b1ffe8f3SIngo Molnar #include <pthread.h>
41b1ffe8f3SIngo Molnar #include <math.h>
42cb06ac25SYunlong Song #include <api/fs/fs.h>
4387ffb6c6SArnaldo Carvalho de Melo #include <perf/cpumap.h>
444fc76e49SArnaldo Carvalho de Melo #include <linux/time64.h>
456ef81c55SMamatha Inamdar #include <linux/err.h>
46419ab0d6SFrederic Weisbecker 
473052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h>
483d689ed6SArnaldo Carvalho de Melo 
49ec156764SIngo Molnar #define PR_SET_NAME		15               /* Set process name */
50b1ffe8f3SIngo Molnar #define MAX_CPUS		4096
51ec156764SIngo Molnar #define COMM_LEN		20
52ec156764SIngo Molnar #define SYM_LEN			129
53a35e27d0SYunlong Song #define MAX_PID			1024000
54ec156764SIngo Molnar 
55c30d630dSDavid Ahern static const char *cpu_list;
56c30d630dSDavid Ahern static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
57c30d630dSDavid Ahern 
5839aeb52fSmingo struct sched_atom;
59ec156764SIngo Molnar 
60ec156764SIngo Molnar struct task_desc {
61ec156764SIngo Molnar 	unsigned long		nr;
62ec156764SIngo Molnar 	unsigned long		pid;
63ec156764SIngo Molnar 	char			comm[COMM_LEN];
64ec156764SIngo Molnar 
65ec156764SIngo Molnar 	unsigned long		nr_events;
66ec156764SIngo Molnar 	unsigned long		curr_event;
6739aeb52fSmingo 	struct sched_atom	**atoms;
68ec156764SIngo Molnar 
69ec156764SIngo Molnar 	pthread_t		thread;
70ec156764SIngo Molnar 	sem_t			sleep_sem;
71ec156764SIngo Molnar 
72ec156764SIngo Molnar 	sem_t			ready_for_work;
73ec156764SIngo Molnar 	sem_t			work_done_sem;
74ec156764SIngo Molnar 
75b1ffe8f3SIngo Molnar 	u64			cpu_usage;
76ec156764SIngo Molnar };
77ec156764SIngo Molnar 
78ec156764SIngo Molnar enum sched_event_type {
79ec156764SIngo Molnar 	SCHED_EVENT_RUN,
80ec156764SIngo Molnar 	SCHED_EVENT_SLEEP,
81ec156764SIngo Molnar 	SCHED_EVENT_WAKEUP,
8255ffb7a6SMike Galbraith 	SCHED_EVENT_MIGRATION,
83ec156764SIngo Molnar };
84ec156764SIngo Molnar 
8539aeb52fSmingo struct sched_atom {
86ec156764SIngo Molnar 	enum sched_event_type	type;
87eed05fe7SArnaldo Carvalho de Melo 	int			specific_wait;
88b1ffe8f3SIngo Molnar 	u64			timestamp;
89b1ffe8f3SIngo Molnar 	u64			duration;
90ec156764SIngo Molnar 	unsigned long		nr;
91ec156764SIngo Molnar 	sem_t			*wait_sem;
92ec156764SIngo Molnar 	struct task_desc	*wakee;
93ec156764SIngo Molnar };
94ec156764SIngo Molnar 
95e936e8e4SDongsheng #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
96b1ffe8f3SIngo Molnar 
97941bdea7SNamhyung Kim /* task state bitmask, copied from include/linux/sched.h */
98941bdea7SNamhyung Kim #define TASK_RUNNING		0
99941bdea7SNamhyung Kim #define TASK_INTERRUPTIBLE	1
100941bdea7SNamhyung Kim #define TASK_UNINTERRUPTIBLE	2
101941bdea7SNamhyung Kim #define __TASK_STOPPED		4
102941bdea7SNamhyung Kim #define __TASK_TRACED		8
103941bdea7SNamhyung Kim /* in tsk->exit_state */
104941bdea7SNamhyung Kim #define EXIT_DEAD		16
105941bdea7SNamhyung Kim #define EXIT_ZOMBIE		32
106941bdea7SNamhyung Kim #define EXIT_TRACE		(EXIT_ZOMBIE | EXIT_DEAD)
107941bdea7SNamhyung Kim /* in tsk->state again */
108941bdea7SNamhyung Kim #define TASK_DEAD		64
109941bdea7SNamhyung Kim #define TASK_WAKEKILL		128
110941bdea7SNamhyung Kim #define TASK_WAKING		256
111941bdea7SNamhyung Kim #define TASK_PARKED		512
112941bdea7SNamhyung Kim 
113b1ffe8f3SIngo Molnar enum thread_state {
114b1ffe8f3SIngo Molnar 	THREAD_SLEEPING = 0,
115b1ffe8f3SIngo Molnar 	THREAD_WAIT_CPU,
116b1ffe8f3SIngo Molnar 	THREAD_SCHED_IN,
117b1ffe8f3SIngo Molnar 	THREAD_IGNORE
118b1ffe8f3SIngo Molnar };
119b1ffe8f3SIngo Molnar 
120b1ffe8f3SIngo Molnar struct work_atom {
121b1ffe8f3SIngo Molnar 	struct list_head	list;
122b1ffe8f3SIngo Molnar 	enum thread_state	state;
123aa1ab9d2SFrederic Weisbecker 	u64			sched_out_time;
124b1ffe8f3SIngo Molnar 	u64			wake_up_time;
125b1ffe8f3SIngo Molnar 	u64			sched_in_time;
126b1ffe8f3SIngo Molnar 	u64			runtime;
127b1ffe8f3SIngo Molnar };
128b1ffe8f3SIngo Molnar 
12939aeb52fSmingo struct work_atoms {
13039aeb52fSmingo 	struct list_head	work_list;
131b1ffe8f3SIngo Molnar 	struct thread		*thread;
132b1ffe8f3SIngo Molnar 	struct rb_node		node;
133b1ffe8f3SIngo Molnar 	u64			max_lat;
134dc000c45SJoel Fernandes (Google) 	u64			max_lat_start;
135dc000c45SJoel Fernandes (Google) 	u64			max_lat_end;
136b1ffe8f3SIngo Molnar 	u64			total_lat;
137b1ffe8f3SIngo Molnar 	u64			nb_atoms;
138b1ffe8f3SIngo Molnar 	u64			total_runtime;
1392f80dd44SJosef Bacik 	int			num_merged;
140b1ffe8f3SIngo Molnar };
141b1ffe8f3SIngo Molnar 
14239aeb52fSmingo typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
143b1ffe8f3SIngo Molnar 
1440e9b07e5SArnaldo Carvalho de Melo struct perf_sched;
1450e9b07e5SArnaldo Carvalho de Melo 
146419ab0d6SFrederic Weisbecker struct trace_sched_handler {
14732dcd021SJiri Olsa 	int (*switch_event)(struct perf_sched *sched, struct evsel *evsel,
1489ec3f4e4SArnaldo Carvalho de Melo 			    struct perf_sample *sample, struct machine *machine);
149419ab0d6SFrederic Weisbecker 
15032dcd021SJiri Olsa 	int (*runtime_event)(struct perf_sched *sched, struct evsel *evsel,
1519ec3f4e4SArnaldo Carvalho de Melo 			     struct perf_sample *sample, struct machine *machine);
15239aeb52fSmingo 
15332dcd021SJiri Olsa 	int (*wakeup_event)(struct perf_sched *sched, struct evsel *evsel,
1549ec3f4e4SArnaldo Carvalho de Melo 			    struct perf_sample *sample, struct machine *machine);
155419ab0d6SFrederic Weisbecker 
156cb627505SDavid Ahern 	/* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
157cb627505SDavid Ahern 	int (*fork_event)(struct perf_sched *sched, union perf_event *event,
158cb627505SDavid Ahern 			  struct machine *machine);
15955ffb7a6SMike Galbraith 
1600e9b07e5SArnaldo Carvalho de Melo 	int (*migrate_task_event)(struct perf_sched *sched,
16132dcd021SJiri Olsa 				  struct evsel *evsel,
1629ec3f4e4SArnaldo Carvalho de Melo 				  struct perf_sample *sample,
1639ec3f4e4SArnaldo Carvalho de Melo 				  struct machine *machine);
164419ab0d6SFrederic Weisbecker };
165419ab0d6SFrederic Weisbecker 
166a151a37aSJiri Olsa #define COLOR_PIDS PERF_COLOR_BLUE
167cf294f24SJiri Olsa #define COLOR_CPUS PERF_COLOR_BG_RED
168a151a37aSJiri Olsa 
16999623c62SJiri Olsa struct perf_sched_map {
17099623c62SJiri Olsa 	DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
1716d18804bSIan Rogers 	struct perf_cpu		*comp_cpus;
17299623c62SJiri Olsa 	bool			 comp;
1739749b90eSJiri Olsa 	struct perf_thread_map *color_pids;
174a151a37aSJiri Olsa 	const char		*color_pids_str;
175f854839bSJiri Olsa 	struct perf_cpu_map	*color_cpus;
176cf294f24SJiri Olsa 	const char		*color_cpus_str;
177f854839bSJiri Olsa 	struct perf_cpu_map	*cpus;
17873643bb6SJiri Olsa 	const char		*cpus_str;
17999623c62SJiri Olsa };
18099623c62SJiri Olsa 
1810e9b07e5SArnaldo Carvalho de Melo struct perf_sched {
1820e9b07e5SArnaldo Carvalho de Melo 	struct perf_tool tool;
1830e9b07e5SArnaldo Carvalho de Melo 	const char	 *sort_order;
1840e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_tasks;
185cb06ac25SYunlong Song 	struct task_desc **pid_to_task;
1860e9b07e5SArnaldo Carvalho de Melo 	struct task_desc **tasks;
1870e9b07e5SArnaldo Carvalho de Melo 	const struct trace_sched_handler *tp_handler;
1880bd14ac2SIan Rogers 	struct mutex	 start_work_mutex;
1890bd14ac2SIan Rogers 	struct mutex	 work_done_wait_mutex;
1900e9b07e5SArnaldo Carvalho de Melo 	int		 profile_cpu;
1910e9b07e5SArnaldo Carvalho de Melo /*
1920e9b07e5SArnaldo Carvalho de Melo  * Track the current task - that way we can know whether there's any
1930e9b07e5SArnaldo Carvalho de Melo  * weird events, such as a task being switched away that is not current.
1940e9b07e5SArnaldo Carvalho de Melo  */
1956d18804bSIan Rogers 	struct perf_cpu	 max_cpu;
196232418a0SIan Rogers 	u32		 *curr_pid;
197232418a0SIan Rogers 	struct thread	 **curr_thread;
1980e9b07e5SArnaldo Carvalho de Melo 	char		 next_shortname1;
1990e9b07e5SArnaldo Carvalho de Melo 	char		 next_shortname2;
2000e9b07e5SArnaldo Carvalho de Melo 	unsigned int	 replay_repeat;
2010e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_run_events;
2020e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_sleep_events;
2030e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_wakeup_events;
2040e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_sleep_corrections;
2050e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_run_events_optimized;
2060e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 targetless_wakeups;
2070e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 multitarget_wakeups;
2080e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_runs;
2090e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_timestamps;
2100e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_unordered_timestamps;
2110e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_context_switch_bugs;
2120e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_events;
2130e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_lost_chunks;
2140e9b07e5SArnaldo Carvalho de Melo 	unsigned long	 nr_lost_events;
2150e9b07e5SArnaldo Carvalho de Melo 	u64		 run_measurement_overhead;
2160e9b07e5SArnaldo Carvalho de Melo 	u64		 sleep_measurement_overhead;
2170e9b07e5SArnaldo Carvalho de Melo 	u64		 start_time;
2180e9b07e5SArnaldo Carvalho de Melo 	u64		 cpu_usage;
2190e9b07e5SArnaldo Carvalho de Melo 	u64		 runavg_cpu_usage;
2200e9b07e5SArnaldo Carvalho de Melo 	u64		 parent_cpu_usage;
2210e9b07e5SArnaldo Carvalho de Melo 	u64		 runavg_parent_cpu_usage;
2220e9b07e5SArnaldo Carvalho de Melo 	u64		 sum_runtime;
2230e9b07e5SArnaldo Carvalho de Melo 	u64		 sum_fluct;
2240e9b07e5SArnaldo Carvalho de Melo 	u64		 run_avg;
2250e9b07e5SArnaldo Carvalho de Melo 	u64		 all_runtime;
2260e9b07e5SArnaldo Carvalho de Melo 	u64		 all_count;
227232418a0SIan Rogers 	u64		 *cpu_last_switched;
228cb4c13a5SDavidlohr Bueso 	struct rb_root_cached atom_root, sorted_atom_root, merged_atom_root;
2290e9b07e5SArnaldo Carvalho de Melo 	struct list_head sort_list, cmp_pid;
230939cda52SYunlong Song 	bool force;
2312f80dd44SJosef Bacik 	bool skip_merge;
23299623c62SJiri Olsa 	struct perf_sched_map map;
23352df138cSDavid Ahern 
23452df138cSDavid Ahern 	/* options for timehist command */
23552df138cSDavid Ahern 	bool		summary;
23652df138cSDavid Ahern 	bool		summary_only;
237699b5b92SNamhyung Kim 	bool		idle_hist;
2386c973c90SDavid Ahern 	bool		show_callchain;
2396c973c90SDavid Ahern 	unsigned int	max_stack;
240a407b067SDavid Ahern 	bool		show_cpu_visual;
241fc1469f1SDavid Ahern 	bool		show_wakeups;
242292c4a8fSBrendan Gregg 	bool		show_next;
243350f54faSDavid Ahern 	bool		show_migrations;
244414e050cSNamhyung Kim 	bool		show_state;
24552df138cSDavid Ahern 	u64		skipped_samples;
246853b7407SDavid Ahern 	const char	*time_str;
247853b7407SDavid Ahern 	struct perf_time_interval ptime;
2489396c9cbSNamhyung Kim 	struct perf_time_interval hist_time;
24959c26660SIan Rogers 	volatile bool   thread_funcs_exit;
2500e9b07e5SArnaldo Carvalho de Melo };
2510e9b07e5SArnaldo Carvalho de Melo 
25249394a2aSDavid Ahern /* per thread run time data */
25349394a2aSDavid Ahern struct thread_runtime {
25449394a2aSDavid Ahern 	u64 last_time;      /* time of previous sched in/out event */
25549394a2aSDavid Ahern 	u64 dt_run;         /* run time */
256941bdea7SNamhyung Kim 	u64 dt_sleep;       /* time between CPU access by sleep (off cpu) */
257941bdea7SNamhyung Kim 	u64 dt_iowait;      /* time between CPU access by iowait (off cpu) */
258941bdea7SNamhyung Kim 	u64 dt_preempt;     /* time between CPU access by preempt (off cpu) */
25949394a2aSDavid Ahern 	u64 dt_delay;       /* time between wakeup and sched-in */
26049394a2aSDavid Ahern 	u64 ready_to_run;   /* time of wakeup */
26149394a2aSDavid Ahern 
26249394a2aSDavid Ahern 	struct stats run_stats;
26349394a2aSDavid Ahern 	u64 total_run_time;
264587782c5SNamhyung Kim 	u64 total_sleep_time;
265587782c5SNamhyung Kim 	u64 total_iowait_time;
266587782c5SNamhyung Kim 	u64 total_preempt_time;
267587782c5SNamhyung Kim 	u64 total_delay_time;
268350f54faSDavid Ahern 
269941bdea7SNamhyung Kim 	int last_state;
2708640da9fSChangbin Du 
2718640da9fSChangbin Du 	char shortname[3];
27299a3c3a9SChangbin Du 	bool comm_changed;
27399a3c3a9SChangbin Du 
274350f54faSDavid Ahern 	u64 migrations;
27549394a2aSDavid Ahern };
27649394a2aSDavid Ahern 
27749394a2aSDavid Ahern /* per event run time data */
27849394a2aSDavid Ahern struct evsel_runtime {
27949394a2aSDavid Ahern 	u64 *last_time; /* time this event was last seen per cpu */
28049394a2aSDavid Ahern 	u32 ncpu;       /* highest cpu slot allocated */
28149394a2aSDavid Ahern };
28249394a2aSDavid Ahern 
2833bc2fa9cSNamhyung Kim /* per cpu idle time data */
2843bc2fa9cSNamhyung Kim struct idle_thread_runtime {
2853bc2fa9cSNamhyung Kim 	struct thread_runtime	tr;
2863bc2fa9cSNamhyung Kim 	struct thread		*last_thread;
287cb4c13a5SDavidlohr Bueso 	struct rb_root_cached	sorted_root;
2883bc2fa9cSNamhyung Kim 	struct callchain_root	callchain;
2893bc2fa9cSNamhyung Kim 	struct callchain_cursor	cursor;
2903bc2fa9cSNamhyung Kim };
2913bc2fa9cSNamhyung Kim 
29249394a2aSDavid Ahern /* track idle times per cpu */
29349394a2aSDavid Ahern static struct thread **idle_threads;
29449394a2aSDavid Ahern static int idle_max_cpu;
29549394a2aSDavid Ahern static char idle_comm[] = "<idle>";
29649394a2aSDavid Ahern 
get_nsecs(void)2970e9b07e5SArnaldo Carvalho de Melo static u64 get_nsecs(void)
2980e9b07e5SArnaldo Carvalho de Melo {
2990e9b07e5SArnaldo Carvalho de Melo 	struct timespec ts;
3000e9b07e5SArnaldo Carvalho de Melo 
3010e9b07e5SArnaldo Carvalho de Melo 	clock_gettime(CLOCK_MONOTONIC, &ts);
3020e9b07e5SArnaldo Carvalho de Melo 
3034fc76e49SArnaldo Carvalho de Melo 	return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
3040e9b07e5SArnaldo Carvalho de Melo }
3050e9b07e5SArnaldo Carvalho de Melo 
burn_nsecs(struct perf_sched * sched,u64 nsecs)3060e9b07e5SArnaldo Carvalho de Melo static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
3070e9b07e5SArnaldo Carvalho de Melo {
3080e9b07e5SArnaldo Carvalho de Melo 	u64 T0 = get_nsecs(), T1;
3090e9b07e5SArnaldo Carvalho de Melo 
3100e9b07e5SArnaldo Carvalho de Melo 	do {
3110e9b07e5SArnaldo Carvalho de Melo 		T1 = get_nsecs();
3120e9b07e5SArnaldo Carvalho de Melo 	} while (T1 + sched->run_measurement_overhead < T0 + nsecs);
3130e9b07e5SArnaldo Carvalho de Melo }
3140e9b07e5SArnaldo Carvalho de Melo 
sleep_nsecs(u64 nsecs)3150e9b07e5SArnaldo Carvalho de Melo static void sleep_nsecs(u64 nsecs)
3160e9b07e5SArnaldo Carvalho de Melo {
3170e9b07e5SArnaldo Carvalho de Melo 	struct timespec ts;
3180e9b07e5SArnaldo Carvalho de Melo 
3190e9b07e5SArnaldo Carvalho de Melo 	ts.tv_nsec = nsecs % 999999999;
3200e9b07e5SArnaldo Carvalho de Melo 	ts.tv_sec = nsecs / 999999999;
3210e9b07e5SArnaldo Carvalho de Melo 
3220e9b07e5SArnaldo Carvalho de Melo 	nanosleep(&ts, NULL);
3230e9b07e5SArnaldo Carvalho de Melo }
3240e9b07e5SArnaldo Carvalho de Melo 
calibrate_run_measurement_overhead(struct perf_sched * sched)3250e9b07e5SArnaldo Carvalho de Melo static void calibrate_run_measurement_overhead(struct perf_sched *sched)
3260e9b07e5SArnaldo Carvalho de Melo {
3274fc76e49SArnaldo Carvalho de Melo 	u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
3280e9b07e5SArnaldo Carvalho de Melo 	int i;
3290e9b07e5SArnaldo Carvalho de Melo 
3300e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < 10; i++) {
3310e9b07e5SArnaldo Carvalho de Melo 		T0 = get_nsecs();
3320e9b07e5SArnaldo Carvalho de Melo 		burn_nsecs(sched, 0);
3330e9b07e5SArnaldo Carvalho de Melo 		T1 = get_nsecs();
3340e9b07e5SArnaldo Carvalho de Melo 		delta = T1-T0;
3350e9b07e5SArnaldo Carvalho de Melo 		min_delta = min(min_delta, delta);
3360e9b07e5SArnaldo Carvalho de Melo 	}
3370e9b07e5SArnaldo Carvalho de Melo 	sched->run_measurement_overhead = min_delta;
3380e9b07e5SArnaldo Carvalho de Melo 
3390e9b07e5SArnaldo Carvalho de Melo 	printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
3400e9b07e5SArnaldo Carvalho de Melo }
3410e9b07e5SArnaldo Carvalho de Melo 
calibrate_sleep_measurement_overhead(struct perf_sched * sched)3420e9b07e5SArnaldo Carvalho de Melo static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
3430e9b07e5SArnaldo Carvalho de Melo {
3444fc76e49SArnaldo Carvalho de Melo 	u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
3450e9b07e5SArnaldo Carvalho de Melo 	int i;
3460e9b07e5SArnaldo Carvalho de Melo 
3470e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < 10; i++) {
3480e9b07e5SArnaldo Carvalho de Melo 		T0 = get_nsecs();
3490e9b07e5SArnaldo Carvalho de Melo 		sleep_nsecs(10000);
3500e9b07e5SArnaldo Carvalho de Melo 		T1 = get_nsecs();
3510e9b07e5SArnaldo Carvalho de Melo 		delta = T1-T0;
3520e9b07e5SArnaldo Carvalho de Melo 		min_delta = min(min_delta, delta);
3530e9b07e5SArnaldo Carvalho de Melo 	}
3540e9b07e5SArnaldo Carvalho de Melo 	min_delta -= 10000;
3550e9b07e5SArnaldo Carvalho de Melo 	sched->sleep_measurement_overhead = min_delta;
3560e9b07e5SArnaldo Carvalho de Melo 
3570e9b07e5SArnaldo Carvalho de Melo 	printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
3580e9b07e5SArnaldo Carvalho de Melo }
3590e9b07e5SArnaldo Carvalho de Melo 
3600e9b07e5SArnaldo Carvalho de Melo static struct sched_atom *
get_new_event(struct task_desc * task,u64 timestamp)3610e9b07e5SArnaldo Carvalho de Melo get_new_event(struct task_desc *task, u64 timestamp)
3620e9b07e5SArnaldo Carvalho de Melo {
3630e9b07e5SArnaldo Carvalho de Melo 	struct sched_atom *event = zalloc(sizeof(*event));
3640e9b07e5SArnaldo Carvalho de Melo 	unsigned long idx = task->nr_events;
3650e9b07e5SArnaldo Carvalho de Melo 	size_t size;
3660e9b07e5SArnaldo Carvalho de Melo 
3670e9b07e5SArnaldo Carvalho de Melo 	event->timestamp = timestamp;
3680e9b07e5SArnaldo Carvalho de Melo 	event->nr = idx;
3690e9b07e5SArnaldo Carvalho de Melo 
3700e9b07e5SArnaldo Carvalho de Melo 	task->nr_events++;
3710e9b07e5SArnaldo Carvalho de Melo 	size = sizeof(struct sched_atom *) * task->nr_events;
3720e9b07e5SArnaldo Carvalho de Melo 	task->atoms = realloc(task->atoms, size);
3730e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(!task->atoms);
3740e9b07e5SArnaldo Carvalho de Melo 
3750e9b07e5SArnaldo Carvalho de Melo 	task->atoms[idx] = event;
3760e9b07e5SArnaldo Carvalho de Melo 
3770e9b07e5SArnaldo Carvalho de Melo 	return event;
3780e9b07e5SArnaldo Carvalho de Melo }
3790e9b07e5SArnaldo Carvalho de Melo 
last_event(struct task_desc * task)3800e9b07e5SArnaldo Carvalho de Melo static struct sched_atom *last_event(struct task_desc *task)
3810e9b07e5SArnaldo Carvalho de Melo {
3820e9b07e5SArnaldo Carvalho de Melo 	if (!task->nr_events)
3830e9b07e5SArnaldo Carvalho de Melo 		return NULL;
3840e9b07e5SArnaldo Carvalho de Melo 
3850e9b07e5SArnaldo Carvalho de Melo 	return task->atoms[task->nr_events - 1];
3860e9b07e5SArnaldo Carvalho de Melo }
3870e9b07e5SArnaldo Carvalho de Melo 
add_sched_event_run(struct perf_sched * sched,struct task_desc * task,u64 timestamp,u64 duration)3880e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
3890e9b07e5SArnaldo Carvalho de Melo 				u64 timestamp, u64 duration)
3900e9b07e5SArnaldo Carvalho de Melo {
3910e9b07e5SArnaldo Carvalho de Melo 	struct sched_atom *event, *curr_event = last_event(task);
3920e9b07e5SArnaldo Carvalho de Melo 
3930e9b07e5SArnaldo Carvalho de Melo 	/*
3940e9b07e5SArnaldo Carvalho de Melo 	 * optimize an existing RUN event by merging this one
3950e9b07e5SArnaldo Carvalho de Melo 	 * to it:
3960e9b07e5SArnaldo Carvalho de Melo 	 */
3970e9b07e5SArnaldo Carvalho de Melo 	if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
3980e9b07e5SArnaldo Carvalho de Melo 		sched->nr_run_events_optimized++;
3990e9b07e5SArnaldo Carvalho de Melo 		curr_event->duration += duration;
4000e9b07e5SArnaldo Carvalho de Melo 		return;
4010e9b07e5SArnaldo Carvalho de Melo 	}
4020e9b07e5SArnaldo Carvalho de Melo 
4030e9b07e5SArnaldo Carvalho de Melo 	event = get_new_event(task, timestamp);
4040e9b07e5SArnaldo Carvalho de Melo 
4050e9b07e5SArnaldo Carvalho de Melo 	event->type = SCHED_EVENT_RUN;
4060e9b07e5SArnaldo Carvalho de Melo 	event->duration = duration;
4070e9b07e5SArnaldo Carvalho de Melo 
4080e9b07e5SArnaldo Carvalho de Melo 	sched->nr_run_events++;
4090e9b07e5SArnaldo Carvalho de Melo }
4100e9b07e5SArnaldo Carvalho de Melo 
add_sched_event_wakeup(struct perf_sched * sched,struct task_desc * task,u64 timestamp,struct task_desc * wakee)4110e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
4120e9b07e5SArnaldo Carvalho de Melo 				   u64 timestamp, struct task_desc *wakee)
4130e9b07e5SArnaldo Carvalho de Melo {
4140e9b07e5SArnaldo Carvalho de Melo 	struct sched_atom *event, *wakee_event;
4150e9b07e5SArnaldo Carvalho de Melo 
4160e9b07e5SArnaldo Carvalho de Melo 	event = get_new_event(task, timestamp);
4170e9b07e5SArnaldo Carvalho de Melo 	event->type = SCHED_EVENT_WAKEUP;
4180e9b07e5SArnaldo Carvalho de Melo 	event->wakee = wakee;
4190e9b07e5SArnaldo Carvalho de Melo 
4200e9b07e5SArnaldo Carvalho de Melo 	wakee_event = last_event(wakee);
4210e9b07e5SArnaldo Carvalho de Melo 	if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
4220e9b07e5SArnaldo Carvalho de Melo 		sched->targetless_wakeups++;
4230e9b07e5SArnaldo Carvalho de Melo 		return;
4240e9b07e5SArnaldo Carvalho de Melo 	}
4250e9b07e5SArnaldo Carvalho de Melo 	if (wakee_event->wait_sem) {
4260e9b07e5SArnaldo Carvalho de Melo 		sched->multitarget_wakeups++;
4270e9b07e5SArnaldo Carvalho de Melo 		return;
4280e9b07e5SArnaldo Carvalho de Melo 	}
4290e9b07e5SArnaldo Carvalho de Melo 
4300e9b07e5SArnaldo Carvalho de Melo 	wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
4310e9b07e5SArnaldo Carvalho de Melo 	sem_init(wakee_event->wait_sem, 0, 0);
4320e9b07e5SArnaldo Carvalho de Melo 	wakee_event->specific_wait = 1;
4330e9b07e5SArnaldo Carvalho de Melo 	event->wait_sem = wakee_event->wait_sem;
4340e9b07e5SArnaldo Carvalho de Melo 
4350e9b07e5SArnaldo Carvalho de Melo 	sched->nr_wakeup_events++;
4360e9b07e5SArnaldo Carvalho de Melo }
4370e9b07e5SArnaldo Carvalho de Melo 
add_sched_event_sleep(struct perf_sched * sched,struct task_desc * task,u64 timestamp,u64 task_state __maybe_unused)4380e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
4390e9b07e5SArnaldo Carvalho de Melo 				  u64 timestamp, u64 task_state __maybe_unused)
4400e9b07e5SArnaldo Carvalho de Melo {
4410e9b07e5SArnaldo Carvalho de Melo 	struct sched_atom *event = get_new_event(task, timestamp);
4420e9b07e5SArnaldo Carvalho de Melo 
4430e9b07e5SArnaldo Carvalho de Melo 	event->type = SCHED_EVENT_SLEEP;
4440e9b07e5SArnaldo Carvalho de Melo 
4450e9b07e5SArnaldo Carvalho de Melo 	sched->nr_sleep_events++;
4460e9b07e5SArnaldo Carvalho de Melo }
4470e9b07e5SArnaldo Carvalho de Melo 
register_pid(struct perf_sched * sched,unsigned long pid,const char * comm)4480e9b07e5SArnaldo Carvalho de Melo static struct task_desc *register_pid(struct perf_sched *sched,
4490e9b07e5SArnaldo Carvalho de Melo 				      unsigned long pid, const char *comm)
4500e9b07e5SArnaldo Carvalho de Melo {
4510e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *task;
452cb06ac25SYunlong Song 	static int pid_max;
4530e9b07e5SArnaldo Carvalho de Melo 
454cb06ac25SYunlong Song 	if (sched->pid_to_task == NULL) {
455cb06ac25SYunlong Song 		if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
456cb06ac25SYunlong Song 			pid_max = MAX_PID;
457cb06ac25SYunlong Song 		BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
458cb06ac25SYunlong Song 	}
4593a423a5cSYunlong Song 	if (pid >= (unsigned long)pid_max) {
4603a423a5cSYunlong Song 		BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
4613a423a5cSYunlong Song 			sizeof(struct task_desc *))) == NULL);
4623a423a5cSYunlong Song 		while (pid >= (unsigned long)pid_max)
4633a423a5cSYunlong Song 			sched->pid_to_task[pid_max++] = NULL;
4643a423a5cSYunlong Song 	}
4650e9b07e5SArnaldo Carvalho de Melo 
4660e9b07e5SArnaldo Carvalho de Melo 	task = sched->pid_to_task[pid];
4670e9b07e5SArnaldo Carvalho de Melo 
4680e9b07e5SArnaldo Carvalho de Melo 	if (task)
4690e9b07e5SArnaldo Carvalho de Melo 		return task;
4700e9b07e5SArnaldo Carvalho de Melo 
4710e9b07e5SArnaldo Carvalho de Melo 	task = zalloc(sizeof(*task));
4720e9b07e5SArnaldo Carvalho de Melo 	task->pid = pid;
4730e9b07e5SArnaldo Carvalho de Melo 	task->nr = sched->nr_tasks;
4740e9b07e5SArnaldo Carvalho de Melo 	strcpy(task->comm, comm);
4750e9b07e5SArnaldo Carvalho de Melo 	/*
4760e9b07e5SArnaldo Carvalho de Melo 	 * every task starts in sleeping state - this gets ignored
4770e9b07e5SArnaldo Carvalho de Melo 	 * if there's no wakeup pointing to this sleep state:
4780e9b07e5SArnaldo Carvalho de Melo 	 */
4790e9b07e5SArnaldo Carvalho de Melo 	add_sched_event_sleep(sched, task, 0, 0);
4800e9b07e5SArnaldo Carvalho de Melo 
4810e9b07e5SArnaldo Carvalho de Melo 	sched->pid_to_task[pid] = task;
4820e9b07e5SArnaldo Carvalho de Melo 	sched->nr_tasks++;
4830755bc4dSYunlong Song 	sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
4840e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(!sched->tasks);
4850e9b07e5SArnaldo Carvalho de Melo 	sched->tasks[task->nr] = task;
4860e9b07e5SArnaldo Carvalho de Melo 
487bb963e16SNamhyung Kim 	if (verbose > 0)
4880e9b07e5SArnaldo Carvalho de Melo 		printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
4890e9b07e5SArnaldo Carvalho de Melo 
4900e9b07e5SArnaldo Carvalho de Melo 	return task;
4910e9b07e5SArnaldo Carvalho de Melo }
4920e9b07e5SArnaldo Carvalho de Melo 
4930e9b07e5SArnaldo Carvalho de Melo 
print_task_traces(struct perf_sched * sched)4940e9b07e5SArnaldo Carvalho de Melo static void print_task_traces(struct perf_sched *sched)
4950e9b07e5SArnaldo Carvalho de Melo {
4960e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *task;
4970e9b07e5SArnaldo Carvalho de Melo 	unsigned long i;
4980e9b07e5SArnaldo Carvalho de Melo 
4990e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
5000e9b07e5SArnaldo Carvalho de Melo 		task = sched->tasks[i];
5010e9b07e5SArnaldo Carvalho de Melo 		printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
5020e9b07e5SArnaldo Carvalho de Melo 			task->nr, task->comm, task->pid, task->nr_events);
5030e9b07e5SArnaldo Carvalho de Melo 	}
5040e9b07e5SArnaldo Carvalho de Melo }
5050e9b07e5SArnaldo Carvalho de Melo 
add_cross_task_wakeups(struct perf_sched * sched)5060e9b07e5SArnaldo Carvalho de Melo static void add_cross_task_wakeups(struct perf_sched *sched)
5070e9b07e5SArnaldo Carvalho de Melo {
5080e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *task1, *task2;
5090e9b07e5SArnaldo Carvalho de Melo 	unsigned long i, j;
5100e9b07e5SArnaldo Carvalho de Melo 
5110e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
5120e9b07e5SArnaldo Carvalho de Melo 		task1 = sched->tasks[i];
5130e9b07e5SArnaldo Carvalho de Melo 		j = i + 1;
5140e9b07e5SArnaldo Carvalho de Melo 		if (j == sched->nr_tasks)
5150e9b07e5SArnaldo Carvalho de Melo 			j = 0;
5160e9b07e5SArnaldo Carvalho de Melo 		task2 = sched->tasks[j];
5170e9b07e5SArnaldo Carvalho de Melo 		add_sched_event_wakeup(sched, task1, 0, task2);
5180e9b07e5SArnaldo Carvalho de Melo 	}
5190e9b07e5SArnaldo Carvalho de Melo }
5200e9b07e5SArnaldo Carvalho de Melo 
perf_sched__process_event(struct perf_sched * sched,struct sched_atom * atom)5210e9b07e5SArnaldo Carvalho de Melo static void perf_sched__process_event(struct perf_sched *sched,
5220e9b07e5SArnaldo Carvalho de Melo 				      struct sched_atom *atom)
5230e9b07e5SArnaldo Carvalho de Melo {
5240e9b07e5SArnaldo Carvalho de Melo 	int ret = 0;
5250e9b07e5SArnaldo Carvalho de Melo 
5260e9b07e5SArnaldo Carvalho de Melo 	switch (atom->type) {
5270e9b07e5SArnaldo Carvalho de Melo 		case SCHED_EVENT_RUN:
5280e9b07e5SArnaldo Carvalho de Melo 			burn_nsecs(sched, atom->duration);
5290e9b07e5SArnaldo Carvalho de Melo 			break;
5300e9b07e5SArnaldo Carvalho de Melo 		case SCHED_EVENT_SLEEP:
5310e9b07e5SArnaldo Carvalho de Melo 			if (atom->wait_sem)
5320e9b07e5SArnaldo Carvalho de Melo 				ret = sem_wait(atom->wait_sem);
5330e9b07e5SArnaldo Carvalho de Melo 			BUG_ON(ret);
5340e9b07e5SArnaldo Carvalho de Melo 			break;
5350e9b07e5SArnaldo Carvalho de Melo 		case SCHED_EVENT_WAKEUP:
5360e9b07e5SArnaldo Carvalho de Melo 			if (atom->wait_sem)
5370e9b07e5SArnaldo Carvalho de Melo 				ret = sem_post(atom->wait_sem);
5380e9b07e5SArnaldo Carvalho de Melo 			BUG_ON(ret);
5390e9b07e5SArnaldo Carvalho de Melo 			break;
5400e9b07e5SArnaldo Carvalho de Melo 		case SCHED_EVENT_MIGRATION:
5410e9b07e5SArnaldo Carvalho de Melo 			break;
5420e9b07e5SArnaldo Carvalho de Melo 		default:
5430e9b07e5SArnaldo Carvalho de Melo 			BUG_ON(1);
5440e9b07e5SArnaldo Carvalho de Melo 	}
5450e9b07e5SArnaldo Carvalho de Melo }
5460e9b07e5SArnaldo Carvalho de Melo 
get_cpu_usage_nsec_parent(void)5470e9b07e5SArnaldo Carvalho de Melo static u64 get_cpu_usage_nsec_parent(void)
5480e9b07e5SArnaldo Carvalho de Melo {
5490e9b07e5SArnaldo Carvalho de Melo 	struct rusage ru;
5500e9b07e5SArnaldo Carvalho de Melo 	u64 sum;
5510e9b07e5SArnaldo Carvalho de Melo 	int err;
5520e9b07e5SArnaldo Carvalho de Melo 
5530e9b07e5SArnaldo Carvalho de Melo 	err = getrusage(RUSAGE_SELF, &ru);
5540e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(err);
5550e9b07e5SArnaldo Carvalho de Melo 
5564fc76e49SArnaldo Carvalho de Melo 	sum =  ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
5574fc76e49SArnaldo Carvalho de Melo 	sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
5580e9b07e5SArnaldo Carvalho de Melo 
5590e9b07e5SArnaldo Carvalho de Melo 	return sum;
5600e9b07e5SArnaldo Carvalho de Melo }
5610e9b07e5SArnaldo Carvalho de Melo 
self_open_counters(struct perf_sched * sched,unsigned long cur_task)562939cda52SYunlong Song static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
5630e9b07e5SArnaldo Carvalho de Melo {
5640e9b07e5SArnaldo Carvalho de Melo 	struct perf_event_attr attr;
565939cda52SYunlong Song 	char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
5660e9b07e5SArnaldo Carvalho de Melo 	int fd;
567939cda52SYunlong Song 	struct rlimit limit;
568939cda52SYunlong Song 	bool need_privilege = false;
5690e9b07e5SArnaldo Carvalho de Melo 
5700e9b07e5SArnaldo Carvalho de Melo 	memset(&attr, 0, sizeof(attr));
5710e9b07e5SArnaldo Carvalho de Melo 
5720e9b07e5SArnaldo Carvalho de Melo 	attr.type = PERF_TYPE_SOFTWARE;
5730e9b07e5SArnaldo Carvalho de Melo 	attr.config = PERF_COUNT_SW_TASK_CLOCK;
5740e9b07e5SArnaldo Carvalho de Melo 
575939cda52SYunlong Song force_again:
57657480d2cSYann Droneaud 	fd = sys_perf_event_open(&attr, 0, -1, -1,
57757480d2cSYann Droneaud 				 perf_event_open_cloexec_flag());
5780e9b07e5SArnaldo Carvalho de Melo 
5791aff59beSYunlong Song 	if (fd < 0) {
580939cda52SYunlong Song 		if (errno == EMFILE) {
581939cda52SYunlong Song 			if (sched->force) {
582939cda52SYunlong Song 				BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
583939cda52SYunlong Song 				limit.rlim_cur += sched->nr_tasks - cur_task;
584939cda52SYunlong Song 				if (limit.rlim_cur > limit.rlim_max) {
585939cda52SYunlong Song 					limit.rlim_max = limit.rlim_cur;
586939cda52SYunlong Song 					need_privilege = true;
587939cda52SYunlong Song 				}
588939cda52SYunlong Song 				if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
589939cda52SYunlong Song 					if (need_privilege && errno == EPERM)
590939cda52SYunlong Song 						strcpy(info, "Need privilege\n");
591939cda52SYunlong Song 				} else
592939cda52SYunlong Song 					goto force_again;
593939cda52SYunlong Song 			} else
594939cda52SYunlong Song 				strcpy(info, "Have a try with -f option\n");
595939cda52SYunlong Song 		}
59660b7d14aSNamhyung Kim 		pr_err("Error: sys_perf_event_open() syscall returned "
597939cda52SYunlong Song 		       "with %d (%s)\n%s", fd,
598c8b5f2c9SArnaldo Carvalho de Melo 		       str_error_r(errno, sbuf, sizeof(sbuf)), info);
5991aff59beSYunlong Song 		exit(EXIT_FAILURE);
6001aff59beSYunlong Song 	}
6010e9b07e5SArnaldo Carvalho de Melo 	return fd;
6020e9b07e5SArnaldo Carvalho de Melo }
6030e9b07e5SArnaldo Carvalho de Melo 
get_cpu_usage_nsec_self(int fd)6040e9b07e5SArnaldo Carvalho de Melo static u64 get_cpu_usage_nsec_self(int fd)
6050e9b07e5SArnaldo Carvalho de Melo {
6060e9b07e5SArnaldo Carvalho de Melo 	u64 runtime;
6070e9b07e5SArnaldo Carvalho de Melo 	int ret;
6080e9b07e5SArnaldo Carvalho de Melo 
6090e9b07e5SArnaldo Carvalho de Melo 	ret = read(fd, &runtime, sizeof(runtime));
6100e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(ret != sizeof(runtime));
6110e9b07e5SArnaldo Carvalho de Melo 
6120e9b07e5SArnaldo Carvalho de Melo 	return runtime;
6130e9b07e5SArnaldo Carvalho de Melo }
6140e9b07e5SArnaldo Carvalho de Melo 
6150e9b07e5SArnaldo Carvalho de Melo struct sched_thread_parms {
6160e9b07e5SArnaldo Carvalho de Melo 	struct task_desc  *task;
6170e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched;
61808097abcSYunlong Song 	int fd;
6190e9b07e5SArnaldo Carvalho de Melo };
6200e9b07e5SArnaldo Carvalho de Melo 
thread_func(void * ctx)6210e9b07e5SArnaldo Carvalho de Melo static void *thread_func(void *ctx)
6220e9b07e5SArnaldo Carvalho de Melo {
6230e9b07e5SArnaldo Carvalho de Melo 	struct sched_thread_parms *parms = ctx;
6240e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *this_task = parms->task;
6250e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched = parms->sched;
6260e9b07e5SArnaldo Carvalho de Melo 	u64 cpu_usage_0, cpu_usage_1;
6270e9b07e5SArnaldo Carvalho de Melo 	unsigned long i, ret;
6280e9b07e5SArnaldo Carvalho de Melo 	char comm2[22];
62908097abcSYunlong Song 	int fd = parms->fd;
6300e9b07e5SArnaldo Carvalho de Melo 
63174cf249dSArnaldo Carvalho de Melo 	zfree(&parms);
6320e9b07e5SArnaldo Carvalho de Melo 
6330e9b07e5SArnaldo Carvalho de Melo 	sprintf(comm2, ":%s", this_task->comm);
6340e9b07e5SArnaldo Carvalho de Melo 	prctl(PR_SET_NAME, comm2);
6350e9b07e5SArnaldo Carvalho de Melo 	if (fd < 0)
6360e9b07e5SArnaldo Carvalho de Melo 		return NULL;
63759c26660SIan Rogers 
63859c26660SIan Rogers 	while (!sched->thread_funcs_exit) {
6390e9b07e5SArnaldo Carvalho de Melo 		ret = sem_post(&this_task->ready_for_work);
6400e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(ret);
6410bd14ac2SIan Rogers 		mutex_lock(&sched->start_work_mutex);
6420bd14ac2SIan Rogers 		mutex_unlock(&sched->start_work_mutex);
6430e9b07e5SArnaldo Carvalho de Melo 
6440e9b07e5SArnaldo Carvalho de Melo 		cpu_usage_0 = get_cpu_usage_nsec_self(fd);
6450e9b07e5SArnaldo Carvalho de Melo 
6460e9b07e5SArnaldo Carvalho de Melo 		for (i = 0; i < this_task->nr_events; i++) {
6470e9b07e5SArnaldo Carvalho de Melo 			this_task->curr_event = i;
6480e9b07e5SArnaldo Carvalho de Melo 			perf_sched__process_event(sched, this_task->atoms[i]);
6490e9b07e5SArnaldo Carvalho de Melo 		}
6500e9b07e5SArnaldo Carvalho de Melo 
6510e9b07e5SArnaldo Carvalho de Melo 		cpu_usage_1 = get_cpu_usage_nsec_self(fd);
6520e9b07e5SArnaldo Carvalho de Melo 		this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
6530e9b07e5SArnaldo Carvalho de Melo 		ret = sem_post(&this_task->work_done_sem);
6540e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(ret);
6550e9b07e5SArnaldo Carvalho de Melo 
6560bd14ac2SIan Rogers 		mutex_lock(&sched->work_done_wait_mutex);
6570bd14ac2SIan Rogers 		mutex_unlock(&sched->work_done_wait_mutex);
65859c26660SIan Rogers 	}
65959c26660SIan Rogers 	return NULL;
6600e9b07e5SArnaldo Carvalho de Melo }
6610e9b07e5SArnaldo Carvalho de Melo 
create_tasks(struct perf_sched * sched)6620e9b07e5SArnaldo Carvalho de Melo static void create_tasks(struct perf_sched *sched)
66359c26660SIan Rogers 	EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex)
66459c26660SIan Rogers 	EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex)
6650e9b07e5SArnaldo Carvalho de Melo {
6660e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *task;
6670e9b07e5SArnaldo Carvalho de Melo 	pthread_attr_t attr;
6680e9b07e5SArnaldo Carvalho de Melo 	unsigned long i;
6690e9b07e5SArnaldo Carvalho de Melo 	int err;
6700e9b07e5SArnaldo Carvalho de Melo 
6710e9b07e5SArnaldo Carvalho de Melo 	err = pthread_attr_init(&attr);
6720e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(err);
6730e9b07e5SArnaldo Carvalho de Melo 	err = pthread_attr_setstacksize(&attr,
674d08c84e0SArnaldo Carvalho de Melo 			(size_t) max(16 * 1024, (int)PTHREAD_STACK_MIN));
6750e9b07e5SArnaldo Carvalho de Melo 	BUG_ON(err);
6760bd14ac2SIan Rogers 	mutex_lock(&sched->start_work_mutex);
6770bd14ac2SIan Rogers 	mutex_lock(&sched->work_done_wait_mutex);
6780e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
6790e9b07e5SArnaldo Carvalho de Melo 		struct sched_thread_parms *parms = malloc(sizeof(*parms));
6800e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(parms == NULL);
6810e9b07e5SArnaldo Carvalho de Melo 		parms->task = task = sched->tasks[i];
6820e9b07e5SArnaldo Carvalho de Melo 		parms->sched = sched;
683939cda52SYunlong Song 		parms->fd = self_open_counters(sched, i);
6840e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->sleep_sem, 0, 0);
6850e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->ready_for_work, 0, 0);
6860e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->work_done_sem, 0, 0);
6870e9b07e5SArnaldo Carvalho de Melo 		task->curr_event = 0;
6880e9b07e5SArnaldo Carvalho de Melo 		err = pthread_create(&task->thread, &attr, thread_func, parms);
6890e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(err);
6900e9b07e5SArnaldo Carvalho de Melo 	}
6910e9b07e5SArnaldo Carvalho de Melo }
6920e9b07e5SArnaldo Carvalho de Melo 
destroy_tasks(struct perf_sched * sched)693165da802SNamhyung Kim static void destroy_tasks(struct perf_sched *sched)
694165da802SNamhyung Kim 	UNLOCK_FUNCTION(sched->start_work_mutex)
695165da802SNamhyung Kim 	UNLOCK_FUNCTION(sched->work_done_wait_mutex)
696165da802SNamhyung Kim {
697165da802SNamhyung Kim 	struct task_desc *task;
698165da802SNamhyung Kim 	unsigned long i;
699165da802SNamhyung Kim 	int err;
700165da802SNamhyung Kim 
701165da802SNamhyung Kim 	mutex_unlock(&sched->start_work_mutex);
702165da802SNamhyung Kim 	mutex_unlock(&sched->work_done_wait_mutex);
703165da802SNamhyung Kim 	/* Get rid of threads so they won't be upset by mutex destrunction */
704165da802SNamhyung Kim 	for (i = 0; i < sched->nr_tasks; i++) {
705165da802SNamhyung Kim 		task = sched->tasks[i];
706165da802SNamhyung Kim 		err = pthread_join(task->thread, NULL);
707165da802SNamhyung Kim 		BUG_ON(err);
708165da802SNamhyung Kim 		sem_destroy(&task->sleep_sem);
709165da802SNamhyung Kim 		sem_destroy(&task->ready_for_work);
710165da802SNamhyung Kim 		sem_destroy(&task->work_done_sem);
711165da802SNamhyung Kim 	}
712165da802SNamhyung Kim }
713165da802SNamhyung Kim 
wait_for_tasks(struct perf_sched * sched)7140e9b07e5SArnaldo Carvalho de Melo static void wait_for_tasks(struct perf_sched *sched)
71559c26660SIan Rogers 	EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex)
71659c26660SIan Rogers 	EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex)
7170e9b07e5SArnaldo Carvalho de Melo {
7180e9b07e5SArnaldo Carvalho de Melo 	u64 cpu_usage_0, cpu_usage_1;
7190e9b07e5SArnaldo Carvalho de Melo 	struct task_desc *task;
7200e9b07e5SArnaldo Carvalho de Melo 	unsigned long i, ret;
7210e9b07e5SArnaldo Carvalho de Melo 
7220e9b07e5SArnaldo Carvalho de Melo 	sched->start_time = get_nsecs();
7230e9b07e5SArnaldo Carvalho de Melo 	sched->cpu_usage = 0;
7240bd14ac2SIan Rogers 	mutex_unlock(&sched->work_done_wait_mutex);
7250e9b07e5SArnaldo Carvalho de Melo 
7260e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
7270e9b07e5SArnaldo Carvalho de Melo 		task = sched->tasks[i];
7280e9b07e5SArnaldo Carvalho de Melo 		ret = sem_wait(&task->ready_for_work);
7290e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(ret);
7300e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->ready_for_work, 0, 0);
7310e9b07e5SArnaldo Carvalho de Melo 	}
7320bd14ac2SIan Rogers 	mutex_lock(&sched->work_done_wait_mutex);
7330e9b07e5SArnaldo Carvalho de Melo 
7340e9b07e5SArnaldo Carvalho de Melo 	cpu_usage_0 = get_cpu_usage_nsec_parent();
7350e9b07e5SArnaldo Carvalho de Melo 
7360bd14ac2SIan Rogers 	mutex_unlock(&sched->start_work_mutex);
7370e9b07e5SArnaldo Carvalho de Melo 
7380e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
7390e9b07e5SArnaldo Carvalho de Melo 		task = sched->tasks[i];
7400e9b07e5SArnaldo Carvalho de Melo 		ret = sem_wait(&task->work_done_sem);
7410e9b07e5SArnaldo Carvalho de Melo 		BUG_ON(ret);
7420e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->work_done_sem, 0, 0);
7430e9b07e5SArnaldo Carvalho de Melo 		sched->cpu_usage += task->cpu_usage;
7440e9b07e5SArnaldo Carvalho de Melo 		task->cpu_usage = 0;
7450e9b07e5SArnaldo Carvalho de Melo 	}
7460e9b07e5SArnaldo Carvalho de Melo 
7470e9b07e5SArnaldo Carvalho de Melo 	cpu_usage_1 = get_cpu_usage_nsec_parent();
7480e9b07e5SArnaldo Carvalho de Melo 	if (!sched->runavg_cpu_usage)
7490e9b07e5SArnaldo Carvalho de Melo 		sched->runavg_cpu_usage = sched->cpu_usage;
750ff5f3bbdSYunlong Song 	sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat;
7510e9b07e5SArnaldo Carvalho de Melo 
7520e9b07e5SArnaldo Carvalho de Melo 	sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
7530e9b07e5SArnaldo Carvalho de Melo 	if (!sched->runavg_parent_cpu_usage)
7540e9b07e5SArnaldo Carvalho de Melo 		sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
755ff5f3bbdSYunlong Song 	sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
756ff5f3bbdSYunlong Song 					 sched->parent_cpu_usage)/sched->replay_repeat;
7570e9b07e5SArnaldo Carvalho de Melo 
7580bd14ac2SIan Rogers 	mutex_lock(&sched->start_work_mutex);
7590e9b07e5SArnaldo Carvalho de Melo 
7600e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->nr_tasks; i++) {
7610e9b07e5SArnaldo Carvalho de Melo 		task = sched->tasks[i];
7620e9b07e5SArnaldo Carvalho de Melo 		sem_init(&task->sleep_sem, 0, 0);
7630e9b07e5SArnaldo Carvalho de Melo 		task->curr_event = 0;
7640e9b07e5SArnaldo Carvalho de Melo 	}
7650e9b07e5SArnaldo Carvalho de Melo }
7660e9b07e5SArnaldo Carvalho de Melo 
run_one_test(struct perf_sched * sched)7670e9b07e5SArnaldo Carvalho de Melo static void run_one_test(struct perf_sched *sched)
76859c26660SIan Rogers 	EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex)
76959c26660SIan Rogers 	EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex)
7700e9b07e5SArnaldo Carvalho de Melo {
7710e9b07e5SArnaldo Carvalho de Melo 	u64 T0, T1, delta, avg_delta, fluct;
7720e9b07e5SArnaldo Carvalho de Melo 
7730e9b07e5SArnaldo Carvalho de Melo 	T0 = get_nsecs();
7740e9b07e5SArnaldo Carvalho de Melo 	wait_for_tasks(sched);
7750e9b07e5SArnaldo Carvalho de Melo 	T1 = get_nsecs();
7760e9b07e5SArnaldo Carvalho de Melo 
7770e9b07e5SArnaldo Carvalho de Melo 	delta = T1 - T0;
7780e9b07e5SArnaldo Carvalho de Melo 	sched->sum_runtime += delta;
7790e9b07e5SArnaldo Carvalho de Melo 	sched->nr_runs++;
7800e9b07e5SArnaldo Carvalho de Melo 
7810e9b07e5SArnaldo Carvalho de Melo 	avg_delta = sched->sum_runtime / sched->nr_runs;
7820e9b07e5SArnaldo Carvalho de Melo 	if (delta < avg_delta)
7830e9b07e5SArnaldo Carvalho de Melo 		fluct = avg_delta - delta;
7840e9b07e5SArnaldo Carvalho de Melo 	else
7850e9b07e5SArnaldo Carvalho de Melo 		fluct = delta - avg_delta;
7860e9b07e5SArnaldo Carvalho de Melo 	sched->sum_fluct += fluct;
7870e9b07e5SArnaldo Carvalho de Melo 	if (!sched->run_avg)
7880e9b07e5SArnaldo Carvalho de Melo 		sched->run_avg = delta;
789ff5f3bbdSYunlong Song 	sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
7900e9b07e5SArnaldo Carvalho de Melo 
7914fc76e49SArnaldo Carvalho de Melo 	printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
7920e9b07e5SArnaldo Carvalho de Melo 
7934fc76e49SArnaldo Carvalho de Melo 	printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
7940e9b07e5SArnaldo Carvalho de Melo 
7950e9b07e5SArnaldo Carvalho de Melo 	printf("cpu: %0.2f / %0.2f",
7964fc76e49SArnaldo Carvalho de Melo 		(double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
7970e9b07e5SArnaldo Carvalho de Melo 
7980e9b07e5SArnaldo Carvalho de Melo #if 0
7990e9b07e5SArnaldo Carvalho de Melo 	/*
8000e9b07e5SArnaldo Carvalho de Melo 	 * rusage statistics done by the parent, these are less
8010e9b07e5SArnaldo Carvalho de Melo 	 * accurate than the sched->sum_exec_runtime based statistics:
8020e9b07e5SArnaldo Carvalho de Melo 	 */
8030e9b07e5SArnaldo Carvalho de Melo 	printf(" [%0.2f / %0.2f]",
8044fc76e49SArnaldo Carvalho de Melo 		(double)sched->parent_cpu_usage / NSEC_PER_MSEC,
8054fc76e49SArnaldo Carvalho de Melo 		(double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
8060e9b07e5SArnaldo Carvalho de Melo #endif
8070e9b07e5SArnaldo Carvalho de Melo 
8080e9b07e5SArnaldo Carvalho de Melo 	printf("\n");
8090e9b07e5SArnaldo Carvalho de Melo 
8100e9b07e5SArnaldo Carvalho de Melo 	if (sched->nr_sleep_corrections)
8110e9b07e5SArnaldo Carvalho de Melo 		printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
8120e9b07e5SArnaldo Carvalho de Melo 	sched->nr_sleep_corrections = 0;
8130e9b07e5SArnaldo Carvalho de Melo }
8140e9b07e5SArnaldo Carvalho de Melo 
test_calibrations(struct perf_sched * sched)8150e9b07e5SArnaldo Carvalho de Melo static void test_calibrations(struct perf_sched *sched)
8160e9b07e5SArnaldo Carvalho de Melo {
8170e9b07e5SArnaldo Carvalho de Melo 	u64 T0, T1;
8180e9b07e5SArnaldo Carvalho de Melo 
8190e9b07e5SArnaldo Carvalho de Melo 	T0 = get_nsecs();
8204fc76e49SArnaldo Carvalho de Melo 	burn_nsecs(sched, NSEC_PER_MSEC);
8210e9b07e5SArnaldo Carvalho de Melo 	T1 = get_nsecs();
8220e9b07e5SArnaldo Carvalho de Melo 
8230e9b07e5SArnaldo Carvalho de Melo 	printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
8240e9b07e5SArnaldo Carvalho de Melo 
8250e9b07e5SArnaldo Carvalho de Melo 	T0 = get_nsecs();
8264fc76e49SArnaldo Carvalho de Melo 	sleep_nsecs(NSEC_PER_MSEC);
8270e9b07e5SArnaldo Carvalho de Melo 	T1 = get_nsecs();
8280e9b07e5SArnaldo Carvalho de Melo 
8290e9b07e5SArnaldo Carvalho de Melo 	printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
8300e9b07e5SArnaldo Carvalho de Melo }
8310e9b07e5SArnaldo Carvalho de Melo 
832a116e05dSArnaldo Carvalho de Melo static int
replay_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)8330e9b07e5SArnaldo Carvalho de Melo replay_wakeup_event(struct perf_sched *sched,
83432dcd021SJiri Olsa 		    struct evsel *evsel, struct perf_sample *sample,
8359ec3f4e4SArnaldo Carvalho de Melo 		    struct machine *machine __maybe_unused)
836ec156764SIngo Molnar {
837efc0cdc9SArnaldo Carvalho de Melo 	const char *comm = evsel__strval(evsel, sample, "comm");
838efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid	 = evsel__intval(evsel, sample, "pid");
839419ab0d6SFrederic Weisbecker 	struct task_desc *waker, *wakee;
840419ab0d6SFrederic Weisbecker 
841bb963e16SNamhyung Kim 	if (verbose > 0) {
8422b7fcbc5SArnaldo Carvalho de Melo 		printf("sched_wakeup event %p\n", evsel);
843419ab0d6SFrederic Weisbecker 
8449ec3f4e4SArnaldo Carvalho de Melo 		printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
845419ab0d6SFrederic Weisbecker 	}
846419ab0d6SFrederic Weisbecker 
8472b7fcbc5SArnaldo Carvalho de Melo 	waker = register_pid(sched, sample->tid, "<unknown>");
8489ec3f4e4SArnaldo Carvalho de Melo 	wakee = register_pid(sched, pid, comm);
849419ab0d6SFrederic Weisbecker 
8500e9b07e5SArnaldo Carvalho de Melo 	add_sched_event_wakeup(sched, waker, sample->time, wakee);
851a116e05dSArnaldo Carvalho de Melo 	return 0;
852419ab0d6SFrederic Weisbecker }
853419ab0d6SFrederic Weisbecker 
replay_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)8549ec3f4e4SArnaldo Carvalho de Melo static int replay_switch_event(struct perf_sched *sched,
85532dcd021SJiri Olsa 			       struct evsel *evsel,
8569ec3f4e4SArnaldo Carvalho de Melo 			       struct perf_sample *sample,
8579ec3f4e4SArnaldo Carvalho de Melo 			       struct machine *machine __maybe_unused)
858419ab0d6SFrederic Weisbecker {
859efc0cdc9SArnaldo Carvalho de Melo 	const char *prev_comm  = evsel__strval(evsel, sample, "prev_comm"),
860efc0cdc9SArnaldo Carvalho de Melo 		   *next_comm  = evsel__strval(evsel, sample, "next_comm");
861efc0cdc9SArnaldo Carvalho de Melo 	const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
862efc0cdc9SArnaldo Carvalho de Melo 		  next_pid = evsel__intval(evsel, sample, "next_pid");
863efc0cdc9SArnaldo Carvalho de Melo 	const u64 prev_state = evsel__intval(evsel, sample, "prev_state");
8641d037ca1SIrina Tirdea 	struct task_desc *prev, __maybe_unused *next;
8657f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp0, timestamp = sample->time;
8667f7f8d0bSArnaldo Carvalho de Melo 	int cpu = sample->cpu;
867fbf94829SIngo Molnar 	s64 delta;
868fbf94829SIngo Molnar 
869bb963e16SNamhyung Kim 	if (verbose > 0)
8702b7fcbc5SArnaldo Carvalho de Melo 		printf("sched_switch event %p\n", evsel);
871ad236fd2SIngo Molnar 
872fbf94829SIngo Molnar 	if (cpu >= MAX_CPUS || cpu < 0)
873a116e05dSArnaldo Carvalho de Melo 		return 0;
874fbf94829SIngo Molnar 
8750e9b07e5SArnaldo Carvalho de Melo 	timestamp0 = sched->cpu_last_switched[cpu];
876fbf94829SIngo Molnar 	if (timestamp0)
877fbf94829SIngo Molnar 		delta = timestamp - timestamp0;
878fbf94829SIngo Molnar 	else
879fbf94829SIngo Molnar 		delta = 0;
880fbf94829SIngo Molnar 
881a116e05dSArnaldo Carvalho de Melo 	if (delta < 0) {
88260b7d14aSNamhyung Kim 		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
883a116e05dSArnaldo Carvalho de Melo 		return -1;
884a116e05dSArnaldo Carvalho de Melo 	}
885fbf94829SIngo Molnar 
8869ec3f4e4SArnaldo Carvalho de Melo 	pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
8879ec3f4e4SArnaldo Carvalho de Melo 		 prev_comm, prev_pid, next_comm, next_pid, delta);
888fbf94829SIngo Molnar 
8899ec3f4e4SArnaldo Carvalho de Melo 	prev = register_pid(sched, prev_pid, prev_comm);
8909ec3f4e4SArnaldo Carvalho de Melo 	next = register_pid(sched, next_pid, next_comm);
891fbf94829SIngo Molnar 
8920e9b07e5SArnaldo Carvalho de Melo 	sched->cpu_last_switched[cpu] = timestamp;
893fbf94829SIngo Molnar 
8940e9b07e5SArnaldo Carvalho de Melo 	add_sched_event_run(sched, prev, timestamp, delta);
8959ec3f4e4SArnaldo Carvalho de Melo 	add_sched_event_sleep(sched, prev, timestamp, prev_state);
896a116e05dSArnaldo Carvalho de Melo 
897a116e05dSArnaldo Carvalho de Melo 	return 0;
898fbf94829SIngo Molnar }
899fbf94829SIngo Molnar 
replay_fork_event(struct perf_sched * sched,union perf_event * event,struct machine * machine)900cb627505SDavid Ahern static int replay_fork_event(struct perf_sched *sched,
901cb627505SDavid Ahern 			     union perf_event *event,
902cb627505SDavid Ahern 			     struct machine *machine)
903419ab0d6SFrederic Weisbecker {
904cb627505SDavid Ahern 	struct thread *child, *parent;
9059ec3f4e4SArnaldo Carvalho de Melo 
906314add6bSAdrian Hunter 	child = machine__findnew_thread(machine, event->fork.pid,
907314add6bSAdrian Hunter 					event->fork.tid);
908314add6bSAdrian Hunter 	parent = machine__findnew_thread(machine, event->fork.ppid,
909314add6bSAdrian Hunter 					 event->fork.ptid);
910cb627505SDavid Ahern 
911cb627505SDavid Ahern 	if (child == NULL || parent == NULL) {
912cb627505SDavid Ahern 		pr_debug("thread does not exist on fork event: child %p, parent %p\n",
913cb627505SDavid Ahern 				 child, parent);
914b91fc39fSArnaldo Carvalho de Melo 		goto out_put;
915419ab0d6SFrederic Weisbecker 	}
9169ec3f4e4SArnaldo Carvalho de Melo 
917bb963e16SNamhyung Kim 	if (verbose > 0) {
918cb627505SDavid Ahern 		printf("fork event\n");
919ee84a303SIan Rogers 		printf("... parent: %s/%d\n", thread__comm_str(parent), thread__tid(parent));
920ee84a303SIan Rogers 		printf("...  child: %s/%d\n", thread__comm_str(child), thread__tid(child));
921cb627505SDavid Ahern 	}
922cb627505SDavid Ahern 
923ee84a303SIan Rogers 	register_pid(sched, thread__tid(parent), thread__comm_str(parent));
924ee84a303SIan Rogers 	register_pid(sched, thread__tid(child), thread__comm_str(child));
925b91fc39fSArnaldo Carvalho de Melo out_put:
926b91fc39fSArnaldo Carvalho de Melo 	thread__put(child);
927b91fc39fSArnaldo Carvalho de Melo 	thread__put(parent);
928a116e05dSArnaldo Carvalho de Melo 	return 0;
929419ab0d6SFrederic Weisbecker }
930419ab0d6SFrederic Weisbecker 
931b1ffe8f3SIngo Molnar struct sort_dimension {
932b1ffe8f3SIngo Molnar 	const char		*name;
933b5fae128SIngo Molnar 	sort_fn_t		cmp;
934b1ffe8f3SIngo Molnar 	struct list_head	list;
935b1ffe8f3SIngo Molnar };
936b1ffe8f3SIngo Molnar 
9378640da9fSChangbin Du /*
9388640da9fSChangbin Du  * handle runtime stats saved per thread
9398640da9fSChangbin Du  */
thread__init_runtime(struct thread * thread)9408640da9fSChangbin Du static struct thread_runtime *thread__init_runtime(struct thread *thread)
9418640da9fSChangbin Du {
9428640da9fSChangbin Du 	struct thread_runtime *r;
9438640da9fSChangbin Du 
9448640da9fSChangbin Du 	r = zalloc(sizeof(struct thread_runtime));
9458640da9fSChangbin Du 	if (!r)
9468640da9fSChangbin Du 		return NULL;
9478640da9fSChangbin Du 
9488640da9fSChangbin Du 	init_stats(&r->run_stats);
9498640da9fSChangbin Du 	thread__set_priv(thread, r);
9508640da9fSChangbin Du 
9518640da9fSChangbin Du 	return r;
9528640da9fSChangbin Du }
9538640da9fSChangbin Du 
thread__get_runtime(struct thread * thread)9548640da9fSChangbin Du static struct thread_runtime *thread__get_runtime(struct thread *thread)
9558640da9fSChangbin Du {
9568640da9fSChangbin Du 	struct thread_runtime *tr;
9578640da9fSChangbin Du 
9588640da9fSChangbin Du 	tr = thread__priv(thread);
9598640da9fSChangbin Du 	if (tr == NULL) {
9608640da9fSChangbin Du 		tr = thread__init_runtime(thread);
9618640da9fSChangbin Du 		if (tr == NULL)
9628640da9fSChangbin Du 			pr_debug("Failed to malloc memory for runtime data.\n");
9638640da9fSChangbin Du 	}
9648640da9fSChangbin Du 
9658640da9fSChangbin Du 	return tr;
9668640da9fSChangbin Du }
9678640da9fSChangbin Du 
968daa1d7a5SFrederic Weisbecker static int
thread_lat_cmp(struct list_head * list,struct work_atoms * l,struct work_atoms * r)96939aeb52fSmingo thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
970daa1d7a5SFrederic Weisbecker {
971daa1d7a5SFrederic Weisbecker 	struct sort_dimension *sort;
972daa1d7a5SFrederic Weisbecker 	int ret = 0;
973daa1d7a5SFrederic Weisbecker 
974b5fae128SIngo Molnar 	BUG_ON(list_empty(list));
975b5fae128SIngo Molnar 
976daa1d7a5SFrederic Weisbecker 	list_for_each_entry(sort, list, list) {
977daa1d7a5SFrederic Weisbecker 		ret = sort->cmp(l, r);
978daa1d7a5SFrederic Weisbecker 		if (ret)
979daa1d7a5SFrederic Weisbecker 			return ret;
980daa1d7a5SFrederic Weisbecker 	}
981daa1d7a5SFrederic Weisbecker 
982daa1d7a5SFrederic Weisbecker 	return ret;
983daa1d7a5SFrederic Weisbecker }
984daa1d7a5SFrederic Weisbecker 
98539aeb52fSmingo static struct work_atoms *
thread_atoms_search(struct rb_root_cached * root,struct thread * thread,struct list_head * sort_list)986cb4c13a5SDavidlohr Bueso thread_atoms_search(struct rb_root_cached *root, struct thread *thread,
987b5fae128SIngo Molnar 			 struct list_head *sort_list)
988b5fae128SIngo Molnar {
989cb4c13a5SDavidlohr Bueso 	struct rb_node *node = root->rb_root.rb_node;
99039aeb52fSmingo 	struct work_atoms key = { .thread = thread };
991b5fae128SIngo Molnar 
992b5fae128SIngo Molnar 	while (node) {
99339aeb52fSmingo 		struct work_atoms *atoms;
994b5fae128SIngo Molnar 		int cmp;
995b5fae128SIngo Molnar 
99639aeb52fSmingo 		atoms = container_of(node, struct work_atoms, node);
997b5fae128SIngo Molnar 
998b5fae128SIngo Molnar 		cmp = thread_lat_cmp(sort_list, &key, atoms);
999b5fae128SIngo Molnar 		if (cmp > 0)
1000b5fae128SIngo Molnar 			node = node->rb_left;
1001b5fae128SIngo Molnar 		else if (cmp < 0)
1002b5fae128SIngo Molnar 			node = node->rb_right;
1003b5fae128SIngo Molnar 		else {
1004b5fae128SIngo Molnar 			BUG_ON(thread != atoms->thread);
1005b5fae128SIngo Molnar 			return atoms;
1006b5fae128SIngo Molnar 		}
1007b5fae128SIngo Molnar 	}
1008b5fae128SIngo Molnar 	return NULL;
1009b5fae128SIngo Molnar }
1010b5fae128SIngo Molnar 
1011cdce9d73SFrederic Weisbecker static void
__thread_latency_insert(struct rb_root_cached * root,struct work_atoms * data,struct list_head * sort_list)1012cb4c13a5SDavidlohr Bueso __thread_latency_insert(struct rb_root_cached *root, struct work_atoms *data,
1013daa1d7a5SFrederic Weisbecker 			 struct list_head *sort_list)
1014cdce9d73SFrederic Weisbecker {
1015cb4c13a5SDavidlohr Bueso 	struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
1016cb4c13a5SDavidlohr Bueso 	bool leftmost = true;
1017cdce9d73SFrederic Weisbecker 
1018cdce9d73SFrederic Weisbecker 	while (*new) {
101939aeb52fSmingo 		struct work_atoms *this;
1020daa1d7a5SFrederic Weisbecker 		int cmp;
1021cdce9d73SFrederic Weisbecker 
102239aeb52fSmingo 		this = container_of(*new, struct work_atoms, node);
1023cdce9d73SFrederic Weisbecker 		parent = *new;
1024daa1d7a5SFrederic Weisbecker 
1025daa1d7a5SFrederic Weisbecker 		cmp = thread_lat_cmp(sort_list, data, this);
1026daa1d7a5SFrederic Weisbecker 
1027daa1d7a5SFrederic Weisbecker 		if (cmp > 0)
1028cdce9d73SFrederic Weisbecker 			new = &((*new)->rb_left);
1029cb4c13a5SDavidlohr Bueso 		else {
1030daa1d7a5SFrederic Weisbecker 			new = &((*new)->rb_right);
1031cb4c13a5SDavidlohr Bueso 			leftmost = false;
1032cb4c13a5SDavidlohr Bueso 		}
1033cdce9d73SFrederic Weisbecker 	}
1034cdce9d73SFrederic Weisbecker 
1035cdce9d73SFrederic Weisbecker 	rb_link_node(&data->node, parent, new);
1036cb4c13a5SDavidlohr Bueso 	rb_insert_color_cached(&data->node, root, leftmost);
1037cdce9d73SFrederic Weisbecker }
1038cdce9d73SFrederic Weisbecker 
thread_atoms_insert(struct perf_sched * sched,struct thread * thread)10390e9b07e5SArnaldo Carvalho de Melo static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
1040cdce9d73SFrederic Weisbecker {
104136479484SArnaldo Carvalho de Melo 	struct work_atoms *atoms = zalloc(sizeof(*atoms));
1042a116e05dSArnaldo Carvalho de Melo 	if (!atoms) {
1043a116e05dSArnaldo Carvalho de Melo 		pr_err("No memory at %s\n", __func__);
1044a116e05dSArnaldo Carvalho de Melo 		return -1;
1045a116e05dSArnaldo Carvalho de Melo 	}
1046cdce9d73SFrederic Weisbecker 
1047f3b623b8SArnaldo Carvalho de Melo 	atoms->thread = thread__get(thread);
104839aeb52fSmingo 	INIT_LIST_HEAD(&atoms->work_list);
10490e9b07e5SArnaldo Carvalho de Melo 	__thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
1050a116e05dSArnaldo Carvalho de Melo 	return 0;
1051cdce9d73SFrederic Weisbecker }
1052cdce9d73SFrederic Weisbecker 
sched_out_state(u64 prev_state)10539ec3f4e4SArnaldo Carvalho de Melo static char sched_out_state(u64 prev_state)
1054cdce9d73SFrederic Weisbecker {
1055cdce9d73SFrederic Weisbecker 	const char *str = TASK_STATE_TO_CHAR_STR;
1056cdce9d73SFrederic Weisbecker 
10579ec3f4e4SArnaldo Carvalho de Melo 	return str[prev_state];
1058cdce9d73SFrederic Weisbecker }
1059cdce9d73SFrederic Weisbecker 
1060a116e05dSArnaldo Carvalho de Melo static int
add_sched_out_event(struct work_atoms * atoms,char run_state,u64 timestamp)106139aeb52fSmingo add_sched_out_event(struct work_atoms *atoms,
106239aeb52fSmingo 		    char run_state,
1063c6ced611SFrederic Weisbecker 		    u64 timestamp)
1064cdce9d73SFrederic Weisbecker {
106536479484SArnaldo Carvalho de Melo 	struct work_atom *atom = zalloc(sizeof(*atom));
1066a116e05dSArnaldo Carvalho de Melo 	if (!atom) {
1067a116e05dSArnaldo Carvalho de Melo 		pr_err("Non memory at %s", __func__);
1068a116e05dSArnaldo Carvalho de Melo 		return -1;
1069a116e05dSArnaldo Carvalho de Melo 	}
1070cdce9d73SFrederic Weisbecker 
1071aa1ab9d2SFrederic Weisbecker 	atom->sched_out_time = timestamp;
1072aa1ab9d2SFrederic Weisbecker 
107339aeb52fSmingo 	if (run_state == 'R') {
1074b1ffe8f3SIngo Molnar 		atom->state = THREAD_WAIT_CPU;
1075aa1ab9d2SFrederic Weisbecker 		atom->wake_up_time = atom->sched_out_time;
1076c6ced611SFrederic Weisbecker 	}
1077c6ced611SFrederic Weisbecker 
107839aeb52fSmingo 	list_add_tail(&atom->list, &atoms->work_list);
1079a116e05dSArnaldo Carvalho de Melo 	return 0;
1080cdce9d73SFrederic Weisbecker }
1081cdce9d73SFrederic Weisbecker 
1082cdce9d73SFrederic Weisbecker static void
add_runtime_event(struct work_atoms * atoms,u64 delta,u64 timestamp __maybe_unused)10831d037ca1SIrina Tirdea add_runtime_event(struct work_atoms *atoms, u64 delta,
10841d037ca1SIrina Tirdea 		  u64 timestamp __maybe_unused)
108539aeb52fSmingo {
108639aeb52fSmingo 	struct work_atom *atom;
108739aeb52fSmingo 
108839aeb52fSmingo 	BUG_ON(list_empty(&atoms->work_list));
108939aeb52fSmingo 
109039aeb52fSmingo 	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
109139aeb52fSmingo 
109239aeb52fSmingo 	atom->runtime += delta;
109339aeb52fSmingo 	atoms->total_runtime += delta;
109439aeb52fSmingo }
109539aeb52fSmingo 
109639aeb52fSmingo static void
add_sched_in_event(struct work_atoms * atoms,u64 timestamp)109739aeb52fSmingo add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1098cdce9d73SFrederic Weisbecker {
1099b1ffe8f3SIngo Molnar 	struct work_atom *atom;
110066685678SFrederic Weisbecker 	u64 delta;
1101cdce9d73SFrederic Weisbecker 
110239aeb52fSmingo 	if (list_empty(&atoms->work_list))
1103cdce9d73SFrederic Weisbecker 		return;
1104cdce9d73SFrederic Weisbecker 
110539aeb52fSmingo 	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1106cdce9d73SFrederic Weisbecker 
1107b1ffe8f3SIngo Molnar 	if (atom->state != THREAD_WAIT_CPU)
1108cdce9d73SFrederic Weisbecker 		return;
1109cdce9d73SFrederic Weisbecker 
1110b1ffe8f3SIngo Molnar 	if (timestamp < atom->wake_up_time) {
1111b1ffe8f3SIngo Molnar 		atom->state = THREAD_IGNORE;
1112cdce9d73SFrederic Weisbecker 		return;
1113cdce9d73SFrederic Weisbecker 	}
1114cdce9d73SFrederic Weisbecker 
1115b1ffe8f3SIngo Molnar 	atom->state = THREAD_SCHED_IN;
1116b1ffe8f3SIngo Molnar 	atom->sched_in_time = timestamp;
111766685678SFrederic Weisbecker 
1118b1ffe8f3SIngo Molnar 	delta = atom->sched_in_time - atom->wake_up_time;
111966685678SFrederic Weisbecker 	atoms->total_lat += delta;
11203786310aSFrederic Weisbecker 	if (delta > atoms->max_lat) {
112166685678SFrederic Weisbecker 		atoms->max_lat = delta;
1122dc000c45SJoel Fernandes (Google) 		atoms->max_lat_start = atom->wake_up_time;
1123dc000c45SJoel Fernandes (Google) 		atoms->max_lat_end = timestamp;
11243786310aSFrederic Weisbecker 	}
112566685678SFrederic Weisbecker 	atoms->nb_atoms++;
1126cdce9d73SFrederic Weisbecker }
1127cdce9d73SFrederic Weisbecker 
latency_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)11289ec3f4e4SArnaldo Carvalho de Melo static int latency_switch_event(struct perf_sched *sched,
112932dcd021SJiri Olsa 				struct evsel *evsel,
11309ec3f4e4SArnaldo Carvalho de Melo 				struct perf_sample *sample,
11319ec3f4e4SArnaldo Carvalho de Melo 				struct machine *machine)
1132cdce9d73SFrederic Weisbecker {
1133efc0cdc9SArnaldo Carvalho de Melo 	const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
1134efc0cdc9SArnaldo Carvalho de Melo 		  next_pid = evsel__intval(evsel, sample, "next_pid");
1135efc0cdc9SArnaldo Carvalho de Melo 	const u64 prev_state = evsel__intval(evsel, sample, "prev_state");
113639aeb52fSmingo 	struct work_atoms *out_events, *in_events;
1137cdce9d73SFrederic Weisbecker 	struct thread *sched_out, *sched_in;
11387f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp0, timestamp = sample->time;
1139b91fc39fSArnaldo Carvalho de Melo 	int cpu = sample->cpu, err = -1;
1140ea92ed5aSIngo Molnar 	s64 delta;
1141ea92ed5aSIngo Molnar 
114239aeb52fSmingo 	BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1143ea92ed5aSIngo Molnar 
11440e9b07e5SArnaldo Carvalho de Melo 	timestamp0 = sched->cpu_last_switched[cpu];
11450e9b07e5SArnaldo Carvalho de Melo 	sched->cpu_last_switched[cpu] = timestamp;
1146ea92ed5aSIngo Molnar 	if (timestamp0)
1147ea92ed5aSIngo Molnar 		delta = timestamp - timestamp0;
1148ea92ed5aSIngo Molnar 	else
1149ea92ed5aSIngo Molnar 		delta = 0;
1150ea92ed5aSIngo Molnar 
1151a116e05dSArnaldo Carvalho de Melo 	if (delta < 0) {
1152a116e05dSArnaldo Carvalho de Melo 		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1153a116e05dSArnaldo Carvalho de Melo 		return -1;
1154a116e05dSArnaldo Carvalho de Melo 	}
1155cdce9d73SFrederic Weisbecker 
11561fcb8768SAdrian Hunter 	sched_out = machine__findnew_thread(machine, -1, prev_pid);
11571fcb8768SAdrian Hunter 	sched_in = machine__findnew_thread(machine, -1, next_pid);
1158b91fc39fSArnaldo Carvalho de Melo 	if (sched_out == NULL || sched_in == NULL)
1159b91fc39fSArnaldo Carvalho de Melo 		goto out_put;
1160cdce9d73SFrederic Weisbecker 
11610e9b07e5SArnaldo Carvalho de Melo 	out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
116239aeb52fSmingo 	if (!out_events) {
11630e9b07e5SArnaldo Carvalho de Melo 		if (thread_atoms_insert(sched, sched_out))
1164b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
11650e9b07e5SArnaldo Carvalho de Melo 		out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
1166a116e05dSArnaldo Carvalho de Melo 		if (!out_events) {
1167a116e05dSArnaldo Carvalho de Melo 			pr_err("out-event: Internal tree error");
1168b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
116939aeb52fSmingo 		}
1170a116e05dSArnaldo Carvalho de Melo 	}
11719ec3f4e4SArnaldo Carvalho de Melo 	if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp))
1172a116e05dSArnaldo Carvalho de Melo 		return -1;
117339aeb52fSmingo 
11740e9b07e5SArnaldo Carvalho de Melo 	in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
117539aeb52fSmingo 	if (!in_events) {
11760e9b07e5SArnaldo Carvalho de Melo 		if (thread_atoms_insert(sched, sched_in))
1177b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
11780e9b07e5SArnaldo Carvalho de Melo 		in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
1179a116e05dSArnaldo Carvalho de Melo 		if (!in_events) {
1180a116e05dSArnaldo Carvalho de Melo 			pr_err("in-event: Internal tree error");
1181b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1182a116e05dSArnaldo Carvalho de Melo 		}
118339aeb52fSmingo 		/*
118439aeb52fSmingo 		 * Take came in we have not heard about yet,
118539aeb52fSmingo 		 * add in an initial atom in runnable state:
118639aeb52fSmingo 		 */
1187a116e05dSArnaldo Carvalho de Melo 		if (add_sched_out_event(in_events, 'R', timestamp))
1188b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
118939aeb52fSmingo 	}
119039aeb52fSmingo 	add_sched_in_event(in_events, timestamp);
1191b91fc39fSArnaldo Carvalho de Melo 	err = 0;
1192b91fc39fSArnaldo Carvalho de Melo out_put:
1193b91fc39fSArnaldo Carvalho de Melo 	thread__put(sched_out);
1194b91fc39fSArnaldo Carvalho de Melo 	thread__put(sched_in);
1195b91fc39fSArnaldo Carvalho de Melo 	return err;
1196cdce9d73SFrederic Weisbecker }
1197cdce9d73SFrederic Weisbecker 
latency_runtime_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)11989ec3f4e4SArnaldo Carvalho de Melo static int latency_runtime_event(struct perf_sched *sched,
119932dcd021SJiri Olsa 				 struct evsel *evsel,
12009ec3f4e4SArnaldo Carvalho de Melo 				 struct perf_sample *sample,
12019ec3f4e4SArnaldo Carvalho de Melo 				 struct machine *machine)
120239aeb52fSmingo {
1203efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid	   = evsel__intval(evsel, sample, "pid");
1204efc0cdc9SArnaldo Carvalho de Melo 	const u64 runtime  = evsel__intval(evsel, sample, "runtime");
12051fcb8768SAdrian Hunter 	struct thread *thread = machine__findnew_thread(machine, -1, pid);
12060e9b07e5SArnaldo Carvalho de Melo 	struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
12077f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp = sample->time;
1208b91fc39fSArnaldo Carvalho de Melo 	int cpu = sample->cpu, err = -1;
1209b91fc39fSArnaldo Carvalho de Melo 
1210b91fc39fSArnaldo Carvalho de Melo 	if (thread == NULL)
1211b91fc39fSArnaldo Carvalho de Melo 		return -1;
121239aeb52fSmingo 
121339aeb52fSmingo 	BUG_ON(cpu >= MAX_CPUS || cpu < 0);
121439aeb52fSmingo 	if (!atoms) {
12150e9b07e5SArnaldo Carvalho de Melo 		if (thread_atoms_insert(sched, thread))
1216b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
12170e9b07e5SArnaldo Carvalho de Melo 		atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
1218a116e05dSArnaldo Carvalho de Melo 		if (!atoms) {
121960b7d14aSNamhyung Kim 			pr_err("in-event: Internal tree error");
1220b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1221a116e05dSArnaldo Carvalho de Melo 		}
1222a116e05dSArnaldo Carvalho de Melo 		if (add_sched_out_event(atoms, 'R', timestamp))
1223b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
122439aeb52fSmingo 	}
122539aeb52fSmingo 
12269ec3f4e4SArnaldo Carvalho de Melo 	add_runtime_event(atoms, runtime, timestamp);
1227b91fc39fSArnaldo Carvalho de Melo 	err = 0;
1228b91fc39fSArnaldo Carvalho de Melo out_put:
1229b91fc39fSArnaldo Carvalho de Melo 	thread__put(thread);
1230b91fc39fSArnaldo Carvalho de Melo 	return err;
1231cdce9d73SFrederic Weisbecker }
1232cdce9d73SFrederic Weisbecker 
latency_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)12339ec3f4e4SArnaldo Carvalho de Melo static int latency_wakeup_event(struct perf_sched *sched,
123432dcd021SJiri Olsa 				struct evsel *evsel,
12359ec3f4e4SArnaldo Carvalho de Melo 				struct perf_sample *sample,
12369ec3f4e4SArnaldo Carvalho de Melo 				struct machine *machine)
1237cdce9d73SFrederic Weisbecker {
1238efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid	  = evsel__intval(evsel, sample, "pid");
123939aeb52fSmingo 	struct work_atoms *atoms;
1240b1ffe8f3SIngo Molnar 	struct work_atom *atom;
1241cdce9d73SFrederic Weisbecker 	struct thread *wakee;
12427f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp = sample->time;
1243b91fc39fSArnaldo Carvalho de Melo 	int err = -1;
1244cdce9d73SFrederic Weisbecker 
12451fcb8768SAdrian Hunter 	wakee = machine__findnew_thread(machine, -1, pid);
1246b91fc39fSArnaldo Carvalho de Melo 	if (wakee == NULL)
1247b91fc39fSArnaldo Carvalho de Melo 		return -1;
12480e9b07e5SArnaldo Carvalho de Melo 	atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
124917562205SFrederic Weisbecker 	if (!atoms) {
12500e9b07e5SArnaldo Carvalho de Melo 		if (thread_atoms_insert(sched, wakee))
1251b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
12520e9b07e5SArnaldo Carvalho de Melo 		atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
1253a116e05dSArnaldo Carvalho de Melo 		if (!atoms) {
125460b7d14aSNamhyung Kim 			pr_err("wakeup-event: Internal tree error");
1255b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1256a116e05dSArnaldo Carvalho de Melo 		}
1257a116e05dSArnaldo Carvalho de Melo 		if (add_sched_out_event(atoms, 'S', timestamp))
1258b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1259cdce9d73SFrederic Weisbecker 	}
1260cdce9d73SFrederic Weisbecker 
126139aeb52fSmingo 	BUG_ON(list_empty(&atoms->work_list));
1262cdce9d73SFrederic Weisbecker 
126339aeb52fSmingo 	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1264cdce9d73SFrederic Weisbecker 
126555ffb7a6SMike Galbraith 	/*
126667d6259dSDongsheng Yang 	 * As we do not guarantee the wakeup event happens when
126767d6259dSDongsheng Yang 	 * task is out of run queue, also may happen when task is
126867d6259dSDongsheng Yang 	 * on run queue and wakeup only change ->state to TASK_RUNNING,
126967d6259dSDongsheng Yang 	 * then we should not set the ->wake_up_time when wake up a
127067d6259dSDongsheng Yang 	 * task which is on run queue.
127167d6259dSDongsheng Yang 	 *
127255ffb7a6SMike Galbraith 	 * You WILL be missing events if you've recorded only
127355ffb7a6SMike Galbraith 	 * one CPU, or are only looking at only one, so don't
127467d6259dSDongsheng Yang 	 * skip in this case.
127555ffb7a6SMike Galbraith 	 */
12760e9b07e5SArnaldo Carvalho de Melo 	if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1277b91fc39fSArnaldo Carvalho de Melo 		goto out_ok;
1278cdce9d73SFrederic Weisbecker 
12790e9b07e5SArnaldo Carvalho de Melo 	sched->nr_timestamps++;
1280ea57c4f5SIngo Molnar 	if (atom->sched_out_time > timestamp) {
12810e9b07e5SArnaldo Carvalho de Melo 		sched->nr_unordered_timestamps++;
1282b91fc39fSArnaldo Carvalho de Melo 		goto out_ok;
1283ea57c4f5SIngo Molnar 	}
1284aa1ab9d2SFrederic Weisbecker 
1285b1ffe8f3SIngo Molnar 	atom->state = THREAD_WAIT_CPU;
1286b1ffe8f3SIngo Molnar 	atom->wake_up_time = timestamp;
1287b91fc39fSArnaldo Carvalho de Melo out_ok:
1288b91fc39fSArnaldo Carvalho de Melo 	err = 0;
1289b91fc39fSArnaldo Carvalho de Melo out_put:
1290b91fc39fSArnaldo Carvalho de Melo 	thread__put(wakee);
1291b91fc39fSArnaldo Carvalho de Melo 	return err;
1292cdce9d73SFrederic Weisbecker }
1293cdce9d73SFrederic Weisbecker 
latency_migrate_task_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)12949ec3f4e4SArnaldo Carvalho de Melo static int latency_migrate_task_event(struct perf_sched *sched,
129532dcd021SJiri Olsa 				      struct evsel *evsel,
12969ec3f4e4SArnaldo Carvalho de Melo 				      struct perf_sample *sample,
12979ec3f4e4SArnaldo Carvalho de Melo 				      struct machine *machine)
129855ffb7a6SMike Galbraith {
1299efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid = evsel__intval(evsel, sample, "pid");
13007f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp = sample->time;
130155ffb7a6SMike Galbraith 	struct work_atoms *atoms;
130255ffb7a6SMike Galbraith 	struct work_atom *atom;
130355ffb7a6SMike Galbraith 	struct thread *migrant;
1304b91fc39fSArnaldo Carvalho de Melo 	int err = -1;
130555ffb7a6SMike Galbraith 
130655ffb7a6SMike Galbraith 	/*
130755ffb7a6SMike Galbraith 	 * Only need to worry about migration when profiling one CPU.
130855ffb7a6SMike Galbraith 	 */
13090e9b07e5SArnaldo Carvalho de Melo 	if (sched->profile_cpu == -1)
1310a116e05dSArnaldo Carvalho de Melo 		return 0;
131155ffb7a6SMike Galbraith 
13121fcb8768SAdrian Hunter 	migrant = machine__findnew_thread(machine, -1, pid);
1313b91fc39fSArnaldo Carvalho de Melo 	if (migrant == NULL)
1314b91fc39fSArnaldo Carvalho de Melo 		return -1;
13150e9b07e5SArnaldo Carvalho de Melo 	atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
131655ffb7a6SMike Galbraith 	if (!atoms) {
13170e9b07e5SArnaldo Carvalho de Melo 		if (thread_atoms_insert(sched, migrant))
1318b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1319ee84a303SIan Rogers 		register_pid(sched, thread__tid(migrant), thread__comm_str(migrant));
13200e9b07e5SArnaldo Carvalho de Melo 		atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
1321a116e05dSArnaldo Carvalho de Melo 		if (!atoms) {
132260b7d14aSNamhyung Kim 			pr_err("migration-event: Internal tree error");
1323b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
1324a116e05dSArnaldo Carvalho de Melo 		}
1325a116e05dSArnaldo Carvalho de Melo 		if (add_sched_out_event(atoms, 'R', timestamp))
1326b91fc39fSArnaldo Carvalho de Melo 			goto out_put;
132755ffb7a6SMike Galbraith 	}
132855ffb7a6SMike Galbraith 
132955ffb7a6SMike Galbraith 	BUG_ON(list_empty(&atoms->work_list));
133055ffb7a6SMike Galbraith 
133155ffb7a6SMike Galbraith 	atom = list_entry(atoms->work_list.prev, struct work_atom, list);
133255ffb7a6SMike Galbraith 	atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
133355ffb7a6SMike Galbraith 
13340e9b07e5SArnaldo Carvalho de Melo 	sched->nr_timestamps++;
133555ffb7a6SMike Galbraith 
133655ffb7a6SMike Galbraith 	if (atom->sched_out_time > timestamp)
13370e9b07e5SArnaldo Carvalho de Melo 		sched->nr_unordered_timestamps++;
1338b91fc39fSArnaldo Carvalho de Melo 	err = 0;
1339b91fc39fSArnaldo Carvalho de Melo out_put:
1340b91fc39fSArnaldo Carvalho de Melo 	thread__put(migrant);
1341b91fc39fSArnaldo Carvalho de Melo 	return err;
134255ffb7a6SMike Galbraith }
134355ffb7a6SMike Galbraith 
output_lat_thread(struct perf_sched * sched,struct work_atoms * work_list)13440e9b07e5SArnaldo Carvalho de Melo static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
1345cdce9d73SFrederic Weisbecker {
1346cdce9d73SFrederic Weisbecker 	int i;
1347cdce9d73SFrederic Weisbecker 	int ret;
134866685678SFrederic Weisbecker 	u64 avg;
1349dc000c45SJoel Fernandes (Google) 	char max_lat_start[32], max_lat_end[32];
1350cdce9d73SFrederic Weisbecker 
135139aeb52fSmingo 	if (!work_list->nb_atoms)
1352cdce9d73SFrederic Weisbecker 		return;
1353ea57c4f5SIngo Molnar 	/*
1354ea57c4f5SIngo Molnar 	 * Ignore idle threads:
1355ea57c4f5SIngo Molnar 	 */
1356b9c5143aSFrederic Weisbecker 	if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
1357ea57c4f5SIngo Molnar 		return;
1358cdce9d73SFrederic Weisbecker 
13590e9b07e5SArnaldo Carvalho de Melo 	sched->all_runtime += work_list->total_runtime;
13600e9b07e5SArnaldo Carvalho de Melo 	sched->all_count   += work_list->nb_atoms;
136166685678SFrederic Weisbecker 
1362ee84a303SIan Rogers 	if (work_list->num_merged > 1) {
1363ee84a303SIan Rogers 		ret = printf("  %s:(%d) ", thread__comm_str(work_list->thread),
1364ee84a303SIan Rogers 			     work_list->num_merged);
1365ee84a303SIan Rogers 	} else {
1366ee84a303SIan Rogers 		ret = printf("  %s:%d ", thread__comm_str(work_list->thread),
1367ee84a303SIan Rogers 			     thread__tid(work_list->thread));
1368ee84a303SIan Rogers 	}
1369cdce9d73SFrederic Weisbecker 
137008f69e6cSmingo 	for (i = 0; i < 24 - ret; i++)
1371cdce9d73SFrederic Weisbecker 		printf(" ");
1372cdce9d73SFrederic Weisbecker 
137339aeb52fSmingo 	avg = work_list->total_lat / work_list->nb_atoms;
1374dc000c45SJoel Fernandes (Google) 	timestamp__scnprintf_usec(work_list->max_lat_start, max_lat_start, sizeof(max_lat_start));
1375dc000c45SJoel Fernandes (Google) 	timestamp__scnprintf_usec(work_list->max_lat_end, max_lat_end, sizeof(max_lat_end));
1376cdce9d73SFrederic Weisbecker 
1377dc000c45SJoel Fernandes (Google) 	printf("|%11.3f ms |%9" PRIu64 " | avg:%8.3f ms | max:%8.3f ms | max start: %12s s | max end: %12s s\n",
13784fc76e49SArnaldo Carvalho de Melo 	      (double)work_list->total_runtime / NSEC_PER_MSEC,
13794fc76e49SArnaldo Carvalho de Melo 		 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
13804fc76e49SArnaldo Carvalho de Melo 		 (double)work_list->max_lat / NSEC_PER_MSEC,
1381dc000c45SJoel Fernandes (Google) 		 max_lat_start, max_lat_end);
1382cdce9d73SFrederic Weisbecker }
1383cdce9d73SFrederic Weisbecker 
pid_cmp(struct work_atoms * l,struct work_atoms * r)138439aeb52fSmingo static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1385daa1d7a5SFrederic Weisbecker {
1386ee84a303SIan Rogers 	pid_t l_tid, r_tid;
1387ee84a303SIan Rogers 
1388f6005cafSIan Rogers 	if (RC_CHK_ACCESS(l->thread) == RC_CHK_ACCESS(r->thread))
13890014de17SJiri Olsa 		return 0;
1390ee84a303SIan Rogers 	l_tid = thread__tid(l->thread);
1391ee84a303SIan Rogers 	r_tid = thread__tid(r->thread);
1392ee84a303SIan Rogers 	if (l_tid < r_tid)
1393daa1d7a5SFrederic Weisbecker 		return -1;
1394ee84a303SIan Rogers 	if (l_tid > r_tid)
1395daa1d7a5SFrederic Weisbecker 		return 1;
1396f6005cafSIan Rogers 	return (int)(RC_CHK_ACCESS(l->thread) - RC_CHK_ACCESS(r->thread));
1397daa1d7a5SFrederic Weisbecker }
1398daa1d7a5SFrederic Weisbecker 
avg_cmp(struct work_atoms * l,struct work_atoms * r)139939aeb52fSmingo static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1400daa1d7a5SFrederic Weisbecker {
1401daa1d7a5SFrederic Weisbecker 	u64 avgl, avgr;
1402daa1d7a5SFrederic Weisbecker 
1403daa1d7a5SFrederic Weisbecker 	if (!l->nb_atoms)
1404daa1d7a5SFrederic Weisbecker 		return -1;
1405daa1d7a5SFrederic Weisbecker 
1406daa1d7a5SFrederic Weisbecker 	if (!r->nb_atoms)
1407daa1d7a5SFrederic Weisbecker 		return 1;
1408daa1d7a5SFrederic Weisbecker 
1409daa1d7a5SFrederic Weisbecker 	avgl = l->total_lat / l->nb_atoms;
1410daa1d7a5SFrederic Weisbecker 	avgr = r->total_lat / r->nb_atoms;
1411daa1d7a5SFrederic Weisbecker 
1412daa1d7a5SFrederic Weisbecker 	if (avgl < avgr)
1413daa1d7a5SFrederic Weisbecker 		return -1;
1414daa1d7a5SFrederic Weisbecker 	if (avgl > avgr)
1415daa1d7a5SFrederic Weisbecker 		return 1;
1416daa1d7a5SFrederic Weisbecker 
1417daa1d7a5SFrederic Weisbecker 	return 0;
1418daa1d7a5SFrederic Weisbecker }
1419daa1d7a5SFrederic Weisbecker 
max_cmp(struct work_atoms * l,struct work_atoms * r)142039aeb52fSmingo static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1421daa1d7a5SFrederic Weisbecker {
1422daa1d7a5SFrederic Weisbecker 	if (l->max_lat < r->max_lat)
1423daa1d7a5SFrederic Weisbecker 		return -1;
1424daa1d7a5SFrederic Weisbecker 	if (l->max_lat > r->max_lat)
1425daa1d7a5SFrederic Weisbecker 		return 1;
1426daa1d7a5SFrederic Weisbecker 
1427daa1d7a5SFrederic Weisbecker 	return 0;
1428daa1d7a5SFrederic Weisbecker }
1429daa1d7a5SFrederic Weisbecker 
switch_cmp(struct work_atoms * l,struct work_atoms * r)143039aeb52fSmingo static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1431daa1d7a5SFrederic Weisbecker {
1432daa1d7a5SFrederic Weisbecker 	if (l->nb_atoms < r->nb_atoms)
1433daa1d7a5SFrederic Weisbecker 		return -1;
1434daa1d7a5SFrederic Weisbecker 	if (l->nb_atoms > r->nb_atoms)
1435daa1d7a5SFrederic Weisbecker 		return 1;
1436daa1d7a5SFrederic Weisbecker 
1437daa1d7a5SFrederic Weisbecker 	return 0;
1438daa1d7a5SFrederic Weisbecker }
1439daa1d7a5SFrederic Weisbecker 
runtime_cmp(struct work_atoms * l,struct work_atoms * r)144039aeb52fSmingo static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1441daa1d7a5SFrederic Weisbecker {
1442daa1d7a5SFrederic Weisbecker 	if (l->total_runtime < r->total_runtime)
1443daa1d7a5SFrederic Weisbecker 		return -1;
1444daa1d7a5SFrederic Weisbecker 	if (l->total_runtime > r->total_runtime)
1445daa1d7a5SFrederic Weisbecker 		return 1;
1446daa1d7a5SFrederic Weisbecker 
1447daa1d7a5SFrederic Weisbecker 	return 0;
1448daa1d7a5SFrederic Weisbecker }
1449daa1d7a5SFrederic Weisbecker 
sort_dimension__add(const char * tok,struct list_head * list)14500e9b07e5SArnaldo Carvalho de Melo static int sort_dimension__add(const char *tok, struct list_head *list)
14510e9b07e5SArnaldo Carvalho de Melo {
14520e9b07e5SArnaldo Carvalho de Melo 	size_t i;
14530e9b07e5SArnaldo Carvalho de Melo 	static struct sort_dimension avg_sort_dimension = {
14540e9b07e5SArnaldo Carvalho de Melo 		.name = "avg",
14550e9b07e5SArnaldo Carvalho de Melo 		.cmp  = avg_cmp,
14560e9b07e5SArnaldo Carvalho de Melo 	};
14570e9b07e5SArnaldo Carvalho de Melo 	static struct sort_dimension max_sort_dimension = {
14580e9b07e5SArnaldo Carvalho de Melo 		.name = "max",
14590e9b07e5SArnaldo Carvalho de Melo 		.cmp  = max_cmp,
14600e9b07e5SArnaldo Carvalho de Melo 	};
14610e9b07e5SArnaldo Carvalho de Melo 	static struct sort_dimension pid_sort_dimension = {
14620e9b07e5SArnaldo Carvalho de Melo 		.name = "pid",
14630e9b07e5SArnaldo Carvalho de Melo 		.cmp  = pid_cmp,
14640e9b07e5SArnaldo Carvalho de Melo 	};
1465daa1d7a5SFrederic Weisbecker 	static struct sort_dimension runtime_sort_dimension = {
1466daa1d7a5SFrederic Weisbecker 		.name = "runtime",
1467daa1d7a5SFrederic Weisbecker 		.cmp  = runtime_cmp,
1468daa1d7a5SFrederic Weisbecker 	};
14690e9b07e5SArnaldo Carvalho de Melo 	static struct sort_dimension switch_sort_dimension = {
14700e9b07e5SArnaldo Carvalho de Melo 		.name = "switch",
14710e9b07e5SArnaldo Carvalho de Melo 		.cmp  = switch_cmp,
14720e9b07e5SArnaldo Carvalho de Melo 	};
14730e9b07e5SArnaldo Carvalho de Melo 	struct sort_dimension *available_sorts[] = {
1474daa1d7a5SFrederic Weisbecker 		&pid_sort_dimension,
1475daa1d7a5SFrederic Weisbecker 		&avg_sort_dimension,
1476daa1d7a5SFrederic Weisbecker 		&max_sort_dimension,
1477daa1d7a5SFrederic Weisbecker 		&switch_sort_dimension,
1478daa1d7a5SFrederic Weisbecker 		&runtime_sort_dimension,
1479daa1d7a5SFrederic Weisbecker 	};
1480daa1d7a5SFrederic Weisbecker 
14810e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
1482daa1d7a5SFrederic Weisbecker 		if (!strcmp(available_sorts[i]->name, tok)) {
1483daa1d7a5SFrederic Weisbecker 			list_add_tail(&available_sorts[i]->list, list);
1484daa1d7a5SFrederic Weisbecker 
1485daa1d7a5SFrederic Weisbecker 			return 0;
1486daa1d7a5SFrederic Weisbecker 		}
1487daa1d7a5SFrederic Weisbecker 	}
1488daa1d7a5SFrederic Weisbecker 
1489daa1d7a5SFrederic Weisbecker 	return -1;
1490daa1d7a5SFrederic Weisbecker }
1491daa1d7a5SFrederic Weisbecker 
perf_sched__sort_lat(struct perf_sched * sched)14920e9b07e5SArnaldo Carvalho de Melo static void perf_sched__sort_lat(struct perf_sched *sched)
1493daa1d7a5SFrederic Weisbecker {
1494daa1d7a5SFrederic Weisbecker 	struct rb_node *node;
1495cb4c13a5SDavidlohr Bueso 	struct rb_root_cached *root = &sched->atom_root;
14962f80dd44SJosef Bacik again:
1497daa1d7a5SFrederic Weisbecker 	for (;;) {
149839aeb52fSmingo 		struct work_atoms *data;
1499cb4c13a5SDavidlohr Bueso 		node = rb_first_cached(root);
1500daa1d7a5SFrederic Weisbecker 		if (!node)
1501daa1d7a5SFrederic Weisbecker 			break;
1502daa1d7a5SFrederic Weisbecker 
1503cb4c13a5SDavidlohr Bueso 		rb_erase_cached(node, root);
150439aeb52fSmingo 		data = rb_entry(node, struct work_atoms, node);
15050e9b07e5SArnaldo Carvalho de Melo 		__thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
1506daa1d7a5SFrederic Weisbecker 	}
15072f80dd44SJosef Bacik 	if (root == &sched->atom_root) {
15082f80dd44SJosef Bacik 		root = &sched->merged_atom_root;
15092f80dd44SJosef Bacik 		goto again;
15102f80dd44SJosef Bacik 	}
1511daa1d7a5SFrederic Weisbecker }
1512daa1d7a5SFrederic Weisbecker 
process_sched_wakeup_event(struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)15130e9b07e5SArnaldo Carvalho de Melo static int process_sched_wakeup_event(struct perf_tool *tool,
151432dcd021SJiri Olsa 				      struct evsel *evsel,
1515ee29be62SArnaldo Carvalho de Melo 				      struct perf_sample *sample,
15164218e673SArnaldo Carvalho de Melo 				      struct machine *machine)
1517419ab0d6SFrederic Weisbecker {
15180e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1519419ab0d6SFrederic Weisbecker 
15209ec3f4e4SArnaldo Carvalho de Melo 	if (sched->tp_handler->wakeup_event)
15219ec3f4e4SArnaldo Carvalho de Melo 		return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
1522419ab0d6SFrederic Weisbecker 
15232b7fcbc5SArnaldo Carvalho de Melo 	return 0;
1524419ab0d6SFrederic Weisbecker }
1525419ab0d6SFrederic Weisbecker 
process_sched_wakeup_ignore(struct perf_tool * tool __maybe_unused,struct evsel * evsel __maybe_unused,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)15264453deacSChunxin Zang static int process_sched_wakeup_ignore(struct perf_tool *tool __maybe_unused,
15274453deacSChunxin Zang 				      struct evsel *evsel __maybe_unused,
15284453deacSChunxin Zang 				      struct perf_sample *sample __maybe_unused,
15294453deacSChunxin Zang 				      struct machine *machine __maybe_unused)
15304453deacSChunxin Zang {
15314453deacSChunxin Zang 	return 0;
15324453deacSChunxin Zang }
15334453deacSChunxin Zang 
1534a151a37aSJiri Olsa union map_priv {
1535a151a37aSJiri Olsa 	void	*ptr;
1536a151a37aSJiri Olsa 	bool	 color;
1537a151a37aSJiri Olsa };
1538a151a37aSJiri Olsa 
thread__has_color(struct thread * thread)1539a151a37aSJiri Olsa static bool thread__has_color(struct thread *thread)
1540a151a37aSJiri Olsa {
1541a151a37aSJiri Olsa 	union map_priv priv = {
1542a151a37aSJiri Olsa 		.ptr = thread__priv(thread),
1543a151a37aSJiri Olsa 	};
1544a151a37aSJiri Olsa 
1545a151a37aSJiri Olsa 	return priv.color;
1546a151a37aSJiri Olsa }
1547a151a37aSJiri Olsa 
1548a151a37aSJiri Olsa static struct thread*
map__findnew_thread(struct perf_sched * sched,struct machine * machine,pid_t pid,pid_t tid)1549a151a37aSJiri Olsa map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1550a151a37aSJiri Olsa {
1551a151a37aSJiri Olsa 	struct thread *thread = machine__findnew_thread(machine, pid, tid);
1552a151a37aSJiri Olsa 	union map_priv priv = {
1553a151a37aSJiri Olsa 		.color = false,
1554a151a37aSJiri Olsa 	};
1555a151a37aSJiri Olsa 
1556a151a37aSJiri Olsa 	if (!sched->map.color_pids || !thread || thread__priv(thread))
1557a151a37aSJiri Olsa 		return thread;
1558a151a37aSJiri Olsa 
1559a151a37aSJiri Olsa 	if (thread_map__has(sched->map.color_pids, tid))
1560a151a37aSJiri Olsa 		priv.color = true;
1561a151a37aSJiri Olsa 
1562a151a37aSJiri Olsa 	thread__set_priv(thread, priv.ptr);
1563a151a37aSJiri Olsa 	return thread;
1564a151a37aSJiri Olsa }
1565a151a37aSJiri Olsa 
map_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)156632dcd021SJiri Olsa static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
15679ec3f4e4SArnaldo Carvalho de Melo 			    struct perf_sample *sample, struct machine *machine)
15680ec04e16SIngo Molnar {
1569efc0cdc9SArnaldo Carvalho de Melo 	const u32 next_pid = evsel__intval(evsel, sample, "next_pid");
15709d372ca5SDongsheng Yang 	struct thread *sched_in;
15718640da9fSChangbin Du 	struct thread_runtime *tr;
15720ec04e16SIngo Molnar 	int new_shortname;
15737f7f8d0bSArnaldo Carvalho de Melo 	u64 timestamp0, timestamp = sample->time;
15740ec04e16SIngo Molnar 	s64 delta;
15756d18804bSIan Rogers 	int i;
15766d18804bSIan Rogers 	struct perf_cpu this_cpu = {
15776d18804bSIan Rogers 		.cpu = sample->cpu,
15786d18804bSIan Rogers 	};
157999623c62SJiri Olsa 	int cpus_nr;
158099623c62SJiri Olsa 	bool new_cpu = false;
15818cd91195SJiri Olsa 	const char *color = PERF_COLOR_NORMAL;
158299620a5dSNamhyung Kim 	char stimestamp[32];
15830ec04e16SIngo Molnar 
15846d18804bSIan Rogers 	BUG_ON(this_cpu.cpu >= MAX_CPUS || this_cpu.cpu < 0);
15850ec04e16SIngo Molnar 
15866d18804bSIan Rogers 	if (this_cpu.cpu > sched->max_cpu.cpu)
15870e9b07e5SArnaldo Carvalho de Melo 		sched->max_cpu = this_cpu;
15880ec04e16SIngo Molnar 
158999623c62SJiri Olsa 	if (sched->map.comp) {
159099623c62SJiri Olsa 		cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
159175d7ba32SSean Christopherson 		if (!__test_and_set_bit(this_cpu.cpu, sched->map.comp_cpus_mask)) {
159299623c62SJiri Olsa 			sched->map.comp_cpus[cpus_nr++] = this_cpu;
159399623c62SJiri Olsa 			new_cpu = true;
159499623c62SJiri Olsa 		}
159599623c62SJiri Olsa 	} else
15966d18804bSIan Rogers 		cpus_nr = sched->max_cpu.cpu;
159799623c62SJiri Olsa 
15986d18804bSIan Rogers 	timestamp0 = sched->cpu_last_switched[this_cpu.cpu];
15996d18804bSIan Rogers 	sched->cpu_last_switched[this_cpu.cpu] = timestamp;
16000ec04e16SIngo Molnar 	if (timestamp0)
16010ec04e16SIngo Molnar 		delta = timestamp - timestamp0;
16020ec04e16SIngo Molnar 	else
16030ec04e16SIngo Molnar 		delta = 0;
16040ec04e16SIngo Molnar 
1605a116e05dSArnaldo Carvalho de Melo 	if (delta < 0) {
160660b7d14aSNamhyung Kim 		pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1607a116e05dSArnaldo Carvalho de Melo 		return -1;
1608a116e05dSArnaldo Carvalho de Melo 	}
16090ec04e16SIngo Molnar 
1610a151a37aSJiri Olsa 	sched_in = map__findnew_thread(sched, machine, -1, next_pid);
1611b91fc39fSArnaldo Carvalho de Melo 	if (sched_in == NULL)
1612b91fc39fSArnaldo Carvalho de Melo 		return -1;
16130ec04e16SIngo Molnar 
16148640da9fSChangbin Du 	tr = thread__get_runtime(sched_in);
16158640da9fSChangbin Du 	if (tr == NULL) {
16168640da9fSChangbin Du 		thread__put(sched_in);
16178640da9fSChangbin Du 		return -1;
16188640da9fSChangbin Du 	}
16198640da9fSChangbin Du 
16206d18804bSIan Rogers 	sched->curr_thread[this_cpu.cpu] = thread__get(sched_in);
16210ec04e16SIngo Molnar 
16220ec04e16SIngo Molnar 	printf("  ");
16230ec04e16SIngo Molnar 
16240ec04e16SIngo Molnar 	new_shortname = 0;
16258640da9fSChangbin Du 	if (!tr->shortname[0]) {
16266bcab4e1SDongsheng 		if (!strcmp(thread__comm_str(sched_in), "swapper")) {
16276bcab4e1SDongsheng 			/*
16286bcab4e1SDongsheng 			 * Don't allocate a letter-number for swapper:0
16296bcab4e1SDongsheng 			 * as a shortname. Instead, we use '.' for it.
16306bcab4e1SDongsheng 			 */
16318640da9fSChangbin Du 			tr->shortname[0] = '.';
16328640da9fSChangbin Du 			tr->shortname[1] = ' ';
16336bcab4e1SDongsheng 		} else {
16348640da9fSChangbin Du 			tr->shortname[0] = sched->next_shortname1;
16358640da9fSChangbin Du 			tr->shortname[1] = sched->next_shortname2;
16360ec04e16SIngo Molnar 
16370e9b07e5SArnaldo Carvalho de Melo 			if (sched->next_shortname1 < 'Z') {
16380e9b07e5SArnaldo Carvalho de Melo 				sched->next_shortname1++;
16390ec04e16SIngo Molnar 			} else {
16400e9b07e5SArnaldo Carvalho de Melo 				sched->next_shortname1 = 'A';
16416bcab4e1SDongsheng 				if (sched->next_shortname2 < '9')
16420e9b07e5SArnaldo Carvalho de Melo 					sched->next_shortname2++;
16436bcab4e1SDongsheng 				else
16440e9b07e5SArnaldo Carvalho de Melo 					sched->next_shortname2 = '0';
16450ec04e16SIngo Molnar 			}
16460ec04e16SIngo Molnar 		}
16470ec04e16SIngo Molnar 		new_shortname = 1;
16480ec04e16SIngo Molnar 	}
16490ec04e16SIngo Molnar 
165099623c62SJiri Olsa 	for (i = 0; i < cpus_nr; i++) {
16516d18804bSIan Rogers 		struct perf_cpu cpu = {
16526d18804bSIan Rogers 			.cpu = sched->map.comp ? sched->map.comp_cpus[i].cpu : i,
16536d18804bSIan Rogers 		};
16546d18804bSIan Rogers 		struct thread *curr_thread = sched->curr_thread[cpu.cpu];
16558640da9fSChangbin Du 		struct thread_runtime *curr_tr;
1656a151a37aSJiri Olsa 		const char *pid_color = color;
1657cf294f24SJiri Olsa 		const char *cpu_color = color;
1658a151a37aSJiri Olsa 
1659a151a37aSJiri Olsa 		if (curr_thread && thread__has_color(curr_thread))
1660a151a37aSJiri Olsa 			pid_color = COLOR_PIDS;
166199623c62SJiri Olsa 
1662dfc66befSIan Rogers 		if (sched->map.cpus && !perf_cpu_map__has(sched->map.cpus, cpu))
166373643bb6SJiri Olsa 			continue;
166473643bb6SJiri Olsa 
1665dfc66befSIan Rogers 		if (sched->map.color_cpus && perf_cpu_map__has(sched->map.color_cpus, cpu))
1666cf294f24SJiri Olsa 			cpu_color = COLOR_CPUS;
1667cf294f24SJiri Olsa 
16686d18804bSIan Rogers 		if (cpu.cpu != this_cpu.cpu)
16691208bb27SNamhyung Kim 			color_fprintf(stdout, color, " ");
16700ec04e16SIngo Molnar 		else
1671cf294f24SJiri Olsa 			color_fprintf(stdout, cpu_color, "*");
16720ec04e16SIngo Molnar 
16736d18804bSIan Rogers 		if (sched->curr_thread[cpu.cpu]) {
16746d18804bSIan Rogers 			curr_tr = thread__get_runtime(sched->curr_thread[cpu.cpu]);
16758640da9fSChangbin Du 			if (curr_tr == NULL) {
16768640da9fSChangbin Du 				thread__put(sched_in);
16778640da9fSChangbin Du 				return -1;
16788640da9fSChangbin Du 			}
16798640da9fSChangbin Du 			color_fprintf(stdout, pid_color, "%2s ", curr_tr->shortname);
16808640da9fSChangbin Du 		} else
16818cd91195SJiri Olsa 			color_fprintf(stdout, color, "   ");
16820ec04e16SIngo Molnar 	}
16830ec04e16SIngo Molnar 
1684dfc66befSIan Rogers 	if (sched->map.cpus && !perf_cpu_map__has(sched->map.cpus, this_cpu))
168573643bb6SJiri Olsa 		goto out;
168673643bb6SJiri Olsa 
168799620a5dSNamhyung Kim 	timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
168899620a5dSNamhyung Kim 	color_fprintf(stdout, color, "  %12s secs ", stimestamp);
1689ee84a303SIan Rogers 	if (new_shortname || tr->comm_changed || (verbose > 0 && thread__tid(sched_in))) {
1690a151a37aSJiri Olsa 		const char *pid_color = color;
1691a151a37aSJiri Olsa 
1692a151a37aSJiri Olsa 		if (thread__has_color(sched_in))
1693a151a37aSJiri Olsa 			pid_color = COLOR_PIDS;
1694a151a37aSJiri Olsa 
1695a151a37aSJiri Olsa 		color_fprintf(stdout, pid_color, "%s => %s:%d",
1696ee84a303SIan Rogers 			tr->shortname, thread__comm_str(sched_in), thread__tid(sched_in));
169799a3c3a9SChangbin Du 		tr->comm_changed = false;
16980ec04e16SIngo Molnar 	}
1699a116e05dSArnaldo Carvalho de Melo 
170099623c62SJiri Olsa 	if (sched->map.comp && new_cpu)
17018cd91195SJiri Olsa 		color_fprintf(stdout, color, " (CPU %d)", this_cpu);
170299623c62SJiri Olsa 
170373643bb6SJiri Olsa out:
17048cd91195SJiri Olsa 	color_fprintf(stdout, color, "\n");
170599623c62SJiri Olsa 
1706b91fc39fSArnaldo Carvalho de Melo 	thread__put(sched_in);
1707b91fc39fSArnaldo Carvalho de Melo 
1708a116e05dSArnaldo Carvalho de Melo 	return 0;
17090ec04e16SIngo Molnar }
17100ec04e16SIngo Molnar 
process_sched_switch_event(struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)17110e9b07e5SArnaldo Carvalho de Melo static int process_sched_switch_event(struct perf_tool *tool,
171232dcd021SJiri Olsa 				      struct evsel *evsel,
1713ee29be62SArnaldo Carvalho de Melo 				      struct perf_sample *sample,
17144218e673SArnaldo Carvalho de Melo 				      struct machine *machine)
1715419ab0d6SFrederic Weisbecker {
17160e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1717a116e05dSArnaldo Carvalho de Melo 	int this_cpu = sample->cpu, err = 0;
1718efc0cdc9SArnaldo Carvalho de Melo 	u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
1719efc0cdc9SArnaldo Carvalho de Melo 	    next_pid = evsel__intval(evsel, sample, "next_pid");
1720419ab0d6SFrederic Weisbecker 
17210e9b07e5SArnaldo Carvalho de Melo 	if (sched->curr_pid[this_cpu] != (u32)-1) {
1722c8a37751SIngo Molnar 		/*
1723c8a37751SIngo Molnar 		 * Are we trying to switch away a PID that is
1724c8a37751SIngo Molnar 		 * not current?
1725c8a37751SIngo Molnar 		 */
17262b7fcbc5SArnaldo Carvalho de Melo 		if (sched->curr_pid[this_cpu] != prev_pid)
17270e9b07e5SArnaldo Carvalho de Melo 			sched->nr_context_switch_bugs++;
1728c8a37751SIngo Molnar 	}
1729c8a37751SIngo Molnar 
17309ec3f4e4SArnaldo Carvalho de Melo 	if (sched->tp_handler->switch_event)
17319ec3f4e4SArnaldo Carvalho de Melo 		err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
17322b7fcbc5SArnaldo Carvalho de Melo 
17332b7fcbc5SArnaldo Carvalho de Melo 	sched->curr_pid[this_cpu] = next_pid;
1734a116e05dSArnaldo Carvalho de Melo 	return err;
1735419ab0d6SFrederic Weisbecker }
1736419ab0d6SFrederic Weisbecker 
process_sched_runtime_event(struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)17370e9b07e5SArnaldo Carvalho de Melo static int process_sched_runtime_event(struct perf_tool *tool,
173832dcd021SJiri Olsa 				       struct evsel *evsel,
1739ee29be62SArnaldo Carvalho de Melo 				       struct perf_sample *sample,
17404218e673SArnaldo Carvalho de Melo 				       struct machine *machine)
174139aeb52fSmingo {
17420e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
174339aeb52fSmingo 
17449ec3f4e4SArnaldo Carvalho de Melo 	if (sched->tp_handler->runtime_event)
17459ec3f4e4SArnaldo Carvalho de Melo 		return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
174639aeb52fSmingo 
1747a116e05dSArnaldo Carvalho de Melo 	return 0;
1748ec156764SIngo Molnar }
1749ec156764SIngo Molnar 
perf_sched__process_fork_event(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)1750cb627505SDavid Ahern static int perf_sched__process_fork_event(struct perf_tool *tool,
1751cb627505SDavid Ahern 					  union perf_event *event,
17522b7fcbc5SArnaldo Carvalho de Melo 					  struct perf_sample *sample,
1753cb627505SDavid Ahern 					  struct machine *machine)
17542b7fcbc5SArnaldo Carvalho de Melo {
17552b7fcbc5SArnaldo Carvalho de Melo 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
17562b7fcbc5SArnaldo Carvalho de Melo 
17574d39c89fSIngo Molnar 	/* run the fork event through the perf machinery */
1758cb627505SDavid Ahern 	perf_event__process_fork(tool, event, sample, machine);
1759cb627505SDavid Ahern 
1760cb627505SDavid Ahern 	/* and then run additional processing needed for this command */
17619ec3f4e4SArnaldo Carvalho de Melo 	if (sched->tp_handler->fork_event)
1762cb627505SDavid Ahern 		return sched->tp_handler->fork_event(sched, event, machine);
17632b7fcbc5SArnaldo Carvalho de Melo 
17642b7fcbc5SArnaldo Carvalho de Melo 	return 0;
17652b7fcbc5SArnaldo Carvalho de Melo }
17662b7fcbc5SArnaldo Carvalho de Melo 
process_sched_migrate_task_event(struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)17670e9b07e5SArnaldo Carvalho de Melo static int process_sched_migrate_task_event(struct perf_tool *tool,
176832dcd021SJiri Olsa 					    struct evsel *evsel,
1769ee29be62SArnaldo Carvalho de Melo 					    struct perf_sample *sample,
17704218e673SArnaldo Carvalho de Melo 					    struct machine *machine)
177155ffb7a6SMike Galbraith {
17720e9b07e5SArnaldo Carvalho de Melo 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
177355ffb7a6SMike Galbraith 
17749ec3f4e4SArnaldo Carvalho de Melo 	if (sched->tp_handler->migrate_task_event)
17759ec3f4e4SArnaldo Carvalho de Melo 		return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
177655ffb7a6SMike Galbraith 
17772b7fcbc5SArnaldo Carvalho de Melo 	return 0;
177855ffb7a6SMike Galbraith }
177955ffb7a6SMike Galbraith 
1780a116e05dSArnaldo Carvalho de Melo typedef int (*tracepoint_handler)(struct perf_tool *tool,
178132dcd021SJiri Olsa 				  struct evsel *evsel,
1782ee29be62SArnaldo Carvalho de Melo 				  struct perf_sample *sample,
17834218e673SArnaldo Carvalho de Melo 				  struct machine *machine);
1784ec156764SIngo Molnar 
perf_sched__process_tracepoint_sample(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)17851d037ca1SIrina Tirdea static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
17861d037ca1SIrina Tirdea 						 union perf_event *event __maybe_unused,
17878115d60cSArnaldo Carvalho de Melo 						 struct perf_sample *sample,
178832dcd021SJiri Olsa 						 struct evsel *evsel,
1789743eb868SArnaldo Carvalho de Melo 						 struct machine *machine)
17900a02ad93SIngo Molnar {
1791a116e05dSArnaldo Carvalho de Melo 	int err = 0;
1792a80deb62SArnaldo Carvalho de Melo 
1793744a9719SArnaldo Carvalho de Melo 	if (evsel->handler != NULL) {
1794744a9719SArnaldo Carvalho de Melo 		tracepoint_handler f = evsel->handler;
17952b7fcbc5SArnaldo Carvalho de Melo 		err = f(tool, evsel, sample, machine);
1796ee29be62SArnaldo Carvalho de Melo 	}
17970a02ad93SIngo Molnar 
1798a116e05dSArnaldo Carvalho de Melo 	return err;
17990a02ad93SIngo Molnar }
18000a02ad93SIngo Molnar 
perf_sched__process_comm(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)180199a3c3a9SChangbin Du static int perf_sched__process_comm(struct perf_tool *tool __maybe_unused,
180299a3c3a9SChangbin Du 				    union perf_event *event,
180399a3c3a9SChangbin Du 				    struct perf_sample *sample,
180499a3c3a9SChangbin Du 				    struct machine *machine)
180599a3c3a9SChangbin Du {
180699a3c3a9SChangbin Du 	struct thread *thread;
180799a3c3a9SChangbin Du 	struct thread_runtime *tr;
180899a3c3a9SChangbin Du 	int err;
180999a3c3a9SChangbin Du 
181099a3c3a9SChangbin Du 	err = perf_event__process_comm(tool, event, sample, machine);
181199a3c3a9SChangbin Du 	if (err)
181299a3c3a9SChangbin Du 		return err;
181399a3c3a9SChangbin Du 
181499a3c3a9SChangbin Du 	thread = machine__find_thread(machine, sample->pid, sample->tid);
181599a3c3a9SChangbin Du 	if (!thread) {
181699a3c3a9SChangbin Du 		pr_err("Internal error: can't find thread\n");
181799a3c3a9SChangbin Du 		return -1;
181899a3c3a9SChangbin Du 	}
181999a3c3a9SChangbin Du 
182099a3c3a9SChangbin Du 	tr = thread__get_runtime(thread);
182199a3c3a9SChangbin Du 	if (tr == NULL) {
182299a3c3a9SChangbin Du 		thread__put(thread);
182399a3c3a9SChangbin Du 		return -1;
182499a3c3a9SChangbin Du 	}
182599a3c3a9SChangbin Du 
182699a3c3a9SChangbin Du 	tr->comm_changed = true;
182799a3c3a9SChangbin Du 	thread__put(thread);
182899a3c3a9SChangbin Du 
182999a3c3a9SChangbin Du 	return 0;
183099a3c3a9SChangbin Du }
183199a3c3a9SChangbin Du 
perf_sched__read_events(struct perf_sched * sched)1832ae536acfSArnaldo Carvalho de Melo static int perf_sched__read_events(struct perf_sched *sched)
18330a02ad93SIngo Molnar {
18344453deacSChunxin Zang 	struct evsel_str_handler handlers[] = {
1835ee29be62SArnaldo Carvalho de Melo 		{ "sched:sched_switch",	      process_sched_switch_event, },
1836ee29be62SArnaldo Carvalho de Melo 		{ "sched:sched_stat_runtime", process_sched_runtime_event, },
1837ee29be62SArnaldo Carvalho de Melo 		{ "sched:sched_wakeup",	      process_sched_wakeup_event, },
18384453deacSChunxin Zang 		{ "sched:sched_waking",	      process_sched_wakeup_event, },
1839ee29be62SArnaldo Carvalho de Melo 		{ "sched:sched_wakeup_new",   process_sched_wakeup_event, },
1840ee29be62SArnaldo Carvalho de Melo 		{ "sched:sched_migrate_task", process_sched_migrate_task_event, },
1841ee29be62SArnaldo Carvalho de Melo 	};
1842da378962SArnaldo Carvalho de Melo 	struct perf_session *session;
18438ceb41d7SJiri Olsa 	struct perf_data data = {
1844f5fc1412SJiri Olsa 		.path  = input_name,
1845f5fc1412SJiri Olsa 		.mode  = PERF_DATA_MODE_READ,
1846f0dd330fSYunlong Song 		.force = sched->force,
1847f5fc1412SJiri Olsa 	};
1848ae536acfSArnaldo Carvalho de Melo 	int rc = -1;
1849da378962SArnaldo Carvalho de Melo 
18502681bd85SNamhyung Kim 	session = perf_session__new(&data, &sched->tool);
18516ef81c55SMamatha Inamdar 	if (IS_ERR(session)) {
18526ef81c55SMamatha Inamdar 		pr_debug("Error creating perf session");
18536ef81c55SMamatha Inamdar 		return PTR_ERR(session);
1854a116e05dSArnaldo Carvalho de Melo 	}
185594c744b6SArnaldo Carvalho de Melo 
18560a7e6d1bSNamhyung Kim 	symbol__init(&session->header.env);
185704934106SNamhyung Kim 
18584453deacSChunxin Zang 	/* prefer sched_waking if it is captured */
18594453deacSChunxin Zang 	if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking"))
18604453deacSChunxin Zang 		handlers[2].handler = process_sched_wakeup_ignore;
18614453deacSChunxin Zang 
1862a116e05dSArnaldo Carvalho de Melo 	if (perf_session__set_tracepoints_handlers(session, handlers))
1863a116e05dSArnaldo Carvalho de Melo 		goto out_delete;
1864ee29be62SArnaldo Carvalho de Melo 
1865cee75ac7SArnaldo Carvalho de Melo 	if (perf_session__has_traces(session, "record -R")) {
1866b7b61cbeSArnaldo Carvalho de Melo 		int err = perf_session__process_events(session);
1867a116e05dSArnaldo Carvalho de Melo 		if (err) {
1868a116e05dSArnaldo Carvalho de Melo 			pr_err("Failed to process events, error %d", err);
1869a116e05dSArnaldo Carvalho de Melo 			goto out_delete;
1870a116e05dSArnaldo Carvalho de Melo 		}
18714c09bafaSJiri Olsa 
187275be989aSArnaldo Carvalho de Melo 		sched->nr_events      = session->evlist->stats.nr_events[0];
187375be989aSArnaldo Carvalho de Melo 		sched->nr_lost_events = session->evlist->stats.total_lost;
187475be989aSArnaldo Carvalho de Melo 		sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
1875cee75ac7SArnaldo Carvalho de Melo 	}
1876d549c769SArnaldo Carvalho de Melo 
1877ae536acfSArnaldo Carvalho de Melo 	rc = 0;
1878a116e05dSArnaldo Carvalho de Melo out_delete:
1879a116e05dSArnaldo Carvalho de Melo 	perf_session__delete(session);
1880ae536acfSArnaldo Carvalho de Melo 	return rc;
18810a02ad93SIngo Molnar }
18820a02ad93SIngo Molnar 
188349394a2aSDavid Ahern /*
188449394a2aSDavid Ahern  * scheduling times are printed as msec.usec
188549394a2aSDavid Ahern  */
print_sched_time(unsigned long long nsecs,int width)188649394a2aSDavid Ahern static inline void print_sched_time(unsigned long long nsecs, int width)
188749394a2aSDavid Ahern {
188849394a2aSDavid Ahern 	unsigned long msecs;
188949394a2aSDavid Ahern 	unsigned long usecs;
189049394a2aSDavid Ahern 
189149394a2aSDavid Ahern 	msecs  = nsecs / NSEC_PER_MSEC;
189249394a2aSDavid Ahern 	nsecs -= msecs * NSEC_PER_MSEC;
189349394a2aSDavid Ahern 	usecs  = nsecs / NSEC_PER_USEC;
189449394a2aSDavid Ahern 	printf("%*lu.%03lu ", width, msecs, usecs);
189549394a2aSDavid Ahern }
189649394a2aSDavid Ahern 
189749394a2aSDavid Ahern /*
189849394a2aSDavid Ahern  * returns runtime data for event, allocating memory for it the
189949394a2aSDavid Ahern  * first time it is used.
190049394a2aSDavid Ahern  */
evsel__get_runtime(struct evsel * evsel)19013b7313f2SArnaldo Carvalho de Melo static struct evsel_runtime *evsel__get_runtime(struct evsel *evsel)
190249394a2aSDavid Ahern {
190349394a2aSDavid Ahern 	struct evsel_runtime *r = evsel->priv;
190449394a2aSDavid Ahern 
190549394a2aSDavid Ahern 	if (r == NULL) {
190649394a2aSDavid Ahern 		r = zalloc(sizeof(struct evsel_runtime));
190749394a2aSDavid Ahern 		evsel->priv = r;
190849394a2aSDavid Ahern 	}
190949394a2aSDavid Ahern 
191049394a2aSDavid Ahern 	return r;
191149394a2aSDavid Ahern }
191249394a2aSDavid Ahern 
191349394a2aSDavid Ahern /*
191449394a2aSDavid Ahern  * save last time event was seen per cpu
191549394a2aSDavid Ahern  */
evsel__save_time(struct evsel * evsel,u64 timestamp,u32 cpu)19163b7313f2SArnaldo Carvalho de Melo static void evsel__save_time(struct evsel *evsel, u64 timestamp, u32 cpu)
191749394a2aSDavid Ahern {
19183b7313f2SArnaldo Carvalho de Melo 	struct evsel_runtime *r = evsel__get_runtime(evsel);
191949394a2aSDavid Ahern 
192049394a2aSDavid Ahern 	if (r == NULL)
192149394a2aSDavid Ahern 		return;
192249394a2aSDavid Ahern 
192349394a2aSDavid Ahern 	if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
192449394a2aSDavid Ahern 		int i, n = __roundup_pow_of_two(cpu+1);
192549394a2aSDavid Ahern 		void *p = r->last_time;
192649394a2aSDavid Ahern 
192749394a2aSDavid Ahern 		p = realloc(r->last_time, n * sizeof(u64));
192849394a2aSDavid Ahern 		if (!p)
192949394a2aSDavid Ahern 			return;
193049394a2aSDavid Ahern 
193149394a2aSDavid Ahern 		r->last_time = p;
193249394a2aSDavid Ahern 		for (i = r->ncpu; i < n; ++i)
193349394a2aSDavid Ahern 			r->last_time[i] = (u64) 0;
193449394a2aSDavid Ahern 
193549394a2aSDavid Ahern 		r->ncpu = n;
193649394a2aSDavid Ahern 	}
193749394a2aSDavid Ahern 
193849394a2aSDavid Ahern 	r->last_time[cpu] = timestamp;
193949394a2aSDavid Ahern }
194049394a2aSDavid Ahern 
194149394a2aSDavid Ahern /* returns last time this event was seen on the given cpu */
evsel__get_time(struct evsel * evsel,u32 cpu)19423b7313f2SArnaldo Carvalho de Melo static u64 evsel__get_time(struct evsel *evsel, u32 cpu)
194349394a2aSDavid Ahern {
19443b7313f2SArnaldo Carvalho de Melo 	struct evsel_runtime *r = evsel__get_runtime(evsel);
194549394a2aSDavid Ahern 
194649394a2aSDavid Ahern 	if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
194749394a2aSDavid Ahern 		return 0;
194849394a2aSDavid Ahern 
194949394a2aSDavid Ahern 	return r->last_time[cpu];
195049394a2aSDavid Ahern }
195149394a2aSDavid Ahern 
19529b8087d7SNamhyung Kim static int comm_width = 30;
195349394a2aSDavid Ahern 
timehist_get_commstr(struct thread * thread)195449394a2aSDavid Ahern static char *timehist_get_commstr(struct thread *thread)
195549394a2aSDavid Ahern {
195649394a2aSDavid Ahern 	static char str[32];
195749394a2aSDavid Ahern 	const char *comm = thread__comm_str(thread);
1958ee84a303SIan Rogers 	pid_t tid = thread__tid(thread);
1959ee84a303SIan Rogers 	pid_t pid = thread__pid(thread);
196049394a2aSDavid Ahern 	int n;
196149394a2aSDavid Ahern 
196249394a2aSDavid Ahern 	if (pid == 0)
196349394a2aSDavid Ahern 		n = scnprintf(str, sizeof(str), "%s", comm);
196449394a2aSDavid Ahern 
196549394a2aSDavid Ahern 	else if (tid != pid)
196649394a2aSDavid Ahern 		n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
196749394a2aSDavid Ahern 
196849394a2aSDavid Ahern 	else
196949394a2aSDavid Ahern 		n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
197049394a2aSDavid Ahern 
197149394a2aSDavid Ahern 	if (n > comm_width)
197249394a2aSDavid Ahern 		comm_width = n;
197349394a2aSDavid Ahern 
197449394a2aSDavid Ahern 	return str;
197549394a2aSDavid Ahern }
197649394a2aSDavid Ahern 
timehist_header(struct perf_sched * sched)1977a407b067SDavid Ahern static void timehist_header(struct perf_sched *sched)
197849394a2aSDavid Ahern {
19796d18804bSIan Rogers 	u32 ncpus = sched->max_cpu.cpu + 1;
1980a407b067SDavid Ahern 	u32 i, j;
1981a407b067SDavid Ahern 
198249394a2aSDavid Ahern 	printf("%15s %6s ", "time", "cpu");
198349394a2aSDavid Ahern 
1984a407b067SDavid Ahern 	if (sched->show_cpu_visual) {
1985a407b067SDavid Ahern 		printf(" ");
1986a407b067SDavid Ahern 		for (i = 0, j = 0; i < ncpus; ++i) {
1987a407b067SDavid Ahern 			printf("%x", j++);
1988a407b067SDavid Ahern 			if (j > 15)
1989a407b067SDavid Ahern 				j = 0;
1990a407b067SDavid Ahern 		}
1991a407b067SDavid Ahern 		printf(" ");
1992a407b067SDavid Ahern 	}
1993a407b067SDavid Ahern 
19940e6758e8SNamhyung Kim 	printf(" %-*s  %9s  %9s  %9s", comm_width,
199549394a2aSDavid Ahern 		"task name", "wait time", "sch delay", "run time");
199649394a2aSDavid Ahern 
1997414e050cSNamhyung Kim 	if (sched->show_state)
1998414e050cSNamhyung Kim 		printf("  %s", "state");
1999414e050cSNamhyung Kim 
200049394a2aSDavid Ahern 	printf("\n");
200149394a2aSDavid Ahern 
200249394a2aSDavid Ahern 	/*
200349394a2aSDavid Ahern 	 * units row
200449394a2aSDavid Ahern 	 */
200549394a2aSDavid Ahern 	printf("%15s %-6s ", "", "");
200649394a2aSDavid Ahern 
2007a407b067SDavid Ahern 	if (sched->show_cpu_visual)
2008a407b067SDavid Ahern 		printf(" %*s ", ncpus, "");
2009a407b067SDavid Ahern 
2010414e050cSNamhyung Kim 	printf(" %-*s  %9s  %9s  %9s", comm_width,
20110e6758e8SNamhyung Kim 	       "[tid/pid]", "(msec)", "(msec)", "(msec)");
201249394a2aSDavid Ahern 
2013414e050cSNamhyung Kim 	if (sched->show_state)
2014414e050cSNamhyung Kim 		printf("  %5s", "");
2015414e050cSNamhyung Kim 
2016414e050cSNamhyung Kim 	printf("\n");
2017414e050cSNamhyung Kim 
201849394a2aSDavid Ahern 	/*
201949394a2aSDavid Ahern 	 * separator
202049394a2aSDavid Ahern 	 */
202149394a2aSDavid Ahern 	printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
202249394a2aSDavid Ahern 
2023a407b067SDavid Ahern 	if (sched->show_cpu_visual)
2024a407b067SDavid Ahern 		printf(" %.*s ", ncpus, graph_dotted_line);
2025a407b067SDavid Ahern 
20260e6758e8SNamhyung Kim 	printf(" %.*s  %.9s  %.9s  %.9s", comm_width,
202749394a2aSDavid Ahern 		graph_dotted_line, graph_dotted_line, graph_dotted_line,
202849394a2aSDavid Ahern 		graph_dotted_line);
202949394a2aSDavid Ahern 
2030414e050cSNamhyung Kim 	if (sched->show_state)
2031414e050cSNamhyung Kim 		printf("  %.5s", graph_dotted_line);
2032414e050cSNamhyung Kim 
203349394a2aSDavid Ahern 	printf("\n");
203449394a2aSDavid Ahern }
203549394a2aSDavid Ahern 
task_state_char(struct thread * thread,int state)2036414e050cSNamhyung Kim static char task_state_char(struct thread *thread, int state)
2037414e050cSNamhyung Kim {
2038414e050cSNamhyung Kim 	static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
2039414e050cSNamhyung Kim 	unsigned bit = state ? ffs(state) : 0;
2040414e050cSNamhyung Kim 
2041414e050cSNamhyung Kim 	/* 'I' for idle */
2042ee84a303SIan Rogers 	if (thread__tid(thread) == 0)
2043414e050cSNamhyung Kim 		return 'I';
2044414e050cSNamhyung Kim 
2045414e050cSNamhyung Kim 	return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
2046414e050cSNamhyung Kim }
2047414e050cSNamhyung Kim 
timehist_print_sample(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct addr_location * al,struct thread * thread,u64 t,int state)2048fc1469f1SDavid Ahern static void timehist_print_sample(struct perf_sched *sched,
204932dcd021SJiri Olsa 				  struct evsel *evsel,
2050fc1469f1SDavid Ahern 				  struct perf_sample *sample,
20516c973c90SDavid Ahern 				  struct addr_location *al,
2052853b7407SDavid Ahern 				  struct thread *thread,
2053414e050cSNamhyung Kim 				  u64 t, int state)
205449394a2aSDavid Ahern {
205549394a2aSDavid Ahern 	struct thread_runtime *tr = thread__priv(thread);
2056efc0cdc9SArnaldo Carvalho de Melo 	const char *next_comm = evsel__strval(evsel, sample, "next_comm");
2057efc0cdc9SArnaldo Carvalho de Melo 	const u32 next_pid = evsel__intval(evsel, sample, "next_pid");
20586d18804bSIan Rogers 	u32 max_cpus = sched->max_cpu.cpu + 1;
205949394a2aSDavid Ahern 	char tstr[64];
2060292c4a8fSBrendan Gregg 	char nstr[30];
2061941bdea7SNamhyung Kim 	u64 wait_time;
206249394a2aSDavid Ahern 
2063c30d630dSDavid Ahern 	if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
2064c30d630dSDavid Ahern 		return;
2065c30d630dSDavid Ahern 
2066853b7407SDavid Ahern 	timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
206749394a2aSDavid Ahern 	printf("%15s [%04d] ", tstr, sample->cpu);
206849394a2aSDavid Ahern 
2069a407b067SDavid Ahern 	if (sched->show_cpu_visual) {
2070a407b067SDavid Ahern 		u32 i;
2071a407b067SDavid Ahern 		char c;
2072a407b067SDavid Ahern 
2073a407b067SDavid Ahern 		printf(" ");
2074a407b067SDavid Ahern 		for (i = 0; i < max_cpus; ++i) {
2075a407b067SDavid Ahern 			/* flag idle times with 'i'; others are sched events */
2076a407b067SDavid Ahern 			if (i == sample->cpu)
2077ee84a303SIan Rogers 				c = (thread__tid(thread) == 0) ? 'i' : 's';
2078a407b067SDavid Ahern 			else
2079a407b067SDavid Ahern 				c = ' ';
2080a407b067SDavid Ahern 			printf("%c", c);
2081a407b067SDavid Ahern 		}
2082a407b067SDavid Ahern 		printf(" ");
2083a407b067SDavid Ahern 	}
2084a407b067SDavid Ahern 
208549394a2aSDavid Ahern 	printf(" %-*s ", comm_width, timehist_get_commstr(thread));
208649394a2aSDavid Ahern 
2087941bdea7SNamhyung Kim 	wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
2088941bdea7SNamhyung Kim 	print_sched_time(wait_time, 6);
2089941bdea7SNamhyung Kim 
209049394a2aSDavid Ahern 	print_sched_time(tr->dt_delay, 6);
209149394a2aSDavid Ahern 	print_sched_time(tr->dt_run, 6);
2092fc1469f1SDavid Ahern 
2093414e050cSNamhyung Kim 	if (sched->show_state)
2094414e050cSNamhyung Kim 		printf(" %5c ", task_state_char(thread, state));
2095414e050cSNamhyung Kim 
2096292c4a8fSBrendan Gregg 	if (sched->show_next) {
2097292c4a8fSBrendan Gregg 		snprintf(nstr, sizeof(nstr), "next: %s[%d]", next_comm, next_pid);
2098292c4a8fSBrendan Gregg 		printf(" %-*s", comm_width, nstr);
2099292c4a8fSBrendan Gregg 	}
2100292c4a8fSBrendan Gregg 
2101292c4a8fSBrendan Gregg 	if (sched->show_wakeups && !sched->show_next)
2102fc1469f1SDavid Ahern 		printf("  %-*s", comm_width, "");
2103fc1469f1SDavid Ahern 
2104ee84a303SIan Rogers 	if (thread__tid(thread) == 0)
21056c973c90SDavid Ahern 		goto out;
21066c973c90SDavid Ahern 
21076c973c90SDavid Ahern 	if (sched->show_callchain)
21086c973c90SDavid Ahern 		printf("  ");
21096c973c90SDavid Ahern 
21106c973c90SDavid Ahern 	sample__fprintf_sym(sample, al, 0,
21116c973c90SDavid Ahern 			    EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
21122d9bbf6eSNamhyung Kim 			    EVSEL__PRINT_CALLCHAIN_ARROW |
21132d9bbf6eSNamhyung Kim 			    EVSEL__PRINT_SKIP_IGNORED,
21148ab12a20SIan Rogers 			    get_tls_callchain_cursor(), symbol_conf.bt_stop_list,  stdout);
21156c973c90SDavid Ahern 
21166c973c90SDavid Ahern out:
211749394a2aSDavid Ahern 	printf("\n");
211849394a2aSDavid Ahern }
211949394a2aSDavid Ahern 
212049394a2aSDavid Ahern /*
212149394a2aSDavid Ahern  * Explanation of delta-time stats:
212249394a2aSDavid Ahern  *
212349394a2aSDavid Ahern  *            t = time of current schedule out event
212449394a2aSDavid Ahern  *        tprev = time of previous sched out event
212549394a2aSDavid Ahern  *                also time of schedule-in event for current task
212649394a2aSDavid Ahern  *    last_time = time of last sched change event for current task
212749394a2aSDavid Ahern  *                (i.e, time process was last scheduled out)
212849394a2aSDavid Ahern  * ready_to_run = time of wakeup for current task
212949394a2aSDavid Ahern  *
213049394a2aSDavid Ahern  * -----|------------|------------|------------|------
213149394a2aSDavid Ahern  *    last         ready        tprev          t
213249394a2aSDavid Ahern  *    time         to run
213349394a2aSDavid Ahern  *
213449394a2aSDavid Ahern  *      |-------- dt_wait --------|
213549394a2aSDavid Ahern  *                   |- dt_delay -|-- dt_run --|
213649394a2aSDavid Ahern  *
213749394a2aSDavid Ahern  *   dt_run = run time of current task
213849394a2aSDavid Ahern  *  dt_wait = time between last schedule out event for task and tprev
213949394a2aSDavid Ahern  *            represents time spent off the cpu
214049394a2aSDavid Ahern  * dt_delay = time between wakeup and schedule-in of task
214149394a2aSDavid Ahern  */
214249394a2aSDavid Ahern 
timehist_update_runtime_stats(struct thread_runtime * r,u64 t,u64 tprev)214349394a2aSDavid Ahern static void timehist_update_runtime_stats(struct thread_runtime *r,
214449394a2aSDavid Ahern 					 u64 t, u64 tprev)
214549394a2aSDavid Ahern {
214649394a2aSDavid Ahern 	r->dt_delay   = 0;
2147941bdea7SNamhyung Kim 	r->dt_sleep   = 0;
2148941bdea7SNamhyung Kim 	r->dt_iowait  = 0;
2149941bdea7SNamhyung Kim 	r->dt_preempt = 0;
215049394a2aSDavid Ahern 	r->dt_run     = 0;
2151941bdea7SNamhyung Kim 
215249394a2aSDavid Ahern 	if (tprev) {
215349394a2aSDavid Ahern 		r->dt_run = t - tprev;
215449394a2aSDavid Ahern 		if (r->ready_to_run) {
215549394a2aSDavid Ahern 			if (r->ready_to_run > tprev)
215649394a2aSDavid Ahern 				pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
215749394a2aSDavid Ahern 			else
215849394a2aSDavid Ahern 				r->dt_delay = tprev - r->ready_to_run;
215949394a2aSDavid Ahern 		}
216049394a2aSDavid Ahern 
216149394a2aSDavid Ahern 		if (r->last_time > tprev)
216249394a2aSDavid Ahern 			pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
2163941bdea7SNamhyung Kim 		else if (r->last_time) {
2164941bdea7SNamhyung Kim 			u64 dt_wait = tprev - r->last_time;
2165941bdea7SNamhyung Kim 
2166941bdea7SNamhyung Kim 			if (r->last_state == TASK_RUNNING)
2167941bdea7SNamhyung Kim 				r->dt_preempt = dt_wait;
2168941bdea7SNamhyung Kim 			else if (r->last_state == TASK_UNINTERRUPTIBLE)
2169941bdea7SNamhyung Kim 				r->dt_iowait = dt_wait;
2170941bdea7SNamhyung Kim 			else
2171941bdea7SNamhyung Kim 				r->dt_sleep = dt_wait;
2172941bdea7SNamhyung Kim 		}
217349394a2aSDavid Ahern 	}
217449394a2aSDavid Ahern 
217549394a2aSDavid Ahern 	update_stats(&r->run_stats, r->dt_run);
2176587782c5SNamhyung Kim 
217749394a2aSDavid Ahern 	r->total_run_time     += r->dt_run;
2178587782c5SNamhyung Kim 	r->total_delay_time   += r->dt_delay;
2179587782c5SNamhyung Kim 	r->total_sleep_time   += r->dt_sleep;
2180587782c5SNamhyung Kim 	r->total_iowait_time  += r->dt_iowait;
2181587782c5SNamhyung Kim 	r->total_preempt_time += r->dt_preempt;
218249394a2aSDavid Ahern }
218349394a2aSDavid Ahern 
is_idle_sample(struct perf_sample * sample,struct evsel * evsel)218496039c7cSNamhyung Kim static bool is_idle_sample(struct perf_sample *sample,
218532dcd021SJiri Olsa 			   struct evsel *evsel)
218696039c7cSNamhyung Kim {
218796039c7cSNamhyung Kim 	/* pid 0 == swapper == idle task */
21888ab2e96dSArnaldo Carvalho de Melo 	if (strcmp(evsel__name(evsel), "sched:sched_switch") == 0)
2189efc0cdc9SArnaldo Carvalho de Melo 		return evsel__intval(evsel, sample, "prev_pid") == 0;
219096039c7cSNamhyung Kim 
219196039c7cSNamhyung Kim 	return sample->pid == 0;
219296039c7cSNamhyung Kim }
219396039c7cSNamhyung Kim 
save_task_callchain(struct perf_sched * sched,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)219496039c7cSNamhyung Kim static void save_task_callchain(struct perf_sched *sched,
21956c973c90SDavid Ahern 				struct perf_sample *sample,
219632dcd021SJiri Olsa 				struct evsel *evsel,
21976c973c90SDavid Ahern 				struct machine *machine)
219849394a2aSDavid Ahern {
21998ab12a20SIan Rogers 	struct callchain_cursor *cursor;
220096039c7cSNamhyung Kim 	struct thread *thread;
22016c973c90SDavid Ahern 
22026c973c90SDavid Ahern 	/* want main thread for process - has maps */
22036c973c90SDavid Ahern 	thread = machine__findnew_thread(machine, sample->pid, sample->pid);
22046c973c90SDavid Ahern 	if (thread == NULL) {
22056c973c90SDavid Ahern 		pr_debug("Failed to get thread for pid %d.\n", sample->pid);
220696039c7cSNamhyung Kim 		return;
22076c973c90SDavid Ahern 	}
22086c973c90SDavid Ahern 
22094c50563dSArnaldo Carvalho de Melo 	if (!sched->show_callchain || sample->callchain == NULL)
221096039c7cSNamhyung Kim 		return;
22116c973c90SDavid Ahern 
22128ab12a20SIan Rogers 	cursor = get_tls_callchain_cursor();
22138ab12a20SIan Rogers 
22146c973c90SDavid Ahern 	if (thread__resolve_callchain(thread, cursor, evsel, sample,
22158388deb3SNamhyung Kim 				      NULL, NULL, sched->max_stack + 2) != 0) {
2216bb963e16SNamhyung Kim 		if (verbose > 0)
221762d94b00SArnaldo Carvalho de Melo 			pr_err("Failed to resolve callchain. Skipping\n");
22186c973c90SDavid Ahern 
221996039c7cSNamhyung Kim 		return;
22206c973c90SDavid Ahern 	}
2221cdeb01bfSNamhyung Kim 
22226c973c90SDavid Ahern 	callchain_cursor_commit(cursor);
2223cdeb01bfSNamhyung Kim 
2224cdeb01bfSNamhyung Kim 	while (true) {
2225cdeb01bfSNamhyung Kim 		struct callchain_cursor_node *node;
2226cdeb01bfSNamhyung Kim 		struct symbol *sym;
2227cdeb01bfSNamhyung Kim 
2228cdeb01bfSNamhyung Kim 		node = callchain_cursor_current(cursor);
2229cdeb01bfSNamhyung Kim 		if (node == NULL)
2230cdeb01bfSNamhyung Kim 			break;
2231cdeb01bfSNamhyung Kim 
22325f0fef8aSArnaldo Carvalho de Melo 		sym = node->ms.sym;
2233a7c3899cSArnaldo Carvalho de Melo 		if (sym) {
2234cdeb01bfSNamhyung Kim 			if (!strcmp(sym->name, "schedule") ||
2235cdeb01bfSNamhyung Kim 			    !strcmp(sym->name, "__schedule") ||
2236cdeb01bfSNamhyung Kim 			    !strcmp(sym->name, "preempt_schedule"))
2237cdeb01bfSNamhyung Kim 				sym->ignore = 1;
2238cdeb01bfSNamhyung Kim 		}
2239cdeb01bfSNamhyung Kim 
2240cdeb01bfSNamhyung Kim 		callchain_cursor_advance(cursor);
2241cdeb01bfSNamhyung Kim 	}
224249394a2aSDavid Ahern }
224349394a2aSDavid Ahern 
init_idle_thread(struct thread * thread)22443bc2fa9cSNamhyung Kim static int init_idle_thread(struct thread *thread)
22453bc2fa9cSNamhyung Kim {
22463bc2fa9cSNamhyung Kim 	struct idle_thread_runtime *itr;
22473bc2fa9cSNamhyung Kim 
22483bc2fa9cSNamhyung Kim 	thread__set_comm(thread, idle_comm, 0);
22493bc2fa9cSNamhyung Kim 
22503bc2fa9cSNamhyung Kim 	itr = zalloc(sizeof(*itr));
22513bc2fa9cSNamhyung Kim 	if (itr == NULL)
22523bc2fa9cSNamhyung Kim 		return -ENOMEM;
22533bc2fa9cSNamhyung Kim 
22543bc2fa9cSNamhyung Kim 	init_stats(&itr->tr.run_stats);
22553bc2fa9cSNamhyung Kim 	callchain_init(&itr->callchain);
22563bc2fa9cSNamhyung Kim 	callchain_cursor_reset(&itr->cursor);
22573bc2fa9cSNamhyung Kim 	thread__set_priv(thread, itr);
22583bc2fa9cSNamhyung Kim 
22593bc2fa9cSNamhyung Kim 	return 0;
22603bc2fa9cSNamhyung Kim }
22613bc2fa9cSNamhyung Kim 
226249394a2aSDavid Ahern /*
226349394a2aSDavid Ahern  * Track idle stats per cpu by maintaining a local thread
226449394a2aSDavid Ahern  * struct for the idle task on each cpu.
226549394a2aSDavid Ahern  */
init_idle_threads(int ncpu)226649394a2aSDavid Ahern static int init_idle_threads(int ncpu)
226749394a2aSDavid Ahern {
22683bc2fa9cSNamhyung Kim 	int i, ret;
226949394a2aSDavid Ahern 
227049394a2aSDavid Ahern 	idle_threads = zalloc(ncpu * sizeof(struct thread *));
227149394a2aSDavid Ahern 	if (!idle_threads)
227249394a2aSDavid Ahern 		return -ENOMEM;
227349394a2aSDavid Ahern 
2274b336352bSNamhyung Kim 	idle_max_cpu = ncpu;
227549394a2aSDavid Ahern 
227649394a2aSDavid Ahern 	/* allocate the actual thread struct if needed */
227749394a2aSDavid Ahern 	for (i = 0; i < ncpu; ++i) {
227849394a2aSDavid Ahern 		idle_threads[i] = thread__new(0, 0);
227949394a2aSDavid Ahern 		if (idle_threads[i] == NULL)
228049394a2aSDavid Ahern 			return -ENOMEM;
228149394a2aSDavid Ahern 
22823bc2fa9cSNamhyung Kim 		ret = init_idle_thread(idle_threads[i]);
22833bc2fa9cSNamhyung Kim 		if (ret < 0)
22843bc2fa9cSNamhyung Kim 			return ret;
228549394a2aSDavid Ahern 	}
228649394a2aSDavid Ahern 
228749394a2aSDavid Ahern 	return 0;
228849394a2aSDavid Ahern }
228949394a2aSDavid Ahern 
free_idle_threads(void)229049394a2aSDavid Ahern static void free_idle_threads(void)
229149394a2aSDavid Ahern {
229249394a2aSDavid Ahern 	int i;
229349394a2aSDavid Ahern 
229449394a2aSDavid Ahern 	if (idle_threads == NULL)
229549394a2aSDavid Ahern 		return;
229649394a2aSDavid Ahern 
2297b336352bSNamhyung Kim 	for (i = 0; i < idle_max_cpu; ++i) {
229849394a2aSDavid Ahern 		if ((idle_threads[i]))
229949394a2aSDavid Ahern 			thread__delete(idle_threads[i]);
230049394a2aSDavid Ahern 	}
230149394a2aSDavid Ahern 
230249394a2aSDavid Ahern 	free(idle_threads);
230349394a2aSDavid Ahern }
230449394a2aSDavid Ahern 
get_idle_thread(int cpu)230549394a2aSDavid Ahern static struct thread *get_idle_thread(int cpu)
230649394a2aSDavid Ahern {
230749394a2aSDavid Ahern 	/*
230849394a2aSDavid Ahern 	 * expand/allocate array of pointers to local thread
230949394a2aSDavid Ahern 	 * structs if needed
231049394a2aSDavid Ahern 	 */
231149394a2aSDavid Ahern 	if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
231249394a2aSDavid Ahern 		int i, j = __roundup_pow_of_two(cpu+1);
231349394a2aSDavid Ahern 		void *p;
231449394a2aSDavid Ahern 
231549394a2aSDavid Ahern 		p = realloc(idle_threads, j * sizeof(struct thread *));
231649394a2aSDavid Ahern 		if (!p)
231749394a2aSDavid Ahern 			return NULL;
231849394a2aSDavid Ahern 
231949394a2aSDavid Ahern 		idle_threads = (struct thread **) p;
2320b336352bSNamhyung Kim 		for (i = idle_max_cpu; i < j; ++i)
232149394a2aSDavid Ahern 			idle_threads[i] = NULL;
232249394a2aSDavid Ahern 
232349394a2aSDavid Ahern 		idle_max_cpu = j;
232449394a2aSDavid Ahern 	}
232549394a2aSDavid Ahern 
232649394a2aSDavid Ahern 	/* allocate a new thread struct if needed */
232749394a2aSDavid Ahern 	if (idle_threads[cpu] == NULL) {
232849394a2aSDavid Ahern 		idle_threads[cpu] = thread__new(0, 0);
232949394a2aSDavid Ahern 
233049394a2aSDavid Ahern 		if (idle_threads[cpu]) {
23313bc2fa9cSNamhyung Kim 			if (init_idle_thread(idle_threads[cpu]) < 0)
23323bc2fa9cSNamhyung Kim 				return NULL;
233349394a2aSDavid Ahern 		}
233449394a2aSDavid Ahern 	}
233549394a2aSDavid Ahern 
233649394a2aSDavid Ahern 	return idle_threads[cpu];
233749394a2aSDavid Ahern }
233849394a2aSDavid Ahern 
save_idle_callchain(struct perf_sched * sched,struct idle_thread_runtime * itr,struct perf_sample * sample)23394c50563dSArnaldo Carvalho de Melo static void save_idle_callchain(struct perf_sched *sched,
23404c50563dSArnaldo Carvalho de Melo 				struct idle_thread_runtime *itr,
2341699b5b92SNamhyung Kim 				struct perf_sample *sample)
2342699b5b92SNamhyung Kim {
23438ab12a20SIan Rogers 	struct callchain_cursor *cursor;
23448ab12a20SIan Rogers 
23454c50563dSArnaldo Carvalho de Melo 	if (!sched->show_callchain || sample->callchain == NULL)
2346699b5b92SNamhyung Kim 		return;
2347699b5b92SNamhyung Kim 
23488ab12a20SIan Rogers 	cursor = get_tls_callchain_cursor();
23498ab12a20SIan Rogers 	if (cursor == NULL)
23508ab12a20SIan Rogers 		return;
23518ab12a20SIan Rogers 
23528ab12a20SIan Rogers 	callchain_cursor__copy(&itr->cursor, cursor);
2353699b5b92SNamhyung Kim }
2354699b5b92SNamhyung Kim 
timehist_get_thread(struct perf_sched * sched,struct perf_sample * sample,struct machine * machine,struct evsel * evsel)23556c973c90SDavid Ahern static struct thread *timehist_get_thread(struct perf_sched *sched,
23566c973c90SDavid Ahern 					  struct perf_sample *sample,
235749394a2aSDavid Ahern 					  struct machine *machine,
235832dcd021SJiri Olsa 					  struct evsel *evsel)
235949394a2aSDavid Ahern {
236049394a2aSDavid Ahern 	struct thread *thread;
236149394a2aSDavid Ahern 
236296039c7cSNamhyung Kim 	if (is_idle_sample(sample, evsel)) {
236349394a2aSDavid Ahern 		thread = get_idle_thread(sample->cpu);
236449394a2aSDavid Ahern 		if (thread == NULL)
236549394a2aSDavid Ahern 			pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
236649394a2aSDavid Ahern 
236749394a2aSDavid Ahern 	} else {
23685d92d96aSNamhyung Kim 		/* there were samples with tid 0 but non-zero pid */
23695d92d96aSNamhyung Kim 		thread = machine__findnew_thread(machine, sample->pid,
23705d92d96aSNamhyung Kim 						 sample->tid ?: sample->pid);
237149394a2aSDavid Ahern 		if (thread == NULL) {
237249394a2aSDavid Ahern 			pr_debug("Failed to get thread for tid %d. skipping sample.\n",
237349394a2aSDavid Ahern 				 sample->tid);
237449394a2aSDavid Ahern 		}
237596039c7cSNamhyung Kim 
237696039c7cSNamhyung Kim 		save_task_callchain(sched, sample, evsel, machine);
2377699b5b92SNamhyung Kim 		if (sched->idle_hist) {
2378699b5b92SNamhyung Kim 			struct thread *idle;
2379699b5b92SNamhyung Kim 			struct idle_thread_runtime *itr;
2380699b5b92SNamhyung Kim 
2381699b5b92SNamhyung Kim 			idle = get_idle_thread(sample->cpu);
2382699b5b92SNamhyung Kim 			if (idle == NULL) {
2383699b5b92SNamhyung Kim 				pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2384699b5b92SNamhyung Kim 				return NULL;
2385699b5b92SNamhyung Kim 			}
2386699b5b92SNamhyung Kim 
2387699b5b92SNamhyung Kim 			itr = thread__priv(idle);
2388699b5b92SNamhyung Kim 			if (itr == NULL)
2389699b5b92SNamhyung Kim 				return NULL;
2390699b5b92SNamhyung Kim 
2391699b5b92SNamhyung Kim 			itr->last_thread = thread;
2392699b5b92SNamhyung Kim 
2393699b5b92SNamhyung Kim 			/* copy task callchain when entering to idle */
2394efc0cdc9SArnaldo Carvalho de Melo 			if (evsel__intval(evsel, sample, "next_pid") == 0)
23954c50563dSArnaldo Carvalho de Melo 				save_idle_callchain(sched, itr, sample);
2396699b5b92SNamhyung Kim 		}
239749394a2aSDavid Ahern 	}
239849394a2aSDavid Ahern 
239949394a2aSDavid Ahern 	return thread;
240049394a2aSDavid Ahern }
240149394a2aSDavid Ahern 
timehist_skip_sample(struct perf_sched * sched,struct thread * thread,struct evsel * evsel,struct perf_sample * sample)240252df138cSDavid Ahern static bool timehist_skip_sample(struct perf_sched *sched,
2403a4b2b6f5SNamhyung Kim 				 struct thread *thread,
240432dcd021SJiri Olsa 				 struct evsel *evsel,
2405a4b2b6f5SNamhyung Kim 				 struct perf_sample *sample)
240649394a2aSDavid Ahern {
240749394a2aSDavid Ahern 	bool rc = false;
240849394a2aSDavid Ahern 
240952df138cSDavid Ahern 	if (thread__is_filtered(thread)) {
241049394a2aSDavid Ahern 		rc = true;
241152df138cSDavid Ahern 		sched->skipped_samples++;
241252df138cSDavid Ahern 	}
241349394a2aSDavid Ahern 
2414a4b2b6f5SNamhyung Kim 	if (sched->idle_hist) {
24158ab2e96dSArnaldo Carvalho de Melo 		if (strcmp(evsel__name(evsel), "sched:sched_switch"))
2416a4b2b6f5SNamhyung Kim 			rc = true;
2417efc0cdc9SArnaldo Carvalho de Melo 		else if (evsel__intval(evsel, sample, "prev_pid") != 0 &&
2418efc0cdc9SArnaldo Carvalho de Melo 			 evsel__intval(evsel, sample, "next_pid") != 0)
2419a4b2b6f5SNamhyung Kim 			rc = true;
2420a4b2b6f5SNamhyung Kim 	}
2421a4b2b6f5SNamhyung Kim 
242249394a2aSDavid Ahern 	return rc;
242349394a2aSDavid Ahern }
242449394a2aSDavid Ahern 
timehist_print_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine,struct thread * awakened)2425fc1469f1SDavid Ahern static void timehist_print_wakeup_event(struct perf_sched *sched,
242632dcd021SJiri Olsa 					struct evsel *evsel,
2427fc1469f1SDavid Ahern 					struct perf_sample *sample,
2428fc1469f1SDavid Ahern 					struct machine *machine,
2429fc1469f1SDavid Ahern 					struct thread *awakened)
2430fc1469f1SDavid Ahern {
2431fc1469f1SDavid Ahern 	struct thread *thread;
2432fc1469f1SDavid Ahern 	char tstr[64];
2433fc1469f1SDavid Ahern 
2434fc1469f1SDavid Ahern 	thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2435fc1469f1SDavid Ahern 	if (thread == NULL)
2436fc1469f1SDavid Ahern 		return;
2437fc1469f1SDavid Ahern 
2438fc1469f1SDavid Ahern 	/* show wakeup unless both awakee and awaker are filtered */
2439a4b2b6f5SNamhyung Kim 	if (timehist_skip_sample(sched, thread, evsel, sample) &&
2440a4b2b6f5SNamhyung Kim 	    timehist_skip_sample(sched, awakened, evsel, sample)) {
2441fc1469f1SDavid Ahern 		return;
2442fc1469f1SDavid Ahern 	}
2443fc1469f1SDavid Ahern 
2444fc1469f1SDavid Ahern 	timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2445fc1469f1SDavid Ahern 	printf("%15s [%04d] ", tstr, sample->cpu);
2446a407b067SDavid Ahern 	if (sched->show_cpu_visual)
24476d18804bSIan Rogers 		printf(" %*s ", sched->max_cpu.cpu + 1, "");
2448fc1469f1SDavid Ahern 
2449fc1469f1SDavid Ahern 	printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2450fc1469f1SDavid Ahern 
2451fc1469f1SDavid Ahern 	/* dt spacer */
2452fc1469f1SDavid Ahern 	printf("  %9s  %9s  %9s ", "", "", "");
2453fc1469f1SDavid Ahern 
2454fc1469f1SDavid Ahern 	printf("awakened: %s", timehist_get_commstr(awakened));
2455fc1469f1SDavid Ahern 
2456fc1469f1SDavid Ahern 	printf("\n");
2457fc1469f1SDavid Ahern }
2458fc1469f1SDavid Ahern 
timehist_sched_wakeup_ignore(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct evsel * evsel __maybe_unused,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)2459d566a9c2SDavid Ahern static int timehist_sched_wakeup_ignore(struct perf_tool *tool __maybe_unused,
2460d566a9c2SDavid Ahern 					union perf_event *event __maybe_unused,
2461d566a9c2SDavid Ahern 					struct evsel *evsel __maybe_unused,
2462d566a9c2SDavid Ahern 					struct perf_sample *sample __maybe_unused,
2463d566a9c2SDavid Ahern 					struct machine *machine __maybe_unused)
2464d566a9c2SDavid Ahern {
2465d566a9c2SDavid Ahern 	return 0;
2466d566a9c2SDavid Ahern }
2467d566a9c2SDavid Ahern 
timehist_sched_wakeup_event(struct perf_tool * tool,union perf_event * event __maybe_unused,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)2468fc1469f1SDavid Ahern static int timehist_sched_wakeup_event(struct perf_tool *tool,
246949394a2aSDavid Ahern 				       union perf_event *event __maybe_unused,
247032dcd021SJiri Olsa 				       struct evsel *evsel,
247149394a2aSDavid Ahern 				       struct perf_sample *sample,
247249394a2aSDavid Ahern 				       struct machine *machine)
247349394a2aSDavid Ahern {
2474fc1469f1SDavid Ahern 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
247549394a2aSDavid Ahern 	struct thread *thread;
247649394a2aSDavid Ahern 	struct thread_runtime *tr = NULL;
247749394a2aSDavid Ahern 	/* want pid of awakened task not pid in sample */
2478efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid = evsel__intval(evsel, sample, "pid");
247949394a2aSDavid Ahern 
248049394a2aSDavid Ahern 	thread = machine__findnew_thread(machine, 0, pid);
248149394a2aSDavid Ahern 	if (thread == NULL)
248249394a2aSDavid Ahern 		return -1;
248349394a2aSDavid Ahern 
248449394a2aSDavid Ahern 	tr = thread__get_runtime(thread);
248549394a2aSDavid Ahern 	if (tr == NULL)
248649394a2aSDavid Ahern 		return -1;
248749394a2aSDavid Ahern 
248849394a2aSDavid Ahern 	if (tr->ready_to_run == 0)
248949394a2aSDavid Ahern 		tr->ready_to_run = sample->time;
249049394a2aSDavid Ahern 
2491fc1469f1SDavid Ahern 	/* show wakeups if requested */
2492853b7407SDavid Ahern 	if (sched->show_wakeups &&
2493853b7407SDavid Ahern 	    !perf_time__skip_sample(&sched->ptime, sample->time))
2494a4b2b6f5SNamhyung Kim 		timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
2495fc1469f1SDavid Ahern 
249649394a2aSDavid Ahern 	return 0;
249749394a2aSDavid Ahern }
249849394a2aSDavid Ahern 
timehist_print_migration_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine,struct thread * migrated)2499350f54faSDavid Ahern static void timehist_print_migration_event(struct perf_sched *sched,
250032dcd021SJiri Olsa 					struct evsel *evsel,
2501350f54faSDavid Ahern 					struct perf_sample *sample,
2502350f54faSDavid Ahern 					struct machine *machine,
2503350f54faSDavid Ahern 					struct thread *migrated)
2504350f54faSDavid Ahern {
2505350f54faSDavid Ahern 	struct thread *thread;
2506350f54faSDavid Ahern 	char tstr[64];
25076d18804bSIan Rogers 	u32 max_cpus;
2508350f54faSDavid Ahern 	u32 ocpu, dcpu;
2509350f54faSDavid Ahern 
2510350f54faSDavid Ahern 	if (sched->summary_only)
2511350f54faSDavid Ahern 		return;
2512350f54faSDavid Ahern 
25136d18804bSIan Rogers 	max_cpus = sched->max_cpu.cpu + 1;
2514efc0cdc9SArnaldo Carvalho de Melo 	ocpu = evsel__intval(evsel, sample, "orig_cpu");
2515efc0cdc9SArnaldo Carvalho de Melo 	dcpu = evsel__intval(evsel, sample, "dest_cpu");
2516350f54faSDavid Ahern 
2517350f54faSDavid Ahern 	thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2518350f54faSDavid Ahern 	if (thread == NULL)
2519350f54faSDavid Ahern 		return;
2520350f54faSDavid Ahern 
2521a4b2b6f5SNamhyung Kim 	if (timehist_skip_sample(sched, thread, evsel, sample) &&
2522a4b2b6f5SNamhyung Kim 	    timehist_skip_sample(sched, migrated, evsel, sample)) {
2523350f54faSDavid Ahern 		return;
2524350f54faSDavid Ahern 	}
2525350f54faSDavid Ahern 
2526350f54faSDavid Ahern 	timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2527350f54faSDavid Ahern 	printf("%15s [%04d] ", tstr, sample->cpu);
2528350f54faSDavid Ahern 
2529350f54faSDavid Ahern 	if (sched->show_cpu_visual) {
2530350f54faSDavid Ahern 		u32 i;
2531350f54faSDavid Ahern 		char c;
2532350f54faSDavid Ahern 
2533350f54faSDavid Ahern 		printf("  ");
2534350f54faSDavid Ahern 		for (i = 0; i < max_cpus; ++i) {
2535350f54faSDavid Ahern 			c = (i == sample->cpu) ? 'm' : ' ';
2536350f54faSDavid Ahern 			printf("%c", c);
2537350f54faSDavid Ahern 		}
2538350f54faSDavid Ahern 		printf("  ");
2539350f54faSDavid Ahern 	}
2540350f54faSDavid Ahern 
2541350f54faSDavid Ahern 	printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2542350f54faSDavid Ahern 
2543350f54faSDavid Ahern 	/* dt spacer */
2544350f54faSDavid Ahern 	printf("  %9s  %9s  %9s ", "", "", "");
2545350f54faSDavid Ahern 
2546350f54faSDavid Ahern 	printf("migrated: %s", timehist_get_commstr(migrated));
2547350f54faSDavid Ahern 	printf(" cpu %d => %d", ocpu, dcpu);
2548350f54faSDavid Ahern 
2549350f54faSDavid Ahern 	printf("\n");
2550350f54faSDavid Ahern }
2551350f54faSDavid Ahern 
timehist_migrate_task_event(struct perf_tool * tool,union perf_event * event __maybe_unused,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)2552350f54faSDavid Ahern static int timehist_migrate_task_event(struct perf_tool *tool,
2553350f54faSDavid Ahern 				       union perf_event *event __maybe_unused,
255432dcd021SJiri Olsa 				       struct evsel *evsel,
2555350f54faSDavid Ahern 				       struct perf_sample *sample,
2556350f54faSDavid Ahern 				       struct machine *machine)
2557350f54faSDavid Ahern {
2558350f54faSDavid Ahern 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2559350f54faSDavid Ahern 	struct thread *thread;
2560350f54faSDavid Ahern 	struct thread_runtime *tr = NULL;
2561350f54faSDavid Ahern 	/* want pid of migrated task not pid in sample */
2562efc0cdc9SArnaldo Carvalho de Melo 	const u32 pid = evsel__intval(evsel, sample, "pid");
2563350f54faSDavid Ahern 
2564350f54faSDavid Ahern 	thread = machine__findnew_thread(machine, 0, pid);
2565350f54faSDavid Ahern 	if (thread == NULL)
2566350f54faSDavid Ahern 		return -1;
2567350f54faSDavid Ahern 
2568350f54faSDavid Ahern 	tr = thread__get_runtime(thread);
2569350f54faSDavid Ahern 	if (tr == NULL)
2570350f54faSDavid Ahern 		return -1;
2571350f54faSDavid Ahern 
2572350f54faSDavid Ahern 	tr->migrations++;
2573350f54faSDavid Ahern 
2574350f54faSDavid Ahern 	/* show migrations if requested */
2575350f54faSDavid Ahern 	timehist_print_migration_event(sched, evsel, sample, machine, thread);
2576350f54faSDavid Ahern 
2577350f54faSDavid Ahern 	return 0;
2578350f54faSDavid Ahern }
2579350f54faSDavid Ahern 
timehist_sched_change_event(struct perf_tool * tool,union perf_event * event,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)258052df138cSDavid Ahern static int timehist_sched_change_event(struct perf_tool *tool,
258149394a2aSDavid Ahern 				       union perf_event *event,
258232dcd021SJiri Olsa 				       struct evsel *evsel,
258349394a2aSDavid Ahern 				       struct perf_sample *sample,
258449394a2aSDavid Ahern 				       struct machine *machine)
258549394a2aSDavid Ahern {
2586fc1469f1SDavid Ahern 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2587853b7407SDavid Ahern 	struct perf_time_interval *ptime = &sched->ptime;
258849394a2aSDavid Ahern 	struct addr_location al;
258949394a2aSDavid Ahern 	struct thread *thread;
259049394a2aSDavid Ahern 	struct thread_runtime *tr = NULL;
2591853b7407SDavid Ahern 	u64 tprev, t = sample->time;
259249394a2aSDavid Ahern 	int rc = 0;
2593efc0cdc9SArnaldo Carvalho de Melo 	int state = evsel__intval(evsel, sample, "prev_state");
259449394a2aSDavid Ahern 
25950dd5041cSIan Rogers 	addr_location__init(&al);
259649394a2aSDavid Ahern 	if (machine__resolve(machine, &al, sample) < 0) {
259749394a2aSDavid Ahern 		pr_err("problem processing %d event. skipping it\n",
259849394a2aSDavid Ahern 		       event->header.type);
259949394a2aSDavid Ahern 		rc = -1;
260049394a2aSDavid Ahern 		goto out;
260149394a2aSDavid Ahern 	}
260249394a2aSDavid Ahern 
26036c973c90SDavid Ahern 	thread = timehist_get_thread(sched, sample, machine, evsel);
260449394a2aSDavid Ahern 	if (thread == NULL) {
260549394a2aSDavid Ahern 		rc = -1;
260649394a2aSDavid Ahern 		goto out;
260749394a2aSDavid Ahern 	}
260849394a2aSDavid Ahern 
2609a4b2b6f5SNamhyung Kim 	if (timehist_skip_sample(sched, thread, evsel, sample))
261049394a2aSDavid Ahern 		goto out;
261149394a2aSDavid Ahern 
261249394a2aSDavid Ahern 	tr = thread__get_runtime(thread);
261349394a2aSDavid Ahern 	if (tr == NULL) {
261449394a2aSDavid Ahern 		rc = -1;
261549394a2aSDavid Ahern 		goto out;
261649394a2aSDavid Ahern 	}
261749394a2aSDavid Ahern 
26183b7313f2SArnaldo Carvalho de Melo 	tprev = evsel__get_time(evsel, sample->cpu);
261949394a2aSDavid Ahern 
2620853b7407SDavid Ahern 	/*
2621853b7407SDavid Ahern 	 * If start time given:
2622853b7407SDavid Ahern 	 * - sample time is under window user cares about - skip sample
2623853b7407SDavid Ahern 	 * - tprev is under window user cares about  - reset to start of window
2624853b7407SDavid Ahern 	 */
2625853b7407SDavid Ahern 	if (ptime->start && ptime->start > t)
2626853b7407SDavid Ahern 		goto out;
2627853b7407SDavid Ahern 
2628bdd75729SNamhyung Kim 	if (tprev && ptime->start > tprev)
2629853b7407SDavid Ahern 		tprev = ptime->start;
2630853b7407SDavid Ahern 
2631853b7407SDavid Ahern 	/*
2632853b7407SDavid Ahern 	 * If end time given:
2633853b7407SDavid Ahern 	 * - previous sched event is out of window - we are done
2634853b7407SDavid Ahern 	 * - sample time is beyond window user cares about - reset it
2635853b7407SDavid Ahern 	 *   to close out stats for time window interest
2636022f9328SYang Jihong 	 * - If tprev is 0, that is, sched_in event for current task is
2637022f9328SYang Jihong 	 *   not recorded, cannot determine whether sched_in event is
2638022f9328SYang Jihong 	 *   within time window interest - ignore it
2639853b7407SDavid Ahern 	 */
2640853b7407SDavid Ahern 	if (ptime->end) {
2641022f9328SYang Jihong 		if (!tprev || tprev > ptime->end)
2642853b7407SDavid Ahern 			goto out;
2643853b7407SDavid Ahern 
2644853b7407SDavid Ahern 		if (t > ptime->end)
2645853b7407SDavid Ahern 			t = ptime->end;
2646853b7407SDavid Ahern 	}
2647853b7407SDavid Ahern 
2648ee84a303SIan Rogers 	if (!sched->idle_hist || thread__tid(thread) == 0) {
2649a74eaf16SDavid Ahern 		if (!cpu_list || test_bit(sample->cpu, cpu_bitmap))
2650853b7407SDavid Ahern 			timehist_update_runtime_stats(tr, t, tprev);
2651853b7407SDavid Ahern 
265207235f84SNamhyung Kim 		if (sched->idle_hist) {
265307235f84SNamhyung Kim 			struct idle_thread_runtime *itr = (void *)tr;
265407235f84SNamhyung Kim 			struct thread_runtime *last_tr;
265507235f84SNamhyung Kim 
2656ee84a303SIan Rogers 			BUG_ON(thread__tid(thread) != 0);
265707235f84SNamhyung Kim 
265807235f84SNamhyung Kim 			if (itr->last_thread == NULL)
265907235f84SNamhyung Kim 				goto out;
266007235f84SNamhyung Kim 
266107235f84SNamhyung Kim 			/* add current idle time as last thread's runtime */
266207235f84SNamhyung Kim 			last_tr = thread__get_runtime(itr->last_thread);
266307235f84SNamhyung Kim 			if (last_tr == NULL)
266407235f84SNamhyung Kim 				goto out;
266507235f84SNamhyung Kim 
266607235f84SNamhyung Kim 			timehist_update_runtime_stats(last_tr, t, tprev);
266707235f84SNamhyung Kim 			/*
266807235f84SNamhyung Kim 			 * remove delta time of last thread as it's not updated
266907235f84SNamhyung Kim 			 * and otherwise it will show an invalid value next
267007235f84SNamhyung Kim 			 * time.  we only care total run time and run stat.
267107235f84SNamhyung Kim 			 */
267207235f84SNamhyung Kim 			last_tr->dt_run = 0;
267307235f84SNamhyung Kim 			last_tr->dt_delay = 0;
2674941bdea7SNamhyung Kim 			last_tr->dt_sleep = 0;
2675941bdea7SNamhyung Kim 			last_tr->dt_iowait = 0;
2676941bdea7SNamhyung Kim 			last_tr->dt_preempt = 0;
267707235f84SNamhyung Kim 
2678ba957ebbSNamhyung Kim 			if (itr->cursor.nr)
2679ba957ebbSNamhyung Kim 				callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2680ba957ebbSNamhyung Kim 
268107235f84SNamhyung Kim 			itr->last_thread = NULL;
268207235f84SNamhyung Kim 		}
268307235f84SNamhyung Kim 	}
268407235f84SNamhyung Kim 
268552df138cSDavid Ahern 	if (!sched->summary_only)
2686292c4a8fSBrendan Gregg 		timehist_print_sample(sched, evsel, sample, &al, thread, t, state);
268749394a2aSDavid Ahern 
268849394a2aSDavid Ahern out:
26899396c9cbSNamhyung Kim 	if (sched->hist_time.start == 0 && t >= ptime->start)
26909396c9cbSNamhyung Kim 		sched->hist_time.start = t;
26919396c9cbSNamhyung Kim 	if (ptime->end == 0 || t <= ptime->end)
26929396c9cbSNamhyung Kim 		sched->hist_time.end = t;
26939396c9cbSNamhyung Kim 
269449394a2aSDavid Ahern 	if (tr) {
269549394a2aSDavid Ahern 		/* time of this sched_switch event becomes last time task seen */
269649394a2aSDavid Ahern 		tr->last_time = sample->time;
269749394a2aSDavid Ahern 
2698941bdea7SNamhyung Kim 		/* last state is used to determine where to account wait time */
2699414e050cSNamhyung Kim 		tr->last_state = state;
2700941bdea7SNamhyung Kim 
270149394a2aSDavid Ahern 		/* sched out event for task so reset ready to run time */
270249394a2aSDavid Ahern 		tr->ready_to_run = 0;
270349394a2aSDavid Ahern 	}
270449394a2aSDavid Ahern 
27053b7313f2SArnaldo Carvalho de Melo 	evsel__save_time(evsel, sample->time, sample->cpu);
270649394a2aSDavid Ahern 
27070dd5041cSIan Rogers 	addr_location__exit(&al);
270849394a2aSDavid Ahern 	return rc;
270949394a2aSDavid Ahern }
271049394a2aSDavid Ahern 
timehist_sched_switch_event(struct perf_tool * tool,union perf_event * event,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)271149394a2aSDavid Ahern static int timehist_sched_switch_event(struct perf_tool *tool,
271249394a2aSDavid Ahern 			     union perf_event *event,
271332dcd021SJiri Olsa 			     struct evsel *evsel,
271449394a2aSDavid Ahern 			     struct perf_sample *sample,
271549394a2aSDavid Ahern 			     struct machine *machine __maybe_unused)
271649394a2aSDavid Ahern {
271749394a2aSDavid Ahern 	return timehist_sched_change_event(tool, event, evsel, sample, machine);
271849394a2aSDavid Ahern }
271949394a2aSDavid Ahern 
process_lost(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine __maybe_unused)272049394a2aSDavid Ahern static int process_lost(struct perf_tool *tool __maybe_unused,
272149394a2aSDavid Ahern 			union perf_event *event,
272249394a2aSDavid Ahern 			struct perf_sample *sample,
272349394a2aSDavid Ahern 			struct machine *machine __maybe_unused)
272449394a2aSDavid Ahern {
272549394a2aSDavid Ahern 	char tstr[64];
272649394a2aSDavid Ahern 
272749394a2aSDavid Ahern 	timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
272849394a2aSDavid Ahern 	printf("%15s ", tstr);
27295290ed69SJiri Olsa 	printf("lost %" PRI_lu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
273049394a2aSDavid Ahern 
273149394a2aSDavid Ahern 	return 0;
273249394a2aSDavid Ahern }
273349394a2aSDavid Ahern 
273449394a2aSDavid Ahern 
print_thread_runtime(struct thread * t,struct thread_runtime * r)273552df138cSDavid Ahern static void print_thread_runtime(struct thread *t,
273652df138cSDavid Ahern 				 struct thread_runtime *r)
273752df138cSDavid Ahern {
273852df138cSDavid Ahern 	double mean = avg_stats(&r->run_stats);
273952df138cSDavid Ahern 	float stddev;
274052df138cSDavid Ahern 
274152df138cSDavid Ahern 	printf("%*s   %5d  %9" PRIu64 " ",
2742ee84a303SIan Rogers 	       comm_width, timehist_get_commstr(t), thread__ppid(t),
274352df138cSDavid Ahern 	       (u64) r->run_stats.n);
274452df138cSDavid Ahern 
274552df138cSDavid Ahern 	print_sched_time(r->total_run_time, 8);
274652df138cSDavid Ahern 	stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
274752df138cSDavid Ahern 	print_sched_time(r->run_stats.min, 6);
274852df138cSDavid Ahern 	printf(" ");
274952df138cSDavid Ahern 	print_sched_time((u64) mean, 6);
275052df138cSDavid Ahern 	printf(" ");
275152df138cSDavid Ahern 	print_sched_time(r->run_stats.max, 6);
275252df138cSDavid Ahern 	printf("  ");
275352df138cSDavid Ahern 	printf("%5.2f", stddev);
2754350f54faSDavid Ahern 	printf("   %5" PRIu64, r->migrations);
275552df138cSDavid Ahern 	printf("\n");
275652df138cSDavid Ahern }
275752df138cSDavid Ahern 
print_thread_waittime(struct thread * t,struct thread_runtime * r)2758587782c5SNamhyung Kim static void print_thread_waittime(struct thread *t,
2759587782c5SNamhyung Kim 				  struct thread_runtime *r)
2760587782c5SNamhyung Kim {
2761587782c5SNamhyung Kim 	printf("%*s   %5d  %9" PRIu64 " ",
2762ee84a303SIan Rogers 	       comm_width, timehist_get_commstr(t), thread__ppid(t),
2763587782c5SNamhyung Kim 	       (u64) r->run_stats.n);
2764587782c5SNamhyung Kim 
2765587782c5SNamhyung Kim 	print_sched_time(r->total_run_time, 8);
2766587782c5SNamhyung Kim 	print_sched_time(r->total_sleep_time, 6);
2767587782c5SNamhyung Kim 	printf(" ");
2768587782c5SNamhyung Kim 	print_sched_time(r->total_iowait_time, 6);
2769587782c5SNamhyung Kim 	printf(" ");
2770587782c5SNamhyung Kim 	print_sched_time(r->total_preempt_time, 6);
2771587782c5SNamhyung Kim 	printf(" ");
2772587782c5SNamhyung Kim 	print_sched_time(r->total_delay_time, 6);
2773587782c5SNamhyung Kim 	printf("\n");
2774587782c5SNamhyung Kim }
2775587782c5SNamhyung Kim 
277652df138cSDavid Ahern struct total_run_stats {
2777587782c5SNamhyung Kim 	struct perf_sched *sched;
277852df138cSDavid Ahern 	u64  sched_count;
277952df138cSDavid Ahern 	u64  task_count;
278052df138cSDavid Ahern 	u64  total_run_time;
278152df138cSDavid Ahern };
278252df138cSDavid Ahern 
show_thread_runtime(struct thread * t,void * priv)278340826c45SIan Rogers static int show_thread_runtime(struct thread *t, void *priv)
278452df138cSDavid Ahern {
278552df138cSDavid Ahern 	struct total_run_stats *stats = priv;
278652df138cSDavid Ahern 	struct thread_runtime *r;
278752df138cSDavid Ahern 
278852df138cSDavid Ahern 	if (thread__is_filtered(t))
278952df138cSDavid Ahern 		return 0;
279052df138cSDavid Ahern 
279152df138cSDavid Ahern 	r = thread__priv(t);
279252df138cSDavid Ahern 	if (r && r->run_stats.n) {
279352df138cSDavid Ahern 		stats->task_count++;
279452df138cSDavid Ahern 		stats->sched_count += r->run_stats.n;
279552df138cSDavid Ahern 		stats->total_run_time += r->total_run_time;
2796587782c5SNamhyung Kim 
2797587782c5SNamhyung Kim 		if (stats->sched->show_state)
2798587782c5SNamhyung Kim 			print_thread_waittime(t, r);
2799587782c5SNamhyung Kim 		else
280052df138cSDavid Ahern 			print_thread_runtime(t, r);
280152df138cSDavid Ahern 	}
280252df138cSDavid Ahern 
280352df138cSDavid Ahern 	return 0;
280452df138cSDavid Ahern }
280552df138cSDavid Ahern 
callchain__fprintf_folded(FILE * fp,struct callchain_node * node)2806ba957ebbSNamhyung Kim static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2807ba957ebbSNamhyung Kim {
2808ba957ebbSNamhyung Kim 	const char *sep = " <- ";
2809ba957ebbSNamhyung Kim 	struct callchain_list *chain;
2810ba957ebbSNamhyung Kim 	size_t ret = 0;
2811ba957ebbSNamhyung Kim 	char bf[1024];
2812ba957ebbSNamhyung Kim 	bool first;
2813ba957ebbSNamhyung Kim 
2814ba957ebbSNamhyung Kim 	if (node == NULL)
2815ba957ebbSNamhyung Kim 		return 0;
2816ba957ebbSNamhyung Kim 
2817ba957ebbSNamhyung Kim 	ret = callchain__fprintf_folded(fp, node->parent);
2818ba957ebbSNamhyung Kim 	first = (ret == 0);
2819ba957ebbSNamhyung Kim 
2820ba957ebbSNamhyung Kim 	list_for_each_entry(chain, &node->val, list) {
2821ba957ebbSNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
2822ba957ebbSNamhyung Kim 			continue;
2823ba957ebbSNamhyung Kim 		if (chain->ms.sym && chain->ms.sym->ignore)
2824ba957ebbSNamhyung Kim 			continue;
2825ba957ebbSNamhyung Kim 		ret += fprintf(fp, "%s%s", first ? "" : sep,
2826ba957ebbSNamhyung Kim 			       callchain_list__sym_name(chain, bf, sizeof(bf),
2827ba957ebbSNamhyung Kim 							false));
2828ba957ebbSNamhyung Kim 		first = false;
2829ba957ebbSNamhyung Kim 	}
2830ba957ebbSNamhyung Kim 
2831ba957ebbSNamhyung Kim 	return ret;
2832ba957ebbSNamhyung Kim }
2833ba957ebbSNamhyung Kim 
timehist_print_idlehist_callchain(struct rb_root_cached * root)2834cb4c13a5SDavidlohr Bueso static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)
2835ba957ebbSNamhyung Kim {
2836ba957ebbSNamhyung Kim 	size_t ret = 0;
2837ba957ebbSNamhyung Kim 	FILE *fp = stdout;
2838ba957ebbSNamhyung Kim 	struct callchain_node *chain;
2839cb4c13a5SDavidlohr Bueso 	struct rb_node *rb_node = rb_first_cached(root);
2840ba957ebbSNamhyung Kim 
2841ba957ebbSNamhyung Kim 	printf("  %16s  %8s  %s\n", "Idle time (msec)", "Count", "Callchains");
2842ba957ebbSNamhyung Kim 	printf("  %.16s  %.8s  %.50s\n", graph_dotted_line, graph_dotted_line,
2843ba957ebbSNamhyung Kim 	       graph_dotted_line);
2844ba957ebbSNamhyung Kim 
2845ba957ebbSNamhyung Kim 	while (rb_node) {
2846ba957ebbSNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
2847ba957ebbSNamhyung Kim 		rb_node = rb_next(rb_node);
2848ba957ebbSNamhyung Kim 
2849ba957ebbSNamhyung Kim 		ret += fprintf(fp, "  ");
2850ba957ebbSNamhyung Kim 		print_sched_time(chain->hit, 12);
2851ba957ebbSNamhyung Kim 		ret += 16;  /* print_sched_time returns 2nd arg + 4 */
2852ba957ebbSNamhyung Kim 		ret += fprintf(fp, " %8d  ", chain->count);
2853ba957ebbSNamhyung Kim 		ret += callchain__fprintf_folded(fp, chain);
2854ba957ebbSNamhyung Kim 		ret += fprintf(fp, "\n");
2855ba957ebbSNamhyung Kim 	}
2856ba957ebbSNamhyung Kim 
2857ba957ebbSNamhyung Kim 	return ret;
2858ba957ebbSNamhyung Kim }
2859ba957ebbSNamhyung Kim 
timehist_print_summary(struct perf_sched * sched,struct perf_session * session)286052df138cSDavid Ahern static void timehist_print_summary(struct perf_sched *sched,
286152df138cSDavid Ahern 				   struct perf_session *session)
286252df138cSDavid Ahern {
286352df138cSDavid Ahern 	struct machine *m = &session->machines.host;
286452df138cSDavid Ahern 	struct total_run_stats totals;
286552df138cSDavid Ahern 	u64 task_count;
286652df138cSDavid Ahern 	struct thread *t;
286752df138cSDavid Ahern 	struct thread_runtime *r;
286852df138cSDavid Ahern 	int i;
28699396c9cbSNamhyung Kim 	u64 hist_time = sched->hist_time.end - sched->hist_time.start;
287052df138cSDavid Ahern 
287152df138cSDavid Ahern 	memset(&totals, 0, sizeof(totals));
2872587782c5SNamhyung Kim 	totals.sched = sched;
287352df138cSDavid Ahern 
287407235f84SNamhyung Kim 	if (sched->idle_hist) {
287507235f84SNamhyung Kim 		printf("\nIdle-time summary\n");
287607235f84SNamhyung Kim 		printf("%*s  parent  sched-out  ", comm_width, "comm");
287707235f84SNamhyung Kim 		printf("  idle-time   min-idle    avg-idle    max-idle  stddev  migrations\n");
2878587782c5SNamhyung Kim 	} else if (sched->show_state) {
2879587782c5SNamhyung Kim 		printf("\nWait-time summary\n");
2880587782c5SNamhyung Kim 		printf("%*s  parent   sched-in  ", comm_width, "comm");
2881587782c5SNamhyung Kim 		printf("   run-time      sleep      iowait     preempt       delay\n");
288207235f84SNamhyung Kim 	} else {
288352df138cSDavid Ahern 		printf("\nRuntime summary\n");
288452df138cSDavid Ahern 		printf("%*s  parent   sched-in  ", comm_width, "comm");
2885350f54faSDavid Ahern 		printf("   run-time    min-run     avg-run     max-run  stddev  migrations\n");
288607235f84SNamhyung Kim 	}
288752df138cSDavid Ahern 	printf("%*s            (count)  ", comm_width, "");
2888587782c5SNamhyung Kim 	printf("     (msec)     (msec)      (msec)      (msec)       %s\n",
2889587782c5SNamhyung Kim 	       sched->show_state ? "(msec)" : "%");
2890350f54faSDavid Ahern 	printf("%.117s\n", graph_dotted_line);
289152df138cSDavid Ahern 
289252df138cSDavid Ahern 	machine__for_each_thread(m, show_thread_runtime, &totals);
289352df138cSDavid Ahern 	task_count = totals.task_count;
289452df138cSDavid Ahern 	if (!task_count)
289552df138cSDavid Ahern 		printf("<no still running tasks>\n");
289652df138cSDavid Ahern 
289752df138cSDavid Ahern 	/* CPU idle stats not tracked when samples were skipped */
289807235f84SNamhyung Kim 	if (sched->skipped_samples && !sched->idle_hist)
289952df138cSDavid Ahern 		return;
290052df138cSDavid Ahern 
290152df138cSDavid Ahern 	printf("\nIdle stats:\n");
2902b336352bSNamhyung Kim 	for (i = 0; i < idle_max_cpu; ++i) {
2903a74eaf16SDavid Ahern 		if (cpu_list && !test_bit(i, cpu_bitmap))
2904a74eaf16SDavid Ahern 			continue;
2905a74eaf16SDavid Ahern 
290652df138cSDavid Ahern 		t = idle_threads[i];
290752df138cSDavid Ahern 		if (!t)
290852df138cSDavid Ahern 			continue;
290952df138cSDavid Ahern 
291052df138cSDavid Ahern 		r = thread__priv(t);
291152df138cSDavid Ahern 		if (r && r->run_stats.n) {
291252df138cSDavid Ahern 			totals.sched_count += r->run_stats.n;
291352df138cSDavid Ahern 			printf("    CPU %2d idle for ", i);
291452df138cSDavid Ahern 			print_sched_time(r->total_run_time, 6);
29159396c9cbSNamhyung Kim 			printf(" msec  (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
291652df138cSDavid Ahern 		} else
291752df138cSDavid Ahern 			printf("    CPU %2d idle entire time window\n", i);
291852df138cSDavid Ahern 	}
291952df138cSDavid Ahern 
29204c50563dSArnaldo Carvalho de Melo 	if (sched->idle_hist && sched->show_callchain) {
2921ba957ebbSNamhyung Kim 		callchain_param.mode  = CHAIN_FOLDED;
2922ba957ebbSNamhyung Kim 		callchain_param.value = CCVAL_PERIOD;
2923ba957ebbSNamhyung Kim 
2924ba957ebbSNamhyung Kim 		callchain_register_param(&callchain_param);
2925ba957ebbSNamhyung Kim 
2926ba957ebbSNamhyung Kim 		printf("\nIdle stats by callchain:\n");
2927ba957ebbSNamhyung Kim 		for (i = 0; i < idle_max_cpu; ++i) {
2928ba957ebbSNamhyung Kim 			struct idle_thread_runtime *itr;
2929ba957ebbSNamhyung Kim 
2930ba957ebbSNamhyung Kim 			t = idle_threads[i];
2931ba957ebbSNamhyung Kim 			if (!t)
2932ba957ebbSNamhyung Kim 				continue;
2933ba957ebbSNamhyung Kim 
2934ba957ebbSNamhyung Kim 			itr = thread__priv(t);
2935ba957ebbSNamhyung Kim 			if (itr == NULL)
2936ba957ebbSNamhyung Kim 				continue;
2937ba957ebbSNamhyung Kim 
2938cb4c13a5SDavidlohr Bueso 			callchain_param.sort(&itr->sorted_root.rb_root, &itr->callchain,
2939ba957ebbSNamhyung Kim 					     0, &callchain_param);
2940ba957ebbSNamhyung Kim 
2941ba957ebbSNamhyung Kim 			printf("  CPU %2d:", i);
2942ba957ebbSNamhyung Kim 			print_sched_time(itr->tr.total_run_time, 6);
2943ba957ebbSNamhyung Kim 			printf(" msec\n");
2944ba957ebbSNamhyung Kim 			timehist_print_idlehist_callchain(&itr->sorted_root);
2945ba957ebbSNamhyung Kim 			printf("\n");
2946ba957ebbSNamhyung Kim 		}
2947ba957ebbSNamhyung Kim 	}
2948ba957ebbSNamhyung Kim 
294952df138cSDavid Ahern 	printf("\n"
295052df138cSDavid Ahern 	       "    Total number of unique tasks: %" PRIu64 "\n"
29519396c9cbSNamhyung Kim 	       "Total number of context switches: %" PRIu64 "\n",
295252df138cSDavid Ahern 	       totals.task_count, totals.sched_count);
295352df138cSDavid Ahern 
29549396c9cbSNamhyung Kim 	printf("           Total run time (msec): ");
295552df138cSDavid Ahern 	print_sched_time(totals.total_run_time, 2);
295652df138cSDavid Ahern 	printf("\n");
29579396c9cbSNamhyung Kim 
29589396c9cbSNamhyung Kim 	printf("    Total scheduling time (msec): ");
29599396c9cbSNamhyung Kim 	print_sched_time(hist_time, 2);
29606d18804bSIan Rogers 	printf(" (x %d)\n", sched->max_cpu.cpu);
296152df138cSDavid Ahern }
296252df138cSDavid Ahern 
296349394a2aSDavid Ahern typedef int (*sched_handler)(struct perf_tool *tool,
296449394a2aSDavid Ahern 			  union perf_event *event,
296532dcd021SJiri Olsa 			  struct evsel *evsel,
296649394a2aSDavid Ahern 			  struct perf_sample *sample,
296749394a2aSDavid Ahern 			  struct machine *machine);
296849394a2aSDavid Ahern 
perf_timehist__process_sample(struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)296949394a2aSDavid Ahern static int perf_timehist__process_sample(struct perf_tool *tool,
297049394a2aSDavid Ahern 					 union perf_event *event,
297149394a2aSDavid Ahern 					 struct perf_sample *sample,
297232dcd021SJiri Olsa 					 struct evsel *evsel,
297349394a2aSDavid Ahern 					 struct machine *machine)
297449394a2aSDavid Ahern {
297549394a2aSDavid Ahern 	struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
297649394a2aSDavid Ahern 	int err = 0;
29776d18804bSIan Rogers 	struct perf_cpu this_cpu = {
29786d18804bSIan Rogers 		.cpu = sample->cpu,
29796d18804bSIan Rogers 	};
298049394a2aSDavid Ahern 
29816d18804bSIan Rogers 	if (this_cpu.cpu > sched->max_cpu.cpu)
298249394a2aSDavid Ahern 		sched->max_cpu = this_cpu;
298349394a2aSDavid Ahern 
298449394a2aSDavid Ahern 	if (evsel->handler != NULL) {
298549394a2aSDavid Ahern 		sched_handler f = evsel->handler;
298649394a2aSDavid Ahern 
298749394a2aSDavid Ahern 		err = f(tool, event, evsel, sample, machine);
298849394a2aSDavid Ahern 	}
298949394a2aSDavid Ahern 
299049394a2aSDavid Ahern 	return err;
299149394a2aSDavid Ahern }
299249394a2aSDavid Ahern 
timehist_check_attr(struct perf_sched * sched,struct evlist * evlist)29936c973c90SDavid Ahern static int timehist_check_attr(struct perf_sched *sched,
299463503dbaSJiri Olsa 			       struct evlist *evlist)
29956c973c90SDavid Ahern {
299632dcd021SJiri Olsa 	struct evsel *evsel;
29976c973c90SDavid Ahern 	struct evsel_runtime *er;
29986c973c90SDavid Ahern 
2999ce9036a6SJiri Olsa 	list_for_each_entry(evsel, &evlist->core.entries, core.node) {
30003b7313f2SArnaldo Carvalho de Melo 		er = evsel__get_runtime(evsel);
30016c973c90SDavid Ahern 		if (er == NULL) {
30026c973c90SDavid Ahern 			pr_err("Failed to allocate memory for evsel runtime data\n");
30036c973c90SDavid Ahern 			return -1;
30046c973c90SDavid Ahern 		}
30056c973c90SDavid Ahern 
300698ed4864SYang Jihong 		/* only need to save callchain related to sched_switch event */
300798ed4864SYang Jihong 		if (sched->show_callchain &&
300898ed4864SYang Jihong 		    evsel__name_is(evsel, "sched:sched_switch") &&
300998ed4864SYang Jihong 		    !evsel__has_callchain(evsel)) {
301098ed4864SYang Jihong 			pr_info("Samples of sched_switch event do not have callchains.\n");
30116c973c90SDavid Ahern 			sched->show_callchain = 0;
30126c973c90SDavid Ahern 			symbol_conf.use_callchain = 0;
30136c973c90SDavid Ahern 		}
30146c973c90SDavid Ahern 	}
30156c973c90SDavid Ahern 
30166c973c90SDavid Ahern 	return 0;
30176c973c90SDavid Ahern }
30186c973c90SDavid Ahern 
perf_sched__timehist(struct perf_sched * sched)301949394a2aSDavid Ahern static int perf_sched__timehist(struct perf_sched *sched)
302049394a2aSDavid Ahern {
3021d566a9c2SDavid Ahern 	struct evsel_str_handler handlers[] = {
302249394a2aSDavid Ahern 		{ "sched:sched_switch",       timehist_sched_switch_event, },
302349394a2aSDavid Ahern 		{ "sched:sched_wakeup",	      timehist_sched_wakeup_event, },
3024d566a9c2SDavid Ahern 		{ "sched:sched_waking",       timehist_sched_wakeup_event, },
302549394a2aSDavid Ahern 		{ "sched:sched_wakeup_new",   timehist_sched_wakeup_event, },
302649394a2aSDavid Ahern 	};
302732dcd021SJiri Olsa 	const struct evsel_str_handler migrate_handlers[] = {
3028350f54faSDavid Ahern 		{ "sched:sched_migrate_task", timehist_migrate_task_event, },
3029350f54faSDavid Ahern 	};
30308ceb41d7SJiri Olsa 	struct perf_data data = {
303149394a2aSDavid Ahern 		.path  = input_name,
303249394a2aSDavid Ahern 		.mode  = PERF_DATA_MODE_READ,
30336fa94258SNamhyung Kim 		.force = sched->force,
303449394a2aSDavid Ahern 	};
303549394a2aSDavid Ahern 
303649394a2aSDavid Ahern 	struct perf_session *session;
303763503dbaSJiri Olsa 	struct evlist *evlist;
303849394a2aSDavid Ahern 	int err = -1;
303949394a2aSDavid Ahern 
304049394a2aSDavid Ahern 	/*
304149394a2aSDavid Ahern 	 * event handlers for timehist option
304249394a2aSDavid Ahern 	 */
304349394a2aSDavid Ahern 	sched->tool.sample	 = perf_timehist__process_sample;
304449394a2aSDavid Ahern 	sched->tool.mmap	 = perf_event__process_mmap;
304549394a2aSDavid Ahern 	sched->tool.comm	 = perf_event__process_comm;
304649394a2aSDavid Ahern 	sched->tool.exit	 = perf_event__process_exit;
304749394a2aSDavid Ahern 	sched->tool.fork	 = perf_event__process_fork;
304849394a2aSDavid Ahern 	sched->tool.lost	 = process_lost;
304949394a2aSDavid Ahern 	sched->tool.attr	 = perf_event__process_attr;
305049394a2aSDavid Ahern 	sched->tool.tracing_data = perf_event__process_tracing_data;
305149394a2aSDavid Ahern 	sched->tool.build_id	 = perf_event__process_build_id;
305249394a2aSDavid Ahern 
305349394a2aSDavid Ahern 	sched->tool.ordered_events = true;
305449394a2aSDavid Ahern 	sched->tool.ordering_requires_timestamps = true;
305549394a2aSDavid Ahern 
30566c973c90SDavid Ahern 	symbol_conf.use_callchain = sched->show_callchain;
30576c973c90SDavid Ahern 
30582681bd85SNamhyung Kim 	session = perf_session__new(&data, &sched->tool);
30596ef81c55SMamatha Inamdar 	if (IS_ERR(session))
30606ef81c55SMamatha Inamdar 		return PTR_ERR(session);
306149394a2aSDavid Ahern 
3062c30d630dSDavid Ahern 	if (cpu_list) {
3063c30d630dSDavid Ahern 		err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3064c30d630dSDavid Ahern 		if (err < 0)
3065c30d630dSDavid Ahern 			goto out;
3066c30d630dSDavid Ahern 	}
3067c30d630dSDavid Ahern 
306852df138cSDavid Ahern 	evlist = session->evlist;
306952df138cSDavid Ahern 
307049394a2aSDavid Ahern 	symbol__init(&session->header.env);
307149394a2aSDavid Ahern 
3072853b7407SDavid Ahern 	if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
3073853b7407SDavid Ahern 		pr_err("Invalid time string\n");
3074505ec050SYang Jihong 		err = -EINVAL;
3075505ec050SYang Jihong 		goto out;
3076853b7407SDavid Ahern 	}
3077853b7407SDavid Ahern 
30786c973c90SDavid Ahern 	if (timehist_check_attr(sched, evlist) != 0)
30796c973c90SDavid Ahern 		goto out;
30806c973c90SDavid Ahern 
308149394a2aSDavid Ahern 	setup_pager();
308249394a2aSDavid Ahern 
3083d566a9c2SDavid Ahern 	/* prefer sched_waking if it is captured */
3084b02736f7SArnaldo Carvalho de Melo 	if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking"))
3085d566a9c2SDavid Ahern 		handlers[1].handler = timehist_sched_wakeup_ignore;
3086d566a9c2SDavid Ahern 
308749394a2aSDavid Ahern 	/* setup per-evsel handlers */
308849394a2aSDavid Ahern 	if (perf_session__set_tracepoints_handlers(session, handlers))
308949394a2aSDavid Ahern 		goto out;
309049394a2aSDavid Ahern 
3091f45bf8d3SDavid Ahern 	/* sched_switch event at a minimum needs to exist */
3092b02736f7SArnaldo Carvalho de Melo 	if (!evlist__find_tracepoint_by_name(session->evlist, "sched:sched_switch")) {
3093f45bf8d3SDavid Ahern 		pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
309449394a2aSDavid Ahern 		goto out;
3095f45bf8d3SDavid Ahern 	}
309649394a2aSDavid Ahern 
3097350f54faSDavid Ahern 	if (sched->show_migrations &&
3098350f54faSDavid Ahern 	    perf_session__set_tracepoints_handlers(session, migrate_handlers))
3099350f54faSDavid Ahern 		goto out;
3100350f54faSDavid Ahern 
310149394a2aSDavid Ahern 	/* pre-allocate struct for per-CPU idle stats */
31026d18804bSIan Rogers 	sched->max_cpu.cpu = session->header.env.nr_cpus_online;
31036d18804bSIan Rogers 	if (sched->max_cpu.cpu == 0)
31046d18804bSIan Rogers 		sched->max_cpu.cpu = 4;
31056d18804bSIan Rogers 	if (init_idle_threads(sched->max_cpu.cpu))
310649394a2aSDavid Ahern 		goto out;
310749394a2aSDavid Ahern 
310852df138cSDavid Ahern 	/* summary_only implies summary option, but don't overwrite summary if set */
310952df138cSDavid Ahern 	if (sched->summary_only)
311052df138cSDavid Ahern 		sched->summary = sched->summary_only;
311152df138cSDavid Ahern 
311252df138cSDavid Ahern 	if (!sched->summary_only)
3113a407b067SDavid Ahern 		timehist_header(sched);
311449394a2aSDavid Ahern 
311549394a2aSDavid Ahern 	err = perf_session__process_events(session);
311649394a2aSDavid Ahern 	if (err) {
311749394a2aSDavid Ahern 		pr_err("Failed to process events, error %d", err);
311849394a2aSDavid Ahern 		goto out;
311949394a2aSDavid Ahern 	}
312049394a2aSDavid Ahern 
312152df138cSDavid Ahern 	sched->nr_events      = evlist->stats.nr_events[0];
312252df138cSDavid Ahern 	sched->nr_lost_events = evlist->stats.total_lost;
312352df138cSDavid Ahern 	sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
312452df138cSDavid Ahern 
312552df138cSDavid Ahern 	if (sched->summary)
312652df138cSDavid Ahern 		timehist_print_summary(sched, session);
312752df138cSDavid Ahern 
312849394a2aSDavid Ahern out:
312949394a2aSDavid Ahern 	free_idle_threads();
313049394a2aSDavid Ahern 	perf_session__delete(session);
313149394a2aSDavid Ahern 
313249394a2aSDavid Ahern 	return err;
313349394a2aSDavid Ahern }
313449394a2aSDavid Ahern 
313549394a2aSDavid Ahern 
print_bad_events(struct perf_sched * sched)31360e9b07e5SArnaldo Carvalho de Melo static void print_bad_events(struct perf_sched *sched)
31370ec04e16SIngo Molnar {
31380e9b07e5SArnaldo Carvalho de Melo 	if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
31390ec04e16SIngo Molnar 		printf("  INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
31400e9b07e5SArnaldo Carvalho de Melo 			(double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
31410e9b07e5SArnaldo Carvalho de Melo 			sched->nr_unordered_timestamps, sched->nr_timestamps);
31420ec04e16SIngo Molnar 	}
31430e9b07e5SArnaldo Carvalho de Melo 	if (sched->nr_lost_events && sched->nr_events) {
31440ec04e16SIngo Molnar 		printf("  INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
31450e9b07e5SArnaldo Carvalho de Melo 			(double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
31460e9b07e5SArnaldo Carvalho de Melo 			sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
31470ec04e16SIngo Molnar 	}
31480e9b07e5SArnaldo Carvalho de Melo 	if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
31490ec04e16SIngo Molnar 		printf("  INFO: %.3f%% context switch bugs (%ld out of %ld)",
31500e9b07e5SArnaldo Carvalho de Melo 			(double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
31510e9b07e5SArnaldo Carvalho de Melo 			sched->nr_context_switch_bugs, sched->nr_timestamps);
31520e9b07e5SArnaldo Carvalho de Melo 		if (sched->nr_lost_events)
31530ec04e16SIngo Molnar 			printf(" (due to lost events?)");
31540ec04e16SIngo Molnar 		printf("\n");
31550ec04e16SIngo Molnar 	}
31560ec04e16SIngo Molnar }
31570ec04e16SIngo Molnar 
__merge_work_atoms(struct rb_root_cached * root,struct work_atoms * data)3158cb4c13a5SDavidlohr Bueso static void __merge_work_atoms(struct rb_root_cached *root, struct work_atoms *data)
31592f80dd44SJosef Bacik {
3160cb4c13a5SDavidlohr Bueso 	struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
31612f80dd44SJosef Bacik 	struct work_atoms *this;
31622f80dd44SJosef Bacik 	const char *comm = thread__comm_str(data->thread), *this_comm;
3163cb4c13a5SDavidlohr Bueso 	bool leftmost = true;
31642f80dd44SJosef Bacik 
31652f80dd44SJosef Bacik 	while (*new) {
31662f80dd44SJosef Bacik 		int cmp;
31672f80dd44SJosef Bacik 
31682f80dd44SJosef Bacik 		this = container_of(*new, struct work_atoms, node);
31692f80dd44SJosef Bacik 		parent = *new;
31702f80dd44SJosef Bacik 
31712f80dd44SJosef Bacik 		this_comm = thread__comm_str(this->thread);
31722f80dd44SJosef Bacik 		cmp = strcmp(comm, this_comm);
31732f80dd44SJosef Bacik 		if (cmp > 0) {
31742f80dd44SJosef Bacik 			new = &((*new)->rb_left);
31752f80dd44SJosef Bacik 		} else if (cmp < 0) {
31762f80dd44SJosef Bacik 			new = &((*new)->rb_right);
3177cb4c13a5SDavidlohr Bueso 			leftmost = false;
31782f80dd44SJosef Bacik 		} else {
31792f80dd44SJosef Bacik 			this->num_merged++;
31802f80dd44SJosef Bacik 			this->total_runtime += data->total_runtime;
31812f80dd44SJosef Bacik 			this->nb_atoms += data->nb_atoms;
31822f80dd44SJosef Bacik 			this->total_lat += data->total_lat;
31832f80dd44SJosef Bacik 			list_splice(&data->work_list, &this->work_list);
31842f80dd44SJosef Bacik 			if (this->max_lat < data->max_lat) {
31852f80dd44SJosef Bacik 				this->max_lat = data->max_lat;
3186dc000c45SJoel Fernandes (Google) 				this->max_lat_start = data->max_lat_start;
3187dc000c45SJoel Fernandes (Google) 				this->max_lat_end = data->max_lat_end;
31882f80dd44SJosef Bacik 			}
31892f80dd44SJosef Bacik 			zfree(&data);
31902f80dd44SJosef Bacik 			return;
31912f80dd44SJosef Bacik 		}
31922f80dd44SJosef Bacik 	}
31932f80dd44SJosef Bacik 
31942f80dd44SJosef Bacik 	data->num_merged++;
31952f80dd44SJosef Bacik 	rb_link_node(&data->node, parent, new);
3196cb4c13a5SDavidlohr Bueso 	rb_insert_color_cached(&data->node, root, leftmost);
31972f80dd44SJosef Bacik }
31982f80dd44SJosef Bacik 
perf_sched__merge_lat(struct perf_sched * sched)31992f80dd44SJosef Bacik static void perf_sched__merge_lat(struct perf_sched *sched)
32002f80dd44SJosef Bacik {
32012f80dd44SJosef Bacik 	struct work_atoms *data;
32022f80dd44SJosef Bacik 	struct rb_node *node;
32032f80dd44SJosef Bacik 
32042f80dd44SJosef Bacik 	if (sched->skip_merge)
32052f80dd44SJosef Bacik 		return;
32062f80dd44SJosef Bacik 
3207cb4c13a5SDavidlohr Bueso 	while ((node = rb_first_cached(&sched->atom_root))) {
3208cb4c13a5SDavidlohr Bueso 		rb_erase_cached(node, &sched->atom_root);
32092f80dd44SJosef Bacik 		data = rb_entry(node, struct work_atoms, node);
32102f80dd44SJosef Bacik 		__merge_work_atoms(&sched->merged_atom_root, data);
32112f80dd44SJosef Bacik 	}
32122f80dd44SJosef Bacik }
32132f80dd44SJosef Bacik 
setup_cpus_switch_event(struct perf_sched * sched)321403cec19fSYang Jihong static int setup_cpus_switch_event(struct perf_sched *sched)
321503cec19fSYang Jihong {
321603cec19fSYang Jihong 	unsigned int i;
321703cec19fSYang Jihong 
321803cec19fSYang Jihong 	sched->cpu_last_switched = calloc(MAX_CPUS, sizeof(*(sched->cpu_last_switched)));
321903cec19fSYang Jihong 	if (!sched->cpu_last_switched)
322003cec19fSYang Jihong 		return -1;
322103cec19fSYang Jihong 
322203cec19fSYang Jihong 	sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
322303cec19fSYang Jihong 	if (!sched->curr_pid) {
322403cec19fSYang Jihong 		zfree(&sched->cpu_last_switched);
322503cec19fSYang Jihong 		return -1;
322603cec19fSYang Jihong 	}
322703cec19fSYang Jihong 
322803cec19fSYang Jihong 	for (i = 0; i < MAX_CPUS; i++)
322903cec19fSYang Jihong 		sched->curr_pid[i] = -1;
323003cec19fSYang Jihong 
323103cec19fSYang Jihong 	return 0;
323203cec19fSYang Jihong }
323303cec19fSYang Jihong 
free_cpus_switch_event(struct perf_sched * sched)323403cec19fSYang Jihong static void free_cpus_switch_event(struct perf_sched *sched)
323503cec19fSYang Jihong {
323603cec19fSYang Jihong 	zfree(&sched->curr_pid);
323703cec19fSYang Jihong 	zfree(&sched->cpu_last_switched);
323803cec19fSYang Jihong }
323903cec19fSYang Jihong 
perf_sched__lat(struct perf_sched * sched)32400e9b07e5SArnaldo Carvalho de Melo static int perf_sched__lat(struct perf_sched *sched)
32410ec04e16SIngo Molnar {
324203cec19fSYang Jihong 	int rc = -1;
32430ec04e16SIngo Molnar 	struct rb_node *next;
32440ec04e16SIngo Molnar 
32450ec04e16SIngo Molnar 	setup_pager();
3246ad9def7cSDavid Ahern 
324703cec19fSYang Jihong 	if (setup_cpus_switch_event(sched))
324803cec19fSYang Jihong 		return rc;
324903cec19fSYang Jihong 
3250ae536acfSArnaldo Carvalho de Melo 	if (perf_sched__read_events(sched))
325103cec19fSYang Jihong 		goto out_free_cpus_switch_event;
3252ad9def7cSDavid Ahern 
32532f80dd44SJosef Bacik 	perf_sched__merge_lat(sched);
32540e9b07e5SArnaldo Carvalho de Melo 	perf_sched__sort_lat(sched);
32550ec04e16SIngo Molnar 
3256dc000c45SJoel Fernandes (Google) 	printf("\n -------------------------------------------------------------------------------------------------------------------------------------------\n");
3257dc000c45SJoel Fernandes (Google) 	printf("  Task                  |   Runtime ms  | Switches | Avg delay ms    | Max delay ms    | Max delay start           | Max delay end          |\n");
3258dc000c45SJoel Fernandes (Google) 	printf(" -------------------------------------------------------------------------------------------------------------------------------------------\n");
32590ec04e16SIngo Molnar 
3260cb4c13a5SDavidlohr Bueso 	next = rb_first_cached(&sched->sorted_atom_root);
32610ec04e16SIngo Molnar 
32620ec04e16SIngo Molnar 	while (next) {
32630ec04e16SIngo Molnar 		struct work_atoms *work_list;
32640ec04e16SIngo Molnar 
32650ec04e16SIngo Molnar 		work_list = rb_entry(next, struct work_atoms, node);
32660e9b07e5SArnaldo Carvalho de Melo 		output_lat_thread(sched, work_list);
32670ec04e16SIngo Molnar 		next = rb_next(next);
3268ae536acfSArnaldo Carvalho de Melo 		thread__zput(work_list->thread);
32690ec04e16SIngo Molnar 	}
32700ec04e16SIngo Molnar 
327180790e0bSRamkumar Ramachandra 	printf(" -----------------------------------------------------------------------------------------------------------------\n");
32729486aa38SArnaldo Carvalho de Melo 	printf("  TOTAL:                |%11.3f ms |%9" PRIu64 " |\n",
32734fc76e49SArnaldo Carvalho de Melo 		(double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
32740ec04e16SIngo Molnar 
32750ec04e16SIngo Molnar 	printf(" ---------------------------------------------------\n");
32760ec04e16SIngo Molnar 
32770e9b07e5SArnaldo Carvalho de Melo 	print_bad_events(sched);
32780ec04e16SIngo Molnar 	printf("\n");
32790ec04e16SIngo Molnar 
328003cec19fSYang Jihong 	rc = 0;
328103cec19fSYang Jihong 
328203cec19fSYang Jihong out_free_cpus_switch_event:
328303cec19fSYang Jihong 	free_cpus_switch_event(sched);
328403cec19fSYang Jihong 	return rc;
32850ec04e16SIngo Molnar }
32860ec04e16SIngo Molnar 
setup_map_cpus(struct perf_sched * sched)328799623c62SJiri Olsa static int setup_map_cpus(struct perf_sched *sched)
32880ec04e16SIngo Molnar {
32896d18804bSIan Rogers 	sched->max_cpu.cpu  = sysconf(_SC_NPROCESSORS_CONF);
329040749d0fSIngo Molnar 
329199623c62SJiri Olsa 	if (sched->map.comp) {
32926d18804bSIan Rogers 		sched->map.comp_cpus = zalloc(sched->max_cpu.cpu * sizeof(int));
3293cf294f24SJiri Olsa 		if (!sched->map.comp_cpus)
3294cf294f24SJiri Olsa 			return -1;
329599623c62SJiri Olsa 	}
329699623c62SJiri Olsa 
3297e649fb7fSYang Jihong 	if (sched->map.cpus_str) {
3298e649fb7fSYang Jihong 		sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
3299e649fb7fSYang Jihong 		if (!sched->map.cpus) {
330073643bb6SJiri Olsa 			pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3301e649fb7fSYang Jihong 			zfree(&sched->map.comp_cpus);
330273643bb6SJiri Olsa 			return -1;
330373643bb6SJiri Olsa 		}
3304e649fb7fSYang Jihong 	}
330573643bb6SJiri Olsa 
330699623c62SJiri Olsa 	return 0;
330799623c62SJiri Olsa }
330899623c62SJiri Olsa 
setup_color_pids(struct perf_sched * sched)3309a151a37aSJiri Olsa static int setup_color_pids(struct perf_sched *sched)
3310a151a37aSJiri Olsa {
33119749b90eSJiri Olsa 	struct perf_thread_map *map;
3312a151a37aSJiri Olsa 
3313a151a37aSJiri Olsa 	if (!sched->map.color_pids_str)
3314a151a37aSJiri Olsa 		return 0;
3315a151a37aSJiri Olsa 
3316a151a37aSJiri Olsa 	map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3317a151a37aSJiri Olsa 	if (!map) {
3318a151a37aSJiri Olsa 		pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3319a151a37aSJiri Olsa 		return -1;
3320a151a37aSJiri Olsa 	}
3321a151a37aSJiri Olsa 
3322a151a37aSJiri Olsa 	sched->map.color_pids = map;
3323a151a37aSJiri Olsa 	return 0;
3324a151a37aSJiri Olsa }
3325a151a37aSJiri Olsa 
setup_color_cpus(struct perf_sched * sched)3326cf294f24SJiri Olsa static int setup_color_cpus(struct perf_sched *sched)
3327cf294f24SJiri Olsa {
3328f854839bSJiri Olsa 	struct perf_cpu_map *map;
3329cf294f24SJiri Olsa 
3330cf294f24SJiri Olsa 	if (!sched->map.color_cpus_str)
3331cf294f24SJiri Olsa 		return 0;
3332cf294f24SJiri Olsa 
33339c3516d1SJiri Olsa 	map = perf_cpu_map__new(sched->map.color_cpus_str);
3334cf294f24SJiri Olsa 	if (!map) {
3335cf294f24SJiri Olsa 		pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3336cf294f24SJiri Olsa 		return -1;
3337cf294f24SJiri Olsa 	}
3338cf294f24SJiri Olsa 
3339cf294f24SJiri Olsa 	sched->map.color_cpus = map;
3340cf294f24SJiri Olsa 	return 0;
3341cf294f24SJiri Olsa }
3342cf294f24SJiri Olsa 
perf_sched__map(struct perf_sched * sched)334399623c62SJiri Olsa static int perf_sched__map(struct perf_sched *sched)
334499623c62SJiri Olsa {
3345e649fb7fSYang Jihong 	int rc = -1;
3346e649fb7fSYang Jihong 
33474078f1d4SYang Jihong 	sched->curr_thread = calloc(MAX_CPUS, sizeof(*(sched->curr_thread)));
33484078f1d4SYang Jihong 	if (!sched->curr_thread)
3349e649fb7fSYang Jihong 		return rc;
335099623c62SJiri Olsa 
335103cec19fSYang Jihong 	if (setup_cpus_switch_event(sched))
33524078f1d4SYang Jihong 		goto out_free_curr_thread;
33534078f1d4SYang Jihong 
335403cec19fSYang Jihong 	if (setup_map_cpus(sched))
335503cec19fSYang Jihong 		goto out_free_cpus_switch_event;
335603cec19fSYang Jihong 
3357a151a37aSJiri Olsa 	if (setup_color_pids(sched))
3358e649fb7fSYang Jihong 		goto out_put_map_cpus;
3359a151a37aSJiri Olsa 
3360cf294f24SJiri Olsa 	if (setup_color_cpus(sched))
3361e649fb7fSYang Jihong 		goto out_put_color_pids;
3362cf294f24SJiri Olsa 
33630ec04e16SIngo Molnar 	setup_pager();
3364ae536acfSArnaldo Carvalho de Melo 	if (perf_sched__read_events(sched))
3365e649fb7fSYang Jihong 		goto out_put_color_cpus;
3366e649fb7fSYang Jihong 
3367e649fb7fSYang Jihong 	rc = 0;
33680e9b07e5SArnaldo Carvalho de Melo 	print_bad_events(sched);
3369e649fb7fSYang Jihong 
3370e649fb7fSYang Jihong out_put_color_cpus:
3371e649fb7fSYang Jihong 	perf_cpu_map__put(sched->map.color_cpus);
3372e649fb7fSYang Jihong 
3373e649fb7fSYang Jihong out_put_color_pids:
3374e649fb7fSYang Jihong 	perf_thread_map__put(sched->map.color_pids);
3375e649fb7fSYang Jihong 
3376e649fb7fSYang Jihong out_put_map_cpus:
3377e649fb7fSYang Jihong 	zfree(&sched->map.comp_cpus);
3378e649fb7fSYang Jihong 	perf_cpu_map__put(sched->map.cpus);
33794078f1d4SYang Jihong 
338003cec19fSYang Jihong out_free_cpus_switch_event:
338103cec19fSYang Jihong 	free_cpus_switch_event(sched);
338203cec19fSYang Jihong 
33834078f1d4SYang Jihong out_free_curr_thread:
33844078f1d4SYang Jihong 	zfree(&sched->curr_thread);
3385e649fb7fSYang Jihong 	return rc;
33860ec04e16SIngo Molnar }
33870ec04e16SIngo Molnar 
perf_sched__replay(struct perf_sched * sched)33880e9b07e5SArnaldo Carvalho de Melo static int perf_sched__replay(struct perf_sched *sched)
33890ec04e16SIngo Molnar {
339085ca3d45SYang Jihong 	int ret;
33910ec04e16SIngo Molnar 	unsigned long i;
33920ec04e16SIngo Molnar 
339385ca3d45SYang Jihong 	mutex_init(&sched->start_work_mutex);
339485ca3d45SYang Jihong 	mutex_init(&sched->work_done_wait_mutex);
339585ca3d45SYang Jihong 
339603cec19fSYang Jihong 	ret = setup_cpus_switch_event(sched);
339703cec19fSYang Jihong 	if (ret)
339803cec19fSYang Jihong 		goto out_mutex_destroy;
339903cec19fSYang Jihong 
34000e9b07e5SArnaldo Carvalho de Melo 	calibrate_run_measurement_overhead(sched);
34010e9b07e5SArnaldo Carvalho de Melo 	calibrate_sleep_measurement_overhead(sched);
34020ec04e16SIngo Molnar 
34030e9b07e5SArnaldo Carvalho de Melo 	test_calibrations(sched);
34040ec04e16SIngo Molnar 
340585ca3d45SYang Jihong 	ret = perf_sched__read_events(sched);
340685ca3d45SYang Jihong 	if (ret)
340703cec19fSYang Jihong 		goto out_free_cpus_switch_event;
34080ec04e16SIngo Molnar 
34090e9b07e5SArnaldo Carvalho de Melo 	printf("nr_run_events:        %ld\n", sched->nr_run_events);
34100e9b07e5SArnaldo Carvalho de Melo 	printf("nr_sleep_events:      %ld\n", sched->nr_sleep_events);
34110e9b07e5SArnaldo Carvalho de Melo 	printf("nr_wakeup_events:     %ld\n", sched->nr_wakeup_events);
34120ec04e16SIngo Molnar 
34130e9b07e5SArnaldo Carvalho de Melo 	if (sched->targetless_wakeups)
34140e9b07e5SArnaldo Carvalho de Melo 		printf("target-less wakeups:  %ld\n", sched->targetless_wakeups);
34150e9b07e5SArnaldo Carvalho de Melo 	if (sched->multitarget_wakeups)
34160e9b07e5SArnaldo Carvalho de Melo 		printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
34170e9b07e5SArnaldo Carvalho de Melo 	if (sched->nr_run_events_optimized)
34180ec04e16SIngo Molnar 		printf("run atoms optimized: %ld\n",
34190e9b07e5SArnaldo Carvalho de Melo 			sched->nr_run_events_optimized);
34200ec04e16SIngo Molnar 
34210e9b07e5SArnaldo Carvalho de Melo 	print_task_traces(sched);
34220e9b07e5SArnaldo Carvalho de Melo 	add_cross_task_wakeups(sched);
34230ec04e16SIngo Molnar 
342459c26660SIan Rogers 	sched->thread_funcs_exit = false;
34250e9b07e5SArnaldo Carvalho de Melo 	create_tasks(sched);
34260ec04e16SIngo Molnar 	printf("------------------------------------------------------------\n");
34270e9b07e5SArnaldo Carvalho de Melo 	for (i = 0; i < sched->replay_repeat; i++)
34280e9b07e5SArnaldo Carvalho de Melo 		run_one_test(sched);
3429a116e05dSArnaldo Carvalho de Melo 
343059c26660SIan Rogers 	sched->thread_funcs_exit = true;
3431165da802SNamhyung Kim 	destroy_tasks(sched);
343285ca3d45SYang Jihong 
343303cec19fSYang Jihong out_free_cpus_switch_event:
343403cec19fSYang Jihong 	free_cpus_switch_event(sched);
343503cec19fSYang Jihong 
343685ca3d45SYang Jihong out_mutex_destroy:
343785ca3d45SYang Jihong 	mutex_destroy(&sched->start_work_mutex);
343885ca3d45SYang Jihong 	mutex_destroy(&sched->work_done_wait_mutex);
343985ca3d45SYang Jihong 	return ret;
34400ec04e16SIngo Molnar }
34410ec04e16SIngo Molnar 
setup_sorting(struct perf_sched * sched,const struct option * options,const char * const usage_msg[])34420e9b07e5SArnaldo Carvalho de Melo static void setup_sorting(struct perf_sched *sched, const struct option *options,
34430e9b07e5SArnaldo Carvalho de Melo 			  const char * const usage_msg[])
3444daa1d7a5SFrederic Weisbecker {
34450e9b07e5SArnaldo Carvalho de Melo 	char *tmp, *tok, *str = strdup(sched->sort_order);
3446daa1d7a5SFrederic Weisbecker 
3447daa1d7a5SFrederic Weisbecker 	for (tok = strtok_r(str, ", ", &tmp);
3448daa1d7a5SFrederic Weisbecker 			tok; tok = strtok_r(NULL, ", ", &tmp)) {
34490e9b07e5SArnaldo Carvalho de Melo 		if (sort_dimension__add(tok, &sched->sort_list) < 0) {
3450c7118369SNamhyung Kim 			usage_with_options_msg(usage_msg, options,
3451c7118369SNamhyung Kim 					"Unknown --sort key: `%s'", tok);
3452daa1d7a5SFrederic Weisbecker 		}
3453daa1d7a5SFrederic Weisbecker 	}
3454daa1d7a5SFrederic Weisbecker 
3455daa1d7a5SFrederic Weisbecker 	free(str);
3456daa1d7a5SFrederic Weisbecker 
34570e9b07e5SArnaldo Carvalho de Melo 	sort_dimension__add("pid", &sched->cmp_pid);
3458daa1d7a5SFrederic Weisbecker }
3459daa1d7a5SFrederic Weisbecker 
schedstat_events_exposed(void)3460b0f00855SYang Jihong static bool schedstat_events_exposed(void)
3461b0f00855SYang Jihong {
3462b0f00855SYang Jihong 	/*
3463b0f00855SYang Jihong 	 * Select "sched:sched_stat_wait" event to check
3464b0f00855SYang Jihong 	 * whether schedstat tracepoints are exposed.
3465b0f00855SYang Jihong 	 */
3466b0f00855SYang Jihong 	return IS_ERR(trace_event__tp_format("sched", "sched_stat_wait")) ?
3467b0f00855SYang Jihong 		false : true;
3468b0f00855SYang Jihong }
3469b0f00855SYang Jihong 
__cmd_record(int argc,const char ** argv)34700e9b07e5SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv)
34710e9b07e5SArnaldo Carvalho de Melo {
34720e9b07e5SArnaldo Carvalho de Melo 	unsigned int rec_argc, i, j;
3473d72e5cf3SIan Rogers 	char **rec_argv;
3474d72e5cf3SIan Rogers 	const char **rec_argv_copy;
34750e9b07e5SArnaldo Carvalho de Melo 	const char * const record_args[] = {
34761fc35b29SIngo Molnar 		"record",
34771fc35b29SIngo Molnar 		"-a",
34781fc35b29SIngo Molnar 		"-R",
3479dc02bf71SIngo Molnar 		"-m", "1024",
34801fc35b29SIngo Molnar 		"-c", "1",
34819710118bSStephane Eranian 		"-e", "sched:sched_switch",
34829710118bSStephane Eranian 		"-e", "sched:sched_stat_runtime",
34839710118bSStephane Eranian 		"-e", "sched:sched_process_fork",
34847fff9597SDongsheng 		"-e", "sched:sched_wakeup_new",
34859710118bSStephane Eranian 		"-e", "sched:sched_migrate_task",
34861fc35b29SIngo Molnar 	};
3487b0f00855SYang Jihong 
3488b0f00855SYang Jihong 	/*
3489b0f00855SYang Jihong 	 * The tracepoints trace_sched_stat_{wait, sleep, iowait}
3490b0f00855SYang Jihong 	 * are not exposed to user if CONFIG_SCHEDSTATS is not set,
3491b0f00855SYang Jihong 	 * to prevent "perf sched record" execution failure, determine
3492b0f00855SYang Jihong 	 * whether to record schedstat events according to actual situation.
3493b0f00855SYang Jihong 	 */
3494b0f00855SYang Jihong 	const char * const schedstat_args[] = {
3495b0f00855SYang Jihong 		"-e", "sched:sched_stat_wait",
3496b0f00855SYang Jihong 		"-e", "sched:sched_stat_sleep",
3497b0f00855SYang Jihong 		"-e", "sched:sched_stat_iowait",
3498b0f00855SYang Jihong 	};
3499b0f00855SYang Jihong 	unsigned int schedstat_argc = schedstat_events_exposed() ?
3500b0f00855SYang Jihong 		ARRAY_SIZE(schedstat_args) : 0;
3501b0f00855SYang Jihong 
3502d566a9c2SDavid Ahern 	struct tep_event *waking_event;
3503d72e5cf3SIan Rogers 	int ret;
35041fc35b29SIngo Molnar 
3505d566a9c2SDavid Ahern 	/*
3506d566a9c2SDavid Ahern 	 * +2 for either "-e", "sched:sched_wakeup" or
3507d566a9c2SDavid Ahern 	 * "-e", "sched:sched_waking"
3508d566a9c2SDavid Ahern 	 */
3509b0f00855SYang Jihong 	rec_argc = ARRAY_SIZE(record_args) + 2 + schedstat_argc + argc - 1;
35101fc35b29SIngo Molnar 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
3511e462dc55SArnaldo Carvalho de Melo 	if (rec_argv == NULL)
3512ce47dc56SChris Samuel 		return -ENOMEM;
3513d72e5cf3SIan Rogers 	rec_argv_copy = calloc(rec_argc + 1, sizeof(char *));
3514d72e5cf3SIan Rogers 	if (rec_argv_copy == NULL) {
3515d72e5cf3SIan Rogers 		free(rec_argv);
3516d72e5cf3SIan Rogers 		return -ENOMEM;
3517d72e5cf3SIan Rogers 	}
3518ce47dc56SChris Samuel 
35191fc35b29SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(record_args); i++)
35201fc35b29SIngo Molnar 		rec_argv[i] = strdup(record_args[i]);
35211fc35b29SIngo Molnar 
3522d72e5cf3SIan Rogers 	rec_argv[i++] = strdup("-e");
3523d566a9c2SDavid Ahern 	waking_event = trace_event__tp_format("sched", "sched_waking");
3524d566a9c2SDavid Ahern 	if (!IS_ERR(waking_event))
3525d566a9c2SDavid Ahern 		rec_argv[i++] = strdup("sched:sched_waking");
3526d566a9c2SDavid Ahern 	else
3527d566a9c2SDavid Ahern 		rec_argv[i++] = strdup("sched:sched_wakeup");
3528d566a9c2SDavid Ahern 
3529b0f00855SYang Jihong 	for (j = 0; j < schedstat_argc; j++)
3530b0f00855SYang Jihong 		rec_argv[i++] = strdup(schedstat_args[j]);
3531b0f00855SYang Jihong 
35321fc35b29SIngo Molnar 	for (j = 1; j < (unsigned int)argc; j++, i++)
3533d72e5cf3SIan Rogers 		rec_argv[i] = strdup(argv[j]);
35341fc35b29SIngo Molnar 
35351fc35b29SIngo Molnar 	BUG_ON(i != rec_argc);
35361fc35b29SIngo Molnar 
3537d72e5cf3SIan Rogers 	memcpy(rec_argv_copy, rec_argv, sizeof(char *) * rec_argc);
3538d72e5cf3SIan Rogers 	ret = cmd_record(rec_argc, rec_argv_copy);
3539d72e5cf3SIan Rogers 
3540d72e5cf3SIan Rogers 	for (i = 0; i < rec_argc; i++)
3541d72e5cf3SIan Rogers 		free(rec_argv[i]);
3542d72e5cf3SIan Rogers 	free(rec_argv);
3543d72e5cf3SIan Rogers 	free(rec_argv_copy);
3544d72e5cf3SIan Rogers 
3545d72e5cf3SIan Rogers 	return ret;
35461fc35b29SIngo Molnar }
35471fc35b29SIngo Molnar 
cmd_sched(int argc,const char ** argv)3548b0ad8ea6SArnaldo Carvalho de Melo int cmd_sched(int argc, const char **argv)
35498a39df8fSAdrian Hunter {
355049b8e2beSRasmus Villemoes 	static const char default_sort_order[] = "avg, max, switch, runtime";
35518a39df8fSAdrian Hunter 	struct perf_sched sched = {
35520e9b07e5SArnaldo Carvalho de Melo 		.tool = {
35530e9b07e5SArnaldo Carvalho de Melo 			.sample		 = perf_sched__process_tracepoint_sample,
355499a3c3a9SChangbin Du 			.comm		 = perf_sched__process_comm,
3555f3b3614aSHari Bathini 			.namespaces	 = perf_event__process_namespaces,
35560e9b07e5SArnaldo Carvalho de Melo 			.lost		 = perf_event__process_lost,
3557cb627505SDavid Ahern 			.fork		 = perf_sched__process_fork_event,
35580a8cb85cSJiri Olsa 			.ordered_events = true,
35590e9b07e5SArnaldo Carvalho de Melo 		},
35600e9b07e5SArnaldo Carvalho de Melo 		.cmp_pid	      = LIST_HEAD_INIT(sched.cmp_pid),
35610e9b07e5SArnaldo Carvalho de Melo 		.sort_list	      = LIST_HEAD_INIT(sched.sort_list),
35620e9b07e5SArnaldo Carvalho de Melo 		.sort_order	      = default_sort_order,
35630e9b07e5SArnaldo Carvalho de Melo 		.replay_repeat	      = 10,
35640e9b07e5SArnaldo Carvalho de Melo 		.profile_cpu	      = -1,
35650e9b07e5SArnaldo Carvalho de Melo 		.next_shortname1      = 'A',
35660e9b07e5SArnaldo Carvalho de Melo 		.next_shortname2      = '0',
35672f80dd44SJosef Bacik 		.skip_merge           = 0,
35686c973c90SDavid Ahern 		.show_callchain	      = 1,
35696c973c90SDavid Ahern 		.max_stack            = 5,
35700e9b07e5SArnaldo Carvalho de Melo 	};
357177f02f44SNamhyung Kim 	const struct option sched_options[] = {
357277f02f44SNamhyung Kim 	OPT_STRING('i', "input", &input_name, "file",
357377f02f44SNamhyung Kim 		    "input file name"),
357477f02f44SNamhyung Kim 	OPT_INCR('v', "verbose", &verbose,
357577f02f44SNamhyung Kim 		    "be more verbose (show symbol address, etc)"),
357677f02f44SNamhyung Kim 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
357777f02f44SNamhyung Kim 		    "dump raw trace in ASCII"),
35786fa94258SNamhyung Kim 	OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
357977f02f44SNamhyung Kim 	OPT_END()
358077f02f44SNamhyung Kim 	};
35810e9b07e5SArnaldo Carvalho de Melo 	const struct option latency_options[] = {
35820e9b07e5SArnaldo Carvalho de Melo 	OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
35830e9b07e5SArnaldo Carvalho de Melo 		   "sort by key(s): runtime, switch, avg, max"),
35840e9b07e5SArnaldo Carvalho de Melo 	OPT_INTEGER('C', "CPU", &sched.profile_cpu,
35850e9b07e5SArnaldo Carvalho de Melo 		    "CPU to profile on"),
35862f80dd44SJosef Bacik 	OPT_BOOLEAN('p', "pids", &sched.skip_merge,
35872f80dd44SJosef Bacik 		    "latency stats per pid instead of per comm"),
358877f02f44SNamhyung Kim 	OPT_PARENT(sched_options)
35890e9b07e5SArnaldo Carvalho de Melo 	};
35900e9b07e5SArnaldo Carvalho de Melo 	const struct option replay_options[] = {
35910e9b07e5SArnaldo Carvalho de Melo 	OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
35920e9b07e5SArnaldo Carvalho de Melo 		     "repeat the workload replay N times (-1: infinite)"),
359377f02f44SNamhyung Kim 	OPT_PARENT(sched_options)
35940e9b07e5SArnaldo Carvalho de Melo 	};
359599623c62SJiri Olsa 	const struct option map_options[] = {
359699623c62SJiri Olsa 	OPT_BOOLEAN(0, "compact", &sched.map.comp,
359799623c62SJiri Olsa 		    "map output in compact mode"),
3598a151a37aSJiri Olsa 	OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3599a151a37aSJiri Olsa 		   "highlight given pids in map"),
3600cf294f24SJiri Olsa 	OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3601cf294f24SJiri Olsa                     "highlight given CPUs in map"),
360273643bb6SJiri Olsa 	OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
360373643bb6SJiri Olsa                     "display given CPUs in map"),
360477f02f44SNamhyung Kim 	OPT_PARENT(sched_options)
360599623c62SJiri Olsa 	};
360649394a2aSDavid Ahern 	const struct option timehist_options[] = {
360749394a2aSDavid Ahern 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
360849394a2aSDavid Ahern 		   "file", "vmlinux pathname"),
360949394a2aSDavid Ahern 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
361049394a2aSDavid Ahern 		   "file", "kallsyms pathname"),
36116c973c90SDavid Ahern 	OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
36126c973c90SDavid Ahern 		    "Display call chains if present (default on)"),
36136c973c90SDavid Ahern 	OPT_UINTEGER(0, "max-stack", &sched.max_stack,
36146c973c90SDavid Ahern 		   "Maximum number of functions to display backtrace."),
361549394a2aSDavid Ahern 	OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
361649394a2aSDavid Ahern 		    "Look for files with symbols relative to this directory"),
361752df138cSDavid Ahern 	OPT_BOOLEAN('s', "summary", &sched.summary_only,
361852df138cSDavid Ahern 		    "Show only syscall summary with statistics"),
361952df138cSDavid Ahern 	OPT_BOOLEAN('S', "with-summary", &sched.summary,
362052df138cSDavid Ahern 		    "Show all syscalls and summary with statistics"),
3621fc1469f1SDavid Ahern 	OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
3622292c4a8fSBrendan Gregg 	OPT_BOOLEAN('n', "next", &sched.show_next, "Show next task"),
3623350f54faSDavid Ahern 	OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
3624a407b067SDavid Ahern 	OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
362507235f84SNamhyung Kim 	OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
3626853b7407SDavid Ahern 	OPT_STRING(0, "time", &sched.time_str, "str",
3627853b7407SDavid Ahern 		   "Time span for analysis (start,stop)"),
3628414e050cSNamhyung Kim 	OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"),
36290f59d7a3SDavid Ahern 	OPT_STRING('p', "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
36300f59d7a3SDavid Ahern 		   "analyze events only for given process id(s)"),
36310f59d7a3SDavid Ahern 	OPT_STRING('t', "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
36320f59d7a3SDavid Ahern 		   "analyze events only for given thread id(s)"),
3633c30d630dSDavid Ahern 	OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
363449394a2aSDavid Ahern 	OPT_PARENT(sched_options)
363549394a2aSDavid Ahern 	};
363649394a2aSDavid Ahern 
36370e9b07e5SArnaldo Carvalho de Melo 	const char * const latency_usage[] = {
36380e9b07e5SArnaldo Carvalho de Melo 		"perf sched latency [<options>]",
36390e9b07e5SArnaldo Carvalho de Melo 		NULL
36400e9b07e5SArnaldo Carvalho de Melo 	};
36410e9b07e5SArnaldo Carvalho de Melo 	const char * const replay_usage[] = {
36420e9b07e5SArnaldo Carvalho de Melo 		"perf sched replay [<options>]",
36430e9b07e5SArnaldo Carvalho de Melo 		NULL
36440e9b07e5SArnaldo Carvalho de Melo 	};
364599623c62SJiri Olsa 	const char * const map_usage[] = {
364699623c62SJiri Olsa 		"perf sched map [<options>]",
364799623c62SJiri Olsa 		NULL
364899623c62SJiri Olsa 	};
364949394a2aSDavid Ahern 	const char * const timehist_usage[] = {
365049394a2aSDavid Ahern 		"perf sched timehist [<options>]",
365149394a2aSDavid Ahern 		NULL
365249394a2aSDavid Ahern 	};
3653a83edb2dSRamkumar Ramachandra 	const char *const sched_subcommands[] = { "record", "latency", "map",
365449394a2aSDavid Ahern 						  "replay", "script",
365549394a2aSDavid Ahern 						  "timehist", NULL };
3656a83edb2dSRamkumar Ramachandra 	const char *sched_usage[] = {
3657a83edb2dSRamkumar Ramachandra 		NULL,
36580e9b07e5SArnaldo Carvalho de Melo 		NULL
36590e9b07e5SArnaldo Carvalho de Melo 	};
36600e9b07e5SArnaldo Carvalho de Melo 	struct trace_sched_handler lat_ops  = {
36610e9b07e5SArnaldo Carvalho de Melo 		.wakeup_event	    = latency_wakeup_event,
36620e9b07e5SArnaldo Carvalho de Melo 		.switch_event	    = latency_switch_event,
36630e9b07e5SArnaldo Carvalho de Melo 		.runtime_event	    = latency_runtime_event,
36640e9b07e5SArnaldo Carvalho de Melo 		.migrate_task_event = latency_migrate_task_event,
36650e9b07e5SArnaldo Carvalho de Melo 	};
36660e9b07e5SArnaldo Carvalho de Melo 	struct trace_sched_handler map_ops  = {
36670e9b07e5SArnaldo Carvalho de Melo 		.switch_event	    = map_switch_event,
36680e9b07e5SArnaldo Carvalho de Melo 	};
36690e9b07e5SArnaldo Carvalho de Melo 	struct trace_sched_handler replay_ops  = {
36700e9b07e5SArnaldo Carvalho de Melo 		.wakeup_event	    = replay_wakeup_event,
36710e9b07e5SArnaldo Carvalho de Melo 		.switch_event	    = replay_switch_event,
36720e9b07e5SArnaldo Carvalho de Melo 		.fork_event	    = replay_fork_event,
36730e9b07e5SArnaldo Carvalho de Melo 	};
367403cec19fSYang Jihong 	int ret;
36750e9b07e5SArnaldo Carvalho de Melo 
3676a83edb2dSRamkumar Ramachandra 	argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3677a83edb2dSRamkumar Ramachandra 					sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
3678f2858d8aSIngo Molnar 	if (!argc)
3679f2858d8aSIngo Molnar 		usage_with_options(sched_usage, sched_options);
3680f2858d8aSIngo Molnar 
3681c0777c5aSXiao Guangrong 	/*
3682133dc4c3SIngo Molnar 	 * Aliased to 'perf script' for now:
3683c0777c5aSXiao Guangrong 	 */
36840bd14ac2SIan Rogers 	if (!strcmp(argv[0], "script")) {
368503cec19fSYang Jihong 		return cmd_script(argc, argv);
36860bd14ac2SIan Rogers 	} else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
368703cec19fSYang Jihong 		return __cmd_record(argc, argv);
3688628881eeSYang Jihong 	} else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) {
36890e9b07e5SArnaldo Carvalho de Melo 		sched.tp_handler = &lat_ops;
3690f2858d8aSIngo Molnar 		if (argc > 1) {
3691f2858d8aSIngo Molnar 			argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3692f2858d8aSIngo Molnar 			if (argc)
3693f2858d8aSIngo Molnar 				usage_with_options(latency_usage, latency_options);
3694f2858d8aSIngo Molnar 		}
36950e9b07e5SArnaldo Carvalho de Melo 		setup_sorting(&sched, latency_options, latency_usage);
369603cec19fSYang Jihong 		return perf_sched__lat(&sched);
36970ec04e16SIngo Molnar 	} else if (!strcmp(argv[0], "map")) {
369899623c62SJiri Olsa 		if (argc) {
3699a151a37aSJiri Olsa 			argc = parse_options(argc, argv, map_options, map_usage, 0);
370099623c62SJiri Olsa 			if (argc)
370199623c62SJiri Olsa 				usage_with_options(map_usage, map_options);
370299623c62SJiri Olsa 		}
37030e9b07e5SArnaldo Carvalho de Melo 		sched.tp_handler = &map_ops;
37040e9b07e5SArnaldo Carvalho de Melo 		setup_sorting(&sched, latency_options, latency_usage);
370503cec19fSYang Jihong 		return perf_sched__map(&sched);
3706ae0f4eb3SWei Li 	} else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
37070e9b07e5SArnaldo Carvalho de Melo 		sched.tp_handler = &replay_ops;
37080a02ad93SIngo Molnar 		if (argc) {
3709f2858d8aSIngo Molnar 			argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3710f2858d8aSIngo Molnar 			if (argc)
3711f2858d8aSIngo Molnar 				usage_with_options(replay_usage, replay_options);
3712f2858d8aSIngo Molnar 		}
371303cec19fSYang Jihong 		return perf_sched__replay(&sched);
371449394a2aSDavid Ahern 	} else if (!strcmp(argv[0], "timehist")) {
371549394a2aSDavid Ahern 		if (argc) {
371649394a2aSDavid Ahern 			argc = parse_options(argc, argv, timehist_options,
371749394a2aSDavid Ahern 					     timehist_usage, 0);
371849394a2aSDavid Ahern 			if (argc)
371949394a2aSDavid Ahern 				usage_with_options(timehist_usage, timehist_options);
372049394a2aSDavid Ahern 		}
3721292c4a8fSBrendan Gregg 		if ((sched.show_wakeups || sched.show_next) &&
3722292c4a8fSBrendan Gregg 		    sched.summary_only) {
3723292c4a8fSBrendan Gregg 			pr_err(" Error: -s and -[n|w] are mutually exclusive.\n");
3724fc1469f1SDavid Ahern 			parse_options_usage(timehist_usage, timehist_options, "s", true);
3725292c4a8fSBrendan Gregg 			if (sched.show_wakeups)
3726fc1469f1SDavid Ahern 				parse_options_usage(NULL, timehist_options, "w", true);
3727292c4a8fSBrendan Gregg 			if (sched.show_next)
3728292c4a8fSBrendan Gregg 				parse_options_usage(NULL, timehist_options, "n", true);
372903cec19fSYang Jihong 			return -EINVAL;
3730fc1469f1SDavid Ahern 		}
37317cc72553SJames Clark 		ret = symbol__validate_sym_arguments();
37327cc72553SJames Clark 		if (ret)
373303cec19fSYang Jihong 			return ret;
3734fc1469f1SDavid Ahern 
373503cec19fSYang Jihong 		return perf_sched__timehist(&sched);
3736f2858d8aSIngo Molnar 	} else {
3737f2858d8aSIngo Molnar 		usage_with_options(sched_usage, sched_options);
37380a02ad93SIngo Molnar 	}
37390a02ad93SIngo Molnar 
3740*ab656dd9SAditya Gupta 	/* free usage string allocated by parse_options_subcommand */
3741*ab656dd9SAditya Gupta 	free((void *)sched_usage[0]);
3742*ab656dd9SAditya Gupta 
374303cec19fSYang Jihong 	return 0;
37440a02ad93SIngo Molnar }
3745