Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | |
| 3 | #include "util/util.h" |
| 4 | #include "util/cache.h" |
| 5 | #include "util/symbol.h" |
| 6 | #include "util/thread.h" |
| 7 | #include "util/header.h" |
| 8 | |
| 9 | #include "util/parse-options.h" |
| 10 | |
| 11 | #include "perf.h" |
| 12 | #include "util/debug.h" |
| 13 | |
| 14 | #include "util/trace-event.h" |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 15 | #include <sys/types.h> |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 16 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 17 | static char const *input_name = "perf.data"; |
| 18 | static int input; |
| 19 | static unsigned long page_size; |
| 20 | static unsigned long mmap_window = 32; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 21 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 22 | static unsigned long total_comm = 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 23 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 24 | static struct rb_root threads; |
| 25 | static struct thread *last_match; |
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 struct perf_header *header; |
| 28 | static u64 sample_type; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 29 | |
| 30 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Scheduler benchmarks |
| 33 | */ |
| 34 | #include <sys/resource.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <sys/prctl.h> |
| 39 | |
| 40 | #include <linux/unistd.h> |
| 41 | |
| 42 | #include <semaphore.h> |
| 43 | #include <pthread.h> |
| 44 | #include <signal.h> |
| 45 | #include <values.h> |
| 46 | #include <string.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <assert.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <time.h> |
| 52 | #include <math.h> |
| 53 | |
| 54 | #include <stdio.h> |
| 55 | |
| 56 | #define PR_SET_NAME 15 /* Set process name */ |
| 57 | |
| 58 | #define BUG_ON(x) assert(!(x)) |
| 59 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 60 | #define DEBUG 0 |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 61 | |
| 62 | typedef unsigned long long nsec_t; |
| 63 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 64 | static nsec_t run_measurement_overhead; |
| 65 | static nsec_t sleep_measurement_overhead; |
| 66 | |
| 67 | static nsec_t get_nsecs(void) |
| 68 | { |
| 69 | struct timespec ts; |
| 70 | |
| 71 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 72 | |
| 73 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 74 | } |
| 75 | |
| 76 | static void burn_nsecs(nsec_t nsecs) |
| 77 | { |
| 78 | nsec_t T0 = get_nsecs(), T1; |
| 79 | |
| 80 | do { |
| 81 | T1 = get_nsecs(); |
| 82 | } while (T1 + run_measurement_overhead < T0 + nsecs); |
| 83 | } |
| 84 | |
| 85 | static void sleep_nsecs(nsec_t nsecs) |
| 86 | { |
| 87 | struct timespec ts; |
| 88 | |
| 89 | ts.tv_nsec = nsecs % 999999999; |
| 90 | ts.tv_sec = nsecs / 999999999; |
| 91 | |
| 92 | nanosleep(&ts, NULL); |
| 93 | } |
| 94 | |
| 95 | static void calibrate_run_measurement_overhead(void) |
| 96 | { |
| 97 | nsec_t T0, T1, delta, min_delta = 1000000000ULL; |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < 10; i++) { |
| 101 | T0 = get_nsecs(); |
| 102 | burn_nsecs(0); |
| 103 | T1 = get_nsecs(); |
| 104 | delta = T1-T0; |
| 105 | min_delta = min(min_delta, delta); |
| 106 | } |
| 107 | run_measurement_overhead = min_delta; |
| 108 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 109 | printf("run measurement overhead: %Ld nsecs\n", min_delta); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void calibrate_sleep_measurement_overhead(void) |
| 113 | { |
| 114 | nsec_t T0, T1, delta, min_delta = 1000000000ULL; |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < 10; i++) { |
| 118 | T0 = get_nsecs(); |
| 119 | sleep_nsecs(10000); |
| 120 | T1 = get_nsecs(); |
| 121 | delta = T1-T0; |
| 122 | min_delta = min(min_delta, delta); |
| 123 | } |
| 124 | min_delta -= 10000; |
| 125 | sleep_measurement_overhead = min_delta; |
| 126 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 127 | printf("sleep measurement overhead: %Ld nsecs\n", min_delta); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #define COMM_LEN 20 |
| 131 | #define SYM_LEN 129 |
| 132 | |
| 133 | #define MAX_PID 65536 |
| 134 | |
| 135 | static unsigned long nr_tasks; |
| 136 | |
| 137 | struct sched_event; |
| 138 | |
| 139 | struct task_desc { |
| 140 | unsigned long nr; |
| 141 | unsigned long pid; |
| 142 | char comm[COMM_LEN]; |
| 143 | |
| 144 | unsigned long nr_events; |
| 145 | unsigned long curr_event; |
| 146 | struct sched_event **events; |
| 147 | |
| 148 | pthread_t thread; |
| 149 | sem_t sleep_sem; |
| 150 | |
| 151 | sem_t ready_for_work; |
| 152 | sem_t work_done_sem; |
| 153 | |
| 154 | nsec_t cpu_usage; |
| 155 | }; |
| 156 | |
| 157 | enum sched_event_type { |
| 158 | SCHED_EVENT_RUN, |
| 159 | SCHED_EVENT_SLEEP, |
| 160 | SCHED_EVENT_WAKEUP, |
| 161 | }; |
| 162 | |
| 163 | struct sched_event { |
| 164 | enum sched_event_type type; |
| 165 | nsec_t timestamp; |
| 166 | nsec_t duration; |
| 167 | unsigned long nr; |
| 168 | int specific_wait; |
| 169 | sem_t *wait_sem; |
| 170 | struct task_desc *wakee; |
| 171 | }; |
| 172 | |
| 173 | static struct task_desc *pid_to_task[MAX_PID]; |
| 174 | |
| 175 | static struct task_desc **tasks; |
| 176 | |
| 177 | static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 178 | static nsec_t start_time; |
| 179 | |
| 180 | static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 181 | |
| 182 | static unsigned long nr_run_events; |
| 183 | static unsigned long nr_sleep_events; |
| 184 | static unsigned long nr_wakeup_events; |
| 185 | |
| 186 | static unsigned long nr_sleep_corrections; |
| 187 | static unsigned long nr_run_events_optimized; |
| 188 | |
| 189 | static struct sched_event * |
| 190 | get_new_event(struct task_desc *task, nsec_t timestamp) |
| 191 | { |
| 192 | struct sched_event *event = calloc(1, sizeof(*event)); |
| 193 | unsigned long idx = task->nr_events; |
| 194 | size_t size; |
| 195 | |
| 196 | event->timestamp = timestamp; |
| 197 | event->nr = idx; |
| 198 | |
| 199 | task->nr_events++; |
| 200 | size = sizeof(struct sched_event *) * task->nr_events; |
| 201 | task->events = realloc(task->events, size); |
| 202 | BUG_ON(!task->events); |
| 203 | |
| 204 | task->events[idx] = event; |
| 205 | |
| 206 | return event; |
| 207 | } |
| 208 | |
| 209 | static struct sched_event *last_event(struct task_desc *task) |
| 210 | { |
| 211 | if (!task->nr_events) |
| 212 | return NULL; |
| 213 | |
| 214 | return task->events[task->nr_events - 1]; |
| 215 | } |
| 216 | |
| 217 | static void |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 218 | add_sched_event_run(struct task_desc *task, nsec_t timestamp, u64 duration) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 219 | { |
| 220 | struct sched_event *event, *curr_event = last_event(task); |
| 221 | |
| 222 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 223 | * optimize an existing RUN event by merging this one |
| 224 | * to it: |
| 225 | */ |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 226 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
| 227 | nr_run_events_optimized++; |
| 228 | curr_event->duration += duration; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | event = get_new_event(task, timestamp); |
| 233 | |
| 234 | event->type = SCHED_EVENT_RUN; |
| 235 | event->duration = duration; |
| 236 | |
| 237 | nr_run_events++; |
| 238 | } |
| 239 | |
| 240 | static unsigned long targetless_wakeups; |
| 241 | static unsigned long multitarget_wakeups; |
| 242 | |
| 243 | static void |
| 244 | add_sched_event_wakeup(struct task_desc *task, nsec_t timestamp, |
| 245 | struct task_desc *wakee) |
| 246 | { |
| 247 | struct sched_event *event, *wakee_event; |
| 248 | |
| 249 | event = get_new_event(task, timestamp); |
| 250 | event->type = SCHED_EVENT_WAKEUP; |
| 251 | event->wakee = wakee; |
| 252 | |
| 253 | wakee_event = last_event(wakee); |
| 254 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { |
| 255 | targetless_wakeups++; |
| 256 | return; |
| 257 | } |
| 258 | if (wakee_event->wait_sem) { |
| 259 | multitarget_wakeups++; |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem)); |
| 264 | sem_init(wakee_event->wait_sem, 0, 0); |
| 265 | wakee_event->specific_wait = 1; |
| 266 | event->wait_sem = wakee_event->wait_sem; |
| 267 | |
| 268 | nr_wakeup_events++; |
| 269 | } |
| 270 | |
| 271 | static void |
| 272 | add_sched_event_sleep(struct task_desc *task, nsec_t timestamp, |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 273 | u64 task_state __used) |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 274 | { |
| 275 | struct sched_event *event = get_new_event(task, timestamp); |
| 276 | |
| 277 | event->type = SCHED_EVENT_SLEEP; |
| 278 | |
| 279 | nr_sleep_events++; |
| 280 | } |
| 281 | |
| 282 | static struct task_desc *register_pid(unsigned long pid, const char *comm) |
| 283 | { |
| 284 | struct task_desc *task; |
| 285 | |
| 286 | BUG_ON(pid >= MAX_PID); |
| 287 | |
| 288 | task = pid_to_task[pid]; |
| 289 | |
| 290 | if (task) |
| 291 | return task; |
| 292 | |
| 293 | task = calloc(1, sizeof(*task)); |
| 294 | task->pid = pid; |
| 295 | task->nr = nr_tasks; |
| 296 | strcpy(task->comm, comm); |
| 297 | /* |
| 298 | * every task starts in sleeping state - this gets ignored |
| 299 | * if there's no wakeup pointing to this sleep state: |
| 300 | */ |
| 301 | add_sched_event_sleep(task, 0, 0); |
| 302 | |
| 303 | pid_to_task[pid] = task; |
| 304 | nr_tasks++; |
| 305 | tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *)); |
| 306 | BUG_ON(!tasks); |
| 307 | tasks[task->nr] = task; |
| 308 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 309 | if (verbose) |
| 310 | printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 311 | |
| 312 | return task; |
| 313 | } |
| 314 | |
| 315 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 316 | static void print_task_traces(void) |
| 317 | { |
| 318 | struct task_desc *task; |
| 319 | unsigned long i; |
| 320 | |
| 321 | for (i = 0; i < nr_tasks; i++) { |
| 322 | task = tasks[i]; |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 323 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 324 | task->nr, task->comm, task->pid, task->nr_events); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static void add_cross_task_wakeups(void) |
| 329 | { |
| 330 | struct task_desc *task1, *task2; |
| 331 | unsigned long i, j; |
| 332 | |
| 333 | for (i = 0; i < nr_tasks; i++) { |
| 334 | task1 = tasks[i]; |
| 335 | j = i + 1; |
| 336 | if (j == nr_tasks) |
| 337 | j = 0; |
| 338 | task2 = tasks[j]; |
| 339 | add_sched_event_wakeup(task1, 0, task2); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static void |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 344 | 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] | 345 | { |
| 346 | int ret = 0; |
| 347 | nsec_t now; |
| 348 | long long delta; |
| 349 | |
| 350 | now = get_nsecs(); |
| 351 | delta = start_time + event->timestamp - now; |
| 352 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 353 | switch (event->type) { |
| 354 | case SCHED_EVENT_RUN: |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 355 | burn_nsecs(event->duration); |
| 356 | break; |
| 357 | case SCHED_EVENT_SLEEP: |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 358 | if (event->wait_sem) |
| 359 | ret = sem_wait(event->wait_sem); |
| 360 | BUG_ON(ret); |
| 361 | break; |
| 362 | case SCHED_EVENT_WAKEUP: |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 363 | if (event->wait_sem) |
| 364 | ret = sem_post(event->wait_sem); |
| 365 | BUG_ON(ret); |
| 366 | break; |
| 367 | default: |
| 368 | BUG_ON(1); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static nsec_t get_cpu_usage_nsec_parent(void) |
| 373 | { |
| 374 | struct rusage ru; |
| 375 | nsec_t sum; |
| 376 | int err; |
| 377 | |
| 378 | err = getrusage(RUSAGE_SELF, &ru); |
| 379 | BUG_ON(err); |
| 380 | |
| 381 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; |
| 382 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; |
| 383 | |
| 384 | return sum; |
| 385 | } |
| 386 | |
| 387 | static nsec_t get_cpu_usage_nsec_self(void) |
| 388 | { |
| 389 | char filename [] = "/proc/1234567890/sched"; |
| 390 | unsigned long msecs, nsecs; |
| 391 | char *line = NULL; |
| 392 | nsec_t total = 0; |
| 393 | size_t len = 0; |
| 394 | ssize_t chars; |
| 395 | FILE *file; |
| 396 | int ret; |
| 397 | |
| 398 | sprintf(filename, "/proc/%d/sched", getpid()); |
| 399 | file = fopen(filename, "r"); |
| 400 | BUG_ON(!file); |
| 401 | |
| 402 | while ((chars = getline(&line, &len, file)) != -1) { |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 403 | ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n", |
| 404 | &msecs, &nsecs); |
| 405 | if (ret == 2) { |
| 406 | total = msecs*1e6 + nsecs; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | } |
| 410 | if (line) |
| 411 | free(line); |
| 412 | fclose(file); |
| 413 | |
| 414 | return total; |
| 415 | } |
| 416 | |
| 417 | static void *thread_func(void *ctx) |
| 418 | { |
| 419 | struct task_desc *this_task = ctx; |
| 420 | nsec_t cpu_usage_0, cpu_usage_1; |
| 421 | unsigned long i, ret; |
| 422 | char comm2[22]; |
| 423 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 424 | sprintf(comm2, ":%s", this_task->comm); |
| 425 | prctl(PR_SET_NAME, comm2); |
| 426 | |
| 427 | again: |
| 428 | ret = sem_post(&this_task->ready_for_work); |
| 429 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 430 | ret = pthread_mutex_lock(&start_work_mutex); |
| 431 | BUG_ON(ret); |
| 432 | ret = pthread_mutex_unlock(&start_work_mutex); |
| 433 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 434 | |
| 435 | cpu_usage_0 = get_cpu_usage_nsec_self(); |
| 436 | |
| 437 | for (i = 0; i < this_task->nr_events; i++) { |
| 438 | this_task->curr_event = i; |
| 439 | process_sched_event(this_task, this_task->events[i]); |
| 440 | } |
| 441 | |
| 442 | cpu_usage_1 = get_cpu_usage_nsec_self(); |
| 443 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 444 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 445 | ret = sem_post(&this_task->work_done_sem); |
| 446 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 447 | |
| 448 | ret = pthread_mutex_lock(&work_done_wait_mutex); |
| 449 | BUG_ON(ret); |
| 450 | ret = pthread_mutex_unlock(&work_done_wait_mutex); |
| 451 | BUG_ON(ret); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 452 | |
| 453 | goto again; |
| 454 | } |
| 455 | |
| 456 | static void create_tasks(void) |
| 457 | { |
| 458 | struct task_desc *task; |
| 459 | pthread_attr_t attr; |
| 460 | unsigned long i; |
| 461 | int err; |
| 462 | |
| 463 | err = pthread_attr_init(&attr); |
| 464 | BUG_ON(err); |
| 465 | err = pthread_attr_setstacksize(&attr, (size_t)(16*1024)); |
| 466 | BUG_ON(err); |
| 467 | err = pthread_mutex_lock(&start_work_mutex); |
| 468 | BUG_ON(err); |
| 469 | err = pthread_mutex_lock(&work_done_wait_mutex); |
| 470 | BUG_ON(err); |
| 471 | for (i = 0; i < nr_tasks; i++) { |
| 472 | task = tasks[i]; |
| 473 | sem_init(&task->sleep_sem, 0, 0); |
| 474 | sem_init(&task->ready_for_work, 0, 0); |
| 475 | sem_init(&task->work_done_sem, 0, 0); |
| 476 | task->curr_event = 0; |
| 477 | err = pthread_create(&task->thread, &attr, thread_func, task); |
| 478 | BUG_ON(err); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static nsec_t cpu_usage; |
| 483 | static nsec_t runavg_cpu_usage; |
| 484 | static nsec_t parent_cpu_usage; |
| 485 | static nsec_t runavg_parent_cpu_usage; |
| 486 | |
| 487 | static void wait_for_tasks(void) |
| 488 | { |
| 489 | nsec_t cpu_usage_0, cpu_usage_1; |
| 490 | struct task_desc *task; |
| 491 | unsigned long i, ret; |
| 492 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 493 | start_time = get_nsecs(); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 494 | cpu_usage = 0; |
| 495 | pthread_mutex_unlock(&work_done_wait_mutex); |
| 496 | |
| 497 | for (i = 0; i < nr_tasks; i++) { |
| 498 | task = tasks[i]; |
| 499 | ret = sem_wait(&task->ready_for_work); |
| 500 | BUG_ON(ret); |
| 501 | sem_init(&task->ready_for_work, 0, 0); |
| 502 | } |
| 503 | ret = pthread_mutex_lock(&work_done_wait_mutex); |
| 504 | BUG_ON(ret); |
| 505 | |
| 506 | cpu_usage_0 = get_cpu_usage_nsec_parent(); |
| 507 | |
| 508 | pthread_mutex_unlock(&start_work_mutex); |
| 509 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 510 | for (i = 0; i < nr_tasks; i++) { |
| 511 | task = tasks[i]; |
| 512 | ret = sem_wait(&task->work_done_sem); |
| 513 | BUG_ON(ret); |
| 514 | sem_init(&task->work_done_sem, 0, 0); |
| 515 | cpu_usage += task->cpu_usage; |
| 516 | task->cpu_usage = 0; |
| 517 | } |
| 518 | |
| 519 | cpu_usage_1 = get_cpu_usage_nsec_parent(); |
| 520 | if (!runavg_cpu_usage) |
| 521 | runavg_cpu_usage = cpu_usage; |
| 522 | runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10; |
| 523 | |
| 524 | parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
| 525 | if (!runavg_parent_cpu_usage) |
| 526 | runavg_parent_cpu_usage = parent_cpu_usage; |
| 527 | runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 + |
| 528 | parent_cpu_usage)/10; |
| 529 | |
| 530 | ret = pthread_mutex_lock(&start_work_mutex); |
| 531 | BUG_ON(ret); |
| 532 | |
| 533 | for (i = 0; i < nr_tasks; i++) { |
| 534 | task = tasks[i]; |
| 535 | sem_init(&task->sleep_sem, 0, 0); |
| 536 | task->curr_event = 0; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | static int __cmd_sched(void); |
| 541 | |
| 542 | static void parse_trace(void) |
| 543 | { |
| 544 | __cmd_sched(); |
| 545 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 546 | printf("nr_run_events: %ld\n", nr_run_events); |
| 547 | printf("nr_sleep_events: %ld\n", nr_sleep_events); |
| 548 | printf("nr_wakeup_events: %ld\n", nr_wakeup_events); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 549 | |
| 550 | if (targetless_wakeups) |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 551 | printf("target-less wakeups: %ld\n", targetless_wakeups); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 552 | if (multitarget_wakeups) |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 553 | printf("multi-target wakeups: %ld\n", multitarget_wakeups); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 554 | if (nr_run_events_optimized) |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 555 | printf("run events optimized: %ld\n", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 556 | nr_run_events_optimized); |
| 557 | } |
| 558 | |
| 559 | static unsigned long nr_runs; |
| 560 | static nsec_t sum_runtime; |
| 561 | static nsec_t sum_fluct; |
| 562 | static nsec_t run_avg; |
| 563 | |
| 564 | static void run_one_test(void) |
| 565 | { |
| 566 | nsec_t T0, T1, delta, avg_delta, fluct, std_dev; |
| 567 | |
| 568 | T0 = get_nsecs(); |
| 569 | wait_for_tasks(); |
| 570 | T1 = get_nsecs(); |
| 571 | |
| 572 | delta = T1 - T0; |
| 573 | sum_runtime += delta; |
| 574 | nr_runs++; |
| 575 | |
| 576 | avg_delta = sum_runtime / nr_runs; |
| 577 | if (delta < avg_delta) |
| 578 | fluct = avg_delta - delta; |
| 579 | else |
| 580 | fluct = delta - avg_delta; |
| 581 | sum_fluct += fluct; |
| 582 | std_dev = sum_fluct / nr_runs / sqrt(nr_runs); |
| 583 | if (!run_avg) |
| 584 | run_avg = delta; |
| 585 | run_avg = (run_avg*9 + delta)/10; |
| 586 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 587 | printf("#%-3ld: %0.3f, ", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 588 | nr_runs, (double)delta/1000000.0); |
| 589 | |
| 590 | #if 0 |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 591 | printf("%0.2f +- %0.2f, ", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 592 | (double)avg_delta/1e6, (double)std_dev/1e6); |
| 593 | #endif |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 594 | printf("ravg: %0.2f, ", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 595 | (double)run_avg/1e6); |
| 596 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 597 | printf("cpu: %0.2f / %0.2f", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 598 | (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6); |
| 599 | |
| 600 | #if 0 |
| 601 | /* |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 602 | * rusage statistics done by the parent, these are less |
| 603 | * accurate than the sum_exec_runtime based statistics: |
| 604 | */ |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 605 | printf(" [%0.2f / %0.2f]", |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 606 | (double)parent_cpu_usage/1e6, |
| 607 | (double)runavg_parent_cpu_usage/1e6); |
| 608 | #endif |
| 609 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 610 | printf("\n"); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 611 | |
| 612 | if (nr_sleep_corrections) |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 613 | printf(" (%ld sleep corrections)\n", nr_sleep_corrections); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 614 | nr_sleep_corrections = 0; |
| 615 | } |
| 616 | |
| 617 | static void test_calibrations(void) |
| 618 | { |
| 619 | nsec_t T0, T1; |
| 620 | |
| 621 | T0 = get_nsecs(); |
| 622 | burn_nsecs(1e6); |
| 623 | T1 = get_nsecs(); |
| 624 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 625 | printf("the run test took %Ld nsecs\n", T1-T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 626 | |
| 627 | T0 = get_nsecs(); |
| 628 | sleep_nsecs(1e6); |
| 629 | T1 = get_nsecs(); |
| 630 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 631 | printf("the sleep test took %Ld nsecs\n", T1-T0); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 634 | static int |
| 635 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 636 | { |
| 637 | struct thread *thread; |
| 638 | |
| 639 | thread = threads__findnew(event->comm.pid, &threads, &last_match); |
| 640 | |
| 641 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 642 | (void *)(offset + head), |
| 643 | (void *)(long)(event->header.size), |
| 644 | event->comm.comm, event->comm.pid); |
| 645 | |
| 646 | if (thread == NULL || |
| 647 | thread__set_comm(thread, event->comm.comm)) { |
| 648 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 649 | return -1; |
| 650 | } |
| 651 | total_comm++; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 656 | struct trace_wakeup_event { |
| 657 | u32 size; |
| 658 | |
| 659 | u16 common_type; |
| 660 | u8 common_flags; |
| 661 | u8 common_preempt_count; |
| 662 | u32 common_pid; |
| 663 | u32 common_tgid; |
| 664 | |
| 665 | char comm[16]; |
| 666 | u32 pid; |
| 667 | |
| 668 | u32 prio; |
| 669 | u32 success; |
| 670 | u32 cpu; |
| 671 | }; |
| 672 | |
| 673 | static void |
| 674 | process_sched_wakeup_event(struct trace_wakeup_event *wakeup_event, struct event *event, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 675 | int cpu __used, u64 timestamp __used, struct thread *thread __used) |
| 676 | { |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 677 | struct task_desc *waker, *wakee; |
| 678 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 679 | if (verbose) { |
| 680 | printf("sched_wakeup event %p\n", event); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 681 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 682 | printf(" ... pid %d woke up %s/%d\n", |
| 683 | wakeup_event->common_pid, |
| 684 | wakeup_event->comm, |
| 685 | wakeup_event->pid); |
| 686 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 687 | |
| 688 | waker = register_pid(wakeup_event->common_pid, "<unknown>"); |
| 689 | wakee = register_pid(wakeup_event->pid, wakeup_event->comm); |
| 690 | |
| 691 | add_sched_event_wakeup(waker, timestamp, wakee); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 694 | struct trace_switch_event { |
| 695 | u32 size; |
| 696 | |
| 697 | u16 common_type; |
| 698 | u8 common_flags; |
| 699 | u8 common_preempt_count; |
| 700 | u32 common_pid; |
| 701 | u32 common_tgid; |
| 702 | |
| 703 | char prev_comm[16]; |
| 704 | u32 prev_pid; |
| 705 | u32 prev_prio; |
| 706 | u64 prev_state; |
| 707 | char next_comm[16]; |
| 708 | u32 next_pid; |
| 709 | u32 next_prio; |
| 710 | }; |
| 711 | |
| 712 | #define MAX_CPUS 4096 |
| 713 | |
| 714 | unsigned long cpu_last_switched[MAX_CPUS]; |
| 715 | |
| 716 | static void |
| 717 | process_sched_switch_event(struct trace_switch_event *switch_event, struct event *event, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 718 | int cpu __used, u64 timestamp __used, struct thread *thread __used) |
| 719 | { |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 720 | struct task_desc *prev, *next; |
| 721 | u64 timestamp0; |
| 722 | s64 delta; |
| 723 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 724 | if (verbose) |
| 725 | printf("sched_switch event %p\n", event); |
| 726 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 727 | if (cpu >= MAX_CPUS || cpu < 0) |
| 728 | return; |
| 729 | |
| 730 | timestamp0 = cpu_last_switched[cpu]; |
| 731 | if (timestamp0) |
| 732 | delta = timestamp - timestamp0; |
| 733 | else |
| 734 | delta = 0; |
| 735 | |
| 736 | if (delta < 0) |
| 737 | die("hm, delta: %Ld < 0 ?\n", delta); |
| 738 | |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 739 | if (verbose) { |
| 740 | printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n", |
| 741 | switch_event->prev_comm, switch_event->prev_pid, |
| 742 | switch_event->next_comm, switch_event->next_pid, |
| 743 | delta); |
| 744 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 745 | |
| 746 | prev = register_pid(switch_event->prev_pid, switch_event->prev_comm); |
| 747 | next = register_pid(switch_event->next_pid, switch_event->next_comm); |
| 748 | |
| 749 | cpu_last_switched[cpu] = timestamp; |
| 750 | |
| 751 | add_sched_event_run(prev, timestamp, delta); |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 752 | add_sched_event_sleep(prev, timestamp, switch_event->prev_state); |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | struct trace_fork_event { |
| 756 | u32 size; |
| 757 | |
| 758 | u16 common_type; |
| 759 | u8 common_flags; |
| 760 | u8 common_preempt_count; |
| 761 | u32 common_pid; |
| 762 | u32 common_tgid; |
| 763 | |
| 764 | char parent_comm[16]; |
| 765 | u32 parent_pid; |
| 766 | char child_comm[16]; |
| 767 | u32 child_pid; |
| 768 | }; |
| 769 | |
| 770 | static void |
| 771 | process_sched_fork_event(struct trace_fork_event *fork_event, struct event *event, |
| 772 | int cpu __used, u64 timestamp __used, struct thread *thread __used) |
| 773 | { |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 774 | if (verbose) { |
| 775 | printf("sched_fork event %p\n", event); |
| 776 | printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid); |
| 777 | printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid); |
| 778 | } |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 779 | register_pid(fork_event->parent_pid, fork_event->parent_comm); |
| 780 | register_pid(fork_event->child_pid, fork_event->child_comm); |
| 781 | } |
| 782 | |
| 783 | static void process_sched_exit_event(struct event *event, |
| 784 | int cpu __used, u64 timestamp __used, struct thread *thread __used) |
| 785 | { |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 786 | if (verbose) |
| 787 | printf("sched_exit event %p\n", event); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static void |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 791 | process_raw_event(event_t *raw_event __used, void *more_data, |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 792 | int cpu, u64 timestamp, struct thread *thread) |
| 793 | { |
| 794 | struct { |
| 795 | u32 size; |
| 796 | char data[0]; |
| 797 | } *raw = more_data; |
| 798 | struct event *event; |
| 799 | int type; |
| 800 | |
| 801 | type = trace_parse_common_type(raw->data); |
| 802 | event = trace_find_event(type); |
| 803 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 804 | if (!strcmp(event->name, "sched_switch")) |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 805 | process_sched_switch_event(more_data, event, cpu, timestamp, thread); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 806 | if (!strcmp(event->name, "sched_wakeup")) |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 807 | process_sched_wakeup_event(more_data, event, cpu, timestamp, thread); |
| 808 | if (!strcmp(event->name, "sched_wakeup_new")) |
| 809 | process_sched_wakeup_event(more_data, event, cpu, timestamp, thread); |
| 810 | if (!strcmp(event->name, "sched_process_fork")) |
| 811 | process_sched_fork_event(more_data, event, cpu, timestamp, thread); |
| 812 | if (!strcmp(event->name, "sched_process_exit")) |
| 813 | process_sched_exit_event(event, cpu, timestamp, thread); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 816 | static int |
| 817 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
| 818 | { |
| 819 | char level; |
| 820 | int show = 0; |
| 821 | struct dso *dso = NULL; |
| 822 | struct thread *thread; |
| 823 | u64 ip = event->ip.ip; |
| 824 | u64 timestamp = -1; |
| 825 | u32 cpu = -1; |
| 826 | u64 period = 1; |
| 827 | void *more_data = event->ip.__more_data; |
| 828 | int cpumode; |
| 829 | |
| 830 | thread = threads__findnew(event->ip.pid, &threads, &last_match); |
| 831 | |
| 832 | if (sample_type & PERF_SAMPLE_TIME) { |
| 833 | timestamp = *(u64 *)more_data; |
| 834 | more_data += sizeof(u64); |
| 835 | } |
| 836 | |
| 837 | if (sample_type & PERF_SAMPLE_CPU) { |
| 838 | cpu = *(u32 *)more_data; |
| 839 | more_data += sizeof(u32); |
| 840 | more_data += sizeof(u32); /* reserved */ |
| 841 | } |
| 842 | |
| 843 | if (sample_type & PERF_SAMPLE_PERIOD) { |
| 844 | period = *(u64 *)more_data; |
| 845 | more_data += sizeof(u64); |
| 846 | } |
| 847 | |
| 848 | dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", |
| 849 | (void *)(offset + head), |
| 850 | (void *)(long)(event->header.size), |
| 851 | event->header.misc, |
| 852 | event->ip.pid, event->ip.tid, |
| 853 | (void *)(long)ip, |
| 854 | (long long)period); |
| 855 | |
| 856 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 857 | |
| 858 | if (thread == NULL) { |
| 859 | eprintf("problem processing %d event, skipping it.\n", |
| 860 | event->header.type); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; |
| 865 | |
| 866 | if (cpumode == PERF_EVENT_MISC_KERNEL) { |
| 867 | show = SHOW_KERNEL; |
| 868 | level = 'k'; |
| 869 | |
| 870 | dso = kernel_dso; |
| 871 | |
| 872 | dump_printf(" ...... dso: %s\n", dso->name); |
| 873 | |
| 874 | } else if (cpumode == PERF_EVENT_MISC_USER) { |
| 875 | |
| 876 | show = SHOW_USER; |
| 877 | level = '.'; |
| 878 | |
| 879 | } else { |
| 880 | show = SHOW_HV; |
| 881 | level = 'H'; |
| 882 | |
| 883 | dso = hypervisor_dso; |
| 884 | |
| 885 | dump_printf(" ...... dso: [hypervisor]\n"); |
| 886 | } |
| 887 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 888 | if (sample_type & PERF_SAMPLE_RAW) |
| 889 | process_raw_event(event, more_data, cpu, timestamp, thread); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static int |
| 895 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 896 | { |
| 897 | trace_event(event); |
| 898 | |
| 899 | switch (event->header.type) { |
| 900 | case PERF_EVENT_MMAP ... PERF_EVENT_LOST: |
| 901 | return 0; |
| 902 | |
| 903 | case PERF_EVENT_COMM: |
| 904 | return process_comm_event(event, offset, head); |
| 905 | |
| 906 | case PERF_EVENT_EXIT ... PERF_EVENT_READ: |
| 907 | return 0; |
| 908 | |
| 909 | case PERF_EVENT_SAMPLE: |
| 910 | return process_sample_event(event, offset, head); |
| 911 | |
| 912 | case PERF_EVENT_MAX: |
| 913 | default: |
| 914 | return -1; |
| 915 | } |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | static int __cmd_sched(void) |
| 921 | { |
| 922 | int ret, rc = EXIT_FAILURE; |
| 923 | unsigned long offset = 0; |
| 924 | unsigned long head = 0; |
| 925 | struct stat perf_stat; |
| 926 | event_t *event; |
| 927 | uint32_t size; |
| 928 | char *buf; |
| 929 | |
| 930 | trace_report(); |
| 931 | register_idle_thread(&threads, &last_match); |
| 932 | |
| 933 | input = open(input_name, O_RDONLY); |
| 934 | if (input < 0) { |
| 935 | perror("failed to open file"); |
| 936 | exit(-1); |
| 937 | } |
| 938 | |
| 939 | ret = fstat(input, &perf_stat); |
| 940 | if (ret < 0) { |
| 941 | perror("failed to stat file"); |
| 942 | exit(-1); |
| 943 | } |
| 944 | |
| 945 | if (!perf_stat.st_size) { |
| 946 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 947 | exit(0); |
| 948 | } |
| 949 | header = perf_header__read(input); |
| 950 | head = header->data_offset; |
| 951 | sample_type = perf_header__sample_type(header); |
| 952 | |
| 953 | if (!(sample_type & PERF_SAMPLE_RAW)) |
| 954 | die("No trace sample to read. Did you call perf record " |
| 955 | "without -R?"); |
| 956 | |
| 957 | if (load_kernel() < 0) { |
| 958 | perror("failed to load kernel symbols"); |
| 959 | return EXIT_FAILURE; |
| 960 | } |
| 961 | |
| 962 | remap: |
| 963 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 964 | MAP_SHARED, input, offset); |
| 965 | if (buf == MAP_FAILED) { |
| 966 | perror("failed to mmap file"); |
| 967 | exit(-1); |
| 968 | } |
| 969 | |
| 970 | more: |
| 971 | event = (event_t *)(buf + head); |
| 972 | |
| 973 | size = event->header.size; |
| 974 | if (!size) |
| 975 | size = 8; |
| 976 | |
| 977 | if (head + event->header.size >= page_size * mmap_window) { |
| 978 | unsigned long shift = page_size * (head / page_size); |
| 979 | int res; |
| 980 | |
| 981 | res = munmap(buf, page_size * mmap_window); |
| 982 | assert(res == 0); |
| 983 | |
| 984 | offset += shift; |
| 985 | head -= shift; |
| 986 | goto remap; |
| 987 | } |
| 988 | |
| 989 | size = event->header.size; |
| 990 | |
| 991 | |
| 992 | if (!size || process_event(event, offset, head) < 0) { |
| 993 | |
| 994 | /* |
| 995 | * assume we lost track of the stream, check alignment, and |
| 996 | * increment a single u64 in the hope to catch on again 'soon'. |
| 997 | */ |
| 998 | |
| 999 | if (unlikely(head & 7)) |
| 1000 | head &= ~7ULL; |
| 1001 | |
| 1002 | size = 8; |
| 1003 | } |
| 1004 | |
| 1005 | head += size; |
| 1006 | |
| 1007 | if (offset + head < (unsigned long)perf_stat.st_size) |
| 1008 | goto more; |
| 1009 | |
| 1010 | rc = EXIT_SUCCESS; |
| 1011 | close(input); |
| 1012 | |
| 1013 | return rc; |
| 1014 | } |
| 1015 | |
| 1016 | static const char * const annotate_usage[] = { |
| 1017 | "perf trace [<options>] <command>", |
| 1018 | NULL |
| 1019 | }; |
| 1020 | |
| 1021 | static const struct option options[] = { |
| 1022 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1023 | "dump raw trace in ASCII"), |
| 1024 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1025 | "be more verbose (show symbol address, etc)"), |
| 1026 | OPT_END() |
| 1027 | }; |
| 1028 | |
| 1029 | int cmd_sched(int argc, const char **argv, const char *prefix __used) |
| 1030 | { |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1031 | long nr_iterations = 10, i; |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1032 | |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1033 | symbol__init(); |
| 1034 | page_size = getpagesize(); |
| 1035 | |
| 1036 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 1037 | if (argc) { |
| 1038 | /* |
| 1039 | * Special case: if there's an argument left then assume tha |
| 1040 | * it's a symbol filter: |
| 1041 | */ |
| 1042 | if (argc > 1) |
| 1043 | usage_with_options(annotate_usage, options); |
| 1044 | } |
| 1045 | |
Ingo Molnar | fbf9482 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1046 | // setup_pager(); |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1047 | |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1048 | calibrate_run_measurement_overhead(); |
| 1049 | calibrate_sleep_measurement_overhead(); |
| 1050 | |
| 1051 | test_calibrations(); |
| 1052 | |
| 1053 | parse_trace(); |
| 1054 | print_task_traces(); |
| 1055 | add_cross_task_wakeups(); |
| 1056 | |
| 1057 | create_tasks(); |
Ingo Molnar | ad236fd | 2009-09-11 12:12:54 +0200 | [diff] [blame^] | 1058 | printf("------------------------------------------------------------\n"); |
Ingo Molnar | ec15676 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1059 | for (i = 0; i < nr_iterations; i++) |
| 1060 | run_one_test(); |
| 1061 | |
| 1062 | return 0; |
Ingo Molnar | 0a02ad9 | 2009-09-11 12:12:54 +0200 | [diff] [blame] | 1063 | } |