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