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