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" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 14 | |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 15 | #include <subcmd/parse-options.h> |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 16 | #include "util/trace-event.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 17 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 18 | #include "util/debug.h" |
| 19 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 20 | #include <sys/prctl.h> |
Markus Trippelsdorf | 7b78f13 | 2012-04-04 10:45:27 +0200 | [diff] [blame] | 21 | #include <sys/resource.h> |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 23 | #include <semaphore.h> |
| 24 | #include <pthread.h> |
| 25 | #include <math.h> |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 26 | #include <api/fs/fs.h> |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 27 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 28 | #define PR_SET_NAME 15 /* Set process name */ |
| 29 | #define MAX_CPUS 4096 |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 30 | #define COMM_LEN 20 |
| 31 | #define SYM_LEN 129 |
Yunlong Song | a35e27d | 2015-03-31 21:46:29 +0800 | [diff] [blame] | 32 | #define MAX_PID 1024000 |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 33 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 34 | struct sched_atom; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 35 | |
| 36 | struct task_desc { |
| 37 | unsigned long nr; |
| 38 | unsigned long pid; |
| 39 | char comm[COMM_LEN]; |
| 40 | |
| 41 | unsigned long nr_events; |
| 42 | unsigned long curr_event; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 43 | struct sched_atom **atoms; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 44 | |
| 45 | pthread_t thread; |
| 46 | sem_t sleep_sem; |
| 47 | |
| 48 | sem_t ready_for_work; |
| 49 | sem_t work_done_sem; |
| 50 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 51 | u64 cpu_usage; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | enum sched_event_type { |
| 55 | SCHED_EVENT_RUN, |
| 56 | SCHED_EVENT_SLEEP, |
| 57 | SCHED_EVENT_WAKEUP, |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 58 | SCHED_EVENT_MIGRATION, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 61 | struct sched_atom { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 62 | enum sched_event_type type; |
Arnaldo Carvalho de Melo | eed05fe | 2010-04-05 12:53:45 -0300 | [diff] [blame] | 63 | int specific_wait; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 64 | u64 timestamp; |
| 65 | u64 duration; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 66 | unsigned long nr; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 67 | sem_t *wait_sem; |
| 68 | struct task_desc *wakee; |
| 69 | }; |
| 70 | |
Dongsheng | e936e8e | 2014-05-05 16:05:54 +0900 | [diff] [blame] | 71 | #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP" |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 72 | |
| 73 | enum thread_state { |
| 74 | THREAD_SLEEPING = 0, |
| 75 | THREAD_WAIT_CPU, |
| 76 | THREAD_SCHED_IN, |
| 77 | THREAD_IGNORE |
| 78 | }; |
| 79 | |
| 80 | struct work_atom { |
| 81 | struct list_head list; |
| 82 | enum thread_state state; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 83 | u64 sched_out_time; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 84 | u64 wake_up_time; |
| 85 | u64 sched_in_time; |
| 86 | u64 runtime; |
| 87 | }; |
| 88 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 89 | struct work_atoms { |
| 90 | struct list_head work_list; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 91 | struct thread *thread; |
| 92 | struct rb_node node; |
| 93 | u64 max_lat; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 94 | u64 max_lat_at; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 95 | u64 total_lat; |
| 96 | u64 nb_atoms; |
| 97 | u64 total_runtime; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 98 | int num_merged; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 101 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 102 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 103 | struct perf_sched; |
| 104 | |
| 105 | struct trace_sched_handler { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 106 | int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
| 107 | struct perf_sample *sample, struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 108 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 109 | int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
| 110 | struct perf_sample *sample, struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 112 | int (*wakeup_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 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 115 | /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */ |
| 116 | int (*fork_event)(struct perf_sched *sched, union perf_event *event, |
| 117 | struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 118 | |
| 119 | int (*migrate_task_event)(struct perf_sched *sched, |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 120 | struct perf_evsel *evsel, |
| 121 | struct perf_sample *sample, |
| 122 | struct machine *machine); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | struct perf_sched { |
| 126 | struct perf_tool tool; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 127 | const char *sort_order; |
| 128 | unsigned long nr_tasks; |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 129 | struct task_desc **pid_to_task; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 130 | struct task_desc **tasks; |
| 131 | const struct trace_sched_handler *tp_handler; |
| 132 | pthread_mutex_t start_work_mutex; |
| 133 | pthread_mutex_t work_done_wait_mutex; |
| 134 | int profile_cpu; |
| 135 | /* |
| 136 | * Track the current task - that way we can know whether there's any |
| 137 | * weird events, such as a task being switched away that is not current. |
| 138 | */ |
| 139 | int max_cpu; |
| 140 | u32 curr_pid[MAX_CPUS]; |
| 141 | struct thread *curr_thread[MAX_CPUS]; |
| 142 | char next_shortname1; |
| 143 | char next_shortname2; |
| 144 | unsigned int replay_repeat; |
| 145 | unsigned long nr_run_events; |
| 146 | unsigned long nr_sleep_events; |
| 147 | unsigned long nr_wakeup_events; |
| 148 | unsigned long nr_sleep_corrections; |
| 149 | unsigned long nr_run_events_optimized; |
| 150 | unsigned long targetless_wakeups; |
| 151 | unsigned long multitarget_wakeups; |
| 152 | unsigned long nr_runs; |
| 153 | unsigned long nr_timestamps; |
| 154 | unsigned long nr_unordered_timestamps; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 155 | unsigned long nr_context_switch_bugs; |
| 156 | unsigned long nr_events; |
| 157 | unsigned long nr_lost_chunks; |
| 158 | unsigned long nr_lost_events; |
| 159 | u64 run_measurement_overhead; |
| 160 | u64 sleep_measurement_overhead; |
| 161 | u64 start_time; |
| 162 | u64 cpu_usage; |
| 163 | u64 runavg_cpu_usage; |
| 164 | u64 parent_cpu_usage; |
| 165 | u64 runavg_parent_cpu_usage; |
| 166 | u64 sum_runtime; |
| 167 | u64 sum_fluct; |
| 168 | u64 run_avg; |
| 169 | u64 all_runtime; |
| 170 | u64 all_count; |
| 171 | u64 cpu_last_switched[MAX_CPUS]; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 172 | 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] | 173 | struct list_head sort_list, cmp_pid; |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 174 | bool force; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 175 | bool skip_merge; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 176 | }; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 177 | |
| 178 | static u64 get_nsecs(void) |
| 179 | { |
| 180 | struct timespec ts; |
| 181 | |
| 182 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 183 | |
| 184 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 185 | } |
| 186 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 187 | static void burn_nsecs(struct perf_sched *sched, u64 nsecs) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 188 | { |
| 189 | u64 T0 = get_nsecs(), T1; |
| 190 | |
| 191 | do { |
| 192 | T1 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 193 | } while (T1 + sched->run_measurement_overhead < T0 + nsecs); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void sleep_nsecs(u64 nsecs) |
| 197 | { |
| 198 | struct timespec ts; |
| 199 | |
| 200 | ts.tv_nsec = nsecs % 999999999; |
| 201 | ts.tv_sec = nsecs / 999999999; |
| 202 | |
| 203 | nanosleep(&ts, NULL); |
| 204 | } |
| 205 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 206 | static void calibrate_run_measurement_overhead(struct perf_sched *sched) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 207 | { |
| 208 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
| 209 | int i; |
| 210 | |
| 211 | for (i = 0; i < 10; i++) { |
| 212 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 213 | burn_nsecs(sched, 0); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 214 | T1 = get_nsecs(); |
| 215 | delta = T1-T0; |
| 216 | min_delta = min(min_delta, delta); |
| 217 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 218 | sched->run_measurement_overhead = min_delta; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 219 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 220 | printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 223 | static void calibrate_sleep_measurement_overhead(struct perf_sched *sched) |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 224 | { |
| 225 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
| 226 | int i; |
| 227 | |
| 228 | for (i = 0; i < 10; i++) { |
| 229 | T0 = get_nsecs(); |
| 230 | sleep_nsecs(10000); |
| 231 | T1 = get_nsecs(); |
| 232 | delta = T1-T0; |
| 233 | min_delta = min(min_delta, delta); |
| 234 | } |
| 235 | min_delta -= 10000; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 236 | sched->sleep_measurement_overhead = min_delta; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 237 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 238 | printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 239 | } |
| 240 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 241 | static struct sched_atom * |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 242 | get_new_event(struct task_desc *task, u64 timestamp) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 243 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 244 | struct sched_atom *event = zalloc(sizeof(*event)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 245 | unsigned long idx = task->nr_events; |
| 246 | size_t size; |
| 247 | |
| 248 | event->timestamp = timestamp; |
| 249 | event->nr = idx; |
| 250 | |
| 251 | task->nr_events++; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 252 | size = sizeof(struct sched_atom *) * task->nr_events; |
| 253 | task->atoms = realloc(task->atoms, size); |
| 254 | BUG_ON(!task->atoms); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 255 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 256 | task->atoms[idx] = event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 257 | |
| 258 | return event; |
| 259 | } |
| 260 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 261 | static struct sched_atom *last_event(struct task_desc *task) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 262 | { |
| 263 | if (!task->nr_events) |
| 264 | return NULL; |
| 265 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 266 | return task->atoms[task->nr_events - 1]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 269 | static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task, |
| 270 | u64 timestamp, u64 duration) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 271 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 272 | struct sched_atom *event, *curr_event = last_event(task); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 273 | |
| 274 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 275 | * optimize an existing RUN event by merging this one |
| 276 | * to it: |
| 277 | */ |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 278 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 279 | sched->nr_run_events_optimized++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 280 | curr_event->duration += duration; |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | event = get_new_event(task, timestamp); |
| 285 | |
| 286 | event->type = SCHED_EVENT_RUN; |
| 287 | event->duration = duration; |
| 288 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 289 | sched->nr_run_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 292 | static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task, |
| 293 | u64 timestamp, struct task_desc *wakee) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 294 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 295 | struct sched_atom *event, *wakee_event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 296 | |
| 297 | event = get_new_event(task, timestamp); |
| 298 | event->type = SCHED_EVENT_WAKEUP; |
| 299 | event->wakee = wakee; |
| 300 | |
| 301 | wakee_event = last_event(wakee); |
| 302 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 303 | sched->targetless_wakeups++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | if (wakee_event->wait_sem) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 307 | sched->multitarget_wakeups++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 311 | wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 312 | sem_init(wakee_event->wait_sem, 0, 0); |
| 313 | wakee_event->specific_wait = 1; |
| 314 | event->wait_sem = wakee_event->wait_sem; |
| 315 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 316 | sched->nr_wakeup_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 319 | static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task, |
| 320 | u64 timestamp, u64 task_state __maybe_unused) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 321 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 322 | struct sched_atom *event = get_new_event(task, timestamp); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 323 | |
| 324 | event->type = SCHED_EVENT_SLEEP; |
| 325 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 326 | sched->nr_sleep_events++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 329 | static struct task_desc *register_pid(struct perf_sched *sched, |
| 330 | unsigned long pid, const char *comm) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 331 | { |
| 332 | struct task_desc *task; |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 333 | static int pid_max; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 334 | |
Yunlong Song | cb06ac2 | 2015-03-31 21:46:30 +0800 | [diff] [blame] | 335 | if (sched->pid_to_task == NULL) { |
| 336 | if (sysctl__read_int("kernel/pid_max", &pid_max) < 0) |
| 337 | pid_max = MAX_PID; |
| 338 | BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); |
| 339 | } |
Yunlong Song | 3a423a5 | 2015-03-31 21:46:31 +0800 | [diff] [blame] | 340 | if (pid >= (unsigned long)pid_max) { |
| 341 | BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * |
| 342 | sizeof(struct task_desc *))) == NULL); |
| 343 | while (pid >= (unsigned long)pid_max) |
| 344 | sched->pid_to_task[pid_max++] = NULL; |
| 345 | } |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 346 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 347 | task = sched->pid_to_task[pid]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 348 | |
| 349 | if (task) |
| 350 | return task; |
| 351 | |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 352 | task = zalloc(sizeof(*task)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 353 | task->pid = pid; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 354 | task->nr = sched->nr_tasks; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 355 | strcpy(task->comm, comm); |
| 356 | /* |
| 357 | * every task starts in sleeping state - this gets ignored |
| 358 | * if there's no wakeup pointing to this sleep state: |
| 359 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 360 | add_sched_event_sleep(sched, task, 0, 0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 361 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 362 | sched->pid_to_task[pid] = task; |
| 363 | sched->nr_tasks++; |
Yunlong Song | 0755bc4 | 2015-03-31 21:46:28 +0800 | [diff] [blame] | 364 | 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] | 365 | BUG_ON(!sched->tasks); |
| 366 | sched->tasks[task->nr] = task; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 367 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 368 | if (verbose) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 369 | 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] | 370 | |
| 371 | return task; |
| 372 | } |
| 373 | |
| 374 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 375 | static void print_task_traces(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 376 | { |
| 377 | struct task_desc *task; |
| 378 | unsigned long i; |
| 379 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 380 | for (i = 0; i < sched->nr_tasks; i++) { |
| 381 | task = sched->tasks[i]; |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 382 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 383 | task->nr, task->comm, task->pid, task->nr_events); |
| 384 | } |
| 385 | } |
| 386 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 387 | static void add_cross_task_wakeups(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 388 | { |
| 389 | struct task_desc *task1, *task2; |
| 390 | unsigned long i, j; |
| 391 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 392 | for (i = 0; i < sched->nr_tasks; i++) { |
| 393 | task1 = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 394 | j = i + 1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 395 | if (j == sched->nr_tasks) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 396 | j = 0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 397 | task2 = sched->tasks[j]; |
| 398 | add_sched_event_wakeup(sched, task1, 0, task2); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 402 | static void perf_sched__process_event(struct perf_sched *sched, |
| 403 | struct sched_atom *atom) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 404 | { |
| 405 | int ret = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 406 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 407 | switch (atom->type) { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 408 | case SCHED_EVENT_RUN: |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 409 | burn_nsecs(sched, atom->duration); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 410 | break; |
| 411 | case SCHED_EVENT_SLEEP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 412 | if (atom->wait_sem) |
| 413 | ret = sem_wait(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 414 | BUG_ON(ret); |
| 415 | break; |
| 416 | case SCHED_EVENT_WAKEUP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 417 | if (atom->wait_sem) |
| 418 | ret = sem_post(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 419 | BUG_ON(ret); |
| 420 | break; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 421 | case SCHED_EVENT_MIGRATION: |
| 422 | break; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 423 | default: |
| 424 | BUG_ON(1); |
| 425 | } |
| 426 | } |
| 427 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 428 | static u64 get_cpu_usage_nsec_parent(void) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 429 | { |
| 430 | struct rusage ru; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 431 | u64 sum; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 432 | int err; |
| 433 | |
| 434 | err = getrusage(RUSAGE_SELF, &ru); |
| 435 | BUG_ON(err); |
| 436 | |
| 437 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; |
| 438 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; |
| 439 | |
| 440 | return sum; |
| 441 | } |
| 442 | |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 443 | 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] | 444 | { |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 445 | struct perf_event_attr attr; |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 446 | char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE]; |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 447 | int fd; |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 448 | struct rlimit limit; |
| 449 | bool need_privilege = false; |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 450 | |
| 451 | memset(&attr, 0, sizeof(attr)); |
| 452 | |
| 453 | attr.type = PERF_TYPE_SOFTWARE; |
| 454 | attr.config = PERF_COUNT_SW_TASK_CLOCK; |
| 455 | |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 456 | force_again: |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 457 | fd = sys_perf_event_open(&attr, 0, -1, -1, |
| 458 | perf_event_open_cloexec_flag()); |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 459 | |
Yunlong Song | 1aff59b | 2015-03-31 21:46:33 +0800 | [diff] [blame] | 460 | if (fd < 0) { |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 461 | if (errno == EMFILE) { |
| 462 | if (sched->force) { |
| 463 | BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); |
| 464 | limit.rlim_cur += sched->nr_tasks - cur_task; |
| 465 | if (limit.rlim_cur > limit.rlim_max) { |
| 466 | limit.rlim_max = limit.rlim_cur; |
| 467 | need_privilege = true; |
| 468 | } |
| 469 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 470 | if (need_privilege && errno == EPERM) |
| 471 | strcpy(info, "Need privilege\n"); |
| 472 | } else |
| 473 | goto force_again; |
| 474 | } else |
| 475 | strcpy(info, "Have a try with -f option\n"); |
| 476 | } |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 477 | pr_err("Error: sys_perf_event_open() syscall returned " |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 478 | "with %d (%s)\n%s", fd, |
| 479 | strerror_r(errno, sbuf, sizeof(sbuf)), info); |
Yunlong Song | 1aff59b | 2015-03-31 21:46:33 +0800 | [diff] [blame] | 480 | exit(EXIT_FAILURE); |
| 481 | } |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 482 | return fd; |
| 483 | } |
| 484 | |
| 485 | static u64 get_cpu_usage_nsec_self(int fd) |
| 486 | { |
| 487 | u64 runtime; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 488 | int ret; |
| 489 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 490 | ret = read(fd, &runtime, sizeof(runtime)); |
| 491 | BUG_ON(ret != sizeof(runtime)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 492 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 493 | return runtime; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 496 | struct sched_thread_parms { |
| 497 | struct task_desc *task; |
| 498 | struct perf_sched *sched; |
Yunlong Song | 08097ab | 2015-03-31 21:46:32 +0800 | [diff] [blame] | 499 | int fd; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 500 | }; |
| 501 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 502 | static void *thread_func(void *ctx) |
| 503 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 504 | struct sched_thread_parms *parms = ctx; |
| 505 | struct task_desc *this_task = parms->task; |
| 506 | struct perf_sched *sched = parms->sched; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 507 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 508 | unsigned long i, ret; |
| 509 | char comm2[22]; |
Yunlong Song | 08097ab | 2015-03-31 21:46:32 +0800 | [diff] [blame] | 510 | int fd = parms->fd; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 511 | |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 512 | zfree(&parms); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 513 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 514 | sprintf(comm2, ":%s", this_task->comm); |
| 515 | prctl(PR_SET_NAME, comm2); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 516 | if (fd < 0) |
| 517 | return NULL; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 518 | again: |
| 519 | ret = sem_post(&this_task->ready_for_work); |
| 520 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 521 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 522 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 523 | ret = pthread_mutex_unlock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 524 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 525 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 526 | cpu_usage_0 = get_cpu_usage_nsec_self(fd); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 527 | |
| 528 | for (i = 0; i < this_task->nr_events; i++) { |
| 529 | this_task->curr_event = i; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 530 | perf_sched__process_event(sched, this_task->atoms[i]); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Xiao Guangrong | c0c9e72 | 2009-12-09 17:51:30 +0800 | [diff] [blame] | 533 | cpu_usage_1 = get_cpu_usage_nsec_self(fd); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 534 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 535 | ret = sem_post(&this_task->work_done_sem); |
| 536 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 537 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 538 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 539 | BUG_ON(ret); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 540 | ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 541 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 542 | |
| 543 | goto again; |
| 544 | } |
| 545 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 546 | static void create_tasks(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 547 | { |
| 548 | struct task_desc *task; |
| 549 | pthread_attr_t attr; |
| 550 | unsigned long i; |
| 551 | int err; |
| 552 | |
| 553 | err = pthread_attr_init(&attr); |
| 554 | BUG_ON(err); |
Jiri Pirko | 12f7e03 | 2011-01-10 14:14:23 -0200 | [diff] [blame] | 555 | err = pthread_attr_setstacksize(&attr, |
| 556 | (size_t) max(16 * 1024, PTHREAD_STACK_MIN)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 557 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 558 | err = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 559 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 560 | err = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 561 | BUG_ON(err); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 562 | for (i = 0; i < sched->nr_tasks; i++) { |
| 563 | struct sched_thread_parms *parms = malloc(sizeof(*parms)); |
| 564 | BUG_ON(parms == NULL); |
| 565 | parms->task = task = sched->tasks[i]; |
| 566 | parms->sched = sched; |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 567 | parms->fd = self_open_counters(sched, i); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 568 | sem_init(&task->sleep_sem, 0, 0); |
| 569 | sem_init(&task->ready_for_work, 0, 0); |
| 570 | sem_init(&task->work_done_sem, 0, 0); |
| 571 | task->curr_event = 0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 572 | err = pthread_create(&task->thread, &attr, thread_func, parms); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 573 | BUG_ON(err); |
| 574 | } |
| 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 577 | static void wait_for_tasks(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 578 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 579 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 580 | struct task_desc *task; |
| 581 | unsigned long i, ret; |
| 582 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 583 | sched->start_time = get_nsecs(); |
| 584 | sched->cpu_usage = 0; |
| 585 | pthread_mutex_unlock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 586 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 587 | for (i = 0; i < sched->nr_tasks; i++) { |
| 588 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 589 | ret = sem_wait(&task->ready_for_work); |
| 590 | BUG_ON(ret); |
| 591 | sem_init(&task->ready_for_work, 0, 0); |
| 592 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 593 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 594 | BUG_ON(ret); |
| 595 | |
| 596 | cpu_usage_0 = get_cpu_usage_nsec_parent(); |
| 597 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 598 | pthread_mutex_unlock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 599 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 600 | for (i = 0; i < sched->nr_tasks; i++) { |
| 601 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 602 | ret = sem_wait(&task->work_done_sem); |
| 603 | BUG_ON(ret); |
| 604 | sem_init(&task->work_done_sem, 0, 0); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 605 | sched->cpu_usage += task->cpu_usage; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 606 | task->cpu_usage = 0; |
| 607 | } |
| 608 | |
| 609 | cpu_usage_1 = get_cpu_usage_nsec_parent(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 610 | if (!sched->runavg_cpu_usage) |
| 611 | sched->runavg_cpu_usage = sched->cpu_usage; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 612 | 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] | 613 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 614 | sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 615 | if (!sched->runavg_parent_cpu_usage) |
| 616 | sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 617 | sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + |
| 618 | sched->parent_cpu_usage)/sched->replay_repeat; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 619 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 620 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 621 | BUG_ON(ret); |
| 622 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 623 | for (i = 0; i < sched->nr_tasks; i++) { |
| 624 | task = sched->tasks[i]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 625 | sem_init(&task->sleep_sem, 0, 0); |
| 626 | task->curr_event = 0; |
| 627 | } |
| 628 | } |
| 629 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 630 | static void run_one_test(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 631 | { |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 632 | u64 T0, T1, delta, avg_delta, fluct; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 633 | |
| 634 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 635 | wait_for_tasks(sched); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 636 | T1 = get_nsecs(); |
| 637 | |
| 638 | delta = T1 - T0; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 639 | sched->sum_runtime += delta; |
| 640 | sched->nr_runs++; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 641 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 642 | avg_delta = sched->sum_runtime / sched->nr_runs; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 643 | if (delta < avg_delta) |
| 644 | fluct = avg_delta - delta; |
| 645 | else |
| 646 | fluct = delta - avg_delta; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 647 | sched->sum_fluct += fluct; |
| 648 | if (!sched->run_avg) |
| 649 | sched->run_avg = delta; |
Yunlong Song | ff5f3bb | 2015-03-31 21:46:36 +0800 | [diff] [blame] | 650 | 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] | 651 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 652 | printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0); |
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 | printf("ravg: %0.2f, ", (double)sched->run_avg / 1e6); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 655 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 656 | printf("cpu: %0.2f / %0.2f", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 657 | (double)sched->cpu_usage / 1e6, (double)sched->runavg_cpu_usage / 1e6); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 658 | |
| 659 | #if 0 |
| 660 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 661 | * rusage statistics done by the parent, these are less |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 662 | * accurate than the sched->sum_exec_runtime based statistics: |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 663 | */ |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 664 | printf(" [%0.2f / %0.2f]", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 665 | (double)sched->parent_cpu_usage/1e6, |
| 666 | (double)sched->runavg_parent_cpu_usage/1e6); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 667 | #endif |
| 668 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 669 | printf("\n"); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 670 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 671 | if (sched->nr_sleep_corrections) |
| 672 | printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); |
| 673 | sched->nr_sleep_corrections = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 676 | static void test_calibrations(struct perf_sched *sched) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 677 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 678 | u64 T0, T1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 679 | |
| 680 | T0 = get_nsecs(); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 681 | burn_nsecs(sched, 1e6); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 682 | T1 = get_nsecs(); |
| 683 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 684 | printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 685 | |
| 686 | T0 = get_nsecs(); |
| 687 | sleep_nsecs(1e6); |
| 688 | T1 = get_nsecs(); |
| 689 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 690 | printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 693 | static int |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 694 | replay_wakeup_event(struct perf_sched *sched, |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 695 | struct perf_evsel *evsel, struct perf_sample *sample, |
| 696 | struct machine *machine __maybe_unused) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 697 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 698 | const char *comm = perf_evsel__strval(evsel, sample, "comm"); |
| 699 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 700 | struct task_desc *waker, *wakee; |
| 701 | |
| 702 | if (verbose) { |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 703 | printf("sched_wakeup event %p\n", evsel); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 704 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 705 | printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 708 | waker = register_pid(sched, sample->tid, "<unknown>"); |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 709 | wakee = register_pid(sched, pid, comm); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 710 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 711 | add_sched_event_wakeup(sched, waker, sample->time, wakee); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 712 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 715 | static int replay_switch_event(struct perf_sched *sched, |
| 716 | struct perf_evsel *evsel, |
| 717 | struct perf_sample *sample, |
| 718 | struct machine *machine __maybe_unused) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 719 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 720 | const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"), |
| 721 | *next_comm = perf_evsel__strval(evsel, sample, "next_comm"); |
| 722 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 723 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 724 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 725 | struct task_desc *prev, __maybe_unused *next; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 726 | u64 timestamp0, timestamp = sample->time; |
| 727 | int cpu = sample->cpu; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 728 | s64 delta; |
| 729 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 730 | if (verbose) |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 731 | printf("sched_switch event %p\n", evsel); |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 732 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 733 | if (cpu >= MAX_CPUS || cpu < 0) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 734 | return 0; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 735 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 736 | timestamp0 = sched->cpu_last_switched[cpu]; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 737 | if (timestamp0) |
| 738 | delta = timestamp - timestamp0; |
| 739 | else |
| 740 | delta = 0; |
| 741 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 742 | if (delta < 0) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 743 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 744 | return -1; |
| 745 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 746 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 747 | pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n", |
| 748 | prev_comm, prev_pid, next_comm, next_pid, delta); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 749 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 750 | prev = register_pid(sched, prev_pid, prev_comm); |
| 751 | next = register_pid(sched, next_pid, next_comm); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 752 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 753 | sched->cpu_last_switched[cpu] = timestamp; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 754 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 755 | add_sched_event_run(sched, prev, timestamp, delta); |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 756 | add_sched_event_sleep(sched, prev, timestamp, prev_state); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 757 | |
| 758 | return 0; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 759 | } |
| 760 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 761 | static int replay_fork_event(struct perf_sched *sched, |
| 762 | union perf_event *event, |
| 763 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 764 | { |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 765 | struct thread *child, *parent; |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 766 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 767 | child = machine__findnew_thread(machine, event->fork.pid, |
| 768 | event->fork.tid); |
| 769 | parent = machine__findnew_thread(machine, event->fork.ppid, |
| 770 | event->fork.ptid); |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 771 | |
| 772 | if (child == NULL || parent == NULL) { |
| 773 | pr_debug("thread does not exist on fork event: child %p, parent %p\n", |
| 774 | child, parent); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 775 | goto out_put; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 776 | } |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 777 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 778 | if (verbose) { |
| 779 | printf("fork event\n"); |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 780 | printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); |
| 781 | printf("... child: %s/%d\n", thread__comm_str(child), child->tid); |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 784 | register_pid(sched, parent->tid, thread__comm_str(parent)); |
| 785 | register_pid(sched, child->tid, thread__comm_str(child)); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 786 | out_put: |
| 787 | thread__put(child); |
| 788 | thread__put(parent); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 789 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 792 | struct sort_dimension { |
| 793 | const char *name; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 794 | sort_fn_t cmp; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 795 | struct list_head list; |
| 796 | }; |
| 797 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 798 | static int |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 799 | 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] | 800 | { |
| 801 | struct sort_dimension *sort; |
| 802 | int ret = 0; |
| 803 | |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 804 | BUG_ON(list_empty(list)); |
| 805 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 806 | list_for_each_entry(sort, list, list) { |
| 807 | ret = sort->cmp(l, r); |
| 808 | if (ret) |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 815 | static struct work_atoms * |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 816 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
| 817 | struct list_head *sort_list) |
| 818 | { |
| 819 | struct rb_node *node = root->rb_node; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 820 | struct work_atoms key = { .thread = thread }; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 821 | |
| 822 | while (node) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 823 | struct work_atoms *atoms; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 824 | int cmp; |
| 825 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 826 | atoms = container_of(node, struct work_atoms, node); |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 827 | |
| 828 | cmp = thread_lat_cmp(sort_list, &key, atoms); |
| 829 | if (cmp > 0) |
| 830 | node = node->rb_left; |
| 831 | else if (cmp < 0) |
| 832 | node = node->rb_right; |
| 833 | else { |
| 834 | BUG_ON(thread != atoms->thread); |
| 835 | return atoms; |
| 836 | } |
| 837 | } |
| 838 | return NULL; |
| 839 | } |
| 840 | |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 841 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 842 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 843 | struct list_head *sort_list) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 844 | { |
| 845 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 846 | |
| 847 | while (*new) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 848 | struct work_atoms *this; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 849 | int cmp; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 850 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 851 | this = container_of(*new, struct work_atoms, node); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 852 | parent = *new; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 853 | |
| 854 | cmp = thread_lat_cmp(sort_list, data, this); |
| 855 | |
| 856 | if (cmp > 0) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 857 | new = &((*new)->rb_left); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 858 | else |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 859 | new = &((*new)->rb_right); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | rb_link_node(&data->node, parent, new); |
| 863 | rb_insert_color(&data->node, root); |
| 864 | } |
| 865 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 866 | static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 867 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 868 | struct work_atoms *atoms = zalloc(sizeof(*atoms)); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 869 | if (!atoms) { |
| 870 | pr_err("No memory at %s\n", __func__); |
| 871 | return -1; |
| 872 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 873 | |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 874 | atoms->thread = thread__get(thread); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 875 | INIT_LIST_HEAD(&atoms->work_list); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 876 | __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 877 | return 0; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 880 | static char sched_out_state(u64 prev_state) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 881 | { |
| 882 | const char *str = TASK_STATE_TO_CHAR_STR; |
| 883 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 884 | return str[prev_state]; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 885 | } |
| 886 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 887 | static int |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 888 | add_sched_out_event(struct work_atoms *atoms, |
| 889 | char run_state, |
| 890 | u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 891 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 892 | struct work_atom *atom = zalloc(sizeof(*atom)); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 893 | if (!atom) { |
| 894 | pr_err("Non memory at %s", __func__); |
| 895 | return -1; |
| 896 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 897 | |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 898 | atom->sched_out_time = timestamp; |
| 899 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 900 | if (run_state == 'R') { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 901 | atom->state = THREAD_WAIT_CPU; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 902 | atom->wake_up_time = atom->sched_out_time; |
Frederic Weisbecker | c6ced61 | 2009-09-13 00:46:19 +0200 | [diff] [blame] | 903 | } |
| 904 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 905 | list_add_tail(&atom->list, &atoms->work_list); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 906 | return 0; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 910 | add_runtime_event(struct work_atoms *atoms, u64 delta, |
| 911 | u64 timestamp __maybe_unused) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 912 | { |
| 913 | struct work_atom *atom; |
| 914 | |
| 915 | BUG_ON(list_empty(&atoms->work_list)); |
| 916 | |
| 917 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
| 918 | |
| 919 | atom->runtime += delta; |
| 920 | atoms->total_runtime += delta; |
| 921 | } |
| 922 | |
| 923 | static void |
| 924 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 925 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 926 | struct work_atom *atom; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 927 | u64 delta; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 928 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 929 | if (list_empty(&atoms->work_list)) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 930 | return; |
| 931 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 932 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 933 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 934 | if (atom->state != THREAD_WAIT_CPU) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 935 | return; |
| 936 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 937 | if (timestamp < atom->wake_up_time) { |
| 938 | atom->state = THREAD_IGNORE; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 939 | return; |
| 940 | } |
| 941 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 942 | atom->state = THREAD_SCHED_IN; |
| 943 | atom->sched_in_time = timestamp; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 944 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 945 | delta = atom->sched_in_time - atom->wake_up_time; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 946 | atoms->total_lat += delta; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 947 | if (delta > atoms->max_lat) { |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 948 | atoms->max_lat = delta; |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 949 | atoms->max_lat_at = timestamp; |
| 950 | } |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 951 | atoms->nb_atoms++; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 954 | static int latency_switch_event(struct perf_sched *sched, |
| 955 | struct perf_evsel *evsel, |
| 956 | struct perf_sample *sample, |
| 957 | struct machine *machine) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 958 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 959 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 960 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 961 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 962 | struct work_atoms *out_events, *in_events; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 963 | struct thread *sched_out, *sched_in; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 964 | u64 timestamp0, timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 965 | int cpu = sample->cpu, err = -1; |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 966 | s64 delta; |
| 967 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 968 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 969 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 970 | timestamp0 = sched->cpu_last_switched[cpu]; |
| 971 | sched->cpu_last_switched[cpu] = timestamp; |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 972 | if (timestamp0) |
| 973 | delta = timestamp - timestamp0; |
| 974 | else |
| 975 | delta = 0; |
| 976 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 977 | if (delta < 0) { |
| 978 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
| 979 | return -1; |
| 980 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 981 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 982 | sched_out = machine__findnew_thread(machine, -1, prev_pid); |
| 983 | sched_in = machine__findnew_thread(machine, -1, next_pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 984 | if (sched_out == NULL || sched_in == NULL) |
| 985 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 986 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 987 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 988 | if (!out_events) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 989 | if (thread_atoms_insert(sched, sched_out)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 990 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 991 | 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] | 992 | if (!out_events) { |
| 993 | pr_err("out-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 994 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 995 | } |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 996 | } |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 997 | 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] | 998 | return -1; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 999 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1000 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1001 | if (!in_events) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1002 | if (thread_atoms_insert(sched, sched_in)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1003 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1004 | 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] | 1005 | if (!in_events) { |
| 1006 | pr_err("in-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1007 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1008 | } |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * Take came in we have not heard about yet, |
| 1011 | * add in an initial atom in runnable state: |
| 1012 | */ |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1013 | if (add_sched_out_event(in_events, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1014 | goto out_put; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1015 | } |
| 1016 | add_sched_in_event(in_events, timestamp); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1017 | err = 0; |
| 1018 | out_put: |
| 1019 | thread__put(sched_out); |
| 1020 | thread__put(sched_in); |
| 1021 | return err; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1024 | static int latency_runtime_event(struct perf_sched *sched, |
| 1025 | struct perf_evsel *evsel, |
| 1026 | struct perf_sample *sample, |
| 1027 | struct machine *machine) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1028 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1029 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
| 1030 | const u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1031 | struct thread *thread = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1032 | 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] | 1033 | u64 timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1034 | int cpu = sample->cpu, err = -1; |
| 1035 | |
| 1036 | if (thread == NULL) |
| 1037 | return -1; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1038 | |
| 1039 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1040 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1041 | if (thread_atoms_insert(sched, thread)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1042 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1043 | 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] | 1044 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1045 | pr_err("in-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1046 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1047 | } |
| 1048 | if (add_sched_out_event(atoms, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1049 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1052 | add_runtime_event(atoms, runtime, timestamp); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1053 | err = 0; |
| 1054 | out_put: |
| 1055 | thread__put(thread); |
| 1056 | return err; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1059 | static int latency_wakeup_event(struct perf_sched *sched, |
| 1060 | struct perf_evsel *evsel, |
| 1061 | struct perf_sample *sample, |
| 1062 | struct machine *machine) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1063 | { |
Peter Zijlstra | 0680ee7 | 2014-05-12 20:19:46 +0200 | [diff] [blame] | 1064 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1065 | struct work_atoms *atoms; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1066 | struct work_atom *atom; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1067 | struct thread *wakee; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1068 | u64 timestamp = sample->time; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1069 | int err = -1; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1070 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1071 | wakee = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1072 | if (wakee == NULL) |
| 1073 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1074 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
Frederic Weisbecker | 1756220 | 2009-09-12 23:11:32 +0200 | [diff] [blame] | 1075 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1076 | if (thread_atoms_insert(sched, wakee)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1077 | goto out_put; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1078 | 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] | 1079 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1080 | pr_err("wakeup-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1081 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1082 | } |
| 1083 | if (add_sched_out_event(atoms, 'S', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1084 | goto out_put; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1087 | BUG_ON(list_empty(&atoms->work_list)); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1088 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1089 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1090 | |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1091 | /* |
Dongsheng Yang | 67d6259 | 2014-05-13 10:38:21 +0900 | [diff] [blame] | 1092 | * As we do not guarantee the wakeup event happens when |
| 1093 | * task is out of run queue, also may happen when task is |
| 1094 | * on run queue and wakeup only change ->state to TASK_RUNNING, |
| 1095 | * then we should not set the ->wake_up_time when wake up a |
| 1096 | * task which is on run queue. |
| 1097 | * |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1098 | * You WILL be missing events if you've recorded only |
| 1099 | * one CPU, or are only looking at only one, so don't |
Dongsheng Yang | 67d6259 | 2014-05-13 10:38:21 +0900 | [diff] [blame] | 1100 | * skip in this case. |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1101 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1102 | if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1103 | goto out_ok; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1104 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1105 | sched->nr_timestamps++; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1106 | if (atom->sched_out_time > timestamp) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1107 | sched->nr_unordered_timestamps++; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1108 | goto out_ok; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1109 | } |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1110 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1111 | atom->state = THREAD_WAIT_CPU; |
| 1112 | atom->wake_up_time = timestamp; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1113 | out_ok: |
| 1114 | err = 0; |
| 1115 | out_put: |
| 1116 | thread__put(wakee); |
| 1117 | return err; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1120 | static int latency_migrate_task_event(struct perf_sched *sched, |
| 1121 | struct perf_evsel *evsel, |
| 1122 | struct perf_sample *sample, |
| 1123 | struct machine *machine) |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1124 | { |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1125 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1126 | u64 timestamp = sample->time; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1127 | struct work_atoms *atoms; |
| 1128 | struct work_atom *atom; |
| 1129 | struct thread *migrant; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1130 | int err = -1; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* |
| 1133 | * Only need to worry about migration when profiling one CPU. |
| 1134 | */ |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1135 | if (sched->profile_cpu == -1) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1136 | return 0; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1137 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1138 | migrant = machine__findnew_thread(machine, -1, pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1139 | if (migrant == NULL) |
| 1140 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1141 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1142 | if (!atoms) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1143 | if (thread_atoms_insert(sched, migrant)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1144 | goto out_put; |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1145 | register_pid(sched, migrant->tid, thread__comm_str(migrant)); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1146 | 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] | 1147 | if (!atoms) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1148 | pr_err("migration-event: Internal tree error"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1149 | goto out_put; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1150 | } |
| 1151 | if (add_sched_out_event(atoms, 'R', timestamp)) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1152 | goto out_put; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | BUG_ON(list_empty(&atoms->work_list)); |
| 1156 | |
| 1157 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
| 1158 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; |
| 1159 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1160 | sched->nr_timestamps++; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1161 | |
| 1162 | if (atom->sched_out_time > timestamp) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1163 | sched->nr_unordered_timestamps++; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1164 | err = 0; |
| 1165 | out_put: |
| 1166 | thread__put(migrant); |
| 1167 | return err; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1170 | 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] | 1171 | { |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1172 | int i; |
| 1173 | int ret; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1174 | u64 avg; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1175 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1176 | if (!work_list->nb_atoms) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1177 | return; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1178 | /* |
| 1179 | * Ignore idle threads: |
| 1180 | */ |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1181 | if (!strcmp(thread__comm_str(work_list->thread), "swapper")) |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1182 | return; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1183 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1184 | sched->all_runtime += work_list->total_runtime; |
| 1185 | sched->all_count += work_list->nb_atoms; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1186 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1187 | if (work_list->num_merged > 1) |
| 1188 | ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged); |
| 1189 | else |
| 1190 | 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] | 1191 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1192 | for (i = 0; i < 24 - ret; i++) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1193 | printf(" "); |
| 1194 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1195 | avg = work_list->total_lat / work_list->nb_atoms; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1196 | |
Ramkumar Ramachandra | 80790e0 | 2014-03-17 10:18:21 -0400 | [diff] [blame] | 1197 | printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n", |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1198 | (double)work_list->total_runtime / 1e6, |
| 1199 | work_list->nb_atoms, (double)avg / 1e6, |
Frederic Weisbecker | 3786310a | 2009-12-09 21:40:08 +0100 | [diff] [blame] | 1200 | (double)work_list->max_lat / 1e6, |
| 1201 | (double)work_list->max_lat_at / 1e9); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1204 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1205 | { |
Jiri Olsa | 0014de1 | 2015-11-02 12:10:25 +0100 | [diff] [blame] | 1206 | if (l->thread == r->thread) |
| 1207 | return 0; |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 1208 | if (l->thread->tid < r->thread->tid) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1209 | return -1; |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 1210 | if (l->thread->tid > r->thread->tid) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1211 | return 1; |
Jiri Olsa | 0014de1 | 2015-11-02 12:10:25 +0100 | [diff] [blame] | 1212 | return (int)(l->thread - r->thread); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1215 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1216 | { |
| 1217 | u64 avgl, avgr; |
| 1218 | |
| 1219 | if (!l->nb_atoms) |
| 1220 | return -1; |
| 1221 | |
| 1222 | if (!r->nb_atoms) |
| 1223 | return 1; |
| 1224 | |
| 1225 | avgl = l->total_lat / l->nb_atoms; |
| 1226 | avgr = r->total_lat / r->nb_atoms; |
| 1227 | |
| 1228 | if (avgl < avgr) |
| 1229 | return -1; |
| 1230 | if (avgl > avgr) |
| 1231 | return 1; |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1236 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1237 | { |
| 1238 | if (l->max_lat < r->max_lat) |
| 1239 | return -1; |
| 1240 | if (l->max_lat > r->max_lat) |
| 1241 | return 1; |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1246 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1247 | { |
| 1248 | if (l->nb_atoms < r->nb_atoms) |
| 1249 | return -1; |
| 1250 | if (l->nb_atoms > r->nb_atoms) |
| 1251 | return 1; |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1256 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1257 | { |
| 1258 | if (l->total_runtime < r->total_runtime) |
| 1259 | return -1; |
| 1260 | if (l->total_runtime > r->total_runtime) |
| 1261 | return 1; |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Randy Dunlap | cbef79a | 2009-10-05 13:17:29 -0700 | [diff] [blame] | 1266 | static int sort_dimension__add(const char *tok, struct list_head *list) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1267 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1268 | size_t i; |
| 1269 | static struct sort_dimension avg_sort_dimension = { |
| 1270 | .name = "avg", |
| 1271 | .cmp = avg_cmp, |
| 1272 | }; |
| 1273 | static struct sort_dimension max_sort_dimension = { |
| 1274 | .name = "max", |
| 1275 | .cmp = max_cmp, |
| 1276 | }; |
| 1277 | static struct sort_dimension pid_sort_dimension = { |
| 1278 | .name = "pid", |
| 1279 | .cmp = pid_cmp, |
| 1280 | }; |
| 1281 | static struct sort_dimension runtime_sort_dimension = { |
| 1282 | .name = "runtime", |
| 1283 | .cmp = runtime_cmp, |
| 1284 | }; |
| 1285 | static struct sort_dimension switch_sort_dimension = { |
| 1286 | .name = "switch", |
| 1287 | .cmp = switch_cmp, |
| 1288 | }; |
| 1289 | struct sort_dimension *available_sorts[] = { |
| 1290 | &pid_sort_dimension, |
| 1291 | &avg_sort_dimension, |
| 1292 | &max_sort_dimension, |
| 1293 | &switch_sort_dimension, |
| 1294 | &runtime_sort_dimension, |
| 1295 | }; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1296 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1297 | for (i = 0; i < ARRAY_SIZE(available_sorts); i++) { |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1298 | if (!strcmp(available_sorts[i]->name, tok)) { |
| 1299 | list_add_tail(&available_sorts[i]->list, list); |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1308 | static void perf_sched__sort_lat(struct perf_sched *sched) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1309 | { |
| 1310 | struct rb_node *node; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1311 | struct rb_root *root = &sched->atom_root; |
| 1312 | again: |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1313 | for (;;) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1314 | struct work_atoms *data; |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1315 | node = rb_first(root); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1316 | if (!node) |
| 1317 | break; |
| 1318 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1319 | rb_erase(node, root); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1320 | data = rb_entry(node, struct work_atoms, node); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1321 | __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1322 | } |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1323 | if (root == &sched->atom_root) { |
| 1324 | root = &sched->merged_atom_root; |
| 1325 | goto again; |
| 1326 | } |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1329 | static int process_sched_wakeup_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1330 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1331 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1332 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1333 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1334 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1335 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1336 | if (sched->tp_handler->wakeup_event) |
| 1337 | return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1338 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1339 | return 0; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1342 | static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel, |
| 1343 | struct perf_sample *sample, struct machine *machine) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1344 | { |
Dongsheng Yang | 9d372ca | 2014-05-16 14:37:05 +0900 | [diff] [blame] | 1345 | const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
| 1346 | struct thread *sched_in; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1347 | int new_shortname; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1348 | u64 timestamp0, timestamp = sample->time; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1349 | s64 delta; |
Arnaldo Carvalho de Melo | 7f7f8d0 | 2012-08-07 11:33:42 -0300 | [diff] [blame] | 1350 | int cpu, this_cpu = sample->cpu; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1351 | |
| 1352 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); |
| 1353 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1354 | if (this_cpu > sched->max_cpu) |
| 1355 | sched->max_cpu = this_cpu; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1356 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1357 | timestamp0 = sched->cpu_last_switched[this_cpu]; |
| 1358 | sched->cpu_last_switched[this_cpu] = timestamp; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1359 | if (timestamp0) |
| 1360 | delta = timestamp - timestamp0; |
| 1361 | else |
| 1362 | delta = 0; |
| 1363 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1364 | if (delta < 0) { |
Namhyung Kim | 60b7d14 | 2012-09-12 11:11:06 +0900 | [diff] [blame] | 1365 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1366 | return -1; |
| 1367 | } |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1368 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 1369 | sched_in = machine__findnew_thread(machine, -1, next_pid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1370 | if (sched_in == NULL) |
| 1371 | return -1; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1372 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1373 | sched->curr_thread[this_cpu] = thread__get(sched_in); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1374 | |
| 1375 | printf(" "); |
| 1376 | |
| 1377 | new_shortname = 0; |
| 1378 | if (!sched_in->shortname[0]) { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1379 | if (!strcmp(thread__comm_str(sched_in), "swapper")) { |
| 1380 | /* |
| 1381 | * Don't allocate a letter-number for swapper:0 |
| 1382 | * as a shortname. Instead, we use '.' for it. |
| 1383 | */ |
| 1384 | sched_in->shortname[0] = '.'; |
| 1385 | sched_in->shortname[1] = ' '; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1386 | } else { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1387 | sched_in->shortname[0] = sched->next_shortname1; |
| 1388 | sched_in->shortname[1] = sched->next_shortname2; |
| 1389 | |
| 1390 | if (sched->next_shortname1 < 'Z') { |
| 1391 | sched->next_shortname1++; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1392 | } else { |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1393 | sched->next_shortname1 = 'A'; |
| 1394 | if (sched->next_shortname2 < '9') |
| 1395 | sched->next_shortname2++; |
| 1396 | else |
| 1397 | sched->next_shortname2 = '0'; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | new_shortname = 1; |
| 1401 | } |
| 1402 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1403 | for (cpu = 0; cpu <= sched->max_cpu; cpu++) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1404 | if (cpu != this_cpu) |
| 1405 | printf(" "); |
| 1406 | else |
| 1407 | printf("*"); |
| 1408 | |
Dongsheng | 6bcab4e | 2014-05-06 14:39:01 +0900 | [diff] [blame] | 1409 | if (sched->curr_thread[cpu]) |
| 1410 | printf("%2s ", sched->curr_thread[cpu]->shortname); |
| 1411 | else |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1412 | printf(" "); |
| 1413 | } |
| 1414 | |
| 1415 | printf(" %12.6f secs ", (double)timestamp/1e9); |
| 1416 | if (new_shortname) { |
| 1417 | printf("%s => %s:%d\n", |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1418 | sched_in->shortname, thread__comm_str(sched_in), sched_in->tid); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1419 | } else { |
| 1420 | printf("\n"); |
| 1421 | } |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1422 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1423 | thread__put(sched_in); |
| 1424 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1425 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1428 | static int process_sched_switch_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1429 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1430 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1431 | struct machine *machine) |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1432 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1433 | 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] | 1434 | int this_cpu = sample->cpu, err = 0; |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1435 | u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
| 1436 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1437 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1438 | if (sched->curr_pid[this_cpu] != (u32)-1) { |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1439 | /* |
| 1440 | * Are we trying to switch away a PID that is |
| 1441 | * not current? |
| 1442 | */ |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1443 | if (sched->curr_pid[this_cpu] != prev_pid) |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1444 | sched->nr_context_switch_bugs++; |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1445 | } |
Ingo Molnar | c8a3775 | 2009-09-16 14:07:00 +0200 | [diff] [blame] | 1446 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1447 | if (sched->tp_handler->switch_event) |
| 1448 | err = sched->tp_handler->switch_event(sched, evsel, sample, machine); |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1449 | |
| 1450 | sched->curr_pid[this_cpu] = next_pid; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1451 | return err; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1454 | static int process_sched_runtime_event(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1455 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1456 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1457 | struct machine *machine) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1458 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1459 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame] | 1460 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1461 | if (sched->tp_handler->runtime_event) |
| 1462 | return sched->tp_handler->runtime_event(sched, evsel, sample, machine); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1463 | |
| 1464 | return 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1467 | static int perf_sched__process_fork_event(struct perf_tool *tool, |
| 1468 | union perf_event *event, |
| 1469 | struct perf_sample *sample, |
| 1470 | struct machine *machine) |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1471 | { |
| 1472 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
| 1473 | |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1474 | /* run the fork event through the perf machineruy */ |
| 1475 | perf_event__process_fork(tool, event, sample, machine); |
| 1476 | |
| 1477 | /* and then run additional processing needed for this command */ |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1478 | if (sched->tp_handler->fork_event) |
David Ahern | cb62750 | 2013-08-07 22:50:47 -0400 | [diff] [blame] | 1479 | return sched->tp_handler->fork_event(sched, event, machine); |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1480 | |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1484 | 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] | 1485 | struct perf_evsel *evsel, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1486 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1487 | struct machine *machine) |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1488 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1489 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1490 | |
Arnaldo Carvalho de Melo | 9ec3f4e | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1491 | if (sched->tp_handler->migrate_task_event) |
| 1492 | return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1493 | |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1494 | return 0; |
Mike Galbraith | 55ffb7a | 2009-10-10 14:46:04 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1497 | typedef int (*tracepoint_handler)(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1498 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1499 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 4218e67 | 2012-09-11 13:18:47 -0300 | [diff] [blame] | 1500 | struct machine *machine); |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1501 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1502 | static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused, |
| 1503 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1504 | struct perf_sample *sample, |
| 1505 | struct perf_evsel *evsel, |
| 1506 | struct machine *machine) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1507 | { |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1508 | int err = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1509 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 1510 | if (evsel->handler != NULL) { |
| 1511 | tracepoint_handler f = evsel->handler; |
Arnaldo Carvalho de Melo | 2b7fcbc | 2012-09-11 19:29:17 -0300 | [diff] [blame] | 1512 | err = f(tool, evsel, sample, machine); |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1513 | } |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1514 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1515 | return err; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1518 | static int perf_sched__read_events(struct perf_sched *sched) |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1519 | { |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1520 | const struct perf_evsel_str_handler handlers[] = { |
| 1521 | { "sched:sched_switch", process_sched_switch_event, }, |
| 1522 | { "sched:sched_stat_runtime", process_sched_runtime_event, }, |
| 1523 | { "sched:sched_wakeup", process_sched_wakeup_event, }, |
| 1524 | { "sched:sched_wakeup_new", process_sched_wakeup_event, }, |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1525 | { "sched:sched_migrate_task", process_sched_migrate_task_event, }, |
| 1526 | }; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1527 | struct perf_session *session; |
Jiri Olsa | f5fc141 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1528 | struct perf_data_file file = { |
| 1529 | .path = input_name, |
| 1530 | .mode = PERF_DATA_MODE_READ, |
Yunlong Song | f0dd330 | 2015-03-31 21:46:35 +0800 | [diff] [blame] | 1531 | .force = sched->force, |
Jiri Olsa | f5fc141 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1532 | }; |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1533 | int rc = -1; |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1534 | |
Jiri Olsa | f5fc141 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 1535 | session = perf_session__new(&file, false, &sched->tool); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1536 | if (session == NULL) { |
| 1537 | pr_debug("No Memory for session\n"); |
| 1538 | return -1; |
| 1539 | } |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1540 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1541 | symbol__init(&session->header.env); |
Namhyung Kim | 0493410 | 2014-08-12 15:40:41 +0900 | [diff] [blame] | 1542 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1543 | if (perf_session__set_tracepoints_handlers(session, handlers)) |
| 1544 | goto out_delete; |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 1545 | |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 1546 | if (perf_session__has_traces(session, "record -R")) { |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 1547 | int err = perf_session__process_events(session); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1548 | if (err) { |
| 1549 | pr_err("Failed to process events, error %d", err); |
| 1550 | goto out_delete; |
| 1551 | } |
Jiri Olsa | 4c09baf | 2011-08-08 23:03:34 +0200 | [diff] [blame] | 1552 | |
Arnaldo Carvalho de Melo | 75be989 | 2015-02-14 14:50:11 -0300 | [diff] [blame] | 1553 | sched->nr_events = session->evlist->stats.nr_events[0]; |
| 1554 | sched->nr_lost_events = session->evlist->stats.total_lost; |
| 1555 | 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] | 1556 | } |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1557 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1558 | rc = 0; |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1559 | out_delete: |
| 1560 | perf_session__delete(session); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1561 | return rc; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1564 | static void print_bad_events(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1565 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1566 | if (sched->nr_unordered_timestamps && sched->nr_timestamps) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1567 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1568 | (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, |
| 1569 | sched->nr_unordered_timestamps, sched->nr_timestamps); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1570 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1571 | if (sched->nr_lost_events && sched->nr_events) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1572 | 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] | 1573 | (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, |
| 1574 | sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1575 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1576 | if (sched->nr_context_switch_bugs && sched->nr_timestamps) { |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1577 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1578 | (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, |
| 1579 | sched->nr_context_switch_bugs, sched->nr_timestamps); |
| 1580 | if (sched->nr_lost_events) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1581 | printf(" (due to lost events?)"); |
| 1582 | printf("\n"); |
| 1583 | } |
| 1584 | } |
| 1585 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1586 | static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data) |
| 1587 | { |
| 1588 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 1589 | struct work_atoms *this; |
| 1590 | const char *comm = thread__comm_str(data->thread), *this_comm; |
| 1591 | |
| 1592 | while (*new) { |
| 1593 | int cmp; |
| 1594 | |
| 1595 | this = container_of(*new, struct work_atoms, node); |
| 1596 | parent = *new; |
| 1597 | |
| 1598 | this_comm = thread__comm_str(this->thread); |
| 1599 | cmp = strcmp(comm, this_comm); |
| 1600 | if (cmp > 0) { |
| 1601 | new = &((*new)->rb_left); |
| 1602 | } else if (cmp < 0) { |
| 1603 | new = &((*new)->rb_right); |
| 1604 | } else { |
| 1605 | this->num_merged++; |
| 1606 | this->total_runtime += data->total_runtime; |
| 1607 | this->nb_atoms += data->nb_atoms; |
| 1608 | this->total_lat += data->total_lat; |
| 1609 | list_splice(&data->work_list, &this->work_list); |
| 1610 | if (this->max_lat < data->max_lat) { |
| 1611 | this->max_lat = data->max_lat; |
| 1612 | this->max_lat_at = data->max_lat_at; |
| 1613 | } |
| 1614 | zfree(&data); |
| 1615 | return; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | data->num_merged++; |
| 1620 | rb_link_node(&data->node, parent, new); |
| 1621 | rb_insert_color(&data->node, root); |
| 1622 | } |
| 1623 | |
| 1624 | static void perf_sched__merge_lat(struct perf_sched *sched) |
| 1625 | { |
| 1626 | struct work_atoms *data; |
| 1627 | struct rb_node *node; |
| 1628 | |
| 1629 | if (sched->skip_merge) |
| 1630 | return; |
| 1631 | |
| 1632 | while ((node = rb_first(&sched->atom_root))) { |
| 1633 | rb_erase(node, &sched->atom_root); |
| 1634 | data = rb_entry(node, struct work_atoms, node); |
| 1635 | __merge_work_atoms(&sched->merged_atom_root, data); |
| 1636 | } |
| 1637 | } |
| 1638 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1639 | static int perf_sched__lat(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1640 | { |
| 1641 | struct rb_node *next; |
| 1642 | |
| 1643 | setup_pager(); |
David Ahern | ad9def7 | 2013-08-07 22:50:44 -0400 | [diff] [blame] | 1644 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1645 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1646 | return -1; |
David Ahern | ad9def7 | 2013-08-07 22:50:44 -0400 | [diff] [blame] | 1647 | |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1648 | perf_sched__merge_lat(sched); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1649 | perf_sched__sort_lat(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1650 | |
Ramkumar Ramachandra | 80790e0 | 2014-03-17 10:18:21 -0400 | [diff] [blame] | 1651 | printf("\n -----------------------------------------------------------------------------------------------------------------\n"); |
| 1652 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n"); |
| 1653 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1654 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1655 | next = rb_first(&sched->sorted_atom_root); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1656 | |
| 1657 | while (next) { |
| 1658 | struct work_atoms *work_list; |
| 1659 | |
| 1660 | work_list = rb_entry(next, struct work_atoms, node); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1661 | output_lat_thread(sched, work_list); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1662 | next = rb_next(next); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1663 | thread__zput(work_list->thread); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1664 | } |
| 1665 | |
Ramkumar Ramachandra | 80790e0 | 2014-03-17 10:18:21 -0400 | [diff] [blame] | 1666 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1667 | printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1668 | (double)sched->all_runtime / 1e6, sched->all_count); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1669 | |
| 1670 | printf(" ---------------------------------------------------\n"); |
| 1671 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1672 | print_bad_events(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1673 | printf("\n"); |
| 1674 | |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1675 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1678 | static int perf_sched__map(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1679 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1680 | sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); |
Ingo Molnar | 40749d0 | 2009-09-17 18:24:55 +0200 | [diff] [blame] | 1681 | |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1682 | setup_pager(); |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1683 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1684 | return -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1685 | print_bad_events(sched); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1686 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1689 | static int perf_sched__replay(struct perf_sched *sched) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1690 | { |
| 1691 | unsigned long i; |
| 1692 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1693 | calibrate_run_measurement_overhead(sched); |
| 1694 | calibrate_sleep_measurement_overhead(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1695 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1696 | test_calibrations(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1697 | |
Arnaldo Carvalho de Melo | ae536ac | 2015-03-02 22:28:41 -0300 | [diff] [blame] | 1698 | if (perf_sched__read_events(sched)) |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1699 | return -1; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1700 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1701 | printf("nr_run_events: %ld\n", sched->nr_run_events); |
| 1702 | printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); |
| 1703 | printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1704 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1705 | if (sched->targetless_wakeups) |
| 1706 | printf("target-less wakeups: %ld\n", sched->targetless_wakeups); |
| 1707 | if (sched->multitarget_wakeups) |
| 1708 | printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); |
| 1709 | if (sched->nr_run_events_optimized) |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1710 | printf("run atoms optimized: %ld\n", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1711 | sched->nr_run_events_optimized); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1712 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1713 | print_task_traces(sched); |
| 1714 | add_cross_task_wakeups(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1715 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1716 | create_tasks(sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1717 | printf("------------------------------------------------------------\n"); |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1718 | for (i = 0; i < sched->replay_repeat; i++) |
| 1719 | run_one_test(sched); |
Arnaldo Carvalho de Melo | a116e05 | 2012-09-08 22:53:06 -0300 | [diff] [blame] | 1720 | |
| 1721 | return 0; |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1724 | static void setup_sorting(struct perf_sched *sched, const struct option *options, |
| 1725 | const char * const usage_msg[]) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1726 | { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1727 | char *tmp, *tok, *str = strdup(sched->sort_order); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1728 | |
| 1729 | for (tok = strtok_r(str, ", ", &tmp); |
| 1730 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1731 | if (sort_dimension__add(tok, &sched->sort_list) < 0) { |
Namhyung Kim | c711836 | 2015-10-25 00:49:27 +0900 | [diff] [blame] | 1732 | usage_with_options_msg(usage_msg, options, |
| 1733 | "Unknown --sort key: `%s'", tok); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | free(str); |
| 1738 | |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1739 | sort_dimension__add("pid", &sched->cmp_pid); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1742 | static int __cmd_record(int argc, const char **argv) |
| 1743 | { |
| 1744 | unsigned int rec_argc, i, j; |
| 1745 | const char **rec_argv; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1746 | const char * const record_args[] = { |
| 1747 | "record", |
| 1748 | "-a", |
| 1749 | "-R", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1750 | "-m", "1024", |
| 1751 | "-c", "1", |
| 1752 | "-e", "sched:sched_switch", |
| 1753 | "-e", "sched:sched_stat_wait", |
| 1754 | "-e", "sched:sched_stat_sleep", |
| 1755 | "-e", "sched:sched_stat_iowait", |
| 1756 | "-e", "sched:sched_stat_runtime", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1757 | "-e", "sched:sched_process_fork", |
| 1758 | "-e", "sched:sched_wakeup", |
Dongsheng | 7fff959 | 2014-05-05 16:05:53 +0900 | [diff] [blame] | 1759 | "-e", "sched:sched_wakeup_new", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1760 | "-e", "sched:sched_migrate_task", |
| 1761 | }; |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1762 | |
| 1763 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
| 1764 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1765 | |
Arnaldo Carvalho de Melo | e462dc5 | 2011-01-10 10:48:47 -0200 | [diff] [blame] | 1766 | if (rec_argv == NULL) |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 1767 | return -ENOMEM; |
| 1768 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1769 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 1770 | rec_argv[i] = strdup(record_args[i]); |
| 1771 | |
| 1772 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 1773 | rec_argv[i] = argv[j]; |
| 1774 | |
| 1775 | BUG_ON(i != rec_argc); |
| 1776 | |
| 1777 | return cmd_record(i, rec_argv, NULL); |
| 1778 | } |
| 1779 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1780 | 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] | 1781 | { |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 1782 | const char default_sort_order[] = "avg, max, switch, runtime"; |
| 1783 | struct perf_sched sched = { |
| 1784 | .tool = { |
| 1785 | .sample = perf_sched__process_tracepoint_sample, |
| 1786 | .comm = perf_event__process_comm, |
| 1787 | .lost = perf_event__process_lost, |
| 1788 | .fork = perf_sched__process_fork_event, |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 1789 | .ordered_events = true, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 1790 | }, |
| 1791 | .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid), |
| 1792 | .sort_list = LIST_HEAD_INIT(sched.sort_list), |
| 1793 | .start_work_mutex = PTHREAD_MUTEX_INITIALIZER, |
| 1794 | .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 1795 | .sort_order = default_sort_order, |
| 1796 | .replay_repeat = 10, |
| 1797 | .profile_cpu = -1, |
| 1798 | .next_shortname1 = 'A', |
| 1799 | .next_shortname2 = '0', |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1800 | .skip_merge = 0, |
Adrian Hunter | 8a39df8 | 2013-10-22 10:34:15 +0300 | [diff] [blame] | 1801 | }; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1802 | const struct option latency_options[] = { |
| 1803 | OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]", |
| 1804 | "sort by key(s): runtime, switch, avg, max"), |
| 1805 | OPT_INCR('v', "verbose", &verbose, |
| 1806 | "be more verbose (show symbol address, etc)"), |
| 1807 | OPT_INTEGER('C', "CPU", &sched.profile_cpu, |
| 1808 | "CPU to profile on"), |
| 1809 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1810 | "dump raw trace in ASCII"), |
Josef Bacik | 2f80dd4 | 2015-05-22 09:18:40 -0400 | [diff] [blame] | 1811 | OPT_BOOLEAN('p', "pids", &sched.skip_merge, |
| 1812 | "latency stats per pid instead of per comm"), |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1813 | OPT_END() |
| 1814 | }; |
| 1815 | const struct option replay_options[] = { |
| 1816 | OPT_UINTEGER('r', "repeat", &sched.replay_repeat, |
| 1817 | "repeat the workload replay N times (-1: infinite)"), |
| 1818 | OPT_INCR('v', "verbose", &verbose, |
| 1819 | "be more verbose (show symbol address, etc)"), |
| 1820 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1821 | "dump raw trace in ASCII"), |
Yunlong Song | 939cda5 | 2015-03-31 21:46:34 +0800 | [diff] [blame] | 1822 | OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"), |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1823 | OPT_END() |
| 1824 | }; |
| 1825 | const struct option sched_options[] = { |
Feng Tang | 70cb4e9 | 2012-10-30 11:56:02 +0800 | [diff] [blame] | 1826 | OPT_STRING('i', "input", &input_name, "file", |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1827 | "input file name"), |
| 1828 | OPT_INCR('v', "verbose", &verbose, |
| 1829 | "be more verbose (show symbol address, etc)"), |
| 1830 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1831 | "dump raw trace in ASCII"), |
| 1832 | OPT_END() |
| 1833 | }; |
| 1834 | const char * const latency_usage[] = { |
| 1835 | "perf sched latency [<options>]", |
| 1836 | NULL |
| 1837 | }; |
| 1838 | const char * const replay_usage[] = { |
| 1839 | "perf sched replay [<options>]", |
| 1840 | NULL |
| 1841 | }; |
Ramkumar Ramachandra | a83edb2 | 2014-03-14 23:17:54 -0400 | [diff] [blame] | 1842 | const char *const sched_subcommands[] = { "record", "latency", "map", |
| 1843 | "replay", "script", NULL }; |
| 1844 | const char *sched_usage[] = { |
| 1845 | NULL, |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1846 | NULL |
| 1847 | }; |
| 1848 | struct trace_sched_handler lat_ops = { |
| 1849 | .wakeup_event = latency_wakeup_event, |
| 1850 | .switch_event = latency_switch_event, |
| 1851 | .runtime_event = latency_runtime_event, |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1852 | .migrate_task_event = latency_migrate_task_event, |
| 1853 | }; |
| 1854 | struct trace_sched_handler map_ops = { |
| 1855 | .switch_event = map_switch_event, |
| 1856 | }; |
| 1857 | struct trace_sched_handler replay_ops = { |
| 1858 | .wakeup_event = replay_wakeup_event, |
| 1859 | .switch_event = replay_switch_event, |
| 1860 | .fork_event = replay_fork_event, |
| 1861 | }; |
Adrian Hunter | 156a2b0 | 2013-10-22 10:34:16 +0300 | [diff] [blame] | 1862 | unsigned int i; |
| 1863 | |
| 1864 | for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++) |
| 1865 | sched.curr_pid[i] = -1; |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1866 | |
Ramkumar Ramachandra | a83edb2 | 2014-03-14 23:17:54 -0400 | [diff] [blame] | 1867 | argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands, |
| 1868 | sched_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1869 | if (!argc) |
| 1870 | usage_with_options(sched_usage, sched_options); |
| 1871 | |
Xiao Guangrong | c0777c5a | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 1872 | /* |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1873 | * Aliased to 'perf script' for now: |
Xiao Guangrong | c0777c5a | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 1874 | */ |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1875 | if (!strcmp(argv[0], "script")) |
| 1876 | return cmd_script(argc, argv, prefix); |
Xiao Guangrong | c0777c5a | 2009-12-07 12:04:49 +0800 | [diff] [blame] | 1877 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1878 | if (!strncmp(argv[0], "rec", 3)) { |
| 1879 | return __cmd_record(argc, argv); |
| 1880 | } else if (!strncmp(argv[0], "lat", 3)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1881 | sched.tp_handler = &lat_ops; |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1882 | if (argc > 1) { |
| 1883 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); |
| 1884 | if (argc) |
| 1885 | usage_with_options(latency_usage, latency_options); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1886 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1887 | setup_sorting(&sched, latency_options, latency_usage); |
| 1888 | return perf_sched__lat(&sched); |
Ingo Molnar | 0ec04e1 | 2009-09-16 17:40:48 +0200 | [diff] [blame] | 1889 | } else if (!strcmp(argv[0], "map")) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1890 | sched.tp_handler = &map_ops; |
| 1891 | setup_sorting(&sched, latency_options, latency_usage); |
| 1892 | return perf_sched__map(&sched); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1893 | } else if (!strncmp(argv[0], "rep", 3)) { |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1894 | sched.tp_handler = &replay_ops; |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1895 | if (argc) { |
| 1896 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); |
| 1897 | if (argc) |
| 1898 | usage_with_options(replay_usage, replay_options); |
| 1899 | } |
Arnaldo Carvalho de Melo | 0e9b07e | 2012-09-11 17:29:27 -0300 | [diff] [blame] | 1900 | return perf_sched__replay(&sched); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1901 | } else { |
| 1902 | usage_with_options(sched_usage, sched_options); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1905 | return 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1906 | } |