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