5 #include "util/evlist.h"
6 #include "util/cache.h"
7 #include "util/evsel.h"
8 #include "util/symbol.h"
9 #include "util/thread.h"
10 #include "util/header.h"
11 #include "util/session.h"
12 #include "util/tool.h"
13 #include "util/cloexec.h"
15 #include "util/parse-options.h"
16 #include "util/trace-event.h"
18 #include "util/debug.h"
20 #include <sys/prctl.h>
21 #include <sys/resource.h>
23 #include <semaphore.h>
26 #include <api/fs/fs.h>
28 #define PR_SET_NAME 15 /* Set process name */
32 #define MAX_PID 1024000
41 unsigned long nr_events
;
42 unsigned long curr_event
;
43 struct sched_atom
**atoms
;
54 enum sched_event_type
{
58 SCHED_EVENT_MIGRATION
,
62 enum sched_event_type type
;
68 struct task_desc
*wakee
;
71 #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
81 struct list_head list
;
82 enum thread_state state
;
90 struct list_head work_list
;
91 struct thread
*thread
;
100 typedef int (*sort_fn_t
)(struct work_atoms
*, struct work_atoms
*);
104 struct trace_sched_handler
{
105 int (*switch_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
106 struct perf_sample
*sample
, struct machine
*machine
);
108 int (*runtime_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
109 struct perf_sample
*sample
, struct machine
*machine
);
111 int (*wakeup_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
112 struct perf_sample
*sample
, struct machine
*machine
);
114 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
115 int (*fork_event
)(struct perf_sched
*sched
, union perf_event
*event
,
116 struct machine
*machine
);
118 int (*migrate_task_event
)(struct perf_sched
*sched
,
119 struct perf_evsel
*evsel
,
120 struct perf_sample
*sample
,
121 struct machine
*machine
);
125 struct perf_tool tool
;
126 const char *sort_order
;
127 unsigned long nr_tasks
;
128 struct task_desc
**pid_to_task
;
129 struct task_desc
**tasks
;
130 const struct trace_sched_handler
*tp_handler
;
131 pthread_mutex_t start_work_mutex
;
132 pthread_mutex_t work_done_wait_mutex
;
135 * Track the current task - that way we can know whether there's any
136 * weird events, such as a task being switched away that is not current.
139 u32 curr_pid
[MAX_CPUS
];
140 struct thread
*curr_thread
[MAX_CPUS
];
141 char next_shortname1
;
142 char next_shortname2
;
143 unsigned int replay_repeat
;
144 unsigned long nr_run_events
;
145 unsigned long nr_sleep_events
;
146 unsigned long nr_wakeup_events
;
147 unsigned long nr_sleep_corrections
;
148 unsigned long nr_run_events_optimized
;
149 unsigned long targetless_wakeups
;
150 unsigned long multitarget_wakeups
;
151 unsigned long nr_runs
;
152 unsigned long nr_timestamps
;
153 unsigned long nr_unordered_timestamps
;
154 unsigned long nr_context_switch_bugs
;
155 unsigned long nr_events
;
156 unsigned long nr_lost_chunks
;
157 unsigned long nr_lost_events
;
158 u64 run_measurement_overhead
;
159 u64 sleep_measurement_overhead
;
162 u64 runavg_cpu_usage
;
163 u64 parent_cpu_usage
;
164 u64 runavg_parent_cpu_usage
;
170 u64 cpu_last_switched
[MAX_CPUS
];
171 struct rb_root atom_root
, sorted_atom_root
;
172 struct list_head sort_list
, cmp_pid
;
176 static u64
get_nsecs(void)
180 clock_gettime(CLOCK_MONOTONIC
, &ts
);
182 return ts
.tv_sec
* 1000000000ULL + ts
.tv_nsec
;
185 static void burn_nsecs(struct perf_sched
*sched
, u64 nsecs
)
187 u64 T0
= get_nsecs(), T1
;
191 } while (T1
+ sched
->run_measurement_overhead
< T0
+ nsecs
);
194 static void sleep_nsecs(u64 nsecs
)
198 ts
.tv_nsec
= nsecs
% 999999999;
199 ts
.tv_sec
= nsecs
/ 999999999;
201 nanosleep(&ts
, NULL
);
204 static void calibrate_run_measurement_overhead(struct perf_sched
*sched
)
206 u64 T0
, T1
, delta
, min_delta
= 1000000000ULL;
209 for (i
= 0; i
< 10; i
++) {
211 burn_nsecs(sched
, 0);
214 min_delta
= min(min_delta
, delta
);
216 sched
->run_measurement_overhead
= min_delta
;
218 printf("run measurement overhead: %" PRIu64
" nsecs\n", min_delta
);
221 static void calibrate_sleep_measurement_overhead(struct perf_sched
*sched
)
223 u64 T0
, T1
, delta
, min_delta
= 1000000000ULL;
226 for (i
= 0; i
< 10; i
++) {
231 min_delta
= min(min_delta
, delta
);
234 sched
->sleep_measurement_overhead
= min_delta
;
236 printf("sleep measurement overhead: %" PRIu64
" nsecs\n", min_delta
);
239 static struct sched_atom
*
240 get_new_event(struct task_desc
*task
, u64 timestamp
)
242 struct sched_atom
*event
= zalloc(sizeof(*event
));
243 unsigned long idx
= task
->nr_events
;
246 event
->timestamp
= timestamp
;
250 size
= sizeof(struct sched_atom
*) * task
->nr_events
;
251 task
->atoms
= realloc(task
->atoms
, size
);
252 BUG_ON(!task
->atoms
);
254 task
->atoms
[idx
] = event
;
259 static struct sched_atom
*last_event(struct task_desc
*task
)
261 if (!task
->nr_events
)
264 return task
->atoms
[task
->nr_events
- 1];
267 static void add_sched_event_run(struct perf_sched
*sched
, struct task_desc
*task
,
268 u64 timestamp
, u64 duration
)
270 struct sched_atom
*event
, *curr_event
= last_event(task
);
273 * optimize an existing RUN event by merging this one
276 if (curr_event
&& curr_event
->type
== SCHED_EVENT_RUN
) {
277 sched
->nr_run_events_optimized
++;
278 curr_event
->duration
+= duration
;
282 event
= get_new_event(task
, timestamp
);
284 event
->type
= SCHED_EVENT_RUN
;
285 event
->duration
= duration
;
287 sched
->nr_run_events
++;
290 static void add_sched_event_wakeup(struct perf_sched
*sched
, struct task_desc
*task
,
291 u64 timestamp
, struct task_desc
*wakee
)
293 struct sched_atom
*event
, *wakee_event
;
295 event
= get_new_event(task
, timestamp
);
296 event
->type
= SCHED_EVENT_WAKEUP
;
297 event
->wakee
= wakee
;
299 wakee_event
= last_event(wakee
);
300 if (!wakee_event
|| wakee_event
->type
!= SCHED_EVENT_SLEEP
) {
301 sched
->targetless_wakeups
++;
304 if (wakee_event
->wait_sem
) {
305 sched
->multitarget_wakeups
++;
309 wakee_event
->wait_sem
= zalloc(sizeof(*wakee_event
->wait_sem
));
310 sem_init(wakee_event
->wait_sem
, 0, 0);
311 wakee_event
->specific_wait
= 1;
312 event
->wait_sem
= wakee_event
->wait_sem
;
314 sched
->nr_wakeup_events
++;
317 static void add_sched_event_sleep(struct perf_sched
*sched
, struct task_desc
*task
,
318 u64 timestamp
, u64 task_state __maybe_unused
)
320 struct sched_atom
*event
= get_new_event(task
, timestamp
);
322 event
->type
= SCHED_EVENT_SLEEP
;
324 sched
->nr_sleep_events
++;
327 static struct task_desc
*register_pid(struct perf_sched
*sched
,
328 unsigned long pid
, const char *comm
)
330 struct task_desc
*task
;
333 if (sched
->pid_to_task
== NULL
) {
334 if (sysctl__read_int("kernel/pid_max", &pid_max
) < 0)
336 BUG_ON((sched
->pid_to_task
= calloc(pid_max
, sizeof(struct task_desc
*))) == NULL
);
338 if (pid
>= (unsigned long)pid_max
) {
339 BUG_ON((sched
->pid_to_task
= realloc(sched
->pid_to_task
, (pid
+ 1) *
340 sizeof(struct task_desc
*))) == NULL
);
341 while (pid
>= (unsigned long)pid_max
)
342 sched
->pid_to_task
[pid_max
++] = NULL
;
345 task
= sched
->pid_to_task
[pid
];
350 task
= zalloc(sizeof(*task
));
352 task
->nr
= sched
->nr_tasks
;
353 strcpy(task
->comm
, comm
);
355 * every task starts in sleeping state - this gets ignored
356 * if there's no wakeup pointing to this sleep state:
358 add_sched_event_sleep(sched
, task
, 0, 0);
360 sched
->pid_to_task
[pid
] = task
;
362 sched
->tasks
= realloc(sched
->tasks
, sched
->nr_tasks
* sizeof(struct task_desc
*));
363 BUG_ON(!sched
->tasks
);
364 sched
->tasks
[task
->nr
] = task
;
367 printf("registered task #%ld, PID %ld (%s)\n", sched
->nr_tasks
, pid
, comm
);
373 static void print_task_traces(struct perf_sched
*sched
)
375 struct task_desc
*task
;
378 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
379 task
= sched
->tasks
[i
];
380 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
381 task
->nr
, task
->comm
, task
->pid
, task
->nr_events
);
385 static void add_cross_task_wakeups(struct perf_sched
*sched
)
387 struct task_desc
*task1
, *task2
;
390 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
391 task1
= sched
->tasks
[i
];
393 if (j
== sched
->nr_tasks
)
395 task2
= sched
->tasks
[j
];
396 add_sched_event_wakeup(sched
, task1
, 0, task2
);
400 static void perf_sched__process_event(struct perf_sched
*sched
,
401 struct sched_atom
*atom
)
405 switch (atom
->type
) {
406 case SCHED_EVENT_RUN
:
407 burn_nsecs(sched
, atom
->duration
);
409 case SCHED_EVENT_SLEEP
:
411 ret
= sem_wait(atom
->wait_sem
);
414 case SCHED_EVENT_WAKEUP
:
416 ret
= sem_post(atom
->wait_sem
);
419 case SCHED_EVENT_MIGRATION
:
426 static u64
get_cpu_usage_nsec_parent(void)
432 err
= getrusage(RUSAGE_SELF
, &ru
);
435 sum
= ru
.ru_utime
.tv_sec
*1e9
+ ru
.ru_utime
.tv_usec
*1e3
;
436 sum
+= ru
.ru_stime
.tv_sec
*1e9
+ ru
.ru_stime
.tv_usec
*1e3
;
441 static int self_open_counters(struct perf_sched
*sched
, unsigned long cur_task
)
443 struct perf_event_attr attr
;
444 char sbuf
[STRERR_BUFSIZE
], info
[STRERR_BUFSIZE
];
447 bool need_privilege
= false;
449 memset(&attr
, 0, sizeof(attr
));
451 attr
.type
= PERF_TYPE_SOFTWARE
;
452 attr
.config
= PERF_COUNT_SW_TASK_CLOCK
;
455 fd
= sys_perf_event_open(&attr
, 0, -1, -1,
456 perf_event_open_cloexec_flag());
459 if (errno
== EMFILE
) {
461 BUG_ON(getrlimit(RLIMIT_NOFILE
, &limit
) == -1);
462 limit
.rlim_cur
+= sched
->nr_tasks
- cur_task
;
463 if (limit
.rlim_cur
> limit
.rlim_max
) {
464 limit
.rlim_max
= limit
.rlim_cur
;
465 need_privilege
= true;
467 if (setrlimit(RLIMIT_NOFILE
, &limit
) == -1) {
468 if (need_privilege
&& errno
== EPERM
)
469 strcpy(info
, "Need privilege\n");
473 strcpy(info
, "Have a try with -f option\n");
475 pr_err("Error: sys_perf_event_open() syscall returned "
476 "with %d (%s)\n%s", fd
,
477 strerror_r(errno
, sbuf
, sizeof(sbuf
)), info
);
483 static u64
get_cpu_usage_nsec_self(int fd
)
488 ret
= read(fd
, &runtime
, sizeof(runtime
));
489 BUG_ON(ret
!= sizeof(runtime
));
494 struct sched_thread_parms
{
495 struct task_desc
*task
;
496 struct perf_sched
*sched
;
500 static void *thread_func(void *ctx
)
502 struct sched_thread_parms
*parms
= ctx
;
503 struct task_desc
*this_task
= parms
->task
;
504 struct perf_sched
*sched
= parms
->sched
;
505 u64 cpu_usage_0
, cpu_usage_1
;
506 unsigned long i
, ret
;
512 sprintf(comm2
, ":%s", this_task
->comm
);
513 prctl(PR_SET_NAME
, comm2
);
517 ret
= sem_post(&this_task
->ready_for_work
);
519 ret
= pthread_mutex_lock(&sched
->start_work_mutex
);
521 ret
= pthread_mutex_unlock(&sched
->start_work_mutex
);
524 cpu_usage_0
= get_cpu_usage_nsec_self(fd
);
526 for (i
= 0; i
< this_task
->nr_events
; i
++) {
527 this_task
->curr_event
= i
;
528 perf_sched__process_event(sched
, this_task
->atoms
[i
]);
531 cpu_usage_1
= get_cpu_usage_nsec_self(fd
);
532 this_task
->cpu_usage
= cpu_usage_1
- cpu_usage_0
;
533 ret
= sem_post(&this_task
->work_done_sem
);
536 ret
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
538 ret
= pthread_mutex_unlock(&sched
->work_done_wait_mutex
);
544 static void create_tasks(struct perf_sched
*sched
)
546 struct task_desc
*task
;
551 err
= pthread_attr_init(&attr
);
553 err
= pthread_attr_setstacksize(&attr
,
554 (size_t) max(16 * 1024, PTHREAD_STACK_MIN
));
556 err
= pthread_mutex_lock(&sched
->start_work_mutex
);
558 err
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
560 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
561 struct sched_thread_parms
*parms
= malloc(sizeof(*parms
));
562 BUG_ON(parms
== NULL
);
563 parms
->task
= task
= sched
->tasks
[i
];
564 parms
->sched
= sched
;
565 parms
->fd
= self_open_counters(sched
, i
);
566 sem_init(&task
->sleep_sem
, 0, 0);
567 sem_init(&task
->ready_for_work
, 0, 0);
568 sem_init(&task
->work_done_sem
, 0, 0);
569 task
->curr_event
= 0;
570 err
= pthread_create(&task
->thread
, &attr
, thread_func
, parms
);
575 static void wait_for_tasks(struct perf_sched
*sched
)
577 u64 cpu_usage_0
, cpu_usage_1
;
578 struct task_desc
*task
;
579 unsigned long i
, ret
;
581 sched
->start_time
= get_nsecs();
582 sched
->cpu_usage
= 0;
583 pthread_mutex_unlock(&sched
->work_done_wait_mutex
);
585 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
586 task
= sched
->tasks
[i
];
587 ret
= sem_wait(&task
->ready_for_work
);
589 sem_init(&task
->ready_for_work
, 0, 0);
591 ret
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
594 cpu_usage_0
= get_cpu_usage_nsec_parent();
596 pthread_mutex_unlock(&sched
->start_work_mutex
);
598 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
599 task
= sched
->tasks
[i
];
600 ret
= sem_wait(&task
->work_done_sem
);
602 sem_init(&task
->work_done_sem
, 0, 0);
603 sched
->cpu_usage
+= task
->cpu_usage
;
607 cpu_usage_1
= get_cpu_usage_nsec_parent();
608 if (!sched
->runavg_cpu_usage
)
609 sched
->runavg_cpu_usage
= sched
->cpu_usage
;
610 sched
->runavg_cpu_usage
= (sched
->runavg_cpu_usage
* (sched
->replay_repeat
- 1) + sched
->cpu_usage
) / sched
->replay_repeat
;
612 sched
->parent_cpu_usage
= cpu_usage_1
- cpu_usage_0
;
613 if (!sched
->runavg_parent_cpu_usage
)
614 sched
->runavg_parent_cpu_usage
= sched
->parent_cpu_usage
;
615 sched
->runavg_parent_cpu_usage
= (sched
->runavg_parent_cpu_usage
* (sched
->replay_repeat
- 1) +
616 sched
->parent_cpu_usage
)/sched
->replay_repeat
;
618 ret
= pthread_mutex_lock(&sched
->start_work_mutex
);
621 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
622 task
= sched
->tasks
[i
];
623 sem_init(&task
->sleep_sem
, 0, 0);
624 task
->curr_event
= 0;
628 static void run_one_test(struct perf_sched
*sched
)
630 u64 T0
, T1
, delta
, avg_delta
, fluct
;
633 wait_for_tasks(sched
);
637 sched
->sum_runtime
+= delta
;
640 avg_delta
= sched
->sum_runtime
/ sched
->nr_runs
;
641 if (delta
< avg_delta
)
642 fluct
= avg_delta
- delta
;
644 fluct
= delta
- avg_delta
;
645 sched
->sum_fluct
+= fluct
;
647 sched
->run_avg
= delta
;
648 sched
->run_avg
= (sched
->run_avg
* (sched
->replay_repeat
- 1) + delta
) / sched
->replay_repeat
;
650 printf("#%-3ld: %0.3f, ", sched
->nr_runs
, (double)delta
/ 1000000.0);
652 printf("ravg: %0.2f, ", (double)sched
->run_avg
/ 1e6
);
654 printf("cpu: %0.2f / %0.2f",
655 (double)sched
->cpu_usage
/ 1e6
, (double)sched
->runavg_cpu_usage
/ 1e6
);
659 * rusage statistics done by the parent, these are less
660 * accurate than the sched->sum_exec_runtime based statistics:
662 printf(" [%0.2f / %0.2f]",
663 (double)sched
->parent_cpu_usage
/1e6
,
664 (double)sched
->runavg_parent_cpu_usage
/1e6
);
669 if (sched
->nr_sleep_corrections
)
670 printf(" (%ld sleep corrections)\n", sched
->nr_sleep_corrections
);
671 sched
->nr_sleep_corrections
= 0;
674 static void test_calibrations(struct perf_sched
*sched
)
679 burn_nsecs(sched
, 1e6
);
682 printf("the run test took %" PRIu64
" nsecs\n", T1
- T0
);
688 printf("the sleep test took %" PRIu64
" nsecs\n", T1
- T0
);
692 replay_wakeup_event(struct perf_sched
*sched
,
693 struct perf_evsel
*evsel
, struct perf_sample
*sample
,
694 struct machine
*machine __maybe_unused
)
696 const char *comm
= perf_evsel__strval(evsel
, sample
, "comm");
697 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
698 struct task_desc
*waker
, *wakee
;
701 printf("sched_wakeup event %p\n", evsel
);
703 printf(" ... pid %d woke up %s/%d\n", sample
->tid
, comm
, pid
);
706 waker
= register_pid(sched
, sample
->tid
, "<unknown>");
707 wakee
= register_pid(sched
, pid
, comm
);
709 add_sched_event_wakeup(sched
, waker
, sample
->time
, wakee
);
713 static int replay_switch_event(struct perf_sched
*sched
,
714 struct perf_evsel
*evsel
,
715 struct perf_sample
*sample
,
716 struct machine
*machine __maybe_unused
)
718 const char *prev_comm
= perf_evsel__strval(evsel
, sample
, "prev_comm"),
719 *next_comm
= perf_evsel__strval(evsel
, sample
, "next_comm");
720 const u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
721 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
722 const u64 prev_state
= perf_evsel__intval(evsel
, sample
, "prev_state");
723 struct task_desc
*prev
, __maybe_unused
*next
;
724 u64 timestamp0
, timestamp
= sample
->time
;
725 int cpu
= sample
->cpu
;
729 printf("sched_switch event %p\n", evsel
);
731 if (cpu
>= MAX_CPUS
|| cpu
< 0)
734 timestamp0
= sched
->cpu_last_switched
[cpu
];
736 delta
= timestamp
- timestamp0
;
741 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
745 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64
" nsecs]\n",
746 prev_comm
, prev_pid
, next_comm
, next_pid
, delta
);
748 prev
= register_pid(sched
, prev_pid
, prev_comm
);
749 next
= register_pid(sched
, next_pid
, next_comm
);
751 sched
->cpu_last_switched
[cpu
] = timestamp
;
753 add_sched_event_run(sched
, prev
, timestamp
, delta
);
754 add_sched_event_sleep(sched
, prev
, timestamp
, prev_state
);
759 static int replay_fork_event(struct perf_sched
*sched
,
760 union perf_event
*event
,
761 struct machine
*machine
)
763 struct thread
*child
, *parent
;
765 child
= machine__findnew_thread(machine
, event
->fork
.pid
,
767 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
770 if (child
== NULL
|| parent
== NULL
) {
771 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
777 printf("fork event\n");
778 printf("... parent: %s/%d\n", thread__comm_str(parent
), parent
->tid
);
779 printf("... child: %s/%d\n", thread__comm_str(child
), child
->tid
);
782 register_pid(sched
, parent
->tid
, thread__comm_str(parent
));
783 register_pid(sched
, child
->tid
, thread__comm_str(child
));
787 struct sort_dimension
{
790 struct list_head list
;
794 thread_lat_cmp(struct list_head
*list
, struct work_atoms
*l
, struct work_atoms
*r
)
796 struct sort_dimension
*sort
;
799 BUG_ON(list_empty(list
));
801 list_for_each_entry(sort
, list
, list
) {
802 ret
= sort
->cmp(l
, r
);
810 static struct work_atoms
*
811 thread_atoms_search(struct rb_root
*root
, struct thread
*thread
,
812 struct list_head
*sort_list
)
814 struct rb_node
*node
= root
->rb_node
;
815 struct work_atoms key
= { .thread
= thread
};
818 struct work_atoms
*atoms
;
821 atoms
= container_of(node
, struct work_atoms
, node
);
823 cmp
= thread_lat_cmp(sort_list
, &key
, atoms
);
825 node
= node
->rb_left
;
827 node
= node
->rb_right
;
829 BUG_ON(thread
!= atoms
->thread
);
837 __thread_latency_insert(struct rb_root
*root
, struct work_atoms
*data
,
838 struct list_head
*sort_list
)
840 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
843 struct work_atoms
*this;
846 this = container_of(*new, struct work_atoms
, node
);
849 cmp
= thread_lat_cmp(sort_list
, data
, this);
852 new = &((*new)->rb_left
);
854 new = &((*new)->rb_right
);
857 rb_link_node(&data
->node
, parent
, new);
858 rb_insert_color(&data
->node
, root
);
861 static int thread_atoms_insert(struct perf_sched
*sched
, struct thread
*thread
)
863 struct work_atoms
*atoms
= zalloc(sizeof(*atoms
));
865 pr_err("No memory at %s\n", __func__
);
869 atoms
->thread
= thread__get(thread
);
870 INIT_LIST_HEAD(&atoms
->work_list
);
871 __thread_latency_insert(&sched
->atom_root
, atoms
, &sched
->cmp_pid
);
875 static char sched_out_state(u64 prev_state
)
877 const char *str
= TASK_STATE_TO_CHAR_STR
;
879 return str
[prev_state
];
883 add_sched_out_event(struct work_atoms
*atoms
,
887 struct work_atom
*atom
= zalloc(sizeof(*atom
));
889 pr_err("Non memory at %s", __func__
);
893 atom
->sched_out_time
= timestamp
;
895 if (run_state
== 'R') {
896 atom
->state
= THREAD_WAIT_CPU
;
897 atom
->wake_up_time
= atom
->sched_out_time
;
900 list_add_tail(&atom
->list
, &atoms
->work_list
);
905 add_runtime_event(struct work_atoms
*atoms
, u64 delta
,
906 u64 timestamp __maybe_unused
)
908 struct work_atom
*atom
;
910 BUG_ON(list_empty(&atoms
->work_list
));
912 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
914 atom
->runtime
+= delta
;
915 atoms
->total_runtime
+= delta
;
919 add_sched_in_event(struct work_atoms
*atoms
, u64 timestamp
)
921 struct work_atom
*atom
;
924 if (list_empty(&atoms
->work_list
))
927 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
929 if (atom
->state
!= THREAD_WAIT_CPU
)
932 if (timestamp
< atom
->wake_up_time
) {
933 atom
->state
= THREAD_IGNORE
;
937 atom
->state
= THREAD_SCHED_IN
;
938 atom
->sched_in_time
= timestamp
;
940 delta
= atom
->sched_in_time
- atom
->wake_up_time
;
941 atoms
->total_lat
+= delta
;
942 if (delta
> atoms
->max_lat
) {
943 atoms
->max_lat
= delta
;
944 atoms
->max_lat_at
= timestamp
;
949 static int latency_switch_event(struct perf_sched
*sched
,
950 struct perf_evsel
*evsel
,
951 struct perf_sample
*sample
,
952 struct machine
*machine
)
954 const u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
955 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
956 const u64 prev_state
= perf_evsel__intval(evsel
, sample
, "prev_state");
957 struct work_atoms
*out_events
, *in_events
;
958 struct thread
*sched_out
, *sched_in
;
959 u64 timestamp0
, timestamp
= sample
->time
;
960 int cpu
= sample
->cpu
;
963 BUG_ON(cpu
>= MAX_CPUS
|| cpu
< 0);
965 timestamp0
= sched
->cpu_last_switched
[cpu
];
966 sched
->cpu_last_switched
[cpu
] = timestamp
;
968 delta
= timestamp
- timestamp0
;
973 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
977 sched_out
= machine__findnew_thread(machine
, -1, prev_pid
);
978 sched_in
= machine__findnew_thread(machine
, -1, next_pid
);
980 out_events
= thread_atoms_search(&sched
->atom_root
, sched_out
, &sched
->cmp_pid
);
982 if (thread_atoms_insert(sched
, sched_out
))
984 out_events
= thread_atoms_search(&sched
->atom_root
, sched_out
, &sched
->cmp_pid
);
986 pr_err("out-event: Internal tree error");
990 if (add_sched_out_event(out_events
, sched_out_state(prev_state
), timestamp
))
993 in_events
= thread_atoms_search(&sched
->atom_root
, sched_in
, &sched
->cmp_pid
);
995 if (thread_atoms_insert(sched
, sched_in
))
997 in_events
= thread_atoms_search(&sched
->atom_root
, sched_in
, &sched
->cmp_pid
);
999 pr_err("in-event: Internal tree error");
1003 * Take came in we have not heard about yet,
1004 * add in an initial atom in runnable state:
1006 if (add_sched_out_event(in_events
, 'R', timestamp
))
1009 add_sched_in_event(in_events
, timestamp
);
1014 static int latency_runtime_event(struct perf_sched
*sched
,
1015 struct perf_evsel
*evsel
,
1016 struct perf_sample
*sample
,
1017 struct machine
*machine
)
1019 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
1020 const u64 runtime
= perf_evsel__intval(evsel
, sample
, "runtime");
1021 struct thread
*thread
= machine__findnew_thread(machine
, -1, pid
);
1022 struct work_atoms
*atoms
= thread_atoms_search(&sched
->atom_root
, thread
, &sched
->cmp_pid
);
1023 u64 timestamp
= sample
->time
;
1024 int cpu
= sample
->cpu
;
1026 BUG_ON(cpu
>= MAX_CPUS
|| cpu
< 0);
1028 if (thread_atoms_insert(sched
, thread
))
1030 atoms
= thread_atoms_search(&sched
->atom_root
, thread
, &sched
->cmp_pid
);
1032 pr_err("in-event: Internal tree error");
1035 if (add_sched_out_event(atoms
, 'R', timestamp
))
1039 add_runtime_event(atoms
, runtime
, timestamp
);
1043 static int latency_wakeup_event(struct perf_sched
*sched
,
1044 struct perf_evsel
*evsel
,
1045 struct perf_sample
*sample
,
1046 struct machine
*machine
)
1048 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
1049 struct work_atoms
*atoms
;
1050 struct work_atom
*atom
;
1051 struct thread
*wakee
;
1052 u64 timestamp
= sample
->time
;
1054 wakee
= machine__findnew_thread(machine
, -1, pid
);
1055 atoms
= thread_atoms_search(&sched
->atom_root
, wakee
, &sched
->cmp_pid
);
1057 if (thread_atoms_insert(sched
, wakee
))
1059 atoms
= thread_atoms_search(&sched
->atom_root
, wakee
, &sched
->cmp_pid
);
1061 pr_err("wakeup-event: Internal tree error");
1064 if (add_sched_out_event(atoms
, 'S', timestamp
))
1068 BUG_ON(list_empty(&atoms
->work_list
));
1070 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
1073 * As we do not guarantee the wakeup event happens when
1074 * task is out of run queue, also may happen when task is
1075 * on run queue and wakeup only change ->state to TASK_RUNNING,
1076 * then we should not set the ->wake_up_time when wake up a
1077 * task which is on run queue.
1079 * You WILL be missing events if you've recorded only
1080 * one CPU, or are only looking at only one, so don't
1081 * skip in this case.
1083 if (sched
->profile_cpu
== -1 && atom
->state
!= THREAD_SLEEPING
)
1086 sched
->nr_timestamps
++;
1087 if (atom
->sched_out_time
> timestamp
) {
1088 sched
->nr_unordered_timestamps
++;
1092 atom
->state
= THREAD_WAIT_CPU
;
1093 atom
->wake_up_time
= timestamp
;
1097 static int latency_migrate_task_event(struct perf_sched
*sched
,
1098 struct perf_evsel
*evsel
,
1099 struct perf_sample
*sample
,
1100 struct machine
*machine
)
1102 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
1103 u64 timestamp
= sample
->time
;
1104 struct work_atoms
*atoms
;
1105 struct work_atom
*atom
;
1106 struct thread
*migrant
;
1109 * Only need to worry about migration when profiling one CPU.
1111 if (sched
->profile_cpu
== -1)
1114 migrant
= machine__findnew_thread(machine
, -1, pid
);
1115 atoms
= thread_atoms_search(&sched
->atom_root
, migrant
, &sched
->cmp_pid
);
1117 if (thread_atoms_insert(sched
, migrant
))
1119 register_pid(sched
, migrant
->tid
, thread__comm_str(migrant
));
1120 atoms
= thread_atoms_search(&sched
->atom_root
, migrant
, &sched
->cmp_pid
);
1122 pr_err("migration-event: Internal tree error");
1125 if (add_sched_out_event(atoms
, 'R', timestamp
))
1129 BUG_ON(list_empty(&atoms
->work_list
));
1131 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
1132 atom
->sched_in_time
= atom
->sched_out_time
= atom
->wake_up_time
= timestamp
;
1134 sched
->nr_timestamps
++;
1136 if (atom
->sched_out_time
> timestamp
)
1137 sched
->nr_unordered_timestamps
++;
1142 static void output_lat_thread(struct perf_sched
*sched
, struct work_atoms
*work_list
)
1148 if (!work_list
->nb_atoms
)
1151 * Ignore idle threads:
1153 if (!strcmp(thread__comm_str(work_list
->thread
), "swapper"))
1156 sched
->all_runtime
+= work_list
->total_runtime
;
1157 sched
->all_count
+= work_list
->nb_atoms
;
1159 ret
= printf(" %s:%d ", thread__comm_str(work_list
->thread
), work_list
->thread
->tid
);
1161 for (i
= 0; i
< 24 - ret
; i
++)
1164 avg
= work_list
->total_lat
/ work_list
->nb_atoms
;
1166 printf("|%11.3f ms |%9" PRIu64
" | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
1167 (double)work_list
->total_runtime
/ 1e6
,
1168 work_list
->nb_atoms
, (double)avg
/ 1e6
,
1169 (double)work_list
->max_lat
/ 1e6
,
1170 (double)work_list
->max_lat_at
/ 1e9
);
1173 static int pid_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1175 if (l
->thread
->tid
< r
->thread
->tid
)
1177 if (l
->thread
->tid
> r
->thread
->tid
)
1183 static int avg_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1193 avgl
= l
->total_lat
/ l
->nb_atoms
;
1194 avgr
= r
->total_lat
/ r
->nb_atoms
;
1204 static int max_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1206 if (l
->max_lat
< r
->max_lat
)
1208 if (l
->max_lat
> r
->max_lat
)
1214 static int switch_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1216 if (l
->nb_atoms
< r
->nb_atoms
)
1218 if (l
->nb_atoms
> r
->nb_atoms
)
1224 static int runtime_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1226 if (l
->total_runtime
< r
->total_runtime
)
1228 if (l
->total_runtime
> r
->total_runtime
)
1234 static int sort_dimension__add(const char *tok
, struct list_head
*list
)
1237 static struct sort_dimension avg_sort_dimension
= {
1241 static struct sort_dimension max_sort_dimension
= {
1245 static struct sort_dimension pid_sort_dimension
= {
1249 static struct sort_dimension runtime_sort_dimension
= {
1253 static struct sort_dimension switch_sort_dimension
= {
1257 struct sort_dimension
*available_sorts
[] = {
1258 &pid_sort_dimension
,
1259 &avg_sort_dimension
,
1260 &max_sort_dimension
,
1261 &switch_sort_dimension
,
1262 &runtime_sort_dimension
,
1265 for (i
= 0; i
< ARRAY_SIZE(available_sorts
); i
++) {
1266 if (!strcmp(available_sorts
[i
]->name
, tok
)) {
1267 list_add_tail(&available_sorts
[i
]->list
, list
);
1276 static void perf_sched__sort_lat(struct perf_sched
*sched
)
1278 struct rb_node
*node
;
1281 struct work_atoms
*data
;
1282 node
= rb_first(&sched
->atom_root
);
1286 rb_erase(node
, &sched
->atom_root
);
1287 data
= rb_entry(node
, struct work_atoms
, node
);
1288 __thread_latency_insert(&sched
->sorted_atom_root
, data
, &sched
->sort_list
);
1292 static int process_sched_wakeup_event(struct perf_tool
*tool
,
1293 struct perf_evsel
*evsel
,
1294 struct perf_sample
*sample
,
1295 struct machine
*machine
)
1297 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1299 if (sched
->tp_handler
->wakeup_event
)
1300 return sched
->tp_handler
->wakeup_event(sched
, evsel
, sample
, machine
);
1305 static int map_switch_event(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
1306 struct perf_sample
*sample
, struct machine
*machine
)
1308 const u32 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
1309 struct thread
*sched_in
;
1311 u64 timestamp0
, timestamp
= sample
->time
;
1313 int cpu
, this_cpu
= sample
->cpu
;
1315 BUG_ON(this_cpu
>= MAX_CPUS
|| this_cpu
< 0);
1317 if (this_cpu
> sched
->max_cpu
)
1318 sched
->max_cpu
= this_cpu
;
1320 timestamp0
= sched
->cpu_last_switched
[this_cpu
];
1321 sched
->cpu_last_switched
[this_cpu
] = timestamp
;
1323 delta
= timestamp
- timestamp0
;
1328 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
1332 sched_in
= machine__findnew_thread(machine
, -1, next_pid
);
1334 sched
->curr_thread
[this_cpu
] = sched_in
;
1339 if (!sched_in
->shortname
[0]) {
1340 if (!strcmp(thread__comm_str(sched_in
), "swapper")) {
1342 * Don't allocate a letter-number for swapper:0
1343 * as a shortname. Instead, we use '.' for it.
1345 sched_in
->shortname
[0] = '.';
1346 sched_in
->shortname
[1] = ' ';
1348 sched_in
->shortname
[0] = sched
->next_shortname1
;
1349 sched_in
->shortname
[1] = sched
->next_shortname2
;
1351 if (sched
->next_shortname1
< 'Z') {
1352 sched
->next_shortname1
++;
1354 sched
->next_shortname1
= 'A';
1355 if (sched
->next_shortname2
< '9')
1356 sched
->next_shortname2
++;
1358 sched
->next_shortname2
= '0';
1364 for (cpu
= 0; cpu
<= sched
->max_cpu
; cpu
++) {
1365 if (cpu
!= this_cpu
)
1370 if (sched
->curr_thread
[cpu
])
1371 printf("%2s ", sched
->curr_thread
[cpu
]->shortname
);
1376 printf(" %12.6f secs ", (double)timestamp
/1e9
);
1377 if (new_shortname
) {
1378 printf("%s => %s:%d\n",
1379 sched_in
->shortname
, thread__comm_str(sched_in
), sched_in
->tid
);
1387 static int process_sched_switch_event(struct perf_tool
*tool
,
1388 struct perf_evsel
*evsel
,
1389 struct perf_sample
*sample
,
1390 struct machine
*machine
)
1392 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1393 int this_cpu
= sample
->cpu
, err
= 0;
1394 u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
1395 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
1397 if (sched
->curr_pid
[this_cpu
] != (u32
)-1) {
1399 * Are we trying to switch away a PID that is
1402 if (sched
->curr_pid
[this_cpu
] != prev_pid
)
1403 sched
->nr_context_switch_bugs
++;
1406 if (sched
->tp_handler
->switch_event
)
1407 err
= sched
->tp_handler
->switch_event(sched
, evsel
, sample
, machine
);
1409 sched
->curr_pid
[this_cpu
] = next_pid
;
1413 static int process_sched_runtime_event(struct perf_tool
*tool
,
1414 struct perf_evsel
*evsel
,
1415 struct perf_sample
*sample
,
1416 struct machine
*machine
)
1418 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1420 if (sched
->tp_handler
->runtime_event
)
1421 return sched
->tp_handler
->runtime_event(sched
, evsel
, sample
, machine
);
1426 static int perf_sched__process_fork_event(struct perf_tool
*tool
,
1427 union perf_event
*event
,
1428 struct perf_sample
*sample
,
1429 struct machine
*machine
)
1431 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1433 /* run the fork event through the perf machineruy */
1434 perf_event__process_fork(tool
, event
, sample
, machine
);
1436 /* and then run additional processing needed for this command */
1437 if (sched
->tp_handler
->fork_event
)
1438 return sched
->tp_handler
->fork_event(sched
, event
, machine
);
1443 static int process_sched_migrate_task_event(struct perf_tool
*tool
,
1444 struct perf_evsel
*evsel
,
1445 struct perf_sample
*sample
,
1446 struct machine
*machine
)
1448 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1450 if (sched
->tp_handler
->migrate_task_event
)
1451 return sched
->tp_handler
->migrate_task_event(sched
, evsel
, sample
, machine
);
1456 typedef int (*tracepoint_handler
)(struct perf_tool
*tool
,
1457 struct perf_evsel
*evsel
,
1458 struct perf_sample
*sample
,
1459 struct machine
*machine
);
1461 static int perf_sched__process_tracepoint_sample(struct perf_tool
*tool __maybe_unused
,
1462 union perf_event
*event __maybe_unused
,
1463 struct perf_sample
*sample
,
1464 struct perf_evsel
*evsel
,
1465 struct machine
*machine
)
1469 if (evsel
->handler
!= NULL
) {
1470 tracepoint_handler f
= evsel
->handler
;
1471 err
= f(tool
, evsel
, sample
, machine
);
1477 static int perf_sched__read_events(struct perf_sched
*sched
)
1479 const struct perf_evsel_str_handler handlers
[] = {
1480 { "sched:sched_switch", process_sched_switch_event
, },
1481 { "sched:sched_stat_runtime", process_sched_runtime_event
, },
1482 { "sched:sched_wakeup", process_sched_wakeup_event
, },
1483 { "sched:sched_wakeup_new", process_sched_wakeup_event
, },
1484 { "sched:sched_migrate_task", process_sched_migrate_task_event
, },
1486 struct perf_session
*session
;
1487 struct perf_data_file file
= {
1489 .mode
= PERF_DATA_MODE_READ
,
1490 .force
= sched
->force
,
1494 session
= perf_session__new(&file
, false, &sched
->tool
);
1495 if (session
== NULL
) {
1496 pr_debug("No Memory for session\n");
1500 symbol__init(&session
->header
.env
);
1502 if (perf_session__set_tracepoints_handlers(session
, handlers
))
1505 if (perf_session__has_traces(session
, "record -R")) {
1506 int err
= perf_session__process_events(session
);
1508 pr_err("Failed to process events, error %d", err
);
1512 sched
->nr_events
= session
->evlist
->stats
.nr_events
[0];
1513 sched
->nr_lost_events
= session
->evlist
->stats
.total_lost
;
1514 sched
->nr_lost_chunks
= session
->evlist
->stats
.nr_events
[PERF_RECORD_LOST
];
1519 perf_session__delete(session
);
1523 static void print_bad_events(struct perf_sched
*sched
)
1525 if (sched
->nr_unordered_timestamps
&& sched
->nr_timestamps
) {
1526 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1527 (double)sched
->nr_unordered_timestamps
/(double)sched
->nr_timestamps
*100.0,
1528 sched
->nr_unordered_timestamps
, sched
->nr_timestamps
);
1530 if (sched
->nr_lost_events
&& sched
->nr_events
) {
1531 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1532 (double)sched
->nr_lost_events
/(double)sched
->nr_events
* 100.0,
1533 sched
->nr_lost_events
, sched
->nr_events
, sched
->nr_lost_chunks
);
1535 if (sched
->nr_context_switch_bugs
&& sched
->nr_timestamps
) {
1536 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1537 (double)sched
->nr_context_switch_bugs
/(double)sched
->nr_timestamps
*100.0,
1538 sched
->nr_context_switch_bugs
, sched
->nr_timestamps
);
1539 if (sched
->nr_lost_events
)
1540 printf(" (due to lost events?)");
1545 static int perf_sched__lat(struct perf_sched
*sched
)
1547 struct rb_node
*next
;
1551 if (perf_sched__read_events(sched
))
1554 perf_sched__sort_lat(sched
);
1556 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1557 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1558 printf(" -----------------------------------------------------------------------------------------------------------------\n");
1560 next
= rb_first(&sched
->sorted_atom_root
);
1563 struct work_atoms
*work_list
;
1565 work_list
= rb_entry(next
, struct work_atoms
, node
);
1566 output_lat_thread(sched
, work_list
);
1567 next
= rb_next(next
);
1568 thread__zput(work_list
->thread
);
1571 printf(" -----------------------------------------------------------------------------------------------------------------\n");
1572 printf(" TOTAL: |%11.3f ms |%9" PRIu64
" |\n",
1573 (double)sched
->all_runtime
/ 1e6
, sched
->all_count
);
1575 printf(" ---------------------------------------------------\n");
1577 print_bad_events(sched
);
1583 static int perf_sched__map(struct perf_sched
*sched
)
1585 sched
->max_cpu
= sysconf(_SC_NPROCESSORS_CONF
);
1588 if (perf_sched__read_events(sched
))
1590 print_bad_events(sched
);
1594 static int perf_sched__replay(struct perf_sched
*sched
)
1598 calibrate_run_measurement_overhead(sched
);
1599 calibrate_sleep_measurement_overhead(sched
);
1601 test_calibrations(sched
);
1603 if (perf_sched__read_events(sched
))
1606 printf("nr_run_events: %ld\n", sched
->nr_run_events
);
1607 printf("nr_sleep_events: %ld\n", sched
->nr_sleep_events
);
1608 printf("nr_wakeup_events: %ld\n", sched
->nr_wakeup_events
);
1610 if (sched
->targetless_wakeups
)
1611 printf("target-less wakeups: %ld\n", sched
->targetless_wakeups
);
1612 if (sched
->multitarget_wakeups
)
1613 printf("multi-target wakeups: %ld\n", sched
->multitarget_wakeups
);
1614 if (sched
->nr_run_events_optimized
)
1615 printf("run atoms optimized: %ld\n",
1616 sched
->nr_run_events_optimized
);
1618 print_task_traces(sched
);
1619 add_cross_task_wakeups(sched
);
1621 create_tasks(sched
);
1622 printf("------------------------------------------------------------\n");
1623 for (i
= 0; i
< sched
->replay_repeat
; i
++)
1624 run_one_test(sched
);
1629 static void setup_sorting(struct perf_sched
*sched
, const struct option
*options
,
1630 const char * const usage_msg
[])
1632 char *tmp
, *tok
, *str
= strdup(sched
->sort_order
);
1634 for (tok
= strtok_r(str
, ", ", &tmp
);
1635 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1636 if (sort_dimension__add(tok
, &sched
->sort_list
) < 0) {
1637 error("Unknown --sort key: `%s'", tok
);
1638 usage_with_options(usage_msg
, options
);
1644 sort_dimension__add("pid", &sched
->cmp_pid
);
1647 static int __cmd_record(int argc
, const char **argv
)
1649 unsigned int rec_argc
, i
, j
;
1650 const char **rec_argv
;
1651 const char * const record_args
[] = {
1657 "-e", "sched:sched_switch",
1658 "-e", "sched:sched_stat_wait",
1659 "-e", "sched:sched_stat_sleep",
1660 "-e", "sched:sched_stat_iowait",
1661 "-e", "sched:sched_stat_runtime",
1662 "-e", "sched:sched_process_fork",
1663 "-e", "sched:sched_wakeup",
1664 "-e", "sched:sched_wakeup_new",
1665 "-e", "sched:sched_migrate_task",
1668 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
1669 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1671 if (rec_argv
== NULL
)
1674 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
1675 rec_argv
[i
] = strdup(record_args
[i
]);
1677 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
1678 rec_argv
[i
] = argv
[j
];
1680 BUG_ON(i
!= rec_argc
);
1682 return cmd_record(i
, rec_argv
, NULL
);
1685 int cmd_sched(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1687 const char default_sort_order
[] = "avg, max, switch, runtime";
1688 struct perf_sched sched
= {
1690 .sample
= perf_sched__process_tracepoint_sample
,
1691 .comm
= perf_event__process_comm
,
1692 .lost
= perf_event__process_lost
,
1693 .fork
= perf_sched__process_fork_event
,
1694 .ordered_events
= true,
1696 .cmp_pid
= LIST_HEAD_INIT(sched
.cmp_pid
),
1697 .sort_list
= LIST_HEAD_INIT(sched
.sort_list
),
1698 .start_work_mutex
= PTHREAD_MUTEX_INITIALIZER
,
1699 .work_done_wait_mutex
= PTHREAD_MUTEX_INITIALIZER
,
1700 .sort_order
= default_sort_order
,
1701 .replay_repeat
= 10,
1703 .next_shortname1
= 'A',
1704 .next_shortname2
= '0',
1706 const struct option latency_options
[] = {
1707 OPT_STRING('s', "sort", &sched
.sort_order
, "key[,key2...]",
1708 "sort by key(s): runtime, switch, avg, max"),
1709 OPT_INCR('v', "verbose", &verbose
,
1710 "be more verbose (show symbol address, etc)"),
1711 OPT_INTEGER('C', "CPU", &sched
.profile_cpu
,
1712 "CPU to profile on"),
1713 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1714 "dump raw trace in ASCII"),
1717 const struct option replay_options
[] = {
1718 OPT_UINTEGER('r', "repeat", &sched
.replay_repeat
,
1719 "repeat the workload replay N times (-1: infinite)"),
1720 OPT_INCR('v', "verbose", &verbose
,
1721 "be more verbose (show symbol address, etc)"),
1722 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1723 "dump raw trace in ASCII"),
1724 OPT_BOOLEAN('f', "force", &sched
.force
, "don't complain, do it"),
1727 const struct option sched_options
[] = {
1728 OPT_STRING('i', "input", &input_name
, "file",
1730 OPT_INCR('v', "verbose", &verbose
,
1731 "be more verbose (show symbol address, etc)"),
1732 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1733 "dump raw trace in ASCII"),
1736 const char * const latency_usage
[] = {
1737 "perf sched latency [<options>]",
1740 const char * const replay_usage
[] = {
1741 "perf sched replay [<options>]",
1744 const char *const sched_subcommands
[] = { "record", "latency", "map",
1745 "replay", "script", NULL
};
1746 const char *sched_usage
[] = {
1750 struct trace_sched_handler lat_ops
= {
1751 .wakeup_event
= latency_wakeup_event
,
1752 .switch_event
= latency_switch_event
,
1753 .runtime_event
= latency_runtime_event
,
1754 .migrate_task_event
= latency_migrate_task_event
,
1756 struct trace_sched_handler map_ops
= {
1757 .switch_event
= map_switch_event
,
1759 struct trace_sched_handler replay_ops
= {
1760 .wakeup_event
= replay_wakeup_event
,
1761 .switch_event
= replay_switch_event
,
1762 .fork_event
= replay_fork_event
,
1766 for (i
= 0; i
< ARRAY_SIZE(sched
.curr_pid
); i
++)
1767 sched
.curr_pid
[i
] = -1;
1769 argc
= parse_options_subcommand(argc
, argv
, sched_options
, sched_subcommands
,
1770 sched_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
1772 usage_with_options(sched_usage
, sched_options
);
1775 * Aliased to 'perf script' for now:
1777 if (!strcmp(argv
[0], "script"))
1778 return cmd_script(argc
, argv
, prefix
);
1780 if (!strncmp(argv
[0], "rec", 3)) {
1781 return __cmd_record(argc
, argv
);
1782 } else if (!strncmp(argv
[0], "lat", 3)) {
1783 sched
.tp_handler
= &lat_ops
;
1785 argc
= parse_options(argc
, argv
, latency_options
, latency_usage
, 0);
1787 usage_with_options(latency_usage
, latency_options
);
1789 setup_sorting(&sched
, latency_options
, latency_usage
);
1790 return perf_sched__lat(&sched
);
1791 } else if (!strcmp(argv
[0], "map")) {
1792 sched
.tp_handler
= &map_ops
;
1793 setup_sorting(&sched
, latency_options
, latency_usage
);
1794 return perf_sched__map(&sched
);
1795 } else if (!strncmp(argv
[0], "rep", 3)) {
1796 sched
.tp_handler
= &replay_ops
;
1798 argc
= parse_options(argc
, argv
, replay_options
, replay_usage
, 0);
1800 usage_with_options(replay_usage
, replay_options
);
1802 return perf_sched__replay(&sched
);
1804 usage_with_options(sched_usage
, sched_options
);