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