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