perf tools: Use __maybe_used for unused variables
[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
efad1415 26static const char *input_name;
0a02ad93 27
daa1d7a5 28static char default_sort_order[] = "avg, max, switch, runtime";
edb7c60e 29static const char *sort_order = default_sort_order;
daa1d7a5 30
55ffb7a6
MG
31static 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
36static u64 run_measurement_overhead;
37static 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 44static unsigned long nr_tasks;
ec156764 45
39aeb52f 46struct sched_atom;
ec156764 47
b1ffe8f3
IM
48struct 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
66enum 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 73struct 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
83static struct task_desc *pid_to_task[MAX_PID];
84
85static struct task_desc **tasks;
86
87static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
88static u64 start_time;
89
90static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
ec156764 91
b1ffe8f3
IM
92static unsigned long nr_run_events;
93static unsigned long nr_sleep_events;
94static unsigned long nr_wakeup_events;
95
96static unsigned long nr_sleep_corrections;
97static unsigned long nr_run_events_optimized;
98
99static unsigned long targetless_wakeups;
100static unsigned long multitarget_wakeups;
101
102static u64 cpu_usage;
103static u64 runavg_cpu_usage;
104static u64 parent_cpu_usage;
105static u64 runavg_parent_cpu_usage;
106
107static unsigned long nr_runs;
108static u64 sum_runtime;
109static u64 sum_fluct;
110static u64 run_avg;
111
1967936d 112static unsigned int replay_repeat = 10;
ea57c4f5 113static unsigned long nr_timestamps;
dc02bf71
IM
114static unsigned long nr_unordered_timestamps;
115static unsigned long nr_state_machine_bugs;
c8a37751 116static unsigned long nr_context_switch_bugs;
dc02bf71
IM
117static unsigned long nr_events;
118static unsigned long nr_lost_chunks;
119static unsigned long nr_lost_events;
b1ffe8f3
IM
120
121#define TASK_STATE_TO_CHAR_STR "RSDTtZX"
122
123enum thread_state {
124 THREAD_SLEEPING = 0,
125 THREAD_WAIT_CPU,
126 THREAD_SCHED_IN,
127 THREAD_IGNORE
128};
129
130struct 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 139struct 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 150typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
b1ffe8f3
IM
151
152static struct rb_root atom_root, sorted_atom_root;
153
154static u64 all_runtime;
155static u64 all_count;
156
b1ffe8f3
IM
157
158static 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 167static 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 176static 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
186static 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
203static 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 221static struct sched_atom *
b1ffe8f3 222get_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 241static 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
249static void
b1ffe8f3 250add_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 272static void
b1ffe8f3 273add_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
300static void
b1ffe8f3 301add_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
311static 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
345static 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
357static 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
372static 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 398static 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 413static 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
431static 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
442static 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
455again:
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
483static 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
510static 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
563static 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
611static 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) \
632do { \
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) \
638do { \
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
648struct 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 666struct 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
681struct 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
698struct 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
713struct 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 729struct 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 753static int
419ab0d6 754replay_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 776static u64 cpu_last_switched[MAX_CPUS];
fbf94829 777
a116e05d 778static int
419ab0d6 779replay_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 825static int
419ab0d6 826replay_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 839static 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
845struct sort_dimension {
846 const char *name;
b5fae128 847 sort_fn_t cmp;
b1ffe8f3
IM
848 struct list_head list;
849};
850
851static LIST_HEAD(cmp_pid);
852
daa1d7a5 853static int
39aeb52f 854thread_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 870static struct work_atoms *
b5fae128
IM
871thread_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 896static 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 921static 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
935static 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
942static 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 949static int
39aeb52f 950add_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
971static void
1d037ca1
IT
972add_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
985static void
986add_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 1016static int
cdce9d73 1017latency_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 1079static int
39aeb52f 1080latency_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 1105static int
cdce9d73 1106latency_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 1157static int
55ffb7a6 1158latency_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 1200static 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 1208static 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 1239static 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
1249static struct sort_dimension pid_sort_dimension = {
b5fae128
IM
1250 .name = "pid",
1251 .cmp = pid_cmp,
daa1d7a5
FW
1252};
1253
39aeb52f 1254static 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
1275static struct sort_dimension avg_sort_dimension = {
b5fae128
IM
1276 .name = "avg",
1277 .cmp = avg_cmp,
daa1d7a5
FW
1278};
1279
39aeb52f 1280static 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
1290static struct sort_dimension max_sort_dimension = {
b5fae128
IM
1291 .name = "max",
1292 .cmp = max_cmp,
daa1d7a5
FW
1293};
1294
39aeb52f 1295static 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
1305static struct sort_dimension switch_sort_dimension = {
b5fae128
IM
1306 .name = "switch",
1307 .cmp = switch_cmp,
daa1d7a5
FW
1308};
1309
39aeb52f 1310static 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
1320static struct sort_dimension runtime_sort_dimension = {
b5fae128
IM
1321 .name = "runtime",
1322 .cmp = runtime_cmp,
daa1d7a5
FW
1323};
1324
1325static 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
1335static LIST_HEAD(sort_list);
1336
cbef79a8 1337static 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
1352static void setup_sorting(void);
1353
1354static 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
1370static struct trace_sched_handler *trace_handler;
1371
1d037ca1
IT
1372static int process_sched_wakeup_event(struct perf_tool *tool __maybe_unused,
1373 struct event_format *event,
1374 struct perf_sample *sample,
1375 struct machine *machine,
1376 struct thread *thread __maybe_unused)
419ab0d6 1377{
ee29be62 1378 void *data = sample->raw_data;
419ab0d6 1379 struct trace_wakeup_event wakeup_event;
a116e05d 1380 int err = 0;
419ab0d6 1381
f48f669d 1382 FILL_COMMON_FIELDS(wakeup_event, event, data);
419ab0d6 1383
f48f669d
XG
1384 FILL_ARRAY(wakeup_event, comm, event, data);
1385 FILL_FIELD(wakeup_event, pid, event, data);
1386 FILL_FIELD(wakeup_event, prio, event, data);
1387 FILL_FIELD(wakeup_event, success, event, data);
1388 FILL_FIELD(wakeup_event, cpu, event, data);
419ab0d6 1389
0ec04e16 1390 if (trace_handler->wakeup_event)
a116e05d
ACM
1391 err = trace_handler->wakeup_event(&wakeup_event, machine, event, sample);
1392
1393 return err;
419ab0d6
FW
1394}
1395
c8a37751
IM
1396/*
1397 * Track the current task - that way we can know whether there's any
1398 * weird events, such as a task being switched away that is not current.
1399 */
40749d0f 1400static int max_cpu;
0ec04e16 1401
c8a37751
IM
1402static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1403
0ec04e16
IM
1404static struct thread *curr_thread[MAX_CPUS];
1405
1406static char next_shortname1 = 'A';
1407static char next_shortname2 = '0';
1408
a116e05d 1409static int
0ec04e16 1410map_switch_event(struct trace_switch_event *switch_event,
743eb868 1411 struct machine *machine,
1d037ca1 1412 struct event_format *event __maybe_unused,
7f7f8d0b 1413 struct perf_sample *sample)
0ec04e16 1414{
1d037ca1 1415 struct thread *sched_out __maybe_unused, *sched_in;
0ec04e16 1416 int new_shortname;
7f7f8d0b 1417 u64 timestamp0, timestamp = sample->time;
0ec04e16 1418 s64 delta;
7f7f8d0b 1419 int cpu, this_cpu = sample->cpu;
0ec04e16
IM
1420
1421 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1422
1423 if (this_cpu > max_cpu)
1424 max_cpu = this_cpu;
1425
1426 timestamp0 = cpu_last_switched[this_cpu];
1427 cpu_last_switched[this_cpu] = timestamp;
1428 if (timestamp0)
1429 delta = timestamp - timestamp0;
1430 else
1431 delta = 0;
1432
a116e05d
ACM
1433 if (delta < 0) {
1434 pr_debug("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1435 return -1;
1436 }
0ec04e16 1437
743eb868
ACM
1438 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1439 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
0ec04e16
IM
1440
1441 curr_thread[this_cpu] = sched_in;
1442
1443 printf(" ");
1444
1445 new_shortname = 0;
1446 if (!sched_in->shortname[0]) {
1447 sched_in->shortname[0] = next_shortname1;
1448 sched_in->shortname[1] = next_shortname2;
1449
1450 if (next_shortname1 < 'Z') {
1451 next_shortname1++;
1452 } else {
1453 next_shortname1='A';
1454 if (next_shortname2 < '9') {
1455 next_shortname2++;
1456 } else {
1457 next_shortname2='0';
1458 }
1459 }
1460 new_shortname = 1;
1461 }
1462
1463 for (cpu = 0; cpu <= max_cpu; cpu++) {
1464 if (cpu != this_cpu)
1465 printf(" ");
1466 else
1467 printf("*");
1468
1469 if (curr_thread[cpu]) {
1470 if (curr_thread[cpu]->pid)
1471 printf("%2s ", curr_thread[cpu]->shortname);
1472 else
1473 printf(". ");
1474 } else
1475 printf(" ");
1476 }
1477
1478 printf(" %12.6f secs ", (double)timestamp/1e9);
1479 if (new_shortname) {
1480 printf("%s => %s:%d\n",
1481 sched_in->shortname, sched_in->comm, sched_in->pid);
1482 } else {
1483 printf("\n");
1484 }
a116e05d
ACM
1485
1486 return 0;
0ec04e16
IM
1487}
1488
1d037ca1
IT
1489static int process_sched_switch_event(struct perf_tool *tool __maybe_unused,
1490 struct event_format *event,
1491 struct perf_sample *sample,
1492 struct machine *machine,
1493 struct thread *thread __maybe_unused)
419ab0d6 1494{
a116e05d 1495 int this_cpu = sample->cpu, err = 0;
ee29be62 1496 void *data = sample->raw_data;
419ab0d6
FW
1497 struct trace_switch_event switch_event;
1498
f48f669d 1499 FILL_COMMON_FIELDS(switch_event, event, data);
419ab0d6 1500
f48f669d
XG
1501 FILL_ARRAY(switch_event, prev_comm, event, data);
1502 FILL_FIELD(switch_event, prev_pid, event, data);
1503 FILL_FIELD(switch_event, prev_prio, event, data);
1504 FILL_FIELD(switch_event, prev_state, event, data);
1505 FILL_ARRAY(switch_event, next_comm, event, data);
1506 FILL_FIELD(switch_event, next_pid, event, data);
1507 FILL_FIELD(switch_event, next_prio, event, data);
419ab0d6 1508
0ec04e16 1509 if (curr_pid[this_cpu] != (u32)-1) {
c8a37751
IM
1510 /*
1511 * Are we trying to switch away a PID that is
1512 * not current?
1513 */
0ec04e16 1514 if (curr_pid[this_cpu] != switch_event.prev_pid)
c8a37751
IM
1515 nr_context_switch_bugs++;
1516 }
0ec04e16 1517 if (trace_handler->switch_event)
a116e05d 1518 err = trace_handler->switch_event(&switch_event, machine, event, sample);
c8a37751 1519
0ec04e16 1520 curr_pid[this_cpu] = switch_event.next_pid;
a116e05d 1521 return err;
419ab0d6
FW
1522}
1523
1d037ca1
IT
1524static int process_sched_runtime_event(struct perf_tool *tool __maybe_unused,
1525 struct event_format *event,
1526 struct perf_sample *sample,
1527 struct machine *machine,
1528 struct thread *thread __maybe_unused)
39aeb52f 1529{
ee29be62 1530 void *data = sample->raw_data;
39aeb52f 1531 struct trace_runtime_event runtime_event;
a116e05d 1532 int err = 0;
39aeb52f 1533
f48f669d
XG
1534 FILL_ARRAY(runtime_event, comm, event, data);
1535 FILL_FIELD(runtime_event, pid, event, data);
1536 FILL_FIELD(runtime_event, runtime, event, data);
1537 FILL_FIELD(runtime_event, vruntime, event, data);
39aeb52f 1538
0ec04e16 1539 if (trace_handler->runtime_event)
a116e05d
ACM
1540 err = trace_handler->runtime_event(&runtime_event, machine, sample);
1541
1542 return err;
39aeb52f 1543}
1544
1d037ca1
IT
1545static int process_sched_fork_event(struct perf_tool *tool __maybe_unused,
1546 struct event_format *event,
1547 struct perf_sample *sample,
1548 struct machine *machine __maybe_unused,
1549 struct thread *thread __maybe_unused)
fbf94829 1550{
ee29be62 1551 void *data = sample->raw_data;
46538818 1552 struct trace_fork_event fork_event;
a116e05d 1553 int err = 0;
46538818 1554
f48f669d 1555 FILL_COMMON_FIELDS(fork_event, event, data);
46538818 1556
f48f669d
XG
1557 FILL_ARRAY(fork_event, parent_comm, event, data);
1558 FILL_FIELD(fork_event, parent_pid, event, data);
1559 FILL_ARRAY(fork_event, child_comm, event, data);
1560 FILL_FIELD(fork_event, child_pid, event, data);
46538818 1561
0ec04e16 1562 if (trace_handler->fork_event)
a116e05d
ACM
1563 err = trace_handler->fork_event(&fork_event, event);
1564
1565 return err;
fbf94829
IM
1566}
1567
1d037ca1
IT
1568static int process_sched_exit_event(struct perf_tool *tool __maybe_unused,
1569 struct event_format *event,
1570 struct perf_sample *sample __maybe_unused,
1571 struct machine *machine __maybe_unused,
1572 struct thread *thread __maybe_unused)
fbf94829 1573{
ad236fd2
IM
1574 if (verbose)
1575 printf("sched_exit event %p\n", event);
a116e05d
ACM
1576
1577 return 0;
ec156764
IM
1578}
1579
1d037ca1
IT
1580static int process_sched_migrate_task_event(struct perf_tool *tool __maybe_unused,
1581 struct event_format *event,
1582 struct perf_sample *sample,
1583 struct machine *machine,
1584 struct thread *thread __maybe_unused)
55ffb7a6 1585{
ee29be62 1586 void *data = sample->raw_data;
55ffb7a6 1587 struct trace_migrate_task_event migrate_task_event;
a116e05d 1588 int err = 0;
55ffb7a6 1589
f48f669d 1590 FILL_COMMON_FIELDS(migrate_task_event, event, data);
55ffb7a6 1591
f48f669d
XG
1592 FILL_ARRAY(migrate_task_event, comm, event, data);
1593 FILL_FIELD(migrate_task_event, pid, event, data);
1594 FILL_FIELD(migrate_task_event, prio, event, data);
1595 FILL_FIELD(migrate_task_event, cpu, event, data);
55ffb7a6
MG
1596
1597 if (trace_handler->migrate_task_event)
a116e05d
ACM
1598 err = trace_handler->migrate_task_event(&migrate_task_event, machine, sample);
1599
1600 return err;
55ffb7a6
MG
1601}
1602
a116e05d
ACM
1603typedef int (*tracepoint_handler)(struct perf_tool *tool,
1604 struct event_format *tp_format,
1605 struct perf_sample *sample,
1606 struct machine *machine,
1607 struct thread *thread);
ec156764 1608
1d037ca1
IT
1609static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused,
1610 union perf_event *event __maybe_unused,
ee29be62
ACM
1611 struct perf_sample *sample,
1612 struct perf_evsel *evsel,
1613 struct machine *machine)
0a02ad93 1614{
ee29be62 1615 struct thread *thread = machine__findnew_thread(machine, sample->pid);
a116e05d 1616 int err = 0;
0a02ad93 1617
0a02ad93 1618 if (thread == NULL) {
ee29be62 1619 pr_debug("problem processing %s event, skipping it.\n",
22c8b843 1620 perf_evsel__name(evsel));
0a02ad93
IM
1621 return -1;
1622 }
1623
ee29be62
ACM
1624 evsel->hists.stats.total_period += sample->period;
1625 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
f39cdf25 1626
ee29be62
ACM
1627 if (evsel->handler.func != NULL) {
1628 tracepoint_handler f = evsel->handler.func;
a116e05d 1629 err = f(tool, evsel->tp_format, sample, machine, thread);
ee29be62 1630 }
0a02ad93 1631
a116e05d 1632 return err;
0a02ad93
IM
1633}
1634
fcf65bf1
ACM
1635static struct perf_tool perf_sched = {
1636 .sample = perf_sched__process_tracepoint_sample,
1637 .comm = perf_event__process_comm,
1638 .lost = perf_event__process_lost,
1639 .fork = perf_event__process_task,
1640 .ordered_samples = true,
016e92fb
FW
1641};
1642
a116e05d 1643static int read_events(bool destroy, struct perf_session **psession)
0a02ad93 1644{
ee29be62
ACM
1645 const struct perf_evsel_str_handler handlers[] = {
1646 { "sched:sched_switch", process_sched_switch_event, },
1647 { "sched:sched_stat_runtime", process_sched_runtime_event, },
1648 { "sched:sched_wakeup", process_sched_wakeup_event, },
1649 { "sched:sched_wakeup_new", process_sched_wakeup_event, },
1650 { "sched:sched_process_fork", process_sched_fork_event, },
1651 { "sched:sched_process_exit", process_sched_exit_event, },
1652 { "sched:sched_migrate_task", process_sched_migrate_task_event, },
1653 };
da378962
ACM
1654 struct perf_session *session;
1655
fcf65bf1 1656 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_sched);
a116e05d
ACM
1657 if (session == NULL) {
1658 pr_debug("No Memory for session\n");
1659 return -1;
1660 }
94c744b6 1661
a116e05d
ACM
1662 if (perf_session__set_tracepoints_handlers(session, handlers))
1663 goto out_delete;
ee29be62 1664
cee75ac7 1665 if (perf_session__has_traces(session, "record -R")) {
a116e05d
ACM
1666 int err = perf_session__process_events(session, &perf_sched);
1667 if (err) {
1668 pr_err("Failed to process events, error %d", err);
1669 goto out_delete;
1670 }
4c09bafa 1671
cee75ac7
ACM
1672 nr_events = session->hists.stats.nr_events[0];
1673 nr_lost_events = session->hists.stats.total_lost;
1674 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1675 }
d549c769 1676
4c09bafa
JO
1677 if (destroy)
1678 perf_session__delete(session);
1679
1680 if (psession)
1681 *psession = session;
a116e05d
ACM
1682
1683 return 0;
1684
1685out_delete:
1686 perf_session__delete(session);
1687 return -1;
0a02ad93
IM
1688}
1689
0ec04e16
IM
1690static void print_bad_events(void)
1691{
1692 if (nr_unordered_timestamps && nr_timestamps) {
1693 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1694 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1695 nr_unordered_timestamps, nr_timestamps);
1696 }
1697 if (nr_lost_events && nr_events) {
1698 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1699 (double)nr_lost_events/(double)nr_events*100.0,
1700 nr_lost_events, nr_events, nr_lost_chunks);
1701 }
1702 if (nr_state_machine_bugs && nr_timestamps) {
1703 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1704 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1705 nr_state_machine_bugs, nr_timestamps);
1706 if (nr_lost_events)
1707 printf(" (due to lost events?)");
1708 printf("\n");
1709 }
1710 if (nr_context_switch_bugs && nr_timestamps) {
1711 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1712 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1713 nr_context_switch_bugs, nr_timestamps);
1714 if (nr_lost_events)
1715 printf(" (due to lost events?)");
1716 printf("\n");
1717 }
1718}
1719
a116e05d 1720static int __cmd_lat(void)
0ec04e16
IM
1721{
1722 struct rb_node *next;
4c09bafa 1723 struct perf_session *session;
0ec04e16
IM
1724
1725 setup_pager();
a116e05d
ACM
1726 if (read_events(false, &session))
1727 return -1;
0ec04e16
IM
1728 sort_lat();
1729
3786310a
FW
1730 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1731 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1732 printf(" ---------------------------------------------------------------------------------------------------------------\n");
0ec04e16
IM
1733
1734 next = rb_first(&sorted_atom_root);
1735
1736 while (next) {
1737 struct work_atoms *work_list;
1738
1739 work_list = rb_entry(next, struct work_atoms, node);
1740 output_lat_thread(work_list);
1741 next = rb_next(next);
1742 }
1743
1744 printf(" -----------------------------------------------------------------------------------------\n");
9486aa38 1745 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
0ec04e16
IM
1746 (double)all_runtime/1e6, all_count);
1747
1748 printf(" ---------------------------------------------------\n");
1749
1750 print_bad_events();
1751 printf("\n");
1752
4c09bafa 1753 perf_session__delete(session);
a116e05d 1754 return 0;
0ec04e16
IM
1755}
1756
1757static struct trace_sched_handler map_ops = {
1758 .wakeup_event = NULL,
1759 .switch_event = map_switch_event,
1760 .runtime_event = NULL,
1761 .fork_event = NULL,
1762};
1763
a116e05d 1764static int __cmd_map(void)
0ec04e16 1765{
40749d0f
IM
1766 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1767
0ec04e16 1768 setup_pager();
a116e05d
ACM
1769 if (read_events(true, NULL))
1770 return -1;
0ec04e16 1771 print_bad_events();
a116e05d 1772 return 0;
0ec04e16
IM
1773}
1774
a116e05d 1775static int __cmd_replay(void)
0ec04e16
IM
1776{
1777 unsigned long i;
1778
1779 calibrate_run_measurement_overhead();
1780 calibrate_sleep_measurement_overhead();
1781
1782 test_calibrations();
1783
a116e05d
ACM
1784 if (read_events(true, NULL))
1785 return -1;
0ec04e16
IM
1786
1787 printf("nr_run_events: %ld\n", nr_run_events);
1788 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1789 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1790
1791 if (targetless_wakeups)
1792 printf("target-less wakeups: %ld\n", targetless_wakeups);
1793 if (multitarget_wakeups)
1794 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1795 if (nr_run_events_optimized)
1796 printf("run atoms optimized: %ld\n",
1797 nr_run_events_optimized);
1798
1799 print_task_traces();
1800 add_cross_task_wakeups();
1801
1802 create_tasks();
1803 printf("------------------------------------------------------------\n");
1804 for (i = 0; i < replay_repeat; i++)
1805 run_one_test();
a116e05d
ACM
1806
1807 return 0;
0ec04e16
IM
1808}
1809
1810
46f392c9 1811static const char * const sched_usage[] = {
580cabed 1812 "perf sched [<options>] {record|latency|map|replay|script}",
0a02ad93
IM
1813 NULL
1814};
1815
f2858d8a 1816static const struct option sched_options[] = {
4b77a729
MG
1817 OPT_STRING('i', "input", &input_name, "file",
1818 "input file name"),
c0555642 1819 OPT_INCR('v', "verbose", &verbose,
f2858d8a 1820 "be more verbose (show symbol address, etc)"),
0a02ad93
IM
1821 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1822 "dump raw trace in ASCII"),
f2858d8a
IM
1823 OPT_END()
1824};
1825
1826static const char * const latency_usage[] = {
1827 "perf sched latency [<options>]",
1828 NULL
1829};
1830
1831static const struct option latency_options[] = {
daa1d7a5
FW
1832 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1833 "sort by key(s): runtime, switch, avg, max"),
c0555642 1834 OPT_INCR('v', "verbose", &verbose,
0a02ad93 1835 "be more verbose (show symbol address, etc)"),
55ffb7a6
MG
1836 OPT_INTEGER('C', "CPU", &profile_cpu,
1837 "CPU to profile on"),
f2858d8a
IM
1838 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1839 "dump raw trace in ASCII"),
1840 OPT_END()
1841};
1842
1843static const char * const replay_usage[] = {
1844 "perf sched replay [<options>]",
1845 NULL
1846};
1847
1848static const struct option replay_options[] = {
1967936d
ACM
1849 OPT_UINTEGER('r', "repeat", &replay_repeat,
1850 "repeat the workload replay N times (-1: infinite)"),
c0555642 1851 OPT_INCR('v', "verbose", &verbose,
f2858d8a
IM
1852 "be more verbose (show symbol address, etc)"),
1853 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1854 "dump raw trace in ASCII"),
0a02ad93
IM
1855 OPT_END()
1856};
1857
daa1d7a5
FW
1858static void setup_sorting(void)
1859{
1860 char *tmp, *tok, *str = strdup(sort_order);
1861
1862 for (tok = strtok_r(str, ", ", &tmp);
1863 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1864 if (sort_dimension__add(tok, &sort_list) < 0) {
1865 error("Unknown --sort key: `%s'", tok);
f2858d8a 1866 usage_with_options(latency_usage, latency_options);
daa1d7a5
FW
1867 }
1868 }
1869
1870 free(str);
1871
cbef79a8 1872 sort_dimension__add("pid", &cmp_pid);
daa1d7a5
FW
1873}
1874
1fc35b29
IM
1875static const char *record_args[] = {
1876 "record",
1877 "-a",
1878 "-R",
ea57c4f5 1879 "-f",
dc02bf71 1880 "-m", "1024",
1fc35b29 1881 "-c", "1",
9710118b
SE
1882 "-e", "sched:sched_switch",
1883 "-e", "sched:sched_stat_wait",
1884 "-e", "sched:sched_stat_sleep",
1885 "-e", "sched:sched_stat_iowait",
1886 "-e", "sched:sched_stat_runtime",
1887 "-e", "sched:sched_process_exit",
1888 "-e", "sched:sched_process_fork",
1889 "-e", "sched:sched_wakeup",
1890 "-e", "sched:sched_migrate_task",
1fc35b29
IM
1891};
1892
1893static int __cmd_record(int argc, const char **argv)
1894{
1895 unsigned int rec_argc, i, j;
1896 const char **rec_argv;
1897
1898 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1899 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1900
e462dc55 1901 if (rec_argv == NULL)
ce47dc56
CS
1902 return -ENOMEM;
1903
1fc35b29
IM
1904 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1905 rec_argv[i] = strdup(record_args[i]);
1906
1907 for (j = 1; j < (unsigned int)argc; j++, i++)
1908 rec_argv[i] = argv[j];
1909
1910 BUG_ON(i != rec_argc);
1911
1912 return cmd_record(i, rec_argv, NULL);
1913}
1914
1d037ca1 1915int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
0a02ad93 1916{
f2858d8a
IM
1917 argc = parse_options(argc, argv, sched_options, sched_usage,
1918 PARSE_OPT_STOP_AT_NON_OPTION);
1919 if (!argc)
1920 usage_with_options(sched_usage, sched_options);
0a02ad93 1921
c0777c5a 1922 /*
133dc4c3 1923 * Aliased to 'perf script' for now:
c0777c5a 1924 */
133dc4c3
IM
1925 if (!strcmp(argv[0], "script"))
1926 return cmd_script(argc, argv, prefix);
c0777c5a 1927
75be6cf4 1928 symbol__init();
1fc35b29
IM
1929 if (!strncmp(argv[0], "rec", 3)) {
1930 return __cmd_record(argc, argv);
1931 } else if (!strncmp(argv[0], "lat", 3)) {
cdce9d73 1932 trace_handler = &lat_ops;
f2858d8a
IM
1933 if (argc > 1) {
1934 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1935 if (argc)
1936 usage_with_options(latency_usage, latency_options);
f2858d8a 1937 }
b5fae128 1938 setup_sorting();
a116e05d 1939 return __cmd_lat();
0ec04e16
IM
1940 } else if (!strcmp(argv[0], "map")) {
1941 trace_handler = &map_ops;
1942 setup_sorting();
a116e05d 1943 return __cmd_map();
f2858d8a
IM
1944 } else if (!strncmp(argv[0], "rep", 3)) {
1945 trace_handler = &replay_ops;
1946 if (argc) {
1947 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1948 if (argc)
1949 usage_with_options(replay_usage, replay_options);
1950 }
a116e05d 1951 return __cmd_replay();
f2858d8a
IM
1952 } else {
1953 usage_with_options(sched_usage, sched_options);
1954 }
1955
ec156764 1956 return 0;
0a02ad93 1957}
This page took 0.334111 seconds and 5 git commands to generate.