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