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