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