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" |
| 5 | #include "util/cache.h" |
| 6 | #include "util/symbol.h" |
| 7 | #include "util/thread.h" |
| 8 | #include "util/header.h" |
| 9 | |
| 10 | #include "util/parse-options.h" |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 11 | #include "util/trace-event.h" |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 12 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 13 | #include "util/debug.h" |
| 14 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 15 | #include <sys/types.h> |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 16 | #include <sys/prctl.h> |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 17 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 18 | #include <semaphore.h> |
| 19 | #include <pthread.h> |
| 20 | #include <math.h> |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 21 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 22 | static char const *input_name = "perf.data"; |
| 23 | static int input; |
| 24 | static unsigned long page_size; |
| 25 | static unsigned long mmap_window = 32; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 26 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 27 | static unsigned long total_comm = 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 28 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 29 | static struct rb_root threads; |
| 30 | static struct thread *last_match; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 31 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 32 | static struct perf_header *header; |
| 33 | static u64 sample_type; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 34 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 35 | static char default_sort_order[] = "avg, max, switch, runtime"; |
| 36 | static char *sort_order = default_sort_order; |
| 37 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 38 | #define PR_SET_NAME 15 /* Set process name */ |
| 39 | #define MAX_CPUS 4096 |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 40 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 41 | #define BUG_ON(x) assert(!(x)) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 42 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 43 | static u64 run_measurement_overhead; |
| 44 | static u64 sleep_measurement_overhead; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 45 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 46 | #define COMM_LEN 20 |
| 47 | #define SYM_LEN 129 |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 48 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 49 | #define MAX_PID 65536 |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 50 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 51 | static unsigned long nr_tasks; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 52 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 53 | struct sched_atom; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 54 | |
| 55 | struct task_desc { |
| 56 | unsigned long nr; |
| 57 | unsigned long pid; |
| 58 | char comm[COMM_LEN]; |
| 59 | |
| 60 | unsigned long nr_events; |
| 61 | unsigned long curr_event; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 62 | struct sched_atom **atoms; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 63 | |
| 64 | pthread_t thread; |
| 65 | sem_t sleep_sem; |
| 66 | |
| 67 | sem_t ready_for_work; |
| 68 | sem_t work_done_sem; |
| 69 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 70 | u64 cpu_usage; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | enum sched_event_type { |
| 74 | SCHED_EVENT_RUN, |
| 75 | SCHED_EVENT_SLEEP, |
| 76 | SCHED_EVENT_WAKEUP, |
| 77 | }; |
| 78 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 79 | struct sched_atom { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 80 | enum sched_event_type type; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 81 | u64 timestamp; |
| 82 | u64 duration; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 83 | unsigned long nr; |
| 84 | int specific_wait; |
| 85 | sem_t *wait_sem; |
| 86 | struct task_desc *wakee; |
| 87 | }; |
| 88 | |
| 89 | static struct task_desc *pid_to_task[MAX_PID]; |
| 90 | |
| 91 | static struct task_desc **tasks; |
| 92 | |
| 93 | static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 94 | static u64 start_time; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 95 | |
| 96 | static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 97 | |
| 98 | static unsigned long nr_run_events; |
| 99 | static unsigned long nr_sleep_events; |
| 100 | static unsigned long nr_wakeup_events; |
| 101 | |
| 102 | static unsigned long nr_sleep_corrections; |
| 103 | static unsigned long nr_run_events_optimized; |
| 104 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 105 | static unsigned long targetless_wakeups; |
| 106 | static unsigned long multitarget_wakeups; |
| 107 | |
| 108 | static u64 cpu_usage; |
| 109 | static u64 runavg_cpu_usage; |
| 110 | static u64 parent_cpu_usage; |
| 111 | static u64 runavg_parent_cpu_usage; |
| 112 | |
| 113 | static unsigned long nr_runs; |
| 114 | static u64 sum_runtime; |
| 115 | static u64 sum_fluct; |
| 116 | static u64 run_avg; |
| 117 | |
| 118 | static unsigned long replay_repeat = 10; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 119 | static unsigned long nr_timestamps; |
| 120 | static unsigned long unordered_timestamps; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 121 | |
| 122 | #define TASK_STATE_TO_CHAR_STR "RSDTtZX" |
| 123 | |
| 124 | enum thread_state { |
| 125 | THREAD_SLEEPING = 0, |
| 126 | THREAD_WAIT_CPU, |
| 127 | THREAD_SCHED_IN, |
| 128 | THREAD_IGNORE |
| 129 | }; |
| 130 | |
| 131 | struct work_atom { |
| 132 | struct list_head list; |
| 133 | enum thread_state state; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 134 | u64 sched_out_time; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 135 | u64 wake_up_time; |
| 136 | u64 sched_in_time; |
| 137 | u64 runtime; |
| 138 | }; |
| 139 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 140 | struct work_atoms { |
| 141 | struct list_head work_list; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 142 | struct thread *thread; |
| 143 | struct rb_node node; |
| 144 | u64 max_lat; |
| 145 | u64 total_lat; |
| 146 | u64 nb_atoms; |
| 147 | u64 total_runtime; |
| 148 | }; |
| 149 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 150 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 151 | |
| 152 | static struct rb_root atom_root, sorted_atom_root; |
| 153 | |
| 154 | static u64 all_runtime; |
| 155 | static u64 all_count; |
| 156 | |
| 157 | static int read_events(void); |
| 158 | |
| 159 | |
| 160 | static u64 get_nsecs(void) |
| 161 | { |
| 162 | struct timespec ts; |
| 163 | |
| 164 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 165 | |
| 166 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 167 | } |
| 168 | |
| 169 | static void burn_nsecs(u64 nsecs) |
| 170 | { |
| 171 | u64 T0 = get_nsecs(), T1; |
| 172 | |
| 173 | do { |
| 174 | T1 = get_nsecs(); |
| 175 | } while (T1 + run_measurement_overhead < T0 + nsecs); |
| 176 | } |
| 177 | |
| 178 | static void sleep_nsecs(u64 nsecs) |
| 179 | { |
| 180 | struct timespec ts; |
| 181 | |
| 182 | ts.tv_nsec = nsecs % 999999999; |
| 183 | ts.tv_sec = nsecs / 999999999; |
| 184 | |
| 185 | nanosleep(&ts, NULL); |
| 186 | } |
| 187 | |
| 188 | static void calibrate_run_measurement_overhead(void) |
| 189 | { |
| 190 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
| 191 | int i; |
| 192 | |
| 193 | for (i = 0; i < 10; i++) { |
| 194 | T0 = get_nsecs(); |
| 195 | burn_nsecs(0); |
| 196 | T1 = get_nsecs(); |
| 197 | delta = T1-T0; |
| 198 | min_delta = min(min_delta, delta); |
| 199 | } |
| 200 | run_measurement_overhead = min_delta; |
| 201 | |
| 202 | printf("run measurement overhead: %Ld nsecs\n", min_delta); |
| 203 | } |
| 204 | |
| 205 | static void calibrate_sleep_measurement_overhead(void) |
| 206 | { |
| 207 | u64 T0, T1, delta, min_delta = 1000000000ULL; |
| 208 | int i; |
| 209 | |
| 210 | for (i = 0; i < 10; i++) { |
| 211 | T0 = get_nsecs(); |
| 212 | sleep_nsecs(10000); |
| 213 | T1 = get_nsecs(); |
| 214 | delta = T1-T0; |
| 215 | min_delta = min(min_delta, delta); |
| 216 | } |
| 217 | min_delta -= 10000; |
| 218 | sleep_measurement_overhead = min_delta; |
| 219 | |
| 220 | printf("sleep measurement overhead: %Ld nsecs\n", min_delta); |
| 221 | } |
| 222 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 223 | static struct sched_atom * |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 224 | get_new_event(struct task_desc *task, u64 timestamp) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 225 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 226 | struct sched_atom *event = calloc(1, sizeof(*event)); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 227 | unsigned long idx = task->nr_events; |
| 228 | size_t size; |
| 229 | |
| 230 | event->timestamp = timestamp; |
| 231 | event->nr = idx; |
| 232 | |
| 233 | task->nr_events++; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 234 | size = sizeof(struct sched_atom *) * task->nr_events; |
| 235 | task->atoms = realloc(task->atoms, size); |
| 236 | BUG_ON(!task->atoms); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 237 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 238 | task->atoms[idx] = event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 239 | |
| 240 | return event; |
| 241 | } |
| 242 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 243 | static struct sched_atom *last_event(struct task_desc *task) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 244 | { |
| 245 | if (!task->nr_events) |
| 246 | return NULL; |
| 247 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 248 | return task->atoms[task->nr_events - 1]; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 252 | add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 253 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 254 | struct sched_atom *event, *curr_event = last_event(task); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 255 | |
| 256 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 257 | * optimize an existing RUN event by merging this one |
| 258 | * to it: |
| 259 | */ |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 260 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
| 261 | nr_run_events_optimized++; |
| 262 | curr_event->duration += duration; |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | event = get_new_event(task, timestamp); |
| 267 | |
| 268 | event->type = SCHED_EVENT_RUN; |
| 269 | event->duration = duration; |
| 270 | |
| 271 | nr_run_events++; |
| 272 | } |
| 273 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 274 | static void |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 275 | add_sched_event_wakeup(struct task_desc *task, u64 timestamp, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 276 | struct task_desc *wakee) |
| 277 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 278 | struct sched_atom *event, *wakee_event; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 279 | |
| 280 | event = get_new_event(task, timestamp); |
| 281 | event->type = SCHED_EVENT_WAKEUP; |
| 282 | event->wakee = wakee; |
| 283 | |
| 284 | wakee_event = last_event(wakee); |
| 285 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { |
| 286 | targetless_wakeups++; |
| 287 | return; |
| 288 | } |
| 289 | if (wakee_event->wait_sem) { |
| 290 | multitarget_wakeups++; |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem)); |
| 295 | sem_init(wakee_event->wait_sem, 0, 0); |
| 296 | wakee_event->specific_wait = 1; |
| 297 | event->wait_sem = wakee_event->wait_sem; |
| 298 | |
| 299 | nr_wakeup_events++; |
| 300 | } |
| 301 | |
| 302 | static void |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 303 | add_sched_event_sleep(struct task_desc *task, u64 timestamp, |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 304 | u64 task_state __used) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 305 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 306 | struct sched_atom *event = get_new_event(task, timestamp); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 307 | |
| 308 | event->type = SCHED_EVENT_SLEEP; |
| 309 | |
| 310 | nr_sleep_events++; |
| 311 | } |
| 312 | |
| 313 | static struct task_desc *register_pid(unsigned long pid, const char *comm) |
| 314 | { |
| 315 | struct task_desc *task; |
| 316 | |
| 317 | BUG_ON(pid >= MAX_PID); |
| 318 | |
| 319 | task = pid_to_task[pid]; |
| 320 | |
| 321 | if (task) |
| 322 | return task; |
| 323 | |
| 324 | task = calloc(1, sizeof(*task)); |
| 325 | task->pid = pid; |
| 326 | task->nr = nr_tasks; |
| 327 | strcpy(task->comm, comm); |
| 328 | /* |
| 329 | * every task starts in sleeping state - this gets ignored |
| 330 | * if there's no wakeup pointing to this sleep state: |
| 331 | */ |
| 332 | add_sched_event_sleep(task, 0, 0); |
| 333 | |
| 334 | pid_to_task[pid] = task; |
| 335 | nr_tasks++; |
| 336 | tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *)); |
| 337 | BUG_ON(!tasks); |
| 338 | tasks[task->nr] = task; |
| 339 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 340 | if (verbose) |
| 341 | printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 342 | |
| 343 | return task; |
| 344 | } |
| 345 | |
| 346 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 347 | static void print_task_traces(void) |
| 348 | { |
| 349 | struct task_desc *task; |
| 350 | unsigned long i; |
| 351 | |
| 352 | for (i = 0; i < nr_tasks; i++) { |
| 353 | task = tasks[i]; |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 354 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 355 | task->nr, task->comm, task->pid, task->nr_events); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | static void add_cross_task_wakeups(void) |
| 360 | { |
| 361 | struct task_desc *task1, *task2; |
| 362 | unsigned long i, j; |
| 363 | |
| 364 | for (i = 0; i < nr_tasks; i++) { |
| 365 | task1 = tasks[i]; |
| 366 | j = i + 1; |
| 367 | if (j == nr_tasks) |
| 368 | j = 0; |
| 369 | task2 = tasks[j]; |
| 370 | add_sched_event_wakeup(task1, 0, task2); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 375 | process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 376 | { |
| 377 | int ret = 0; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 378 | u64 now; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 379 | long long delta; |
| 380 | |
| 381 | now = get_nsecs(); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 382 | delta = start_time + atom->timestamp - now; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 383 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 384 | switch (atom->type) { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 385 | case SCHED_EVENT_RUN: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 386 | burn_nsecs(atom->duration); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 387 | break; |
| 388 | case SCHED_EVENT_SLEEP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 389 | if (atom->wait_sem) |
| 390 | ret = sem_wait(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 391 | BUG_ON(ret); |
| 392 | break; |
| 393 | case SCHED_EVENT_WAKEUP: |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 394 | if (atom->wait_sem) |
| 395 | ret = sem_post(atom->wait_sem); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 396 | BUG_ON(ret); |
| 397 | break; |
| 398 | default: |
| 399 | BUG_ON(1); |
| 400 | } |
| 401 | } |
| 402 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 403 | static u64 get_cpu_usage_nsec_parent(void) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 404 | { |
| 405 | struct rusage ru; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 406 | u64 sum; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 407 | int err; |
| 408 | |
| 409 | err = getrusage(RUSAGE_SELF, &ru); |
| 410 | BUG_ON(err); |
| 411 | |
| 412 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; |
| 413 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; |
| 414 | |
| 415 | return sum; |
| 416 | } |
| 417 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 418 | static u64 get_cpu_usage_nsec_self(void) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 419 | { |
| 420 | char filename [] = "/proc/1234567890/sched"; |
| 421 | unsigned long msecs, nsecs; |
| 422 | char *line = NULL; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 423 | u64 total = 0; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 424 | size_t len = 0; |
| 425 | ssize_t chars; |
| 426 | FILE *file; |
| 427 | int ret; |
| 428 | |
| 429 | sprintf(filename, "/proc/%d/sched", getpid()); |
| 430 | file = fopen(filename, "r"); |
| 431 | BUG_ON(!file); |
| 432 | |
| 433 | while ((chars = getline(&line, &len, file)) != -1) { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 434 | ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n", |
| 435 | &msecs, &nsecs); |
| 436 | if (ret == 2) { |
| 437 | total = msecs*1e6 + nsecs; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | } |
| 441 | if (line) |
| 442 | free(line); |
| 443 | fclose(file); |
| 444 | |
| 445 | return total; |
| 446 | } |
| 447 | |
| 448 | static void *thread_func(void *ctx) |
| 449 | { |
| 450 | struct task_desc *this_task = ctx; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 451 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 452 | unsigned long i, ret; |
| 453 | char comm2[22]; |
| 454 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 455 | sprintf(comm2, ":%s", this_task->comm); |
| 456 | prctl(PR_SET_NAME, comm2); |
| 457 | |
| 458 | again: |
| 459 | ret = sem_post(&this_task->ready_for_work); |
| 460 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 461 | ret = pthread_mutex_lock(&start_work_mutex); |
| 462 | BUG_ON(ret); |
| 463 | ret = pthread_mutex_unlock(&start_work_mutex); |
| 464 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 465 | |
| 466 | cpu_usage_0 = get_cpu_usage_nsec_self(); |
| 467 | |
| 468 | for (i = 0; i < this_task->nr_events; i++) { |
| 469 | this_task->curr_event = i; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 470 | process_sched_event(this_task, this_task->atoms[i]); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | cpu_usage_1 = get_cpu_usage_nsec_self(); |
| 474 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 475 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 476 | ret = sem_post(&this_task->work_done_sem); |
| 477 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 478 | |
| 479 | ret = pthread_mutex_lock(&work_done_wait_mutex); |
| 480 | BUG_ON(ret); |
| 481 | ret = pthread_mutex_unlock(&work_done_wait_mutex); |
| 482 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 483 | |
| 484 | goto again; |
| 485 | } |
| 486 | |
| 487 | static void create_tasks(void) |
| 488 | { |
| 489 | struct task_desc *task; |
| 490 | pthread_attr_t attr; |
| 491 | unsigned long i; |
| 492 | int err; |
| 493 | |
| 494 | err = pthread_attr_init(&attr); |
| 495 | BUG_ON(err); |
| 496 | err = pthread_attr_setstacksize(&attr, (size_t)(16*1024)); |
| 497 | BUG_ON(err); |
| 498 | err = pthread_mutex_lock(&start_work_mutex); |
| 499 | BUG_ON(err); |
| 500 | err = pthread_mutex_lock(&work_done_wait_mutex); |
| 501 | BUG_ON(err); |
| 502 | for (i = 0; i < nr_tasks; i++) { |
| 503 | task = tasks[i]; |
| 504 | sem_init(&task->sleep_sem, 0, 0); |
| 505 | sem_init(&task->ready_for_work, 0, 0); |
| 506 | sem_init(&task->work_done_sem, 0, 0); |
| 507 | task->curr_event = 0; |
| 508 | err = pthread_create(&task->thread, &attr, thread_func, task); |
| 509 | BUG_ON(err); |
| 510 | } |
| 511 | } |
| 512 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 513 | static void wait_for_tasks(void) |
| 514 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 515 | u64 cpu_usage_0, cpu_usage_1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 516 | struct task_desc *task; |
| 517 | unsigned long i, ret; |
| 518 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 519 | start_time = get_nsecs(); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 520 | cpu_usage = 0; |
| 521 | pthread_mutex_unlock(&work_done_wait_mutex); |
| 522 | |
| 523 | for (i = 0; i < nr_tasks; i++) { |
| 524 | task = tasks[i]; |
| 525 | ret = sem_wait(&task->ready_for_work); |
| 526 | BUG_ON(ret); |
| 527 | sem_init(&task->ready_for_work, 0, 0); |
| 528 | } |
| 529 | ret = pthread_mutex_lock(&work_done_wait_mutex); |
| 530 | BUG_ON(ret); |
| 531 | |
| 532 | cpu_usage_0 = get_cpu_usage_nsec_parent(); |
| 533 | |
| 534 | pthread_mutex_unlock(&start_work_mutex); |
| 535 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 536 | for (i = 0; i < nr_tasks; i++) { |
| 537 | task = tasks[i]; |
| 538 | ret = sem_wait(&task->work_done_sem); |
| 539 | BUG_ON(ret); |
| 540 | sem_init(&task->work_done_sem, 0, 0); |
| 541 | cpu_usage += task->cpu_usage; |
| 542 | task->cpu_usage = 0; |
| 543 | } |
| 544 | |
| 545 | cpu_usage_1 = get_cpu_usage_nsec_parent(); |
| 546 | if (!runavg_cpu_usage) |
| 547 | runavg_cpu_usage = cpu_usage; |
| 548 | runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10; |
| 549 | |
| 550 | parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 551 | if (!runavg_parent_cpu_usage) |
| 552 | runavg_parent_cpu_usage = parent_cpu_usage; |
| 553 | runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 + |
| 554 | parent_cpu_usage)/10; |
| 555 | |
| 556 | ret = pthread_mutex_lock(&start_work_mutex); |
| 557 | BUG_ON(ret); |
| 558 | |
| 559 | for (i = 0; i < nr_tasks; i++) { |
| 560 | task = tasks[i]; |
| 561 | sem_init(&task->sleep_sem, 0, 0); |
| 562 | task->curr_event = 0; |
| 563 | } |
| 564 | } |
| 565 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 566 | static void run_one_test(void) |
| 567 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 568 | u64 T0, T1, delta, avg_delta, fluct, std_dev; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 569 | |
| 570 | T0 = get_nsecs(); |
| 571 | wait_for_tasks(); |
| 572 | T1 = get_nsecs(); |
| 573 | |
| 574 | delta = T1 - T0; |
| 575 | sum_runtime += delta; |
| 576 | nr_runs++; |
| 577 | |
| 578 | avg_delta = sum_runtime / nr_runs; |
| 579 | if (delta < avg_delta) |
| 580 | fluct = avg_delta - delta; |
| 581 | else |
| 582 | fluct = delta - avg_delta; |
| 583 | sum_fluct += fluct; |
| 584 | std_dev = sum_fluct / nr_runs / sqrt(nr_runs); |
| 585 | if (!run_avg) |
| 586 | run_avg = delta; |
| 587 | run_avg = (run_avg*9 + delta)/10; |
| 588 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 589 | printf("#%-3ld: %0.3f, ", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 590 | nr_runs, (double)delta/1000000.0); |
| 591 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 592 | printf("ravg: %0.2f, ", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 593 | (double)run_avg/1e6); |
| 594 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 595 | printf("cpu: %0.2f / %0.2f", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 596 | (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6); |
| 597 | |
| 598 | #if 0 |
| 599 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 600 | * rusage statistics done by the parent, these are less |
| 601 | * accurate than the sum_exec_runtime based statistics: |
| 602 | */ |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 603 | printf(" [%0.2f / %0.2f]", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 604 | (double)parent_cpu_usage/1e6, |
| 605 | (double)runavg_parent_cpu_usage/1e6); |
| 606 | #endif |
| 607 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 608 | printf("\n"); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 609 | |
| 610 | if (nr_sleep_corrections) |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 611 | printf(" (%ld sleep corrections)\n", nr_sleep_corrections); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 612 | nr_sleep_corrections = 0; |
| 613 | } |
| 614 | |
| 615 | static void test_calibrations(void) |
| 616 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 617 | u64 T0, T1; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 618 | |
| 619 | T0 = get_nsecs(); |
| 620 | burn_nsecs(1e6); |
| 621 | T1 = get_nsecs(); |
| 622 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 623 | printf("the run test took %Ld nsecs\n", T1-T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 624 | |
| 625 | T0 = get_nsecs(); |
| 626 | sleep_nsecs(1e6); |
| 627 | T1 = get_nsecs(); |
| 628 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 629 | printf("the sleep test took %Ld nsecs\n", T1-T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 632 | static void __cmd_replay(void) |
| 633 | { |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 634 | unsigned long i; |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 635 | |
| 636 | calibrate_run_measurement_overhead(); |
| 637 | calibrate_sleep_measurement_overhead(); |
| 638 | |
| 639 | test_calibrations(); |
| 640 | |
| 641 | read_events(); |
| 642 | |
| 643 | printf("nr_run_events: %ld\n", nr_run_events); |
| 644 | printf("nr_sleep_events: %ld\n", nr_sleep_events); |
| 645 | printf("nr_wakeup_events: %ld\n", nr_wakeup_events); |
| 646 | |
| 647 | if (targetless_wakeups) |
| 648 | printf("target-less wakeups: %ld\n", targetless_wakeups); |
| 649 | if (multitarget_wakeups) |
| 650 | printf("multi-target wakeups: %ld\n", multitarget_wakeups); |
| 651 | if (nr_run_events_optimized) |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 652 | printf("run atoms optimized: %ld\n", |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 653 | nr_run_events_optimized); |
| 654 | |
| 655 | print_task_traces(); |
| 656 | add_cross_task_wakeups(); |
| 657 | |
| 658 | create_tasks(); |
| 659 | printf("------------------------------------------------------------\n"); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 660 | for (i = 0; i < replay_repeat; i++) |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 661 | run_one_test(); |
| 662 | } |
| 663 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 664 | static int |
| 665 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 666 | { |
| 667 | struct thread *thread; |
| 668 | |
| 669 | thread = threads__findnew(event->comm.pid, &threads, &last_match); |
| 670 | |
| 671 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 672 | (void *)(offset + head), |
| 673 | (void *)(long)(event->header.size), |
| 674 | event->comm.comm, event->comm.pid); |
| 675 | |
| 676 | if (thread == NULL || |
| 677 | thread__set_comm(thread, event->comm.comm)) { |
| 678 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 679 | return -1; |
| 680 | } |
| 681 | total_comm++; |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 686 | |
| 687 | struct raw_event_sample { |
| 688 | u32 size; |
| 689 | char data[0]; |
| 690 | }; |
| 691 | |
| 692 | #define FILL_FIELD(ptr, field, event, data) \ |
| 693 | ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data) |
| 694 | |
| 695 | #define FILL_ARRAY(ptr, array, event, data) \ |
| 696 | do { \ |
| 697 | void *__array = raw_field_ptr(event, #array, data); \ |
| 698 | memcpy(ptr.array, __array, sizeof(ptr.array)); \ |
| 699 | } while(0) |
| 700 | |
| 701 | #define FILL_COMMON_FIELDS(ptr, event, data) \ |
| 702 | do { \ |
| 703 | FILL_FIELD(ptr, common_type, event, data); \ |
| 704 | FILL_FIELD(ptr, common_flags, event, data); \ |
| 705 | FILL_FIELD(ptr, common_preempt_count, event, data); \ |
| 706 | FILL_FIELD(ptr, common_pid, event, data); \ |
| 707 | FILL_FIELD(ptr, common_tgid, event, data); \ |
| 708 | } while (0) |
| 709 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 710 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 711 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 712 | struct trace_switch_event { |
| 713 | u32 size; |
| 714 | |
| 715 | u16 common_type; |
| 716 | u8 common_flags; |
| 717 | u8 common_preempt_count; |
| 718 | u32 common_pid; |
| 719 | u32 common_tgid; |
| 720 | |
| 721 | char prev_comm[16]; |
| 722 | u32 prev_pid; |
| 723 | u32 prev_prio; |
| 724 | u64 prev_state; |
| 725 | char next_comm[16]; |
| 726 | u32 next_pid; |
| 727 | u32 next_prio; |
| 728 | }; |
| 729 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 730 | struct trace_runtime_event { |
| 731 | u32 size; |
| 732 | |
| 733 | u16 common_type; |
| 734 | u8 common_flags; |
| 735 | u8 common_preempt_count; |
| 736 | u32 common_pid; |
| 737 | u32 common_tgid; |
| 738 | |
| 739 | char comm[16]; |
| 740 | u32 pid; |
| 741 | u64 runtime; |
| 742 | u64 vruntime; |
| 743 | }; |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 744 | |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 745 | struct trace_wakeup_event { |
| 746 | u32 size; |
| 747 | |
| 748 | u16 common_type; |
| 749 | u8 common_flags; |
| 750 | u8 common_preempt_count; |
| 751 | u32 common_pid; |
| 752 | u32 common_tgid; |
| 753 | |
| 754 | char comm[16]; |
| 755 | u32 pid; |
| 756 | |
| 757 | u32 prio; |
| 758 | u32 success; |
| 759 | u32 cpu; |
| 760 | }; |
| 761 | |
| 762 | struct trace_fork_event { |
| 763 | u32 size; |
| 764 | |
| 765 | u16 common_type; |
| 766 | u8 common_flags; |
| 767 | u8 common_preempt_count; |
| 768 | u32 common_pid; |
| 769 | u32 common_tgid; |
| 770 | |
| 771 | char parent_comm[16]; |
| 772 | u32 parent_pid; |
| 773 | char child_comm[16]; |
| 774 | u32 child_pid; |
| 775 | }; |
| 776 | |
| 777 | struct trace_sched_handler { |
| 778 | void (*switch_event)(struct trace_switch_event *, |
| 779 | struct event *, |
| 780 | int cpu, |
| 781 | u64 timestamp, |
| 782 | struct thread *thread); |
| 783 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 784 | void (*runtime_event)(struct trace_runtime_event *, |
| 785 | struct event *, |
| 786 | int cpu, |
| 787 | u64 timestamp, |
| 788 | struct thread *thread); |
| 789 | |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 790 | void (*wakeup_event)(struct trace_wakeup_event *, |
| 791 | struct event *, |
| 792 | int cpu, |
| 793 | u64 timestamp, |
| 794 | struct thread *thread); |
| 795 | |
| 796 | void (*fork_event)(struct trace_fork_event *, |
| 797 | struct event *, |
| 798 | int cpu, |
| 799 | u64 timestamp, |
| 800 | struct thread *thread); |
| 801 | }; |
| 802 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 803 | |
| 804 | static void |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 805 | replay_wakeup_event(struct trace_wakeup_event *wakeup_event, |
| 806 | struct event *event, |
| 807 | int cpu __used, |
| 808 | u64 timestamp __used, |
| 809 | struct thread *thread __used) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 810 | { |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 811 | struct task_desc *waker, *wakee; |
| 812 | |
| 813 | if (verbose) { |
| 814 | printf("sched_wakeup event %p\n", event); |
| 815 | |
| 816 | printf(" ... pid %d woke up %s/%d\n", |
| 817 | wakeup_event->common_pid, |
| 818 | wakeup_event->comm, |
| 819 | wakeup_event->pid); |
| 820 | } |
| 821 | |
| 822 | waker = register_pid(wakeup_event->common_pid, "<unknown>"); |
| 823 | wakee = register_pid(wakeup_event->pid, wakeup_event->comm); |
| 824 | |
| 825 | add_sched_event_wakeup(waker, timestamp, wakee); |
| 826 | } |
| 827 | |
Ingo Molnar | d115338 | 2009-09-14 18:22:53 +0200 | [diff] [blame] | 828 | static u64 cpu_last_switched[MAX_CPUS]; |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 829 | |
| 830 | static void |
| 831 | replay_switch_event(struct trace_switch_event *switch_event, |
| 832 | struct event *event, |
| 833 | int cpu, |
| 834 | u64 timestamp, |
| 835 | struct thread *thread __used) |
| 836 | { |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 837 | struct task_desc *prev, *next; |
| 838 | u64 timestamp0; |
| 839 | s64 delta; |
| 840 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 841 | if (verbose) |
| 842 | printf("sched_switch event %p\n", event); |
| 843 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 844 | if (cpu >= MAX_CPUS || cpu < 0) |
| 845 | return; |
| 846 | |
| 847 | timestamp0 = cpu_last_switched[cpu]; |
| 848 | if (timestamp0) |
| 849 | delta = timestamp - timestamp0; |
| 850 | else |
| 851 | delta = 0; |
| 852 | |
| 853 | if (delta < 0) |
| 854 | die("hm, delta: %Ld < 0 ?\n", delta); |
| 855 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 856 | if (verbose) { |
| 857 | printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n", |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 858 | switch_event->prev_comm, switch_event->prev_pid, |
| 859 | switch_event->next_comm, switch_event->next_pid, |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 860 | delta); |
| 861 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 862 | |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 863 | prev = register_pid(switch_event->prev_pid, switch_event->prev_comm); |
| 864 | next = register_pid(switch_event->next_pid, switch_event->next_comm); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 865 | |
| 866 | cpu_last_switched[cpu] = timestamp; |
| 867 | |
| 868 | add_sched_event_run(prev, timestamp, delta); |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 869 | add_sched_event_sleep(prev, timestamp, switch_event->prev_state); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 870 | } |
| 871 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 872 | |
| 873 | static void |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 874 | replay_fork_event(struct trace_fork_event *fork_event, |
| 875 | struct event *event, |
| 876 | int cpu __used, |
| 877 | u64 timestamp __used, |
| 878 | struct thread *thread __used) |
| 879 | { |
| 880 | if (verbose) { |
| 881 | printf("sched_fork event %p\n", event); |
| 882 | printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid); |
| 883 | printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid); |
| 884 | } |
| 885 | register_pid(fork_event->parent_pid, fork_event->parent_comm); |
| 886 | register_pid(fork_event->child_pid, fork_event->child_comm); |
| 887 | } |
| 888 | |
| 889 | static struct trace_sched_handler replay_ops = { |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 890 | .wakeup_event = replay_wakeup_event, |
| 891 | .switch_event = replay_switch_event, |
| 892 | .fork_event = replay_fork_event, |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 893 | }; |
| 894 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 895 | struct sort_dimension { |
| 896 | const char *name; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 897 | sort_fn_t cmp; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 898 | struct list_head list; |
| 899 | }; |
| 900 | |
| 901 | static LIST_HEAD(cmp_pid); |
| 902 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 903 | static int |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 904 | 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] | 905 | { |
| 906 | struct sort_dimension *sort; |
| 907 | int ret = 0; |
| 908 | |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 909 | BUG_ON(list_empty(list)); |
| 910 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 911 | list_for_each_entry(sort, list, list) { |
| 912 | ret = sort->cmp(l, r); |
| 913 | if (ret) |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 920 | static struct work_atoms * |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 921 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
| 922 | struct list_head *sort_list) |
| 923 | { |
| 924 | struct rb_node *node = root->rb_node; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 925 | struct work_atoms key = { .thread = thread }; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 926 | |
| 927 | while (node) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 928 | struct work_atoms *atoms; |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 929 | int cmp; |
| 930 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 931 | atoms = container_of(node, struct work_atoms, node); |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 932 | |
| 933 | cmp = thread_lat_cmp(sort_list, &key, atoms); |
| 934 | if (cmp > 0) |
| 935 | node = node->rb_left; |
| 936 | else if (cmp < 0) |
| 937 | node = node->rb_right; |
| 938 | else { |
| 939 | BUG_ON(thread != atoms->thread); |
| 940 | return atoms; |
| 941 | } |
| 942 | } |
| 943 | return NULL; |
| 944 | } |
| 945 | |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 946 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 947 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 948 | struct list_head *sort_list) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 949 | { |
| 950 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 951 | |
| 952 | while (*new) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 953 | struct work_atoms *this; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 954 | int cmp; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 955 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 956 | this = container_of(*new, struct work_atoms, node); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 957 | parent = *new; |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 958 | |
| 959 | cmp = thread_lat_cmp(sort_list, data, this); |
| 960 | |
| 961 | if (cmp > 0) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 962 | new = &((*new)->rb_left); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 963 | else |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 964 | new = &((*new)->rb_right); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | rb_link_node(&data->node, parent, new); |
| 968 | rb_insert_color(&data->node, root); |
| 969 | } |
| 970 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 971 | static void thread_atoms_insert(struct thread *thread) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 972 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 973 | struct work_atoms *atoms; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 974 | |
Frederic Weisbecker | 1756220 | 2009-09-12 23:11:32 +0200 | [diff] [blame] | 975 | atoms = calloc(sizeof(*atoms), 1); |
| 976 | if (!atoms) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 977 | die("No memory"); |
| 978 | |
Frederic Weisbecker | 1756220 | 2009-09-12 23:11:32 +0200 | [diff] [blame] | 979 | atoms->thread = thread; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 980 | INIT_LIST_HEAD(&atoms->work_list); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 981 | __thread_latency_insert(&atom_root, atoms, &cmp_pid); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static void |
| 985 | latency_fork_event(struct trace_fork_event *fork_event __used, |
| 986 | struct event *event __used, |
| 987 | int cpu __used, |
| 988 | u64 timestamp __used, |
| 989 | struct thread *thread __used) |
| 990 | { |
| 991 | /* should insert the newcomer */ |
| 992 | } |
| 993 | |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 994 | __used |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 995 | static char sched_out_state(struct trace_switch_event *switch_event) |
| 996 | { |
| 997 | const char *str = TASK_STATE_TO_CHAR_STR; |
| 998 | |
| 999 | return str[switch_event->prev_state]; |
| 1000 | } |
| 1001 | |
| 1002 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1003 | add_sched_out_event(struct work_atoms *atoms, |
| 1004 | char run_state, |
| 1005 | u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1006 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1007 | struct work_atom *atom; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1008 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1009 | atom = calloc(sizeof(*atom), 1); |
| 1010 | if (!atom) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1011 | die("Non memory"); |
| 1012 | |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1013 | atom->sched_out_time = timestamp; |
| 1014 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1015 | if (run_state == 'R') { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1016 | atom->state = THREAD_WAIT_CPU; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1017 | atom->wake_up_time = atom->sched_out_time; |
Frederic Weisbecker | c6ced61 | 2009-09-13 00:46:19 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1020 | list_add_tail(&atom->list, &atoms->work_list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1024 | add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used) |
| 1025 | { |
| 1026 | struct work_atom *atom; |
| 1027 | |
| 1028 | BUG_ON(list_empty(&atoms->work_list)); |
| 1029 | |
| 1030 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
| 1031 | |
| 1032 | atom->runtime += delta; |
| 1033 | atoms->total_runtime += delta; |
| 1034 | } |
| 1035 | |
| 1036 | static void |
| 1037 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1038 | { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1039 | struct work_atom *atom; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1040 | u64 delta; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1041 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1042 | if (list_empty(&atoms->work_list)) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1043 | return; |
| 1044 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1045 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1046 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1047 | if (atom->state != THREAD_WAIT_CPU) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1048 | return; |
| 1049 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1050 | if (timestamp < atom->wake_up_time) { |
| 1051 | atom->state = THREAD_IGNORE; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1052 | return; |
| 1053 | } |
| 1054 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1055 | atom->state = THREAD_SCHED_IN; |
| 1056 | atom->sched_in_time = timestamp; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1057 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1058 | delta = atom->sched_in_time - atom->wake_up_time; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1059 | atoms->total_lat += delta; |
| 1060 | if (delta > atoms->max_lat) |
| 1061 | atoms->max_lat = delta; |
| 1062 | atoms->nb_atoms++; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1065 | static void |
| 1066 | latency_switch_event(struct trace_switch_event *switch_event, |
| 1067 | struct event *event __used, |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1068 | int cpu, |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1069 | u64 timestamp, |
| 1070 | struct thread *thread __used) |
| 1071 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1072 | struct work_atoms *out_events, *in_events; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1073 | struct thread *sched_out, *sched_in; |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1074 | u64 timestamp0; |
| 1075 | s64 delta; |
| 1076 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1077 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1078 | |
| 1079 | timestamp0 = cpu_last_switched[cpu]; |
| 1080 | cpu_last_switched[cpu] = timestamp; |
| 1081 | if (timestamp0) |
| 1082 | delta = timestamp - timestamp0; |
| 1083 | else |
| 1084 | delta = 0; |
| 1085 | |
| 1086 | if (delta < 0) |
| 1087 | die("hm, delta: %Ld < 0 ?\n", delta); |
| 1088 | |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1089 | |
| 1090 | sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match); |
| 1091 | sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match); |
| 1092 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1093 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); |
| 1094 | if (!out_events) { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1095 | thread_atoms_insert(sched_out); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1096 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); |
| 1097 | if (!out_events) |
| 1098 | die("out-event: Internal tree error"); |
| 1099 | } |
| 1100 | add_sched_out_event(out_events, sched_out_state(switch_event), timestamp); |
| 1101 | |
| 1102 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); |
| 1103 | if (!in_events) { |
| 1104 | thread_atoms_insert(sched_in); |
| 1105 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); |
| 1106 | if (!in_events) |
| 1107 | die("in-event: Internal tree error"); |
| 1108 | /* |
| 1109 | * Take came in we have not heard about yet, |
| 1110 | * add in an initial atom in runnable state: |
| 1111 | */ |
| 1112 | add_sched_out_event(in_events, 'R', timestamp); |
| 1113 | } |
| 1114 | add_sched_in_event(in_events, timestamp); |
| 1115 | } |
| 1116 | |
| 1117 | static void |
| 1118 | latency_runtime_event(struct trace_runtime_event *runtime_event, |
| 1119 | struct event *event __used, |
| 1120 | int cpu, |
| 1121 | u64 timestamp, |
| 1122 | struct thread *this_thread __used) |
| 1123 | { |
| 1124 | struct work_atoms *atoms; |
| 1125 | struct thread *thread; |
| 1126 | |
| 1127 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
| 1128 | |
| 1129 | thread = threads__findnew(runtime_event->pid, &threads, &last_match); |
| 1130 | atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); |
| 1131 | if (!atoms) { |
| 1132 | thread_atoms_insert(thread); |
| 1133 | atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); |
| 1134 | if (!atoms) |
| 1135 | die("in-event: Internal tree error"); |
| 1136 | add_sched_out_event(atoms, 'R', timestamp); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1139 | add_runtime_event(atoms, runtime_event->runtime, timestamp); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static void |
| 1143 | latency_wakeup_event(struct trace_wakeup_event *wakeup_event, |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1144 | struct event *__event __used, |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1145 | int cpu __used, |
| 1146 | u64 timestamp, |
| 1147 | struct thread *thread __used) |
| 1148 | { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1149 | struct work_atoms *atoms; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1150 | struct work_atom *atom; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1151 | struct thread *wakee; |
| 1152 | |
| 1153 | /* Note for later, it may be interesting to observe the failing cases */ |
| 1154 | if (!wakeup_event->success) |
| 1155 | return; |
| 1156 | |
| 1157 | wakee = threads__findnew(wakeup_event->pid, &threads, &last_match); |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1158 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); |
Frederic Weisbecker | 1756220 | 2009-09-12 23:11:32 +0200 | [diff] [blame] | 1159 | if (!atoms) { |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1160 | thread_atoms_insert(wakee); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1161 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); |
| 1162 | if (!atoms) |
| 1163 | die("wakeup-event: Internal tree error"); |
| 1164 | add_sched_out_event(atoms, 'S', timestamp); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1167 | BUG_ON(list_empty(&atoms->work_list)); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1168 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1169 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1170 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1171 | if (atom->state != THREAD_SLEEPING) { |
| 1172 | printf("boo2\n"); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1173 | return; |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1174 | } |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1175 | |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1176 | nr_timestamps++; |
| 1177 | if (atom->sched_out_time > timestamp) { |
| 1178 | unordered_timestamps++; |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1179 | return; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1180 | } |
Frederic Weisbecker | aa1ab9d | 2009-09-14 03:01:12 +0200 | [diff] [blame] | 1181 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1182 | atom->state = THREAD_WAIT_CPU; |
| 1183 | atom->wake_up_time = timestamp; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static struct trace_sched_handler lat_ops = { |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1187 | .wakeup_event = latency_wakeup_event, |
| 1188 | .switch_event = latency_switch_event, |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1189 | .runtime_event = latency_runtime_event, |
Ingo Molnar | ea92ed5 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1190 | .fork_event = latency_fork_event, |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1191 | }; |
| 1192 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1193 | static void output_lat_thread(struct work_atoms *work_list) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1194 | { |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1195 | int i; |
| 1196 | int ret; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1197 | u64 avg; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1198 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1199 | if (!work_list->nb_atoms) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1200 | return; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1201 | /* |
| 1202 | * Ignore idle threads: |
| 1203 | */ |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1204 | if (!work_list->thread->pid) |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1205 | return; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1206 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1207 | all_runtime += work_list->total_runtime; |
| 1208 | all_count += work_list->nb_atoms; |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1209 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1210 | ret = printf(" %s-%d ", work_list->thread->comm, work_list->thread->pid); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1211 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1212 | for (i = 0; i < 24 - ret; i++) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1213 | printf(" "); |
| 1214 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1215 | avg = work_list->total_lat / work_list->nb_atoms; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1216 | |
Frederic Weisbecker | 6668567 | 2009-09-13 01:56:25 +0200 | [diff] [blame] | 1217 | printf("|%9.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n", |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1218 | (double)work_list->total_runtime / 1e6, |
| 1219 | work_list->nb_atoms, (double)avg / 1e6, |
| 1220 | (double)work_list->max_lat / 1e6); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1223 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1224 | { |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1225 | if (l->thread->pid < r->thread->pid) |
| 1226 | return -1; |
| 1227 | if (l->thread->pid > r->thread->pid) |
| 1228 | return 1; |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static struct sort_dimension pid_sort_dimension = { |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1234 | .name = "pid", |
| 1235 | .cmp = pid_cmp, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1236 | }; |
| 1237 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1238 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1239 | { |
| 1240 | u64 avgl, avgr; |
| 1241 | |
| 1242 | if (!l->nb_atoms) |
| 1243 | return -1; |
| 1244 | |
| 1245 | if (!r->nb_atoms) |
| 1246 | return 1; |
| 1247 | |
| 1248 | avgl = l->total_lat / l->nb_atoms; |
| 1249 | avgr = r->total_lat / r->nb_atoms; |
| 1250 | |
| 1251 | if (avgl < avgr) |
| 1252 | return -1; |
| 1253 | if (avgl > avgr) |
| 1254 | return 1; |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | static struct sort_dimension avg_sort_dimension = { |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1260 | .name = "avg", |
| 1261 | .cmp = avg_cmp, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1262 | }; |
| 1263 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1264 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1265 | { |
| 1266 | if (l->max_lat < r->max_lat) |
| 1267 | return -1; |
| 1268 | if (l->max_lat > r->max_lat) |
| 1269 | return 1; |
| 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static struct sort_dimension max_sort_dimension = { |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1275 | .name = "max", |
| 1276 | .cmp = max_cmp, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1277 | }; |
| 1278 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1279 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1280 | { |
| 1281 | if (l->nb_atoms < r->nb_atoms) |
| 1282 | return -1; |
| 1283 | if (l->nb_atoms > r->nb_atoms) |
| 1284 | return 1; |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | static struct sort_dimension switch_sort_dimension = { |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1290 | .name = "switch", |
| 1291 | .cmp = switch_cmp, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1292 | }; |
| 1293 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1294 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1295 | { |
| 1296 | if (l->total_runtime < r->total_runtime) |
| 1297 | return -1; |
| 1298 | if (l->total_runtime > r->total_runtime) |
| 1299 | return 1; |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static struct sort_dimension runtime_sort_dimension = { |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1305 | .name = "runtime", |
| 1306 | .cmp = runtime_cmp, |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1307 | }; |
| 1308 | |
| 1309 | static struct sort_dimension *available_sorts[] = { |
| 1310 | &pid_sort_dimension, |
| 1311 | &avg_sort_dimension, |
| 1312 | &max_sort_dimension, |
| 1313 | &switch_sort_dimension, |
| 1314 | &runtime_sort_dimension, |
| 1315 | }; |
| 1316 | |
| 1317 | #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *)) |
| 1318 | |
| 1319 | static LIST_HEAD(sort_list); |
| 1320 | |
| 1321 | static int sort_dimension__add(char *tok, struct list_head *list) |
| 1322 | { |
| 1323 | int i; |
| 1324 | |
| 1325 | for (i = 0; i < NB_AVAILABLE_SORTS; i++) { |
| 1326 | if (!strcmp(available_sorts[i]->name, tok)) { |
| 1327 | list_add_tail(&available_sorts[i]->list, list); |
| 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | return -1; |
| 1334 | } |
| 1335 | |
| 1336 | static void setup_sorting(void); |
| 1337 | |
| 1338 | static void sort_lat(void) |
| 1339 | { |
| 1340 | struct rb_node *node; |
| 1341 | |
| 1342 | for (;;) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1343 | struct work_atoms *data; |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1344 | node = rb_first(&atom_root); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1345 | if (!node) |
| 1346 | break; |
| 1347 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1348 | rb_erase(node, &atom_root); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1349 | data = rb_entry(node, struct work_atoms, node); |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1350 | __thread_latency_insert(&sorted_atom_root, data, &sort_list); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1354 | static void __cmd_lat(void) |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1355 | { |
| 1356 | struct rb_node *next; |
| 1357 | |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1358 | setup_pager(); |
| 1359 | read_events(); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1360 | sort_lat(); |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1361 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1362 | printf("\n ---------------------------------------------------------------------------------------\n"); |
| 1363 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n"); |
| 1364 | printf(" ---------------------------------------------------------------------------------------\n"); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1365 | |
Ingo Molnar | b1ffe8f | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1366 | next = rb_first(&sorted_atom_root); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1367 | |
| 1368 | while (next) { |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1369 | struct work_atoms *work_list; |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1370 | |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1371 | work_list = rb_entry(next, struct work_atoms, node); |
| 1372 | output_lat_thread(work_list); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1373 | next = rb_next(next); |
| 1374 | } |
Ingo Molnar | d9340c1 | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1375 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1376 | printf(" ---------------------------------------------------------------------------------------\n"); |
| 1377 | printf(" TOTAL: |%9.3f ms |%9Ld |", |
Frederic Weisbecker | 7362262 | 2009-09-13 01:59:05 +0200 | [diff] [blame] | 1378 | (double)all_runtime/1e6, all_count); |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1379 | |
| 1380 | if (unordered_timestamps && nr_timestamps) { |
| 1381 | printf(" INFO: %.2f%% unordered events.\n", |
| 1382 | (double)unordered_timestamps/(double)nr_timestamps*100.0); |
| 1383 | } else { |
| 1384 | printf("\n"); |
| 1385 | } |
| 1386 | |
mingo | 08f69e6 | 2009-09-14 18:30:44 +0200 | [diff] [blame] | 1387 | printf(" -------------------------------------------------\n\n"); |
Frederic Weisbecker | cdce9d7 | 2009-09-12 08:06:14 +0200 | [diff] [blame] | 1388 | } |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1389 | |
| 1390 | static struct trace_sched_handler *trace_handler; |
| 1391 | |
| 1392 | static void |
| 1393 | process_sched_wakeup_event(struct raw_event_sample *raw, |
| 1394 | struct event *event, |
| 1395 | int cpu __used, |
| 1396 | u64 timestamp __used, |
| 1397 | struct thread *thread __used) |
| 1398 | { |
| 1399 | struct trace_wakeup_event wakeup_event; |
| 1400 | |
| 1401 | FILL_COMMON_FIELDS(wakeup_event, event, raw->data); |
| 1402 | |
| 1403 | FILL_ARRAY(wakeup_event, comm, event, raw->data); |
| 1404 | FILL_FIELD(wakeup_event, pid, event, raw->data); |
| 1405 | FILL_FIELD(wakeup_event, prio, event, raw->data); |
| 1406 | FILL_FIELD(wakeup_event, success, event, raw->data); |
| 1407 | FILL_FIELD(wakeup_event, cpu, event, raw->data); |
| 1408 | |
| 1409 | trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread); |
| 1410 | } |
| 1411 | |
| 1412 | static void |
| 1413 | process_sched_switch_event(struct raw_event_sample *raw, |
| 1414 | struct event *event, |
| 1415 | int cpu __used, |
| 1416 | u64 timestamp __used, |
| 1417 | struct thread *thread __used) |
| 1418 | { |
| 1419 | struct trace_switch_event switch_event; |
| 1420 | |
| 1421 | FILL_COMMON_FIELDS(switch_event, event, raw->data); |
| 1422 | |
| 1423 | FILL_ARRAY(switch_event, prev_comm, event, raw->data); |
| 1424 | FILL_FIELD(switch_event, prev_pid, event, raw->data); |
| 1425 | FILL_FIELD(switch_event, prev_prio, event, raw->data); |
| 1426 | FILL_FIELD(switch_event, prev_state, event, raw->data); |
| 1427 | FILL_ARRAY(switch_event, next_comm, event, raw->data); |
| 1428 | FILL_FIELD(switch_event, next_pid, event, raw->data); |
| 1429 | FILL_FIELD(switch_event, next_prio, event, raw->data); |
| 1430 | |
| 1431 | trace_handler->switch_event(&switch_event, event, cpu, timestamp, thread); |
| 1432 | } |
| 1433 | |
| 1434 | static void |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1435 | process_sched_runtime_event(struct raw_event_sample *raw, |
| 1436 | struct event *event, |
| 1437 | int cpu __used, |
| 1438 | u64 timestamp __used, |
| 1439 | struct thread *thread __used) |
| 1440 | { |
| 1441 | struct trace_runtime_event runtime_event; |
| 1442 | |
| 1443 | FILL_ARRAY(runtime_event, comm, event, raw->data); |
| 1444 | FILL_FIELD(runtime_event, pid, event, raw->data); |
| 1445 | FILL_FIELD(runtime_event, runtime, event, raw->data); |
| 1446 | FILL_FIELD(runtime_event, vruntime, event, raw->data); |
| 1447 | |
| 1448 | trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread); |
| 1449 | } |
| 1450 | |
| 1451 | static void |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1452 | process_sched_fork_event(struct raw_event_sample *raw, |
| 1453 | struct event *event, |
| 1454 | int cpu __used, |
| 1455 | u64 timestamp __used, |
| 1456 | struct thread *thread __used) |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1457 | { |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1458 | struct trace_fork_event fork_event; |
| 1459 | |
| 1460 | FILL_COMMON_FIELDS(fork_event, event, raw->data); |
| 1461 | |
| 1462 | FILL_ARRAY(fork_event, parent_comm, event, raw->data); |
| 1463 | FILL_FIELD(fork_event, parent_pid, event, raw->data); |
| 1464 | FILL_ARRAY(fork_event, child_comm, event, raw->data); |
| 1465 | FILL_FIELD(fork_event, child_pid, event, raw->data); |
| 1466 | |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1467 | trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Frederic Weisbecker | 419ab0d | 2009-09-12 03:59:01 +0200 | [diff] [blame] | 1470 | static void |
| 1471 | process_sched_exit_event(struct event *event, |
| 1472 | int cpu __used, |
| 1473 | u64 timestamp __used, |
| 1474 | struct thread *thread __used) |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1475 | { |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1476 | if (verbose) |
| 1477 | printf("sched_exit event %p\n", event); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | static void |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1481 | process_raw_event(event_t *raw_event __used, void *more_data, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1482 | int cpu, u64 timestamp, struct thread *thread) |
| 1483 | { |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1484 | struct raw_event_sample *raw = more_data; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1485 | struct event *event; |
| 1486 | int type; |
| 1487 | |
| 1488 | type = trace_parse_common_type(raw->data); |
| 1489 | event = trace_find_event(type); |
| 1490 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1491 | if (!strcmp(event->name, "sched_switch")) |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1492 | process_sched_switch_event(raw, event, cpu, timestamp, thread); |
mingo | 39aeb52 | 2009-09-14 20:04:48 +0200 | [diff] [blame^] | 1493 | if (!strcmp(event->name, "sched_stat_runtime")) |
| 1494 | process_sched_runtime_event(raw, event, cpu, timestamp, thread); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1495 | if (!strcmp(event->name, "sched_wakeup")) |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1496 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1497 | if (!strcmp(event->name, "sched_wakeup_new")) |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1498 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1499 | if (!strcmp(event->name, "sched_process_fork")) |
Frederic Weisbecker | 4653881 | 2009-09-12 02:43:45 +0200 | [diff] [blame] | 1500 | process_sched_fork_event(raw, event, cpu, timestamp, thread); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1501 | if (!strcmp(event->name, "sched_process_exit")) |
| 1502 | process_sched_exit_event(event, cpu, timestamp, thread); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1505 | static int |
| 1506 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
| 1507 | { |
| 1508 | char level; |
| 1509 | int show = 0; |
| 1510 | struct dso *dso = NULL; |
| 1511 | struct thread *thread; |
| 1512 | u64 ip = event->ip.ip; |
| 1513 | u64 timestamp = -1; |
| 1514 | u32 cpu = -1; |
| 1515 | u64 period = 1; |
| 1516 | void *more_data = event->ip.__more_data; |
| 1517 | int cpumode; |
| 1518 | |
| 1519 | thread = threads__findnew(event->ip.pid, &threads, &last_match); |
| 1520 | |
| 1521 | if (sample_type & PERF_SAMPLE_TIME) { |
| 1522 | timestamp = *(u64 *)more_data; |
| 1523 | more_data += sizeof(u64); |
| 1524 | } |
| 1525 | |
| 1526 | if (sample_type & PERF_SAMPLE_CPU) { |
| 1527 | cpu = *(u32 *)more_data; |
| 1528 | more_data += sizeof(u32); |
| 1529 | more_data += sizeof(u32); /* reserved */ |
| 1530 | } |
| 1531 | |
| 1532 | if (sample_type & PERF_SAMPLE_PERIOD) { |
| 1533 | period = *(u64 *)more_data; |
| 1534 | more_data += sizeof(u64); |
| 1535 | } |
| 1536 | |
| 1537 | dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", |
| 1538 | (void *)(offset + head), |
| 1539 | (void *)(long)(event->header.size), |
| 1540 | event->header.misc, |
| 1541 | event->ip.pid, event->ip.tid, |
| 1542 | (void *)(long)ip, |
| 1543 | (long long)period); |
| 1544 | |
| 1545 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 1546 | |
| 1547 | if (thread == NULL) { |
| 1548 | eprintf("problem processing %d event, skipping it.\n", |
| 1549 | event->header.type); |
| 1550 | return -1; |
| 1551 | } |
| 1552 | |
| 1553 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; |
| 1554 | |
| 1555 | if (cpumode == PERF_EVENT_MISC_KERNEL) { |
| 1556 | show = SHOW_KERNEL; |
| 1557 | level = 'k'; |
| 1558 | |
| 1559 | dso = kernel_dso; |
| 1560 | |
| 1561 | dump_printf(" ...... dso: %s\n", dso->name); |
| 1562 | |
| 1563 | } else if (cpumode == PERF_EVENT_MISC_USER) { |
| 1564 | |
| 1565 | show = SHOW_USER; |
| 1566 | level = '.'; |
| 1567 | |
| 1568 | } else { |
| 1569 | show = SHOW_HV; |
| 1570 | level = 'H'; |
| 1571 | |
| 1572 | dso = hypervisor_dso; |
| 1573 | |
| 1574 | dump_printf(" ...... dso: [hypervisor]\n"); |
| 1575 | } |
| 1576 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1577 | if (sample_type & PERF_SAMPLE_RAW) |
| 1578 | process_raw_event(event, more_data, cpu, timestamp, thread); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | static int |
| 1584 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 1585 | { |
| 1586 | trace_event(event); |
| 1587 | |
| 1588 | switch (event->header.type) { |
| 1589 | case PERF_EVENT_MMAP ... PERF_EVENT_LOST: |
| 1590 | return 0; |
| 1591 | |
| 1592 | case PERF_EVENT_COMM: |
| 1593 | return process_comm_event(event, offset, head); |
| 1594 | |
| 1595 | case PERF_EVENT_EXIT ... PERF_EVENT_READ: |
| 1596 | return 0; |
| 1597 | |
| 1598 | case PERF_EVENT_SAMPLE: |
| 1599 | return process_sample_event(event, offset, head); |
| 1600 | |
| 1601 | case PERF_EVENT_MAX: |
| 1602 | default: |
| 1603 | return -1; |
| 1604 | } |
| 1605 | |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1609 | static int read_events(void) |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1610 | { |
| 1611 | int ret, rc = EXIT_FAILURE; |
| 1612 | unsigned long offset = 0; |
| 1613 | unsigned long head = 0; |
| 1614 | struct stat perf_stat; |
| 1615 | event_t *event; |
| 1616 | uint32_t size; |
| 1617 | char *buf; |
| 1618 | |
| 1619 | trace_report(); |
| 1620 | register_idle_thread(&threads, &last_match); |
| 1621 | |
| 1622 | input = open(input_name, O_RDONLY); |
| 1623 | if (input < 0) { |
| 1624 | perror("failed to open file"); |
| 1625 | exit(-1); |
| 1626 | } |
| 1627 | |
| 1628 | ret = fstat(input, &perf_stat); |
| 1629 | if (ret < 0) { |
| 1630 | perror("failed to stat file"); |
| 1631 | exit(-1); |
| 1632 | } |
| 1633 | |
| 1634 | if (!perf_stat.st_size) { |
| 1635 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 1636 | exit(0); |
| 1637 | } |
| 1638 | header = perf_header__read(input); |
| 1639 | head = header->data_offset; |
| 1640 | sample_type = perf_header__sample_type(header); |
| 1641 | |
| 1642 | if (!(sample_type & PERF_SAMPLE_RAW)) |
| 1643 | die("No trace sample to read. Did you call perf record " |
| 1644 | "without -R?"); |
| 1645 | |
| 1646 | if (load_kernel() < 0) { |
| 1647 | perror("failed to load kernel symbols"); |
| 1648 | return EXIT_FAILURE; |
| 1649 | } |
| 1650 | |
| 1651 | remap: |
| 1652 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 1653 | MAP_SHARED, input, offset); |
| 1654 | if (buf == MAP_FAILED) { |
| 1655 | perror("failed to mmap file"); |
| 1656 | exit(-1); |
| 1657 | } |
| 1658 | |
| 1659 | more: |
| 1660 | event = (event_t *)(buf + head); |
| 1661 | |
| 1662 | size = event->header.size; |
| 1663 | if (!size) |
| 1664 | size = 8; |
| 1665 | |
| 1666 | if (head + event->header.size >= page_size * mmap_window) { |
| 1667 | unsigned long shift = page_size * (head / page_size); |
| 1668 | int res; |
| 1669 | |
| 1670 | res = munmap(buf, page_size * mmap_window); |
| 1671 | assert(res == 0); |
| 1672 | |
| 1673 | offset += shift; |
| 1674 | head -= shift; |
| 1675 | goto remap; |
| 1676 | } |
| 1677 | |
| 1678 | size = event->header.size; |
| 1679 | |
| 1680 | |
| 1681 | if (!size || process_event(event, offset, head) < 0) { |
| 1682 | |
| 1683 | /* |
| 1684 | * assume we lost track of the stream, check alignment, and |
| 1685 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1686 | */ |
| 1687 | |
| 1688 | if (unlikely(head & 7)) |
| 1689 | head &= ~7ULL; |
| 1690 | |
| 1691 | size = 8; |
| 1692 | } |
| 1693 | |
| 1694 | head += size; |
| 1695 | |
| 1696 | if (offset + head < (unsigned long)perf_stat.st_size) |
| 1697 | goto more; |
| 1698 | |
| 1699 | rc = EXIT_SUCCESS; |
| 1700 | close(input); |
| 1701 | |
| 1702 | return rc; |
| 1703 | } |
| 1704 | |
Ingo Molnar | 46f392c | 2009-09-12 10:08:34 +0200 | [diff] [blame] | 1705 | static const char * const sched_usage[] = { |
Ingo Molnar | c13f0d3 | 2009-09-13 16:51:04 +0200 | [diff] [blame] | 1706 | "perf sched [<options>] {record|latency|replay|trace}", |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1707 | NULL |
| 1708 | }; |
| 1709 | |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1710 | static const struct option sched_options[] = { |
| 1711 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1712 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1713 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1714 | "dump raw trace in ASCII"), |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1715 | OPT_END() |
| 1716 | }; |
| 1717 | |
| 1718 | static const char * const latency_usage[] = { |
| 1719 | "perf sched latency [<options>]", |
| 1720 | NULL |
| 1721 | }; |
| 1722 | |
| 1723 | static const struct option latency_options[] = { |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1724 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
| 1725 | "sort by key(s): runtime, switch, avg, max"), |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1726 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1727 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1728 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1729 | "dump raw trace in ASCII"), |
| 1730 | OPT_END() |
| 1731 | }; |
| 1732 | |
| 1733 | static const char * const replay_usage[] = { |
| 1734 | "perf sched replay [<options>]", |
| 1735 | NULL |
| 1736 | }; |
| 1737 | |
| 1738 | static const struct option replay_options[] = { |
| 1739 | OPT_INTEGER('r', "repeat", &replay_repeat, |
| 1740 | "repeat the workload replay N times (-1: infinite)"), |
| 1741 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1742 | "be more verbose (show symbol address, etc)"), |
| 1743 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1744 | "dump raw trace in ASCII"), |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1745 | OPT_END() |
| 1746 | }; |
| 1747 | |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1748 | static void setup_sorting(void) |
| 1749 | { |
| 1750 | char *tmp, *tok, *str = strdup(sort_order); |
| 1751 | |
| 1752 | for (tok = strtok_r(str, ", ", &tmp); |
| 1753 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 1754 | if (sort_dimension__add(tok, &sort_list) < 0) { |
| 1755 | error("Unknown --sort key: `%s'", tok); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1756 | usage_with_options(latency_usage, latency_options); |
Frederic Weisbecker | daa1d7a | 2009-09-13 03:36:29 +0200 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | free(str); |
| 1761 | |
| 1762 | sort_dimension__add((char *)"pid", &cmp_pid); |
| 1763 | } |
| 1764 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1765 | static const char *record_args[] = { |
| 1766 | "record", |
| 1767 | "-a", |
| 1768 | "-R", |
Frederic Weisbecker | d130252 | 2009-09-14 08:57:15 +0200 | [diff] [blame] | 1769 | "-M", |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1770 | "-f", |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1771 | "-c", "1", |
| 1772 | "-e", "sched:sched_switch:r", |
| 1773 | "-e", "sched:sched_stat_wait:r", |
| 1774 | "-e", "sched:sched_stat_sleep:r", |
| 1775 | "-e", "sched:sched_stat_iowait:r", |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 1776 | "-e", "sched:sched_stat_runtime:r", |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1777 | "-e", "sched:sched_process_exit:r", |
| 1778 | "-e", "sched:sched_process_fork:r", |
| 1779 | "-e", "sched:sched_wakeup:r", |
| 1780 | "-e", "sched:sched_migrate_task:r", |
| 1781 | }; |
| 1782 | |
| 1783 | static int __cmd_record(int argc, const char **argv) |
| 1784 | { |
| 1785 | unsigned int rec_argc, i, j; |
| 1786 | const char **rec_argv; |
| 1787 | |
| 1788 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
| 1789 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 1790 | |
| 1791 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
| 1792 | rec_argv[i] = strdup(record_args[i]); |
| 1793 | |
| 1794 | for (j = 1; j < (unsigned int)argc; j++, i++) |
| 1795 | rec_argv[i] = argv[j]; |
| 1796 | |
| 1797 | BUG_ON(i != rec_argc); |
| 1798 | |
| 1799 | return cmd_record(i, rec_argv, NULL); |
| 1800 | } |
| 1801 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1802 | int cmd_sched(int argc, const char **argv, const char *prefix __used) |
| 1803 | { |
| 1804 | symbol__init(); |
| 1805 | page_size = getpagesize(); |
| 1806 | |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1807 | argc = parse_options(argc, argv, sched_options, sched_usage, |
| 1808 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1809 | if (!argc) |
| 1810 | usage_with_options(sched_usage, sched_options); |
| 1811 | |
Ingo Molnar | 1fc35b2 | 2009-09-13 09:44:29 +0200 | [diff] [blame] | 1812 | if (!strncmp(argv[0], "rec", 3)) { |
| 1813 | return __cmd_record(argc, argv); |
| 1814 | } else if (!strncmp(argv[0], "lat", 3)) { |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1815 | trace_handler = &lat_ops; |
| 1816 | if (argc > 1) { |
| 1817 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); |
| 1818 | if (argc) |
| 1819 | usage_with_options(latency_usage, latency_options); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1820 | } |
Ingo Molnar | b5fae12 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1821 | setup_sorting(); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1822 | __cmd_lat(); |
| 1823 | } else if (!strncmp(argv[0], "rep", 3)) { |
| 1824 | trace_handler = &replay_ops; |
| 1825 | if (argc) { |
| 1826 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); |
| 1827 | if (argc) |
| 1828 | usage_with_options(replay_usage, replay_options); |
| 1829 | } |
| 1830 | __cmd_replay(); |
Ingo Molnar | c13f0d3 | 2009-09-13 16:51:04 +0200 | [diff] [blame] | 1831 | } else if (!strcmp(argv[0], "trace")) { |
| 1832 | /* |
| 1833 | * Aliased to 'perf trace' for now: |
| 1834 | */ |
| 1835 | return cmd_trace(argc, argv, prefix); |
Ingo Molnar | f2858d8 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1836 | } else { |
| 1837 | usage_with_options(sched_usage, sched_options); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1840 | return 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1841 | } |