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