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