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
549b3a48bbSYang Jihong #define MAX_PRIO 140
55ec156764SIngo Molnar
56c30d630dSDavid Ahern static const char *cpu_list;
57c30d630dSDavid Ahern static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
58c30d630dSDavid Ahern
5939aeb52fSmingo struct sched_atom;
60ec156764SIngo Molnar
61ec156764SIngo Molnar struct task_desc {
62ec156764SIngo Molnar unsigned long nr;
63ec156764SIngo Molnar unsigned long pid;
64ec156764SIngo Molnar char comm[COMM_LEN];
65ec156764SIngo Molnar
66ec156764SIngo Molnar unsigned long nr_events;
67ec156764SIngo Molnar unsigned long curr_event;
6839aeb52fSmingo struct sched_atom **atoms;
69ec156764SIngo Molnar
70ec156764SIngo Molnar pthread_t thread;
71ec156764SIngo Molnar sem_t sleep_sem;
72ec156764SIngo Molnar
73ec156764SIngo Molnar sem_t ready_for_work;
74ec156764SIngo Molnar sem_t work_done_sem;
75ec156764SIngo Molnar
76b1ffe8f3SIngo Molnar u64 cpu_usage;
77ec156764SIngo Molnar };
78ec156764SIngo Molnar
79ec156764SIngo Molnar enum sched_event_type {
80ec156764SIngo Molnar SCHED_EVENT_RUN,
81ec156764SIngo Molnar SCHED_EVENT_SLEEP,
82ec156764SIngo Molnar SCHED_EVENT_WAKEUP,
8355ffb7a6SMike Galbraith SCHED_EVENT_MIGRATION,
84ec156764SIngo Molnar };
85ec156764SIngo Molnar
8639aeb52fSmingo struct sched_atom {
87ec156764SIngo Molnar enum sched_event_type type;
88eed05fe7SArnaldo Carvalho de Melo int specific_wait;
89b1ffe8f3SIngo Molnar u64 timestamp;
90b1ffe8f3SIngo Molnar u64 duration;
91ec156764SIngo Molnar unsigned long nr;
92ec156764SIngo Molnar sem_t *wait_sem;
93ec156764SIngo Molnar struct task_desc *wakee;
94ec156764SIngo Molnar };
95ec156764SIngo Molnar
96b1ffe8f3SIngo Molnar enum thread_state {
97b1ffe8f3SIngo Molnar THREAD_SLEEPING = 0,
98b1ffe8f3SIngo Molnar THREAD_WAIT_CPU,
99b1ffe8f3SIngo Molnar THREAD_SCHED_IN,
100b1ffe8f3SIngo Molnar THREAD_IGNORE
101b1ffe8f3SIngo Molnar };
102b1ffe8f3SIngo Molnar
103b1ffe8f3SIngo Molnar struct work_atom {
104b1ffe8f3SIngo Molnar struct list_head list;
105b1ffe8f3SIngo Molnar enum thread_state state;
106aa1ab9d2SFrederic Weisbecker u64 sched_out_time;
107b1ffe8f3SIngo Molnar u64 wake_up_time;
108b1ffe8f3SIngo Molnar u64 sched_in_time;
109b1ffe8f3SIngo Molnar u64 runtime;
110b1ffe8f3SIngo Molnar };
111b1ffe8f3SIngo Molnar
11239aeb52fSmingo struct work_atoms {
11339aeb52fSmingo struct list_head work_list;
114b1ffe8f3SIngo Molnar struct thread *thread;
115b1ffe8f3SIngo Molnar struct rb_node node;
116b1ffe8f3SIngo Molnar u64 max_lat;
117dc000c45SJoel Fernandes (Google) u64 max_lat_start;
118dc000c45SJoel Fernandes (Google) u64 max_lat_end;
119b1ffe8f3SIngo Molnar u64 total_lat;
120b1ffe8f3SIngo Molnar u64 nb_atoms;
121b1ffe8f3SIngo Molnar u64 total_runtime;
1222f80dd44SJosef Bacik int num_merged;
123b1ffe8f3SIngo Molnar };
124b1ffe8f3SIngo Molnar
12539aeb52fSmingo typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
126b1ffe8f3SIngo Molnar
1270e9b07e5SArnaldo Carvalho de Melo struct perf_sched;
1280e9b07e5SArnaldo Carvalho de Melo
129419ab0d6SFrederic Weisbecker struct trace_sched_handler {
13032dcd021SJiri Olsa int (*switch_event)(struct perf_sched *sched, struct evsel *evsel,
1319ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine);
132419ab0d6SFrederic Weisbecker
13332dcd021SJiri Olsa int (*runtime_event)(struct perf_sched *sched, struct evsel *evsel,
1349ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine);
13539aeb52fSmingo
13632dcd021SJiri Olsa int (*wakeup_event)(struct perf_sched *sched, struct evsel *evsel,
1379ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine);
138419ab0d6SFrederic Weisbecker
139cb627505SDavid Ahern /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
140cb627505SDavid Ahern int (*fork_event)(struct perf_sched *sched, union perf_event *event,
141cb627505SDavid Ahern struct machine *machine);
14255ffb7a6SMike Galbraith
1430e9b07e5SArnaldo Carvalho de Melo int (*migrate_task_event)(struct perf_sched *sched,
14432dcd021SJiri Olsa struct evsel *evsel,
1459ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
1469ec3f4e4SArnaldo Carvalho de Melo struct machine *machine);
147419ab0d6SFrederic Weisbecker };
148419ab0d6SFrederic Weisbecker
149a151a37aSJiri Olsa #define COLOR_PIDS PERF_COLOR_BLUE
150cf294f24SJiri Olsa #define COLOR_CPUS PERF_COLOR_BG_RED
151a151a37aSJiri Olsa
15299623c62SJiri Olsa struct perf_sched_map {
15399623c62SJiri Olsa DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS);
1546d18804bSIan Rogers struct perf_cpu *comp_cpus;
15599623c62SJiri Olsa bool comp;
1569749b90eSJiri Olsa struct perf_thread_map *color_pids;
157a151a37aSJiri Olsa const char *color_pids_str;
158f854839bSJiri Olsa struct perf_cpu_map *color_cpus;
159cf294f24SJiri Olsa const char *color_cpus_str;
1603116d609SMadadi Vineeth Reddy const char *task_name;
1619cc0afedSMadadi Vineeth Reddy struct strlist *task_names;
162306f921eSMadadi Vineeth Reddy bool fuzzy;
163f854839bSJiri Olsa struct perf_cpu_map *cpus;
16473643bb6SJiri Olsa const char *cpus_str;
16599623c62SJiri Olsa };
16699623c62SJiri Olsa
1670e9b07e5SArnaldo Carvalho de Melo struct perf_sched {
1680e9b07e5SArnaldo Carvalho de Melo struct perf_tool tool;
1690e9b07e5SArnaldo Carvalho de Melo const char *sort_order;
1700e9b07e5SArnaldo Carvalho de Melo unsigned long nr_tasks;
171cb06ac25SYunlong Song struct task_desc **pid_to_task;
1720e9b07e5SArnaldo Carvalho de Melo struct task_desc **tasks;
1730e9b07e5SArnaldo Carvalho de Melo const struct trace_sched_handler *tp_handler;
1740bd14ac2SIan Rogers struct mutex start_work_mutex;
1750bd14ac2SIan Rogers struct mutex work_done_wait_mutex;
1760e9b07e5SArnaldo Carvalho de Melo int profile_cpu;
1770e9b07e5SArnaldo Carvalho de Melo /*
1780e9b07e5SArnaldo Carvalho de Melo * Track the current task - that way we can know whether there's any
1790e9b07e5SArnaldo Carvalho de Melo * weird events, such as a task being switched away that is not current.
1800e9b07e5SArnaldo Carvalho de Melo */
1816d18804bSIan Rogers struct perf_cpu max_cpu;
182232418a0SIan Rogers u32 *curr_pid;
183232418a0SIan Rogers struct thread **curr_thread;
1843116d609SMadadi Vineeth Reddy struct thread **curr_out_thread;
1850e9b07e5SArnaldo Carvalho de Melo char next_shortname1;
1860e9b07e5SArnaldo Carvalho de Melo char next_shortname2;
1870e9b07e5SArnaldo Carvalho de Melo unsigned int replay_repeat;
1880e9b07e5SArnaldo Carvalho de Melo unsigned long nr_run_events;
1890e9b07e5SArnaldo Carvalho de Melo unsigned long nr_sleep_events;
1900e9b07e5SArnaldo Carvalho de Melo unsigned long nr_wakeup_events;
1910e9b07e5SArnaldo Carvalho de Melo unsigned long nr_sleep_corrections;
1920e9b07e5SArnaldo Carvalho de Melo unsigned long nr_run_events_optimized;
1930e9b07e5SArnaldo Carvalho de Melo unsigned long targetless_wakeups;
1940e9b07e5SArnaldo Carvalho de Melo unsigned long multitarget_wakeups;
1950e9b07e5SArnaldo Carvalho de Melo unsigned long nr_runs;
1960e9b07e5SArnaldo Carvalho de Melo unsigned long nr_timestamps;
1970e9b07e5SArnaldo Carvalho de Melo unsigned long nr_unordered_timestamps;
1980e9b07e5SArnaldo Carvalho de Melo unsigned long nr_context_switch_bugs;
1990e9b07e5SArnaldo Carvalho de Melo unsigned long nr_events;
2000e9b07e5SArnaldo Carvalho de Melo unsigned long nr_lost_chunks;
2010e9b07e5SArnaldo Carvalho de Melo unsigned long nr_lost_events;
2020e9b07e5SArnaldo Carvalho de Melo u64 run_measurement_overhead;
2030e9b07e5SArnaldo Carvalho de Melo u64 sleep_measurement_overhead;
2040e9b07e5SArnaldo Carvalho de Melo u64 start_time;
2050e9b07e5SArnaldo Carvalho de Melo u64 cpu_usage;
2060e9b07e5SArnaldo Carvalho de Melo u64 runavg_cpu_usage;
2070e9b07e5SArnaldo Carvalho de Melo u64 parent_cpu_usage;
2080e9b07e5SArnaldo Carvalho de Melo u64 runavg_parent_cpu_usage;
2090e9b07e5SArnaldo Carvalho de Melo u64 sum_runtime;
2100e9b07e5SArnaldo Carvalho de Melo u64 sum_fluct;
2110e9b07e5SArnaldo Carvalho de Melo u64 run_avg;
2120e9b07e5SArnaldo Carvalho de Melo u64 all_runtime;
2130e9b07e5SArnaldo Carvalho de Melo u64 all_count;
214232418a0SIan Rogers u64 *cpu_last_switched;
215cb4c13a5SDavidlohr Bueso struct rb_root_cached atom_root, sorted_atom_root, merged_atom_root;
2160e9b07e5SArnaldo Carvalho de Melo struct list_head sort_list, cmp_pid;
217939cda52SYunlong Song bool force;
2182f80dd44SJosef Bacik bool skip_merge;
21999623c62SJiri Olsa struct perf_sched_map map;
22052df138cSDavid Ahern
22152df138cSDavid Ahern /* options for timehist command */
22252df138cSDavid Ahern bool summary;
22352df138cSDavid Ahern bool summary_only;
224699b5b92SNamhyung Kim bool idle_hist;
2256c973c90SDavid Ahern bool show_callchain;
2266c973c90SDavid Ahern unsigned int max_stack;
227a407b067SDavid Ahern bool show_cpu_visual;
228fc1469f1SDavid Ahern bool show_wakeups;
229292c4a8fSBrendan Gregg bool show_next;
230350f54faSDavid Ahern bool show_migrations;
231414e050cSNamhyung Kim bool show_state;
2323fcd7409SYang Jihong bool show_prio;
23352df138cSDavid Ahern u64 skipped_samples;
234853b7407SDavid Ahern const char *time_str;
235853b7407SDavid Ahern struct perf_time_interval ptime;
2369396c9cbSNamhyung Kim struct perf_time_interval hist_time;
23759c26660SIan Rogers volatile bool thread_funcs_exit;
2389b3a48bbSYang Jihong const char *prio_str;
2399b3a48bbSYang Jihong DECLARE_BITMAP(prio_bitmap, MAX_PRIO);
2400e9b07e5SArnaldo Carvalho de Melo };
2410e9b07e5SArnaldo Carvalho de Melo
24249394a2aSDavid Ahern /* per thread run time data */
24349394a2aSDavid Ahern struct thread_runtime {
24449394a2aSDavid Ahern u64 last_time; /* time of previous sched in/out event */
24549394a2aSDavid Ahern u64 dt_run; /* run time */
246941bdea7SNamhyung Kim u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
247941bdea7SNamhyung Kim u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
248941bdea7SNamhyung Kim u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
24949394a2aSDavid Ahern u64 dt_delay; /* time between wakeup and sched-in */
25049394a2aSDavid Ahern u64 ready_to_run; /* time of wakeup */
25149394a2aSDavid Ahern
25249394a2aSDavid Ahern struct stats run_stats;
25349394a2aSDavid Ahern u64 total_run_time;
254587782c5SNamhyung Kim u64 total_sleep_time;
255587782c5SNamhyung Kim u64 total_iowait_time;
256587782c5SNamhyung Kim u64 total_preempt_time;
257587782c5SNamhyung Kim u64 total_delay_time;
258350f54faSDavid Ahern
25968f87f24SZe Gao char last_state;
2608640da9fSChangbin Du
2618640da9fSChangbin Du char shortname[3];
26299a3c3a9SChangbin Du bool comm_changed;
26399a3c3a9SChangbin Du
264350f54faSDavid Ahern u64 migrations;
2653fcd7409SYang Jihong
2663fcd7409SYang Jihong int prio;
26749394a2aSDavid Ahern };
26849394a2aSDavid Ahern
26949394a2aSDavid Ahern /* per event run time data */
27049394a2aSDavid Ahern struct evsel_runtime {
27149394a2aSDavid Ahern u64 *last_time; /* time this event was last seen per cpu */
27249394a2aSDavid Ahern u32 ncpu; /* highest cpu slot allocated */
27349394a2aSDavid Ahern };
27449394a2aSDavid Ahern
2753bc2fa9cSNamhyung Kim /* per cpu idle time data */
2763bc2fa9cSNamhyung Kim struct idle_thread_runtime {
2773bc2fa9cSNamhyung Kim struct thread_runtime tr;
2783bc2fa9cSNamhyung Kim struct thread *last_thread;
279cb4c13a5SDavidlohr Bueso struct rb_root_cached sorted_root;
2803bc2fa9cSNamhyung Kim struct callchain_root callchain;
2813bc2fa9cSNamhyung Kim struct callchain_cursor cursor;
2823bc2fa9cSNamhyung Kim };
2833bc2fa9cSNamhyung Kim
28449394a2aSDavid Ahern /* track idle times per cpu */
28549394a2aSDavid Ahern static struct thread **idle_threads;
28649394a2aSDavid Ahern static int idle_max_cpu;
28749394a2aSDavid Ahern static char idle_comm[] = "<idle>";
28849394a2aSDavid Ahern
get_nsecs(void)2890e9b07e5SArnaldo Carvalho de Melo static u64 get_nsecs(void)
2900e9b07e5SArnaldo Carvalho de Melo {
2910e9b07e5SArnaldo Carvalho de Melo struct timespec ts;
2920e9b07e5SArnaldo Carvalho de Melo
2930e9b07e5SArnaldo Carvalho de Melo clock_gettime(CLOCK_MONOTONIC, &ts);
2940e9b07e5SArnaldo Carvalho de Melo
2954fc76e49SArnaldo Carvalho de Melo return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
2960e9b07e5SArnaldo Carvalho de Melo }
2970e9b07e5SArnaldo Carvalho de Melo
burn_nsecs(struct perf_sched * sched,u64 nsecs)2980e9b07e5SArnaldo Carvalho de Melo static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
2990e9b07e5SArnaldo Carvalho de Melo {
3000e9b07e5SArnaldo Carvalho de Melo u64 T0 = get_nsecs(), T1;
3010e9b07e5SArnaldo Carvalho de Melo
3020e9b07e5SArnaldo Carvalho de Melo do {
3030e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
3040e9b07e5SArnaldo Carvalho de Melo } while (T1 + sched->run_measurement_overhead < T0 + nsecs);
3050e9b07e5SArnaldo Carvalho de Melo }
3060e9b07e5SArnaldo Carvalho de Melo
sleep_nsecs(u64 nsecs)3070e9b07e5SArnaldo Carvalho de Melo static void sleep_nsecs(u64 nsecs)
3080e9b07e5SArnaldo Carvalho de Melo {
3090e9b07e5SArnaldo Carvalho de Melo struct timespec ts;
3100e9b07e5SArnaldo Carvalho de Melo
3110e9b07e5SArnaldo Carvalho de Melo ts.tv_nsec = nsecs % 999999999;
3120e9b07e5SArnaldo Carvalho de Melo ts.tv_sec = nsecs / 999999999;
3130e9b07e5SArnaldo Carvalho de Melo
3140e9b07e5SArnaldo Carvalho de Melo nanosleep(&ts, NULL);
3150e9b07e5SArnaldo Carvalho de Melo }
3160e9b07e5SArnaldo Carvalho de Melo
calibrate_run_measurement_overhead(struct perf_sched * sched)3170e9b07e5SArnaldo Carvalho de Melo static void calibrate_run_measurement_overhead(struct perf_sched *sched)
3180e9b07e5SArnaldo Carvalho de Melo {
3194fc76e49SArnaldo Carvalho de Melo u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
3200e9b07e5SArnaldo Carvalho de Melo int i;
3210e9b07e5SArnaldo Carvalho de Melo
3220e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < 10; i++) {
3230e9b07e5SArnaldo Carvalho de Melo T0 = get_nsecs();
3240e9b07e5SArnaldo Carvalho de Melo burn_nsecs(sched, 0);
3250e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
3260e9b07e5SArnaldo Carvalho de Melo delta = T1-T0;
3270e9b07e5SArnaldo Carvalho de Melo min_delta = min(min_delta, delta);
3280e9b07e5SArnaldo Carvalho de Melo }
3290e9b07e5SArnaldo Carvalho de Melo sched->run_measurement_overhead = min_delta;
3300e9b07e5SArnaldo Carvalho de Melo
3310e9b07e5SArnaldo Carvalho de Melo printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
3320e9b07e5SArnaldo Carvalho de Melo }
3330e9b07e5SArnaldo Carvalho de Melo
calibrate_sleep_measurement_overhead(struct perf_sched * sched)3340e9b07e5SArnaldo Carvalho de Melo static void calibrate_sleep_measurement_overhead(struct perf_sched *sched)
3350e9b07e5SArnaldo Carvalho de Melo {
3364fc76e49SArnaldo Carvalho de Melo u64 T0, T1, delta, min_delta = NSEC_PER_SEC;
3370e9b07e5SArnaldo Carvalho de Melo int i;
3380e9b07e5SArnaldo Carvalho de Melo
3390e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < 10; i++) {
3400e9b07e5SArnaldo Carvalho de Melo T0 = get_nsecs();
3410e9b07e5SArnaldo Carvalho de Melo sleep_nsecs(10000);
3420e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
3430e9b07e5SArnaldo Carvalho de Melo delta = T1-T0;
3440e9b07e5SArnaldo Carvalho de Melo min_delta = min(min_delta, delta);
3450e9b07e5SArnaldo Carvalho de Melo }
3460e9b07e5SArnaldo Carvalho de Melo min_delta -= 10000;
3470e9b07e5SArnaldo Carvalho de Melo sched->sleep_measurement_overhead = min_delta;
3480e9b07e5SArnaldo Carvalho de Melo
3490e9b07e5SArnaldo Carvalho de Melo printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
3500e9b07e5SArnaldo Carvalho de Melo }
3510e9b07e5SArnaldo Carvalho de Melo
3520e9b07e5SArnaldo Carvalho de Melo static struct sched_atom *
get_new_event(struct task_desc * task,u64 timestamp)3530e9b07e5SArnaldo Carvalho de Melo get_new_event(struct task_desc *task, u64 timestamp)
3540e9b07e5SArnaldo Carvalho de Melo {
3550e9b07e5SArnaldo Carvalho de Melo struct sched_atom *event = zalloc(sizeof(*event));
3560e9b07e5SArnaldo Carvalho de Melo unsigned long idx = task->nr_events;
3570e9b07e5SArnaldo Carvalho de Melo size_t size;
3580e9b07e5SArnaldo Carvalho de Melo
3590e9b07e5SArnaldo Carvalho de Melo event->timestamp = timestamp;
3600e9b07e5SArnaldo Carvalho de Melo event->nr = idx;
3610e9b07e5SArnaldo Carvalho de Melo
3620e9b07e5SArnaldo Carvalho de Melo task->nr_events++;
3630e9b07e5SArnaldo Carvalho de Melo size = sizeof(struct sched_atom *) * task->nr_events;
3640e9b07e5SArnaldo Carvalho de Melo task->atoms = realloc(task->atoms, size);
3650e9b07e5SArnaldo Carvalho de Melo BUG_ON(!task->atoms);
3660e9b07e5SArnaldo Carvalho de Melo
3670e9b07e5SArnaldo Carvalho de Melo task->atoms[idx] = event;
3680e9b07e5SArnaldo Carvalho de Melo
3690e9b07e5SArnaldo Carvalho de Melo return event;
3700e9b07e5SArnaldo Carvalho de Melo }
3710e9b07e5SArnaldo Carvalho de Melo
last_event(struct task_desc * task)3720e9b07e5SArnaldo Carvalho de Melo static struct sched_atom *last_event(struct task_desc *task)
3730e9b07e5SArnaldo Carvalho de Melo {
3740e9b07e5SArnaldo Carvalho de Melo if (!task->nr_events)
3750e9b07e5SArnaldo Carvalho de Melo return NULL;
3760e9b07e5SArnaldo Carvalho de Melo
3770e9b07e5SArnaldo Carvalho de Melo return task->atoms[task->nr_events - 1];
3780e9b07e5SArnaldo Carvalho de Melo }
3790e9b07e5SArnaldo Carvalho de Melo
add_sched_event_run(struct perf_sched * sched,struct task_desc * task,u64 timestamp,u64 duration)3800e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task,
3810e9b07e5SArnaldo Carvalho de Melo u64 timestamp, u64 duration)
3820e9b07e5SArnaldo Carvalho de Melo {
3830e9b07e5SArnaldo Carvalho de Melo struct sched_atom *event, *curr_event = last_event(task);
3840e9b07e5SArnaldo Carvalho de Melo
3850e9b07e5SArnaldo Carvalho de Melo /*
3860e9b07e5SArnaldo Carvalho de Melo * optimize an existing RUN event by merging this one
3870e9b07e5SArnaldo Carvalho de Melo * to it:
3880e9b07e5SArnaldo Carvalho de Melo */
3890e9b07e5SArnaldo Carvalho de Melo if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
3900e9b07e5SArnaldo Carvalho de Melo sched->nr_run_events_optimized++;
3910e9b07e5SArnaldo Carvalho de Melo curr_event->duration += duration;
3920e9b07e5SArnaldo Carvalho de Melo return;
3930e9b07e5SArnaldo Carvalho de Melo }
3940e9b07e5SArnaldo Carvalho de Melo
3950e9b07e5SArnaldo Carvalho de Melo event = get_new_event(task, timestamp);
3960e9b07e5SArnaldo Carvalho de Melo
3970e9b07e5SArnaldo Carvalho de Melo event->type = SCHED_EVENT_RUN;
3980e9b07e5SArnaldo Carvalho de Melo event->duration = duration;
3990e9b07e5SArnaldo Carvalho de Melo
4000e9b07e5SArnaldo Carvalho de Melo sched->nr_run_events++;
4010e9b07e5SArnaldo Carvalho de Melo }
4020e9b07e5SArnaldo Carvalho de Melo
add_sched_event_wakeup(struct perf_sched * sched,struct task_desc * task,u64 timestamp,struct task_desc * wakee)4030e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task,
4040e9b07e5SArnaldo Carvalho de Melo u64 timestamp, struct task_desc *wakee)
4050e9b07e5SArnaldo Carvalho de Melo {
4060e9b07e5SArnaldo Carvalho de Melo struct sched_atom *event, *wakee_event;
4070e9b07e5SArnaldo Carvalho de Melo
4080e9b07e5SArnaldo Carvalho de Melo event = get_new_event(task, timestamp);
4090e9b07e5SArnaldo Carvalho de Melo event->type = SCHED_EVENT_WAKEUP;
4100e9b07e5SArnaldo Carvalho de Melo event->wakee = wakee;
4110e9b07e5SArnaldo Carvalho de Melo
4120e9b07e5SArnaldo Carvalho de Melo wakee_event = last_event(wakee);
4130e9b07e5SArnaldo Carvalho de Melo if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
4140e9b07e5SArnaldo Carvalho de Melo sched->targetless_wakeups++;
4150e9b07e5SArnaldo Carvalho de Melo return;
4160e9b07e5SArnaldo Carvalho de Melo }
4170e9b07e5SArnaldo Carvalho de Melo if (wakee_event->wait_sem) {
4180e9b07e5SArnaldo Carvalho de Melo sched->multitarget_wakeups++;
4190e9b07e5SArnaldo Carvalho de Melo return;
4200e9b07e5SArnaldo Carvalho de Melo }
4210e9b07e5SArnaldo Carvalho de Melo
4220e9b07e5SArnaldo Carvalho de Melo wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
4230e9b07e5SArnaldo Carvalho de Melo sem_init(wakee_event->wait_sem, 0, 0);
4240e9b07e5SArnaldo Carvalho de Melo wakee_event->specific_wait = 1;
4250e9b07e5SArnaldo Carvalho de Melo event->wait_sem = wakee_event->wait_sem;
4260e9b07e5SArnaldo Carvalho de Melo
4270e9b07e5SArnaldo Carvalho de Melo sched->nr_wakeup_events++;
4280e9b07e5SArnaldo Carvalho de Melo }
4290e9b07e5SArnaldo Carvalho de Melo
add_sched_event_sleep(struct perf_sched * sched,struct task_desc * task,u64 timestamp,const char task_state __maybe_unused)4300e9b07e5SArnaldo Carvalho de Melo static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task,
43168f87f24SZe Gao u64 timestamp, const char task_state __maybe_unused)
4320e9b07e5SArnaldo Carvalho de Melo {
4330e9b07e5SArnaldo Carvalho de Melo struct sched_atom *event = get_new_event(task, timestamp);
4340e9b07e5SArnaldo Carvalho de Melo
4350e9b07e5SArnaldo Carvalho de Melo event->type = SCHED_EVENT_SLEEP;
4360e9b07e5SArnaldo Carvalho de Melo
4370e9b07e5SArnaldo Carvalho de Melo sched->nr_sleep_events++;
4380e9b07e5SArnaldo Carvalho de Melo }
4390e9b07e5SArnaldo Carvalho de Melo
register_pid(struct perf_sched * sched,unsigned long pid,const char * comm)4400e9b07e5SArnaldo Carvalho de Melo static struct task_desc *register_pid(struct perf_sched *sched,
4410e9b07e5SArnaldo Carvalho de Melo unsigned long pid, const char *comm)
4420e9b07e5SArnaldo Carvalho de Melo {
4430e9b07e5SArnaldo Carvalho de Melo struct task_desc *task;
444cb06ac25SYunlong Song static int pid_max;
4450e9b07e5SArnaldo Carvalho de Melo
446cb06ac25SYunlong Song if (sched->pid_to_task == NULL) {
447cb06ac25SYunlong Song if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
448cb06ac25SYunlong Song pid_max = MAX_PID;
449cb06ac25SYunlong Song BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
450cb06ac25SYunlong Song }
4513a423a5cSYunlong Song if (pid >= (unsigned long)pid_max) {
4523a423a5cSYunlong Song BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
4533a423a5cSYunlong Song sizeof(struct task_desc *))) == NULL);
4543a423a5cSYunlong Song while (pid >= (unsigned long)pid_max)
4553a423a5cSYunlong Song sched->pid_to_task[pid_max++] = NULL;
4563a423a5cSYunlong Song }
4570e9b07e5SArnaldo Carvalho de Melo
4580e9b07e5SArnaldo Carvalho de Melo task = sched->pid_to_task[pid];
4590e9b07e5SArnaldo Carvalho de Melo
4600e9b07e5SArnaldo Carvalho de Melo if (task)
4610e9b07e5SArnaldo Carvalho de Melo return task;
4620e9b07e5SArnaldo Carvalho de Melo
4630e9b07e5SArnaldo Carvalho de Melo task = zalloc(sizeof(*task));
4640e9b07e5SArnaldo Carvalho de Melo task->pid = pid;
4650e9b07e5SArnaldo Carvalho de Melo task->nr = sched->nr_tasks;
4660e9b07e5SArnaldo Carvalho de Melo strcpy(task->comm, comm);
4670e9b07e5SArnaldo Carvalho de Melo /*
4680e9b07e5SArnaldo Carvalho de Melo * every task starts in sleeping state - this gets ignored
4690e9b07e5SArnaldo Carvalho de Melo * if there's no wakeup pointing to this sleep state:
4700e9b07e5SArnaldo Carvalho de Melo */
4710e9b07e5SArnaldo Carvalho de Melo add_sched_event_sleep(sched, task, 0, 0);
4720e9b07e5SArnaldo Carvalho de Melo
4730e9b07e5SArnaldo Carvalho de Melo sched->pid_to_task[pid] = task;
4740e9b07e5SArnaldo Carvalho de Melo sched->nr_tasks++;
4750755bc4dSYunlong Song sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
4760e9b07e5SArnaldo Carvalho de Melo BUG_ON(!sched->tasks);
4770e9b07e5SArnaldo Carvalho de Melo sched->tasks[task->nr] = task;
4780e9b07e5SArnaldo Carvalho de Melo
479bb963e16SNamhyung Kim if (verbose > 0)
4800e9b07e5SArnaldo Carvalho de Melo printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm);
4810e9b07e5SArnaldo Carvalho de Melo
4820e9b07e5SArnaldo Carvalho de Melo return task;
4830e9b07e5SArnaldo Carvalho de Melo }
4840e9b07e5SArnaldo Carvalho de Melo
4850e9b07e5SArnaldo Carvalho de Melo
print_task_traces(struct perf_sched * sched)4860e9b07e5SArnaldo Carvalho de Melo static void print_task_traces(struct perf_sched *sched)
4870e9b07e5SArnaldo Carvalho de Melo {
4880e9b07e5SArnaldo Carvalho de Melo struct task_desc *task;
4890e9b07e5SArnaldo Carvalho de Melo unsigned long i;
4900e9b07e5SArnaldo Carvalho de Melo
4910e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
4920e9b07e5SArnaldo Carvalho de Melo task = sched->tasks[i];
4930e9b07e5SArnaldo Carvalho de Melo printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
4940e9b07e5SArnaldo Carvalho de Melo task->nr, task->comm, task->pid, task->nr_events);
4950e9b07e5SArnaldo Carvalho de Melo }
4960e9b07e5SArnaldo Carvalho de Melo }
4970e9b07e5SArnaldo Carvalho de Melo
add_cross_task_wakeups(struct perf_sched * sched)4980e9b07e5SArnaldo Carvalho de Melo static void add_cross_task_wakeups(struct perf_sched *sched)
4990e9b07e5SArnaldo Carvalho de Melo {
5000e9b07e5SArnaldo Carvalho de Melo struct task_desc *task1, *task2;
5010e9b07e5SArnaldo Carvalho de Melo unsigned long i, j;
5020e9b07e5SArnaldo Carvalho de Melo
5030e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
5040e9b07e5SArnaldo Carvalho de Melo task1 = sched->tasks[i];
5050e9b07e5SArnaldo Carvalho de Melo j = i + 1;
5060e9b07e5SArnaldo Carvalho de Melo if (j == sched->nr_tasks)
5070e9b07e5SArnaldo Carvalho de Melo j = 0;
5080e9b07e5SArnaldo Carvalho de Melo task2 = sched->tasks[j];
5090e9b07e5SArnaldo Carvalho de Melo add_sched_event_wakeup(sched, task1, 0, task2);
5100e9b07e5SArnaldo Carvalho de Melo }
5110e9b07e5SArnaldo Carvalho de Melo }
5120e9b07e5SArnaldo Carvalho de Melo
perf_sched__process_event(struct perf_sched * sched,struct sched_atom * atom)5130e9b07e5SArnaldo Carvalho de Melo static void perf_sched__process_event(struct perf_sched *sched,
5140e9b07e5SArnaldo Carvalho de Melo struct sched_atom *atom)
5150e9b07e5SArnaldo Carvalho de Melo {
5160e9b07e5SArnaldo Carvalho de Melo int ret = 0;
5170e9b07e5SArnaldo Carvalho de Melo
5180e9b07e5SArnaldo Carvalho de Melo switch (atom->type) {
5190e9b07e5SArnaldo Carvalho de Melo case SCHED_EVENT_RUN:
5200e9b07e5SArnaldo Carvalho de Melo burn_nsecs(sched, atom->duration);
5210e9b07e5SArnaldo Carvalho de Melo break;
5220e9b07e5SArnaldo Carvalho de Melo case SCHED_EVENT_SLEEP:
5230e9b07e5SArnaldo Carvalho de Melo if (atom->wait_sem)
5240e9b07e5SArnaldo Carvalho de Melo ret = sem_wait(atom->wait_sem);
5250e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
5260e9b07e5SArnaldo Carvalho de Melo break;
5270e9b07e5SArnaldo Carvalho de Melo case SCHED_EVENT_WAKEUP:
5280e9b07e5SArnaldo Carvalho de Melo if (atom->wait_sem)
5290e9b07e5SArnaldo Carvalho de Melo ret = sem_post(atom->wait_sem);
5300e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
5310e9b07e5SArnaldo Carvalho de Melo break;
5320e9b07e5SArnaldo Carvalho de Melo case SCHED_EVENT_MIGRATION:
5330e9b07e5SArnaldo Carvalho de Melo break;
5340e9b07e5SArnaldo Carvalho de Melo default:
5350e9b07e5SArnaldo Carvalho de Melo BUG_ON(1);
5360e9b07e5SArnaldo Carvalho de Melo }
5370e9b07e5SArnaldo Carvalho de Melo }
5380e9b07e5SArnaldo Carvalho de Melo
get_cpu_usage_nsec_parent(void)5390e9b07e5SArnaldo Carvalho de Melo static u64 get_cpu_usage_nsec_parent(void)
5400e9b07e5SArnaldo Carvalho de Melo {
5410e9b07e5SArnaldo Carvalho de Melo struct rusage ru;
5420e9b07e5SArnaldo Carvalho de Melo u64 sum;
5430e9b07e5SArnaldo Carvalho de Melo int err;
5440e9b07e5SArnaldo Carvalho de Melo
5450e9b07e5SArnaldo Carvalho de Melo err = getrusage(RUSAGE_SELF, &ru);
5460e9b07e5SArnaldo Carvalho de Melo BUG_ON(err);
5470e9b07e5SArnaldo Carvalho de Melo
5484fc76e49SArnaldo Carvalho de Melo sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC;
5494fc76e49SArnaldo Carvalho de Melo sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC;
5500e9b07e5SArnaldo Carvalho de Melo
5510e9b07e5SArnaldo Carvalho de Melo return sum;
5520e9b07e5SArnaldo Carvalho de Melo }
5530e9b07e5SArnaldo Carvalho de Melo
self_open_counters(struct perf_sched * sched,unsigned long cur_task)554939cda52SYunlong Song static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
5550e9b07e5SArnaldo Carvalho de Melo {
5560e9b07e5SArnaldo Carvalho de Melo struct perf_event_attr attr;
557939cda52SYunlong Song char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
5580e9b07e5SArnaldo Carvalho de Melo int fd;
559939cda52SYunlong Song struct rlimit limit;
560939cda52SYunlong Song bool need_privilege = false;
5610e9b07e5SArnaldo Carvalho de Melo
5620e9b07e5SArnaldo Carvalho de Melo memset(&attr, 0, sizeof(attr));
5630e9b07e5SArnaldo Carvalho de Melo
5640e9b07e5SArnaldo Carvalho de Melo attr.type = PERF_TYPE_SOFTWARE;
5650e9b07e5SArnaldo Carvalho de Melo attr.config = PERF_COUNT_SW_TASK_CLOCK;
5660e9b07e5SArnaldo Carvalho de Melo
567939cda52SYunlong Song force_again:
56857480d2cSYann Droneaud fd = sys_perf_event_open(&attr, 0, -1, -1,
56957480d2cSYann Droneaud perf_event_open_cloexec_flag());
5700e9b07e5SArnaldo Carvalho de Melo
5711aff59beSYunlong Song if (fd < 0) {
572939cda52SYunlong Song if (errno == EMFILE) {
573939cda52SYunlong Song if (sched->force) {
574939cda52SYunlong Song BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
575939cda52SYunlong Song limit.rlim_cur += sched->nr_tasks - cur_task;
576939cda52SYunlong Song if (limit.rlim_cur > limit.rlim_max) {
577939cda52SYunlong Song limit.rlim_max = limit.rlim_cur;
578939cda52SYunlong Song need_privilege = true;
579939cda52SYunlong Song }
580939cda52SYunlong Song if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
581939cda52SYunlong Song if (need_privilege && errno == EPERM)
582939cda52SYunlong Song strcpy(info, "Need privilege\n");
583939cda52SYunlong Song } else
584939cda52SYunlong Song goto force_again;
585939cda52SYunlong Song } else
586939cda52SYunlong Song strcpy(info, "Have a try with -f option\n");
587939cda52SYunlong Song }
58860b7d14aSNamhyung Kim pr_err("Error: sys_perf_event_open() syscall returned "
589939cda52SYunlong Song "with %d (%s)\n%s", fd,
590c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf)), info);
5911aff59beSYunlong Song exit(EXIT_FAILURE);
5921aff59beSYunlong Song }
5930e9b07e5SArnaldo Carvalho de Melo return fd;
5940e9b07e5SArnaldo Carvalho de Melo }
5950e9b07e5SArnaldo Carvalho de Melo
get_cpu_usage_nsec_self(int fd)5960e9b07e5SArnaldo Carvalho de Melo static u64 get_cpu_usage_nsec_self(int fd)
5970e9b07e5SArnaldo Carvalho de Melo {
5980e9b07e5SArnaldo Carvalho de Melo u64 runtime;
5990e9b07e5SArnaldo Carvalho de Melo int ret;
6000e9b07e5SArnaldo Carvalho de Melo
6010e9b07e5SArnaldo Carvalho de Melo ret = read(fd, &runtime, sizeof(runtime));
6020e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret != sizeof(runtime));
6030e9b07e5SArnaldo Carvalho de Melo
6040e9b07e5SArnaldo Carvalho de Melo return runtime;
6050e9b07e5SArnaldo Carvalho de Melo }
6060e9b07e5SArnaldo Carvalho de Melo
6070e9b07e5SArnaldo Carvalho de Melo struct sched_thread_parms {
6080e9b07e5SArnaldo Carvalho de Melo struct task_desc *task;
6090e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched;
61008097abcSYunlong Song int fd;
6110e9b07e5SArnaldo Carvalho de Melo };
6120e9b07e5SArnaldo Carvalho de Melo
thread_func(void * ctx)6130e9b07e5SArnaldo Carvalho de Melo static void *thread_func(void *ctx)
6140e9b07e5SArnaldo Carvalho de Melo {
6150e9b07e5SArnaldo Carvalho de Melo struct sched_thread_parms *parms = ctx;
6160e9b07e5SArnaldo Carvalho de Melo struct task_desc *this_task = parms->task;
6170e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched = parms->sched;
6180e9b07e5SArnaldo Carvalho de Melo u64 cpu_usage_0, cpu_usage_1;
6190e9b07e5SArnaldo Carvalho de Melo unsigned long i, ret;
6200e9b07e5SArnaldo Carvalho de Melo char comm2[22];
62108097abcSYunlong Song int fd = parms->fd;
6220e9b07e5SArnaldo Carvalho de Melo
62374cf249dSArnaldo Carvalho de Melo zfree(&parms);
6240e9b07e5SArnaldo Carvalho de Melo
6250e9b07e5SArnaldo Carvalho de Melo sprintf(comm2, ":%s", this_task->comm);
6260e9b07e5SArnaldo Carvalho de Melo prctl(PR_SET_NAME, comm2);
6270e9b07e5SArnaldo Carvalho de Melo if (fd < 0)
6280e9b07e5SArnaldo Carvalho de Melo return NULL;
62959c26660SIan Rogers
63059c26660SIan Rogers while (!sched->thread_funcs_exit) {
6310e9b07e5SArnaldo Carvalho de Melo ret = sem_post(&this_task->ready_for_work);
6320e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
6330bd14ac2SIan Rogers mutex_lock(&sched->start_work_mutex);
6340bd14ac2SIan Rogers mutex_unlock(&sched->start_work_mutex);
6350e9b07e5SArnaldo Carvalho de Melo
6360e9b07e5SArnaldo Carvalho de Melo cpu_usage_0 = get_cpu_usage_nsec_self(fd);
6370e9b07e5SArnaldo Carvalho de Melo
6380e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < this_task->nr_events; i++) {
6390e9b07e5SArnaldo Carvalho de Melo this_task->curr_event = i;
6400e9b07e5SArnaldo Carvalho de Melo perf_sched__process_event(sched, this_task->atoms[i]);
6410e9b07e5SArnaldo Carvalho de Melo }
6420e9b07e5SArnaldo Carvalho de Melo
6430e9b07e5SArnaldo Carvalho de Melo cpu_usage_1 = get_cpu_usage_nsec_self(fd);
6440e9b07e5SArnaldo Carvalho de Melo this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
6450e9b07e5SArnaldo Carvalho de Melo ret = sem_post(&this_task->work_done_sem);
6460e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
6470e9b07e5SArnaldo Carvalho de Melo
6480bd14ac2SIan Rogers mutex_lock(&sched->work_done_wait_mutex);
6490bd14ac2SIan Rogers mutex_unlock(&sched->work_done_wait_mutex);
65059c26660SIan Rogers }
65159c26660SIan Rogers return NULL;
6520e9b07e5SArnaldo Carvalho de Melo }
6530e9b07e5SArnaldo Carvalho de Melo
create_tasks(struct perf_sched * sched)6540e9b07e5SArnaldo Carvalho de Melo static void create_tasks(struct perf_sched *sched)
65559c26660SIan Rogers EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex)
65659c26660SIan Rogers EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex)
6570e9b07e5SArnaldo Carvalho de Melo {
6580e9b07e5SArnaldo Carvalho de Melo struct task_desc *task;
6590e9b07e5SArnaldo Carvalho de Melo pthread_attr_t attr;
6600e9b07e5SArnaldo Carvalho de Melo unsigned long i;
6610e9b07e5SArnaldo Carvalho de Melo int err;
6620e9b07e5SArnaldo Carvalho de Melo
6630e9b07e5SArnaldo Carvalho de Melo err = pthread_attr_init(&attr);
6640e9b07e5SArnaldo Carvalho de Melo BUG_ON(err);
6650e9b07e5SArnaldo Carvalho de Melo err = pthread_attr_setstacksize(&attr,
666d08c84e0SArnaldo Carvalho de Melo (size_t) max(16 * 1024, (int)PTHREAD_STACK_MIN));
6670e9b07e5SArnaldo Carvalho de Melo BUG_ON(err);
6680bd14ac2SIan Rogers mutex_lock(&sched->start_work_mutex);
6690bd14ac2SIan Rogers mutex_lock(&sched->work_done_wait_mutex);
6700e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
6710e9b07e5SArnaldo Carvalho de Melo struct sched_thread_parms *parms = malloc(sizeof(*parms));
6720e9b07e5SArnaldo Carvalho de Melo BUG_ON(parms == NULL);
6730e9b07e5SArnaldo Carvalho de Melo parms->task = task = sched->tasks[i];
6740e9b07e5SArnaldo Carvalho de Melo parms->sched = sched;
675939cda52SYunlong Song parms->fd = self_open_counters(sched, i);
6760e9b07e5SArnaldo Carvalho de Melo sem_init(&task->sleep_sem, 0, 0);
6770e9b07e5SArnaldo Carvalho de Melo sem_init(&task->ready_for_work, 0, 0);
6780e9b07e5SArnaldo Carvalho de Melo sem_init(&task->work_done_sem, 0, 0);
6790e9b07e5SArnaldo Carvalho de Melo task->curr_event = 0;
6800e9b07e5SArnaldo Carvalho de Melo err = pthread_create(&task->thread, &attr, thread_func, parms);
6810e9b07e5SArnaldo Carvalho de Melo BUG_ON(err);
6820e9b07e5SArnaldo Carvalho de Melo }
6830e9b07e5SArnaldo Carvalho de Melo }
6840e9b07e5SArnaldo Carvalho de Melo
destroy_tasks(struct perf_sched * sched)685165da802SNamhyung Kim static void destroy_tasks(struct perf_sched *sched)
686165da802SNamhyung Kim UNLOCK_FUNCTION(sched->start_work_mutex)
687165da802SNamhyung Kim UNLOCK_FUNCTION(sched->work_done_wait_mutex)
688165da802SNamhyung Kim {
689165da802SNamhyung Kim struct task_desc *task;
690165da802SNamhyung Kim unsigned long i;
691165da802SNamhyung Kim int err;
692165da802SNamhyung Kim
693165da802SNamhyung Kim mutex_unlock(&sched->start_work_mutex);
694165da802SNamhyung Kim mutex_unlock(&sched->work_done_wait_mutex);
695165da802SNamhyung Kim /* Get rid of threads so they won't be upset by mutex destrunction */
696165da802SNamhyung Kim for (i = 0; i < sched->nr_tasks; i++) {
697165da802SNamhyung Kim task = sched->tasks[i];
698165da802SNamhyung Kim err = pthread_join(task->thread, NULL);
699165da802SNamhyung Kim BUG_ON(err);
700165da802SNamhyung Kim sem_destroy(&task->sleep_sem);
701165da802SNamhyung Kim sem_destroy(&task->ready_for_work);
702165da802SNamhyung Kim sem_destroy(&task->work_done_sem);
703165da802SNamhyung Kim }
704165da802SNamhyung Kim }
705165da802SNamhyung Kim
wait_for_tasks(struct perf_sched * sched)7060e9b07e5SArnaldo Carvalho de Melo static void wait_for_tasks(struct perf_sched *sched)
70759c26660SIan Rogers EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex)
70859c26660SIan Rogers EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex)
7090e9b07e5SArnaldo Carvalho de Melo {
7100e9b07e5SArnaldo Carvalho de Melo u64 cpu_usage_0, cpu_usage_1;
7110e9b07e5SArnaldo Carvalho de Melo struct task_desc *task;
7120e9b07e5SArnaldo Carvalho de Melo unsigned long i, ret;
7130e9b07e5SArnaldo Carvalho de Melo
7140e9b07e5SArnaldo Carvalho de Melo sched->start_time = get_nsecs();
7150e9b07e5SArnaldo Carvalho de Melo sched->cpu_usage = 0;
7160bd14ac2SIan Rogers mutex_unlock(&sched->work_done_wait_mutex);
7170e9b07e5SArnaldo Carvalho de Melo
7180e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
7190e9b07e5SArnaldo Carvalho de Melo task = sched->tasks[i];
7200e9b07e5SArnaldo Carvalho de Melo ret = sem_wait(&task->ready_for_work);
7210e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
7220e9b07e5SArnaldo Carvalho de Melo sem_init(&task->ready_for_work, 0, 0);
7230e9b07e5SArnaldo Carvalho de Melo }
7240bd14ac2SIan Rogers mutex_lock(&sched->work_done_wait_mutex);
7250e9b07e5SArnaldo Carvalho de Melo
7260e9b07e5SArnaldo Carvalho de Melo cpu_usage_0 = get_cpu_usage_nsec_parent();
7270e9b07e5SArnaldo Carvalho de Melo
7280bd14ac2SIan Rogers mutex_unlock(&sched->start_work_mutex);
7290e9b07e5SArnaldo Carvalho de Melo
7300e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
7310e9b07e5SArnaldo Carvalho de Melo task = sched->tasks[i];
7320e9b07e5SArnaldo Carvalho de Melo ret = sem_wait(&task->work_done_sem);
7330e9b07e5SArnaldo Carvalho de Melo BUG_ON(ret);
7340e9b07e5SArnaldo Carvalho de Melo sem_init(&task->work_done_sem, 0, 0);
7350e9b07e5SArnaldo Carvalho de Melo sched->cpu_usage += task->cpu_usage;
7360e9b07e5SArnaldo Carvalho de Melo task->cpu_usage = 0;
7370e9b07e5SArnaldo Carvalho de Melo }
7380e9b07e5SArnaldo Carvalho de Melo
7390e9b07e5SArnaldo Carvalho de Melo cpu_usage_1 = get_cpu_usage_nsec_parent();
7400e9b07e5SArnaldo Carvalho de Melo if (!sched->runavg_cpu_usage)
7410e9b07e5SArnaldo Carvalho de Melo sched->runavg_cpu_usage = sched->cpu_usage;
742ff5f3bbdSYunlong Song sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat;
7430e9b07e5SArnaldo Carvalho de Melo
7440e9b07e5SArnaldo Carvalho de Melo sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
7450e9b07e5SArnaldo Carvalho de Melo if (!sched->runavg_parent_cpu_usage)
7460e9b07e5SArnaldo Carvalho de Melo sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
747ff5f3bbdSYunlong Song sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
748ff5f3bbdSYunlong Song sched->parent_cpu_usage)/sched->replay_repeat;
7490e9b07e5SArnaldo Carvalho de Melo
7500bd14ac2SIan Rogers mutex_lock(&sched->start_work_mutex);
7510e9b07e5SArnaldo Carvalho de Melo
7520e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->nr_tasks; i++) {
7530e9b07e5SArnaldo Carvalho de Melo task = sched->tasks[i];
7540e9b07e5SArnaldo Carvalho de Melo sem_init(&task->sleep_sem, 0, 0);
7550e9b07e5SArnaldo Carvalho de Melo task->curr_event = 0;
7560e9b07e5SArnaldo Carvalho de Melo }
7570e9b07e5SArnaldo Carvalho de Melo }
7580e9b07e5SArnaldo Carvalho de Melo
run_one_test(struct perf_sched * sched)7590e9b07e5SArnaldo Carvalho de Melo static void run_one_test(struct perf_sched *sched)
76059c26660SIan Rogers EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex)
76159c26660SIan Rogers EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex)
7620e9b07e5SArnaldo Carvalho de Melo {
7630e9b07e5SArnaldo Carvalho de Melo u64 T0, T1, delta, avg_delta, fluct;
7640e9b07e5SArnaldo Carvalho de Melo
7650e9b07e5SArnaldo Carvalho de Melo T0 = get_nsecs();
7660e9b07e5SArnaldo Carvalho de Melo wait_for_tasks(sched);
7670e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
7680e9b07e5SArnaldo Carvalho de Melo
7690e9b07e5SArnaldo Carvalho de Melo delta = T1 - T0;
7700e9b07e5SArnaldo Carvalho de Melo sched->sum_runtime += delta;
7710e9b07e5SArnaldo Carvalho de Melo sched->nr_runs++;
7720e9b07e5SArnaldo Carvalho de Melo
7730e9b07e5SArnaldo Carvalho de Melo avg_delta = sched->sum_runtime / sched->nr_runs;
7740e9b07e5SArnaldo Carvalho de Melo if (delta < avg_delta)
7750e9b07e5SArnaldo Carvalho de Melo fluct = avg_delta - delta;
7760e9b07e5SArnaldo Carvalho de Melo else
7770e9b07e5SArnaldo Carvalho de Melo fluct = delta - avg_delta;
7780e9b07e5SArnaldo Carvalho de Melo sched->sum_fluct += fluct;
7790e9b07e5SArnaldo Carvalho de Melo if (!sched->run_avg)
7800e9b07e5SArnaldo Carvalho de Melo sched->run_avg = delta;
781ff5f3bbdSYunlong Song sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
7820e9b07e5SArnaldo Carvalho de Melo
7834fc76e49SArnaldo Carvalho de Melo printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC);
7840e9b07e5SArnaldo Carvalho de Melo
7854fc76e49SArnaldo Carvalho de Melo printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC);
7860e9b07e5SArnaldo Carvalho de Melo
7870e9b07e5SArnaldo Carvalho de Melo printf("cpu: %0.2f / %0.2f",
7884fc76e49SArnaldo Carvalho de Melo (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC);
7890e9b07e5SArnaldo Carvalho de Melo
7900e9b07e5SArnaldo Carvalho de Melo #if 0
7910e9b07e5SArnaldo Carvalho de Melo /*
7920e9b07e5SArnaldo Carvalho de Melo * rusage statistics done by the parent, these are less
7930e9b07e5SArnaldo Carvalho de Melo * accurate than the sched->sum_exec_runtime based statistics:
7940e9b07e5SArnaldo Carvalho de Melo */
7950e9b07e5SArnaldo Carvalho de Melo printf(" [%0.2f / %0.2f]",
7964fc76e49SArnaldo Carvalho de Melo (double)sched->parent_cpu_usage / NSEC_PER_MSEC,
7974fc76e49SArnaldo Carvalho de Melo (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC);
7980e9b07e5SArnaldo Carvalho de Melo #endif
7990e9b07e5SArnaldo Carvalho de Melo
8000e9b07e5SArnaldo Carvalho de Melo printf("\n");
8010e9b07e5SArnaldo Carvalho de Melo
8020e9b07e5SArnaldo Carvalho de Melo if (sched->nr_sleep_corrections)
8030e9b07e5SArnaldo Carvalho de Melo printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections);
8040e9b07e5SArnaldo Carvalho de Melo sched->nr_sleep_corrections = 0;
8050e9b07e5SArnaldo Carvalho de Melo }
8060e9b07e5SArnaldo Carvalho de Melo
test_calibrations(struct perf_sched * sched)8070e9b07e5SArnaldo Carvalho de Melo static void test_calibrations(struct perf_sched *sched)
8080e9b07e5SArnaldo Carvalho de Melo {
8090e9b07e5SArnaldo Carvalho de Melo u64 T0, T1;
8100e9b07e5SArnaldo Carvalho de Melo
8110e9b07e5SArnaldo Carvalho de Melo T0 = get_nsecs();
8124fc76e49SArnaldo Carvalho de Melo burn_nsecs(sched, NSEC_PER_MSEC);
8130e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
8140e9b07e5SArnaldo Carvalho de Melo
8150e9b07e5SArnaldo Carvalho de Melo printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
8160e9b07e5SArnaldo Carvalho de Melo
8170e9b07e5SArnaldo Carvalho de Melo T0 = get_nsecs();
8184fc76e49SArnaldo Carvalho de Melo sleep_nsecs(NSEC_PER_MSEC);
8190e9b07e5SArnaldo Carvalho de Melo T1 = get_nsecs();
8200e9b07e5SArnaldo Carvalho de Melo
8210e9b07e5SArnaldo Carvalho de Melo printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
8220e9b07e5SArnaldo Carvalho de Melo }
8230e9b07e5SArnaldo Carvalho de Melo
824a116e05dSArnaldo Carvalho de Melo static int
replay_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)8250e9b07e5SArnaldo Carvalho de Melo replay_wakeup_event(struct perf_sched *sched,
82632dcd021SJiri Olsa struct evsel *evsel, struct perf_sample *sample,
8279ec3f4e4SArnaldo Carvalho de Melo struct machine *machine __maybe_unused)
828ec156764SIngo Molnar {
829efc0cdc9SArnaldo Carvalho de Melo const char *comm = evsel__strval(evsel, sample, "comm");
830efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
831419ab0d6SFrederic Weisbecker struct task_desc *waker, *wakee;
832419ab0d6SFrederic Weisbecker
833bb963e16SNamhyung Kim if (verbose > 0) {
8342b7fcbc5SArnaldo Carvalho de Melo printf("sched_wakeup event %p\n", evsel);
835419ab0d6SFrederic Weisbecker
8369ec3f4e4SArnaldo Carvalho de Melo printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid);
837419ab0d6SFrederic Weisbecker }
838419ab0d6SFrederic Weisbecker
8392b7fcbc5SArnaldo Carvalho de Melo waker = register_pid(sched, sample->tid, "<unknown>");
8409ec3f4e4SArnaldo Carvalho de Melo wakee = register_pid(sched, pid, comm);
841419ab0d6SFrederic Weisbecker
8420e9b07e5SArnaldo Carvalho de Melo add_sched_event_wakeup(sched, waker, sample->time, wakee);
843a116e05dSArnaldo Carvalho de Melo return 0;
844419ab0d6SFrederic Weisbecker }
845419ab0d6SFrederic Weisbecker
replay_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)8469ec3f4e4SArnaldo Carvalho de Melo static int replay_switch_event(struct perf_sched *sched,
84732dcd021SJiri Olsa struct evsel *evsel,
8489ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
8499ec3f4e4SArnaldo Carvalho de Melo struct machine *machine __maybe_unused)
850419ab0d6SFrederic Weisbecker {
851efc0cdc9SArnaldo Carvalho de Melo const char *prev_comm = evsel__strval(evsel, sample, "prev_comm"),
852efc0cdc9SArnaldo Carvalho de Melo *next_comm = evsel__strval(evsel, sample, "next_comm");
853efc0cdc9SArnaldo Carvalho de Melo const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
854efc0cdc9SArnaldo Carvalho de Melo next_pid = evsel__intval(evsel, sample, "next_pid");
85568f87f24SZe Gao const char prev_state = evsel__taskstate(evsel, sample, "prev_state");
8561d037ca1SIrina Tirdea struct task_desc *prev, __maybe_unused *next;
8577f7f8d0bSArnaldo Carvalho de Melo u64 timestamp0, timestamp = sample->time;
8587f7f8d0bSArnaldo Carvalho de Melo int cpu = sample->cpu;
859fbf94829SIngo Molnar s64 delta;
860fbf94829SIngo Molnar
861bb963e16SNamhyung Kim if (verbose > 0)
8622b7fcbc5SArnaldo Carvalho de Melo printf("sched_switch event %p\n", evsel);
863ad236fd2SIngo Molnar
864fbf94829SIngo Molnar if (cpu >= MAX_CPUS || cpu < 0)
865a116e05dSArnaldo Carvalho de Melo return 0;
866fbf94829SIngo Molnar
8670e9b07e5SArnaldo Carvalho de Melo timestamp0 = sched->cpu_last_switched[cpu];
868fbf94829SIngo Molnar if (timestamp0)
869fbf94829SIngo Molnar delta = timestamp - timestamp0;
870fbf94829SIngo Molnar else
871fbf94829SIngo Molnar delta = 0;
872fbf94829SIngo Molnar
873a116e05dSArnaldo Carvalho de Melo if (delta < 0) {
87460b7d14aSNamhyung Kim pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
875a116e05dSArnaldo Carvalho de Melo return -1;
876a116e05dSArnaldo Carvalho de Melo }
877fbf94829SIngo Molnar
8789ec3f4e4SArnaldo Carvalho de Melo pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
8799ec3f4e4SArnaldo Carvalho de Melo prev_comm, prev_pid, next_comm, next_pid, delta);
880fbf94829SIngo Molnar
8819ec3f4e4SArnaldo Carvalho de Melo prev = register_pid(sched, prev_pid, prev_comm);
8829ec3f4e4SArnaldo Carvalho de Melo next = register_pid(sched, next_pid, next_comm);
883fbf94829SIngo Molnar
8840e9b07e5SArnaldo Carvalho de Melo sched->cpu_last_switched[cpu] = timestamp;
885fbf94829SIngo Molnar
8860e9b07e5SArnaldo Carvalho de Melo add_sched_event_run(sched, prev, timestamp, delta);
8879ec3f4e4SArnaldo Carvalho de Melo add_sched_event_sleep(sched, prev, timestamp, prev_state);
888a116e05dSArnaldo Carvalho de Melo
889a116e05dSArnaldo Carvalho de Melo return 0;
890fbf94829SIngo Molnar }
891fbf94829SIngo Molnar
replay_fork_event(struct perf_sched * sched,union perf_event * event,struct machine * machine)892cb627505SDavid Ahern static int replay_fork_event(struct perf_sched *sched,
893cb627505SDavid Ahern union perf_event *event,
894cb627505SDavid Ahern struct machine *machine)
895419ab0d6SFrederic Weisbecker {
896cb627505SDavid Ahern struct thread *child, *parent;
8979ec3f4e4SArnaldo Carvalho de Melo
898314add6bSAdrian Hunter child = machine__findnew_thread(machine, event->fork.pid,
899314add6bSAdrian Hunter event->fork.tid);
900314add6bSAdrian Hunter parent = machine__findnew_thread(machine, event->fork.ppid,
901314add6bSAdrian Hunter event->fork.ptid);
902cb627505SDavid Ahern
903cb627505SDavid Ahern if (child == NULL || parent == NULL) {
904cb627505SDavid Ahern pr_debug("thread does not exist on fork event: child %p, parent %p\n",
905cb627505SDavid Ahern child, parent);
906b91fc39fSArnaldo Carvalho de Melo goto out_put;
907419ab0d6SFrederic Weisbecker }
9089ec3f4e4SArnaldo Carvalho de Melo
909bb963e16SNamhyung Kim if (verbose > 0) {
910cb627505SDavid Ahern printf("fork event\n");
911ee84a303SIan Rogers printf("... parent: %s/%d\n", thread__comm_str(parent), thread__tid(parent));
912ee84a303SIan Rogers printf("... child: %s/%d\n", thread__comm_str(child), thread__tid(child));
913cb627505SDavid Ahern }
914cb627505SDavid Ahern
915ee84a303SIan Rogers register_pid(sched, thread__tid(parent), thread__comm_str(parent));
916ee84a303SIan Rogers register_pid(sched, thread__tid(child), thread__comm_str(child));
917b91fc39fSArnaldo Carvalho de Melo out_put:
918b91fc39fSArnaldo Carvalho de Melo thread__put(child);
919b91fc39fSArnaldo Carvalho de Melo thread__put(parent);
920a116e05dSArnaldo Carvalho de Melo return 0;
921419ab0d6SFrederic Weisbecker }
922419ab0d6SFrederic Weisbecker
923b1ffe8f3SIngo Molnar struct sort_dimension {
924b1ffe8f3SIngo Molnar const char *name;
925b5fae128SIngo Molnar sort_fn_t cmp;
926b1ffe8f3SIngo Molnar struct list_head list;
927b1ffe8f3SIngo Molnar };
928b1ffe8f3SIngo Molnar
init_prio(struct thread_runtime * r)9293fcd7409SYang Jihong static inline void init_prio(struct thread_runtime *r)
9303fcd7409SYang Jihong {
9313fcd7409SYang Jihong r->prio = -1;
9323fcd7409SYang Jihong }
9333fcd7409SYang Jihong
9348640da9fSChangbin Du /*
9358640da9fSChangbin Du * handle runtime stats saved per thread
9368640da9fSChangbin Du */
thread__init_runtime(struct thread * thread)9378640da9fSChangbin Du static struct thread_runtime *thread__init_runtime(struct thread *thread)
9388640da9fSChangbin Du {
9398640da9fSChangbin Du struct thread_runtime *r;
9408640da9fSChangbin Du
9418640da9fSChangbin Du r = zalloc(sizeof(struct thread_runtime));
9428640da9fSChangbin Du if (!r)
9438640da9fSChangbin Du return NULL;
9448640da9fSChangbin Du
9458640da9fSChangbin Du init_stats(&r->run_stats);
9463fcd7409SYang Jihong init_prio(r);
9478640da9fSChangbin Du thread__set_priv(thread, r);
9488640da9fSChangbin Du
9498640da9fSChangbin Du return r;
9508640da9fSChangbin Du }
9518640da9fSChangbin Du
thread__get_runtime(struct thread * thread)9528640da9fSChangbin Du static struct thread_runtime *thread__get_runtime(struct thread *thread)
9538640da9fSChangbin Du {
9548640da9fSChangbin Du struct thread_runtime *tr;
9558640da9fSChangbin Du
9568640da9fSChangbin Du tr = thread__priv(thread);
9578640da9fSChangbin Du if (tr == NULL) {
9588640da9fSChangbin Du tr = thread__init_runtime(thread);
9598640da9fSChangbin Du if (tr == NULL)
9608640da9fSChangbin Du pr_debug("Failed to malloc memory for runtime data.\n");
9618640da9fSChangbin Du }
9628640da9fSChangbin Du
9638640da9fSChangbin Du return tr;
9648640da9fSChangbin Du }
9658640da9fSChangbin Du
966daa1d7a5SFrederic Weisbecker static int
thread_lat_cmp(struct list_head * list,struct work_atoms * l,struct work_atoms * r)96739aeb52fSmingo thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
968daa1d7a5SFrederic Weisbecker {
969daa1d7a5SFrederic Weisbecker struct sort_dimension *sort;
970daa1d7a5SFrederic Weisbecker int ret = 0;
971daa1d7a5SFrederic Weisbecker
972b5fae128SIngo Molnar BUG_ON(list_empty(list));
973b5fae128SIngo Molnar
974daa1d7a5SFrederic Weisbecker list_for_each_entry(sort, list, list) {
975daa1d7a5SFrederic Weisbecker ret = sort->cmp(l, r);
976daa1d7a5SFrederic Weisbecker if (ret)
977daa1d7a5SFrederic Weisbecker return ret;
978daa1d7a5SFrederic Weisbecker }
979daa1d7a5SFrederic Weisbecker
980daa1d7a5SFrederic Weisbecker return ret;
981daa1d7a5SFrederic Weisbecker }
982daa1d7a5SFrederic Weisbecker
98339aeb52fSmingo static struct work_atoms *
thread_atoms_search(struct rb_root_cached * root,struct thread * thread,struct list_head * sort_list)984cb4c13a5SDavidlohr Bueso thread_atoms_search(struct rb_root_cached *root, struct thread *thread,
985b5fae128SIngo Molnar struct list_head *sort_list)
986b5fae128SIngo Molnar {
987cb4c13a5SDavidlohr Bueso struct rb_node *node = root->rb_root.rb_node;
98839aeb52fSmingo struct work_atoms key = { .thread = thread };
989b5fae128SIngo Molnar
990b5fae128SIngo Molnar while (node) {
99139aeb52fSmingo struct work_atoms *atoms;
992b5fae128SIngo Molnar int cmp;
993b5fae128SIngo Molnar
99439aeb52fSmingo atoms = container_of(node, struct work_atoms, node);
995b5fae128SIngo Molnar
996b5fae128SIngo Molnar cmp = thread_lat_cmp(sort_list, &key, atoms);
997b5fae128SIngo Molnar if (cmp > 0)
998b5fae128SIngo Molnar node = node->rb_left;
999b5fae128SIngo Molnar else if (cmp < 0)
1000b5fae128SIngo Molnar node = node->rb_right;
1001b5fae128SIngo Molnar else {
1002b5fae128SIngo Molnar BUG_ON(thread != atoms->thread);
1003b5fae128SIngo Molnar return atoms;
1004b5fae128SIngo Molnar }
1005b5fae128SIngo Molnar }
1006b5fae128SIngo Molnar return NULL;
1007b5fae128SIngo Molnar }
1008b5fae128SIngo Molnar
1009cdce9d73SFrederic Weisbecker static void
__thread_latency_insert(struct rb_root_cached * root,struct work_atoms * data,struct list_head * sort_list)1010cb4c13a5SDavidlohr Bueso __thread_latency_insert(struct rb_root_cached *root, struct work_atoms *data,
1011daa1d7a5SFrederic Weisbecker struct list_head *sort_list)
1012cdce9d73SFrederic Weisbecker {
1013cb4c13a5SDavidlohr Bueso struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
1014cb4c13a5SDavidlohr Bueso bool leftmost = true;
1015cdce9d73SFrederic Weisbecker
1016cdce9d73SFrederic Weisbecker while (*new) {
101739aeb52fSmingo struct work_atoms *this;
1018daa1d7a5SFrederic Weisbecker int cmp;
1019cdce9d73SFrederic Weisbecker
102039aeb52fSmingo this = container_of(*new, struct work_atoms, node);
1021cdce9d73SFrederic Weisbecker parent = *new;
1022daa1d7a5SFrederic Weisbecker
1023daa1d7a5SFrederic Weisbecker cmp = thread_lat_cmp(sort_list, data, this);
1024daa1d7a5SFrederic Weisbecker
1025daa1d7a5SFrederic Weisbecker if (cmp > 0)
1026cdce9d73SFrederic Weisbecker new = &((*new)->rb_left);
1027cb4c13a5SDavidlohr Bueso else {
1028daa1d7a5SFrederic Weisbecker new = &((*new)->rb_right);
1029cb4c13a5SDavidlohr Bueso leftmost = false;
1030cb4c13a5SDavidlohr Bueso }
1031cdce9d73SFrederic Weisbecker }
1032cdce9d73SFrederic Weisbecker
1033cdce9d73SFrederic Weisbecker rb_link_node(&data->node, parent, new);
1034cb4c13a5SDavidlohr Bueso rb_insert_color_cached(&data->node, root, leftmost);
1035cdce9d73SFrederic Weisbecker }
1036cdce9d73SFrederic Weisbecker
thread_atoms_insert(struct perf_sched * sched,struct thread * thread)10370e9b07e5SArnaldo Carvalho de Melo static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
1038cdce9d73SFrederic Weisbecker {
103936479484SArnaldo Carvalho de Melo struct work_atoms *atoms = zalloc(sizeof(*atoms));
1040a116e05dSArnaldo Carvalho de Melo if (!atoms) {
1041a116e05dSArnaldo Carvalho de Melo pr_err("No memory at %s\n", __func__);
1042a116e05dSArnaldo Carvalho de Melo return -1;
1043a116e05dSArnaldo Carvalho de Melo }
1044cdce9d73SFrederic Weisbecker
1045f3b623b8SArnaldo Carvalho de Melo atoms->thread = thread__get(thread);
104639aeb52fSmingo INIT_LIST_HEAD(&atoms->work_list);
10470e9b07e5SArnaldo Carvalho de Melo __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
1048a116e05dSArnaldo Carvalho de Melo return 0;
1049cdce9d73SFrederic Weisbecker }
1050cdce9d73SFrederic Weisbecker
1051a116e05dSArnaldo Carvalho de Melo static int
add_sched_out_event(struct work_atoms * atoms,char run_state,u64 timestamp)105239aeb52fSmingo add_sched_out_event(struct work_atoms *atoms,
105339aeb52fSmingo char run_state,
1054c6ced611SFrederic Weisbecker u64 timestamp)
1055cdce9d73SFrederic Weisbecker {
105636479484SArnaldo Carvalho de Melo struct work_atom *atom = zalloc(sizeof(*atom));
1057a116e05dSArnaldo Carvalho de Melo if (!atom) {
1058a116e05dSArnaldo Carvalho de Melo pr_err("Non memory at %s", __func__);
1059a116e05dSArnaldo Carvalho de Melo return -1;
1060a116e05dSArnaldo Carvalho de Melo }
1061cdce9d73SFrederic Weisbecker
1062aa1ab9d2SFrederic Weisbecker atom->sched_out_time = timestamp;
1063aa1ab9d2SFrederic Weisbecker
106439aeb52fSmingo if (run_state == 'R') {
1065b1ffe8f3SIngo Molnar atom->state = THREAD_WAIT_CPU;
1066aa1ab9d2SFrederic Weisbecker atom->wake_up_time = atom->sched_out_time;
1067c6ced611SFrederic Weisbecker }
1068c6ced611SFrederic Weisbecker
106939aeb52fSmingo list_add_tail(&atom->list, &atoms->work_list);
1070a116e05dSArnaldo Carvalho de Melo return 0;
1071cdce9d73SFrederic Weisbecker }
1072cdce9d73SFrederic Weisbecker
1073cdce9d73SFrederic Weisbecker static void
add_runtime_event(struct work_atoms * atoms,u64 delta,u64 timestamp __maybe_unused)10741d037ca1SIrina Tirdea add_runtime_event(struct work_atoms *atoms, u64 delta,
10751d037ca1SIrina Tirdea u64 timestamp __maybe_unused)
107639aeb52fSmingo {
107739aeb52fSmingo struct work_atom *atom;
107839aeb52fSmingo
107939aeb52fSmingo BUG_ON(list_empty(&atoms->work_list));
108039aeb52fSmingo
108139aeb52fSmingo atom = list_entry(atoms->work_list.prev, struct work_atom, list);
108239aeb52fSmingo
108339aeb52fSmingo atom->runtime += delta;
108439aeb52fSmingo atoms->total_runtime += delta;
108539aeb52fSmingo }
108639aeb52fSmingo
108739aeb52fSmingo static void
add_sched_in_event(struct work_atoms * atoms,u64 timestamp)108839aeb52fSmingo add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1089cdce9d73SFrederic Weisbecker {
1090b1ffe8f3SIngo Molnar struct work_atom *atom;
109166685678SFrederic Weisbecker u64 delta;
1092cdce9d73SFrederic Weisbecker
109339aeb52fSmingo if (list_empty(&atoms->work_list))
1094cdce9d73SFrederic Weisbecker return;
1095cdce9d73SFrederic Weisbecker
109639aeb52fSmingo atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1097cdce9d73SFrederic Weisbecker
1098b1ffe8f3SIngo Molnar if (atom->state != THREAD_WAIT_CPU)
1099cdce9d73SFrederic Weisbecker return;
1100cdce9d73SFrederic Weisbecker
1101b1ffe8f3SIngo Molnar if (timestamp < atom->wake_up_time) {
1102b1ffe8f3SIngo Molnar atom->state = THREAD_IGNORE;
1103cdce9d73SFrederic Weisbecker return;
1104cdce9d73SFrederic Weisbecker }
1105cdce9d73SFrederic Weisbecker
1106b1ffe8f3SIngo Molnar atom->state = THREAD_SCHED_IN;
1107b1ffe8f3SIngo Molnar atom->sched_in_time = timestamp;
110866685678SFrederic Weisbecker
1109b1ffe8f3SIngo Molnar delta = atom->sched_in_time - atom->wake_up_time;
111066685678SFrederic Weisbecker atoms->total_lat += delta;
11113786310aSFrederic Weisbecker if (delta > atoms->max_lat) {
111266685678SFrederic Weisbecker atoms->max_lat = delta;
1113dc000c45SJoel Fernandes (Google) atoms->max_lat_start = atom->wake_up_time;
1114dc000c45SJoel Fernandes (Google) atoms->max_lat_end = timestamp;
11153786310aSFrederic Weisbecker }
111666685678SFrederic Weisbecker atoms->nb_atoms++;
1117cdce9d73SFrederic Weisbecker }
1118cdce9d73SFrederic Weisbecker
latency_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)11199ec3f4e4SArnaldo Carvalho de Melo static int latency_switch_event(struct perf_sched *sched,
112032dcd021SJiri Olsa struct evsel *evsel,
11219ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
11229ec3f4e4SArnaldo Carvalho de Melo struct machine *machine)
1123cdce9d73SFrederic Weisbecker {
1124efc0cdc9SArnaldo Carvalho de Melo const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
1125efc0cdc9SArnaldo Carvalho de Melo next_pid = evsel__intval(evsel, sample, "next_pid");
112668f87f24SZe Gao const char prev_state = evsel__taskstate(evsel, sample, "prev_state");
112739aeb52fSmingo struct work_atoms *out_events, *in_events;
1128cdce9d73SFrederic Weisbecker struct thread *sched_out, *sched_in;
11297f7f8d0bSArnaldo Carvalho de Melo u64 timestamp0, timestamp = sample->time;
1130b91fc39fSArnaldo Carvalho de Melo int cpu = sample->cpu, err = -1;
1131ea92ed5aSIngo Molnar s64 delta;
1132ea92ed5aSIngo Molnar
113339aeb52fSmingo BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1134ea92ed5aSIngo Molnar
11350e9b07e5SArnaldo Carvalho de Melo timestamp0 = sched->cpu_last_switched[cpu];
11360e9b07e5SArnaldo Carvalho de Melo sched->cpu_last_switched[cpu] = timestamp;
1137ea92ed5aSIngo Molnar if (timestamp0)
1138ea92ed5aSIngo Molnar delta = timestamp - timestamp0;
1139ea92ed5aSIngo Molnar else
1140ea92ed5aSIngo Molnar delta = 0;
1141ea92ed5aSIngo Molnar
1142a116e05dSArnaldo Carvalho de Melo if (delta < 0) {
1143a116e05dSArnaldo Carvalho de Melo pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1144a116e05dSArnaldo Carvalho de Melo return -1;
1145a116e05dSArnaldo Carvalho de Melo }
1146cdce9d73SFrederic Weisbecker
11471fcb8768SAdrian Hunter sched_out = machine__findnew_thread(machine, -1, prev_pid);
11481fcb8768SAdrian Hunter sched_in = machine__findnew_thread(machine, -1, next_pid);
1149b91fc39fSArnaldo Carvalho de Melo if (sched_out == NULL || sched_in == NULL)
1150b91fc39fSArnaldo Carvalho de Melo goto out_put;
1151cdce9d73SFrederic Weisbecker
11520e9b07e5SArnaldo Carvalho de Melo out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
115339aeb52fSmingo if (!out_events) {
11540e9b07e5SArnaldo Carvalho de Melo if (thread_atoms_insert(sched, sched_out))
1155b91fc39fSArnaldo Carvalho de Melo goto out_put;
11560e9b07e5SArnaldo Carvalho de Melo out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid);
1157a116e05dSArnaldo Carvalho de Melo if (!out_events) {
1158a116e05dSArnaldo Carvalho de Melo pr_err("out-event: Internal tree error");
1159b91fc39fSArnaldo Carvalho de Melo goto out_put;
116039aeb52fSmingo }
1161a116e05dSArnaldo Carvalho de Melo }
116268f87f24SZe Gao if (add_sched_out_event(out_events, prev_state, timestamp))
1163a116e05dSArnaldo Carvalho de Melo return -1;
116439aeb52fSmingo
11650e9b07e5SArnaldo Carvalho de Melo in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
116639aeb52fSmingo if (!in_events) {
11670e9b07e5SArnaldo Carvalho de Melo if (thread_atoms_insert(sched, sched_in))
1168b91fc39fSArnaldo Carvalho de Melo goto out_put;
11690e9b07e5SArnaldo Carvalho de Melo in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid);
1170a116e05dSArnaldo Carvalho de Melo if (!in_events) {
1171a116e05dSArnaldo Carvalho de Melo pr_err("in-event: Internal tree error");
1172b91fc39fSArnaldo Carvalho de Melo goto out_put;
1173a116e05dSArnaldo Carvalho de Melo }
117439aeb52fSmingo /*
117539aeb52fSmingo * Take came in we have not heard about yet,
117639aeb52fSmingo * add in an initial atom in runnable state:
117739aeb52fSmingo */
1178a116e05dSArnaldo Carvalho de Melo if (add_sched_out_event(in_events, 'R', timestamp))
1179b91fc39fSArnaldo Carvalho de Melo goto out_put;
118039aeb52fSmingo }
118139aeb52fSmingo add_sched_in_event(in_events, timestamp);
1182b91fc39fSArnaldo Carvalho de Melo err = 0;
1183b91fc39fSArnaldo Carvalho de Melo out_put:
1184b91fc39fSArnaldo Carvalho de Melo thread__put(sched_out);
1185b91fc39fSArnaldo Carvalho de Melo thread__put(sched_in);
1186b91fc39fSArnaldo Carvalho de Melo return err;
1187cdce9d73SFrederic Weisbecker }
1188cdce9d73SFrederic Weisbecker
latency_runtime_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)11899ec3f4e4SArnaldo Carvalho de Melo static int latency_runtime_event(struct perf_sched *sched,
119032dcd021SJiri Olsa struct evsel *evsel,
11919ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
11929ec3f4e4SArnaldo Carvalho de Melo struct machine *machine)
119339aeb52fSmingo {
1194efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
1195efc0cdc9SArnaldo Carvalho de Melo const u64 runtime = evsel__intval(evsel, sample, "runtime");
11961fcb8768SAdrian Hunter struct thread *thread = machine__findnew_thread(machine, -1, pid);
11970e9b07e5SArnaldo Carvalho de Melo struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
11987f7f8d0bSArnaldo Carvalho de Melo u64 timestamp = sample->time;
1199b91fc39fSArnaldo Carvalho de Melo int cpu = sample->cpu, err = -1;
1200b91fc39fSArnaldo Carvalho de Melo
1201b91fc39fSArnaldo Carvalho de Melo if (thread == NULL)
1202b91fc39fSArnaldo Carvalho de Melo return -1;
120339aeb52fSmingo
120439aeb52fSmingo BUG_ON(cpu >= MAX_CPUS || cpu < 0);
120539aeb52fSmingo if (!atoms) {
12060e9b07e5SArnaldo Carvalho de Melo if (thread_atoms_insert(sched, thread))
1207b91fc39fSArnaldo Carvalho de Melo goto out_put;
12080e9b07e5SArnaldo Carvalho de Melo atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid);
1209a116e05dSArnaldo Carvalho de Melo if (!atoms) {
121060b7d14aSNamhyung Kim pr_err("in-event: Internal tree error");
1211b91fc39fSArnaldo Carvalho de Melo goto out_put;
1212a116e05dSArnaldo Carvalho de Melo }
1213a116e05dSArnaldo Carvalho de Melo if (add_sched_out_event(atoms, 'R', timestamp))
1214b91fc39fSArnaldo Carvalho de Melo goto out_put;
121539aeb52fSmingo }
121639aeb52fSmingo
12179ec3f4e4SArnaldo Carvalho de Melo add_runtime_event(atoms, runtime, timestamp);
1218b91fc39fSArnaldo Carvalho de Melo err = 0;
1219b91fc39fSArnaldo Carvalho de Melo out_put:
1220b91fc39fSArnaldo Carvalho de Melo thread__put(thread);
1221b91fc39fSArnaldo Carvalho de Melo return err;
1222cdce9d73SFrederic Weisbecker }
1223cdce9d73SFrederic Weisbecker
latency_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)12249ec3f4e4SArnaldo Carvalho de Melo static int latency_wakeup_event(struct perf_sched *sched,
122532dcd021SJiri Olsa struct evsel *evsel,
12269ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
12279ec3f4e4SArnaldo Carvalho de Melo struct machine *machine)
1228cdce9d73SFrederic Weisbecker {
1229efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
123039aeb52fSmingo struct work_atoms *atoms;
1231b1ffe8f3SIngo Molnar struct work_atom *atom;
1232cdce9d73SFrederic Weisbecker struct thread *wakee;
12337f7f8d0bSArnaldo Carvalho de Melo u64 timestamp = sample->time;
1234b91fc39fSArnaldo Carvalho de Melo int err = -1;
1235cdce9d73SFrederic Weisbecker
12361fcb8768SAdrian Hunter wakee = machine__findnew_thread(machine, -1, pid);
1237b91fc39fSArnaldo Carvalho de Melo if (wakee == NULL)
1238b91fc39fSArnaldo Carvalho de Melo return -1;
12390e9b07e5SArnaldo Carvalho de Melo atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
124017562205SFrederic Weisbecker if (!atoms) {
12410e9b07e5SArnaldo Carvalho de Melo if (thread_atoms_insert(sched, wakee))
1242b91fc39fSArnaldo Carvalho de Melo goto out_put;
12430e9b07e5SArnaldo Carvalho de Melo atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid);
1244a116e05dSArnaldo Carvalho de Melo if (!atoms) {
124560b7d14aSNamhyung Kim pr_err("wakeup-event: Internal tree error");
1246b91fc39fSArnaldo Carvalho de Melo goto out_put;
1247a116e05dSArnaldo Carvalho de Melo }
1248a116e05dSArnaldo Carvalho de Melo if (add_sched_out_event(atoms, 'S', timestamp))
1249b91fc39fSArnaldo Carvalho de Melo goto out_put;
1250cdce9d73SFrederic Weisbecker }
1251cdce9d73SFrederic Weisbecker
125239aeb52fSmingo BUG_ON(list_empty(&atoms->work_list));
1253cdce9d73SFrederic Weisbecker
125439aeb52fSmingo atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1255cdce9d73SFrederic Weisbecker
125655ffb7a6SMike Galbraith /*
125767d6259dSDongsheng Yang * As we do not guarantee the wakeup event happens when
125867d6259dSDongsheng Yang * task is out of run queue, also may happen when task is
125967d6259dSDongsheng Yang * on run queue and wakeup only change ->state to TASK_RUNNING,
126067d6259dSDongsheng Yang * then we should not set the ->wake_up_time when wake up a
126167d6259dSDongsheng Yang * task which is on run queue.
126267d6259dSDongsheng Yang *
126355ffb7a6SMike Galbraith * You WILL be missing events if you've recorded only
126455ffb7a6SMike Galbraith * one CPU, or are only looking at only one, so don't
126567d6259dSDongsheng Yang * skip in this case.
126655ffb7a6SMike Galbraith */
12670e9b07e5SArnaldo Carvalho de Melo if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1268b91fc39fSArnaldo Carvalho de Melo goto out_ok;
1269cdce9d73SFrederic Weisbecker
12700e9b07e5SArnaldo Carvalho de Melo sched->nr_timestamps++;
1271ea57c4f5SIngo Molnar if (atom->sched_out_time > timestamp) {
12720e9b07e5SArnaldo Carvalho de Melo sched->nr_unordered_timestamps++;
1273b91fc39fSArnaldo Carvalho de Melo goto out_ok;
1274ea57c4f5SIngo Molnar }
1275aa1ab9d2SFrederic Weisbecker
1276b1ffe8f3SIngo Molnar atom->state = THREAD_WAIT_CPU;
1277b1ffe8f3SIngo Molnar atom->wake_up_time = timestamp;
1278b91fc39fSArnaldo Carvalho de Melo out_ok:
1279b91fc39fSArnaldo Carvalho de Melo err = 0;
1280b91fc39fSArnaldo Carvalho de Melo out_put:
1281b91fc39fSArnaldo Carvalho de Melo thread__put(wakee);
1282b91fc39fSArnaldo Carvalho de Melo return err;
1283cdce9d73SFrederic Weisbecker }
1284cdce9d73SFrederic Weisbecker
latency_migrate_task_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)12859ec3f4e4SArnaldo Carvalho de Melo static int latency_migrate_task_event(struct perf_sched *sched,
128632dcd021SJiri Olsa struct evsel *evsel,
12879ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample,
12889ec3f4e4SArnaldo Carvalho de Melo struct machine *machine)
128955ffb7a6SMike Galbraith {
1290efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
12917f7f8d0bSArnaldo Carvalho de Melo u64 timestamp = sample->time;
129255ffb7a6SMike Galbraith struct work_atoms *atoms;
129355ffb7a6SMike Galbraith struct work_atom *atom;
129455ffb7a6SMike Galbraith struct thread *migrant;
1295b91fc39fSArnaldo Carvalho de Melo int err = -1;
129655ffb7a6SMike Galbraith
129755ffb7a6SMike Galbraith /*
129855ffb7a6SMike Galbraith * Only need to worry about migration when profiling one CPU.
129955ffb7a6SMike Galbraith */
13000e9b07e5SArnaldo Carvalho de Melo if (sched->profile_cpu == -1)
1301a116e05dSArnaldo Carvalho de Melo return 0;
130255ffb7a6SMike Galbraith
13031fcb8768SAdrian Hunter migrant = machine__findnew_thread(machine, -1, pid);
1304b91fc39fSArnaldo Carvalho de Melo if (migrant == NULL)
1305b91fc39fSArnaldo Carvalho de Melo return -1;
13060e9b07e5SArnaldo Carvalho de Melo atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
130755ffb7a6SMike Galbraith if (!atoms) {
13080e9b07e5SArnaldo Carvalho de Melo if (thread_atoms_insert(sched, migrant))
1309b91fc39fSArnaldo Carvalho de Melo goto out_put;
1310ee84a303SIan Rogers register_pid(sched, thread__tid(migrant), thread__comm_str(migrant));
13110e9b07e5SArnaldo Carvalho de Melo atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid);
1312a116e05dSArnaldo Carvalho de Melo if (!atoms) {
131360b7d14aSNamhyung Kim pr_err("migration-event: Internal tree error");
1314b91fc39fSArnaldo Carvalho de Melo goto out_put;
1315a116e05dSArnaldo Carvalho de Melo }
1316a116e05dSArnaldo Carvalho de Melo if (add_sched_out_event(atoms, 'R', timestamp))
1317b91fc39fSArnaldo Carvalho de Melo goto out_put;
131855ffb7a6SMike Galbraith }
131955ffb7a6SMike Galbraith
132055ffb7a6SMike Galbraith BUG_ON(list_empty(&atoms->work_list));
132155ffb7a6SMike Galbraith
132255ffb7a6SMike Galbraith atom = list_entry(atoms->work_list.prev, struct work_atom, list);
132355ffb7a6SMike Galbraith atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
132455ffb7a6SMike Galbraith
13250e9b07e5SArnaldo Carvalho de Melo sched->nr_timestamps++;
132655ffb7a6SMike Galbraith
132755ffb7a6SMike Galbraith if (atom->sched_out_time > timestamp)
13280e9b07e5SArnaldo Carvalho de Melo sched->nr_unordered_timestamps++;
1329b91fc39fSArnaldo Carvalho de Melo err = 0;
1330b91fc39fSArnaldo Carvalho de Melo out_put:
1331b91fc39fSArnaldo Carvalho de Melo thread__put(migrant);
1332b91fc39fSArnaldo Carvalho de Melo return err;
133355ffb7a6SMike Galbraith }
133455ffb7a6SMike Galbraith
output_lat_thread(struct perf_sched * sched,struct work_atoms * work_list)13350e9b07e5SArnaldo Carvalho de Melo static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list)
1336cdce9d73SFrederic Weisbecker {
1337cdce9d73SFrederic Weisbecker int i;
1338cdce9d73SFrederic Weisbecker int ret;
133966685678SFrederic Weisbecker u64 avg;
1340dc000c45SJoel Fernandes (Google) char max_lat_start[32], max_lat_end[32];
1341cdce9d73SFrederic Weisbecker
134239aeb52fSmingo if (!work_list->nb_atoms)
1343cdce9d73SFrederic Weisbecker return;
1344ea57c4f5SIngo Molnar /*
1345ea57c4f5SIngo Molnar * Ignore idle threads:
1346ea57c4f5SIngo Molnar */
1347b9c5143aSFrederic Weisbecker if (!strcmp(thread__comm_str(work_list->thread), "swapper"))
1348ea57c4f5SIngo Molnar return;
1349cdce9d73SFrederic Weisbecker
13500e9b07e5SArnaldo Carvalho de Melo sched->all_runtime += work_list->total_runtime;
13510e9b07e5SArnaldo Carvalho de Melo sched->all_count += work_list->nb_atoms;
135266685678SFrederic Weisbecker
1353ee84a303SIan Rogers if (work_list->num_merged > 1) {
1354ee84a303SIan Rogers ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread),
1355ee84a303SIan Rogers work_list->num_merged);
1356ee84a303SIan Rogers } else {
1357ee84a303SIan Rogers ret = printf(" %s:%d ", thread__comm_str(work_list->thread),
1358ee84a303SIan Rogers thread__tid(work_list->thread));
1359ee84a303SIan Rogers }
1360cdce9d73SFrederic Weisbecker
136108f69e6cSmingo for (i = 0; i < 24 - ret; i++)
1362cdce9d73SFrederic Weisbecker printf(" ");
1363cdce9d73SFrederic Weisbecker
136439aeb52fSmingo avg = work_list->total_lat / work_list->nb_atoms;
1365dc000c45SJoel Fernandes (Google) timestamp__scnprintf_usec(work_list->max_lat_start, max_lat_start, sizeof(max_lat_start));
1366dc000c45SJoel Fernandes (Google) timestamp__scnprintf_usec(work_list->max_lat_end, max_lat_end, sizeof(max_lat_end));
1367cdce9d73SFrederic Weisbecker
1368dc000c45SJoel 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",
13694fc76e49SArnaldo Carvalho de Melo (double)work_list->total_runtime / NSEC_PER_MSEC,
13704fc76e49SArnaldo Carvalho de Melo work_list->nb_atoms, (double)avg / NSEC_PER_MSEC,
13714fc76e49SArnaldo Carvalho de Melo (double)work_list->max_lat / NSEC_PER_MSEC,
1372dc000c45SJoel Fernandes (Google) max_lat_start, max_lat_end);
1373cdce9d73SFrederic Weisbecker }
1374cdce9d73SFrederic Weisbecker
pid_cmp(struct work_atoms * l,struct work_atoms * r)137539aeb52fSmingo static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1376daa1d7a5SFrederic Weisbecker {
1377ee84a303SIan Rogers pid_t l_tid, r_tid;
1378ee84a303SIan Rogers
137978c32f4cSIan Rogers if (RC_CHK_EQUAL(l->thread, r->thread))
13800014de17SJiri Olsa return 0;
1381ee84a303SIan Rogers l_tid = thread__tid(l->thread);
1382ee84a303SIan Rogers r_tid = thread__tid(r->thread);
1383ee84a303SIan Rogers if (l_tid < r_tid)
1384daa1d7a5SFrederic Weisbecker return -1;
1385ee84a303SIan Rogers if (l_tid > r_tid)
1386daa1d7a5SFrederic Weisbecker return 1;
1387f6005cafSIan Rogers return (int)(RC_CHK_ACCESS(l->thread) - RC_CHK_ACCESS(r->thread));
1388daa1d7a5SFrederic Weisbecker }
1389daa1d7a5SFrederic Weisbecker
avg_cmp(struct work_atoms * l,struct work_atoms * r)139039aeb52fSmingo static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1391daa1d7a5SFrederic Weisbecker {
1392daa1d7a5SFrederic Weisbecker u64 avgl, avgr;
1393daa1d7a5SFrederic Weisbecker
1394daa1d7a5SFrederic Weisbecker if (!l->nb_atoms)
1395daa1d7a5SFrederic Weisbecker return -1;
1396daa1d7a5SFrederic Weisbecker
1397daa1d7a5SFrederic Weisbecker if (!r->nb_atoms)
1398daa1d7a5SFrederic Weisbecker return 1;
1399daa1d7a5SFrederic Weisbecker
1400daa1d7a5SFrederic Weisbecker avgl = l->total_lat / l->nb_atoms;
1401daa1d7a5SFrederic Weisbecker avgr = r->total_lat / r->nb_atoms;
1402daa1d7a5SFrederic Weisbecker
1403daa1d7a5SFrederic Weisbecker if (avgl < avgr)
1404daa1d7a5SFrederic Weisbecker return -1;
1405daa1d7a5SFrederic Weisbecker if (avgl > avgr)
1406daa1d7a5SFrederic Weisbecker return 1;
1407daa1d7a5SFrederic Weisbecker
1408daa1d7a5SFrederic Weisbecker return 0;
1409daa1d7a5SFrederic Weisbecker }
1410daa1d7a5SFrederic Weisbecker
max_cmp(struct work_atoms * l,struct work_atoms * r)141139aeb52fSmingo static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1412daa1d7a5SFrederic Weisbecker {
1413daa1d7a5SFrederic Weisbecker if (l->max_lat < r->max_lat)
1414daa1d7a5SFrederic Weisbecker return -1;
1415daa1d7a5SFrederic Weisbecker if (l->max_lat > r->max_lat)
1416daa1d7a5SFrederic Weisbecker return 1;
1417daa1d7a5SFrederic Weisbecker
1418daa1d7a5SFrederic Weisbecker return 0;
1419daa1d7a5SFrederic Weisbecker }
1420daa1d7a5SFrederic Weisbecker
switch_cmp(struct work_atoms * l,struct work_atoms * r)142139aeb52fSmingo static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1422daa1d7a5SFrederic Weisbecker {
1423daa1d7a5SFrederic Weisbecker if (l->nb_atoms < r->nb_atoms)
1424daa1d7a5SFrederic Weisbecker return -1;
1425daa1d7a5SFrederic Weisbecker if (l->nb_atoms > r->nb_atoms)
1426daa1d7a5SFrederic Weisbecker return 1;
1427daa1d7a5SFrederic Weisbecker
1428daa1d7a5SFrederic Weisbecker return 0;
1429daa1d7a5SFrederic Weisbecker }
1430daa1d7a5SFrederic Weisbecker
runtime_cmp(struct work_atoms * l,struct work_atoms * r)143139aeb52fSmingo static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1432daa1d7a5SFrederic Weisbecker {
1433daa1d7a5SFrederic Weisbecker if (l->total_runtime < r->total_runtime)
1434daa1d7a5SFrederic Weisbecker return -1;
1435daa1d7a5SFrederic Weisbecker if (l->total_runtime > r->total_runtime)
1436daa1d7a5SFrederic Weisbecker return 1;
1437daa1d7a5SFrederic Weisbecker
1438daa1d7a5SFrederic Weisbecker return 0;
1439daa1d7a5SFrederic Weisbecker }
1440daa1d7a5SFrederic Weisbecker
sort_dimension__add(const char * tok,struct list_head * list)14410e9b07e5SArnaldo Carvalho de Melo static int sort_dimension__add(const char *tok, struct list_head *list)
14420e9b07e5SArnaldo Carvalho de Melo {
14430e9b07e5SArnaldo Carvalho de Melo size_t i;
14440e9b07e5SArnaldo Carvalho de Melo static struct sort_dimension avg_sort_dimension = {
14450e9b07e5SArnaldo Carvalho de Melo .name = "avg",
14460e9b07e5SArnaldo Carvalho de Melo .cmp = avg_cmp,
14470e9b07e5SArnaldo Carvalho de Melo };
14480e9b07e5SArnaldo Carvalho de Melo static struct sort_dimension max_sort_dimension = {
14490e9b07e5SArnaldo Carvalho de Melo .name = "max",
14500e9b07e5SArnaldo Carvalho de Melo .cmp = max_cmp,
14510e9b07e5SArnaldo Carvalho de Melo };
14520e9b07e5SArnaldo Carvalho de Melo static struct sort_dimension pid_sort_dimension = {
14530e9b07e5SArnaldo Carvalho de Melo .name = "pid",
14540e9b07e5SArnaldo Carvalho de Melo .cmp = pid_cmp,
14550e9b07e5SArnaldo Carvalho de Melo };
1456daa1d7a5SFrederic Weisbecker static struct sort_dimension runtime_sort_dimension = {
1457daa1d7a5SFrederic Weisbecker .name = "runtime",
1458daa1d7a5SFrederic Weisbecker .cmp = runtime_cmp,
1459daa1d7a5SFrederic Weisbecker };
14600e9b07e5SArnaldo Carvalho de Melo static struct sort_dimension switch_sort_dimension = {
14610e9b07e5SArnaldo Carvalho de Melo .name = "switch",
14620e9b07e5SArnaldo Carvalho de Melo .cmp = switch_cmp,
14630e9b07e5SArnaldo Carvalho de Melo };
14640e9b07e5SArnaldo Carvalho de Melo struct sort_dimension *available_sorts[] = {
1465daa1d7a5SFrederic Weisbecker &pid_sort_dimension,
1466daa1d7a5SFrederic Weisbecker &avg_sort_dimension,
1467daa1d7a5SFrederic Weisbecker &max_sort_dimension,
1468daa1d7a5SFrederic Weisbecker &switch_sort_dimension,
1469daa1d7a5SFrederic Weisbecker &runtime_sort_dimension,
1470daa1d7a5SFrederic Weisbecker };
1471daa1d7a5SFrederic Weisbecker
14720e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < ARRAY_SIZE(available_sorts); i++) {
1473daa1d7a5SFrederic Weisbecker if (!strcmp(available_sorts[i]->name, tok)) {
1474daa1d7a5SFrederic Weisbecker list_add_tail(&available_sorts[i]->list, list);
1475daa1d7a5SFrederic Weisbecker
1476daa1d7a5SFrederic Weisbecker return 0;
1477daa1d7a5SFrederic Weisbecker }
1478daa1d7a5SFrederic Weisbecker }
1479daa1d7a5SFrederic Weisbecker
1480daa1d7a5SFrederic Weisbecker return -1;
1481daa1d7a5SFrederic Weisbecker }
1482daa1d7a5SFrederic Weisbecker
perf_sched__sort_lat(struct perf_sched * sched)14830e9b07e5SArnaldo Carvalho de Melo static void perf_sched__sort_lat(struct perf_sched *sched)
1484daa1d7a5SFrederic Weisbecker {
1485daa1d7a5SFrederic Weisbecker struct rb_node *node;
1486cb4c13a5SDavidlohr Bueso struct rb_root_cached *root = &sched->atom_root;
14872f80dd44SJosef Bacik again:
1488daa1d7a5SFrederic Weisbecker for (;;) {
148939aeb52fSmingo struct work_atoms *data;
1490cb4c13a5SDavidlohr Bueso node = rb_first_cached(root);
1491daa1d7a5SFrederic Weisbecker if (!node)
1492daa1d7a5SFrederic Weisbecker break;
1493daa1d7a5SFrederic Weisbecker
1494cb4c13a5SDavidlohr Bueso rb_erase_cached(node, root);
149539aeb52fSmingo data = rb_entry(node, struct work_atoms, node);
14960e9b07e5SArnaldo Carvalho de Melo __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list);
1497daa1d7a5SFrederic Weisbecker }
14982f80dd44SJosef Bacik if (root == &sched->atom_root) {
14992f80dd44SJosef Bacik root = &sched->merged_atom_root;
15002f80dd44SJosef Bacik goto again;
15012f80dd44SJosef Bacik }
1502daa1d7a5SFrederic Weisbecker }
1503daa1d7a5SFrederic Weisbecker
process_sched_wakeup_event(const struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)150430f29baeSIan Rogers static int process_sched_wakeup_event(const struct perf_tool *tool,
150532dcd021SJiri Olsa struct evsel *evsel,
1506ee29be62SArnaldo Carvalho de Melo struct perf_sample *sample,
15074218e673SArnaldo Carvalho de Melo struct machine *machine)
1508419ab0d6SFrederic Weisbecker {
15090e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1510419ab0d6SFrederic Weisbecker
15119ec3f4e4SArnaldo Carvalho de Melo if (sched->tp_handler->wakeup_event)
15129ec3f4e4SArnaldo Carvalho de Melo return sched->tp_handler->wakeup_event(sched, evsel, sample, machine);
1513419ab0d6SFrederic Weisbecker
15142b7fcbc5SArnaldo Carvalho de Melo return 0;
1515419ab0d6SFrederic Weisbecker }
1516419ab0d6SFrederic Weisbecker
process_sched_wakeup_ignore(const struct perf_tool * tool __maybe_unused,struct evsel * evsel __maybe_unused,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)151730f29baeSIan Rogers static int process_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unused,
15184453deacSChunxin Zang struct evsel *evsel __maybe_unused,
15194453deacSChunxin Zang struct perf_sample *sample __maybe_unused,
15204453deacSChunxin Zang struct machine *machine __maybe_unused)
15214453deacSChunxin Zang {
15224453deacSChunxin Zang return 0;
15234453deacSChunxin Zang }
15244453deacSChunxin Zang
1525a151a37aSJiri Olsa union map_priv {
1526a151a37aSJiri Olsa void *ptr;
1527a151a37aSJiri Olsa bool color;
1528a151a37aSJiri Olsa };
1529a151a37aSJiri Olsa
thread__has_color(struct thread * thread)1530a151a37aSJiri Olsa static bool thread__has_color(struct thread *thread)
1531a151a37aSJiri Olsa {
1532a151a37aSJiri Olsa union map_priv priv = {
1533a151a37aSJiri Olsa .ptr = thread__priv(thread),
1534a151a37aSJiri Olsa };
1535a151a37aSJiri Olsa
1536a151a37aSJiri Olsa return priv.color;
1537a151a37aSJiri Olsa }
1538a151a37aSJiri Olsa
1539a151a37aSJiri Olsa static struct thread*
map__findnew_thread(struct perf_sched * sched,struct machine * machine,pid_t pid,pid_t tid)1540a151a37aSJiri Olsa map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid)
1541a151a37aSJiri Olsa {
1542a151a37aSJiri Olsa struct thread *thread = machine__findnew_thread(machine, pid, tid);
1543a151a37aSJiri Olsa union map_priv priv = {
1544a151a37aSJiri Olsa .color = false,
1545a151a37aSJiri Olsa };
1546a151a37aSJiri Olsa
1547a151a37aSJiri Olsa if (!sched->map.color_pids || !thread || thread__priv(thread))
1548a151a37aSJiri Olsa return thread;
1549a151a37aSJiri Olsa
1550a151a37aSJiri Olsa if (thread_map__has(sched->map.color_pids, tid))
1551a151a37aSJiri Olsa priv.color = true;
1552a151a37aSJiri Olsa
1553a151a37aSJiri Olsa thread__set_priv(thread, priv.ptr);
1554a151a37aSJiri Olsa return thread;
1555a151a37aSJiri Olsa }
1556a151a37aSJiri Olsa
sched_match_task(struct perf_sched * sched,const char * comm_str)1557306f921eSMadadi Vineeth Reddy static bool sched_match_task(struct perf_sched *sched, const char *comm_str)
15589cc0afedSMadadi Vineeth Reddy {
1559306f921eSMadadi Vineeth Reddy bool fuzzy_match = sched->map.fuzzy;
1560306f921eSMadadi Vineeth Reddy struct strlist *task_names = sched->map.task_names;
15619cc0afedSMadadi Vineeth Reddy struct str_node *node;
15629cc0afedSMadadi Vineeth Reddy
15639cc0afedSMadadi Vineeth Reddy strlist__for_each_entry(node, task_names) {
1564306f921eSMadadi Vineeth Reddy bool match_found = fuzzy_match ? !!strstr(comm_str, node->s) :
1565306f921eSMadadi Vineeth Reddy !strcmp(comm_str, node->s);
1566306f921eSMadadi Vineeth Reddy if (match_found)
15679cc0afedSMadadi Vineeth Reddy return true;
15689cc0afedSMadadi Vineeth Reddy }
15699cc0afedSMadadi Vineeth Reddy
15709cc0afedSMadadi Vineeth Reddy return false;
15719cc0afedSMadadi Vineeth Reddy }
15729cc0afedSMadadi Vineeth Reddy
print_sched_map(struct perf_sched * sched,struct perf_cpu this_cpu,int cpus_nr,const char * color,bool sched_out)15733116d609SMadadi Vineeth Reddy static void print_sched_map(struct perf_sched *sched, struct perf_cpu this_cpu, int cpus_nr,
15743116d609SMadadi Vineeth Reddy const char *color, bool sched_out)
15753116d609SMadadi Vineeth Reddy {
15763116d609SMadadi Vineeth Reddy for (int i = 0; i < cpus_nr; i++) {
15773116d609SMadadi Vineeth Reddy struct perf_cpu cpu = {
15783116d609SMadadi Vineeth Reddy .cpu = sched->map.comp ? sched->map.comp_cpus[i].cpu : i,
15793116d609SMadadi Vineeth Reddy };
15803116d609SMadadi Vineeth Reddy struct thread *curr_thread = sched->curr_thread[cpu.cpu];
15813116d609SMadadi Vineeth Reddy struct thread *curr_out_thread = sched->curr_out_thread[cpu.cpu];
15823116d609SMadadi Vineeth Reddy struct thread_runtime *curr_tr;
15833116d609SMadadi Vineeth Reddy const char *pid_color = color;
15843116d609SMadadi Vineeth Reddy const char *cpu_color = color;
15853116d609SMadadi Vineeth Reddy char symbol = ' ';
15863116d609SMadadi Vineeth Reddy struct thread *thread_to_check = sched_out ? curr_out_thread : curr_thread;
15873116d609SMadadi Vineeth Reddy
15883116d609SMadadi Vineeth Reddy if (thread_to_check && thread__has_color(thread_to_check))
15893116d609SMadadi Vineeth Reddy pid_color = COLOR_PIDS;
15903116d609SMadadi Vineeth Reddy
15913116d609SMadadi Vineeth Reddy if (sched->map.color_cpus && perf_cpu_map__has(sched->map.color_cpus, cpu))
15923116d609SMadadi Vineeth Reddy cpu_color = COLOR_CPUS;
15933116d609SMadadi Vineeth Reddy
15943116d609SMadadi Vineeth Reddy if (cpu.cpu == this_cpu.cpu)
15953116d609SMadadi Vineeth Reddy symbol = '*';
15963116d609SMadadi Vineeth Reddy
15973116d609SMadadi Vineeth Reddy color_fprintf(stdout, cpu.cpu != this_cpu.cpu ? color : cpu_color, "%c", symbol);
15983116d609SMadadi Vineeth Reddy
15993116d609SMadadi Vineeth Reddy thread_to_check = sched_out ? sched->curr_out_thread[cpu.cpu] :
16003116d609SMadadi Vineeth Reddy sched->curr_thread[cpu.cpu];
16013116d609SMadadi Vineeth Reddy
16023116d609SMadadi Vineeth Reddy if (thread_to_check) {
16033116d609SMadadi Vineeth Reddy curr_tr = thread__get_runtime(thread_to_check);
16043116d609SMadadi Vineeth Reddy if (curr_tr == NULL)
16053116d609SMadadi Vineeth Reddy return;
16063116d609SMadadi Vineeth Reddy
16073116d609SMadadi Vineeth Reddy if (sched_out) {
16083116d609SMadadi Vineeth Reddy if (cpu.cpu == this_cpu.cpu)
16093116d609SMadadi Vineeth Reddy color_fprintf(stdout, color, "- ");
16103116d609SMadadi Vineeth Reddy else {
16113116d609SMadadi Vineeth Reddy curr_tr = thread__get_runtime(sched->curr_thread[cpu.cpu]);
16123116d609SMadadi Vineeth Reddy if (curr_tr != NULL)
16133116d609SMadadi Vineeth Reddy color_fprintf(stdout, pid_color, "%2s ",
16143116d609SMadadi Vineeth Reddy curr_tr->shortname);
16153116d609SMadadi Vineeth Reddy }
16163116d609SMadadi Vineeth Reddy } else
16173116d609SMadadi Vineeth Reddy color_fprintf(stdout, pid_color, "%2s ", curr_tr->shortname);
16183116d609SMadadi Vineeth Reddy } else
16193116d609SMadadi Vineeth Reddy color_fprintf(stdout, color, " ");
16203116d609SMadadi Vineeth Reddy }
16213116d609SMadadi Vineeth Reddy }
16223116d609SMadadi Vineeth Reddy
map_switch_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)162332dcd021SJiri Olsa static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
16249ec3f4e4SArnaldo Carvalho de Melo struct perf_sample *sample, struct machine *machine)
16250ec04e16SIngo Molnar {
1626efc0cdc9SArnaldo Carvalho de Melo const u32 next_pid = evsel__intval(evsel, sample, "next_pid");
16273116d609SMadadi Vineeth Reddy const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid");
16283116d609SMadadi Vineeth Reddy struct thread *sched_in, *sched_out;
16298640da9fSChangbin Du struct thread_runtime *tr;
16300ec04e16SIngo Molnar int new_shortname;
16317f7f8d0bSArnaldo Carvalho de Melo u64 timestamp0, timestamp = sample->time;
16320ec04e16SIngo Molnar s64 delta;
16336d18804bSIan Rogers struct perf_cpu this_cpu = {
16346d18804bSIan Rogers .cpu = sample->cpu,
16356d18804bSIan Rogers };
163699623c62SJiri Olsa int cpus_nr;
16373116d609SMadadi Vineeth Reddy int proceed;
163899623c62SJiri Olsa bool new_cpu = false;
16398cd91195SJiri Olsa const char *color = PERF_COLOR_NORMAL;
164099620a5dSNamhyung Kim char stimestamp[32];
16413116d609SMadadi Vineeth Reddy const char *str;
16420ec04e16SIngo Molnar
16436d18804bSIan Rogers BUG_ON(this_cpu.cpu >= MAX_CPUS || this_cpu.cpu < 0);
16440ec04e16SIngo Molnar
16456d18804bSIan Rogers if (this_cpu.cpu > sched->max_cpu.cpu)
16460e9b07e5SArnaldo Carvalho de Melo sched->max_cpu = this_cpu;
16470ec04e16SIngo Molnar
164899623c62SJiri Olsa if (sched->map.comp) {
164999623c62SJiri Olsa cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS);
165075d7ba32SSean Christopherson if (!__test_and_set_bit(this_cpu.cpu, sched->map.comp_cpus_mask)) {
165199623c62SJiri Olsa sched->map.comp_cpus[cpus_nr++] = this_cpu;
165299623c62SJiri Olsa new_cpu = true;
165399623c62SJiri Olsa }
165499623c62SJiri Olsa } else
16556d18804bSIan Rogers cpus_nr = sched->max_cpu.cpu;
165699623c62SJiri Olsa
16576d18804bSIan Rogers timestamp0 = sched->cpu_last_switched[this_cpu.cpu];
16586d18804bSIan Rogers sched->cpu_last_switched[this_cpu.cpu] = timestamp;
16590ec04e16SIngo Molnar if (timestamp0)
16600ec04e16SIngo Molnar delta = timestamp - timestamp0;
16610ec04e16SIngo Molnar else
16620ec04e16SIngo Molnar delta = 0;
16630ec04e16SIngo Molnar
1664a116e05dSArnaldo Carvalho de Melo if (delta < 0) {
166560b7d14aSNamhyung Kim pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1666a116e05dSArnaldo Carvalho de Melo return -1;
1667a116e05dSArnaldo Carvalho de Melo }
16680ec04e16SIngo Molnar
1669a151a37aSJiri Olsa sched_in = map__findnew_thread(sched, machine, -1, next_pid);
16703116d609SMadadi Vineeth Reddy sched_out = map__findnew_thread(sched, machine, -1, prev_pid);
16713116d609SMadadi Vineeth Reddy if (sched_in == NULL || sched_out == NULL)
1672b91fc39fSArnaldo Carvalho de Melo return -1;
16730ec04e16SIngo Molnar
16748640da9fSChangbin Du tr = thread__get_runtime(sched_in);
16758640da9fSChangbin Du if (tr == NULL) {
16768640da9fSChangbin Du thread__put(sched_in);
16778640da9fSChangbin Du return -1;
16788640da9fSChangbin Du }
16798640da9fSChangbin Du
16806d18804bSIan Rogers sched->curr_thread[this_cpu.cpu] = thread__get(sched_in);
16813116d609SMadadi Vineeth Reddy sched->curr_out_thread[this_cpu.cpu] = thread__get(sched_out);
16820ec04e16SIngo Molnar
16833116d609SMadadi Vineeth Reddy str = thread__comm_str(sched_in);
16840ec04e16SIngo Molnar new_shortname = 0;
16858640da9fSChangbin Du if (!tr->shortname[0]) {
16866bcab4e1SDongsheng if (!strcmp(thread__comm_str(sched_in), "swapper")) {
16876bcab4e1SDongsheng /*
16886bcab4e1SDongsheng * Don't allocate a letter-number for swapper:0
16896bcab4e1SDongsheng * as a shortname. Instead, we use '.' for it.
16906bcab4e1SDongsheng */
16918640da9fSChangbin Du tr->shortname[0] = '.';
16928640da9fSChangbin Du tr->shortname[1] = ' ';
1693306f921eSMadadi Vineeth Reddy } else if (!sched->map.task_name || sched_match_task(sched, str)) {
16948640da9fSChangbin Du tr->shortname[0] = sched->next_shortname1;
16958640da9fSChangbin Du tr->shortname[1] = sched->next_shortname2;
16960ec04e16SIngo Molnar
16970e9b07e5SArnaldo Carvalho de Melo if (sched->next_shortname1 < 'Z') {
16980e9b07e5SArnaldo Carvalho de Melo sched->next_shortname1++;
16990ec04e16SIngo Molnar } else {
17000e9b07e5SArnaldo Carvalho de Melo sched->next_shortname1 = 'A';
17016bcab4e1SDongsheng if (sched->next_shortname2 < '9')
17020e9b07e5SArnaldo Carvalho de Melo sched->next_shortname2++;
17036bcab4e1SDongsheng else
17040e9b07e5SArnaldo Carvalho de Melo sched->next_shortname2 = '0';
17050ec04e16SIngo Molnar }
17063116d609SMadadi Vineeth Reddy } else {
17073116d609SMadadi Vineeth Reddy tr->shortname[0] = '-';
17083116d609SMadadi Vineeth Reddy tr->shortname[1] = ' ';
17090ec04e16SIngo Molnar }
17100ec04e16SIngo Molnar new_shortname = 1;
17110ec04e16SIngo Molnar }
17120ec04e16SIngo Molnar
1713231295a1SFernand Sieber if (sched->map.cpus && !perf_cpu_map__has(sched->map.cpus, this_cpu))
1714231295a1SFernand Sieber goto out;
1715231295a1SFernand Sieber
17163116d609SMadadi Vineeth Reddy proceed = 0;
17173116d609SMadadi Vineeth Reddy str = thread__comm_str(sched_in);
17183116d609SMadadi Vineeth Reddy /*
17193116d609SMadadi Vineeth Reddy * Check which of sched_in and sched_out matches the passed --task-name
17203116d609SMadadi Vineeth Reddy * arguments and call the corresponding print_sched_map.
17213116d609SMadadi Vineeth Reddy */
1722306f921eSMadadi Vineeth Reddy if (sched->map.task_name && !sched_match_task(sched, str)) {
1723306f921eSMadadi Vineeth Reddy if (!sched_match_task(sched, thread__comm_str(sched_out)))
17243116d609SMadadi Vineeth Reddy goto out;
17253116d609SMadadi Vineeth Reddy else
17263116d609SMadadi Vineeth Reddy goto sched_out;
17273116d609SMadadi Vineeth Reddy
17283116d609SMadadi Vineeth Reddy } else {
17293116d609SMadadi Vineeth Reddy str = thread__comm_str(sched_out);
1730306f921eSMadadi Vineeth Reddy if (!(sched->map.task_name && !sched_match_task(sched, str)))
17313116d609SMadadi Vineeth Reddy proceed = 1;
17323116d609SMadadi Vineeth Reddy }
17333116d609SMadadi Vineeth Reddy
1734231295a1SFernand Sieber printf(" ");
1735231295a1SFernand Sieber
17363116d609SMadadi Vineeth Reddy print_sched_map(sched, this_cpu, cpus_nr, color, false);
17370ec04e16SIngo Molnar
173899620a5dSNamhyung Kim timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
173999620a5dSNamhyung Kim color_fprintf(stdout, color, " %12s secs ", stimestamp);
1740ee84a303SIan Rogers if (new_shortname || tr->comm_changed || (verbose > 0 && thread__tid(sched_in))) {
1741a151a37aSJiri Olsa const char *pid_color = color;
1742a151a37aSJiri Olsa
1743a151a37aSJiri Olsa if (thread__has_color(sched_in))
1744a151a37aSJiri Olsa pid_color = COLOR_PIDS;
1745a151a37aSJiri Olsa
1746a151a37aSJiri Olsa color_fprintf(stdout, pid_color, "%s => %s:%d",
1747ee84a303SIan Rogers tr->shortname, thread__comm_str(sched_in), thread__tid(sched_in));
174899a3c3a9SChangbin Du tr->comm_changed = false;
17490ec04e16SIngo Molnar }
1750a116e05dSArnaldo Carvalho de Melo
175199623c62SJiri Olsa if (sched->map.comp && new_cpu)
17528cd91195SJiri Olsa color_fprintf(stdout, color, " (CPU %d)", this_cpu);
175399623c62SJiri Olsa
17543116d609SMadadi Vineeth Reddy if (proceed != 1) {
17553116d609SMadadi Vineeth Reddy color_fprintf(stdout, color, "\n");
17563116d609SMadadi Vineeth Reddy goto out;
17573116d609SMadadi Vineeth Reddy }
17583116d609SMadadi Vineeth Reddy
17593116d609SMadadi Vineeth Reddy sched_out:
17603116d609SMadadi Vineeth Reddy if (sched->map.task_name) {
17613116d609SMadadi Vineeth Reddy tr = thread__get_runtime(sched->curr_out_thread[this_cpu.cpu]);
17623116d609SMadadi Vineeth Reddy if (strcmp(tr->shortname, "") == 0)
17633116d609SMadadi Vineeth Reddy goto out;
17643116d609SMadadi Vineeth Reddy
17653116d609SMadadi Vineeth Reddy if (proceed == 1)
17663116d609SMadadi Vineeth Reddy color_fprintf(stdout, color, "\n");
17673116d609SMadadi Vineeth Reddy
17683116d609SMadadi Vineeth Reddy printf(" ");
17693116d609SMadadi Vineeth Reddy print_sched_map(sched, this_cpu, cpus_nr, color, true);
17703116d609SMadadi Vineeth Reddy timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp));
17713116d609SMadadi Vineeth Reddy color_fprintf(stdout, color, " %12s secs ", stimestamp);
17723116d609SMadadi Vineeth Reddy }
17733116d609SMadadi Vineeth Reddy
17748cd91195SJiri Olsa color_fprintf(stdout, color, "\n");
177599623c62SJiri Olsa
1776231295a1SFernand Sieber out:
17773116d609SMadadi Vineeth Reddy if (sched->map.task_name)
17783116d609SMadadi Vineeth Reddy thread__put(sched_out);
17793116d609SMadadi Vineeth Reddy
1780b91fc39fSArnaldo Carvalho de Melo thread__put(sched_in);
1781b91fc39fSArnaldo Carvalho de Melo
1782a116e05dSArnaldo Carvalho de Melo return 0;
17830ec04e16SIngo Molnar }
17840ec04e16SIngo Molnar
process_sched_switch_event(const struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)178530f29baeSIan Rogers static int process_sched_switch_event(const struct perf_tool *tool,
178632dcd021SJiri Olsa struct evsel *evsel,
1787ee29be62SArnaldo Carvalho de Melo struct perf_sample *sample,
17884218e673SArnaldo Carvalho de Melo struct machine *machine)
1789419ab0d6SFrederic Weisbecker {
17900e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
1791a116e05dSArnaldo Carvalho de Melo int this_cpu = sample->cpu, err = 0;
1792efc0cdc9SArnaldo Carvalho de Melo u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"),
1793efc0cdc9SArnaldo Carvalho de Melo next_pid = evsel__intval(evsel, sample, "next_pid");
1794419ab0d6SFrederic Weisbecker
17950e9b07e5SArnaldo Carvalho de Melo if (sched->curr_pid[this_cpu] != (u32)-1) {
1796c8a37751SIngo Molnar /*
1797c8a37751SIngo Molnar * Are we trying to switch away a PID that is
1798c8a37751SIngo Molnar * not current?
1799c8a37751SIngo Molnar */
18002b7fcbc5SArnaldo Carvalho de Melo if (sched->curr_pid[this_cpu] != prev_pid)
18010e9b07e5SArnaldo Carvalho de Melo sched->nr_context_switch_bugs++;
1802c8a37751SIngo Molnar }
1803c8a37751SIngo Molnar
18049ec3f4e4SArnaldo Carvalho de Melo if (sched->tp_handler->switch_event)
18059ec3f4e4SArnaldo Carvalho de Melo err = sched->tp_handler->switch_event(sched, evsel, sample, machine);
18062b7fcbc5SArnaldo Carvalho de Melo
18072b7fcbc5SArnaldo Carvalho de Melo sched->curr_pid[this_cpu] = next_pid;
1808a116e05dSArnaldo Carvalho de Melo return err;
1809419ab0d6SFrederic Weisbecker }
1810419ab0d6SFrederic Weisbecker
process_sched_runtime_event(const struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)181130f29baeSIan Rogers static int process_sched_runtime_event(const struct perf_tool *tool,
181232dcd021SJiri Olsa struct evsel *evsel,
1813ee29be62SArnaldo Carvalho de Melo struct perf_sample *sample,
18144218e673SArnaldo Carvalho de Melo struct machine *machine)
181539aeb52fSmingo {
18160e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
181739aeb52fSmingo
18189ec3f4e4SArnaldo Carvalho de Melo if (sched->tp_handler->runtime_event)
18199ec3f4e4SArnaldo Carvalho de Melo return sched->tp_handler->runtime_event(sched, evsel, sample, machine);
182039aeb52fSmingo
1821a116e05dSArnaldo Carvalho de Melo return 0;
1822ec156764SIngo Molnar }
1823ec156764SIngo Molnar
perf_sched__process_fork_event(const struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)182430f29baeSIan Rogers static int perf_sched__process_fork_event(const struct perf_tool *tool,
1825cb627505SDavid Ahern union perf_event *event,
18262b7fcbc5SArnaldo Carvalho de Melo struct perf_sample *sample,
1827cb627505SDavid Ahern struct machine *machine)
18282b7fcbc5SArnaldo Carvalho de Melo {
18292b7fcbc5SArnaldo Carvalho de Melo struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
18302b7fcbc5SArnaldo Carvalho de Melo
18314d39c89fSIngo Molnar /* run the fork event through the perf machinery */
1832cb627505SDavid Ahern perf_event__process_fork(tool, event, sample, machine);
1833cb627505SDavid Ahern
1834cb627505SDavid Ahern /* and then run additional processing needed for this command */
18359ec3f4e4SArnaldo Carvalho de Melo if (sched->tp_handler->fork_event)
1836cb627505SDavid Ahern return sched->tp_handler->fork_event(sched, event, machine);
18372b7fcbc5SArnaldo Carvalho de Melo
18382b7fcbc5SArnaldo Carvalho de Melo return 0;
18392b7fcbc5SArnaldo Carvalho de Melo }
18402b7fcbc5SArnaldo Carvalho de Melo
process_sched_migrate_task_event(const struct perf_tool * tool,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)184130f29baeSIan Rogers static int process_sched_migrate_task_event(const struct perf_tool *tool,
184232dcd021SJiri Olsa struct evsel *evsel,
1843ee29be62SArnaldo Carvalho de Melo struct perf_sample *sample,
18444218e673SArnaldo Carvalho de Melo struct machine *machine)
184555ffb7a6SMike Galbraith {
18460e9b07e5SArnaldo Carvalho de Melo struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
184755ffb7a6SMike Galbraith
18489ec3f4e4SArnaldo Carvalho de Melo if (sched->tp_handler->migrate_task_event)
18499ec3f4e4SArnaldo Carvalho de Melo return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine);
185055ffb7a6SMike Galbraith
18512b7fcbc5SArnaldo Carvalho de Melo return 0;
185255ffb7a6SMike Galbraith }
185355ffb7a6SMike Galbraith
185430f29baeSIan Rogers typedef int (*tracepoint_handler)(const struct perf_tool *tool,
185532dcd021SJiri Olsa struct evsel *evsel,
1856ee29be62SArnaldo Carvalho de Melo struct perf_sample *sample,
18574218e673SArnaldo Carvalho de Melo struct machine *machine);
1858ec156764SIngo Molnar
perf_sched__process_tracepoint_sample(const struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)185930f29baeSIan Rogers static int perf_sched__process_tracepoint_sample(const struct perf_tool *tool __maybe_unused,
18601d037ca1SIrina Tirdea union perf_event *event __maybe_unused,
18618115d60cSArnaldo Carvalho de Melo struct perf_sample *sample,
186232dcd021SJiri Olsa struct evsel *evsel,
1863743eb868SArnaldo Carvalho de Melo struct machine *machine)
18640a02ad93SIngo Molnar {
1865a116e05dSArnaldo Carvalho de Melo int err = 0;
1866a80deb62SArnaldo Carvalho de Melo
1867744a9719SArnaldo Carvalho de Melo if (evsel->handler != NULL) {
1868744a9719SArnaldo Carvalho de Melo tracepoint_handler f = evsel->handler;
18692b7fcbc5SArnaldo Carvalho de Melo err = f(tool, evsel, sample, machine);
1870ee29be62SArnaldo Carvalho de Melo }
18710a02ad93SIngo Molnar
1872a116e05dSArnaldo Carvalho de Melo return err;
18730a02ad93SIngo Molnar }
18740a02ad93SIngo Molnar
perf_sched__process_comm(const struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)187530f29baeSIan Rogers static int perf_sched__process_comm(const struct perf_tool *tool __maybe_unused,
187699a3c3a9SChangbin Du union perf_event *event,
187799a3c3a9SChangbin Du struct perf_sample *sample,
187899a3c3a9SChangbin Du struct machine *machine)
187999a3c3a9SChangbin Du {
188099a3c3a9SChangbin Du struct thread *thread;
188199a3c3a9SChangbin Du struct thread_runtime *tr;
188299a3c3a9SChangbin Du int err;
188399a3c3a9SChangbin Du
188499a3c3a9SChangbin Du err = perf_event__process_comm(tool, event, sample, machine);
188599a3c3a9SChangbin Du if (err)
188699a3c3a9SChangbin Du return err;
188799a3c3a9SChangbin Du
188899a3c3a9SChangbin Du thread = machine__find_thread(machine, sample->pid, sample->tid);
188999a3c3a9SChangbin Du if (!thread) {
189099a3c3a9SChangbin Du pr_err("Internal error: can't find thread\n");
189199a3c3a9SChangbin Du return -1;
189299a3c3a9SChangbin Du }
189399a3c3a9SChangbin Du
189499a3c3a9SChangbin Du tr = thread__get_runtime(thread);
189599a3c3a9SChangbin Du if (tr == NULL) {
189699a3c3a9SChangbin Du thread__put(thread);
189799a3c3a9SChangbin Du return -1;
189899a3c3a9SChangbin Du }
189999a3c3a9SChangbin Du
190099a3c3a9SChangbin Du tr->comm_changed = true;
190199a3c3a9SChangbin Du thread__put(thread);
190299a3c3a9SChangbin Du
190399a3c3a9SChangbin Du return 0;
190499a3c3a9SChangbin Du }
190599a3c3a9SChangbin Du
perf_sched__read_events(struct perf_sched * sched)1906ae536acfSArnaldo Carvalho de Melo static int perf_sched__read_events(struct perf_sched *sched)
19070a02ad93SIngo Molnar {
19084453deacSChunxin Zang struct evsel_str_handler handlers[] = {
1909ee29be62SArnaldo Carvalho de Melo { "sched:sched_switch", process_sched_switch_event, },
1910ee29be62SArnaldo Carvalho de Melo { "sched:sched_stat_runtime", process_sched_runtime_event, },
1911ee29be62SArnaldo Carvalho de Melo { "sched:sched_wakeup", process_sched_wakeup_event, },
19124453deacSChunxin Zang { "sched:sched_waking", process_sched_wakeup_event, },
1913ee29be62SArnaldo Carvalho de Melo { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1914ee29be62SArnaldo Carvalho de Melo { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1915ee29be62SArnaldo Carvalho de Melo };
1916da378962SArnaldo Carvalho de Melo struct perf_session *session;
19178ceb41d7SJiri Olsa struct perf_data data = {
1918f5fc1412SJiri Olsa .path = input_name,
1919f5fc1412SJiri Olsa .mode = PERF_DATA_MODE_READ,
1920f0dd330fSYunlong Song .force = sched->force,
1921f5fc1412SJiri Olsa };
1922ae536acfSArnaldo Carvalho de Melo int rc = -1;
1923da378962SArnaldo Carvalho de Melo
19242681bd85SNamhyung Kim session = perf_session__new(&data, &sched->tool);
19256ef81c55SMamatha Inamdar if (IS_ERR(session)) {
19266ef81c55SMamatha Inamdar pr_debug("Error creating perf session");
19276ef81c55SMamatha Inamdar return PTR_ERR(session);
1928a116e05dSArnaldo Carvalho de Melo }
192994c744b6SArnaldo Carvalho de Melo
19300a7e6d1bSNamhyung Kim symbol__init(&session->header.env);
193104934106SNamhyung Kim
19324453deacSChunxin Zang /* prefer sched_waking if it is captured */
19334453deacSChunxin Zang if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking"))
19344453deacSChunxin Zang handlers[2].handler = process_sched_wakeup_ignore;
19354453deacSChunxin Zang
1936a116e05dSArnaldo Carvalho de Melo if (perf_session__set_tracepoints_handlers(session, handlers))
1937a116e05dSArnaldo Carvalho de Melo goto out_delete;
1938ee29be62SArnaldo Carvalho de Melo
1939cee75ac7SArnaldo Carvalho de Melo if (perf_session__has_traces(session, "record -R")) {
1940b7b61cbeSArnaldo Carvalho de Melo int err = perf_session__process_events(session);
1941a116e05dSArnaldo Carvalho de Melo if (err) {
1942a116e05dSArnaldo Carvalho de Melo pr_err("Failed to process events, error %d", err);
1943a116e05dSArnaldo Carvalho de Melo goto out_delete;
1944a116e05dSArnaldo Carvalho de Melo }
19454c09bafaSJiri Olsa
194675be989aSArnaldo Carvalho de Melo sched->nr_events = session->evlist->stats.nr_events[0];
194775be989aSArnaldo Carvalho de Melo sched->nr_lost_events = session->evlist->stats.total_lost;
194875be989aSArnaldo Carvalho de Melo sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
1949cee75ac7SArnaldo Carvalho de Melo }
1950d549c769SArnaldo Carvalho de Melo
1951ae536acfSArnaldo Carvalho de Melo rc = 0;
1952a116e05dSArnaldo Carvalho de Melo out_delete:
1953a116e05dSArnaldo Carvalho de Melo perf_session__delete(session);
1954ae536acfSArnaldo Carvalho de Melo return rc;
19550a02ad93SIngo Molnar }
19560a02ad93SIngo Molnar
195749394a2aSDavid Ahern /*
195849394a2aSDavid Ahern * scheduling times are printed as msec.usec
195949394a2aSDavid Ahern */
print_sched_time(unsigned long long nsecs,int width)196049394a2aSDavid Ahern static inline void print_sched_time(unsigned long long nsecs, int width)
196149394a2aSDavid Ahern {
196249394a2aSDavid Ahern unsigned long msecs;
196349394a2aSDavid Ahern unsigned long usecs;
196449394a2aSDavid Ahern
196549394a2aSDavid Ahern msecs = nsecs / NSEC_PER_MSEC;
196649394a2aSDavid Ahern nsecs -= msecs * NSEC_PER_MSEC;
196749394a2aSDavid Ahern usecs = nsecs / NSEC_PER_USEC;
196849394a2aSDavid Ahern printf("%*lu.%03lu ", width, msecs, usecs);
196949394a2aSDavid Ahern }
197049394a2aSDavid Ahern
197149394a2aSDavid Ahern /*
197249394a2aSDavid Ahern * returns runtime data for event, allocating memory for it the
197349394a2aSDavid Ahern * first time it is used.
197449394a2aSDavid Ahern */
evsel__get_runtime(struct evsel * evsel)19753b7313f2SArnaldo Carvalho de Melo static struct evsel_runtime *evsel__get_runtime(struct evsel *evsel)
197649394a2aSDavid Ahern {
197749394a2aSDavid Ahern struct evsel_runtime *r = evsel->priv;
197849394a2aSDavid Ahern
197949394a2aSDavid Ahern if (r == NULL) {
198049394a2aSDavid Ahern r = zalloc(sizeof(struct evsel_runtime));
198149394a2aSDavid Ahern evsel->priv = r;
198249394a2aSDavid Ahern }
198349394a2aSDavid Ahern
198449394a2aSDavid Ahern return r;
198549394a2aSDavid Ahern }
198649394a2aSDavid Ahern
198749394a2aSDavid Ahern /*
198849394a2aSDavid Ahern * save last time event was seen per cpu
198949394a2aSDavid Ahern */
evsel__save_time(struct evsel * evsel,u64 timestamp,u32 cpu)19903b7313f2SArnaldo Carvalho de Melo static void evsel__save_time(struct evsel *evsel, u64 timestamp, u32 cpu)
199149394a2aSDavid Ahern {
19923b7313f2SArnaldo Carvalho de Melo struct evsel_runtime *r = evsel__get_runtime(evsel);
199349394a2aSDavid Ahern
199449394a2aSDavid Ahern if (r == NULL)
199549394a2aSDavid Ahern return;
199649394a2aSDavid Ahern
199749394a2aSDavid Ahern if ((cpu >= r->ncpu) || (r->last_time == NULL)) {
199849394a2aSDavid Ahern int i, n = __roundup_pow_of_two(cpu+1);
199949394a2aSDavid Ahern void *p = r->last_time;
200049394a2aSDavid Ahern
200149394a2aSDavid Ahern p = realloc(r->last_time, n * sizeof(u64));
200249394a2aSDavid Ahern if (!p)
200349394a2aSDavid Ahern return;
200449394a2aSDavid Ahern
200549394a2aSDavid Ahern r->last_time = p;
200649394a2aSDavid Ahern for (i = r->ncpu; i < n; ++i)
200749394a2aSDavid Ahern r->last_time[i] = (u64) 0;
200849394a2aSDavid Ahern
200949394a2aSDavid Ahern r->ncpu = n;
201049394a2aSDavid Ahern }
201149394a2aSDavid Ahern
201249394a2aSDavid Ahern r->last_time[cpu] = timestamp;
201349394a2aSDavid Ahern }
201449394a2aSDavid Ahern
201549394a2aSDavid Ahern /* returns last time this event was seen on the given cpu */
evsel__get_time(struct evsel * evsel,u32 cpu)20163b7313f2SArnaldo Carvalho de Melo static u64 evsel__get_time(struct evsel *evsel, u32 cpu)
201749394a2aSDavid Ahern {
20183b7313f2SArnaldo Carvalho de Melo struct evsel_runtime *r = evsel__get_runtime(evsel);
201949394a2aSDavid Ahern
202049394a2aSDavid Ahern if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu))
202149394a2aSDavid Ahern return 0;
202249394a2aSDavid Ahern
202349394a2aSDavid Ahern return r->last_time[cpu];
202449394a2aSDavid Ahern }
202549394a2aSDavid Ahern
20269b8087d7SNamhyung Kim static int comm_width = 30;
202749394a2aSDavid Ahern
timehist_get_commstr(struct thread * thread)202849394a2aSDavid Ahern static char *timehist_get_commstr(struct thread *thread)
202949394a2aSDavid Ahern {
203049394a2aSDavid Ahern static char str[32];
203149394a2aSDavid Ahern const char *comm = thread__comm_str(thread);
2032ee84a303SIan Rogers pid_t tid = thread__tid(thread);
2033ee84a303SIan Rogers pid_t pid = thread__pid(thread);
203449394a2aSDavid Ahern int n;
203549394a2aSDavid Ahern
203649394a2aSDavid Ahern if (pid == 0)
203749394a2aSDavid Ahern n = scnprintf(str, sizeof(str), "%s", comm);
203849394a2aSDavid Ahern
203949394a2aSDavid Ahern else if (tid != pid)
204049394a2aSDavid Ahern n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid);
204149394a2aSDavid Ahern
204249394a2aSDavid Ahern else
204349394a2aSDavid Ahern n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid);
204449394a2aSDavid Ahern
204549394a2aSDavid Ahern if (n > comm_width)
204649394a2aSDavid Ahern comm_width = n;
204749394a2aSDavid Ahern
204849394a2aSDavid Ahern return str;
204949394a2aSDavid Ahern }
205049394a2aSDavid Ahern
20513fcd7409SYang Jihong /* prio field format: xxx or xxx->yyy */
20523fcd7409SYang Jihong #define MAX_PRIO_STR_LEN 8
timehist_get_priostr(struct evsel * evsel,struct thread * thread,struct perf_sample * sample)20533fcd7409SYang Jihong static char *timehist_get_priostr(struct evsel *evsel,
20543fcd7409SYang Jihong struct thread *thread,
20553fcd7409SYang Jihong struct perf_sample *sample)
20563fcd7409SYang Jihong {
20573fcd7409SYang Jihong static char prio_str[16];
20583fcd7409SYang Jihong int prev_prio = (int)evsel__intval(evsel, sample, "prev_prio");
20593fcd7409SYang Jihong struct thread_runtime *tr = thread__priv(thread);
20603fcd7409SYang Jihong
20613fcd7409SYang Jihong if (tr->prio != prev_prio && tr->prio != -1)
20623fcd7409SYang Jihong scnprintf(prio_str, sizeof(prio_str), "%d->%d", tr->prio, prev_prio);
20633fcd7409SYang Jihong else
20643fcd7409SYang Jihong scnprintf(prio_str, sizeof(prio_str), "%d", prev_prio);
20653fcd7409SYang Jihong
20663fcd7409SYang Jihong return prio_str;
20673fcd7409SYang Jihong }
20683fcd7409SYang Jihong
timehist_header(struct perf_sched * sched)2069a407b067SDavid Ahern static void timehist_header(struct perf_sched *sched)
207049394a2aSDavid Ahern {
20716d18804bSIan Rogers u32 ncpus = sched->max_cpu.cpu + 1;
2072a407b067SDavid Ahern u32 i, j;
2073a407b067SDavid Ahern
207449394a2aSDavid Ahern printf("%15s %6s ", "time", "cpu");
207549394a2aSDavid Ahern
2076a407b067SDavid Ahern if (sched->show_cpu_visual) {
2077a407b067SDavid Ahern printf(" ");
2078a407b067SDavid Ahern for (i = 0, j = 0; i < ncpus; ++i) {
2079a407b067SDavid Ahern printf("%x", j++);
2080a407b067SDavid Ahern if (j > 15)
2081a407b067SDavid Ahern j = 0;
2082a407b067SDavid Ahern }
2083a407b067SDavid Ahern printf(" ");
2084a407b067SDavid Ahern }
2085a407b067SDavid Ahern
20863fcd7409SYang Jihong if (sched->show_prio) {
20873fcd7409SYang Jihong printf(" %-*s %-*s %9s %9s %9s",
20883fcd7409SYang Jihong comm_width, "task name", MAX_PRIO_STR_LEN, "prio",
20893fcd7409SYang Jihong "wait time", "sch delay", "run time");
20903fcd7409SYang Jihong } else {
20910e6758e8SNamhyung Kim printf(" %-*s %9s %9s %9s", comm_width,
209249394a2aSDavid Ahern "task name", "wait time", "sch delay", "run time");
20933fcd7409SYang Jihong }
209449394a2aSDavid Ahern
2095414e050cSNamhyung Kim if (sched->show_state)
2096414e050cSNamhyung Kim printf(" %s", "state");
2097414e050cSNamhyung Kim
209849394a2aSDavid Ahern printf("\n");
209949394a2aSDavid Ahern
210049394a2aSDavid Ahern /*
210149394a2aSDavid Ahern * units row
210249394a2aSDavid Ahern */
210349394a2aSDavid Ahern printf("%15s %-6s ", "", "");
210449394a2aSDavid Ahern
2105a407b067SDavid Ahern if (sched->show_cpu_visual)
2106a407b067SDavid Ahern printf(" %*s ", ncpus, "");
2107a407b067SDavid Ahern
21083fcd7409SYang Jihong if (sched->show_prio) {
21093fcd7409SYang Jihong printf(" %-*s %-*s %9s %9s %9s",
21103fcd7409SYang Jihong comm_width, "[tid/pid]", MAX_PRIO_STR_LEN, "",
21113fcd7409SYang Jihong "(msec)", "(msec)", "(msec)");
21123fcd7409SYang Jihong } else {
2113414e050cSNamhyung Kim printf(" %-*s %9s %9s %9s", comm_width,
21140e6758e8SNamhyung Kim "[tid/pid]", "(msec)", "(msec)", "(msec)");
21153fcd7409SYang Jihong }
211649394a2aSDavid Ahern
2117414e050cSNamhyung Kim if (sched->show_state)
2118414e050cSNamhyung Kim printf(" %5s", "");
2119414e050cSNamhyung Kim
2120414e050cSNamhyung Kim printf("\n");
2121414e050cSNamhyung Kim
212249394a2aSDavid Ahern /*
212349394a2aSDavid Ahern * separator
212449394a2aSDavid Ahern */
212549394a2aSDavid Ahern printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line);
212649394a2aSDavid Ahern
2127a407b067SDavid Ahern if (sched->show_cpu_visual)
2128a407b067SDavid Ahern printf(" %.*s ", ncpus, graph_dotted_line);
2129a407b067SDavid Ahern
21303fcd7409SYang Jihong if (sched->show_prio) {
21313fcd7409SYang Jihong printf(" %.*s %.*s %.9s %.9s %.9s",
21323fcd7409SYang Jihong comm_width, graph_dotted_line, MAX_PRIO_STR_LEN, graph_dotted_line,
21333fcd7409SYang Jihong graph_dotted_line, graph_dotted_line, graph_dotted_line);
21343fcd7409SYang Jihong } else {
21350e6758e8SNamhyung Kim printf(" %.*s %.9s %.9s %.9s", comm_width,
213649394a2aSDavid Ahern graph_dotted_line, graph_dotted_line, graph_dotted_line,
213749394a2aSDavid Ahern graph_dotted_line);
21383fcd7409SYang Jihong }
213949394a2aSDavid Ahern
2140414e050cSNamhyung Kim if (sched->show_state)
2141414e050cSNamhyung Kim printf(" %.5s", graph_dotted_line);
2142414e050cSNamhyung Kim
214349394a2aSDavid Ahern printf("\n");
214449394a2aSDavid Ahern }
214549394a2aSDavid Ahern
timehist_print_sample(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct addr_location * al,struct thread * thread,u64 t,const char state)2146fc1469f1SDavid Ahern static void timehist_print_sample(struct perf_sched *sched,
214732dcd021SJiri Olsa struct evsel *evsel,
2148fc1469f1SDavid Ahern struct perf_sample *sample,
21496c973c90SDavid Ahern struct addr_location *al,
2150853b7407SDavid Ahern struct thread *thread,
215168f87f24SZe Gao u64 t, const char state)
215249394a2aSDavid Ahern {
215349394a2aSDavid Ahern struct thread_runtime *tr = thread__priv(thread);
2154efc0cdc9SArnaldo Carvalho de Melo const char *next_comm = evsel__strval(evsel, sample, "next_comm");
2155efc0cdc9SArnaldo Carvalho de Melo const u32 next_pid = evsel__intval(evsel, sample, "next_pid");
21566d18804bSIan Rogers u32 max_cpus = sched->max_cpu.cpu + 1;
215749394a2aSDavid Ahern char tstr[64];
2158292c4a8fSBrendan Gregg char nstr[30];
2159941bdea7SNamhyung Kim u64 wait_time;
216049394a2aSDavid Ahern
2161c30d630dSDavid Ahern if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
2162c30d630dSDavid Ahern return;
2163c30d630dSDavid Ahern
2164853b7407SDavid Ahern timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
216549394a2aSDavid Ahern printf("%15s [%04d] ", tstr, sample->cpu);
216649394a2aSDavid Ahern
2167a407b067SDavid Ahern if (sched->show_cpu_visual) {
2168a407b067SDavid Ahern u32 i;
2169a407b067SDavid Ahern char c;
2170a407b067SDavid Ahern
2171a407b067SDavid Ahern printf(" ");
2172a407b067SDavid Ahern for (i = 0; i < max_cpus; ++i) {
2173a407b067SDavid Ahern /* flag idle times with 'i'; others are sched events */
2174a407b067SDavid Ahern if (i == sample->cpu)
2175ee84a303SIan Rogers c = (thread__tid(thread) == 0) ? 'i' : 's';
2176a407b067SDavid Ahern else
2177a407b067SDavid Ahern c = ' ';
2178a407b067SDavid Ahern printf("%c", c);
2179a407b067SDavid Ahern }
2180a407b067SDavid Ahern printf(" ");
2181a407b067SDavid Ahern }
2182a407b067SDavid Ahern
218349394a2aSDavid Ahern printf(" %-*s ", comm_width, timehist_get_commstr(thread));
218449394a2aSDavid Ahern
21853fcd7409SYang Jihong if (sched->show_prio)
21863fcd7409SYang Jihong printf(" %-*s ", MAX_PRIO_STR_LEN, timehist_get_priostr(evsel, thread, sample));
21873fcd7409SYang Jihong
2188941bdea7SNamhyung Kim wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt;
2189941bdea7SNamhyung Kim print_sched_time(wait_time, 6);
2190941bdea7SNamhyung Kim
219149394a2aSDavid Ahern print_sched_time(tr->dt_delay, 6);
219249394a2aSDavid Ahern print_sched_time(tr->dt_run, 6);
2193fc1469f1SDavid Ahern
2194414e050cSNamhyung Kim if (sched->show_state)
219568f87f24SZe Gao printf(" %5c ", thread__tid(thread) == 0 ? 'I' : state);
2196414e050cSNamhyung Kim
2197292c4a8fSBrendan Gregg if (sched->show_next) {
2198292c4a8fSBrendan Gregg snprintf(nstr, sizeof(nstr), "next: %s[%d]", next_comm, next_pid);
2199292c4a8fSBrendan Gregg printf(" %-*s", comm_width, nstr);
2200292c4a8fSBrendan Gregg }
2201292c4a8fSBrendan Gregg
2202292c4a8fSBrendan Gregg if (sched->show_wakeups && !sched->show_next)
2203fc1469f1SDavid Ahern printf(" %-*s", comm_width, "");
2204fc1469f1SDavid Ahern
2205ee84a303SIan Rogers if (thread__tid(thread) == 0)
22066c973c90SDavid Ahern goto out;
22076c973c90SDavid Ahern
22086c973c90SDavid Ahern if (sched->show_callchain)
22096c973c90SDavid Ahern printf(" ");
22106c973c90SDavid Ahern
22116c973c90SDavid Ahern sample__fprintf_sym(sample, al, 0,
22126c973c90SDavid Ahern EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
22132d9bbf6eSNamhyung Kim EVSEL__PRINT_CALLCHAIN_ARROW |
22142d9bbf6eSNamhyung Kim EVSEL__PRINT_SKIP_IGNORED,
22158ab12a20SIan Rogers get_tls_callchain_cursor(), symbol_conf.bt_stop_list, stdout);
22166c973c90SDavid Ahern
22176c973c90SDavid Ahern out:
221849394a2aSDavid Ahern printf("\n");
221949394a2aSDavid Ahern }
222049394a2aSDavid Ahern
222149394a2aSDavid Ahern /*
222249394a2aSDavid Ahern * Explanation of delta-time stats:
222349394a2aSDavid Ahern *
222449394a2aSDavid Ahern * t = time of current schedule out event
222549394a2aSDavid Ahern * tprev = time of previous sched out event
222649394a2aSDavid Ahern * also time of schedule-in event for current task
222749394a2aSDavid Ahern * last_time = time of last sched change event for current task
222849394a2aSDavid Ahern * (i.e, time process was last scheduled out)
222949394a2aSDavid Ahern * ready_to_run = time of wakeup for current task
223049394a2aSDavid Ahern *
223149394a2aSDavid Ahern * -----|------------|------------|------------|------
223249394a2aSDavid Ahern * last ready tprev t
223349394a2aSDavid Ahern * time to run
223449394a2aSDavid Ahern *
223549394a2aSDavid Ahern * |-------- dt_wait --------|
223649394a2aSDavid Ahern * |- dt_delay -|-- dt_run --|
223749394a2aSDavid Ahern *
223849394a2aSDavid Ahern * dt_run = run time of current task
223949394a2aSDavid Ahern * dt_wait = time between last schedule out event for task and tprev
224049394a2aSDavid Ahern * represents time spent off the cpu
224149394a2aSDavid Ahern * dt_delay = time between wakeup and schedule-in of task
224249394a2aSDavid Ahern */
224349394a2aSDavid Ahern
timehist_update_runtime_stats(struct thread_runtime * r,u64 t,u64 tprev)224449394a2aSDavid Ahern static void timehist_update_runtime_stats(struct thread_runtime *r,
224549394a2aSDavid Ahern u64 t, u64 tprev)
224649394a2aSDavid Ahern {
224749394a2aSDavid Ahern r->dt_delay = 0;
2248941bdea7SNamhyung Kim r->dt_sleep = 0;
2249941bdea7SNamhyung Kim r->dt_iowait = 0;
2250941bdea7SNamhyung Kim r->dt_preempt = 0;
225149394a2aSDavid Ahern r->dt_run = 0;
2252941bdea7SNamhyung Kim
225349394a2aSDavid Ahern if (tprev) {
225449394a2aSDavid Ahern r->dt_run = t - tprev;
225549394a2aSDavid Ahern if (r->ready_to_run) {
225649394a2aSDavid Ahern if (r->ready_to_run > tprev)
225749394a2aSDavid Ahern pr_debug("time travel: wakeup time for task > previous sched_switch event\n");
225849394a2aSDavid Ahern else
225949394a2aSDavid Ahern r->dt_delay = tprev - r->ready_to_run;
226049394a2aSDavid Ahern }
226149394a2aSDavid Ahern
226249394a2aSDavid Ahern if (r->last_time > tprev)
226349394a2aSDavid Ahern pr_debug("time travel: last sched out time for task > previous sched_switch event\n");
2264941bdea7SNamhyung Kim else if (r->last_time) {
2265941bdea7SNamhyung Kim u64 dt_wait = tprev - r->last_time;
2266941bdea7SNamhyung Kim
226768f87f24SZe Gao if (r->last_state == 'R')
2268941bdea7SNamhyung Kim r->dt_preempt = dt_wait;
226968f87f24SZe Gao else if (r->last_state == 'D')
2270941bdea7SNamhyung Kim r->dt_iowait = dt_wait;
2271941bdea7SNamhyung Kim else
2272941bdea7SNamhyung Kim r->dt_sleep = dt_wait;
2273941bdea7SNamhyung Kim }
227449394a2aSDavid Ahern }
227549394a2aSDavid Ahern
227649394a2aSDavid Ahern update_stats(&r->run_stats, r->dt_run);
2277587782c5SNamhyung Kim
227849394a2aSDavid Ahern r->total_run_time += r->dt_run;
2279587782c5SNamhyung Kim r->total_delay_time += r->dt_delay;
2280587782c5SNamhyung Kim r->total_sleep_time += r->dt_sleep;
2281587782c5SNamhyung Kim r->total_iowait_time += r->dt_iowait;
2282587782c5SNamhyung Kim r->total_preempt_time += r->dt_preempt;
228349394a2aSDavid Ahern }
228449394a2aSDavid Ahern
is_idle_sample(struct perf_sample * sample,struct evsel * evsel)228596039c7cSNamhyung Kim static bool is_idle_sample(struct perf_sample *sample,
228632dcd021SJiri Olsa struct evsel *evsel)
228796039c7cSNamhyung Kim {
228896039c7cSNamhyung Kim /* pid 0 == swapper == idle task */
228909d2056eSYang Jihong if (evsel__name_is(evsel, "sched:sched_switch"))
2290efc0cdc9SArnaldo Carvalho de Melo return evsel__intval(evsel, sample, "prev_pid") == 0;
229196039c7cSNamhyung Kim
229296039c7cSNamhyung Kim return sample->pid == 0;
229396039c7cSNamhyung Kim }
229496039c7cSNamhyung Kim
save_task_callchain(struct perf_sched * sched,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)229596039c7cSNamhyung Kim static void save_task_callchain(struct perf_sched *sched,
22966c973c90SDavid Ahern struct perf_sample *sample,
229732dcd021SJiri Olsa struct evsel *evsel,
22986c973c90SDavid Ahern struct machine *machine)
229949394a2aSDavid Ahern {
23008ab12a20SIan Rogers struct callchain_cursor *cursor;
230196039c7cSNamhyung Kim struct thread *thread;
23026c973c90SDavid Ahern
23036c973c90SDavid Ahern /* want main thread for process - has maps */
23046c973c90SDavid Ahern thread = machine__findnew_thread(machine, sample->pid, sample->pid);
23056c973c90SDavid Ahern if (thread == NULL) {
23066c973c90SDavid Ahern pr_debug("Failed to get thread for pid %d.\n", sample->pid);
230796039c7cSNamhyung Kim return;
23086c973c90SDavid Ahern }
23096c973c90SDavid Ahern
23104c50563dSArnaldo Carvalho de Melo if (!sched->show_callchain || sample->callchain == NULL)
231196039c7cSNamhyung Kim return;
23126c973c90SDavid Ahern
23138ab12a20SIan Rogers cursor = get_tls_callchain_cursor();
23148ab12a20SIan Rogers
23156c973c90SDavid Ahern if (thread__resolve_callchain(thread, cursor, evsel, sample,
23168388deb3SNamhyung Kim NULL, NULL, sched->max_stack + 2) != 0) {
2317bb963e16SNamhyung Kim if (verbose > 0)
231862d94b00SArnaldo Carvalho de Melo pr_err("Failed to resolve callchain. Skipping\n");
23196c973c90SDavid Ahern
232096039c7cSNamhyung Kim return;
23216c973c90SDavid Ahern }
2322cdeb01bfSNamhyung Kim
23236c973c90SDavid Ahern callchain_cursor_commit(cursor);
2324cdeb01bfSNamhyung Kim
2325cdeb01bfSNamhyung Kim while (true) {
2326cdeb01bfSNamhyung Kim struct callchain_cursor_node *node;
2327cdeb01bfSNamhyung Kim struct symbol *sym;
2328cdeb01bfSNamhyung Kim
2329cdeb01bfSNamhyung Kim node = callchain_cursor_current(cursor);
2330cdeb01bfSNamhyung Kim if (node == NULL)
2331cdeb01bfSNamhyung Kim break;
2332cdeb01bfSNamhyung Kim
23335f0fef8aSArnaldo Carvalho de Melo sym = node->ms.sym;
2334a7c3899cSArnaldo Carvalho de Melo if (sym) {
2335cdeb01bfSNamhyung Kim if (!strcmp(sym->name, "schedule") ||
2336cdeb01bfSNamhyung Kim !strcmp(sym->name, "__schedule") ||
2337cdeb01bfSNamhyung Kim !strcmp(sym->name, "preempt_schedule"))
2338cdeb01bfSNamhyung Kim sym->ignore = 1;
2339cdeb01bfSNamhyung Kim }
2340cdeb01bfSNamhyung Kim
2341cdeb01bfSNamhyung Kim callchain_cursor_advance(cursor);
2342cdeb01bfSNamhyung Kim }
234349394a2aSDavid Ahern }
234449394a2aSDavid Ahern
init_idle_thread(struct thread * thread)23453bc2fa9cSNamhyung Kim static int init_idle_thread(struct thread *thread)
23463bc2fa9cSNamhyung Kim {
23473bc2fa9cSNamhyung Kim struct idle_thread_runtime *itr;
23483bc2fa9cSNamhyung Kim
23493bc2fa9cSNamhyung Kim thread__set_comm(thread, idle_comm, 0);
23503bc2fa9cSNamhyung Kim
23513bc2fa9cSNamhyung Kim itr = zalloc(sizeof(*itr));
23523bc2fa9cSNamhyung Kim if (itr == NULL)
23533bc2fa9cSNamhyung Kim return -ENOMEM;
23543bc2fa9cSNamhyung Kim
23553fcd7409SYang Jihong init_prio(&itr->tr);
23563bc2fa9cSNamhyung Kim init_stats(&itr->tr.run_stats);
23573bc2fa9cSNamhyung Kim callchain_init(&itr->callchain);
23583bc2fa9cSNamhyung Kim callchain_cursor_reset(&itr->cursor);
23593bc2fa9cSNamhyung Kim thread__set_priv(thread, itr);
23603bc2fa9cSNamhyung Kim
23613bc2fa9cSNamhyung Kim return 0;
23623bc2fa9cSNamhyung Kim }
23633bc2fa9cSNamhyung Kim
236449394a2aSDavid Ahern /*
236549394a2aSDavid Ahern * Track idle stats per cpu by maintaining a local thread
236649394a2aSDavid Ahern * struct for the idle task on each cpu.
236749394a2aSDavid Ahern */
init_idle_threads(int ncpu)236849394a2aSDavid Ahern static int init_idle_threads(int ncpu)
236949394a2aSDavid Ahern {
23703bc2fa9cSNamhyung Kim int i, ret;
237149394a2aSDavid Ahern
237249394a2aSDavid Ahern idle_threads = zalloc(ncpu * sizeof(struct thread *));
237349394a2aSDavid Ahern if (!idle_threads)
237449394a2aSDavid Ahern return -ENOMEM;
237549394a2aSDavid Ahern
2376b336352bSNamhyung Kim idle_max_cpu = ncpu;
237749394a2aSDavid Ahern
237849394a2aSDavid Ahern /* allocate the actual thread struct if needed */
237949394a2aSDavid Ahern for (i = 0; i < ncpu; ++i) {
238049394a2aSDavid Ahern idle_threads[i] = thread__new(0, 0);
238149394a2aSDavid Ahern if (idle_threads[i] == NULL)
238249394a2aSDavid Ahern return -ENOMEM;
238349394a2aSDavid Ahern
23843bc2fa9cSNamhyung Kim ret = init_idle_thread(idle_threads[i]);
23853bc2fa9cSNamhyung Kim if (ret < 0)
23863bc2fa9cSNamhyung Kim return ret;
238749394a2aSDavid Ahern }
238849394a2aSDavid Ahern
238949394a2aSDavid Ahern return 0;
239049394a2aSDavid Ahern }
239149394a2aSDavid Ahern
free_idle_threads(void)239249394a2aSDavid Ahern static void free_idle_threads(void)
239349394a2aSDavid Ahern {
239449394a2aSDavid Ahern int i;
239549394a2aSDavid Ahern
239649394a2aSDavid Ahern if (idle_threads == NULL)
239749394a2aSDavid Ahern return;
239849394a2aSDavid Ahern
2399b336352bSNamhyung Kim for (i = 0; i < idle_max_cpu; ++i) {
240049394a2aSDavid Ahern if ((idle_threads[i]))
240149394a2aSDavid Ahern thread__delete(idle_threads[i]);
240249394a2aSDavid Ahern }
240349394a2aSDavid Ahern
240449394a2aSDavid Ahern free(idle_threads);
240549394a2aSDavid Ahern }
240649394a2aSDavid Ahern
get_idle_thread(int cpu)240749394a2aSDavid Ahern static struct thread *get_idle_thread(int cpu)
240849394a2aSDavid Ahern {
240949394a2aSDavid Ahern /*
241049394a2aSDavid Ahern * expand/allocate array of pointers to local thread
241149394a2aSDavid Ahern * structs if needed
241249394a2aSDavid Ahern */
241349394a2aSDavid Ahern if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) {
241449394a2aSDavid Ahern int i, j = __roundup_pow_of_two(cpu+1);
241549394a2aSDavid Ahern void *p;
241649394a2aSDavid Ahern
241749394a2aSDavid Ahern p = realloc(idle_threads, j * sizeof(struct thread *));
241849394a2aSDavid Ahern if (!p)
241949394a2aSDavid Ahern return NULL;
242049394a2aSDavid Ahern
242149394a2aSDavid Ahern idle_threads = (struct thread **) p;
2422b336352bSNamhyung Kim for (i = idle_max_cpu; i < j; ++i)
242349394a2aSDavid Ahern idle_threads[i] = NULL;
242449394a2aSDavid Ahern
242549394a2aSDavid Ahern idle_max_cpu = j;
242649394a2aSDavid Ahern }
242749394a2aSDavid Ahern
242849394a2aSDavid Ahern /* allocate a new thread struct if needed */
242949394a2aSDavid Ahern if (idle_threads[cpu] == NULL) {
243049394a2aSDavid Ahern idle_threads[cpu] = thread__new(0, 0);
243149394a2aSDavid Ahern
243249394a2aSDavid Ahern if (idle_threads[cpu]) {
24333bc2fa9cSNamhyung Kim if (init_idle_thread(idle_threads[cpu]) < 0)
24343bc2fa9cSNamhyung Kim return NULL;
243549394a2aSDavid Ahern }
243649394a2aSDavid Ahern }
243749394a2aSDavid Ahern
243849394a2aSDavid Ahern return idle_threads[cpu];
243949394a2aSDavid Ahern }
244049394a2aSDavid Ahern
save_idle_callchain(struct perf_sched * sched,struct idle_thread_runtime * itr,struct perf_sample * sample)24414c50563dSArnaldo Carvalho de Melo static void save_idle_callchain(struct perf_sched *sched,
24424c50563dSArnaldo Carvalho de Melo struct idle_thread_runtime *itr,
2443699b5b92SNamhyung Kim struct perf_sample *sample)
2444699b5b92SNamhyung Kim {
24458ab12a20SIan Rogers struct callchain_cursor *cursor;
24468ab12a20SIan Rogers
24474c50563dSArnaldo Carvalho de Melo if (!sched->show_callchain || sample->callchain == NULL)
2448699b5b92SNamhyung Kim return;
2449699b5b92SNamhyung Kim
24508ab12a20SIan Rogers cursor = get_tls_callchain_cursor();
24518ab12a20SIan Rogers if (cursor == NULL)
24528ab12a20SIan Rogers return;
24538ab12a20SIan Rogers
24548ab12a20SIan Rogers callchain_cursor__copy(&itr->cursor, cursor);
2455699b5b92SNamhyung Kim }
2456699b5b92SNamhyung Kim
timehist_get_thread(struct perf_sched * sched,struct perf_sample * sample,struct machine * machine,struct evsel * evsel)24576c973c90SDavid Ahern static struct thread *timehist_get_thread(struct perf_sched *sched,
24586c973c90SDavid Ahern struct perf_sample *sample,
245949394a2aSDavid Ahern struct machine *machine,
246032dcd021SJiri Olsa struct evsel *evsel)
246149394a2aSDavid Ahern {
246249394a2aSDavid Ahern struct thread *thread;
246349394a2aSDavid Ahern
246496039c7cSNamhyung Kim if (is_idle_sample(sample, evsel)) {
246549394a2aSDavid Ahern thread = get_idle_thread(sample->cpu);
246649394a2aSDavid Ahern if (thread == NULL)
246749394a2aSDavid Ahern pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
246849394a2aSDavid Ahern
246949394a2aSDavid Ahern } else {
24705d92d96aSNamhyung Kim /* there were samples with tid 0 but non-zero pid */
24715d92d96aSNamhyung Kim thread = machine__findnew_thread(machine, sample->pid,
24725d92d96aSNamhyung Kim sample->tid ?: sample->pid);
247349394a2aSDavid Ahern if (thread == NULL) {
247449394a2aSDavid Ahern pr_debug("Failed to get thread for tid %d. skipping sample.\n",
247549394a2aSDavid Ahern sample->tid);
247649394a2aSDavid Ahern }
247796039c7cSNamhyung Kim
247896039c7cSNamhyung Kim save_task_callchain(sched, sample, evsel, machine);
2479699b5b92SNamhyung Kim if (sched->idle_hist) {
2480699b5b92SNamhyung Kim struct thread *idle;
2481699b5b92SNamhyung Kim struct idle_thread_runtime *itr;
2482699b5b92SNamhyung Kim
2483699b5b92SNamhyung Kim idle = get_idle_thread(sample->cpu);
2484699b5b92SNamhyung Kim if (idle == NULL) {
2485699b5b92SNamhyung Kim pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu);
2486699b5b92SNamhyung Kim return NULL;
2487699b5b92SNamhyung Kim }
2488699b5b92SNamhyung Kim
2489699b5b92SNamhyung Kim itr = thread__priv(idle);
2490699b5b92SNamhyung Kim if (itr == NULL)
2491699b5b92SNamhyung Kim return NULL;
2492699b5b92SNamhyung Kim
2493699b5b92SNamhyung Kim itr->last_thread = thread;
2494699b5b92SNamhyung Kim
2495699b5b92SNamhyung Kim /* copy task callchain when entering to idle */
2496efc0cdc9SArnaldo Carvalho de Melo if (evsel__intval(evsel, sample, "next_pid") == 0)
24974c50563dSArnaldo Carvalho de Melo save_idle_callchain(sched, itr, sample);
2498699b5b92SNamhyung Kim }
249949394a2aSDavid Ahern }
250049394a2aSDavid Ahern
250149394a2aSDavid Ahern return thread;
250249394a2aSDavid Ahern }
250349394a2aSDavid Ahern
timehist_skip_sample(struct perf_sched * sched,struct thread * thread,struct evsel * evsel,struct perf_sample * sample)250452df138cSDavid Ahern static bool timehist_skip_sample(struct perf_sched *sched,
2505a4b2b6f5SNamhyung Kim struct thread *thread,
250632dcd021SJiri Olsa struct evsel *evsel,
2507a4b2b6f5SNamhyung Kim struct perf_sample *sample)
250849394a2aSDavid Ahern {
250949394a2aSDavid Ahern bool rc = false;
25109b3a48bbSYang Jihong int prio = -1;
25119b3a48bbSYang Jihong struct thread_runtime *tr = NULL;
251249394a2aSDavid Ahern
251352df138cSDavid Ahern if (thread__is_filtered(thread)) {
251449394a2aSDavid Ahern rc = true;
251552df138cSDavid Ahern sched->skipped_samples++;
251652df138cSDavid Ahern }
251749394a2aSDavid Ahern
25189b3a48bbSYang Jihong if (sched->prio_str) {
25199b3a48bbSYang Jihong /*
25209b3a48bbSYang Jihong * Because priority may be changed during task execution,
25219b3a48bbSYang Jihong * first read priority from prev sched_in event for current task.
25229b3a48bbSYang Jihong * If prev sched_in event is not saved, then read priority from
25239b3a48bbSYang Jihong * current task sched_out event.
25249b3a48bbSYang Jihong */
25259b3a48bbSYang Jihong tr = thread__get_runtime(thread);
25269b3a48bbSYang Jihong if (tr && tr->prio != -1)
25279b3a48bbSYang Jihong prio = tr->prio;
25289b3a48bbSYang Jihong else if (evsel__name_is(evsel, "sched:sched_switch"))
25299b3a48bbSYang Jihong prio = evsel__intval(evsel, sample, "prev_prio");
25309b3a48bbSYang Jihong
25319b3a48bbSYang Jihong if (prio != -1 && !test_bit(prio, sched->prio_bitmap)) {
25329b3a48bbSYang Jihong rc = true;
25339b3a48bbSYang Jihong sched->skipped_samples++;
25349b3a48bbSYang Jihong }
25359b3a48bbSYang Jihong }
25369b3a48bbSYang Jihong
2537a4b2b6f5SNamhyung Kim if (sched->idle_hist) {
253809d2056eSYang Jihong if (!evsel__name_is(evsel, "sched:sched_switch"))
2539a4b2b6f5SNamhyung Kim rc = true;
2540efc0cdc9SArnaldo Carvalho de Melo else if (evsel__intval(evsel, sample, "prev_pid") != 0 &&
2541efc0cdc9SArnaldo Carvalho de Melo evsel__intval(evsel, sample, "next_pid") != 0)
2542a4b2b6f5SNamhyung Kim rc = true;
2543a4b2b6f5SNamhyung Kim }
2544a4b2b6f5SNamhyung Kim
254549394a2aSDavid Ahern return rc;
254649394a2aSDavid Ahern }
254749394a2aSDavid Ahern
timehist_print_wakeup_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine,struct thread * awakened)2548fc1469f1SDavid Ahern static void timehist_print_wakeup_event(struct perf_sched *sched,
254932dcd021SJiri Olsa struct evsel *evsel,
2550fc1469f1SDavid Ahern struct perf_sample *sample,
2551fc1469f1SDavid Ahern struct machine *machine,
2552fc1469f1SDavid Ahern struct thread *awakened)
2553fc1469f1SDavid Ahern {
2554fc1469f1SDavid Ahern struct thread *thread;
2555fc1469f1SDavid Ahern char tstr[64];
2556fc1469f1SDavid Ahern
2557fc1469f1SDavid Ahern thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2558fc1469f1SDavid Ahern if (thread == NULL)
2559fc1469f1SDavid Ahern return;
2560fc1469f1SDavid Ahern
2561fc1469f1SDavid Ahern /* show wakeup unless both awakee and awaker are filtered */
2562a4b2b6f5SNamhyung Kim if (timehist_skip_sample(sched, thread, evsel, sample) &&
2563a4b2b6f5SNamhyung Kim timehist_skip_sample(sched, awakened, evsel, sample)) {
2564fc1469f1SDavid Ahern return;
2565fc1469f1SDavid Ahern }
2566fc1469f1SDavid Ahern
2567fc1469f1SDavid Ahern timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2568fc1469f1SDavid Ahern printf("%15s [%04d] ", tstr, sample->cpu);
2569a407b067SDavid Ahern if (sched->show_cpu_visual)
25706d18804bSIan Rogers printf(" %*s ", sched->max_cpu.cpu + 1, "");
2571fc1469f1SDavid Ahern
2572fc1469f1SDavid Ahern printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2573fc1469f1SDavid Ahern
2574fc1469f1SDavid Ahern /* dt spacer */
2575fc1469f1SDavid Ahern printf(" %9s %9s %9s ", "", "", "");
2576fc1469f1SDavid Ahern
2577fc1469f1SDavid Ahern printf("awakened: %s", timehist_get_commstr(awakened));
2578fc1469f1SDavid Ahern
2579fc1469f1SDavid Ahern printf("\n");
2580fc1469f1SDavid Ahern }
2581fc1469f1SDavid Ahern
timehist_sched_wakeup_ignore(const 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)258230f29baeSIan Rogers static int timehist_sched_wakeup_ignore(const struct perf_tool *tool __maybe_unused,
2583d566a9c2SDavid Ahern union perf_event *event __maybe_unused,
2584d566a9c2SDavid Ahern struct evsel *evsel __maybe_unused,
2585d566a9c2SDavid Ahern struct perf_sample *sample __maybe_unused,
2586d566a9c2SDavid Ahern struct machine *machine __maybe_unused)
2587d566a9c2SDavid Ahern {
2588d566a9c2SDavid Ahern return 0;
2589d566a9c2SDavid Ahern }
2590d566a9c2SDavid Ahern
timehist_sched_wakeup_event(const struct perf_tool * tool,union perf_event * event __maybe_unused,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)259130f29baeSIan Rogers static int timehist_sched_wakeup_event(const struct perf_tool *tool,
259249394a2aSDavid Ahern union perf_event *event __maybe_unused,
259332dcd021SJiri Olsa struct evsel *evsel,
259449394a2aSDavid Ahern struct perf_sample *sample,
259549394a2aSDavid Ahern struct machine *machine)
259649394a2aSDavid Ahern {
2597fc1469f1SDavid Ahern struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
259849394a2aSDavid Ahern struct thread *thread;
259949394a2aSDavid Ahern struct thread_runtime *tr = NULL;
260049394a2aSDavid Ahern /* want pid of awakened task not pid in sample */
2601efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
260249394a2aSDavid Ahern
260349394a2aSDavid Ahern thread = machine__findnew_thread(machine, 0, pid);
260449394a2aSDavid Ahern if (thread == NULL)
260549394a2aSDavid Ahern return -1;
260649394a2aSDavid Ahern
260749394a2aSDavid Ahern tr = thread__get_runtime(thread);
260849394a2aSDavid Ahern if (tr == NULL)
260949394a2aSDavid Ahern return -1;
261049394a2aSDavid Ahern
261149394a2aSDavid Ahern if (tr->ready_to_run == 0)
261249394a2aSDavid Ahern tr->ready_to_run = sample->time;
261349394a2aSDavid Ahern
2614fc1469f1SDavid Ahern /* show wakeups if requested */
2615853b7407SDavid Ahern if (sched->show_wakeups &&
2616853b7407SDavid Ahern !perf_time__skip_sample(&sched->ptime, sample->time))
2617a4b2b6f5SNamhyung Kim timehist_print_wakeup_event(sched, evsel, sample, machine, thread);
2618fc1469f1SDavid Ahern
261949394a2aSDavid Ahern return 0;
262049394a2aSDavid Ahern }
262149394a2aSDavid Ahern
timehist_print_migration_event(struct perf_sched * sched,struct evsel * evsel,struct perf_sample * sample,struct machine * machine,struct thread * migrated)2622350f54faSDavid Ahern static void timehist_print_migration_event(struct perf_sched *sched,
262332dcd021SJiri Olsa struct evsel *evsel,
2624350f54faSDavid Ahern struct perf_sample *sample,
2625350f54faSDavid Ahern struct machine *machine,
2626350f54faSDavid Ahern struct thread *migrated)
2627350f54faSDavid Ahern {
2628350f54faSDavid Ahern struct thread *thread;
2629350f54faSDavid Ahern char tstr[64];
26306d18804bSIan Rogers u32 max_cpus;
2631350f54faSDavid Ahern u32 ocpu, dcpu;
2632350f54faSDavid Ahern
2633350f54faSDavid Ahern if (sched->summary_only)
2634350f54faSDavid Ahern return;
2635350f54faSDavid Ahern
26366d18804bSIan Rogers max_cpus = sched->max_cpu.cpu + 1;
2637efc0cdc9SArnaldo Carvalho de Melo ocpu = evsel__intval(evsel, sample, "orig_cpu");
2638efc0cdc9SArnaldo Carvalho de Melo dcpu = evsel__intval(evsel, sample, "dest_cpu");
2639350f54faSDavid Ahern
2640350f54faSDavid Ahern thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2641350f54faSDavid Ahern if (thread == NULL)
2642350f54faSDavid Ahern return;
2643350f54faSDavid Ahern
2644a4b2b6f5SNamhyung Kim if (timehist_skip_sample(sched, thread, evsel, sample) &&
2645a4b2b6f5SNamhyung Kim timehist_skip_sample(sched, migrated, evsel, sample)) {
2646350f54faSDavid Ahern return;
2647350f54faSDavid Ahern }
2648350f54faSDavid Ahern
2649350f54faSDavid Ahern timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
2650350f54faSDavid Ahern printf("%15s [%04d] ", tstr, sample->cpu);
2651350f54faSDavid Ahern
2652350f54faSDavid Ahern if (sched->show_cpu_visual) {
2653350f54faSDavid Ahern u32 i;
2654350f54faSDavid Ahern char c;
2655350f54faSDavid Ahern
2656350f54faSDavid Ahern printf(" ");
2657350f54faSDavid Ahern for (i = 0; i < max_cpus; ++i) {
2658350f54faSDavid Ahern c = (i == sample->cpu) ? 'm' : ' ';
2659350f54faSDavid Ahern printf("%c", c);
2660350f54faSDavid Ahern }
2661350f54faSDavid Ahern printf(" ");
2662350f54faSDavid Ahern }
2663350f54faSDavid Ahern
2664350f54faSDavid Ahern printf(" %-*s ", comm_width, timehist_get_commstr(thread));
2665350f54faSDavid Ahern
2666350f54faSDavid Ahern /* dt spacer */
2667350f54faSDavid Ahern printf(" %9s %9s %9s ", "", "", "");
2668350f54faSDavid Ahern
2669350f54faSDavid Ahern printf("migrated: %s", timehist_get_commstr(migrated));
2670350f54faSDavid Ahern printf(" cpu %d => %d", ocpu, dcpu);
2671350f54faSDavid Ahern
2672350f54faSDavid Ahern printf("\n");
2673350f54faSDavid Ahern }
2674350f54faSDavid Ahern
timehist_migrate_task_event(const struct perf_tool * tool,union perf_event * event __maybe_unused,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)267530f29baeSIan Rogers static int timehist_migrate_task_event(const struct perf_tool *tool,
2676350f54faSDavid Ahern union perf_event *event __maybe_unused,
267732dcd021SJiri Olsa struct evsel *evsel,
2678350f54faSDavid Ahern struct perf_sample *sample,
2679350f54faSDavid Ahern struct machine *machine)
2680350f54faSDavid Ahern {
2681350f54faSDavid Ahern struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2682350f54faSDavid Ahern struct thread *thread;
2683350f54faSDavid Ahern struct thread_runtime *tr = NULL;
2684350f54faSDavid Ahern /* want pid of migrated task not pid in sample */
2685efc0cdc9SArnaldo Carvalho de Melo const u32 pid = evsel__intval(evsel, sample, "pid");
2686350f54faSDavid Ahern
2687350f54faSDavid Ahern thread = machine__findnew_thread(machine, 0, pid);
2688350f54faSDavid Ahern if (thread == NULL)
2689350f54faSDavid Ahern return -1;
2690350f54faSDavid Ahern
2691350f54faSDavid Ahern tr = thread__get_runtime(thread);
2692350f54faSDavid Ahern if (tr == NULL)
2693350f54faSDavid Ahern return -1;
2694350f54faSDavid Ahern
2695350f54faSDavid Ahern tr->migrations++;
2696350f54faSDavid Ahern
2697350f54faSDavid Ahern /* show migrations if requested */
2698350f54faSDavid Ahern timehist_print_migration_event(sched, evsel, sample, machine, thread);
2699350f54faSDavid Ahern
2700350f54faSDavid Ahern return 0;
2701350f54faSDavid Ahern }
2702350f54faSDavid Ahern
timehist_update_task_prio(struct evsel * evsel,struct perf_sample * sample,struct machine * machine)27033fcd7409SYang Jihong static void timehist_update_task_prio(struct evsel *evsel,
27043fcd7409SYang Jihong struct perf_sample *sample,
27053fcd7409SYang Jihong struct machine *machine)
27063fcd7409SYang Jihong {
27073fcd7409SYang Jihong struct thread *thread;
27083fcd7409SYang Jihong struct thread_runtime *tr = NULL;
27093fcd7409SYang Jihong const u32 next_pid = evsel__intval(evsel, sample, "next_pid");
27103fcd7409SYang Jihong const u32 next_prio = evsel__intval(evsel, sample, "next_prio");
27113fcd7409SYang Jihong
27123fcd7409SYang Jihong if (next_pid == 0)
27133fcd7409SYang Jihong thread = get_idle_thread(sample->cpu);
27143fcd7409SYang Jihong else
27153fcd7409SYang Jihong thread = machine__findnew_thread(machine, -1, next_pid);
27163fcd7409SYang Jihong
27173fcd7409SYang Jihong if (thread == NULL)
27183fcd7409SYang Jihong return;
27193fcd7409SYang Jihong
27203fcd7409SYang Jihong tr = thread__get_runtime(thread);
27213fcd7409SYang Jihong if (tr == NULL)
27223fcd7409SYang Jihong return;
27233fcd7409SYang Jihong
27243fcd7409SYang Jihong tr->prio = next_prio;
27253fcd7409SYang Jihong }
27263fcd7409SYang Jihong
timehist_sched_change_event(const struct perf_tool * tool,union perf_event * event,struct evsel * evsel,struct perf_sample * sample,struct machine * machine)272730f29baeSIan Rogers static int timehist_sched_change_event(const struct perf_tool *tool,
272849394a2aSDavid Ahern union perf_event *event,
272932dcd021SJiri Olsa struct evsel *evsel,
273049394a2aSDavid Ahern struct perf_sample *sample,
273149394a2aSDavid Ahern struct machine *machine)
273249394a2aSDavid Ahern {
2733fc1469f1SDavid Ahern struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
2734853b7407SDavid Ahern struct perf_time_interval *ptime = &sched->ptime;
273549394a2aSDavid Ahern struct addr_location al;
273649394a2aSDavid Ahern struct thread *thread;
273749394a2aSDavid Ahern struct thread_runtime *tr = NULL;
2738853b7407SDavid Ahern u64 tprev, t = sample->time;
273949394a2aSDavid Ahern int rc = 0;
274068f87f24SZe Gao const char state = evsel__taskstate(evsel, sample, "prev_state");
274149394a2aSDavid Ahern
27420dd5041cSIan Rogers addr_location__init(&al);
274349394a2aSDavid Ahern if (machine__resolve(machine, &al, sample) < 0) {
274449394a2aSDavid Ahern pr_err("problem processing %d event. skipping it\n",
274549394a2aSDavid Ahern event->header.type);
274649394a2aSDavid Ahern rc = -1;
274749394a2aSDavid Ahern goto out;
274849394a2aSDavid Ahern }
274949394a2aSDavid Ahern
27509b3a48bbSYang Jihong if (sched->show_prio || sched->prio_str)
27513fcd7409SYang Jihong timehist_update_task_prio(evsel, sample, machine);
27523fcd7409SYang Jihong
27536c973c90SDavid Ahern thread = timehist_get_thread(sched, sample, machine, evsel);
275449394a2aSDavid Ahern if (thread == NULL) {
275549394a2aSDavid Ahern rc = -1;
275649394a2aSDavid Ahern goto out;
275749394a2aSDavid Ahern }
275849394a2aSDavid Ahern
2759a4b2b6f5SNamhyung Kim if (timehist_skip_sample(sched, thread, evsel, sample))
276049394a2aSDavid Ahern goto out;
276149394a2aSDavid Ahern
276249394a2aSDavid Ahern tr = thread__get_runtime(thread);
276349394a2aSDavid Ahern if (tr == NULL) {
276449394a2aSDavid Ahern rc = -1;
276549394a2aSDavid Ahern goto out;
276649394a2aSDavid Ahern }
276749394a2aSDavid Ahern
27683b7313f2SArnaldo Carvalho de Melo tprev = evsel__get_time(evsel, sample->cpu);
276949394a2aSDavid Ahern
2770853b7407SDavid Ahern /*
2771853b7407SDavid Ahern * If start time given:
2772853b7407SDavid Ahern * - sample time is under window user cares about - skip sample
2773853b7407SDavid Ahern * - tprev is under window user cares about - reset to start of window
2774853b7407SDavid Ahern */
2775853b7407SDavid Ahern if (ptime->start && ptime->start > t)
2776853b7407SDavid Ahern goto out;
2777853b7407SDavid Ahern
2778bdd75729SNamhyung Kim if (tprev && ptime->start > tprev)
2779853b7407SDavid Ahern tprev = ptime->start;
2780853b7407SDavid Ahern
2781853b7407SDavid Ahern /*
2782853b7407SDavid Ahern * If end time given:
2783853b7407SDavid Ahern * - previous sched event is out of window - we are done
2784853b7407SDavid Ahern * - sample time is beyond window user cares about - reset it
2785853b7407SDavid Ahern * to close out stats for time window interest
278639c24341SYang Jihong * - If tprev is 0, that is, sched_in event for current task is
278739c24341SYang Jihong * not recorded, cannot determine whether sched_in event is
278839c24341SYang Jihong * within time window interest - ignore it
2789853b7407SDavid Ahern */
2790853b7407SDavid Ahern if (ptime->end) {
279139c24341SYang Jihong if (!tprev || tprev > ptime->end)
2792853b7407SDavid Ahern goto out;
2793853b7407SDavid Ahern
2794853b7407SDavid Ahern if (t > ptime->end)
2795853b7407SDavid Ahern t = ptime->end;
2796853b7407SDavid Ahern }
2797853b7407SDavid Ahern
2798ee84a303SIan Rogers if (!sched->idle_hist || thread__tid(thread) == 0) {
2799a74eaf16SDavid Ahern if (!cpu_list || test_bit(sample->cpu, cpu_bitmap))
2800853b7407SDavid Ahern timehist_update_runtime_stats(tr, t, tprev);
2801853b7407SDavid Ahern
280207235f84SNamhyung Kim if (sched->idle_hist) {
280307235f84SNamhyung Kim struct idle_thread_runtime *itr = (void *)tr;
280407235f84SNamhyung Kim struct thread_runtime *last_tr;
280507235f84SNamhyung Kim
280607235f84SNamhyung Kim if (itr->last_thread == NULL)
280707235f84SNamhyung Kim goto out;
280807235f84SNamhyung Kim
280907235f84SNamhyung Kim /* add current idle time as last thread's runtime */
281007235f84SNamhyung Kim last_tr = thread__get_runtime(itr->last_thread);
281107235f84SNamhyung Kim if (last_tr == NULL)
281207235f84SNamhyung Kim goto out;
281307235f84SNamhyung Kim
281407235f84SNamhyung Kim timehist_update_runtime_stats(last_tr, t, tprev);
281507235f84SNamhyung Kim /*
281607235f84SNamhyung Kim * remove delta time of last thread as it's not updated
281707235f84SNamhyung Kim * and otherwise it will show an invalid value next
281807235f84SNamhyung Kim * time. we only care total run time and run stat.
281907235f84SNamhyung Kim */
282007235f84SNamhyung Kim last_tr->dt_run = 0;
282107235f84SNamhyung Kim last_tr->dt_delay = 0;
2822941bdea7SNamhyung Kim last_tr->dt_sleep = 0;
2823941bdea7SNamhyung Kim last_tr->dt_iowait = 0;
2824941bdea7SNamhyung Kim last_tr->dt_preempt = 0;
282507235f84SNamhyung Kim
2826ba957ebbSNamhyung Kim if (itr->cursor.nr)
2827ba957ebbSNamhyung Kim callchain_append(&itr->callchain, &itr->cursor, t - tprev);
2828ba957ebbSNamhyung Kim
282907235f84SNamhyung Kim itr->last_thread = NULL;
283007235f84SNamhyung Kim }
283107235f84SNamhyung Kim
283252df138cSDavid Ahern if (!sched->summary_only)
2833292c4a8fSBrendan Gregg timehist_print_sample(sched, evsel, sample, &al, thread, t, state);
2834575eec21SYang Jihong }
283549394a2aSDavid Ahern
283649394a2aSDavid Ahern out:
28379396c9cbSNamhyung Kim if (sched->hist_time.start == 0 && t >= ptime->start)
28389396c9cbSNamhyung Kim sched->hist_time.start = t;
28399396c9cbSNamhyung Kim if (ptime->end == 0 || t <= ptime->end)
28409396c9cbSNamhyung Kim sched->hist_time.end = t;
28419396c9cbSNamhyung Kim
284249394a2aSDavid Ahern if (tr) {
284349394a2aSDavid Ahern /* time of this sched_switch event becomes last time task seen */
284449394a2aSDavid Ahern tr->last_time = sample->time;
284549394a2aSDavid Ahern
2846941bdea7SNamhyung Kim /* last state is used to determine where to account wait time */
2847414e050cSNamhyung Kim tr->last_state = state;
2848941bdea7SNamhyung Kim
284949394a2aSDavid Ahern /* sched out event for task so reset ready to run time */
2850d363c2a8SFernand Sieber if (state == 'R')
2851d363c2a8SFernand Sieber tr->ready_to_run = t;
2852d363c2a8SFernand Sieber else
285349394a2aSDavid Ahern tr->ready_to_run = 0;
285449394a2aSDavid Ahern }
285549394a2aSDavid Ahern
28563b7313f2SArnaldo Carvalho de Melo evsel__save_time(evsel, sample->time, sample->cpu);
285749394a2aSDavid Ahern
28580dd5041cSIan Rogers addr_location__exit(&al);
285949394a2aSDavid Ahern return rc;
286049394a2aSDavid Ahern }
286149394a2aSDavid Ahern
timehist_sched_switch_event(const struct perf_tool * tool,union perf_event * event,struct evsel * evsel,struct perf_sample * sample,struct machine * machine __maybe_unused)286230f29baeSIan Rogers static int timehist_sched_switch_event(const struct perf_tool *tool,
286349394a2aSDavid Ahern union perf_event *event,
286432dcd021SJiri Olsa struct evsel *evsel,
286549394a2aSDavid Ahern struct perf_sample *sample,
286649394a2aSDavid Ahern struct machine *machine __maybe_unused)
286749394a2aSDavid Ahern {
286849394a2aSDavid Ahern return timehist_sched_change_event(tool, event, evsel, sample, machine);
286949394a2aSDavid Ahern }
287049394a2aSDavid Ahern
process_lost(const struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine __maybe_unused)287130f29baeSIan Rogers static int process_lost(const struct perf_tool *tool __maybe_unused,
287249394a2aSDavid Ahern union perf_event *event,
287349394a2aSDavid Ahern struct perf_sample *sample,
287449394a2aSDavid Ahern struct machine *machine __maybe_unused)
287549394a2aSDavid Ahern {
287649394a2aSDavid Ahern char tstr[64];
287749394a2aSDavid Ahern
287849394a2aSDavid Ahern timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
287949394a2aSDavid Ahern printf("%15s ", tstr);
28805290ed69SJiri Olsa printf("lost %" PRI_lu64 " events on cpu %d\n", event->lost.lost, sample->cpu);
288149394a2aSDavid Ahern
288249394a2aSDavid Ahern return 0;
288349394a2aSDavid Ahern }
288449394a2aSDavid Ahern
288549394a2aSDavid Ahern
print_thread_runtime(struct thread * t,struct thread_runtime * r)288652df138cSDavid Ahern static void print_thread_runtime(struct thread *t,
288752df138cSDavid Ahern struct thread_runtime *r)
288852df138cSDavid Ahern {
288952df138cSDavid Ahern double mean = avg_stats(&r->run_stats);
289052df138cSDavid Ahern float stddev;
289152df138cSDavid Ahern
289252df138cSDavid Ahern printf("%*s %5d %9" PRIu64 " ",
2893ee84a303SIan Rogers comm_width, timehist_get_commstr(t), thread__ppid(t),
289452df138cSDavid Ahern (u64) r->run_stats.n);
289552df138cSDavid Ahern
289652df138cSDavid Ahern print_sched_time(r->total_run_time, 8);
289752df138cSDavid Ahern stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean);
289852df138cSDavid Ahern print_sched_time(r->run_stats.min, 6);
289952df138cSDavid Ahern printf(" ");
290052df138cSDavid Ahern print_sched_time((u64) mean, 6);
290152df138cSDavid Ahern printf(" ");
290252df138cSDavid Ahern print_sched_time(r->run_stats.max, 6);
290352df138cSDavid Ahern printf(" ");
290452df138cSDavid Ahern printf("%5.2f", stddev);
2905350f54faSDavid Ahern printf(" %5" PRIu64, r->migrations);
290652df138cSDavid Ahern printf("\n");
290752df138cSDavid Ahern }
290852df138cSDavid Ahern
print_thread_waittime(struct thread * t,struct thread_runtime * r)2909587782c5SNamhyung Kim static void print_thread_waittime(struct thread *t,
2910587782c5SNamhyung Kim struct thread_runtime *r)
2911587782c5SNamhyung Kim {
2912587782c5SNamhyung Kim printf("%*s %5d %9" PRIu64 " ",
2913ee84a303SIan Rogers comm_width, timehist_get_commstr(t), thread__ppid(t),
2914587782c5SNamhyung Kim (u64) r->run_stats.n);
2915587782c5SNamhyung Kim
2916587782c5SNamhyung Kim print_sched_time(r->total_run_time, 8);
2917587782c5SNamhyung Kim print_sched_time(r->total_sleep_time, 6);
2918587782c5SNamhyung Kim printf(" ");
2919587782c5SNamhyung Kim print_sched_time(r->total_iowait_time, 6);
2920587782c5SNamhyung Kim printf(" ");
2921587782c5SNamhyung Kim print_sched_time(r->total_preempt_time, 6);
2922587782c5SNamhyung Kim printf(" ");
2923587782c5SNamhyung Kim print_sched_time(r->total_delay_time, 6);
2924587782c5SNamhyung Kim printf("\n");
2925587782c5SNamhyung Kim }
2926587782c5SNamhyung Kim
292752df138cSDavid Ahern struct total_run_stats {
2928587782c5SNamhyung Kim struct perf_sched *sched;
292952df138cSDavid Ahern u64 sched_count;
293052df138cSDavid Ahern u64 task_count;
293152df138cSDavid Ahern u64 total_run_time;
293252df138cSDavid Ahern };
293352df138cSDavid Ahern
show_thread_runtime(struct thread * t,void * priv)293440826c45SIan Rogers static int show_thread_runtime(struct thread *t, void *priv)
293552df138cSDavid Ahern {
293652df138cSDavid Ahern struct total_run_stats *stats = priv;
293752df138cSDavid Ahern struct thread_runtime *r;
293852df138cSDavid Ahern
293952df138cSDavid Ahern if (thread__is_filtered(t))
294052df138cSDavid Ahern return 0;
294152df138cSDavid Ahern
294252df138cSDavid Ahern r = thread__priv(t);
294352df138cSDavid Ahern if (r && r->run_stats.n) {
294452df138cSDavid Ahern stats->task_count++;
294552df138cSDavid Ahern stats->sched_count += r->run_stats.n;
294652df138cSDavid Ahern stats->total_run_time += r->total_run_time;
2947587782c5SNamhyung Kim
2948587782c5SNamhyung Kim if (stats->sched->show_state)
2949587782c5SNamhyung Kim print_thread_waittime(t, r);
2950587782c5SNamhyung Kim else
295152df138cSDavid Ahern print_thread_runtime(t, r);
295252df138cSDavid Ahern }
295352df138cSDavid Ahern
295452df138cSDavid Ahern return 0;
295552df138cSDavid Ahern }
295652df138cSDavid Ahern
callchain__fprintf_folded(FILE * fp,struct callchain_node * node)2957ba957ebbSNamhyung Kim static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
2958ba957ebbSNamhyung Kim {
2959ba957ebbSNamhyung Kim const char *sep = " <- ";
2960ba957ebbSNamhyung Kim struct callchain_list *chain;
2961ba957ebbSNamhyung Kim size_t ret = 0;
2962ba957ebbSNamhyung Kim char bf[1024];
2963ba957ebbSNamhyung Kim bool first;
2964ba957ebbSNamhyung Kim
2965ba957ebbSNamhyung Kim if (node == NULL)
2966ba957ebbSNamhyung Kim return 0;
2967ba957ebbSNamhyung Kim
2968ba957ebbSNamhyung Kim ret = callchain__fprintf_folded(fp, node->parent);
2969ba957ebbSNamhyung Kim first = (ret == 0);
2970ba957ebbSNamhyung Kim
2971ba957ebbSNamhyung Kim list_for_each_entry(chain, &node->val, list) {
2972ba957ebbSNamhyung Kim if (chain->ip >= PERF_CONTEXT_MAX)
2973ba957ebbSNamhyung Kim continue;
2974ba957ebbSNamhyung Kim if (chain->ms.sym && chain->ms.sym->ignore)
2975ba957ebbSNamhyung Kim continue;
2976ba957ebbSNamhyung Kim ret += fprintf(fp, "%s%s", first ? "" : sep,
2977ba957ebbSNamhyung Kim callchain_list__sym_name(chain, bf, sizeof(bf),
2978ba957ebbSNamhyung Kim false));
2979ba957ebbSNamhyung Kim first = false;
2980ba957ebbSNamhyung Kim }
2981ba957ebbSNamhyung Kim
2982ba957ebbSNamhyung Kim return ret;
2983ba957ebbSNamhyung Kim }
2984ba957ebbSNamhyung Kim
timehist_print_idlehist_callchain(struct rb_root_cached * root)2985cb4c13a5SDavidlohr Bueso static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)
2986ba957ebbSNamhyung Kim {
2987ba957ebbSNamhyung Kim size_t ret = 0;
2988ba957ebbSNamhyung Kim FILE *fp = stdout;
2989ba957ebbSNamhyung Kim struct callchain_node *chain;
2990cb4c13a5SDavidlohr Bueso struct rb_node *rb_node = rb_first_cached(root);
2991ba957ebbSNamhyung Kim
2992ba957ebbSNamhyung Kim printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains");
2993ba957ebbSNamhyung Kim printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
2994ba957ebbSNamhyung Kim graph_dotted_line);
2995ba957ebbSNamhyung Kim
2996ba957ebbSNamhyung Kim while (rb_node) {
2997ba957ebbSNamhyung Kim chain = rb_entry(rb_node, struct callchain_node, rb_node);
2998ba957ebbSNamhyung Kim rb_node = rb_next(rb_node);
2999ba957ebbSNamhyung Kim
3000ba957ebbSNamhyung Kim ret += fprintf(fp, " ");
3001ba957ebbSNamhyung Kim print_sched_time(chain->hit, 12);
3002ba957ebbSNamhyung Kim ret += 16; /* print_sched_time returns 2nd arg + 4 */
3003ba957ebbSNamhyung Kim ret += fprintf(fp, " %8d ", chain->count);
3004ba957ebbSNamhyung Kim ret += callchain__fprintf_folded(fp, chain);
3005ba957ebbSNamhyung Kim ret += fprintf(fp, "\n");
3006ba957ebbSNamhyung Kim }
3007ba957ebbSNamhyung Kim
3008ba957ebbSNamhyung Kim return ret;
3009ba957ebbSNamhyung Kim }
3010ba957ebbSNamhyung Kim
timehist_print_summary(struct perf_sched * sched,struct perf_session * session)301152df138cSDavid Ahern static void timehist_print_summary(struct perf_sched *sched,
301252df138cSDavid Ahern struct perf_session *session)
301352df138cSDavid Ahern {
301452df138cSDavid Ahern struct machine *m = &session->machines.host;
301552df138cSDavid Ahern struct total_run_stats totals;
301652df138cSDavid Ahern u64 task_count;
301752df138cSDavid Ahern struct thread *t;
301852df138cSDavid Ahern struct thread_runtime *r;
301952df138cSDavid Ahern int i;
30209396c9cbSNamhyung Kim u64 hist_time = sched->hist_time.end - sched->hist_time.start;
302152df138cSDavid Ahern
302252df138cSDavid Ahern memset(&totals, 0, sizeof(totals));
3023587782c5SNamhyung Kim totals.sched = sched;
302452df138cSDavid Ahern
302507235f84SNamhyung Kim if (sched->idle_hist) {
302607235f84SNamhyung Kim printf("\nIdle-time summary\n");
302707235f84SNamhyung Kim printf("%*s parent sched-out ", comm_width, "comm");
302807235f84SNamhyung Kim printf(" idle-time min-idle avg-idle max-idle stddev migrations\n");
3029587782c5SNamhyung Kim } else if (sched->show_state) {
3030587782c5SNamhyung Kim printf("\nWait-time summary\n");
3031587782c5SNamhyung Kim printf("%*s parent sched-in ", comm_width, "comm");
3032587782c5SNamhyung Kim printf(" run-time sleep iowait preempt delay\n");
303307235f84SNamhyung Kim } else {
303452df138cSDavid Ahern printf("\nRuntime summary\n");
303552df138cSDavid Ahern printf("%*s parent sched-in ", comm_width, "comm");
3036350f54faSDavid Ahern printf(" run-time min-run avg-run max-run stddev migrations\n");
303707235f84SNamhyung Kim }
303852df138cSDavid Ahern printf("%*s (count) ", comm_width, "");
3039587782c5SNamhyung Kim printf(" (msec) (msec) (msec) (msec) %s\n",
3040587782c5SNamhyung Kim sched->show_state ? "(msec)" : "%");
3041350f54faSDavid Ahern printf("%.117s\n", graph_dotted_line);
304252df138cSDavid Ahern
304352df138cSDavid Ahern machine__for_each_thread(m, show_thread_runtime, &totals);
304452df138cSDavid Ahern task_count = totals.task_count;
304552df138cSDavid Ahern if (!task_count)
304652df138cSDavid Ahern printf("<no still running tasks>\n");
304752df138cSDavid Ahern
304852df138cSDavid Ahern /* CPU idle stats not tracked when samples were skipped */
304907235f84SNamhyung Kim if (sched->skipped_samples && !sched->idle_hist)
305052df138cSDavid Ahern return;
305152df138cSDavid Ahern
305252df138cSDavid Ahern printf("\nIdle stats:\n");
3053b336352bSNamhyung Kim for (i = 0; i < idle_max_cpu; ++i) {
3054a74eaf16SDavid Ahern if (cpu_list && !test_bit(i, cpu_bitmap))
3055a74eaf16SDavid Ahern continue;
3056a74eaf16SDavid Ahern
305752df138cSDavid Ahern t = idle_threads[i];
305852df138cSDavid Ahern if (!t)
305952df138cSDavid Ahern continue;
306052df138cSDavid Ahern
306152df138cSDavid Ahern r = thread__priv(t);
306252df138cSDavid Ahern if (r && r->run_stats.n) {
306352df138cSDavid Ahern totals.sched_count += r->run_stats.n;
306452df138cSDavid Ahern printf(" CPU %2d idle for ", i);
306552df138cSDavid Ahern print_sched_time(r->total_run_time, 6);
30669396c9cbSNamhyung Kim printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time);
306752df138cSDavid Ahern } else
306852df138cSDavid Ahern printf(" CPU %2d idle entire time window\n", i);
306952df138cSDavid Ahern }
307052df138cSDavid Ahern
30714c50563dSArnaldo Carvalho de Melo if (sched->idle_hist && sched->show_callchain) {
3072ba957ebbSNamhyung Kim callchain_param.mode = CHAIN_FOLDED;
3073ba957ebbSNamhyung Kim callchain_param.value = CCVAL_PERIOD;
3074ba957ebbSNamhyung Kim
3075ba957ebbSNamhyung Kim callchain_register_param(&callchain_param);
3076ba957ebbSNamhyung Kim
3077ba957ebbSNamhyung Kim printf("\nIdle stats by callchain:\n");
3078ba957ebbSNamhyung Kim for (i = 0; i < idle_max_cpu; ++i) {
3079ba957ebbSNamhyung Kim struct idle_thread_runtime *itr;
3080ba957ebbSNamhyung Kim
3081ba957ebbSNamhyung Kim t = idle_threads[i];
3082ba957ebbSNamhyung Kim if (!t)
3083ba957ebbSNamhyung Kim continue;
3084ba957ebbSNamhyung Kim
3085ba957ebbSNamhyung Kim itr = thread__priv(t);
3086ba957ebbSNamhyung Kim if (itr == NULL)
3087ba957ebbSNamhyung Kim continue;
3088ba957ebbSNamhyung Kim
3089cb4c13a5SDavidlohr Bueso callchain_param.sort(&itr->sorted_root.rb_root, &itr->callchain,
3090ba957ebbSNamhyung Kim 0, &callchain_param);
3091ba957ebbSNamhyung Kim
3092ba957ebbSNamhyung Kim printf(" CPU %2d:", i);
3093ba957ebbSNamhyung Kim print_sched_time(itr->tr.total_run_time, 6);
3094ba957ebbSNamhyung Kim printf(" msec\n");
3095ba957ebbSNamhyung Kim timehist_print_idlehist_callchain(&itr->sorted_root);
3096ba957ebbSNamhyung Kim printf("\n");
3097ba957ebbSNamhyung Kim }
3098ba957ebbSNamhyung Kim }
3099ba957ebbSNamhyung Kim
310052df138cSDavid Ahern printf("\n"
310152df138cSDavid Ahern " Total number of unique tasks: %" PRIu64 "\n"
31029396c9cbSNamhyung Kim "Total number of context switches: %" PRIu64 "\n",
310352df138cSDavid Ahern totals.task_count, totals.sched_count);
310452df138cSDavid Ahern
31059396c9cbSNamhyung Kim printf(" Total run time (msec): ");
310652df138cSDavid Ahern print_sched_time(totals.total_run_time, 2);
310752df138cSDavid Ahern printf("\n");
31089396c9cbSNamhyung Kim
31099396c9cbSNamhyung Kim printf(" Total scheduling time (msec): ");
31109396c9cbSNamhyung Kim print_sched_time(hist_time, 2);
31116d18804bSIan Rogers printf(" (x %d)\n", sched->max_cpu.cpu);
311252df138cSDavid Ahern }
311352df138cSDavid Ahern
311430f29baeSIan Rogers typedef int (*sched_handler)(const struct perf_tool *tool,
311549394a2aSDavid Ahern union perf_event *event,
311632dcd021SJiri Olsa struct evsel *evsel,
311749394a2aSDavid Ahern struct perf_sample *sample,
311849394a2aSDavid Ahern struct machine *machine);
311949394a2aSDavid Ahern
perf_timehist__process_sample(const struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)312030f29baeSIan Rogers static int perf_timehist__process_sample(const struct perf_tool *tool,
312149394a2aSDavid Ahern union perf_event *event,
312249394a2aSDavid Ahern struct perf_sample *sample,
312332dcd021SJiri Olsa struct evsel *evsel,
312449394a2aSDavid Ahern struct machine *machine)
312549394a2aSDavid Ahern {
312649394a2aSDavid Ahern struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
312749394a2aSDavid Ahern int err = 0;
31286d18804bSIan Rogers struct perf_cpu this_cpu = {
31296d18804bSIan Rogers .cpu = sample->cpu,
31306d18804bSIan Rogers };
313149394a2aSDavid Ahern
31326d18804bSIan Rogers if (this_cpu.cpu > sched->max_cpu.cpu)
313349394a2aSDavid Ahern sched->max_cpu = this_cpu;
313449394a2aSDavid Ahern
313549394a2aSDavid Ahern if (evsel->handler != NULL) {
313649394a2aSDavid Ahern sched_handler f = evsel->handler;
313749394a2aSDavid Ahern
313849394a2aSDavid Ahern err = f(tool, event, evsel, sample, machine);
313949394a2aSDavid Ahern }
314049394a2aSDavid Ahern
314149394a2aSDavid Ahern return err;
314249394a2aSDavid Ahern }
314349394a2aSDavid Ahern
timehist_check_attr(struct perf_sched * sched,struct evlist * evlist)31446c973c90SDavid Ahern static int timehist_check_attr(struct perf_sched *sched,
314563503dbaSJiri Olsa struct evlist *evlist)
31466c973c90SDavid Ahern {
314732dcd021SJiri Olsa struct evsel *evsel;
31486c973c90SDavid Ahern struct evsel_runtime *er;
31496c973c90SDavid Ahern
3150ce9036a6SJiri Olsa list_for_each_entry(evsel, &evlist->core.entries, core.node) {
31513b7313f2SArnaldo Carvalho de Melo er = evsel__get_runtime(evsel);
31526c973c90SDavid Ahern if (er == NULL) {
31536c973c90SDavid Ahern pr_err("Failed to allocate memory for evsel runtime data\n");
31546c973c90SDavid Ahern return -1;
31556c973c90SDavid Ahern }
31566c973c90SDavid Ahern
31576e4b3987SYang Jihong /* only need to save callchain related to sched_switch event */
31586e4b3987SYang Jihong if (sched->show_callchain &&
31596e4b3987SYang Jihong evsel__name_is(evsel, "sched:sched_switch") &&
31606e4b3987SYang Jihong !evsel__has_callchain(evsel)) {
31616e4b3987SYang Jihong pr_info("Samples of sched_switch event do not have callchains.\n");
31626c973c90SDavid Ahern sched->show_callchain = 0;
31636c973c90SDavid Ahern symbol_conf.use_callchain = 0;
31646c973c90SDavid Ahern }
31656c973c90SDavid Ahern }
31666c973c90SDavid Ahern
31676c973c90SDavid Ahern return 0;
31686c973c90SDavid Ahern }
31696c973c90SDavid Ahern
timehist_parse_prio_str(struct perf_sched * sched)31709b3a48bbSYang Jihong static int timehist_parse_prio_str(struct perf_sched *sched)
31719b3a48bbSYang Jihong {
31729b3a48bbSYang Jihong char *p;
31739b3a48bbSYang Jihong unsigned long start_prio, end_prio;
31749b3a48bbSYang Jihong const char *str = sched->prio_str;
31759b3a48bbSYang Jihong
31769b3a48bbSYang Jihong if (!str)
31779b3a48bbSYang Jihong return 0;
31789b3a48bbSYang Jihong
31799b3a48bbSYang Jihong while (isdigit(*str)) {
31809b3a48bbSYang Jihong p = NULL;
31819b3a48bbSYang Jihong start_prio = strtoul(str, &p, 0);
31829b3a48bbSYang Jihong if (start_prio >= MAX_PRIO || (*p != '\0' && *p != ',' && *p != '-'))
31839b3a48bbSYang Jihong return -1;
31849b3a48bbSYang Jihong
31859b3a48bbSYang Jihong if (*p == '-') {
31869b3a48bbSYang Jihong str = ++p;
31879b3a48bbSYang Jihong p = NULL;
31889b3a48bbSYang Jihong end_prio = strtoul(str, &p, 0);
31899b3a48bbSYang Jihong
31909b3a48bbSYang Jihong if (end_prio >= MAX_PRIO || (*p != '\0' && *p != ','))
31919b3a48bbSYang Jihong return -1;
31929b3a48bbSYang Jihong
31939b3a48bbSYang Jihong if (end_prio < start_prio)
31949b3a48bbSYang Jihong return -1;
31959b3a48bbSYang Jihong } else {
31969b3a48bbSYang Jihong end_prio = start_prio;
31979b3a48bbSYang Jihong }
31989b3a48bbSYang Jihong
31999b3a48bbSYang Jihong for (; start_prio <= end_prio; start_prio++)
32009b3a48bbSYang Jihong __set_bit(start_prio, sched->prio_bitmap);
32019b3a48bbSYang Jihong
32029b3a48bbSYang Jihong if (*p)
32039b3a48bbSYang Jihong ++p;
32049b3a48bbSYang Jihong
32059b3a48bbSYang Jihong str = p;
32069b3a48bbSYang Jihong }
32079b3a48bbSYang Jihong
32089b3a48bbSYang Jihong return 0;
32099b3a48bbSYang Jihong }
32109b3a48bbSYang Jihong
perf_sched__timehist(struct perf_sched * sched)321149394a2aSDavid Ahern static int perf_sched__timehist(struct perf_sched *sched)
321249394a2aSDavid Ahern {
3213d566a9c2SDavid Ahern struct evsel_str_handler handlers[] = {
321449394a2aSDavid Ahern { "sched:sched_switch", timehist_sched_switch_event, },
321549394a2aSDavid Ahern { "sched:sched_wakeup", timehist_sched_wakeup_event, },
3216d566a9c2SDavid Ahern { "sched:sched_waking", timehist_sched_wakeup_event, },
321749394a2aSDavid Ahern { "sched:sched_wakeup_new", timehist_sched_wakeup_event, },
321849394a2aSDavid Ahern };
321932dcd021SJiri Olsa const struct evsel_str_handler migrate_handlers[] = {
3220350f54faSDavid Ahern { "sched:sched_migrate_task", timehist_migrate_task_event, },
3221350f54faSDavid Ahern };
32228ceb41d7SJiri Olsa struct perf_data data = {
322349394a2aSDavid Ahern .path = input_name,
322449394a2aSDavid Ahern .mode = PERF_DATA_MODE_READ,
32256fa94258SNamhyung Kim .force = sched->force,
322649394a2aSDavid Ahern };
322749394a2aSDavid Ahern
322849394a2aSDavid Ahern struct perf_session *session;
322963503dbaSJiri Olsa struct evlist *evlist;
323049394a2aSDavid Ahern int err = -1;
323149394a2aSDavid Ahern
323249394a2aSDavid Ahern /*
323349394a2aSDavid Ahern * event handlers for timehist option
323449394a2aSDavid Ahern */
323549394a2aSDavid Ahern sched->tool.sample = perf_timehist__process_sample;
323649394a2aSDavid Ahern sched->tool.mmap = perf_event__process_mmap;
323749394a2aSDavid Ahern sched->tool.comm = perf_event__process_comm;
323849394a2aSDavid Ahern sched->tool.exit = perf_event__process_exit;
323949394a2aSDavid Ahern sched->tool.fork = perf_event__process_fork;
324049394a2aSDavid Ahern sched->tool.lost = process_lost;
324149394a2aSDavid Ahern sched->tool.attr = perf_event__process_attr;
324249394a2aSDavid Ahern sched->tool.tracing_data = perf_event__process_tracing_data;
324349394a2aSDavid Ahern sched->tool.build_id = perf_event__process_build_id;
324449394a2aSDavid Ahern
324549394a2aSDavid Ahern sched->tool.ordering_requires_timestamps = true;
324649394a2aSDavid Ahern
32476c973c90SDavid Ahern symbol_conf.use_callchain = sched->show_callchain;
32486c973c90SDavid Ahern
32492681bd85SNamhyung Kim session = perf_session__new(&data, &sched->tool);
32506ef81c55SMamatha Inamdar if (IS_ERR(session))
32516ef81c55SMamatha Inamdar return PTR_ERR(session);
325249394a2aSDavid Ahern
3253c30d630dSDavid Ahern if (cpu_list) {
3254c30d630dSDavid Ahern err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3255c30d630dSDavid Ahern if (err < 0)
3256c30d630dSDavid Ahern goto out;
3257c30d630dSDavid Ahern }
3258c30d630dSDavid Ahern
325952df138cSDavid Ahern evlist = session->evlist;
326052df138cSDavid Ahern
326149394a2aSDavid Ahern symbol__init(&session->header.env);
326249394a2aSDavid Ahern
3263853b7407SDavid Ahern if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
3264853b7407SDavid Ahern pr_err("Invalid time string\n");
32656bdf5168SYang Jihong err = -EINVAL;
32666bdf5168SYang Jihong goto out;
3267853b7407SDavid Ahern }
3268853b7407SDavid Ahern
32696c973c90SDavid Ahern if (timehist_check_attr(sched, evlist) != 0)
32706c973c90SDavid Ahern goto out;
32716c973c90SDavid Ahern
32729b3a48bbSYang Jihong if (timehist_parse_prio_str(sched) != 0) {
32739b3a48bbSYang Jihong pr_err("Invalid prio string\n");
32749b3a48bbSYang Jihong goto out;
32759b3a48bbSYang Jihong }
32769b3a48bbSYang Jihong
327749394a2aSDavid Ahern setup_pager();
327849394a2aSDavid Ahern
3279d566a9c2SDavid Ahern /* prefer sched_waking if it is captured */
3280b02736f7SArnaldo Carvalho de Melo if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking"))
3281d566a9c2SDavid Ahern handlers[1].handler = timehist_sched_wakeup_ignore;
3282d566a9c2SDavid Ahern
328349394a2aSDavid Ahern /* setup per-evsel handlers */
328449394a2aSDavid Ahern if (perf_session__set_tracepoints_handlers(session, handlers))
328549394a2aSDavid Ahern goto out;
328649394a2aSDavid Ahern
3287f45bf8d3SDavid Ahern /* sched_switch event at a minimum needs to exist */
3288b02736f7SArnaldo Carvalho de Melo if (!evlist__find_tracepoint_by_name(session->evlist, "sched:sched_switch")) {
3289f45bf8d3SDavid Ahern pr_err("No sched_switch events found. Have you run 'perf sched record'?\n");
329049394a2aSDavid Ahern goto out;
3291f45bf8d3SDavid Ahern }
329249394a2aSDavid Ahern
3293350f54faSDavid Ahern if (sched->show_migrations &&
3294350f54faSDavid Ahern perf_session__set_tracepoints_handlers(session, migrate_handlers))
3295350f54faSDavid Ahern goto out;
3296350f54faSDavid Ahern
329749394a2aSDavid Ahern /* pre-allocate struct for per-CPU idle stats */
32986d18804bSIan Rogers sched->max_cpu.cpu = session->header.env.nr_cpus_online;
32996d18804bSIan Rogers if (sched->max_cpu.cpu == 0)
33006d18804bSIan Rogers sched->max_cpu.cpu = 4;
33016d18804bSIan Rogers if (init_idle_threads(sched->max_cpu.cpu))
330249394a2aSDavid Ahern goto out;
330349394a2aSDavid Ahern
330452df138cSDavid Ahern /* summary_only implies summary option, but don't overwrite summary if set */
330552df138cSDavid Ahern if (sched->summary_only)
330652df138cSDavid Ahern sched->summary = sched->summary_only;
330752df138cSDavid Ahern
330852df138cSDavid Ahern if (!sched->summary_only)
3309a407b067SDavid Ahern timehist_header(sched);
331049394a2aSDavid Ahern
331149394a2aSDavid Ahern err = perf_session__process_events(session);
331249394a2aSDavid Ahern if (err) {
331349394a2aSDavid Ahern pr_err("Failed to process events, error %d", err);
331449394a2aSDavid Ahern goto out;
331549394a2aSDavid Ahern }
331649394a2aSDavid Ahern
331752df138cSDavid Ahern sched->nr_events = evlist->stats.nr_events[0];
331852df138cSDavid Ahern sched->nr_lost_events = evlist->stats.total_lost;
331952df138cSDavid Ahern sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST];
332052df138cSDavid Ahern
332152df138cSDavid Ahern if (sched->summary)
332252df138cSDavid Ahern timehist_print_summary(sched, session);
332352df138cSDavid Ahern
332449394a2aSDavid Ahern out:
332549394a2aSDavid Ahern free_idle_threads();
332649394a2aSDavid Ahern perf_session__delete(session);
332749394a2aSDavid Ahern
332849394a2aSDavid Ahern return err;
332949394a2aSDavid Ahern }
333049394a2aSDavid Ahern
333149394a2aSDavid Ahern
print_bad_events(struct perf_sched * sched)33320e9b07e5SArnaldo Carvalho de Melo static void print_bad_events(struct perf_sched *sched)
33330ec04e16SIngo Molnar {
33340e9b07e5SArnaldo Carvalho de Melo if (sched->nr_unordered_timestamps && sched->nr_timestamps) {
33350ec04e16SIngo Molnar printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
33360e9b07e5SArnaldo Carvalho de Melo (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0,
33370e9b07e5SArnaldo Carvalho de Melo sched->nr_unordered_timestamps, sched->nr_timestamps);
33380ec04e16SIngo Molnar }
33390e9b07e5SArnaldo Carvalho de Melo if (sched->nr_lost_events && sched->nr_events) {
33400ec04e16SIngo Molnar printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
33410e9b07e5SArnaldo Carvalho de Melo (double)sched->nr_lost_events/(double)sched->nr_events * 100.0,
33420e9b07e5SArnaldo Carvalho de Melo sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks);
33430ec04e16SIngo Molnar }
33440e9b07e5SArnaldo Carvalho de Melo if (sched->nr_context_switch_bugs && sched->nr_timestamps) {
33450ec04e16SIngo Molnar printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
33460e9b07e5SArnaldo Carvalho de Melo (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0,
33470e9b07e5SArnaldo Carvalho de Melo sched->nr_context_switch_bugs, sched->nr_timestamps);
33480e9b07e5SArnaldo Carvalho de Melo if (sched->nr_lost_events)
33490ec04e16SIngo Molnar printf(" (due to lost events?)");
33500ec04e16SIngo Molnar printf("\n");
33510ec04e16SIngo Molnar }
33520ec04e16SIngo Molnar }
33530ec04e16SIngo Molnar
__merge_work_atoms(struct rb_root_cached * root,struct work_atoms * data)3354cb4c13a5SDavidlohr Bueso static void __merge_work_atoms(struct rb_root_cached *root, struct work_atoms *data)
33552f80dd44SJosef Bacik {
3356cb4c13a5SDavidlohr Bueso struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
33572f80dd44SJosef Bacik struct work_atoms *this;
33582f80dd44SJosef Bacik const char *comm = thread__comm_str(data->thread), *this_comm;
3359cb4c13a5SDavidlohr Bueso bool leftmost = true;
33602f80dd44SJosef Bacik
33612f80dd44SJosef Bacik while (*new) {
33622f80dd44SJosef Bacik int cmp;
33632f80dd44SJosef Bacik
33642f80dd44SJosef Bacik this = container_of(*new, struct work_atoms, node);
33652f80dd44SJosef Bacik parent = *new;
33662f80dd44SJosef Bacik
33672f80dd44SJosef Bacik this_comm = thread__comm_str(this->thread);
33682f80dd44SJosef Bacik cmp = strcmp(comm, this_comm);
33692f80dd44SJosef Bacik if (cmp > 0) {
33702f80dd44SJosef Bacik new = &((*new)->rb_left);
33712f80dd44SJosef Bacik } else if (cmp < 0) {
33722f80dd44SJosef Bacik new = &((*new)->rb_right);
3373cb4c13a5SDavidlohr Bueso leftmost = false;
33742f80dd44SJosef Bacik } else {
33752f80dd44SJosef Bacik this->num_merged++;
33762f80dd44SJosef Bacik this->total_runtime += data->total_runtime;
33772f80dd44SJosef Bacik this->nb_atoms += data->nb_atoms;
33782f80dd44SJosef Bacik this->total_lat += data->total_lat;
33792f80dd44SJosef Bacik list_splice(&data->work_list, &this->work_list);
33802f80dd44SJosef Bacik if (this->max_lat < data->max_lat) {
33812f80dd44SJosef Bacik this->max_lat = data->max_lat;
3382dc000c45SJoel Fernandes (Google) this->max_lat_start = data->max_lat_start;
3383dc000c45SJoel Fernandes (Google) this->max_lat_end = data->max_lat_end;
33842f80dd44SJosef Bacik }
33852f80dd44SJosef Bacik zfree(&data);
33862f80dd44SJosef Bacik return;
33872f80dd44SJosef Bacik }
33882f80dd44SJosef Bacik }
33892f80dd44SJosef Bacik
33902f80dd44SJosef Bacik data->num_merged++;
33912f80dd44SJosef Bacik rb_link_node(&data->node, parent, new);
3392cb4c13a5SDavidlohr Bueso rb_insert_color_cached(&data->node, root, leftmost);
33932f80dd44SJosef Bacik }
33942f80dd44SJosef Bacik
perf_sched__merge_lat(struct perf_sched * sched)33952f80dd44SJosef Bacik static void perf_sched__merge_lat(struct perf_sched *sched)
33962f80dd44SJosef Bacik {
33972f80dd44SJosef Bacik struct work_atoms *data;
33982f80dd44SJosef Bacik struct rb_node *node;
33992f80dd44SJosef Bacik
34002f80dd44SJosef Bacik if (sched->skip_merge)
34012f80dd44SJosef Bacik return;
34022f80dd44SJosef Bacik
3403cb4c13a5SDavidlohr Bueso while ((node = rb_first_cached(&sched->atom_root))) {
3404cb4c13a5SDavidlohr Bueso rb_erase_cached(node, &sched->atom_root);
34052f80dd44SJosef Bacik data = rb_entry(node, struct work_atoms, node);
34062f80dd44SJosef Bacik __merge_work_atoms(&sched->merged_atom_root, data);
34072f80dd44SJosef Bacik }
34082f80dd44SJosef Bacik }
34092f80dd44SJosef Bacik
setup_cpus_switch_event(struct perf_sched * sched)3410bd2cdf26SYang Jihong static int setup_cpus_switch_event(struct perf_sched *sched)
3411bd2cdf26SYang Jihong {
3412bd2cdf26SYang Jihong unsigned int i;
3413bd2cdf26SYang Jihong
3414bd2cdf26SYang Jihong sched->cpu_last_switched = calloc(MAX_CPUS, sizeof(*(sched->cpu_last_switched)));
3415bd2cdf26SYang Jihong if (!sched->cpu_last_switched)
3416bd2cdf26SYang Jihong return -1;
3417bd2cdf26SYang Jihong
3418bd2cdf26SYang Jihong sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
3419bd2cdf26SYang Jihong if (!sched->curr_pid) {
3420bd2cdf26SYang Jihong zfree(&sched->cpu_last_switched);
3421bd2cdf26SYang Jihong return -1;
3422bd2cdf26SYang Jihong }
3423bd2cdf26SYang Jihong
3424bd2cdf26SYang Jihong for (i = 0; i < MAX_CPUS; i++)
3425bd2cdf26SYang Jihong sched->curr_pid[i] = -1;
3426bd2cdf26SYang Jihong
3427bd2cdf26SYang Jihong return 0;
3428bd2cdf26SYang Jihong }
3429bd2cdf26SYang Jihong
free_cpus_switch_event(struct perf_sched * sched)3430bd2cdf26SYang Jihong static void free_cpus_switch_event(struct perf_sched *sched)
3431bd2cdf26SYang Jihong {
3432bd2cdf26SYang Jihong zfree(&sched->curr_pid);
3433bd2cdf26SYang Jihong zfree(&sched->cpu_last_switched);
3434bd2cdf26SYang Jihong }
3435bd2cdf26SYang Jihong
perf_sched__lat(struct perf_sched * sched)34360e9b07e5SArnaldo Carvalho de Melo static int perf_sched__lat(struct perf_sched *sched)
34370ec04e16SIngo Molnar {
3438bd2cdf26SYang Jihong int rc = -1;
34390ec04e16SIngo Molnar struct rb_node *next;
34400ec04e16SIngo Molnar
34410ec04e16SIngo Molnar setup_pager();
3442ad9def7cSDavid Ahern
3443bd2cdf26SYang Jihong if (setup_cpus_switch_event(sched))
3444bd2cdf26SYang Jihong return rc;
3445bd2cdf26SYang Jihong
3446ae536acfSArnaldo Carvalho de Melo if (perf_sched__read_events(sched))
3447bd2cdf26SYang Jihong goto out_free_cpus_switch_event;
3448ad9def7cSDavid Ahern
34492f80dd44SJosef Bacik perf_sched__merge_lat(sched);
34500e9b07e5SArnaldo Carvalho de Melo perf_sched__sort_lat(sched);
34510ec04e16SIngo Molnar
3452dc000c45SJoel Fernandes (Google) printf("\n -------------------------------------------------------------------------------------------------------------------------------------------\n");
34536fe61cb4SMadadi Vineeth Reddy printf(" Task | Runtime ms | Count | Avg delay ms | Max delay ms | Max delay start | Max delay end |\n");
3454dc000c45SJoel Fernandes (Google) printf(" -------------------------------------------------------------------------------------------------------------------------------------------\n");
34550ec04e16SIngo Molnar
3456cb4c13a5SDavidlohr Bueso next = rb_first_cached(&sched->sorted_atom_root);
34570ec04e16SIngo Molnar
34580ec04e16SIngo Molnar while (next) {
34590ec04e16SIngo Molnar struct work_atoms *work_list;
34600ec04e16SIngo Molnar
34610ec04e16SIngo Molnar work_list = rb_entry(next, struct work_atoms, node);
34620e9b07e5SArnaldo Carvalho de Melo output_lat_thread(sched, work_list);
34630ec04e16SIngo Molnar next = rb_next(next);
3464ae536acfSArnaldo Carvalho de Melo thread__zput(work_list->thread);
34650ec04e16SIngo Molnar }
34660ec04e16SIngo Molnar
346780790e0bSRamkumar Ramachandra printf(" -----------------------------------------------------------------------------------------------------------------\n");
34689486aa38SArnaldo Carvalho de Melo printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
34694fc76e49SArnaldo Carvalho de Melo (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count);
34700ec04e16SIngo Molnar
34710ec04e16SIngo Molnar printf(" ---------------------------------------------------\n");
34720ec04e16SIngo Molnar
34730e9b07e5SArnaldo Carvalho de Melo print_bad_events(sched);
34740ec04e16SIngo Molnar printf("\n");
34750ec04e16SIngo Molnar
3476bd2cdf26SYang Jihong rc = 0;
3477bd2cdf26SYang Jihong
3478bd2cdf26SYang Jihong out_free_cpus_switch_event:
3479bd2cdf26SYang Jihong free_cpus_switch_event(sched);
3480bd2cdf26SYang Jihong return rc;
34810ec04e16SIngo Molnar }
34820ec04e16SIngo Molnar
setup_map_cpus(struct perf_sched * sched)348399623c62SJiri Olsa static int setup_map_cpus(struct perf_sched *sched)
34840ec04e16SIngo Molnar {
34856d18804bSIan Rogers sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF);
348640749d0fSIngo Molnar
348799623c62SJiri Olsa if (sched->map.comp) {
34886d18804bSIan Rogers sched->map.comp_cpus = zalloc(sched->max_cpu.cpu * sizeof(int));
3489cf294f24SJiri Olsa if (!sched->map.comp_cpus)
3490cf294f24SJiri Olsa return -1;
349199623c62SJiri Olsa }
349299623c62SJiri Olsa
3493ef76a5afSYang Jihong if (sched->map.cpus_str) {
3494ef76a5afSYang Jihong sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str);
3495ef76a5afSYang Jihong if (!sched->map.cpus) {
349673643bb6SJiri Olsa pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
3497ef76a5afSYang Jihong zfree(&sched->map.comp_cpus);
349873643bb6SJiri Olsa return -1;
349973643bb6SJiri Olsa }
3500ef76a5afSYang Jihong }
350173643bb6SJiri Olsa
350299623c62SJiri Olsa return 0;
350399623c62SJiri Olsa }
350499623c62SJiri Olsa
setup_color_pids(struct perf_sched * sched)3505a151a37aSJiri Olsa static int setup_color_pids(struct perf_sched *sched)
3506a151a37aSJiri Olsa {
35079749b90eSJiri Olsa struct perf_thread_map *map;
3508a151a37aSJiri Olsa
3509a151a37aSJiri Olsa if (!sched->map.color_pids_str)
3510a151a37aSJiri Olsa return 0;
3511a151a37aSJiri Olsa
3512a151a37aSJiri Olsa map = thread_map__new_by_tid_str(sched->map.color_pids_str);
3513a151a37aSJiri Olsa if (!map) {
3514a151a37aSJiri Olsa pr_err("failed to get thread map from %s\n", sched->map.color_pids_str);
3515a151a37aSJiri Olsa return -1;
3516a151a37aSJiri Olsa }
3517a151a37aSJiri Olsa
3518a151a37aSJiri Olsa sched->map.color_pids = map;
3519a151a37aSJiri Olsa return 0;
3520a151a37aSJiri Olsa }
3521a151a37aSJiri Olsa
setup_color_cpus(struct perf_sched * sched)3522cf294f24SJiri Olsa static int setup_color_cpus(struct perf_sched *sched)
3523cf294f24SJiri Olsa {
3524f854839bSJiri Olsa struct perf_cpu_map *map;
3525cf294f24SJiri Olsa
3526cf294f24SJiri Olsa if (!sched->map.color_cpus_str)
3527cf294f24SJiri Olsa return 0;
3528cf294f24SJiri Olsa
35299c3516d1SJiri Olsa map = perf_cpu_map__new(sched->map.color_cpus_str);
3530cf294f24SJiri Olsa if (!map) {
3531cf294f24SJiri Olsa pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
3532cf294f24SJiri Olsa return -1;
3533cf294f24SJiri Olsa }
3534cf294f24SJiri Olsa
3535cf294f24SJiri Olsa sched->map.color_cpus = map;
3536cf294f24SJiri Olsa return 0;
3537cf294f24SJiri Olsa }
3538cf294f24SJiri Olsa
perf_sched__map(struct perf_sched * sched)353999623c62SJiri Olsa static int perf_sched__map(struct perf_sched *sched)
354099623c62SJiri Olsa {
3541ef76a5afSYang Jihong int rc = -1;
3542ef76a5afSYang Jihong
35435e895278SYang Jihong sched->curr_thread = calloc(MAX_CPUS, sizeof(*(sched->curr_thread)));
35445e895278SYang Jihong if (!sched->curr_thread)
3545ef76a5afSYang Jihong return rc;
354699623c62SJiri Olsa
35473116d609SMadadi Vineeth Reddy sched->curr_out_thread = calloc(MAX_CPUS, sizeof(*(sched->curr_out_thread)));
35483116d609SMadadi Vineeth Reddy if (!sched->curr_out_thread)
35493116d609SMadadi Vineeth Reddy return rc;
35503116d609SMadadi Vineeth Reddy
3551bd2cdf26SYang Jihong if (setup_cpus_switch_event(sched))
35525e895278SYang Jihong goto out_free_curr_thread;
35535e895278SYang Jihong
3554bd2cdf26SYang Jihong if (setup_map_cpus(sched))
3555bd2cdf26SYang Jihong goto out_free_cpus_switch_event;
3556bd2cdf26SYang Jihong
3557a151a37aSJiri Olsa if (setup_color_pids(sched))
3558ef76a5afSYang Jihong goto out_put_map_cpus;
3559a151a37aSJiri Olsa
3560cf294f24SJiri Olsa if (setup_color_cpus(sched))
3561ef76a5afSYang Jihong goto out_put_color_pids;
3562cf294f24SJiri Olsa
35630ec04e16SIngo Molnar setup_pager();
3564ae536acfSArnaldo Carvalho de Melo if (perf_sched__read_events(sched))
3565ef76a5afSYang Jihong goto out_put_color_cpus;
3566ef76a5afSYang Jihong
3567ef76a5afSYang Jihong rc = 0;
35680e9b07e5SArnaldo Carvalho de Melo print_bad_events(sched);
3569ef76a5afSYang Jihong
3570ef76a5afSYang Jihong out_put_color_cpus:
3571ef76a5afSYang Jihong perf_cpu_map__put(sched->map.color_cpus);
3572ef76a5afSYang Jihong
3573ef76a5afSYang Jihong out_put_color_pids:
3574ef76a5afSYang Jihong perf_thread_map__put(sched->map.color_pids);
3575ef76a5afSYang Jihong
3576ef76a5afSYang Jihong out_put_map_cpus:
3577ef76a5afSYang Jihong zfree(&sched->map.comp_cpus);
3578ef76a5afSYang Jihong perf_cpu_map__put(sched->map.cpus);
35795e895278SYang Jihong
3580bd2cdf26SYang Jihong out_free_cpus_switch_event:
3581bd2cdf26SYang Jihong free_cpus_switch_event(sched);
3582bd2cdf26SYang Jihong
35835e895278SYang Jihong out_free_curr_thread:
35845e895278SYang Jihong zfree(&sched->curr_thread);
3585ef76a5afSYang Jihong return rc;
35860ec04e16SIngo Molnar }
35870ec04e16SIngo Molnar
perf_sched__replay(struct perf_sched * sched)35880e9b07e5SArnaldo Carvalho de Melo static int perf_sched__replay(struct perf_sched *sched)
35890ec04e16SIngo Molnar {
3590c6907863SYang Jihong int ret;
35910ec04e16SIngo Molnar unsigned long i;
35920ec04e16SIngo Molnar
3593c6907863SYang Jihong mutex_init(&sched->start_work_mutex);
3594c6907863SYang Jihong mutex_init(&sched->work_done_wait_mutex);
3595c6907863SYang Jihong
3596bd2cdf26SYang Jihong ret = setup_cpus_switch_event(sched);
3597bd2cdf26SYang Jihong if (ret)
3598bd2cdf26SYang Jihong goto out_mutex_destroy;
3599bd2cdf26SYang Jihong
36000e9b07e5SArnaldo Carvalho de Melo calibrate_run_measurement_overhead(sched);
36010e9b07e5SArnaldo Carvalho de Melo calibrate_sleep_measurement_overhead(sched);
36020ec04e16SIngo Molnar
36030e9b07e5SArnaldo Carvalho de Melo test_calibrations(sched);
36040ec04e16SIngo Molnar
3605c6907863SYang Jihong ret = perf_sched__read_events(sched);
3606c6907863SYang Jihong if (ret)
3607bd2cdf26SYang Jihong goto out_free_cpus_switch_event;
36080ec04e16SIngo Molnar
36090e9b07e5SArnaldo Carvalho de Melo printf("nr_run_events: %ld\n", sched->nr_run_events);
36100e9b07e5SArnaldo Carvalho de Melo printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
36110e9b07e5SArnaldo Carvalho de Melo printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events);
36120ec04e16SIngo Molnar
36130e9b07e5SArnaldo Carvalho de Melo if (sched->targetless_wakeups)
36140e9b07e5SArnaldo Carvalho de Melo printf("target-less wakeups: %ld\n", sched->targetless_wakeups);
36150e9b07e5SArnaldo Carvalho de Melo if (sched->multitarget_wakeups)
36160e9b07e5SArnaldo Carvalho de Melo printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups);
36170e9b07e5SArnaldo Carvalho de Melo if (sched->nr_run_events_optimized)
36180ec04e16SIngo Molnar printf("run atoms optimized: %ld\n",
36190e9b07e5SArnaldo Carvalho de Melo sched->nr_run_events_optimized);
36200ec04e16SIngo Molnar
36210e9b07e5SArnaldo Carvalho de Melo print_task_traces(sched);
36220e9b07e5SArnaldo Carvalho de Melo add_cross_task_wakeups(sched);
36230ec04e16SIngo Molnar
362459c26660SIan Rogers sched->thread_funcs_exit = false;
36250e9b07e5SArnaldo Carvalho de Melo create_tasks(sched);
36260ec04e16SIngo Molnar printf("------------------------------------------------------------\n");
3627a7cacaa0SMadadi Vineeth Reddy if (sched->replay_repeat == 0)
3628a7cacaa0SMadadi Vineeth Reddy sched->replay_repeat = UINT_MAX;
3629a7cacaa0SMadadi Vineeth Reddy
36300e9b07e5SArnaldo Carvalho de Melo for (i = 0; i < sched->replay_repeat; i++)
36310e9b07e5SArnaldo Carvalho de Melo run_one_test(sched);
3632a116e05dSArnaldo Carvalho de Melo
363359c26660SIan Rogers sched->thread_funcs_exit = true;
3634165da802SNamhyung Kim destroy_tasks(sched);
3635c6907863SYang Jihong
3636bd2cdf26SYang Jihong out_free_cpus_switch_event:
3637bd2cdf26SYang Jihong free_cpus_switch_event(sched);
3638bd2cdf26SYang Jihong
3639c6907863SYang Jihong out_mutex_destroy:
3640c6907863SYang Jihong mutex_destroy(&sched->start_work_mutex);
3641c6907863SYang Jihong mutex_destroy(&sched->work_done_wait_mutex);
3642c6907863SYang Jihong return ret;
36430ec04e16SIngo Molnar }
36440ec04e16SIngo Molnar
setup_sorting(struct perf_sched * sched,const struct option * options,const char * const usage_msg[])36450e9b07e5SArnaldo Carvalho de Melo static void setup_sorting(struct perf_sched *sched, const struct option *options,
36460e9b07e5SArnaldo Carvalho de Melo const char * const usage_msg[])
3647daa1d7a5SFrederic Weisbecker {
36480e9b07e5SArnaldo Carvalho de Melo char *tmp, *tok, *str = strdup(sched->sort_order);
3649daa1d7a5SFrederic Weisbecker
3650daa1d7a5SFrederic Weisbecker for (tok = strtok_r(str, ", ", &tmp);
3651daa1d7a5SFrederic Weisbecker tok; tok = strtok_r(NULL, ", ", &tmp)) {
36520e9b07e5SArnaldo Carvalho de Melo if (sort_dimension__add(tok, &sched->sort_list) < 0) {
3653c7118369SNamhyung Kim usage_with_options_msg(usage_msg, options,
3654c7118369SNamhyung Kim "Unknown --sort key: `%s'", tok);
3655daa1d7a5SFrederic Weisbecker }
3656daa1d7a5SFrederic Weisbecker }
3657daa1d7a5SFrederic Weisbecker
3658daa1d7a5SFrederic Weisbecker free(str);
3659daa1d7a5SFrederic Weisbecker
36600e9b07e5SArnaldo Carvalho de Melo sort_dimension__add("pid", &sched->cmp_pid);
3661daa1d7a5SFrederic Weisbecker }
3662daa1d7a5SFrederic Weisbecker
schedstat_events_exposed(void)3663b0f00855SYang Jihong static bool schedstat_events_exposed(void)
3664b0f00855SYang Jihong {
3665b0f00855SYang Jihong /*
3666b0f00855SYang Jihong * Select "sched:sched_stat_wait" event to check
3667b0f00855SYang Jihong * whether schedstat tracepoints are exposed.
3668b0f00855SYang Jihong */
3669b0f00855SYang Jihong return IS_ERR(trace_event__tp_format("sched", "sched_stat_wait")) ?
3670b0f00855SYang Jihong false : true;
3671b0f00855SYang Jihong }
3672b0f00855SYang Jihong
__cmd_record(int argc,const char ** argv)36730e9b07e5SArnaldo Carvalho de Melo static int __cmd_record(int argc, const char **argv)
36740e9b07e5SArnaldo Carvalho de Melo {
36750e9b07e5SArnaldo Carvalho de Melo unsigned int rec_argc, i, j;
3676d72e5cf3SIan Rogers char **rec_argv;
3677d72e5cf3SIan Rogers const char **rec_argv_copy;
36780e9b07e5SArnaldo Carvalho de Melo const char * const record_args[] = {
36791fc35b29SIngo Molnar "record",
36801fc35b29SIngo Molnar "-a",
36811fc35b29SIngo Molnar "-R",
3682dc02bf71SIngo Molnar "-m", "1024",
36831fc35b29SIngo Molnar "-c", "1",
36849710118bSStephane Eranian "-e", "sched:sched_switch",
36859710118bSStephane Eranian "-e", "sched:sched_stat_runtime",
36869710118bSStephane Eranian "-e", "sched:sched_process_fork",
36877fff9597SDongsheng "-e", "sched:sched_wakeup_new",
36889710118bSStephane Eranian "-e", "sched:sched_migrate_task",
36891fc35b29SIngo Molnar };
3690b0f00855SYang Jihong
3691b0f00855SYang Jihong /*
3692b0f00855SYang Jihong * The tracepoints trace_sched_stat_{wait, sleep, iowait}
3693b0f00855SYang Jihong * are not exposed to user if CONFIG_SCHEDSTATS is not set,
3694b0f00855SYang Jihong * to prevent "perf sched record" execution failure, determine
3695b0f00855SYang Jihong * whether to record schedstat events according to actual situation.
3696b0f00855SYang Jihong */
3697b0f00855SYang Jihong const char * const schedstat_args[] = {
3698b0f00855SYang Jihong "-e", "sched:sched_stat_wait",
3699b0f00855SYang Jihong "-e", "sched:sched_stat_sleep",
3700b0f00855SYang Jihong "-e", "sched:sched_stat_iowait",
3701b0f00855SYang Jihong };
3702b0f00855SYang Jihong unsigned int schedstat_argc = schedstat_events_exposed() ?
3703b0f00855SYang Jihong ARRAY_SIZE(schedstat_args) : 0;
3704b0f00855SYang Jihong
3705d566a9c2SDavid Ahern struct tep_event *waking_event;
3706d72e5cf3SIan Rogers int ret;
37071fc35b29SIngo Molnar
3708d566a9c2SDavid Ahern /*
3709d566a9c2SDavid Ahern * +2 for either "-e", "sched:sched_wakeup" or
3710d566a9c2SDavid Ahern * "-e", "sched:sched_waking"
3711d566a9c2SDavid Ahern */
3712b0f00855SYang Jihong rec_argc = ARRAY_SIZE(record_args) + 2 + schedstat_argc + argc - 1;
37131fc35b29SIngo Molnar rec_argv = calloc(rec_argc + 1, sizeof(char *));
3714e462dc55SArnaldo Carvalho de Melo if (rec_argv == NULL)
3715ce47dc56SChris Samuel return -ENOMEM;
3716d72e5cf3SIan Rogers rec_argv_copy = calloc(rec_argc + 1, sizeof(char *));
3717d72e5cf3SIan Rogers if (rec_argv_copy == NULL) {
3718d72e5cf3SIan Rogers free(rec_argv);
3719d72e5cf3SIan Rogers return -ENOMEM;
3720d72e5cf3SIan Rogers }
3721ce47dc56SChris Samuel
37221fc35b29SIngo Molnar for (i = 0; i < ARRAY_SIZE(record_args); i++)
37231fc35b29SIngo Molnar rec_argv[i] = strdup(record_args[i]);
37241fc35b29SIngo Molnar
3725d72e5cf3SIan Rogers rec_argv[i++] = strdup("-e");
3726d566a9c2SDavid Ahern waking_event = trace_event__tp_format("sched", "sched_waking");
3727d566a9c2SDavid Ahern if (!IS_ERR(waking_event))
3728d566a9c2SDavid Ahern rec_argv[i++] = strdup("sched:sched_waking");
3729d566a9c2SDavid Ahern else
3730d566a9c2SDavid Ahern rec_argv[i++] = strdup("sched:sched_wakeup");
3731d566a9c2SDavid Ahern
3732b0f00855SYang Jihong for (j = 0; j < schedstat_argc; j++)
3733b0f00855SYang Jihong rec_argv[i++] = strdup(schedstat_args[j]);
3734b0f00855SYang Jihong
37351fc35b29SIngo Molnar for (j = 1; j < (unsigned int)argc; j++, i++)
3736d72e5cf3SIan Rogers rec_argv[i] = strdup(argv[j]);
37371fc35b29SIngo Molnar
37381fc35b29SIngo Molnar BUG_ON(i != rec_argc);
37391fc35b29SIngo Molnar
3740d72e5cf3SIan Rogers memcpy(rec_argv_copy, rec_argv, sizeof(char *) * rec_argc);
3741d72e5cf3SIan Rogers ret = cmd_record(rec_argc, rec_argv_copy);
3742d72e5cf3SIan Rogers
3743d72e5cf3SIan Rogers for (i = 0; i < rec_argc; i++)
3744d72e5cf3SIan Rogers free(rec_argv[i]);
3745d72e5cf3SIan Rogers free(rec_argv);
3746d72e5cf3SIan Rogers free(rec_argv_copy);
3747d72e5cf3SIan Rogers
3748d72e5cf3SIan Rogers return ret;
37491fc35b29SIngo Molnar }
37501fc35b29SIngo Molnar
cmd_sched(int argc,const char ** argv)3751b0ad8ea6SArnaldo Carvalho de Melo int cmd_sched(int argc, const char **argv)
37528a39df8fSAdrian Hunter {
375349b8e2beSRasmus Villemoes static const char default_sort_order[] = "avg, max, switch, runtime";
37548a39df8fSAdrian Hunter struct perf_sched sched = {
37550e9b07e5SArnaldo Carvalho de Melo .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid),
37560e9b07e5SArnaldo Carvalho de Melo .sort_list = LIST_HEAD_INIT(sched.sort_list),
37570e9b07e5SArnaldo Carvalho de Melo .sort_order = default_sort_order,
37580e9b07e5SArnaldo Carvalho de Melo .replay_repeat = 10,
37590e9b07e5SArnaldo Carvalho de Melo .profile_cpu = -1,
37600e9b07e5SArnaldo Carvalho de Melo .next_shortname1 = 'A',
37610e9b07e5SArnaldo Carvalho de Melo .next_shortname2 = '0',
37622f80dd44SJosef Bacik .skip_merge = 0,
37636c973c90SDavid Ahern .show_callchain = 1,
37646c973c90SDavid Ahern .max_stack = 5,
37650e9b07e5SArnaldo Carvalho de Melo };
376677f02f44SNamhyung Kim const struct option sched_options[] = {
376777f02f44SNamhyung Kim OPT_STRING('i', "input", &input_name, "file",
376877f02f44SNamhyung Kim "input file name"),
376977f02f44SNamhyung Kim OPT_INCR('v', "verbose", &verbose,
377077f02f44SNamhyung Kim "be more verbose (show symbol address, etc)"),
377177f02f44SNamhyung Kim OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
377277f02f44SNamhyung Kim "dump raw trace in ASCII"),
37736fa94258SNamhyung Kim OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
377477f02f44SNamhyung Kim OPT_END()
377577f02f44SNamhyung Kim };
37760e9b07e5SArnaldo Carvalho de Melo const struct option latency_options[] = {
37770e9b07e5SArnaldo Carvalho de Melo OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]",
37780e9b07e5SArnaldo Carvalho de Melo "sort by key(s): runtime, switch, avg, max"),
37790e9b07e5SArnaldo Carvalho de Melo OPT_INTEGER('C', "CPU", &sched.profile_cpu,
37800e9b07e5SArnaldo Carvalho de Melo "CPU to profile on"),
37812f80dd44SJosef Bacik OPT_BOOLEAN('p', "pids", &sched.skip_merge,
37822f80dd44SJosef Bacik "latency stats per pid instead of per comm"),
378377f02f44SNamhyung Kim OPT_PARENT(sched_options)
37840e9b07e5SArnaldo Carvalho de Melo };
37850e9b07e5SArnaldo Carvalho de Melo const struct option replay_options[] = {
37860e9b07e5SArnaldo Carvalho de Melo OPT_UINTEGER('r', "repeat", &sched.replay_repeat,
3787a7cacaa0SMadadi Vineeth Reddy "repeat the workload replay N times (0: infinite)"),
378877f02f44SNamhyung Kim OPT_PARENT(sched_options)
37890e9b07e5SArnaldo Carvalho de Melo };
379099623c62SJiri Olsa const struct option map_options[] = {
379199623c62SJiri Olsa OPT_BOOLEAN(0, "compact", &sched.map.comp,
379299623c62SJiri Olsa "map output in compact mode"),
3793a151a37aSJiri Olsa OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids",
3794a151a37aSJiri Olsa "highlight given pids in map"),
3795cf294f24SJiri Olsa OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus",
3796cf294f24SJiri Olsa "highlight given CPUs in map"),
379773643bb6SJiri Olsa OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus",
379873643bb6SJiri Olsa "display given CPUs in map"),
37993116d609SMadadi Vineeth Reddy OPT_STRING(0, "task-name", &sched.map.task_name, "task",
38009cc0afedSMadadi Vineeth Reddy "map output only for the given task name(s)."),
3801306f921eSMadadi Vineeth Reddy OPT_BOOLEAN(0, "fuzzy-name", &sched.map.fuzzy,
3802306f921eSMadadi Vineeth Reddy "given command name can be partially matched (fuzzy matching)"),
380377f02f44SNamhyung Kim OPT_PARENT(sched_options)
380499623c62SJiri Olsa };
380549394a2aSDavid Ahern const struct option timehist_options[] = {
380649394a2aSDavid Ahern OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
380749394a2aSDavid Ahern "file", "vmlinux pathname"),
380849394a2aSDavid Ahern OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
380949394a2aSDavid Ahern "file", "kallsyms pathname"),
38106c973c90SDavid Ahern OPT_BOOLEAN('g', "call-graph", &sched.show_callchain,
38116c973c90SDavid Ahern "Display call chains if present (default on)"),
38126c973c90SDavid Ahern OPT_UINTEGER(0, "max-stack", &sched.max_stack,
38136c973c90SDavid Ahern "Maximum number of functions to display backtrace."),
381449394a2aSDavid Ahern OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
381549394a2aSDavid Ahern "Look for files with symbols relative to this directory"),
381652df138cSDavid Ahern OPT_BOOLEAN('s', "summary", &sched.summary_only,
381752df138cSDavid Ahern "Show only syscall summary with statistics"),
381852df138cSDavid Ahern OPT_BOOLEAN('S', "with-summary", &sched.summary,
381952df138cSDavid Ahern "Show all syscalls and summary with statistics"),
3820fc1469f1SDavid Ahern OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"),
3821292c4a8fSBrendan Gregg OPT_BOOLEAN('n', "next", &sched.show_next, "Show next task"),
3822350f54faSDavid Ahern OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"),
3823a407b067SDavid Ahern OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"),
382407235f84SNamhyung Kim OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"),
3825853b7407SDavid Ahern OPT_STRING(0, "time", &sched.time_str, "str",
3826853b7407SDavid Ahern "Time span for analysis (start,stop)"),
3827414e050cSNamhyung Kim OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"),
38280f59d7a3SDavid Ahern OPT_STRING('p', "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
38290f59d7a3SDavid Ahern "analyze events only for given process id(s)"),
38300f59d7a3SDavid Ahern OPT_STRING('t', "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
38310f59d7a3SDavid Ahern "analyze events only for given thread id(s)"),
3832c30d630dSDavid Ahern OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
38333fcd7409SYang Jihong OPT_BOOLEAN(0, "show-prio", &sched.show_prio, "Show task priority"),
38349b3a48bbSYang Jihong OPT_STRING(0, "prio", &sched.prio_str, "prio",
38359b3a48bbSYang Jihong "analyze events only for given task priority(ies)"),
383649394a2aSDavid Ahern OPT_PARENT(sched_options)
383749394a2aSDavid Ahern };
383849394a2aSDavid Ahern
38390e9b07e5SArnaldo Carvalho de Melo const char * const latency_usage[] = {
38400e9b07e5SArnaldo Carvalho de Melo "perf sched latency [<options>]",
38410e9b07e5SArnaldo Carvalho de Melo NULL
38420e9b07e5SArnaldo Carvalho de Melo };
38430e9b07e5SArnaldo Carvalho de Melo const char * const replay_usage[] = {
38440e9b07e5SArnaldo Carvalho de Melo "perf sched replay [<options>]",
38450e9b07e5SArnaldo Carvalho de Melo NULL
38460e9b07e5SArnaldo Carvalho de Melo };
384799623c62SJiri Olsa const char * const map_usage[] = {
384899623c62SJiri Olsa "perf sched map [<options>]",
384999623c62SJiri Olsa NULL
385099623c62SJiri Olsa };
385149394a2aSDavid Ahern const char * const timehist_usage[] = {
385249394a2aSDavid Ahern "perf sched timehist [<options>]",
385349394a2aSDavid Ahern NULL
385449394a2aSDavid Ahern };
3855a83edb2dSRamkumar Ramachandra const char *const sched_subcommands[] = { "record", "latency", "map",
385649394a2aSDavid Ahern "replay", "script",
385749394a2aSDavid Ahern "timehist", NULL };
3858a83edb2dSRamkumar Ramachandra const char *sched_usage[] = {
3859a83edb2dSRamkumar Ramachandra NULL,
38600e9b07e5SArnaldo Carvalho de Melo NULL
38610e9b07e5SArnaldo Carvalho de Melo };
38620e9b07e5SArnaldo Carvalho de Melo struct trace_sched_handler lat_ops = {
38630e9b07e5SArnaldo Carvalho de Melo .wakeup_event = latency_wakeup_event,
38640e9b07e5SArnaldo Carvalho de Melo .switch_event = latency_switch_event,
38650e9b07e5SArnaldo Carvalho de Melo .runtime_event = latency_runtime_event,
38660e9b07e5SArnaldo Carvalho de Melo .migrate_task_event = latency_migrate_task_event,
38670e9b07e5SArnaldo Carvalho de Melo };
38680e9b07e5SArnaldo Carvalho de Melo struct trace_sched_handler map_ops = {
38690e9b07e5SArnaldo Carvalho de Melo .switch_event = map_switch_event,
38700e9b07e5SArnaldo Carvalho de Melo };
38710e9b07e5SArnaldo Carvalho de Melo struct trace_sched_handler replay_ops = {
38720e9b07e5SArnaldo Carvalho de Melo .wakeup_event = replay_wakeup_event,
38730e9b07e5SArnaldo Carvalho de Melo .switch_event = replay_switch_event,
38740e9b07e5SArnaldo Carvalho de Melo .fork_event = replay_fork_event,
38750e9b07e5SArnaldo Carvalho de Melo };
3876bd2cdf26SYang Jihong int ret;
38770e9b07e5SArnaldo Carvalho de Melo
387841860d49SIan Rogers perf_tool__init(&sched.tool, /*ordered_events=*/true);
387941860d49SIan Rogers sched.tool.sample = perf_sched__process_tracepoint_sample;
388041860d49SIan Rogers sched.tool.comm = perf_sched__process_comm;
388141860d49SIan Rogers sched.tool.namespaces = perf_event__process_namespaces;
388241860d49SIan Rogers sched.tool.lost = perf_event__process_lost;
388341860d49SIan Rogers sched.tool.fork = perf_sched__process_fork_event;
388441860d49SIan Rogers
3885a83edb2dSRamkumar Ramachandra argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
3886a83edb2dSRamkumar Ramachandra sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
3887f2858d8aSIngo Molnar if (!argc)
3888f2858d8aSIngo Molnar usage_with_options(sched_usage, sched_options);
3889f2858d8aSIngo Molnar
3890c0777c5aSXiao Guangrong /*
3891133dc4c3SIngo Molnar * Aliased to 'perf script' for now:
3892c0777c5aSXiao Guangrong */
38930bd14ac2SIan Rogers if (!strcmp(argv[0], "script")) {
3894bd2cdf26SYang Jihong return cmd_script(argc, argv);
38950bd14ac2SIan Rogers } else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3896bd2cdf26SYang Jihong return __cmd_record(argc, argv);
3897628881eeSYang Jihong } else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) {
38980e9b07e5SArnaldo Carvalho de Melo sched.tp_handler = &lat_ops;
3899f2858d8aSIngo Molnar if (argc > 1) {
3900f2858d8aSIngo Molnar argc = parse_options(argc, argv, latency_options, latency_usage, 0);
3901f2858d8aSIngo Molnar if (argc)
3902f2858d8aSIngo Molnar usage_with_options(latency_usage, latency_options);
3903f2858d8aSIngo Molnar }
39040e9b07e5SArnaldo Carvalho de Melo setup_sorting(&sched, latency_options, latency_usage);
3905bd2cdf26SYang Jihong return perf_sched__lat(&sched);
39060ec04e16SIngo Molnar } else if (!strcmp(argv[0], "map")) {
390799623c62SJiri Olsa if (argc) {
3908a151a37aSJiri Olsa argc = parse_options(argc, argv, map_options, map_usage, 0);
390999623c62SJiri Olsa if (argc)
391099623c62SJiri Olsa usage_with_options(map_usage, map_options);
39119cc0afedSMadadi Vineeth Reddy
39129cc0afedSMadadi Vineeth Reddy if (sched.map.task_name) {
39139cc0afedSMadadi Vineeth Reddy sched.map.task_names = strlist__new(sched.map.task_name, NULL);
39149cc0afedSMadadi Vineeth Reddy if (sched.map.task_names == NULL) {
39159cc0afedSMadadi Vineeth Reddy fprintf(stderr, "Failed to parse task names\n");
39169cc0afedSMadadi Vineeth Reddy return -1;
39179cc0afedSMadadi Vineeth Reddy }
39189cc0afedSMadadi Vineeth Reddy }
391999623c62SJiri Olsa }
39200e9b07e5SArnaldo Carvalho de Melo sched.tp_handler = &map_ops;
39210e9b07e5SArnaldo Carvalho de Melo setup_sorting(&sched, latency_options, latency_usage);
3922bd2cdf26SYang Jihong return perf_sched__map(&sched);
3923ae0f4eb3SWei Li } else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
39240e9b07e5SArnaldo Carvalho de Melo sched.tp_handler = &replay_ops;
39250a02ad93SIngo Molnar if (argc) {
3926f2858d8aSIngo Molnar argc = parse_options(argc, argv, replay_options, replay_usage, 0);
3927f2858d8aSIngo Molnar if (argc)
3928f2858d8aSIngo Molnar usage_with_options(replay_usage, replay_options);
3929f2858d8aSIngo Molnar }
3930bd2cdf26SYang Jihong return perf_sched__replay(&sched);
393149394a2aSDavid Ahern } else if (!strcmp(argv[0], "timehist")) {
393249394a2aSDavid Ahern if (argc) {
393349394a2aSDavid Ahern argc = parse_options(argc, argv, timehist_options,
393449394a2aSDavid Ahern timehist_usage, 0);
393549394a2aSDavid Ahern if (argc)
393649394a2aSDavid Ahern usage_with_options(timehist_usage, timehist_options);
393749394a2aSDavid Ahern }
3938292c4a8fSBrendan Gregg if ((sched.show_wakeups || sched.show_next) &&
3939292c4a8fSBrendan Gregg sched.summary_only) {
3940292c4a8fSBrendan Gregg pr_err(" Error: -s and -[n|w] are mutually exclusive.\n");
3941fc1469f1SDavid Ahern parse_options_usage(timehist_usage, timehist_options, "s", true);
3942292c4a8fSBrendan Gregg if (sched.show_wakeups)
3943fc1469f1SDavid Ahern parse_options_usage(NULL, timehist_options, "w", true);
3944292c4a8fSBrendan Gregg if (sched.show_next)
3945292c4a8fSBrendan Gregg parse_options_usage(NULL, timehist_options, "n", true);
3946bd2cdf26SYang Jihong return -EINVAL;
3947fc1469f1SDavid Ahern }
39487cc72553SJames Clark ret = symbol__validate_sym_arguments();
39497cc72553SJames Clark if (ret)
3950bd2cdf26SYang Jihong return ret;
3951fc1469f1SDavid Ahern
3952bd2cdf26SYang Jihong return perf_sched__timehist(&sched);
3953f2858d8aSIngo Molnar } else {
3954f2858d8aSIngo Molnar usage_with_options(sched_usage, sched_options);
39550a02ad93SIngo Molnar }
39560a02ad93SIngo Molnar
3957*1a5efc9eSAditya Gupta /* free usage string allocated by parse_options_subcommand */
3958*1a5efc9eSAditya Gupta free((void *)sched_usage[0]);
3959*1a5efc9eSAditya Gupta
3960bd2cdf26SYang Jihong return 0;
39610a02ad93SIngo Molnar }
3962