Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 2 | #include "perf.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3 | |
| 4 | #include "util/util.h" |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 5 | #include "util/evlist.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 6 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | e3f4260 | 2011-11-16 17:02:54 -0200 | [diff] [blame] | 7 | #include "util/evsel.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 8 | #include "util/symbol.h" |
| 9 | #include "util/thread.h" |
| 10 | #include "util/header.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 11 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 12 | #include "util/tool.h" |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 13 | #include "util/cloexec.h" |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 14 | #include "util/thread_map.h" |
Jiri Olsa | 8cd9119 | 2016-04-12 15:29:27 +0200 | [diff] [blame] | 15 | #include "util/color.h" |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 16 | #include "util/stat.h" |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 17 | #include "util/callchain.h" |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 18 | #include "util/time-utils.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 19 | |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 20 | #include <subcmd/parse-options.h> |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 21 | #include "util/trace-event.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 23 | #include "util/debug.h" |
| 24 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 25 | #include <linux/log2.h> |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 26 | #include <sys/prctl.h> |
Markus Trippelsdorf | 7b78f13 | 2012-04-04 10:45:27 +0200 | [diff] [blame] | 27 | #include <sys/resource.h> |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 28 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 29 | #include <semaphore.h> |
| 30 | #include <pthread.h> |
| 31 | #include <math.h> |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 32 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 33 | #include <linux/time64.h> |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 34 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 35 | #define PR_SET_NAME 15 /* Set process name */ |
| 36 | #define MAX_CPUS 4096 |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 37 | #define COMM_LEN 20 |
| 38 | #define SYM_LEN 129 |
Yunlong Song | a35e27d | 2015-03-31 21:46:29 +0800 | [diff] [blame] | 39 | #define MAX_PID 1024000 |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 40 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 41 | struct sched_atom; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 42 | |
| 43 | struct task_desc { |
| 44 | unsigned long nr; |
| 45 | unsigned long pid; |
| 46 | char comm[COMM_LEN]; |
| 47 | |
| 48 | unsigned long nr_events; |
| 49 | unsigned long curr_event; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 50 | struct sched_atom **atoms; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 51 | |
| 52 | pthread_t thread; |
| 53 | sem_t sleep_sem; |
| 54 | |
| 55 | sem_t ready_for_work; |
| 56 | sem_t work_done_sem; |
| 57 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 58 | u64 cpu_usage; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | enum sched_event_type { |
| 62 | SCHED_EVENT_RUN, |
| 63 | SCHED_EVENT_SLEEP, |
| 64 | SCHED_EVENT_WAKEUP, |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 65 | SCHED_EVENT_MIGRATION, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 68 | struct sched_atom { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 69 | enum sched_event_type type; |
Arnaldo Carvalho de Melo | eed05fe | 2010-04-05 12:53:45 -0300 | [diff] [blame] | 70 | int specific_wait; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 71 | u64 timestamp; |
| 72 | u64 duration; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 73 | unsigned long nr; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 74 | sem_t *wait_sem; |
| 75 | struct task_desc *wakee; |
| 76 | }; |
| 77 | |
Dongsheng | e936e8e | 2014-05-05 16:05:54 +0900 | [diff] [blame] | 78 | #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP" |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 79 | |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 80 | /* task state bitmask, copied from include/linux/sched.h */ |
| 81 | #define TASK_RUNNING 0 |
| 82 | #define TASK_INTERRUPTIBLE 1 |
| 83 | #define TASK_UNINTERRUPTIBLE 2 |
| 84 | #define __TASK_STOPPED 4 |
| 85 | #define __TASK_TRACED 8 |
| 86 | /* in tsk->exit_state */ |
| 87 | #define EXIT_DEAD 16 |
| 88 | #define EXIT_ZOMBIE 32 |
| 89 | #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD) |
| 90 | /* in tsk->state again */ |
| 91 | #define TASK_DEAD 64 |
| 92 | #define TASK_WAKEKILL 128 |
| 93 | #define TASK_WAKING 256 |
| 94 | #define TASK_PARKED 512 |
| 95 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 96 | enum thread_state { |
| 97 | THREAD_SLEEPING = 0, |
| 98 | THREAD_WAIT_CPU, |
| 99 | THREAD_SCHED_IN, |
| 100 | THREAD_IGNORE |
| 101 | }; |
| 102 | |
| 103 | struct work_atom { |
| 104 | struct list_head list; |
| 105 | enum thread_state state; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 106 | u64 sched_out_time; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 107 | u64 wake_up_time; |
| 108 | u64 sched_in_time; |
| 109 | u64 runtime; |
| 110 | }; |
| 111 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 112 | struct work_atoms { |
| 113 | struct list_head work_list; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 114 | struct thread *thread; |
| 115 | struct rb_node node; |
| 116 | u64 max_lat; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 117 | u64 max_lat_at; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 118 | u64 total_lat; |
| 119 | u64 nb_atoms; |
| 120 | u64 total_runtime; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 121 | int num_merged; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 124 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 125 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 126 | struct perf_sched; |
| 127 | |
| 128 | struct trace_sched_handler { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 129 | int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
| 130 | struct perf_sample *sample, struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 131 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 132 | int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
| 133 | struct perf_sample *sample, struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 134 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 135 | int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
| 136 | struct perf_sample *sample, struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 137 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 138 | /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */ |
| 139 | int (*fork_event)(struct perf_sched *sched, union perf_event *event, |
| 140 | struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 141 | |
| 142 | int (*migrate_task_event)(struct perf_sched *sched, |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 143 | struct perf_evsel *evsel, |
| 144 | struct perf_sample *sample, |
| 145 | struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 146 | }; |
| 147 | |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 148 | #define COLOR_PIDS PERF_COLOR_BLUE |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 149 | #define COLOR_CPUS PERF_COLOR_BG_RED |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 150 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 151 | struct perf_sched_map { |
| 152 | DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS); |
| 153 | int *comp_cpus; |
| 154 | bool comp; |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 155 | struct thread_map *color_pids; |
| 156 | const char *color_pids_str; |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 157 | struct cpu_map *color_cpus; |
| 158 | const char *color_cpus_str; |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 159 | struct cpu_map *cpus; |
| 160 | const char *cpus_str; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 163 | struct perf_sched { |
| 164 | struct perf_tool tool; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 165 | const char *sort_order; |
| 166 | unsigned long nr_tasks; |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 167 | struct task_desc **pid_to_task; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 168 | struct task_desc **tasks; |
| 169 | const struct trace_sched_handler *tp_handler; |
| 170 | pthread_mutex_t start_work_mutex; |
| 171 | pthread_mutex_t work_done_wait_mutex; |
| 172 | int profile_cpu; |
| 173 | /* |
| 174 | * Track the current task - that way we can know whether there's any |
| 175 | * weird events, such as a task being switched away that is not current. |
| 176 | */ |
| 177 | int max_cpu; |
| 178 | u32 curr_pid[MAX_CPUS]; |
| 179 | struct thread *curr_thread[MAX_CPUS]; |
| 180 | char next_shortname1; |
| 181 | char next_shortname2; |
| 182 | unsigned int replay_repeat; |
| 183 | unsigned long nr_run_events; |
| 184 | unsigned long nr_sleep_events; |
| 185 | unsigned long nr_wakeup_events; |
| 186 | unsigned long nr_sleep_corrections; |
| 187 | unsigned long nr_run_events_optimized; |
| 188 | unsigned long targetless_wakeups; |
| 189 | unsigned long multitarget_wakeups; |
| 190 | unsigned long nr_runs; |
| 191 | unsigned long nr_timestamps; |
| 192 | unsigned long nr_unordered_timestamps; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 193 | unsigned long nr_context_switch_bugs; |
| 194 | unsigned long nr_events; |
| 195 | unsigned long nr_lost_chunks; |
| 196 | unsigned long nr_lost_events; |
| 197 | u64 run_measurement_overhead; |
| 198 | u64 sleep_measurement_overhead; |
| 199 | u64 start_time; |
| 200 | u64 cpu_usage; |
| 201 | u64 runavg_cpu_usage; |
| 202 | u64 parent_cpu_usage; |
| 203 | u64 runavg_parent_cpu_usage; |
| 204 | u64 sum_runtime; |
| 205 | u64 sum_fluct; |
| 206 | u64 run_avg; |
| 207 | u64 all_runtime; |
| 208 | u64 all_count; |
| 209 | u64 cpu_last_switched[MAX_CPUS]; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 210 | struct rb_root atom_root, sorted_atom_root, merged_atom_root; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 211 | struct list_head sort_list, cmp_pid; |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 212 | bool force; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 213 | bool skip_merge; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 214 | struct perf_sched_map map; |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 215 | |
| 216 | /* options for timehist command */ |
| 217 | bool summary; |
| 218 | bool summary_only; |
Namhyung Kim | 699b5b9 | 2016-12-08 23:47:52 +0900 | [diff] [blame] | 219 | bool idle_hist; |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 220 | bool show_callchain; |
| 221 | unsigned int max_stack; |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 222 | bool show_cpu_visual; |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 223 | bool show_wakeups; |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 224 | bool show_migrations; |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 225 | bool show_state; |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 226 | u64 skipped_samples; |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 227 | const char *time_str; |
| 228 | struct perf_time_interval ptime; |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 229 | struct perf_time_interval hist_time; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 230 | }; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 231 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 232 | /* per thread run time data */ |
| 233 | struct thread_runtime { |
| 234 | u64 last_time; /* time of previous sched in/out event */ |
| 235 | u64 dt_run; /* run time */ |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 236 | u64 dt_sleep; /* time between CPU access by sleep (off cpu) */ |
| 237 | u64 dt_iowait; /* time between CPU access by iowait (off cpu) */ |
| 238 | u64 dt_preempt; /* time between CPU access by preempt (off cpu) */ |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 239 | u64 dt_delay; /* time between wakeup and sched-in */ |
| 240 | u64 ready_to_run; /* time of wakeup */ |
| 241 | |
| 242 | struct stats run_stats; |
| 243 | u64 total_run_time; |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 244 | |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 245 | int last_state; |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 246 | u64 migrations; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | /* per event run time data */ |
| 250 | struct evsel_runtime { |
| 251 | u64 *last_time; /* time this event was last seen per cpu */ |
| 252 | u32 ncpu; /* highest cpu slot allocated */ |
| 253 | }; |
| 254 | |
Namhyung Kim | 3bc2fa9 | 2016-12-08 23:47:51 +0900 | [diff] [blame] | 255 | /* per cpu idle time data */ |
| 256 | struct idle_thread_runtime { |
| 257 | struct thread_runtime tr; |
| 258 | struct thread *last_thread; |
| 259 | struct rb_root sorted_root; |
| 260 | struct callchain_root callchain; |
| 261 | struct callchain_cursor cursor; |
| 262 | }; |
| 263 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 264 | /* track idle times per cpu */ |
| 265 | static struct thread **idle_threads; |
| 266 | static int idle_max_cpu; |
| 267 | static char idle_comm[] = "<idle>"; |
| 268 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 269 | static u64 get_nsecs(void) |
| 270 | { |
| 271 | struct timespec ts; |
| 272 | |
| 273 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 274 | |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 275 | return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 278 | static void burn_nsecs(struct perf_sched *sched, u64 nsecs) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 279 | { |
| 280 | u64 T0 = get_nsecs(), T1; |
| 281 | |
| 282 | do { |
| 283 | T1 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 284 | } while (T1 + sched->run_measurement_overhead < T0 + nsecs); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void sleep_nsecs(u64 nsecs) |
| 288 | { |
| 289 | struct timespec ts; |
| 290 | |
| 291 | ts.tv_nsec = nsecs % 999999999; |
| 292 | ts.tv_sec = nsecs / 999999999; |
| 293 | |
| 294 | nanosleep(&ts, NULL); |
| 295 | } |
| 296 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 297 | static void calibrate_run_measurement_overhead(struct perf_sched *sched) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 298 | { |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 299 | u64 T0, T1, delta, min_delta = NSEC_PER_SEC; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < 10; i++) { |
| 303 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 304 | burn_nsecs(sched, 0); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 305 | T1 = get_nsecs(); |
| 306 | delta = T1-T0; |
| 307 | min_delta = min(min_delta, delta); |
| 308 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 309 | sched->run_measurement_overhead = min_delta; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 310 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 311 | printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 314 | static void calibrate_sleep_measurement_overhead(struct perf_sched *sched) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 315 | { |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 316 | u64 T0, T1, delta, min_delta = NSEC_PER_SEC; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 317 | int i; |
| 318 | |
| 319 | for (i = 0; i < 10; i++) { |
| 320 | T0 = get_nsecs(); |
| 321 | sleep_nsecs(10000); |
| 322 | T1 = get_nsecs(); |
| 323 | delta = T1-T0; |
| 324 | min_delta = min(min_delta, delta); |
| 325 | } |
| 326 | min_delta -= 10000; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 327 | sched->sleep_measurement_overhead = min_delta; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 328 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 329 | printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 330 | } |
| 331 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 332 | static struct sched_atom * |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 333 | get_new_event(struct task_desc *task, u64 timestamp) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 334 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 335 | struct sched_atom *event = zalloc(sizeof(*event)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 336 | unsigned long idx = task->nr_events; |
| 337 | size_t size; |
| 338 | |
| 339 | event->timestamp = timestamp; |
| 340 | event->nr = idx; |
| 341 | |
| 342 | task->nr_events++; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 343 | size = sizeof(struct sched_atom *) * task->nr_events; |
| 344 | task->atoms = realloc(task->atoms, size); |
| 345 | BUG_ON(!task->atoms); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 346 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 347 | task->atoms[idx] = event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 348 | |
| 349 | return event; |
| 350 | } |
| 351 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 352 | static struct sched_atom *last_event(struct task_desc *task) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 353 | { |
| 354 | if (!task->nr_events) |
| 355 | return NULL; |
| 356 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 357 | return task->atoms[task->nr_events - 1]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 360 | static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task, |
| 361 | u64 timestamp, u64 duration) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 362 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 363 | struct sched_atom *event, *curr_event = last_event(task); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 364 | |
| 365 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 366 | * optimize an existing RUN event by merging this one |
| 367 | * to it: |
| 368 | */ |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 369 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 370 | sched->nr_run_events_optimized++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 371 | curr_event->duration += duration; |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | event = get_new_event(task, timestamp); |
| 376 | |
| 377 | event->type = SCHED_EVENT_RUN; |
| 378 | event->duration = duration; |
| 379 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 380 | sched->nr_run_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 383 | static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task, |
| 384 | u64 timestamp, struct task_desc *wakee) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 385 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 386 | struct sched_atom *event, *wakee_event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 387 | |
| 388 | event = get_new_event(task, timestamp); |
| 389 | event->type = SCHED_EVENT_WAKEUP; |
| 390 | event->wakee = wakee; |
| 391 | |
| 392 | wakee_event = last_event(wakee); |
| 393 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 394 | sched->targetless_wakeups++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | if (wakee_event->wait_sem) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 398 | sched->multitarget_wakeups++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 402 | wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 403 | sem_init(wakee_event->wait_sem, 0, 0); |
| 404 | wakee_event->specific_wait = 1; |
| 405 | event->wait_sem = wakee_event->wait_sem; |
| 406 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 407 | sched->nr_wakeup_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 410 | static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task, |
| 411 | u64 timestamp, u64 task_state __maybe_unused) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 412 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 413 | struct sched_atom *event = get_new_event(task, timestamp); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 414 | |
| 415 | event->type = SCHED_EVENT_SLEEP; |
| 416 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 417 | sched->nr_sleep_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 420 | static struct task_desc *register_pid(struct perf_sched *sched, |
| 421 | unsigned long pid, const char *comm) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 422 | { |
| 423 | struct task_desc *task; |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 424 | static int pid_max; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 425 | |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 426 | if (sched->pid_to_task == NULL) { |
| 427 | if (sysctl__read_int("kernel/pid_max", &pid_max) < 0) |
| 428 | pid_max = MAX_PID; |
| 429 | BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); |
| 430 | } |
Yunlong Song | 3a423a5 | 2015-03-31 21:46:31 +0800 | [diff] [blame] | 431 | if (pid >= (unsigned long)pid_max) { |
| 432 | BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * |
| 433 | sizeof(struct task_desc *))) == NULL); |
| 434 | while (pid >= (unsigned long)pid_max) |
| 435 | sched->pid_to_task[pid_max++] = NULL; |
| 436 | } |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 437 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 438 | task = sched->pid_to_task[pid]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 439 | |
| 440 | if (task) |
| 441 | return task; |
| 442 | |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 443 | task = zalloc(sizeof(*task)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 444 | task->pid = pid; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 445 | task->nr = sched->nr_tasks; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 446 | strcpy(task->comm, comm); |
| 447 | /* |
| 448 | * every task starts in sleeping state - this gets ignored |
| 449 | * if there's no wakeup pointing to this sleep state: |
| 450 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 451 | add_sched_event_sleep(sched, task, 0, 0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 452 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 453 | sched->pid_to_task[pid] = task; |
| 454 | sched->nr_tasks++; |
Yunlong Song | 0755bc4 | 2015-03-31 21:46:28 +0800 | [diff] [blame] | 455 | sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *)); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 456 | BUG_ON(!sched->tasks); |
| 457 | sched->tasks[task->nr] = task; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 458 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 459 | if (verbose) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 460 | printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 461 | |
| 462 | return task; |
| 463 | } |
| 464 | |
| 465 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 466 | static void print_task_traces(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 467 | { |
| 468 | struct task_desc *task; |
| 469 | unsigned long i; |
| 470 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 471 | for (i = 0; i < sched->nr_tasks; i++) { |
| 472 | task = sched->tasks[i]; |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 473 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 474 | task->nr, task->comm, task->pid, task->nr_events); |
| 475 | } |
| 476 | } |
| 477 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 478 | static void add_cross_task_wakeups(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 479 | { |
| 480 | struct task_desc *task1, *task2; |
| 481 | unsigned long i, j; |
| 482 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 483 | for (i = 0; i < sched->nr_tasks; i++) { |
| 484 | task1 = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 485 | j = i + 1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 486 | if (j == sched->nr_tasks) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 487 | j = 0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 488 | task2 = sched->tasks[j]; |
| 489 | add_sched_event_wakeup(sched, task1, 0, task2); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 493 | static void perf_sched__process_event(struct perf_sched *sched, |
| 494 | struct sched_atom *atom) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 495 | { |
| 496 | int ret = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 497 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 498 | switch (atom->type) { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 499 | case SCHED_EVENT_RUN: |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 500 | burn_nsecs(sched, atom->duration); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 501 | break; |
| 502 | case SCHED_EVENT_SLEEP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 503 | if (atom->wait_sem) |
| 504 | ret = sem_wait(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 505 | BUG_ON(ret); |
| 506 | break; |
| 507 | case SCHED_EVENT_WAKEUP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 508 | if (atom->wait_sem) |
| 509 | ret = sem_post(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 510 | BUG_ON(ret); |
| 511 | break; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 512 | case SCHED_EVENT_MIGRATION: |
| 513 | break; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 514 | default: |
| 515 | BUG_ON(1); |
| 516 | } |
| 517 | } |
| 518 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 519 | static u64 get_cpu_usage_nsec_parent(void) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 520 | { |
| 521 | struct rusage ru; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 522 | u64 sum; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 523 | int err; |
| 524 | |
| 525 | err = getrusage(RUSAGE_SELF, &ru); |
| 526 | BUG_ON(err); |
| 527 | |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 528 | sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC; |
| 529 | sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 530 | |
| 531 | return sum; |
| 532 | } |
| 533 | |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 534 | static int self_open_counters(struct perf_sched *sched, unsigned long cur_task) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 535 | { |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 536 | struct perf_event_attr attr; |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 537 | char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE]; |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 538 | int fd; |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 539 | struct rlimit limit; |
| 540 | bool need_privilege = false; |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 541 | |
| 542 | memset(&attr, 0, sizeof(attr)); |
| 543 | |
| 544 | attr.type = PERF_TYPE_SOFTWARE; |
| 545 | attr.config = PERF_COUNT_SW_TASK_CLOCK; |
| 546 | |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 547 | force_again: |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 548 | fd = sys_perf_event_open(&attr, 0, -1, -1, |
| 549 | perf_event_open_cloexec_flag()); |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 550 | |
Yunlong Song | 1aff59b | 2015-03-31 21:46:33 +0800 | [diff] [blame] | 551 | if (fd < 0) { |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 552 | if (errno == EMFILE) { |
| 553 | if (sched->force) { |
| 554 | BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); |
| 555 | limit.rlim_cur += sched->nr_tasks - cur_task; |
| 556 | if (limit.rlim_cur > limit.rlim_max) { |
| 557 | limit.rlim_max = limit.rlim_cur; |
| 558 | need_privilege = true; |
| 559 | } |
| 560 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 561 | if (need_privilege && errno == EPERM) |
| 562 | strcpy(info, "Need privilege\n"); |
| 563 | } else |
| 564 | goto force_again; |
| 565 | } else |
| 566 | strcpy(info, "Have a try with -f option\n"); |
| 567 | } |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 568 | pr_err("Error: sys_perf_event_open() syscall returned " |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 569 | "with %d (%s)\n%s", fd, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 570 | str_error_r(errno, sbuf, sizeof(sbuf)), info); |
Yunlong Song | 1aff59b | 2015-03-31 21:46:33 +0800 | [diff] [blame] | 571 | exit(EXIT_FAILURE); |
| 572 | } |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 573 | return fd; |
| 574 | } |
| 575 | |
| 576 | static u64 get_cpu_usage_nsec_self(int fd) |
| 577 | { |
| 578 | u64 runtime; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 579 | int ret; |
| 580 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 581 | ret = read(fd, &runtime, sizeof(runtime)); |
| 582 | BUG_ON(ret != sizeof(runtime)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 583 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 584 | return runtime; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 587 | struct sched_thread_parms { |
| 588 | struct task_desc *task; |
| 589 | struct perf_sched *sched; |
Yunlong Song | 08097ab | 2015-03-31 21:46:32 +0800 | [diff] [blame] | 590 | int fd; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 591 | }; |
| 592 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 593 | static void *thread_func(void *ctx) |
| 594 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 595 | struct sched_thread_parms *parms = ctx; |
| 596 | struct task_desc *this_task = parms->task; |
| 597 | struct perf_sched *sched = parms->sched; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 598 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 599 | unsigned long i, ret; |
| 600 | char comm2[22]; |
Yunlong Song | 08097ab | 2015-03-31 21:46:32 +0800 | [diff] [blame] | 601 | int fd = parms->fd; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 602 | |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 603 | zfree(&parms); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 604 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 605 | sprintf(comm2, ":%s", this_task->comm); |
| 606 | prctl(PR_SET_NAME, comm2); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 607 | if (fd < 0) |
| 608 | return NULL; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 609 | again: |
| 610 | ret = sem_post(&this_task->ready_for_work); |
| 611 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 612 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 613 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 614 | ret = pthread_mutex_unlock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 615 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 616 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 617 | cpu_usage_0 = get_cpu_usage_nsec_self(fd); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 618 | |
| 619 | for (i = 0; i < this_task->nr_events; i++) { |
| 620 | this_task->curr_event = i; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 621 | perf_sched__process_event(sched, this_task->atoms[i]); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 624 | cpu_usage_1 = get_cpu_usage_nsec_self(fd); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 625 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 626 | ret = sem_post(&this_task->work_done_sem); |
| 627 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 628 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 629 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 630 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 631 | ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 632 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 633 | |
| 634 | goto again; |
| 635 | } |
| 636 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 637 | static void create_tasks(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 638 | { |
| 639 | struct task_desc *task; |
| 640 | pthread_attr_t attr; |
| 641 | unsigned long i; |
| 642 | int err; |
| 643 | |
| 644 | err = pthread_attr_init(&attr); |
| 645 | BUG_ON(err); |
Jiri Pirko | 12f7e03 | 2011-01-10 14:14:23 -0200 | [diff] [blame] | 646 | err = pthread_attr_setstacksize(&attr, |
| 647 | (size_t) max(16 * 1024, PTHREAD_STACK_MIN)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 648 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 649 | err = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 650 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 651 | err = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 652 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 653 | for (i = 0; i < sched->nr_tasks; i++) { |
| 654 | struct sched_thread_parms *parms = malloc(sizeof(*parms)); |
| 655 | BUG_ON(parms == NULL); |
| 656 | parms->task = task = sched->tasks[i]; |
| 657 | parms->sched = sched; |
Yunlong Song | 939cda521 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 658 | parms->fd = self_open_counters(sched, i); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 659 | sem_init(&task->sleep_sem, 0, 0); |
| 660 | sem_init(&task->ready_for_work, 0, 0); |
| 661 | sem_init(&task->work_done_sem, 0, 0); |
| 662 | task->curr_event = 0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 663 | err = pthread_create(&task->thread, &attr, thread_func, parms); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 664 | BUG_ON(err); |
| 665 | } |
| 666 | } |
| 667 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 668 | static void wait_for_tasks(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 669 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 670 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 671 | struct task_desc *task; |
| 672 | unsigned long i, ret; |
| 673 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 674 | sched->start_time = get_nsecs(); |
| 675 | sched->cpu_usage = 0; |
| 676 | pthread_mutex_unlock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 677 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 678 | for (i = 0; i < sched->nr_tasks; i++) { |
| 679 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 680 | ret = sem_wait(&task->ready_for_work); |
| 681 | BUG_ON(ret); |
| 682 | sem_init(&task->ready_for_work, 0, 0); |
| 683 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 684 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 685 | BUG_ON(ret); |
| 686 | |
| 687 | cpu_usage_0 = get_cpu_usage_nsec_parent(); |
| 688 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 689 | pthread_mutex_unlock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 690 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 691 | for (i = 0; i < sched->nr_tasks; i++) { |
| 692 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 693 | ret = sem_wait(&task->work_done_sem); |
| 694 | BUG_ON(ret); |
| 695 | sem_init(&task->work_done_sem, 0, 0); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 696 | sched->cpu_usage += task->cpu_usage; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 697 | task->cpu_usage = 0; |
| 698 | } |
| 699 | |
| 700 | cpu_usage_1 = get_cpu_usage_nsec_parent(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 701 | if (!sched->runavg_cpu_usage) |
| 702 | sched->runavg_cpu_usage = sched->cpu_usage; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 703 | sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 704 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 705 | sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 706 | if (!sched->runavg_parent_cpu_usage) |
| 707 | sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 708 | sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + |
| 709 | sched->parent_cpu_usage)/sched->replay_repeat; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 710 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 711 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 712 | BUG_ON(ret); |
| 713 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 714 | for (i = 0; i < sched->nr_tasks; i++) { |
| 715 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 716 | sem_init(&task->sleep_sem, 0, 0); |
| 717 | task->curr_event = 0; |
| 718 | } |
| 719 | } |
| 720 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 721 | static void run_one_test(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 722 | { |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 723 | u64 T0, T1, delta, avg_delta, fluct; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 724 | |
| 725 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 726 | wait_for_tasks(sched); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 727 | T1 = get_nsecs(); |
| 728 | |
| 729 | delta = T1 - T0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 730 | sched->sum_runtime += delta; |
| 731 | sched->nr_runs++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 732 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 733 | avg_delta = sched->sum_runtime / sched->nr_runs; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 734 | if (delta < avg_delta) |
| 735 | fluct = avg_delta - delta; |
| 736 | else |
| 737 | fluct = delta - avg_delta; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 738 | sched->sum_fluct += fluct; |
| 739 | if (!sched->run_avg) |
| 740 | sched->run_avg = delta; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 741 | sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 742 | |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 743 | printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 744 | |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 745 | printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 746 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 747 | printf("cpu: %0.2f / %0.2f", |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 748 | (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 749 | |
| 750 | #if 0 |
| 751 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 752 | * rusage statistics done by the parent, these are less |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 753 | * accurate than the sched->sum_exec_runtime based statistics: |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 754 | */ |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 755 | printf(" [%0.2f / %0.2f]", |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 756 | (double)sched->parent_cpu_usage / NSEC_PER_MSEC, |
| 757 | (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 758 | #endif |
| 759 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 760 | printf("\n"); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 761 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 762 | if (sched->nr_sleep_corrections) |
| 763 | printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); |
| 764 | sched->nr_sleep_corrections = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 767 | static void test_calibrations(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 768 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 769 | u64 T0, T1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 770 | |
| 771 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 772 | burn_nsecs(sched, NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 773 | T1 = get_nsecs(); |
| 774 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 775 | printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 776 | |
| 777 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 778 | sleep_nsecs(NSEC_PER_MSEC); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 779 | T1 = get_nsecs(); |
| 780 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 781 | printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 784 | static int |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 785 | replay_wakeup_event(struct perf_sched *sched, |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 786 | struct perf_evsel *evsel, struct perf_sample *sample, |
| 787 | struct machine *machine __maybe_unused) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 788 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 789 | const char *comm = perf_evsel__strval(evsel, sample, "comm"); |
| 790 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 791 | struct task_desc *waker, *wakee; |
| 792 | |
| 793 | if (verbose) { |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 794 | printf("sched_wakeup event %p\n", evsel); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 795 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 796 | printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 799 | waker = register_pid(sched, sample->tid, "<unknown>"); |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 800 | wakee = register_pid(sched, pid, comm); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 801 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 802 | add_sched_event_wakeup(sched, waker, sample->time, wakee); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 803 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 806 | static int replay_switch_event(struct perf_sched *sched, |
| 807 | struct perf_evsel *evsel, |
| 808 | struct perf_sample *sample, |
| 809 | struct machine *machine __maybe_unused) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 810 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 811 | const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"), |
| 812 | *next_comm = perf_evsel__strval(evsel, sample, "next_comm"); |
| 813 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 814 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 815 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 816 | struct task_desc *prev, __maybe_unused *next; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 817 | u64 timestamp0, timestamp = sample->time; |
| 818 | int cpu = sample->cpu; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 819 | s64 delta; |
| 820 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 821 | if (verbose) |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 822 | printf("sched_switch event %p\n", evsel); |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 823 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 824 | if (cpu >= MAX_CPUS || cpu < 0) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 825 | return 0; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 826 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 827 | timestamp0 = sched->cpu_last_switched[cpu]; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 828 | if (timestamp0) |
| 829 | delta = timestamp - timestamp0; |
| 830 | else |
| 831 | delta = 0; |
| 832 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 833 | if (delta < 0) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 834 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 835 | return -1; |
| 836 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 837 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 838 | pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n", |
| 839 | prev_comm, prev_pid, next_comm, next_pid, delta); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 840 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 841 | prev = register_pid(sched, prev_pid, prev_comm); |
| 842 | next = register_pid(sched, next_pid, next_comm); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 843 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 844 | sched->cpu_last_switched[cpu] = timestamp; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 845 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 846 | add_sched_event_run(sched, prev, timestamp, delta); |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 847 | add_sched_event_sleep(sched, prev, timestamp, prev_state); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 848 | |
| 849 | return 0; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 850 | } |
| 851 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 852 | static int replay_fork_event(struct perf_sched *sched, |
| 853 | union perf_event *event, |
| 854 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 855 | { |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 856 | struct thread *child, *parent; |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 857 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 858 | child = machine__findnew_thread(machine, event->fork.pid, |
| 859 | event->fork.tid); |
| 860 | parent = machine__findnew_thread(machine, event->fork.ppid, |
| 861 | event->fork.ptid); |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 862 | |
| 863 | if (child == NULL || parent == NULL) { |
| 864 | pr_debug("thread does not exist on fork event: child %p, parent %p\n", |
| 865 | child, parent); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 866 | goto out_put; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 867 | } |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 868 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 869 | if (verbose) { |
| 870 | printf("fork event\n"); |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 871 | printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); |
| 872 | printf("... child: %s/%d\n", thread__comm_str(child), child->tid); |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 875 | register_pid(sched, parent->tid, thread__comm_str(parent)); |
| 876 | register_pid(sched, child->tid, thread__comm_str(child)); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 877 | out_put: |
| 878 | thread__put(child); |
| 879 | thread__put(parent); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 880 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 883 | struct sort_dimension { |
| 884 | const char *name; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 885 | sort_fn_t cmp; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 886 | struct list_head list; |
| 887 | }; |
| 888 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 889 | static int |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 890 | thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 891 | { |
| 892 | struct sort_dimension *sort; |
| 893 | int ret = 0; |
| 894 | |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 895 | BUG_ON(list_empty(list)); |
| 896 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 897 | list_for_each_entry(sort, list, list) { |
| 898 | ret = sort->cmp(l, r); |
| 899 | if (ret) |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | return ret; |
| 904 | } |
| 905 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 906 | static struct work_atoms * |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 907 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
| 908 | struct list_head *sort_list) |
| 909 | { |
| 910 | struct rb_node *node = root->rb_node; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 911 | struct work_atoms key = { .thread = thread }; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 912 | |
| 913 | while (node) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 914 | struct work_atoms *atoms; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 915 | int cmp; |
| 916 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 917 | atoms = container_of(node, struct work_atoms, node); |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 918 | |
| 919 | cmp = thread_lat_cmp(sort_list, &key, atoms); |
| 920 | if (cmp > 0) |
| 921 | node = node->rb_left; |
| 922 | else if (cmp < 0) |
| 923 | node = node->rb_right; |
| 924 | else { |
| 925 | BUG_ON(thread != atoms->thread); |
| 926 | return atoms; |
| 927 | } |
| 928 | } |
| 929 | return NULL; |
| 930 | } |
| 931 | |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 932 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 933 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 934 | struct list_head *sort_list) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 935 | { |
| 936 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 937 | |
| 938 | while (*new) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 939 | struct work_atoms *this; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 940 | int cmp; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 941 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 942 | this = container_of(*new, struct work_atoms, node); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 943 | parent = *new; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 944 | |
| 945 | cmp = thread_lat_cmp(sort_list, data, this); |
| 946 | |
| 947 | if (cmp > 0) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 948 | new = &((*new)->rb_left); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 949 | else |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 950 | new = &((*new)->rb_right); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | rb_link_node(&data->node, parent, new); |
| 954 | rb_insert_color(&data->node, root); |
| 955 | } |
| 956 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 957 | static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 958 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 959 | struct work_atoms *atoms = zalloc(sizeof(*atoms)); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 960 | if (!atoms) { |
| 961 | pr_err("No memory at %s\n", __func__); |
| 962 | return -1; |
| 963 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 964 | |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 965 | atoms->thread = thread__get(thread); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 966 | INIT_LIST_HEAD(&atoms->work_list); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 967 | __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 968 | return 0; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 971 | static char sched_out_state(u64 prev_state) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 972 | { |
| 973 | const char *str = TASK_STATE_TO_CHAR_STR; |
| 974 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 975 | return str[prev_state]; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 976 | } |
| 977 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 978 | static int |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 979 | add_sched_out_event(struct work_atoms *atoms, |
| 980 | char run_state, |
| 981 | u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 982 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 983 | struct work_atom *atom = zalloc(sizeof(*atom)); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 984 | if (!atom) { |
| 985 | pr_err("Non memory at %s", __func__); |
| 986 | return -1; |
| 987 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 988 | |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 989 | atom->sched_out_time = timestamp; |
| 990 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 991 | if (run_state == 'R') { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 992 | atom->state = THREAD_WAIT_CPU; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 993 | atom->wake_up_time = atom->sched_out_time; |
Frederic Weisbecker | c6ced61 | 2009-09-13 00:46:19 +0200 | [diff] [blame] | 994 | } |
| 995 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 996 | list_add_tail(&atom->list, &atoms->work_list); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 997 | return 0; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static void |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1001 | add_runtime_event(struct work_atoms *atoms, u64 delta, |
| 1002 | u64 timestamp __maybe_unused) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1003 | { |
| 1004 | struct work_atom *atom; |
| 1005 | |
| 1006 | BUG_ON(list_empty(&atoms->work_list)); |
| 1007 | |
| 1008 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
| 1009 | |
| 1010 | atom->runtime += delta; |
| 1011 | atoms->total_runtime += delta; |
| 1012 | } |
| 1013 | |
| 1014 | static void |
| 1015 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1016 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1017 | struct work_atom *atom; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1018 | u64 delta; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1019 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1020 | if (list_empty(&atoms->work_list)) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1021 | return; |
| 1022 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1023 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1024 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1025 | if (atom->state != THREAD_WAIT_CPU) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1026 | return; |
| 1027 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1028 | if (timestamp < atom->wake_up_time) { |
| 1029 | atom->state = THREAD_IGNORE; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1030 | return; |
| 1031 | } |
| 1032 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1033 | atom->state = THREAD_SCHED_IN; |
| 1034 | atom->sched_in_time = timestamp; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1035 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1036 | delta = atom->sched_in_time - atom->wake_up_time; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1037 | atoms->total_lat += delta; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 1038 | if (delta > atoms->max_lat) { |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1039 | atoms->max_lat = delta; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 1040 | atoms->max_lat_at = timestamp; |
| 1041 | } |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1042 | atoms->nb_atoms++; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1045 | static int latency_switch_event(struct perf_sched *sched, |
| 1046 | struct perf_evsel *evsel, |
| 1047 | struct perf_sample *sample, |
| 1048 | struct machine *machine) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1049 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1050 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 1051 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 1052 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1053 | struct work_atoms *out_events, *in_events; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1054 | struct thread *sched_out, *sched_in; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1055 | u64 timestamp0, timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1056 | int cpu = sample->cpu, err = -1; |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1057 | s64 delta; |
| 1058 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1059 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1060 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1061 | timestamp0 = sched->cpu_last_switched[cpu]; |
| 1062 | sched->cpu_last_switched[cpu] = timestamp; |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1063 | if (timestamp0) |
| 1064 | delta = timestamp - timestamp0; |
| 1065 | else |
| 1066 | delta = 0; |
| 1067 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1068 | if (delta < 0) { |
| 1069 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
| 1070 | return -1; |
| 1071 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1072 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1073 | sched_out = machine__findnew_thread(machine, -1, prev_pid); |
| 1074 | sched_in = machine__findnew_thread(machine, -1, next_pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1075 | if (sched_out == NULL || sched_in == NULL) |
| 1076 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1077 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1078 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1079 | if (!out_events) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1080 | if (thread_atoms_insert(sched, sched_out)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1081 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1082 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1083 | if (!out_events) { |
| 1084 | pr_err("out-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1085 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1086 | } |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1087 | } |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1088 | if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1089 | return -1; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1090 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1091 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1092 | if (!in_events) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1093 | if (thread_atoms_insert(sched, sched_in)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1094 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1095 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1096 | if (!in_events) { |
| 1097 | pr_err("in-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1098 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1099 | } |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1100 | /* |
| 1101 | * Take came in we have not heard about yet, |
| 1102 | * add in an initial atom in runnable state: |
| 1103 | */ |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1104 | if (add_sched_out_event(in_events, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1105 | goto out_put; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1106 | } |
| 1107 | add_sched_in_event(in_events, timestamp); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1108 | err = 0; |
| 1109 | out_put: |
| 1110 | thread__put(sched_out); |
| 1111 | thread__put(sched_in); |
| 1112 | return err; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1115 | static int latency_runtime_event(struct perf_sched *sched, |
| 1116 | struct perf_evsel *evsel, |
| 1117 | struct perf_sample *sample, |
| 1118 | struct machine *machine) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1119 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1120 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 1121 | const u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1122 | struct thread *thread = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1123 | struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1124 | u64 timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1125 | int cpu = sample->cpu, err = -1; |
| 1126 | |
| 1127 | if (thread == NULL) |
| 1128 | return -1; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1129 | |
| 1130 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1131 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1132 | if (thread_atoms_insert(sched, thread)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1133 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1134 | atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1135 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1136 | pr_err("in-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1137 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1138 | } |
| 1139 | if (add_sched_out_event(atoms, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1140 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1143 | add_runtime_event(atoms, runtime, timestamp); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1144 | err = 0; |
| 1145 | out_put: |
| 1146 | thread__put(thread); |
| 1147 | return err; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1150 | static int latency_wakeup_event(struct perf_sched *sched, |
| 1151 | struct perf_evsel *evsel, |
| 1152 | struct perf_sample *sample, |
| 1153 | struct machine *machine) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1154 | { |
Peter Zijlstra | 0680ee7 | 2014-05-12 20:19:46 +0200 | [diff] [blame] | 1155 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1156 | struct work_atoms *atoms; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1157 | struct work_atom *atom; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1158 | struct thread *wakee; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1159 | u64 timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1160 | int err = -1; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1161 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1162 | wakee = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1163 | if (wakee == NULL) |
| 1164 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1165 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
Frederic Weisbecker | 1756220 | 2009-09-12 23:11:32 +0200 | [diff] [blame] | 1166 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1167 | if (thread_atoms_insert(sched, wakee)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1168 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1169 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1170 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1171 | pr_err("wakeup-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1172 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1173 | } |
| 1174 | if (add_sched_out_event(atoms, 'S', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1175 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1178 | BUG_ON(list_empty(&atoms->work_list)); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1179 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1180 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1181 | |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1182 | /* |
Dongsheng Yang | 67d6259dd | 2014-05-13 10:38:21 +0900 | [diff] [blame] | 1183 | * As we do not guarantee the wakeup event happens when |
| 1184 | * task is out of run queue, also may happen when task is |
| 1185 | * on run queue and wakeup only change ->state to TASK_RUNNING, |
| 1186 | * then we should not set the ->wake_up_time when wake up a |
| 1187 | * task which is on run queue. |
| 1188 | * |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1189 | * You WILL be missing events if you've recorded only |
| 1190 | * one CPU, or are only looking at only one, so don't |
Dongsheng Yang | 67d6259dd | 2014-05-13 10:38:21 +0900 | [diff] [blame] | 1191 | * skip in this case. |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1192 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1193 | if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1194 | goto out_ok; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1195 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1196 | sched->nr_timestamps++; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1197 | if (atom->sched_out_time > timestamp) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1198 | sched->nr_unordered_timestamps++; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1199 | goto out_ok; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1200 | } |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1201 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1202 | atom->state = THREAD_WAIT_CPU; |
| 1203 | atom->wake_up_time = timestamp; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1204 | out_ok: |
| 1205 | err = 0; |
| 1206 | out_put: |
| 1207 | thread__put(wakee); |
| 1208 | return err; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1211 | static int latency_migrate_task_event(struct perf_sched *sched, |
| 1212 | struct perf_evsel *evsel, |
| 1213 | struct perf_sample *sample, |
| 1214 | struct machine *machine) |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1215 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1216 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1217 | u64 timestamp = sample->time; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1218 | struct work_atoms *atoms; |
| 1219 | struct work_atom *atom; |
| 1220 | struct thread *migrant; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1221 | int err = -1; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1222 | |
| 1223 | /* |
| 1224 | * Only need to worry about migration when profiling one CPU. |
| 1225 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1226 | if (sched->profile_cpu == -1) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1227 | return 0; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1228 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1229 | migrant = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1230 | if (migrant == NULL) |
| 1231 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1232 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1233 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1234 | if (thread_atoms_insert(sched, migrant)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1235 | goto out_put; |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1236 | register_pid(sched, migrant->tid, thread__comm_str(migrant)); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1237 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1238 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1239 | pr_err("migration-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1240 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1241 | } |
| 1242 | if (add_sched_out_event(atoms, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1243 | goto out_put; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | BUG_ON(list_empty(&atoms->work_list)); |
| 1247 | |
| 1248 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
| 1249 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; |
| 1250 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1251 | sched->nr_timestamps++; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1252 | |
| 1253 | if (atom->sched_out_time > timestamp) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1254 | sched->nr_unordered_timestamps++; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1255 | err = 0; |
| 1256 | out_put: |
| 1257 | thread__put(migrant); |
| 1258 | return err; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1261 | static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1262 | { |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1263 | int i; |
| 1264 | int ret; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1265 | u64 avg; |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1266 | char max_lat_at[32]; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1267 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1268 | if (!work_list->nb_atoms) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1269 | return; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1270 | /* |
| 1271 | * Ignore idle threads: |
| 1272 | */ |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1273 | if (!strcmp(thread__comm_str(work_list->thread), "swapper")) |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1274 | return; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1275 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1276 | sched->all_runtime += work_list->total_runtime; |
| 1277 | sched->all_count += work_list->nb_atoms; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1278 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1279 | if (work_list->num_merged > 1) |
| 1280 | ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged); |
| 1281 | else |
| 1282 | ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1283 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1284 | for (i = 0; i < 24 - ret; i++) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1285 | printf(" "); |
| 1286 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1287 | avg = work_list->total_lat / work_list->nb_atoms; |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1288 | timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at)); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1289 | |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1290 | printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n", |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 1291 | (double)work_list->total_runtime / NSEC_PER_MSEC, |
| 1292 | work_list->nb_atoms, (double)avg / NSEC_PER_MSEC, |
| 1293 | (double)work_list->max_lat / NSEC_PER_MSEC, |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1294 | max_lat_at); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1297 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1298 | { |
Jiri Olsa | 0014de1 | 2015-11-02 12:10:25 +0100 | [diff] [blame] | 1299 | if (l->thread == r->thread) |
| 1300 | return 0; |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 1301 | if (l->thread->tid < r->thread->tid) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1302 | return -1; |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 1303 | if (l->thread->tid > r->thread->tid) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1304 | return 1; |
Jiri Olsa | 0014de1 | 2015-11-02 12:10:25 +0100 | [diff] [blame] | 1305 | return (int)(l->thread - r->thread); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1308 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1309 | { |
| 1310 | u64 avgl, avgr; |
| 1311 | |
| 1312 | if (!l->nb_atoms) |
| 1313 | return -1; |
| 1314 | |
| 1315 | if (!r->nb_atoms) |
| 1316 | return 1; |
| 1317 | |
| 1318 | avgl = l->total_lat / l->nb_atoms; |
| 1319 | avgr = r->total_lat / r->nb_atoms; |
| 1320 | |
| 1321 | if (avgl < avgr) |
| 1322 | return -1; |
| 1323 | if (avgl > avgr) |
| 1324 | return 1; |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1329 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1330 | { |
| 1331 | if (l->max_lat < r->max_lat) |
| 1332 | return -1; |
| 1333 | if (l->max_lat > r->max_lat) |
| 1334 | return 1; |
| 1335 | |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1339 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1340 | { |
| 1341 | if (l->nb_atoms < r->nb_atoms) |
| 1342 | return -1; |
| 1343 | if (l->nb_atoms > r->nb_atoms) |
| 1344 | return 1; |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1349 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1350 | { |
| 1351 | if (l->total_runtime < r->total_runtime) |
| 1352 | return -1; |
| 1353 | if (l->total_runtime > r->total_runtime) |
| 1354 | return 1; |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Randy Dunlap | cbef79a | 2009-10-05 13:17:29 -0700 | [diff] [blame] | 1359 | static int sort_dimension__add(const char *tok, struct list_head *list) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1360 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1361 | size_t i; |
| 1362 | static struct sort_dimension avg_sort_dimension = { |
| 1363 | .name = "avg", |
| 1364 | .cmp = avg_cmp, |
| 1365 | }; |
| 1366 | static struct sort_dimension max_sort_dimension = { |
| 1367 | .name = "max", |
| 1368 | .cmp = max_cmp, |
| 1369 | }; |
| 1370 | static struct sort_dimension pid_sort_dimension = { |
| 1371 | .name = "pid", |
| 1372 | .cmp = pid_cmp, |
| 1373 | }; |
| 1374 | static struct sort_dimension runtime_sort_dimension = { |
| 1375 | .name = "runtime", |
| 1376 | .cmp = runtime_cmp, |
| 1377 | }; |
| 1378 | static struct sort_dimension switch_sort_dimension = { |
| 1379 | .name = "switch", |
| 1380 | .cmp = switch_cmp, |
| 1381 | }; |
| 1382 | struct sort_dimension *available_sorts[] = { |
| 1383 | &pid_sort_dimension, |
| 1384 | &avg_sort_dimension, |
| 1385 | &max_sort_dimension, |
| 1386 | &switch_sort_dimension, |
| 1387 | &runtime_sort_dimension, |
| 1388 | }; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1389 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1390 | for (i = 0; i < ARRAY_SIZE(available_sorts); i++) { |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1391 | if (!strcmp(available_sorts[i]->name, tok)) { |
| 1392 | list_add_tail(&available_sorts[i]->list, list); |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1401 | static void perf_sched__sort_lat(struct perf_sched *sched) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1402 | { |
| 1403 | struct rb_node *node; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1404 | struct rb_root *root = &sched->atom_root; |
| 1405 | again: |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1406 | for (;;) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1407 | struct work_atoms *data; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1408 | node = rb_first(root); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1409 | if (!node) |
| 1410 | break; |
| 1411 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1412 | rb_erase(node, root); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1413 | data = rb_entry(node, struct work_atoms, node); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1414 | __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1415 | } |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1416 | if (root == &sched->atom_root) { |
| 1417 | root = &sched->merged_atom_root; |
| 1418 | goto again; |
| 1419 | } |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1422 | static int process_sched_wakeup_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1423 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1424 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1425 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1426 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1427 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1428 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1429 | if (sched->tp_handler->wakeup_event) |
| 1430 | return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1431 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1432 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1435 | union map_priv { |
| 1436 | void *ptr; |
| 1437 | bool color; |
| 1438 | }; |
| 1439 | |
| 1440 | static bool thread__has_color(struct thread *thread) |
| 1441 | { |
| 1442 | union map_priv priv = { |
| 1443 | .ptr = thread__priv(thread), |
| 1444 | }; |
| 1445 | |
| 1446 | return priv.color; |
| 1447 | } |
| 1448 | |
| 1449 | static struct thread* |
| 1450 | map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid) |
| 1451 | { |
| 1452 | struct thread *thread = machine__findnew_thread(machine, pid, tid); |
| 1453 | union map_priv priv = { |
| 1454 | .color = false, |
| 1455 | }; |
| 1456 | |
| 1457 | if (!sched->map.color_pids || !thread || thread__priv(thread)) |
| 1458 | return thread; |
| 1459 | |
| 1460 | if (thread_map__has(sched->map.color_pids, tid)) |
| 1461 | priv.color = true; |
| 1462 | |
| 1463 | thread__set_priv(thread, priv.ptr); |
| 1464 | return thread; |
| 1465 | } |
| 1466 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1467 | static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel, |
| 1468 | struct perf_sample *sample, struct machine *machine) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1469 | { |
Dongsheng Yang | 9d372ca | 2014-05-16 14:37:05 +0900 | [diff] [blame] | 1470 | const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 1471 | struct thread *sched_in; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1472 | int new_shortname; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1473 | u64 timestamp0, timestamp = sample->time; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1474 | s64 delta; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1475 | int i, this_cpu = sample->cpu; |
| 1476 | int cpus_nr; |
| 1477 | bool new_cpu = false; |
Jiri Olsa | 8cd9119 | 2016-04-12 15:29:27 +0200 | [diff] [blame] | 1478 | const char *color = PERF_COLOR_NORMAL; |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1479 | char stimestamp[32]; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1480 | |
| 1481 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); |
| 1482 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1483 | if (this_cpu > sched->max_cpu) |
| 1484 | sched->max_cpu = this_cpu; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1485 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1486 | if (sched->map.comp) { |
| 1487 | cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS); |
| 1488 | if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) { |
| 1489 | sched->map.comp_cpus[cpus_nr++] = this_cpu; |
| 1490 | new_cpu = true; |
| 1491 | } |
| 1492 | } else |
| 1493 | cpus_nr = sched->max_cpu; |
| 1494 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1495 | timestamp0 = sched->cpu_last_switched[this_cpu]; |
| 1496 | sched->cpu_last_switched[this_cpu] = timestamp; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1497 | if (timestamp0) |
| 1498 | delta = timestamp - timestamp0; |
| 1499 | else |
| 1500 | delta = 0; |
| 1501 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1502 | if (delta < 0) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1503 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1504 | return -1; |
| 1505 | } |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1506 | |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1507 | sched_in = map__findnew_thread(sched, machine, -1, next_pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1508 | if (sched_in == NULL) |
| 1509 | return -1; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1510 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1511 | sched->curr_thread[this_cpu] = thread__get(sched_in); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1512 | |
| 1513 | printf(" "); |
| 1514 | |
| 1515 | new_shortname = 0; |
| 1516 | if (!sched_in->shortname[0]) { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1517 | if (!strcmp(thread__comm_str(sched_in), "swapper")) { |
| 1518 | /* |
| 1519 | * Don't allocate a letter-number for swapper:0 |
| 1520 | * as a shortname. Instead, we use '.' for it. |
| 1521 | */ |
| 1522 | sched_in->shortname[0] = '.'; |
| 1523 | sched_in->shortname[1] = ' '; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1524 | } else { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1525 | sched_in->shortname[0] = sched->next_shortname1; |
| 1526 | sched_in->shortname[1] = sched->next_shortname2; |
| 1527 | |
| 1528 | if (sched->next_shortname1 < 'Z') { |
| 1529 | sched->next_shortname1++; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1530 | } else { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1531 | sched->next_shortname1 = 'A'; |
| 1532 | if (sched->next_shortname2 < '9') |
| 1533 | sched->next_shortname2++; |
| 1534 | else |
| 1535 | sched->next_shortname2 = '0'; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | new_shortname = 1; |
| 1539 | } |
| 1540 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1541 | for (i = 0; i < cpus_nr; i++) { |
| 1542 | int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i; |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1543 | struct thread *curr_thread = sched->curr_thread[cpu]; |
| 1544 | const char *pid_color = color; |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 1545 | const char *cpu_color = color; |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1546 | |
| 1547 | if (curr_thread && thread__has_color(curr_thread)) |
| 1548 | pid_color = COLOR_PIDS; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1549 | |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 1550 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu)) |
| 1551 | continue; |
| 1552 | |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 1553 | if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu)) |
| 1554 | cpu_color = COLOR_CPUS; |
| 1555 | |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1556 | if (cpu != this_cpu) |
Namhyung Kim | 1208bb2 | 2016-10-24 11:02:43 +0900 | [diff] [blame] | 1557 | color_fprintf(stdout, color, " "); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1558 | else |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 1559 | color_fprintf(stdout, cpu_color, "*"); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1560 | |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1561 | if (sched->curr_thread[cpu]) |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1562 | color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname); |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1563 | else |
Jiri Olsa | 8cd9119 | 2016-04-12 15:29:27 +0200 | [diff] [blame] | 1564 | color_fprintf(stdout, color, " "); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 1567 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu)) |
| 1568 | goto out; |
| 1569 | |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 1570 | timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp)); |
| 1571 | color_fprintf(stdout, color, " %12s secs ", stimestamp); |
Namhyung Kim | e107f12 | 2016-10-24 11:02:44 +0900 | [diff] [blame] | 1572 | if (new_shortname || (verbose && sched_in->tid)) { |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 1573 | const char *pid_color = color; |
| 1574 | |
| 1575 | if (thread__has_color(sched_in)) |
| 1576 | pid_color = COLOR_PIDS; |
| 1577 | |
| 1578 | color_fprintf(stdout, pid_color, "%s => %s:%d", |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1579 | sched_in->shortname, thread__comm_str(sched_in), sched_in->tid); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1580 | } |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1581 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1582 | if (sched->map.comp && new_cpu) |
Jiri Olsa | 8cd9119 | 2016-04-12 15:29:27 +0200 | [diff] [blame] | 1583 | color_fprintf(stdout, color, " (CPU %d)", this_cpu); |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1584 | |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 1585 | out: |
Jiri Olsa | 8cd9119 | 2016-04-12 15:29:27 +0200 | [diff] [blame] | 1586 | color_fprintf(stdout, color, "\n"); |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 1587 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1588 | thread__put(sched_in); |
| 1589 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1590 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1593 | static int process_sched_switch_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1594 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1595 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1596 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1597 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1598 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1599 | int this_cpu = sample->cpu, err = 0; |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1600 | u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 1601 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1602 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1603 | if (sched->curr_pid[this_cpu] != (u32)-1) { |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1604 | /* |
| 1605 | * Are we trying to switch away a PID that is |
| 1606 | * not current? |
| 1607 | */ |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1608 | if (sched->curr_pid[this_cpu] != prev_pid) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1609 | sched->nr_context_switch_bugs++; |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1610 | } |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1611 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1612 | if (sched->tp_handler->switch_event) |
| 1613 | err = sched->tp_handler->switch_event(sched, evsel, sample, machine); |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1614 | |
| 1615 | sched->curr_pid[this_cpu] = next_pid; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1616 | return err; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1617 | } |
| 1618 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1619 | static int process_sched_runtime_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1620 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1621 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1622 | struct machine *machine) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1623 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1624 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1625 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1626 | if (sched->tp_handler->runtime_event) |
| 1627 | return sched->tp_handler->runtime_event(sched, evsel, sample, machine); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1628 | |
| 1629 | return 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1632 | static int perf_sched__process_fork_event(struct perf_tool *tool, |
| 1633 | union perf_event *event, |
| 1634 | struct perf_sample *sample, |
| 1635 | struct machine *machine) |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1636 | { |
| 1637 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
| 1638 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1639 | /* run the fork event through the perf machineruy */ |
| 1640 | perf_event__process_fork(tool, event, sample, machine); |
| 1641 | |
| 1642 | /* and then run additional processing needed for this command */ |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1643 | if (sched->tp_handler->fork_event) |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1644 | return sched->tp_handler->fork_event(sched, event, machine); |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1649 | static int process_sched_migrate_task_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1650 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1651 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1652 | struct machine *machine) |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1653 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1654 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1655 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1656 | if (sched->tp_handler->migrate_task_event) |
| 1657 | return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1658 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1659 | return 0; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1662 | typedef int (*tracepoint_handler)(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1663 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1664 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1665 | struct machine *machine); |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1666 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1667 | static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused, |
| 1668 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1669 | struct perf_sample *sample, |
| 1670 | struct perf_evsel *evsel, |
| 1671 | struct machine *machine) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1672 | { |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1673 | int err = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1674 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 1675 | if (evsel->handler != NULL) { |
| 1676 | tracepoint_handler f = evsel->handler; |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1677 | err = f(tool, evsel, sample, machine); |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1678 | } |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1679 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1680 | return err; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1683 | static int perf_sched__read_events(struct perf_sched *sched) |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1684 | { |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1685 | const struct perf_evsel_str_handler handlers[] = { |
| 1686 | { "sched:sched_switch", process_sched_switch_event, }, |
| 1687 | { "sched:sched_stat_runtime", process_sched_runtime_event, }, |
| 1688 | { "sched:sched_wakeup", process_sched_wakeup_event, }, |
| 1689 | { "sched:sched_wakeup_new", process_sched_wakeup_event, }, |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1690 | { "sched:sched_migrate_task", process_sched_migrate_task_event, }, |
| 1691 | }; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1692 | struct perf_session *session; |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1693 | struct perf_data_file file = { |
| 1694 | .path = input_name, |
| 1695 | .mode = PERF_DATA_MODE_READ, |
Yunlong Song | f0dd330 | 2015-03-31 21:46:35 +0800 | [diff] [blame] | 1696 | .force = sched->force, |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1697 | }; |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1698 | int rc = -1; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1699 | |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1700 | session = perf_session__new(&file, false, &sched->tool); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1701 | if (session == NULL) { |
| 1702 | pr_debug("No Memory for session\n"); |
| 1703 | return -1; |
| 1704 | } |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1705 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1706 | symbol__init(&session->header.env); |
Namhyung Kim | 0493410 | 2014-08-12 15:40:41 +0900 | [diff] [blame] | 1707 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1708 | if (perf_session__set_tracepoints_handlers(session, handlers)) |
| 1709 | goto out_delete; |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1710 | |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 1711 | if (perf_session__has_traces(session, "record -R")) { |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 1712 | int err = perf_session__process_events(session); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1713 | if (err) { |
| 1714 | pr_err("Failed to process events, error %d", err); |
| 1715 | goto out_delete; |
| 1716 | } |
Jiri Olsa | 4c09baf | 2011-08-08 23:03:34 +0200 | [diff] [blame] | 1717 | |
Arnaldo Carvalho de Melo | 75be989 | 2015-02-14 14:50:11 -0300 | [diff] [blame] | 1718 | sched->nr_events = session->evlist->stats.nr_events[0]; |
| 1719 | sched->nr_lost_events = session->evlist->stats.total_lost; |
| 1720 | sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 1721 | } |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1722 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1723 | rc = 0; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1724 | out_delete: |
| 1725 | perf_session__delete(session); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1726 | return rc; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1727 | } |
| 1728 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1729 | /* |
| 1730 | * scheduling times are printed as msec.usec |
| 1731 | */ |
| 1732 | static inline void print_sched_time(unsigned long long nsecs, int width) |
| 1733 | { |
| 1734 | unsigned long msecs; |
| 1735 | unsigned long usecs; |
| 1736 | |
| 1737 | msecs = nsecs / NSEC_PER_MSEC; |
| 1738 | nsecs -= msecs * NSEC_PER_MSEC; |
| 1739 | usecs = nsecs / NSEC_PER_USEC; |
| 1740 | printf("%*lu.%03lu ", width, msecs, usecs); |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * returns runtime data for event, allocating memory for it the |
| 1745 | * first time it is used. |
| 1746 | */ |
| 1747 | static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel) |
| 1748 | { |
| 1749 | struct evsel_runtime *r = evsel->priv; |
| 1750 | |
| 1751 | if (r == NULL) { |
| 1752 | r = zalloc(sizeof(struct evsel_runtime)); |
| 1753 | evsel->priv = r; |
| 1754 | } |
| 1755 | |
| 1756 | return r; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * save last time event was seen per cpu |
| 1761 | */ |
| 1762 | static void perf_evsel__save_time(struct perf_evsel *evsel, |
| 1763 | u64 timestamp, u32 cpu) |
| 1764 | { |
| 1765 | struct evsel_runtime *r = perf_evsel__get_runtime(evsel); |
| 1766 | |
| 1767 | if (r == NULL) |
| 1768 | return; |
| 1769 | |
| 1770 | if ((cpu >= r->ncpu) || (r->last_time == NULL)) { |
| 1771 | int i, n = __roundup_pow_of_two(cpu+1); |
| 1772 | void *p = r->last_time; |
| 1773 | |
| 1774 | p = realloc(r->last_time, n * sizeof(u64)); |
| 1775 | if (!p) |
| 1776 | return; |
| 1777 | |
| 1778 | r->last_time = p; |
| 1779 | for (i = r->ncpu; i < n; ++i) |
| 1780 | r->last_time[i] = (u64) 0; |
| 1781 | |
| 1782 | r->ncpu = n; |
| 1783 | } |
| 1784 | |
| 1785 | r->last_time[cpu] = timestamp; |
| 1786 | } |
| 1787 | |
| 1788 | /* returns last time this event was seen on the given cpu */ |
| 1789 | static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu) |
| 1790 | { |
| 1791 | struct evsel_runtime *r = perf_evsel__get_runtime(evsel); |
| 1792 | |
| 1793 | if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu)) |
| 1794 | return 0; |
| 1795 | |
| 1796 | return r->last_time[cpu]; |
| 1797 | } |
| 1798 | |
Namhyung Kim | 9b8087d | 2016-12-22 15:03:48 +0900 | [diff] [blame] | 1799 | static int comm_width = 30; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1800 | |
| 1801 | static char *timehist_get_commstr(struct thread *thread) |
| 1802 | { |
| 1803 | static char str[32]; |
| 1804 | const char *comm = thread__comm_str(thread); |
| 1805 | pid_t tid = thread->tid; |
| 1806 | pid_t pid = thread->pid_; |
| 1807 | int n; |
| 1808 | |
| 1809 | if (pid == 0) |
| 1810 | n = scnprintf(str, sizeof(str), "%s", comm); |
| 1811 | |
| 1812 | else if (tid != pid) |
| 1813 | n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid); |
| 1814 | |
| 1815 | else |
| 1816 | n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid); |
| 1817 | |
| 1818 | if (n > comm_width) |
| 1819 | comm_width = n; |
| 1820 | |
| 1821 | return str; |
| 1822 | } |
| 1823 | |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1824 | static void timehist_header(struct perf_sched *sched) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1825 | { |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1826 | u32 ncpus = sched->max_cpu + 1; |
| 1827 | u32 i, j; |
| 1828 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1829 | printf("%15s %6s ", "time", "cpu"); |
| 1830 | |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1831 | if (sched->show_cpu_visual) { |
| 1832 | printf(" "); |
| 1833 | for (i = 0, j = 0; i < ncpus; ++i) { |
| 1834 | printf("%x", j++); |
| 1835 | if (j > 15) |
| 1836 | j = 0; |
| 1837 | } |
| 1838 | printf(" "); |
| 1839 | } |
| 1840 | |
Namhyung Kim | 0e6758e | 2016-12-22 15:03:48 +0900 | [diff] [blame] | 1841 | printf(" %-*s %9s %9s %9s", comm_width, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1842 | "task name", "wait time", "sch delay", "run time"); |
| 1843 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1844 | if (sched->show_state) |
| 1845 | printf(" %s", "state"); |
| 1846 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1847 | printf("\n"); |
| 1848 | |
| 1849 | /* |
| 1850 | * units row |
| 1851 | */ |
| 1852 | printf("%15s %-6s ", "", ""); |
| 1853 | |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1854 | if (sched->show_cpu_visual) |
| 1855 | printf(" %*s ", ncpus, ""); |
| 1856 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1857 | printf(" %-*s %9s %9s %9s", comm_width, |
Namhyung Kim | 0e6758e | 2016-12-22 15:03:48 +0900 | [diff] [blame] | 1858 | "[tid/pid]", "(msec)", "(msec)", "(msec)"); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1859 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1860 | if (sched->show_state) |
| 1861 | printf(" %5s", ""); |
| 1862 | |
| 1863 | printf("\n"); |
| 1864 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1865 | /* |
| 1866 | * separator |
| 1867 | */ |
| 1868 | printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line); |
| 1869 | |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1870 | if (sched->show_cpu_visual) |
| 1871 | printf(" %.*s ", ncpus, graph_dotted_line); |
| 1872 | |
Namhyung Kim | 0e6758e | 2016-12-22 15:03:48 +0900 | [diff] [blame] | 1873 | printf(" %.*s %.9s %.9s %.9s", comm_width, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1874 | graph_dotted_line, graph_dotted_line, graph_dotted_line, |
| 1875 | graph_dotted_line); |
| 1876 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1877 | if (sched->show_state) |
| 1878 | printf(" %.5s", graph_dotted_line); |
| 1879 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1880 | printf("\n"); |
| 1881 | } |
| 1882 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1883 | static char task_state_char(struct thread *thread, int state) |
| 1884 | { |
| 1885 | static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; |
| 1886 | unsigned bit = state ? ffs(state) : 0; |
| 1887 | |
| 1888 | /* 'I' for idle */ |
| 1889 | if (thread->tid == 0) |
| 1890 | return 'I'; |
| 1891 | |
| 1892 | return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; |
| 1893 | } |
| 1894 | |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 1895 | static void timehist_print_sample(struct perf_sched *sched, |
| 1896 | struct perf_sample *sample, |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 1897 | struct addr_location *al, |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 1898 | struct thread *thread, |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1899 | u64 t, int state) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1900 | { |
| 1901 | struct thread_runtime *tr = thread__priv(thread); |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1902 | u32 max_cpus = sched->max_cpu + 1; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1903 | char tstr[64]; |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 1904 | u64 wait_time; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1905 | |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 1906 | timestamp__scnprintf_usec(t, tstr, sizeof(tstr)); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1907 | printf("%15s [%04d] ", tstr, sample->cpu); |
| 1908 | |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 1909 | if (sched->show_cpu_visual) { |
| 1910 | u32 i; |
| 1911 | char c; |
| 1912 | |
| 1913 | printf(" "); |
| 1914 | for (i = 0; i < max_cpus; ++i) { |
| 1915 | /* flag idle times with 'i'; others are sched events */ |
| 1916 | if (i == sample->cpu) |
| 1917 | c = (thread->tid == 0) ? 'i' : 's'; |
| 1918 | else |
| 1919 | c = ' '; |
| 1920 | printf("%c", c); |
| 1921 | } |
| 1922 | printf(" "); |
| 1923 | } |
| 1924 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1925 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); |
| 1926 | |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 1927 | wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt; |
| 1928 | print_sched_time(wait_time, 6); |
| 1929 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1930 | print_sched_time(tr->dt_delay, 6); |
| 1931 | print_sched_time(tr->dt_run, 6); |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 1932 | |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 1933 | if (sched->show_state) |
| 1934 | printf(" %5c ", task_state_char(thread, state)); |
| 1935 | |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 1936 | if (sched->show_wakeups) |
| 1937 | printf(" %-*s", comm_width, ""); |
| 1938 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 1939 | if (thread->tid == 0) |
| 1940 | goto out; |
| 1941 | |
| 1942 | if (sched->show_callchain) |
| 1943 | printf(" "); |
| 1944 | |
| 1945 | sample__fprintf_sym(sample, al, 0, |
| 1946 | EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE | |
Namhyung Kim | 2d9bbf6 | 2016-11-24 10:11:13 +0900 | [diff] [blame] | 1947 | EVSEL__PRINT_CALLCHAIN_ARROW | |
| 1948 | EVSEL__PRINT_SKIP_IGNORED, |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 1949 | &callchain_cursor, stdout); |
| 1950 | |
| 1951 | out: |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1952 | printf("\n"); |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * Explanation of delta-time stats: |
| 1957 | * |
| 1958 | * t = time of current schedule out event |
| 1959 | * tprev = time of previous sched out event |
| 1960 | * also time of schedule-in event for current task |
| 1961 | * last_time = time of last sched change event for current task |
| 1962 | * (i.e, time process was last scheduled out) |
| 1963 | * ready_to_run = time of wakeup for current task |
| 1964 | * |
| 1965 | * -----|------------|------------|------------|------ |
| 1966 | * last ready tprev t |
| 1967 | * time to run |
| 1968 | * |
| 1969 | * |-------- dt_wait --------| |
| 1970 | * |- dt_delay -|-- dt_run --| |
| 1971 | * |
| 1972 | * dt_run = run time of current task |
| 1973 | * dt_wait = time between last schedule out event for task and tprev |
| 1974 | * represents time spent off the cpu |
| 1975 | * dt_delay = time between wakeup and schedule-in of task |
| 1976 | */ |
| 1977 | |
| 1978 | static void timehist_update_runtime_stats(struct thread_runtime *r, |
| 1979 | u64 t, u64 tprev) |
| 1980 | { |
| 1981 | r->dt_delay = 0; |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 1982 | r->dt_sleep = 0; |
| 1983 | r->dt_iowait = 0; |
| 1984 | r->dt_preempt = 0; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1985 | r->dt_run = 0; |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 1986 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 1987 | if (tprev) { |
| 1988 | r->dt_run = t - tprev; |
| 1989 | if (r->ready_to_run) { |
| 1990 | if (r->ready_to_run > tprev) |
| 1991 | pr_debug("time travel: wakeup time for task > previous sched_switch event\n"); |
| 1992 | else |
| 1993 | r->dt_delay = tprev - r->ready_to_run; |
| 1994 | } |
| 1995 | |
| 1996 | if (r->last_time > tprev) |
| 1997 | pr_debug("time travel: last sched out time for task > previous sched_switch event\n"); |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 1998 | else if (r->last_time) { |
| 1999 | u64 dt_wait = tprev - r->last_time; |
| 2000 | |
| 2001 | if (r->last_state == TASK_RUNNING) |
| 2002 | r->dt_preempt = dt_wait; |
| 2003 | else if (r->last_state == TASK_UNINTERRUPTIBLE) |
| 2004 | r->dt_iowait = dt_wait; |
| 2005 | else |
| 2006 | r->dt_sleep = dt_wait; |
| 2007 | } |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | update_stats(&r->run_stats, r->dt_run); |
| 2011 | r->total_run_time += r->dt_run; |
| 2012 | } |
| 2013 | |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2014 | static bool is_idle_sample(struct perf_sample *sample, |
| 2015 | struct perf_evsel *evsel) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2016 | { |
| 2017 | /* pid 0 == swapper == idle task */ |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2018 | if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) |
| 2019 | return perf_evsel__intval(evsel, sample, "prev_pid") == 0; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2020 | |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2021 | return sample->pid == 0; |
| 2022 | } |
| 2023 | |
| 2024 | static void save_task_callchain(struct perf_sched *sched, |
| 2025 | struct perf_sample *sample, |
| 2026 | struct perf_evsel *evsel, |
| 2027 | struct machine *machine) |
| 2028 | { |
| 2029 | struct callchain_cursor *cursor = &callchain_cursor; |
| 2030 | struct thread *thread; |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2031 | |
| 2032 | /* want main thread for process - has maps */ |
| 2033 | thread = machine__findnew_thread(machine, sample->pid, sample->pid); |
| 2034 | if (thread == NULL) { |
| 2035 | pr_debug("Failed to get thread for pid %d.\n", sample->pid); |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2036 | return; |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2040 | return; |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2041 | |
| 2042 | if (thread__resolve_callchain(thread, cursor, evsel, sample, |
Namhyung Kim | 8388deb | 2016-11-24 10:11:14 +0900 | [diff] [blame] | 2043 | NULL, NULL, sched->max_stack + 2) != 0) { |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2044 | if (verbose) |
| 2045 | error("Failed to resolve callchain. Skipping\n"); |
| 2046 | |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2047 | return; |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2048 | } |
Namhyung Kim | cdeb01b | 2016-11-24 10:11:12 +0900 | [diff] [blame] | 2049 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2050 | callchain_cursor_commit(cursor); |
Namhyung Kim | cdeb01b | 2016-11-24 10:11:12 +0900 | [diff] [blame] | 2051 | |
| 2052 | while (true) { |
| 2053 | struct callchain_cursor_node *node; |
| 2054 | struct symbol *sym; |
| 2055 | |
| 2056 | node = callchain_cursor_current(cursor); |
| 2057 | if (node == NULL) |
| 2058 | break; |
| 2059 | |
| 2060 | sym = node->sym; |
| 2061 | if (sym && sym->name) { |
| 2062 | if (!strcmp(sym->name, "schedule") || |
| 2063 | !strcmp(sym->name, "__schedule") || |
| 2064 | !strcmp(sym->name, "preempt_schedule")) |
| 2065 | sym->ignore = 1; |
| 2066 | } |
| 2067 | |
| 2068 | callchain_cursor_advance(cursor); |
| 2069 | } |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2070 | } |
| 2071 | |
Namhyung Kim | 3bc2fa9 | 2016-12-08 23:47:51 +0900 | [diff] [blame] | 2072 | static int init_idle_thread(struct thread *thread) |
| 2073 | { |
| 2074 | struct idle_thread_runtime *itr; |
| 2075 | |
| 2076 | thread__set_comm(thread, idle_comm, 0); |
| 2077 | |
| 2078 | itr = zalloc(sizeof(*itr)); |
| 2079 | if (itr == NULL) |
| 2080 | return -ENOMEM; |
| 2081 | |
| 2082 | init_stats(&itr->tr.run_stats); |
| 2083 | callchain_init(&itr->callchain); |
| 2084 | callchain_cursor_reset(&itr->cursor); |
| 2085 | thread__set_priv(thread, itr); |
| 2086 | |
| 2087 | return 0; |
| 2088 | } |
| 2089 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2090 | /* |
| 2091 | * Track idle stats per cpu by maintaining a local thread |
| 2092 | * struct for the idle task on each cpu. |
| 2093 | */ |
| 2094 | static int init_idle_threads(int ncpu) |
| 2095 | { |
Namhyung Kim | 3bc2fa9 | 2016-12-08 23:47:51 +0900 | [diff] [blame] | 2096 | int i, ret; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2097 | |
| 2098 | idle_threads = zalloc(ncpu * sizeof(struct thread *)); |
| 2099 | if (!idle_threads) |
| 2100 | return -ENOMEM; |
| 2101 | |
Namhyung Kim | b336352 | 2016-12-06 12:40:05 +0900 | [diff] [blame] | 2102 | idle_max_cpu = ncpu; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2103 | |
| 2104 | /* allocate the actual thread struct if needed */ |
| 2105 | for (i = 0; i < ncpu; ++i) { |
| 2106 | idle_threads[i] = thread__new(0, 0); |
| 2107 | if (idle_threads[i] == NULL) |
| 2108 | return -ENOMEM; |
| 2109 | |
Namhyung Kim | 3bc2fa9 | 2016-12-08 23:47:51 +0900 | [diff] [blame] | 2110 | ret = init_idle_thread(idle_threads[i]); |
| 2111 | if (ret < 0) |
| 2112 | return ret; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
| 2118 | static void free_idle_threads(void) |
| 2119 | { |
| 2120 | int i; |
| 2121 | |
| 2122 | if (idle_threads == NULL) |
| 2123 | return; |
| 2124 | |
Namhyung Kim | b336352 | 2016-12-06 12:40:05 +0900 | [diff] [blame] | 2125 | for (i = 0; i < idle_max_cpu; ++i) { |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2126 | if ((idle_threads[i])) |
| 2127 | thread__delete(idle_threads[i]); |
| 2128 | } |
| 2129 | |
| 2130 | free(idle_threads); |
| 2131 | } |
| 2132 | |
| 2133 | static struct thread *get_idle_thread(int cpu) |
| 2134 | { |
| 2135 | /* |
| 2136 | * expand/allocate array of pointers to local thread |
| 2137 | * structs if needed |
| 2138 | */ |
| 2139 | if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) { |
| 2140 | int i, j = __roundup_pow_of_two(cpu+1); |
| 2141 | void *p; |
| 2142 | |
| 2143 | p = realloc(idle_threads, j * sizeof(struct thread *)); |
| 2144 | if (!p) |
| 2145 | return NULL; |
| 2146 | |
| 2147 | idle_threads = (struct thread **) p; |
Namhyung Kim | b336352 | 2016-12-06 12:40:05 +0900 | [diff] [blame] | 2148 | for (i = idle_max_cpu; i < j; ++i) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2149 | idle_threads[i] = NULL; |
| 2150 | |
| 2151 | idle_max_cpu = j; |
| 2152 | } |
| 2153 | |
| 2154 | /* allocate a new thread struct if needed */ |
| 2155 | if (idle_threads[cpu] == NULL) { |
| 2156 | idle_threads[cpu] = thread__new(0, 0); |
| 2157 | |
| 2158 | if (idle_threads[cpu]) { |
Namhyung Kim | 3bc2fa9 | 2016-12-08 23:47:51 +0900 | [diff] [blame] | 2159 | if (init_idle_thread(idle_threads[cpu]) < 0) |
| 2160 | return NULL; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | return idle_threads[cpu]; |
| 2165 | } |
| 2166 | |
Namhyung Kim | 699b5b9 | 2016-12-08 23:47:52 +0900 | [diff] [blame] | 2167 | static void save_idle_callchain(struct idle_thread_runtime *itr, |
| 2168 | struct perf_sample *sample) |
| 2169 | { |
| 2170 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
| 2171 | return; |
| 2172 | |
| 2173 | callchain_cursor__copy(&itr->cursor, &callchain_cursor); |
| 2174 | } |
| 2175 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2176 | /* |
| 2177 | * handle runtime stats saved per thread |
| 2178 | */ |
| 2179 | static struct thread_runtime *thread__init_runtime(struct thread *thread) |
| 2180 | { |
| 2181 | struct thread_runtime *r; |
| 2182 | |
| 2183 | r = zalloc(sizeof(struct thread_runtime)); |
| 2184 | if (!r) |
| 2185 | return NULL; |
| 2186 | |
| 2187 | init_stats(&r->run_stats); |
| 2188 | thread__set_priv(thread, r); |
| 2189 | |
| 2190 | return r; |
| 2191 | } |
| 2192 | |
| 2193 | static struct thread_runtime *thread__get_runtime(struct thread *thread) |
| 2194 | { |
| 2195 | struct thread_runtime *tr; |
| 2196 | |
| 2197 | tr = thread__priv(thread); |
| 2198 | if (tr == NULL) { |
| 2199 | tr = thread__init_runtime(thread); |
| 2200 | if (tr == NULL) |
| 2201 | pr_debug("Failed to malloc memory for runtime data.\n"); |
| 2202 | } |
| 2203 | |
| 2204 | return tr; |
| 2205 | } |
| 2206 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2207 | static struct thread *timehist_get_thread(struct perf_sched *sched, |
| 2208 | struct perf_sample *sample, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2209 | struct machine *machine, |
| 2210 | struct perf_evsel *evsel) |
| 2211 | { |
| 2212 | struct thread *thread; |
| 2213 | |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2214 | if (is_idle_sample(sample, evsel)) { |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2215 | thread = get_idle_thread(sample->cpu); |
| 2216 | if (thread == NULL) |
| 2217 | pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); |
| 2218 | |
| 2219 | } else { |
Namhyung Kim | 5d92d96 | 2016-12-06 12:40:03 +0900 | [diff] [blame] | 2220 | /* there were samples with tid 0 but non-zero pid */ |
| 2221 | thread = machine__findnew_thread(machine, sample->pid, |
| 2222 | sample->tid ?: sample->pid); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2223 | if (thread == NULL) { |
| 2224 | pr_debug("Failed to get thread for tid %d. skipping sample.\n", |
| 2225 | sample->tid); |
| 2226 | } |
Namhyung Kim | 96039c7 | 2016-12-08 23:47:50 +0900 | [diff] [blame] | 2227 | |
| 2228 | save_task_callchain(sched, sample, evsel, machine); |
Namhyung Kim | 699b5b9 | 2016-12-08 23:47:52 +0900 | [diff] [blame] | 2229 | if (sched->idle_hist) { |
| 2230 | struct thread *idle; |
| 2231 | struct idle_thread_runtime *itr; |
| 2232 | |
| 2233 | idle = get_idle_thread(sample->cpu); |
| 2234 | if (idle == NULL) { |
| 2235 | pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); |
| 2236 | return NULL; |
| 2237 | } |
| 2238 | |
| 2239 | itr = thread__priv(idle); |
| 2240 | if (itr == NULL) |
| 2241 | return NULL; |
| 2242 | |
| 2243 | itr->last_thread = thread; |
| 2244 | |
| 2245 | /* copy task callchain when entering to idle */ |
| 2246 | if (perf_evsel__intval(evsel, sample, "next_pid") == 0) |
| 2247 | save_idle_callchain(itr, sample); |
| 2248 | } |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | return thread; |
| 2252 | } |
| 2253 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2254 | static bool timehist_skip_sample(struct perf_sched *sched, |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2255 | struct thread *thread, |
| 2256 | struct perf_evsel *evsel, |
| 2257 | struct perf_sample *sample) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2258 | { |
| 2259 | bool rc = false; |
| 2260 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2261 | if (thread__is_filtered(thread)) { |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2262 | rc = true; |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2263 | sched->skipped_samples++; |
| 2264 | } |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2265 | |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2266 | if (sched->idle_hist) { |
| 2267 | if (strcmp(perf_evsel__name(evsel), "sched:sched_switch")) |
| 2268 | rc = true; |
| 2269 | else if (perf_evsel__intval(evsel, sample, "prev_pid") != 0 && |
| 2270 | perf_evsel__intval(evsel, sample, "next_pid") != 0) |
| 2271 | rc = true; |
| 2272 | } |
| 2273 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2274 | return rc; |
| 2275 | } |
| 2276 | |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2277 | static void timehist_print_wakeup_event(struct perf_sched *sched, |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2278 | struct perf_evsel *evsel, |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2279 | struct perf_sample *sample, |
| 2280 | struct machine *machine, |
| 2281 | struct thread *awakened) |
| 2282 | { |
| 2283 | struct thread *thread; |
| 2284 | char tstr[64]; |
| 2285 | |
| 2286 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); |
| 2287 | if (thread == NULL) |
| 2288 | return; |
| 2289 | |
| 2290 | /* show wakeup unless both awakee and awaker are filtered */ |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2291 | if (timehist_skip_sample(sched, thread, evsel, sample) && |
| 2292 | timehist_skip_sample(sched, awakened, evsel, sample)) { |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2293 | return; |
| 2294 | } |
| 2295 | |
| 2296 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); |
| 2297 | printf("%15s [%04d] ", tstr, sample->cpu); |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 2298 | if (sched->show_cpu_visual) |
| 2299 | printf(" %*s ", sched->max_cpu + 1, ""); |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2300 | |
| 2301 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); |
| 2302 | |
| 2303 | /* dt spacer */ |
| 2304 | printf(" %9s %9s %9s ", "", "", ""); |
| 2305 | |
| 2306 | printf("awakened: %s", timehist_get_commstr(awakened)); |
| 2307 | |
| 2308 | printf("\n"); |
| 2309 | } |
| 2310 | |
| 2311 | static int timehist_sched_wakeup_event(struct perf_tool *tool, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2312 | union perf_event *event __maybe_unused, |
| 2313 | struct perf_evsel *evsel, |
| 2314 | struct perf_sample *sample, |
| 2315 | struct machine *machine) |
| 2316 | { |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2317 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2318 | struct thread *thread; |
| 2319 | struct thread_runtime *tr = NULL; |
| 2320 | /* want pid of awakened task not pid in sample */ |
| 2321 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 2322 | |
| 2323 | thread = machine__findnew_thread(machine, 0, pid); |
| 2324 | if (thread == NULL) |
| 2325 | return -1; |
| 2326 | |
| 2327 | tr = thread__get_runtime(thread); |
| 2328 | if (tr == NULL) |
| 2329 | return -1; |
| 2330 | |
| 2331 | if (tr->ready_to_run == 0) |
| 2332 | tr->ready_to_run = sample->time; |
| 2333 | |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2334 | /* show wakeups if requested */ |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2335 | if (sched->show_wakeups && |
| 2336 | !perf_time__skip_sample(&sched->ptime, sample->time)) |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2337 | timehist_print_wakeup_event(sched, evsel, sample, machine, thread); |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2338 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2339 | return 0; |
| 2340 | } |
| 2341 | |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2342 | static void timehist_print_migration_event(struct perf_sched *sched, |
| 2343 | struct perf_evsel *evsel, |
| 2344 | struct perf_sample *sample, |
| 2345 | struct machine *machine, |
| 2346 | struct thread *migrated) |
| 2347 | { |
| 2348 | struct thread *thread; |
| 2349 | char tstr[64]; |
| 2350 | u32 max_cpus = sched->max_cpu + 1; |
| 2351 | u32 ocpu, dcpu; |
| 2352 | |
| 2353 | if (sched->summary_only) |
| 2354 | return; |
| 2355 | |
| 2356 | max_cpus = sched->max_cpu + 1; |
| 2357 | ocpu = perf_evsel__intval(evsel, sample, "orig_cpu"); |
| 2358 | dcpu = perf_evsel__intval(evsel, sample, "dest_cpu"); |
| 2359 | |
| 2360 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); |
| 2361 | if (thread == NULL) |
| 2362 | return; |
| 2363 | |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2364 | if (timehist_skip_sample(sched, thread, evsel, sample) && |
| 2365 | timehist_skip_sample(sched, migrated, evsel, sample)) { |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); |
| 2370 | printf("%15s [%04d] ", tstr, sample->cpu); |
| 2371 | |
| 2372 | if (sched->show_cpu_visual) { |
| 2373 | u32 i; |
| 2374 | char c; |
| 2375 | |
| 2376 | printf(" "); |
| 2377 | for (i = 0; i < max_cpus; ++i) { |
| 2378 | c = (i == sample->cpu) ? 'm' : ' '; |
| 2379 | printf("%c", c); |
| 2380 | } |
| 2381 | printf(" "); |
| 2382 | } |
| 2383 | |
| 2384 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); |
| 2385 | |
| 2386 | /* dt spacer */ |
| 2387 | printf(" %9s %9s %9s ", "", "", ""); |
| 2388 | |
| 2389 | printf("migrated: %s", timehist_get_commstr(migrated)); |
| 2390 | printf(" cpu %d => %d", ocpu, dcpu); |
| 2391 | |
| 2392 | printf("\n"); |
| 2393 | } |
| 2394 | |
| 2395 | static int timehist_migrate_task_event(struct perf_tool *tool, |
| 2396 | union perf_event *event __maybe_unused, |
| 2397 | struct perf_evsel *evsel, |
| 2398 | struct perf_sample *sample, |
| 2399 | struct machine *machine) |
| 2400 | { |
| 2401 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
| 2402 | struct thread *thread; |
| 2403 | struct thread_runtime *tr = NULL; |
| 2404 | /* want pid of migrated task not pid in sample */ |
| 2405 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 2406 | |
| 2407 | thread = machine__findnew_thread(machine, 0, pid); |
| 2408 | if (thread == NULL) |
| 2409 | return -1; |
| 2410 | |
| 2411 | tr = thread__get_runtime(thread); |
| 2412 | if (tr == NULL) |
| 2413 | return -1; |
| 2414 | |
| 2415 | tr->migrations++; |
| 2416 | |
| 2417 | /* show migrations if requested */ |
| 2418 | timehist_print_migration_event(sched, evsel, sample, machine, thread); |
| 2419 | |
| 2420 | return 0; |
| 2421 | } |
| 2422 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2423 | static int timehist_sched_change_event(struct perf_tool *tool, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2424 | union perf_event *event, |
| 2425 | struct perf_evsel *evsel, |
| 2426 | struct perf_sample *sample, |
| 2427 | struct machine *machine) |
| 2428 | { |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 2429 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2430 | struct perf_time_interval *ptime = &sched->ptime; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2431 | struct addr_location al; |
| 2432 | struct thread *thread; |
| 2433 | struct thread_runtime *tr = NULL; |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2434 | u64 tprev, t = sample->time; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2435 | int rc = 0; |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 2436 | int state = perf_evsel__intval(evsel, sample, "prev_state"); |
| 2437 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2438 | |
| 2439 | if (machine__resolve(machine, &al, sample) < 0) { |
| 2440 | pr_err("problem processing %d event. skipping it\n", |
| 2441 | event->header.type); |
| 2442 | rc = -1; |
| 2443 | goto out; |
| 2444 | } |
| 2445 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2446 | thread = timehist_get_thread(sched, sample, machine, evsel); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2447 | if (thread == NULL) { |
| 2448 | rc = -1; |
| 2449 | goto out; |
| 2450 | } |
| 2451 | |
Namhyung Kim | a4b2b6f | 2016-12-08 23:47:53 +0900 | [diff] [blame] | 2452 | if (timehist_skip_sample(sched, thread, evsel, sample)) |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2453 | goto out; |
| 2454 | |
| 2455 | tr = thread__get_runtime(thread); |
| 2456 | if (tr == NULL) { |
| 2457 | rc = -1; |
| 2458 | goto out; |
| 2459 | } |
| 2460 | |
| 2461 | tprev = perf_evsel__get_time(evsel, sample->cpu); |
| 2462 | |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2463 | /* |
| 2464 | * If start time given: |
| 2465 | * - sample time is under window user cares about - skip sample |
| 2466 | * - tprev is under window user cares about - reset to start of window |
| 2467 | */ |
| 2468 | if (ptime->start && ptime->start > t) |
| 2469 | goto out; |
| 2470 | |
Namhyung Kim | bdd7572 | 2016-12-22 15:03:49 +0900 | [diff] [blame] | 2471 | if (tprev && ptime->start > tprev) |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2472 | tprev = ptime->start; |
| 2473 | |
| 2474 | /* |
| 2475 | * If end time given: |
| 2476 | * - previous sched event is out of window - we are done |
| 2477 | * - sample time is beyond window user cares about - reset it |
| 2478 | * to close out stats for time window interest |
| 2479 | */ |
| 2480 | if (ptime->end) { |
| 2481 | if (tprev > ptime->end) |
| 2482 | goto out; |
| 2483 | |
| 2484 | if (t > ptime->end) |
| 2485 | t = ptime->end; |
| 2486 | } |
| 2487 | |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2488 | if (!sched->idle_hist || thread->tid == 0) { |
| 2489 | timehist_update_runtime_stats(tr, t, tprev); |
| 2490 | |
| 2491 | if (sched->idle_hist) { |
| 2492 | struct idle_thread_runtime *itr = (void *)tr; |
| 2493 | struct thread_runtime *last_tr; |
| 2494 | |
| 2495 | BUG_ON(thread->tid != 0); |
| 2496 | |
| 2497 | if (itr->last_thread == NULL) |
| 2498 | goto out; |
| 2499 | |
| 2500 | /* add current idle time as last thread's runtime */ |
| 2501 | last_tr = thread__get_runtime(itr->last_thread); |
| 2502 | if (last_tr == NULL) |
| 2503 | goto out; |
| 2504 | |
| 2505 | timehist_update_runtime_stats(last_tr, t, tprev); |
| 2506 | /* |
| 2507 | * remove delta time of last thread as it's not updated |
| 2508 | * and otherwise it will show an invalid value next |
| 2509 | * time. we only care total run time and run stat. |
| 2510 | */ |
| 2511 | last_tr->dt_run = 0; |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2512 | last_tr->dt_delay = 0; |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 2513 | last_tr->dt_sleep = 0; |
| 2514 | last_tr->dt_iowait = 0; |
| 2515 | last_tr->dt_preempt = 0; |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2516 | |
Namhyung Kim | ba957eb | 2016-12-08 23:47:55 +0900 | [diff] [blame] | 2517 | if (itr->cursor.nr) |
| 2518 | callchain_append(&itr->callchain, &itr->cursor, t - tprev); |
| 2519 | |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2520 | itr->last_thread = NULL; |
| 2521 | } |
| 2522 | } |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2523 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2524 | if (!sched->summary_only) |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 2525 | timehist_print_sample(sched, sample, &al, thread, t, state); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2526 | |
| 2527 | out: |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2528 | if (sched->hist_time.start == 0 && t >= ptime->start) |
| 2529 | sched->hist_time.start = t; |
| 2530 | if (ptime->end == 0 || t <= ptime->end) |
| 2531 | sched->hist_time.end = t; |
| 2532 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2533 | if (tr) { |
| 2534 | /* time of this sched_switch event becomes last time task seen */ |
| 2535 | tr->last_time = sample->time; |
| 2536 | |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 2537 | /* last state is used to determine where to account wait time */ |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 2538 | tr->last_state = state; |
Namhyung Kim | 941bdea | 2017-01-13 19:45:21 +0900 | [diff] [blame] | 2539 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2540 | /* sched out event for task so reset ready to run time */ |
| 2541 | tr->ready_to_run = 0; |
| 2542 | } |
| 2543 | |
| 2544 | perf_evsel__save_time(evsel, sample->time, sample->cpu); |
| 2545 | |
| 2546 | return rc; |
| 2547 | } |
| 2548 | |
| 2549 | static int timehist_sched_switch_event(struct perf_tool *tool, |
| 2550 | union perf_event *event, |
| 2551 | struct perf_evsel *evsel, |
| 2552 | struct perf_sample *sample, |
| 2553 | struct machine *machine __maybe_unused) |
| 2554 | { |
| 2555 | return timehist_sched_change_event(tool, event, evsel, sample, machine); |
| 2556 | } |
| 2557 | |
| 2558 | static int process_lost(struct perf_tool *tool __maybe_unused, |
| 2559 | union perf_event *event, |
| 2560 | struct perf_sample *sample, |
| 2561 | struct machine *machine __maybe_unused) |
| 2562 | { |
| 2563 | char tstr[64]; |
| 2564 | |
| 2565 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); |
| 2566 | printf("%15s ", tstr); |
| 2567 | printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu); |
| 2568 | |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
| 2572 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2573 | static void print_thread_runtime(struct thread *t, |
| 2574 | struct thread_runtime *r) |
| 2575 | { |
| 2576 | double mean = avg_stats(&r->run_stats); |
| 2577 | float stddev; |
| 2578 | |
| 2579 | printf("%*s %5d %9" PRIu64 " ", |
| 2580 | comm_width, timehist_get_commstr(t), t->ppid, |
| 2581 | (u64) r->run_stats.n); |
| 2582 | |
| 2583 | print_sched_time(r->total_run_time, 8); |
| 2584 | stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean); |
| 2585 | print_sched_time(r->run_stats.min, 6); |
| 2586 | printf(" "); |
| 2587 | print_sched_time((u64) mean, 6); |
| 2588 | printf(" "); |
| 2589 | print_sched_time(r->run_stats.max, 6); |
| 2590 | printf(" "); |
| 2591 | printf("%5.2f", stddev); |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2592 | printf(" %5" PRIu64, r->migrations); |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2593 | printf("\n"); |
| 2594 | } |
| 2595 | |
| 2596 | struct total_run_stats { |
| 2597 | u64 sched_count; |
| 2598 | u64 task_count; |
| 2599 | u64 total_run_time; |
| 2600 | }; |
| 2601 | |
| 2602 | static int __show_thread_runtime(struct thread *t, void *priv) |
| 2603 | { |
| 2604 | struct total_run_stats *stats = priv; |
| 2605 | struct thread_runtime *r; |
| 2606 | |
| 2607 | if (thread__is_filtered(t)) |
| 2608 | return 0; |
| 2609 | |
| 2610 | r = thread__priv(t); |
| 2611 | if (r && r->run_stats.n) { |
| 2612 | stats->task_count++; |
| 2613 | stats->sched_count += r->run_stats.n; |
| 2614 | stats->total_run_time += r->total_run_time; |
| 2615 | print_thread_runtime(t, r); |
| 2616 | } |
| 2617 | |
| 2618 | return 0; |
| 2619 | } |
| 2620 | |
| 2621 | static int show_thread_runtime(struct thread *t, void *priv) |
| 2622 | { |
| 2623 | if (t->dead) |
| 2624 | return 0; |
| 2625 | |
| 2626 | return __show_thread_runtime(t, priv); |
| 2627 | } |
| 2628 | |
| 2629 | static int show_deadthread_runtime(struct thread *t, void *priv) |
| 2630 | { |
| 2631 | if (!t->dead) |
| 2632 | return 0; |
| 2633 | |
| 2634 | return __show_thread_runtime(t, priv); |
| 2635 | } |
| 2636 | |
Namhyung Kim | ba957eb | 2016-12-08 23:47:55 +0900 | [diff] [blame] | 2637 | static size_t callchain__fprintf_folded(FILE *fp, struct callchain_node *node) |
| 2638 | { |
| 2639 | const char *sep = " <- "; |
| 2640 | struct callchain_list *chain; |
| 2641 | size_t ret = 0; |
| 2642 | char bf[1024]; |
| 2643 | bool first; |
| 2644 | |
| 2645 | if (node == NULL) |
| 2646 | return 0; |
| 2647 | |
| 2648 | ret = callchain__fprintf_folded(fp, node->parent); |
| 2649 | first = (ret == 0); |
| 2650 | |
| 2651 | list_for_each_entry(chain, &node->val, list) { |
| 2652 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 2653 | continue; |
| 2654 | if (chain->ms.sym && chain->ms.sym->ignore) |
| 2655 | continue; |
| 2656 | ret += fprintf(fp, "%s%s", first ? "" : sep, |
| 2657 | callchain_list__sym_name(chain, bf, sizeof(bf), |
| 2658 | false)); |
| 2659 | first = false; |
| 2660 | } |
| 2661 | |
| 2662 | return ret; |
| 2663 | } |
| 2664 | |
| 2665 | static size_t timehist_print_idlehist_callchain(struct rb_root *root) |
| 2666 | { |
| 2667 | size_t ret = 0; |
| 2668 | FILE *fp = stdout; |
| 2669 | struct callchain_node *chain; |
| 2670 | struct rb_node *rb_node = rb_first(root); |
| 2671 | |
| 2672 | printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains"); |
| 2673 | printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line, |
| 2674 | graph_dotted_line); |
| 2675 | |
| 2676 | while (rb_node) { |
| 2677 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
| 2678 | rb_node = rb_next(rb_node); |
| 2679 | |
| 2680 | ret += fprintf(fp, " "); |
| 2681 | print_sched_time(chain->hit, 12); |
| 2682 | ret += 16; /* print_sched_time returns 2nd arg + 4 */ |
| 2683 | ret += fprintf(fp, " %8d ", chain->count); |
| 2684 | ret += callchain__fprintf_folded(fp, chain); |
| 2685 | ret += fprintf(fp, "\n"); |
| 2686 | } |
| 2687 | |
| 2688 | return ret; |
| 2689 | } |
| 2690 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2691 | static void timehist_print_summary(struct perf_sched *sched, |
| 2692 | struct perf_session *session) |
| 2693 | { |
| 2694 | struct machine *m = &session->machines.host; |
| 2695 | struct total_run_stats totals; |
| 2696 | u64 task_count; |
| 2697 | struct thread *t; |
| 2698 | struct thread_runtime *r; |
| 2699 | int i; |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2700 | u64 hist_time = sched->hist_time.end - sched->hist_time.start; |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2701 | |
| 2702 | memset(&totals, 0, sizeof(totals)); |
| 2703 | |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2704 | if (sched->idle_hist) { |
| 2705 | printf("\nIdle-time summary\n"); |
| 2706 | printf("%*s parent sched-out ", comm_width, "comm"); |
| 2707 | printf(" idle-time min-idle avg-idle max-idle stddev migrations\n"); |
| 2708 | } else { |
| 2709 | printf("\nRuntime summary\n"); |
| 2710 | printf("%*s parent sched-in ", comm_width, "comm"); |
| 2711 | printf(" run-time min-run avg-run max-run stddev migrations\n"); |
| 2712 | } |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2713 | printf("%*s (count) ", comm_width, ""); |
| 2714 | printf(" (msec) (msec) (msec) (msec) %%\n"); |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2715 | printf("%.117s\n", graph_dotted_line); |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2716 | |
| 2717 | machine__for_each_thread(m, show_thread_runtime, &totals); |
| 2718 | task_count = totals.task_count; |
| 2719 | if (!task_count) |
| 2720 | printf("<no still running tasks>\n"); |
| 2721 | |
| 2722 | printf("\nTerminated tasks:\n"); |
| 2723 | machine__for_each_thread(m, show_deadthread_runtime, &totals); |
| 2724 | if (task_count == totals.task_count) |
| 2725 | printf("<no terminated tasks>\n"); |
| 2726 | |
| 2727 | /* CPU idle stats not tracked when samples were skipped */ |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 2728 | if (sched->skipped_samples && !sched->idle_hist) |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2729 | return; |
| 2730 | |
| 2731 | printf("\nIdle stats:\n"); |
Namhyung Kim | b336352 | 2016-12-06 12:40:05 +0900 | [diff] [blame] | 2732 | for (i = 0; i < idle_max_cpu; ++i) { |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2733 | t = idle_threads[i]; |
| 2734 | if (!t) |
| 2735 | continue; |
| 2736 | |
| 2737 | r = thread__priv(t); |
| 2738 | if (r && r->run_stats.n) { |
| 2739 | totals.sched_count += r->run_stats.n; |
| 2740 | printf(" CPU %2d idle for ", i); |
| 2741 | print_sched_time(r->total_run_time, 6); |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2742 | printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time); |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2743 | } else |
| 2744 | printf(" CPU %2d idle entire time window\n", i); |
| 2745 | } |
| 2746 | |
Namhyung Kim | ba957eb | 2016-12-08 23:47:55 +0900 | [diff] [blame] | 2747 | if (sched->idle_hist && symbol_conf.use_callchain) { |
| 2748 | callchain_param.mode = CHAIN_FOLDED; |
| 2749 | callchain_param.value = CCVAL_PERIOD; |
| 2750 | |
| 2751 | callchain_register_param(&callchain_param); |
| 2752 | |
| 2753 | printf("\nIdle stats by callchain:\n"); |
| 2754 | for (i = 0; i < idle_max_cpu; ++i) { |
| 2755 | struct idle_thread_runtime *itr; |
| 2756 | |
| 2757 | t = idle_threads[i]; |
| 2758 | if (!t) |
| 2759 | continue; |
| 2760 | |
| 2761 | itr = thread__priv(t); |
| 2762 | if (itr == NULL) |
| 2763 | continue; |
| 2764 | |
| 2765 | callchain_param.sort(&itr->sorted_root, &itr->callchain, |
| 2766 | 0, &callchain_param); |
| 2767 | |
| 2768 | printf(" CPU %2d:", i); |
| 2769 | print_sched_time(itr->tr.total_run_time, 6); |
| 2770 | printf(" msec\n"); |
| 2771 | timehist_print_idlehist_callchain(&itr->sorted_root); |
| 2772 | printf("\n"); |
| 2773 | } |
| 2774 | } |
| 2775 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2776 | printf("\n" |
| 2777 | " Total number of unique tasks: %" PRIu64 "\n" |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2778 | "Total number of context switches: %" PRIu64 "\n", |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2779 | totals.task_count, totals.sched_count); |
| 2780 | |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2781 | printf(" Total run time (msec): "); |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2782 | print_sched_time(totals.total_run_time, 2); |
| 2783 | printf("\n"); |
Namhyung Kim | 9396c9c | 2016-12-22 15:03:50 +0900 | [diff] [blame] | 2784 | |
| 2785 | printf(" Total scheduling time (msec): "); |
| 2786 | print_sched_time(hist_time, 2); |
| 2787 | printf(" (x %d)\n", sched->max_cpu); |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2788 | } |
| 2789 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2790 | typedef int (*sched_handler)(struct perf_tool *tool, |
| 2791 | union perf_event *event, |
| 2792 | struct perf_evsel *evsel, |
| 2793 | struct perf_sample *sample, |
| 2794 | struct machine *machine); |
| 2795 | |
| 2796 | static int perf_timehist__process_sample(struct perf_tool *tool, |
| 2797 | union perf_event *event, |
| 2798 | struct perf_sample *sample, |
| 2799 | struct perf_evsel *evsel, |
| 2800 | struct machine *machine) |
| 2801 | { |
| 2802 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
| 2803 | int err = 0; |
| 2804 | int this_cpu = sample->cpu; |
| 2805 | |
| 2806 | if (this_cpu > sched->max_cpu) |
| 2807 | sched->max_cpu = this_cpu; |
| 2808 | |
| 2809 | if (evsel->handler != NULL) { |
| 2810 | sched_handler f = evsel->handler; |
| 2811 | |
| 2812 | err = f(tool, event, evsel, sample, machine); |
| 2813 | } |
| 2814 | |
| 2815 | return err; |
| 2816 | } |
| 2817 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2818 | static int timehist_check_attr(struct perf_sched *sched, |
| 2819 | struct perf_evlist *evlist) |
| 2820 | { |
| 2821 | struct perf_evsel *evsel; |
| 2822 | struct evsel_runtime *er; |
| 2823 | |
| 2824 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 2825 | er = perf_evsel__get_runtime(evsel); |
| 2826 | if (er == NULL) { |
| 2827 | pr_err("Failed to allocate memory for evsel runtime data\n"); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
| 2831 | if (sched->show_callchain && |
| 2832 | !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) { |
| 2833 | pr_info("Samples do not have callchains.\n"); |
| 2834 | sched->show_callchain = 0; |
| 2835 | symbol_conf.use_callchain = 0; |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | return 0; |
| 2840 | } |
| 2841 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2842 | static int perf_sched__timehist(struct perf_sched *sched) |
| 2843 | { |
| 2844 | const struct perf_evsel_str_handler handlers[] = { |
| 2845 | { "sched:sched_switch", timehist_sched_switch_event, }, |
| 2846 | { "sched:sched_wakeup", timehist_sched_wakeup_event, }, |
| 2847 | { "sched:sched_wakeup_new", timehist_sched_wakeup_event, }, |
| 2848 | }; |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2849 | const struct perf_evsel_str_handler migrate_handlers[] = { |
| 2850 | { "sched:sched_migrate_task", timehist_migrate_task_event, }, |
| 2851 | }; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2852 | struct perf_data_file file = { |
| 2853 | .path = input_name, |
| 2854 | .mode = PERF_DATA_MODE_READ, |
Namhyung Kim | 6fa9425 | 2016-12-06 12:40:01 +0900 | [diff] [blame] | 2855 | .force = sched->force, |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2856 | }; |
| 2857 | |
| 2858 | struct perf_session *session; |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2859 | struct perf_evlist *evlist; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2860 | int err = -1; |
| 2861 | |
| 2862 | /* |
| 2863 | * event handlers for timehist option |
| 2864 | */ |
| 2865 | sched->tool.sample = perf_timehist__process_sample; |
| 2866 | sched->tool.mmap = perf_event__process_mmap; |
| 2867 | sched->tool.comm = perf_event__process_comm; |
| 2868 | sched->tool.exit = perf_event__process_exit; |
| 2869 | sched->tool.fork = perf_event__process_fork; |
| 2870 | sched->tool.lost = process_lost; |
| 2871 | sched->tool.attr = perf_event__process_attr; |
| 2872 | sched->tool.tracing_data = perf_event__process_tracing_data; |
| 2873 | sched->tool.build_id = perf_event__process_build_id; |
| 2874 | |
| 2875 | sched->tool.ordered_events = true; |
| 2876 | sched->tool.ordering_requires_timestamps = true; |
| 2877 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2878 | symbol_conf.use_callchain = sched->show_callchain; |
| 2879 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2880 | session = perf_session__new(&file, false, &sched->tool); |
| 2881 | if (session == NULL) |
| 2882 | return -ENOMEM; |
| 2883 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2884 | evlist = session->evlist; |
| 2885 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2886 | symbol__init(&session->header.env); |
| 2887 | |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 2888 | if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) { |
| 2889 | pr_err("Invalid time string\n"); |
| 2890 | return -EINVAL; |
| 2891 | } |
| 2892 | |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 2893 | if (timehist_check_attr(sched, evlist) != 0) |
| 2894 | goto out; |
| 2895 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2896 | setup_pager(); |
| 2897 | |
| 2898 | /* setup per-evsel handlers */ |
| 2899 | if (perf_session__set_tracepoints_handlers(session, handlers)) |
| 2900 | goto out; |
| 2901 | |
David Ahern | f45bf8d | 2016-11-29 13:39:48 -0700 | [diff] [blame] | 2902 | /* sched_switch event at a minimum needs to exist */ |
| 2903 | if (!perf_evlist__find_tracepoint_by_name(session->evlist, |
| 2904 | "sched:sched_switch")) { |
| 2905 | pr_err("No sched_switch events found. Have you run 'perf sched record'?\n"); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2906 | goto out; |
David Ahern | f45bf8d | 2016-11-29 13:39:48 -0700 | [diff] [blame] | 2907 | } |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2908 | |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 2909 | if (sched->show_migrations && |
| 2910 | perf_session__set_tracepoints_handlers(session, migrate_handlers)) |
| 2911 | goto out; |
| 2912 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2913 | /* pre-allocate struct for per-CPU idle stats */ |
| 2914 | sched->max_cpu = session->header.env.nr_cpus_online; |
| 2915 | if (sched->max_cpu == 0) |
| 2916 | sched->max_cpu = 4; |
| 2917 | if (init_idle_threads(sched->max_cpu)) |
| 2918 | goto out; |
| 2919 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2920 | /* summary_only implies summary option, but don't overwrite summary if set */ |
| 2921 | if (sched->summary_only) |
| 2922 | sched->summary = sched->summary_only; |
| 2923 | |
| 2924 | if (!sched->summary_only) |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 2925 | timehist_header(sched); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2926 | |
| 2927 | err = perf_session__process_events(session); |
| 2928 | if (err) { |
| 2929 | pr_err("Failed to process events, error %d", err); |
| 2930 | goto out; |
| 2931 | } |
| 2932 | |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 2933 | sched->nr_events = evlist->stats.nr_events[0]; |
| 2934 | sched->nr_lost_events = evlist->stats.total_lost; |
| 2935 | sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST]; |
| 2936 | |
| 2937 | if (sched->summary) |
| 2938 | timehist_print_summary(sched, session); |
| 2939 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 2940 | out: |
| 2941 | free_idle_threads(); |
| 2942 | perf_session__delete(session); |
| 2943 | |
| 2944 | return err; |
| 2945 | } |
| 2946 | |
| 2947 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2948 | static void print_bad_events(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2949 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2950 | if (sched->nr_unordered_timestamps && sched->nr_timestamps) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2951 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2952 | (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, |
| 2953 | sched->nr_unordered_timestamps, sched->nr_timestamps); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2954 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2955 | if (sched->nr_lost_events && sched->nr_events) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2956 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2957 | (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, |
| 2958 | sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2959 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2960 | if (sched->nr_context_switch_bugs && sched->nr_timestamps) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2961 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 2962 | (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, |
| 2963 | sched->nr_context_switch_bugs, sched->nr_timestamps); |
| 2964 | if (sched->nr_lost_events) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 2965 | printf(" (due to lost events?)"); |
| 2966 | printf("\n"); |
| 2967 | } |
| 2968 | } |
| 2969 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 2970 | static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data) |
| 2971 | { |
| 2972 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2973 | struct work_atoms *this; |
| 2974 | const char *comm = thread__comm_str(data->thread), *this_comm; |
| 2975 | |
| 2976 | while (*new) { |
| 2977 | int cmp; |
| 2978 | |
| 2979 | this = container_of(*new, struct work_atoms, node); |
| 2980 | parent = *new; |
| 2981 | |
| 2982 | this_comm = thread__comm_str(this->thread); |
| 2983 | cmp = strcmp(comm, this_comm); |
| 2984 | if (cmp > 0) { |
| 2985 | new = &((*new)->rb_left); |
| 2986 | } else if (cmp < 0) { |
| 2987 | new = &((*new)->rb_right); |
| 2988 | } else { |
| 2989 | this->num_merged++; |
| 2990 | this->total_runtime += data->total_runtime; |
| 2991 | this->nb_atoms += data->nb_atoms; |
| 2992 | this->total_lat += data->total_lat; |
| 2993 | list_splice(&data->work_list, &this->work_list); |
| 2994 | if (this->max_lat < data->max_lat) { |
| 2995 | this->max_lat = data->max_lat; |
| 2996 | this->max_lat_at = data->max_lat_at; |
| 2997 | } |
| 2998 | zfree(&data); |
| 2999 | return; |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | data->num_merged++; |
| 3004 | rb_link_node(&data->node, parent, new); |
| 3005 | rb_insert_color(&data->node, root); |
| 3006 | } |
| 3007 | |
| 3008 | static void perf_sched__merge_lat(struct perf_sched *sched) |
| 3009 | { |
| 3010 | struct work_atoms *data; |
| 3011 | struct rb_node *node; |
| 3012 | |
| 3013 | if (sched->skip_merge) |
| 3014 | return; |
| 3015 | |
| 3016 | while ((node = rb_first(&sched->atom_root))) { |
| 3017 | rb_erase(node, &sched->atom_root); |
| 3018 | data = rb_entry(node, struct work_atoms, node); |
| 3019 | __merge_work_atoms(&sched->merged_atom_root, data); |
| 3020 | } |
| 3021 | } |
| 3022 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3023 | static int perf_sched__lat(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3024 | { |
| 3025 | struct rb_node *next; |
| 3026 | |
| 3027 | setup_pager(); |
David Ahern | ad9def7 | 2013-08-07 22:50:44 -0400 | [diff] [blame] | 3028 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 3029 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3030 | return -1; |
David Ahern | ad9def7 | 2013-08-07 22:50:44 -0400 | [diff] [blame] | 3031 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 3032 | perf_sched__merge_lat(sched); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3033 | perf_sched__sort_lat(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3034 | |
Ramkumar Ramachandra | 80790e0 | 2014-03-17 10:18:21 -0400 | [diff] [blame] | 3035 | printf("\n -----------------------------------------------------------------------------------------------------------------\n"); |
| 3036 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n"); |
| 3037 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3038 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3039 | next = rb_first(&sched->sorted_atom_root); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3040 | |
| 3041 | while (next) { |
| 3042 | struct work_atoms *work_list; |
| 3043 | |
| 3044 | work_list = rb_entry(next, struct work_atoms, node); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3045 | output_lat_thread(sched, work_list); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3046 | next = rb_next(next); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 3047 | thread__zput(work_list->thread); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3048 | } |
| 3049 | |
Ramkumar Ramachandra | 80790e0 | 2014-03-17 10:18:21 -0400 | [diff] [blame] | 3050 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 3051 | printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n", |
Arnaldo Carvalho de Melo | 4fc76e4 | 2016-08-08 12:23:49 -0300 | [diff] [blame] | 3052 | (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3053 | |
| 3054 | printf(" ---------------------------------------------------\n"); |
| 3055 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3056 | print_bad_events(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3057 | printf("\n"); |
| 3058 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3059 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3062 | static int setup_map_cpus(struct perf_sched *sched) |
| 3063 | { |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 3064 | struct cpu_map *map; |
| 3065 | |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3066 | sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); |
| 3067 | |
| 3068 | if (sched->map.comp) { |
| 3069 | sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int)); |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 3070 | if (!sched->map.comp_cpus) |
| 3071 | return -1; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3072 | } |
| 3073 | |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 3074 | if (!sched->map.cpus_str) |
| 3075 | return 0; |
| 3076 | |
| 3077 | map = cpu_map__new(sched->map.cpus_str); |
| 3078 | if (!map) { |
| 3079 | pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); |
| 3080 | return -1; |
| 3081 | } |
| 3082 | |
| 3083 | sched->map.cpus = map; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3084 | return 0; |
| 3085 | } |
| 3086 | |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 3087 | static int setup_color_pids(struct perf_sched *sched) |
| 3088 | { |
| 3089 | struct thread_map *map; |
| 3090 | |
| 3091 | if (!sched->map.color_pids_str) |
| 3092 | return 0; |
| 3093 | |
| 3094 | map = thread_map__new_by_tid_str(sched->map.color_pids_str); |
| 3095 | if (!map) { |
| 3096 | pr_err("failed to get thread map from %s\n", sched->map.color_pids_str); |
| 3097 | return -1; |
| 3098 | } |
| 3099 | |
| 3100 | sched->map.color_pids = map; |
| 3101 | return 0; |
| 3102 | } |
| 3103 | |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 3104 | static int setup_color_cpus(struct perf_sched *sched) |
| 3105 | { |
| 3106 | struct cpu_map *map; |
| 3107 | |
| 3108 | if (!sched->map.color_cpus_str) |
| 3109 | return 0; |
| 3110 | |
| 3111 | map = cpu_map__new(sched->map.color_cpus_str); |
| 3112 | if (!map) { |
| 3113 | pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str); |
| 3114 | return -1; |
| 3115 | } |
| 3116 | |
| 3117 | sched->map.color_cpus = map; |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3121 | static int perf_sched__map(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3122 | { |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3123 | if (setup_map_cpus(sched)) |
| 3124 | return -1; |
Ingo Molnar | 40749d0 | 2009-09-17 18:24:55 +0200 | [diff] [blame] | 3125 | |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 3126 | if (setup_color_pids(sched)) |
| 3127 | return -1; |
| 3128 | |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 3129 | if (setup_color_cpus(sched)) |
| 3130 | return -1; |
| 3131 | |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3132 | setup_pager(); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 3133 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3134 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3135 | print_bad_events(sched); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3136 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3137 | } |
| 3138 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3139 | static int perf_sched__replay(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3140 | { |
| 3141 | unsigned long i; |
| 3142 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3143 | calibrate_run_measurement_overhead(sched); |
| 3144 | calibrate_sleep_measurement_overhead(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3145 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3146 | test_calibrations(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3147 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 3148 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3149 | return -1; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3150 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3151 | printf("nr_run_events: %ld\n", sched->nr_run_events); |
| 3152 | printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); |
| 3153 | printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3154 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3155 | if (sched->targetless_wakeups) |
| 3156 | printf("target-less wakeups: %ld\n", sched->targetless_wakeups); |
| 3157 | if (sched->multitarget_wakeups) |
| 3158 | printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); |
| 3159 | if (sched->nr_run_events_optimized) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3160 | printf("run atoms optimized: %ld\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3161 | sched->nr_run_events_optimized); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3162 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3163 | print_task_traces(sched); |
| 3164 | add_cross_task_wakeups(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3165 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3166 | create_tasks(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3167 | printf("------------------------------------------------------------\n"); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3168 | for (i = 0; i < sched->replay_repeat; i++) |
| 3169 | run_one_test(sched); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 3170 | |
| 3171 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3172 | } |
| 3173 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3174 | static void setup_sorting(struct perf_sched *sched, const struct option *options, |
| 3175 | const char * const usage_msg[]) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 3176 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3177 | char *tmp, *tok, *str = strdup(sched->sort_order); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 3178 | |
| 3179 | for (tok = strtok_r(str, ", ", &tmp); |
| 3180 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3181 | if (sort_dimension__add(tok, &sched->sort_list) < 0) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 3182 | usage_with_options_msg(usage_msg, options, |
| 3183 | "Unknown --sort key: `%s'", tok); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | free(str); |
| 3188 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3189 | sort_dimension__add("pid", &sched->cmp_pid); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 3192 | static int __cmd_record(int argc, const char **argv) |
| 3193 | { |
| 3194 | unsigned int rec_argc, i, j; |
| 3195 | const char **rec_argv; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3196 | const char * const record_args[] = { |
| 3197 | "record", |
| 3198 | "-a", |
| 3199 | "-R", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3200 | "-m", "1024", |
| 3201 | "-c", "1", |
| 3202 | "-e", "sched:sched_switch", |
| 3203 | "-e", "sched:sched_stat_wait", |
| 3204 | "-e", "sched:sched_stat_sleep", |
| 3205 | "-e", "sched:sched_stat_iowait", |
| 3206 | "-e", "sched:sched_stat_runtime", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3207 | "-e", "sched:sched_process_fork", |
| 3208 | "-e", "sched:sched_wakeup", |
Dongsheng | 7fff959 | 2014-05-05 16:05:53 +0900 | [diff] [blame] | 3209 | "-e", "sched:sched_wakeup_new", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3210 | "-e", "sched:sched_migrate_task", |
| 3211 | }; |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 3212 | |
| 3213 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
| 3214 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 3215 | |
Arnaldo Carvalho de Melo | e462dc5 | 2011-01-10 10:48:47 -0200 | [diff] [blame] | 3216 | if (rec_argv == NULL) |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 3217 | return -ENOMEM; |
| 3218 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 3219 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 3220 | rec_argv[i] = strdup(record_args[i]); |
| 3221 | |
| 3222 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 3223 | rec_argv[i] = argv[j]; |
| 3224 | |
| 3225 | BUG_ON(i != rec_argc); |
| 3226 | |
| 3227 | return cmd_record(i, rec_argv, NULL); |
| 3228 | } |
| 3229 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 3230 | int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused) |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3231 | { |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 3232 | const char default_sort_order[] = "avg, max, switch, runtime"; |
| 3233 | struct perf_sched sched = { |
| 3234 | .tool = { |
| 3235 | .sample = perf_sched__process_tracepoint_sample, |
| 3236 | .comm = perf_event__process_comm, |
| 3237 | .lost = perf_event__process_lost, |
| 3238 | .fork = perf_sched__process_fork_event, |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 3239 | .ordered_events = true, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 3240 | }, |
| 3241 | .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid), |
| 3242 | .sort_list = LIST_HEAD_INIT(sched.sort_list), |
| 3243 | .start_work_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 3244 | .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 3245 | .sort_order = default_sort_order, |
| 3246 | .replay_repeat = 10, |
| 3247 | .profile_cpu = -1, |
| 3248 | .next_shortname1 = 'A', |
| 3249 | .next_shortname2 = '0', |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 3250 | .skip_merge = 0, |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 3251 | .show_callchain = 1, |
| 3252 | .max_stack = 5, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 3253 | }; |
Namhyung Kim | 77f02f4 | 2016-10-24 12:00:03 +0900 | [diff] [blame] | 3254 | const struct option sched_options[] = { |
| 3255 | OPT_STRING('i', "input", &input_name, "file", |
| 3256 | "input file name"), |
| 3257 | OPT_INCR('v', "verbose", &verbose, |
| 3258 | "be more verbose (show symbol address, etc)"), |
| 3259 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 3260 | "dump raw trace in ASCII"), |
Namhyung Kim | 6fa9425 | 2016-12-06 12:40:01 +0900 | [diff] [blame] | 3261 | OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"), |
Namhyung Kim | 77f02f4 | 2016-10-24 12:00:03 +0900 | [diff] [blame] | 3262 | OPT_END() |
| 3263 | }; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3264 | const struct option latency_options[] = { |
| 3265 | OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]", |
| 3266 | "sort by key(s): runtime, switch, avg, max"), |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3267 | OPT_INTEGER('C', "CPU", &sched.profile_cpu, |
| 3268 | "CPU to profile on"), |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 3269 | OPT_BOOLEAN('p', "pids", &sched.skip_merge, |
| 3270 | "latency stats per pid instead of per comm"), |
Namhyung Kim | 77f02f4 | 2016-10-24 12:00:03 +0900 | [diff] [blame] | 3271 | OPT_PARENT(sched_options) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3272 | }; |
| 3273 | const struct option replay_options[] = { |
| 3274 | OPT_UINTEGER('r', "repeat", &sched.replay_repeat, |
| 3275 | "repeat the workload replay N times (-1: infinite)"), |
Namhyung Kim | 77f02f4 | 2016-10-24 12:00:03 +0900 | [diff] [blame] | 3276 | OPT_PARENT(sched_options) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3277 | }; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3278 | const struct option map_options[] = { |
| 3279 | OPT_BOOLEAN(0, "compact", &sched.map.comp, |
| 3280 | "map output in compact mode"), |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 3281 | OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids", |
| 3282 | "highlight given pids in map"), |
Jiri Olsa | cf294f2 | 2016-04-12 15:29:30 +0200 | [diff] [blame] | 3283 | OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus", |
| 3284 | "highlight given CPUs in map"), |
Jiri Olsa | 73643bb | 2016-04-12 15:29:31 +0200 | [diff] [blame] | 3285 | OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus", |
| 3286 | "display given CPUs in map"), |
Namhyung Kim | 77f02f4 | 2016-10-24 12:00:03 +0900 | [diff] [blame] | 3287 | OPT_PARENT(sched_options) |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3288 | }; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3289 | const struct option timehist_options[] = { |
| 3290 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 3291 | "file", "vmlinux pathname"), |
| 3292 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, |
| 3293 | "file", "kallsyms pathname"), |
David Ahern | 6c973c9 | 2016-11-16 15:06:32 +0900 | [diff] [blame] | 3294 | OPT_BOOLEAN('g', "call-graph", &sched.show_callchain, |
| 3295 | "Display call chains if present (default on)"), |
| 3296 | OPT_UINTEGER(0, "max-stack", &sched.max_stack, |
| 3297 | "Maximum number of functions to display backtrace."), |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3298 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
| 3299 | "Look for files with symbols relative to this directory"), |
David Ahern | 52df138 | 2016-11-16 15:06:30 +0900 | [diff] [blame] | 3300 | OPT_BOOLEAN('s', "summary", &sched.summary_only, |
| 3301 | "Show only syscall summary with statistics"), |
| 3302 | OPT_BOOLEAN('S', "with-summary", &sched.summary, |
| 3303 | "Show all syscalls and summary with statistics"), |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 3304 | OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"), |
David Ahern | 350f54f | 2016-11-25 09:28:41 -0700 | [diff] [blame] | 3305 | OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"), |
David Ahern | a407b06 | 2016-11-16 15:06:33 +0900 | [diff] [blame] | 3306 | OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"), |
Namhyung Kim | 07235f8 | 2016-12-08 23:47:54 +0900 | [diff] [blame] | 3307 | OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"), |
David Ahern | 853b740 | 2016-11-29 10:15:44 -0700 | [diff] [blame] | 3308 | OPT_STRING(0, "time", &sched.time_str, "str", |
| 3309 | "Time span for analysis (start,stop)"), |
Namhyung Kim | 414e050 | 2017-01-13 19:45:22 +0900 | [diff] [blame^] | 3310 | OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"), |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3311 | OPT_PARENT(sched_options) |
| 3312 | }; |
| 3313 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3314 | const char * const latency_usage[] = { |
| 3315 | "perf sched latency [<options>]", |
| 3316 | NULL |
| 3317 | }; |
| 3318 | const char * const replay_usage[] = { |
| 3319 | "perf sched replay [<options>]", |
| 3320 | NULL |
| 3321 | }; |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3322 | const char * const map_usage[] = { |
| 3323 | "perf sched map [<options>]", |
| 3324 | NULL |
| 3325 | }; |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3326 | const char * const timehist_usage[] = { |
| 3327 | "perf sched timehist [<options>]", |
| 3328 | NULL |
| 3329 | }; |
Ramkumar Ramachandra | a83edb2 | 2014-03-14 23:17:54 -0400 | [diff] [blame] | 3330 | const char *const sched_subcommands[] = { "record", "latency", "map", |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3331 | "replay", "script", |
| 3332 | "timehist", NULL }; |
Ramkumar Ramachandra | a83edb2 | 2014-03-14 23:17:54 -0400 | [diff] [blame] | 3333 | const char *sched_usage[] = { |
| 3334 | NULL, |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3335 | NULL |
| 3336 | }; |
| 3337 | struct trace_sched_handler lat_ops = { |
| 3338 | .wakeup_event = latency_wakeup_event, |
| 3339 | .switch_event = latency_switch_event, |
| 3340 | .runtime_event = latency_runtime_event, |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3341 | .migrate_task_event = latency_migrate_task_event, |
| 3342 | }; |
| 3343 | struct trace_sched_handler map_ops = { |
| 3344 | .switch_event = map_switch_event, |
| 3345 | }; |
| 3346 | struct trace_sched_handler replay_ops = { |
| 3347 | .wakeup_event = replay_wakeup_event, |
| 3348 | .switch_event = replay_switch_event, |
| 3349 | .fork_event = replay_fork_event, |
| 3350 | }; |
Adrian Hunter | 156a2b0 | 2013-10-22 10:34:16 +0300 | [diff] [blame] | 3351 | unsigned int i; |
| 3352 | |
| 3353 | for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++) |
| 3354 | sched.curr_pid[i] = -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3355 | |
Ramkumar Ramachandra | a83edb2 | 2014-03-14 23:17:54 -0400 | [diff] [blame] | 3356 | argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands, |
| 3357 | sched_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3358 | if (!argc) |
| 3359 | usage_with_options(sched_usage, sched_options); |
| 3360 | |
Xiao Guangrong | c0777c5 | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 3361 | /* |
Ingo Molnar | 133dc4c3 | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 3362 | * Aliased to 'perf script' for now: |
Xiao Guangrong | c0777c5 | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 3363 | */ |
Ingo Molnar | 133dc4c3 | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 3364 | if (!strcmp(argv[0], "script")) |
| 3365 | return cmd_script(argc, argv, prefix); |
Xiao Guangrong | c0777c5 | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 3366 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 3367 | if (!strncmp(argv[0], "rec", 3)) { |
| 3368 | return __cmd_record(argc, argv); |
| 3369 | } else if (!strncmp(argv[0], "lat", 3)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3370 | sched.tp_handler = &lat_ops; |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3371 | if (argc > 1) { |
| 3372 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); |
| 3373 | if (argc) |
| 3374 | usage_with_options(latency_usage, latency_options); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3375 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3376 | setup_sorting(&sched, latency_options, latency_usage); |
| 3377 | return perf_sched__lat(&sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 3378 | } else if (!strcmp(argv[0], "map")) { |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3379 | if (argc) { |
Jiri Olsa | a151a37 | 2016-04-12 15:29:29 +0200 | [diff] [blame] | 3380 | argc = parse_options(argc, argv, map_options, map_usage, 0); |
Jiri Olsa | 99623c6 | 2016-04-12 15:29:26 +0200 | [diff] [blame] | 3381 | if (argc) |
| 3382 | usage_with_options(map_usage, map_options); |
| 3383 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3384 | sched.tp_handler = &map_ops; |
| 3385 | setup_sorting(&sched, latency_options, latency_usage); |
| 3386 | return perf_sched__map(&sched); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3387 | } else if (!strncmp(argv[0], "rep", 3)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3388 | sched.tp_handler = &replay_ops; |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3389 | if (argc) { |
| 3390 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); |
| 3391 | if (argc) |
| 3392 | usage_with_options(replay_usage, replay_options); |
| 3393 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 3394 | return perf_sched__replay(&sched); |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3395 | } else if (!strcmp(argv[0], "timehist")) { |
| 3396 | if (argc) { |
| 3397 | argc = parse_options(argc, argv, timehist_options, |
| 3398 | timehist_usage, 0); |
| 3399 | if (argc) |
| 3400 | usage_with_options(timehist_usage, timehist_options); |
| 3401 | } |
David Ahern | fc1469f | 2016-11-16 15:06:31 +0900 | [diff] [blame] | 3402 | if (sched.show_wakeups && sched.summary_only) { |
| 3403 | pr_err(" Error: -s and -w are mutually exclusive.\n"); |
| 3404 | parse_options_usage(timehist_usage, timehist_options, "s", true); |
| 3405 | parse_options_usage(NULL, timehist_options, "w", true); |
| 3406 | return -EINVAL; |
| 3407 | } |
| 3408 | |
David Ahern | 49394a2 | 2016-11-16 15:06:29 +0900 | [diff] [blame] | 3409 | return perf_sched__timehist(&sched); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3410 | } else { |
| 3411 | usage_with_options(sched_usage, sched_options); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3414 | return 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 3415 | } |