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