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"
14 #include "util/thread_map.h"
15 #include "util/color.h"
17 #include <subcmd/parse-options.h>
18 #include "util/trace-event.h"
20 #include "util/debug.h"
22 #include <sys/prctl.h>
23 #include <sys/resource.h>
25 #include <semaphore.h>
28 #include <api/fs/fs.h>
29 #include <linux/time64.h>
31 #define PR_SET_NAME 15 /* Set process name */
35 #define MAX_PID 1024000
44 unsigned long nr_events
;
45 unsigned long curr_event
;
46 struct sched_atom
**atoms
;
57 enum sched_event_type
{
61 SCHED_EVENT_MIGRATION
,
65 enum sched_event_type type
;
71 struct task_desc
*wakee
;
74 #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
84 struct list_head list
;
85 enum thread_state state
;
93 struct list_head work_list
;
94 struct thread
*thread
;
104 typedef int (*sort_fn_t
)(struct work_atoms
*, struct work_atoms
*);
108 struct trace_sched_handler
{
109 int (*switch_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
110 struct perf_sample
*sample
, struct machine
*machine
);
112 int (*runtime_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
113 struct perf_sample
*sample
, struct machine
*machine
);
115 int (*wakeup_event
)(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
116 struct perf_sample
*sample
, struct machine
*machine
);
118 /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */
119 int (*fork_event
)(struct perf_sched
*sched
, union perf_event
*event
,
120 struct machine
*machine
);
122 int (*migrate_task_event
)(struct perf_sched
*sched
,
123 struct perf_evsel
*evsel
,
124 struct perf_sample
*sample
,
125 struct machine
*machine
);
128 #define COLOR_PIDS PERF_COLOR_BLUE
129 #define COLOR_CPUS PERF_COLOR_BG_RED
131 struct perf_sched_map
{
132 DECLARE_BITMAP(comp_cpus_mask
, MAX_CPUS
);
135 struct thread_map
*color_pids
;
136 const char *color_pids_str
;
137 struct cpu_map
*color_cpus
;
138 const char *color_cpus_str
;
139 struct cpu_map
*cpus
;
140 const char *cpus_str
;
144 struct perf_tool tool
;
145 const char *sort_order
;
146 unsigned long nr_tasks
;
147 struct task_desc
**pid_to_task
;
148 struct task_desc
**tasks
;
149 const struct trace_sched_handler
*tp_handler
;
150 pthread_mutex_t start_work_mutex
;
151 pthread_mutex_t work_done_wait_mutex
;
154 * Track the current task - that way we can know whether there's any
155 * weird events, such as a task being switched away that is not current.
158 u32 curr_pid
[MAX_CPUS
];
159 struct thread
*curr_thread
[MAX_CPUS
];
160 char next_shortname1
;
161 char next_shortname2
;
162 unsigned int replay_repeat
;
163 unsigned long nr_run_events
;
164 unsigned long nr_sleep_events
;
165 unsigned long nr_wakeup_events
;
166 unsigned long nr_sleep_corrections
;
167 unsigned long nr_run_events_optimized
;
168 unsigned long targetless_wakeups
;
169 unsigned long multitarget_wakeups
;
170 unsigned long nr_runs
;
171 unsigned long nr_timestamps
;
172 unsigned long nr_unordered_timestamps
;
173 unsigned long nr_context_switch_bugs
;
174 unsigned long nr_events
;
175 unsigned long nr_lost_chunks
;
176 unsigned long nr_lost_events
;
177 u64 run_measurement_overhead
;
178 u64 sleep_measurement_overhead
;
181 u64 runavg_cpu_usage
;
182 u64 parent_cpu_usage
;
183 u64 runavg_parent_cpu_usage
;
189 u64 cpu_last_switched
[MAX_CPUS
];
190 struct rb_root atom_root
, sorted_atom_root
, merged_atom_root
;
191 struct list_head sort_list
, cmp_pid
;
194 struct perf_sched_map map
;
197 static u64
get_nsecs(void)
201 clock_gettime(CLOCK_MONOTONIC
, &ts
);
203 return ts
.tv_sec
* NSEC_PER_SEC
+ ts
.tv_nsec
;
206 static void burn_nsecs(struct perf_sched
*sched
, u64 nsecs
)
208 u64 T0
= get_nsecs(), T1
;
212 } while (T1
+ sched
->run_measurement_overhead
< T0
+ nsecs
);
215 static void sleep_nsecs(u64 nsecs
)
219 ts
.tv_nsec
= nsecs
% 999999999;
220 ts
.tv_sec
= nsecs
/ 999999999;
222 nanosleep(&ts
, NULL
);
225 static void calibrate_run_measurement_overhead(struct perf_sched
*sched
)
227 u64 T0
, T1
, delta
, min_delta
= NSEC_PER_SEC
;
230 for (i
= 0; i
< 10; i
++) {
232 burn_nsecs(sched
, 0);
235 min_delta
= min(min_delta
, delta
);
237 sched
->run_measurement_overhead
= min_delta
;
239 printf("run measurement overhead: %" PRIu64
" nsecs\n", min_delta
);
242 static void calibrate_sleep_measurement_overhead(struct perf_sched
*sched
)
244 u64 T0
, T1
, delta
, min_delta
= NSEC_PER_SEC
;
247 for (i
= 0; i
< 10; i
++) {
252 min_delta
= min(min_delta
, delta
);
255 sched
->sleep_measurement_overhead
= min_delta
;
257 printf("sleep measurement overhead: %" PRIu64
" nsecs\n", min_delta
);
260 static struct sched_atom
*
261 get_new_event(struct task_desc
*task
, u64 timestamp
)
263 struct sched_atom
*event
= zalloc(sizeof(*event
));
264 unsigned long idx
= task
->nr_events
;
267 event
->timestamp
= timestamp
;
271 size
= sizeof(struct sched_atom
*) * task
->nr_events
;
272 task
->atoms
= realloc(task
->atoms
, size
);
273 BUG_ON(!task
->atoms
);
275 task
->atoms
[idx
] = event
;
280 static struct sched_atom
*last_event(struct task_desc
*task
)
282 if (!task
->nr_events
)
285 return task
->atoms
[task
->nr_events
- 1];
288 static void add_sched_event_run(struct perf_sched
*sched
, struct task_desc
*task
,
289 u64 timestamp
, u64 duration
)
291 struct sched_atom
*event
, *curr_event
= last_event(task
);
294 * optimize an existing RUN event by merging this one
297 if (curr_event
&& curr_event
->type
== SCHED_EVENT_RUN
) {
298 sched
->nr_run_events_optimized
++;
299 curr_event
->duration
+= duration
;
303 event
= get_new_event(task
, timestamp
);
305 event
->type
= SCHED_EVENT_RUN
;
306 event
->duration
= duration
;
308 sched
->nr_run_events
++;
311 static void add_sched_event_wakeup(struct perf_sched
*sched
, struct task_desc
*task
,
312 u64 timestamp
, struct task_desc
*wakee
)
314 struct sched_atom
*event
, *wakee_event
;
316 event
= get_new_event(task
, timestamp
);
317 event
->type
= SCHED_EVENT_WAKEUP
;
318 event
->wakee
= wakee
;
320 wakee_event
= last_event(wakee
);
321 if (!wakee_event
|| wakee_event
->type
!= SCHED_EVENT_SLEEP
) {
322 sched
->targetless_wakeups
++;
325 if (wakee_event
->wait_sem
) {
326 sched
->multitarget_wakeups
++;
330 wakee_event
->wait_sem
= zalloc(sizeof(*wakee_event
->wait_sem
));
331 sem_init(wakee_event
->wait_sem
, 0, 0);
332 wakee_event
->specific_wait
= 1;
333 event
->wait_sem
= wakee_event
->wait_sem
;
335 sched
->nr_wakeup_events
++;
338 static void add_sched_event_sleep(struct perf_sched
*sched
, struct task_desc
*task
,
339 u64 timestamp
, u64 task_state __maybe_unused
)
341 struct sched_atom
*event
= get_new_event(task
, timestamp
);
343 event
->type
= SCHED_EVENT_SLEEP
;
345 sched
->nr_sleep_events
++;
348 static struct task_desc
*register_pid(struct perf_sched
*sched
,
349 unsigned long pid
, const char *comm
)
351 struct task_desc
*task
;
354 if (sched
->pid_to_task
== NULL
) {
355 if (sysctl__read_int("kernel/pid_max", &pid_max
) < 0)
357 BUG_ON((sched
->pid_to_task
= calloc(pid_max
, sizeof(struct task_desc
*))) == NULL
);
359 if (pid
>= (unsigned long)pid_max
) {
360 BUG_ON((sched
->pid_to_task
= realloc(sched
->pid_to_task
, (pid
+ 1) *
361 sizeof(struct task_desc
*))) == NULL
);
362 while (pid
>= (unsigned long)pid_max
)
363 sched
->pid_to_task
[pid_max
++] = NULL
;
366 task
= sched
->pid_to_task
[pid
];
371 task
= zalloc(sizeof(*task
));
373 task
->nr
= sched
->nr_tasks
;
374 strcpy(task
->comm
, comm
);
376 * every task starts in sleeping state - this gets ignored
377 * if there's no wakeup pointing to this sleep state:
379 add_sched_event_sleep(sched
, task
, 0, 0);
381 sched
->pid_to_task
[pid
] = task
;
383 sched
->tasks
= realloc(sched
->tasks
, sched
->nr_tasks
* sizeof(struct task_desc
*));
384 BUG_ON(!sched
->tasks
);
385 sched
->tasks
[task
->nr
] = task
;
388 printf("registered task #%ld, PID %ld (%s)\n", sched
->nr_tasks
, pid
, comm
);
394 static void print_task_traces(struct perf_sched
*sched
)
396 struct task_desc
*task
;
399 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
400 task
= sched
->tasks
[i
];
401 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
402 task
->nr
, task
->comm
, task
->pid
, task
->nr_events
);
406 static void add_cross_task_wakeups(struct perf_sched
*sched
)
408 struct task_desc
*task1
, *task2
;
411 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
412 task1
= sched
->tasks
[i
];
414 if (j
== sched
->nr_tasks
)
416 task2
= sched
->tasks
[j
];
417 add_sched_event_wakeup(sched
, task1
, 0, task2
);
421 static void perf_sched__process_event(struct perf_sched
*sched
,
422 struct sched_atom
*atom
)
426 switch (atom
->type
) {
427 case SCHED_EVENT_RUN
:
428 burn_nsecs(sched
, atom
->duration
);
430 case SCHED_EVENT_SLEEP
:
432 ret
= sem_wait(atom
->wait_sem
);
435 case SCHED_EVENT_WAKEUP
:
437 ret
= sem_post(atom
->wait_sem
);
440 case SCHED_EVENT_MIGRATION
:
447 static u64
get_cpu_usage_nsec_parent(void)
453 err
= getrusage(RUSAGE_SELF
, &ru
);
456 sum
= ru
.ru_utime
.tv_sec
* NSEC_PER_SEC
+ ru
.ru_utime
.tv_usec
* NSEC_PER_USEC
;
457 sum
+= ru
.ru_stime
.tv_sec
* NSEC_PER_SEC
+ ru
.ru_stime
.tv_usec
* NSEC_PER_USEC
;
462 static int self_open_counters(struct perf_sched
*sched
, unsigned long cur_task
)
464 struct perf_event_attr attr
;
465 char sbuf
[STRERR_BUFSIZE
], info
[STRERR_BUFSIZE
];
468 bool need_privilege
= false;
470 memset(&attr
, 0, sizeof(attr
));
472 attr
.type
= PERF_TYPE_SOFTWARE
;
473 attr
.config
= PERF_COUNT_SW_TASK_CLOCK
;
476 fd
= sys_perf_event_open(&attr
, 0, -1, -1,
477 perf_event_open_cloexec_flag());
480 if (errno
== EMFILE
) {
482 BUG_ON(getrlimit(RLIMIT_NOFILE
, &limit
) == -1);
483 limit
.rlim_cur
+= sched
->nr_tasks
- cur_task
;
484 if (limit
.rlim_cur
> limit
.rlim_max
) {
485 limit
.rlim_max
= limit
.rlim_cur
;
486 need_privilege
= true;
488 if (setrlimit(RLIMIT_NOFILE
, &limit
) == -1) {
489 if (need_privilege
&& errno
== EPERM
)
490 strcpy(info
, "Need privilege\n");
494 strcpy(info
, "Have a try with -f option\n");
496 pr_err("Error: sys_perf_event_open() syscall returned "
497 "with %d (%s)\n%s", fd
,
498 str_error_r(errno
, sbuf
, sizeof(sbuf
)), info
);
504 static u64
get_cpu_usage_nsec_self(int fd
)
509 ret
= read(fd
, &runtime
, sizeof(runtime
));
510 BUG_ON(ret
!= sizeof(runtime
));
515 struct sched_thread_parms
{
516 struct task_desc
*task
;
517 struct perf_sched
*sched
;
521 static void *thread_func(void *ctx
)
523 struct sched_thread_parms
*parms
= ctx
;
524 struct task_desc
*this_task
= parms
->task
;
525 struct perf_sched
*sched
= parms
->sched
;
526 u64 cpu_usage_0
, cpu_usage_1
;
527 unsigned long i
, ret
;
533 sprintf(comm2
, ":%s", this_task
->comm
);
534 prctl(PR_SET_NAME
, comm2
);
538 ret
= sem_post(&this_task
->ready_for_work
);
540 ret
= pthread_mutex_lock(&sched
->start_work_mutex
);
542 ret
= pthread_mutex_unlock(&sched
->start_work_mutex
);
545 cpu_usage_0
= get_cpu_usage_nsec_self(fd
);
547 for (i
= 0; i
< this_task
->nr_events
; i
++) {
548 this_task
->curr_event
= i
;
549 perf_sched__process_event(sched
, this_task
->atoms
[i
]);
552 cpu_usage_1
= get_cpu_usage_nsec_self(fd
);
553 this_task
->cpu_usage
= cpu_usage_1
- cpu_usage_0
;
554 ret
= sem_post(&this_task
->work_done_sem
);
557 ret
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
559 ret
= pthread_mutex_unlock(&sched
->work_done_wait_mutex
);
565 static void create_tasks(struct perf_sched
*sched
)
567 struct task_desc
*task
;
572 err
= pthread_attr_init(&attr
);
574 err
= pthread_attr_setstacksize(&attr
,
575 (size_t) max(16 * 1024, PTHREAD_STACK_MIN
));
577 err
= pthread_mutex_lock(&sched
->start_work_mutex
);
579 err
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
581 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
582 struct sched_thread_parms
*parms
= malloc(sizeof(*parms
));
583 BUG_ON(parms
== NULL
);
584 parms
->task
= task
= sched
->tasks
[i
];
585 parms
->sched
= sched
;
586 parms
->fd
= self_open_counters(sched
, i
);
587 sem_init(&task
->sleep_sem
, 0, 0);
588 sem_init(&task
->ready_for_work
, 0, 0);
589 sem_init(&task
->work_done_sem
, 0, 0);
590 task
->curr_event
= 0;
591 err
= pthread_create(&task
->thread
, &attr
, thread_func
, parms
);
596 static void wait_for_tasks(struct perf_sched
*sched
)
598 u64 cpu_usage_0
, cpu_usage_1
;
599 struct task_desc
*task
;
600 unsigned long i
, ret
;
602 sched
->start_time
= get_nsecs();
603 sched
->cpu_usage
= 0;
604 pthread_mutex_unlock(&sched
->work_done_wait_mutex
);
606 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
607 task
= sched
->tasks
[i
];
608 ret
= sem_wait(&task
->ready_for_work
);
610 sem_init(&task
->ready_for_work
, 0, 0);
612 ret
= pthread_mutex_lock(&sched
->work_done_wait_mutex
);
615 cpu_usage_0
= get_cpu_usage_nsec_parent();
617 pthread_mutex_unlock(&sched
->start_work_mutex
);
619 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
620 task
= sched
->tasks
[i
];
621 ret
= sem_wait(&task
->work_done_sem
);
623 sem_init(&task
->work_done_sem
, 0, 0);
624 sched
->cpu_usage
+= task
->cpu_usage
;
628 cpu_usage_1
= get_cpu_usage_nsec_parent();
629 if (!sched
->runavg_cpu_usage
)
630 sched
->runavg_cpu_usage
= sched
->cpu_usage
;
631 sched
->runavg_cpu_usage
= (sched
->runavg_cpu_usage
* (sched
->replay_repeat
- 1) + sched
->cpu_usage
) / sched
->replay_repeat
;
633 sched
->parent_cpu_usage
= cpu_usage_1
- cpu_usage_0
;
634 if (!sched
->runavg_parent_cpu_usage
)
635 sched
->runavg_parent_cpu_usage
= sched
->parent_cpu_usage
;
636 sched
->runavg_parent_cpu_usage
= (sched
->runavg_parent_cpu_usage
* (sched
->replay_repeat
- 1) +
637 sched
->parent_cpu_usage
)/sched
->replay_repeat
;
639 ret
= pthread_mutex_lock(&sched
->start_work_mutex
);
642 for (i
= 0; i
< sched
->nr_tasks
; i
++) {
643 task
= sched
->tasks
[i
];
644 sem_init(&task
->sleep_sem
, 0, 0);
645 task
->curr_event
= 0;
649 static void run_one_test(struct perf_sched
*sched
)
651 u64 T0
, T1
, delta
, avg_delta
, fluct
;
654 wait_for_tasks(sched
);
658 sched
->sum_runtime
+= delta
;
661 avg_delta
= sched
->sum_runtime
/ sched
->nr_runs
;
662 if (delta
< avg_delta
)
663 fluct
= avg_delta
- delta
;
665 fluct
= delta
- avg_delta
;
666 sched
->sum_fluct
+= fluct
;
668 sched
->run_avg
= delta
;
669 sched
->run_avg
= (sched
->run_avg
* (sched
->replay_repeat
- 1) + delta
) / sched
->replay_repeat
;
671 printf("#%-3ld: %0.3f, ", sched
->nr_runs
, (double)delta
/ NSEC_PER_MSEC
);
673 printf("ravg: %0.2f, ", (double)sched
->run_avg
/ NSEC_PER_MSEC
);
675 printf("cpu: %0.2f / %0.2f",
676 (double)sched
->cpu_usage
/ NSEC_PER_MSEC
, (double)sched
->runavg_cpu_usage
/ NSEC_PER_MSEC
);
680 * rusage statistics done by the parent, these are less
681 * accurate than the sched->sum_exec_runtime based statistics:
683 printf(" [%0.2f / %0.2f]",
684 (double)sched
->parent_cpu_usage
/ NSEC_PER_MSEC
,
685 (double)sched
->runavg_parent_cpu_usage
/ NSEC_PER_MSEC
);
690 if (sched
->nr_sleep_corrections
)
691 printf(" (%ld sleep corrections)\n", sched
->nr_sleep_corrections
);
692 sched
->nr_sleep_corrections
= 0;
695 static void test_calibrations(struct perf_sched
*sched
)
700 burn_nsecs(sched
, NSEC_PER_MSEC
);
703 printf("the run test took %" PRIu64
" nsecs\n", T1
- T0
);
706 sleep_nsecs(NSEC_PER_MSEC
);
709 printf("the sleep test took %" PRIu64
" nsecs\n", T1
- T0
);
713 replay_wakeup_event(struct perf_sched
*sched
,
714 struct perf_evsel
*evsel
, struct perf_sample
*sample
,
715 struct machine
*machine __maybe_unused
)
717 const char *comm
= perf_evsel__strval(evsel
, sample
, "comm");
718 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
719 struct task_desc
*waker
, *wakee
;
722 printf("sched_wakeup event %p\n", evsel
);
724 printf(" ... pid %d woke up %s/%d\n", sample
->tid
, comm
, pid
);
727 waker
= register_pid(sched
, sample
->tid
, "<unknown>");
728 wakee
= register_pid(sched
, pid
, comm
);
730 add_sched_event_wakeup(sched
, waker
, sample
->time
, wakee
);
734 static int replay_switch_event(struct perf_sched
*sched
,
735 struct perf_evsel
*evsel
,
736 struct perf_sample
*sample
,
737 struct machine
*machine __maybe_unused
)
739 const char *prev_comm
= perf_evsel__strval(evsel
, sample
, "prev_comm"),
740 *next_comm
= perf_evsel__strval(evsel
, sample
, "next_comm");
741 const u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
742 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
743 const u64 prev_state
= perf_evsel__intval(evsel
, sample
, "prev_state");
744 struct task_desc
*prev
, __maybe_unused
*next
;
745 u64 timestamp0
, timestamp
= sample
->time
;
746 int cpu
= sample
->cpu
;
750 printf("sched_switch event %p\n", evsel
);
752 if (cpu
>= MAX_CPUS
|| cpu
< 0)
755 timestamp0
= sched
->cpu_last_switched
[cpu
];
757 delta
= timestamp
- timestamp0
;
762 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
766 pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64
" nsecs]\n",
767 prev_comm
, prev_pid
, next_comm
, next_pid
, delta
);
769 prev
= register_pid(sched
, prev_pid
, prev_comm
);
770 next
= register_pid(sched
, next_pid
, next_comm
);
772 sched
->cpu_last_switched
[cpu
] = timestamp
;
774 add_sched_event_run(sched
, prev
, timestamp
, delta
);
775 add_sched_event_sleep(sched
, prev
, timestamp
, prev_state
);
780 static int replay_fork_event(struct perf_sched
*sched
,
781 union perf_event
*event
,
782 struct machine
*machine
)
784 struct thread
*child
, *parent
;
786 child
= machine__findnew_thread(machine
, event
->fork
.pid
,
788 parent
= machine__findnew_thread(machine
, event
->fork
.ppid
,
791 if (child
== NULL
|| parent
== NULL
) {
792 pr_debug("thread does not exist on fork event: child %p, parent %p\n",
798 printf("fork event\n");
799 printf("... parent: %s/%d\n", thread__comm_str(parent
), parent
->tid
);
800 printf("... child: %s/%d\n", thread__comm_str(child
), child
->tid
);
803 register_pid(sched
, parent
->tid
, thread__comm_str(parent
));
804 register_pid(sched
, child
->tid
, thread__comm_str(child
));
811 struct sort_dimension
{
814 struct list_head list
;
818 thread_lat_cmp(struct list_head
*list
, struct work_atoms
*l
, struct work_atoms
*r
)
820 struct sort_dimension
*sort
;
823 BUG_ON(list_empty(list
));
825 list_for_each_entry(sort
, list
, list
) {
826 ret
= sort
->cmp(l
, r
);
834 static struct work_atoms
*
835 thread_atoms_search(struct rb_root
*root
, struct thread
*thread
,
836 struct list_head
*sort_list
)
838 struct rb_node
*node
= root
->rb_node
;
839 struct work_atoms key
= { .thread
= thread
};
842 struct work_atoms
*atoms
;
845 atoms
= container_of(node
, struct work_atoms
, node
);
847 cmp
= thread_lat_cmp(sort_list
, &key
, atoms
);
849 node
= node
->rb_left
;
851 node
= node
->rb_right
;
853 BUG_ON(thread
!= atoms
->thread
);
861 __thread_latency_insert(struct rb_root
*root
, struct work_atoms
*data
,
862 struct list_head
*sort_list
)
864 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
867 struct work_atoms
*this;
870 this = container_of(*new, struct work_atoms
, node
);
873 cmp
= thread_lat_cmp(sort_list
, data
, this);
876 new = &((*new)->rb_left
);
878 new = &((*new)->rb_right
);
881 rb_link_node(&data
->node
, parent
, new);
882 rb_insert_color(&data
->node
, root
);
885 static int thread_atoms_insert(struct perf_sched
*sched
, struct thread
*thread
)
887 struct work_atoms
*atoms
= zalloc(sizeof(*atoms
));
889 pr_err("No memory at %s\n", __func__
);
893 atoms
->thread
= thread__get(thread
);
894 INIT_LIST_HEAD(&atoms
->work_list
);
895 __thread_latency_insert(&sched
->atom_root
, atoms
, &sched
->cmp_pid
);
899 static char sched_out_state(u64 prev_state
)
901 const char *str
= TASK_STATE_TO_CHAR_STR
;
903 return str
[prev_state
];
907 add_sched_out_event(struct work_atoms
*atoms
,
911 struct work_atom
*atom
= zalloc(sizeof(*atom
));
913 pr_err("Non memory at %s", __func__
);
917 atom
->sched_out_time
= timestamp
;
919 if (run_state
== 'R') {
920 atom
->state
= THREAD_WAIT_CPU
;
921 atom
->wake_up_time
= atom
->sched_out_time
;
924 list_add_tail(&atom
->list
, &atoms
->work_list
);
929 add_runtime_event(struct work_atoms
*atoms
, u64 delta
,
930 u64 timestamp __maybe_unused
)
932 struct work_atom
*atom
;
934 BUG_ON(list_empty(&atoms
->work_list
));
936 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
938 atom
->runtime
+= delta
;
939 atoms
->total_runtime
+= delta
;
943 add_sched_in_event(struct work_atoms
*atoms
, u64 timestamp
)
945 struct work_atom
*atom
;
948 if (list_empty(&atoms
->work_list
))
951 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
953 if (atom
->state
!= THREAD_WAIT_CPU
)
956 if (timestamp
< atom
->wake_up_time
) {
957 atom
->state
= THREAD_IGNORE
;
961 atom
->state
= THREAD_SCHED_IN
;
962 atom
->sched_in_time
= timestamp
;
964 delta
= atom
->sched_in_time
- atom
->wake_up_time
;
965 atoms
->total_lat
+= delta
;
966 if (delta
> atoms
->max_lat
) {
967 atoms
->max_lat
= delta
;
968 atoms
->max_lat_at
= timestamp
;
973 static int latency_switch_event(struct perf_sched
*sched
,
974 struct perf_evsel
*evsel
,
975 struct perf_sample
*sample
,
976 struct machine
*machine
)
978 const u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
979 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
980 const u64 prev_state
= perf_evsel__intval(evsel
, sample
, "prev_state");
981 struct work_atoms
*out_events
, *in_events
;
982 struct thread
*sched_out
, *sched_in
;
983 u64 timestamp0
, timestamp
= sample
->time
;
984 int cpu
= sample
->cpu
, err
= -1;
987 BUG_ON(cpu
>= MAX_CPUS
|| cpu
< 0);
989 timestamp0
= sched
->cpu_last_switched
[cpu
];
990 sched
->cpu_last_switched
[cpu
] = timestamp
;
992 delta
= timestamp
- timestamp0
;
997 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
1001 sched_out
= machine__findnew_thread(machine
, -1, prev_pid
);
1002 sched_in
= machine__findnew_thread(machine
, -1, next_pid
);
1003 if (sched_out
== NULL
|| sched_in
== NULL
)
1006 out_events
= thread_atoms_search(&sched
->atom_root
, sched_out
, &sched
->cmp_pid
);
1008 if (thread_atoms_insert(sched
, sched_out
))
1010 out_events
= thread_atoms_search(&sched
->atom_root
, sched_out
, &sched
->cmp_pid
);
1012 pr_err("out-event: Internal tree error");
1016 if (add_sched_out_event(out_events
, sched_out_state(prev_state
), timestamp
))
1019 in_events
= thread_atoms_search(&sched
->atom_root
, sched_in
, &sched
->cmp_pid
);
1021 if (thread_atoms_insert(sched
, sched_in
))
1023 in_events
= thread_atoms_search(&sched
->atom_root
, sched_in
, &sched
->cmp_pid
);
1025 pr_err("in-event: Internal tree error");
1029 * Take came in we have not heard about yet,
1030 * add in an initial atom in runnable state:
1032 if (add_sched_out_event(in_events
, 'R', timestamp
))
1035 add_sched_in_event(in_events
, timestamp
);
1038 thread__put(sched_out
);
1039 thread__put(sched_in
);
1043 static int latency_runtime_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 const u64 runtime
= perf_evsel__intval(evsel
, sample
, "runtime");
1050 struct thread
*thread
= machine__findnew_thread(machine
, -1, pid
);
1051 struct work_atoms
*atoms
= thread_atoms_search(&sched
->atom_root
, thread
, &sched
->cmp_pid
);
1052 u64 timestamp
= sample
->time
;
1053 int cpu
= sample
->cpu
, err
= -1;
1058 BUG_ON(cpu
>= MAX_CPUS
|| cpu
< 0);
1060 if (thread_atoms_insert(sched
, thread
))
1062 atoms
= thread_atoms_search(&sched
->atom_root
, thread
, &sched
->cmp_pid
);
1064 pr_err("in-event: Internal tree error");
1067 if (add_sched_out_event(atoms
, 'R', timestamp
))
1071 add_runtime_event(atoms
, runtime
, timestamp
);
1074 thread__put(thread
);
1078 static int latency_wakeup_event(struct perf_sched
*sched
,
1079 struct perf_evsel
*evsel
,
1080 struct perf_sample
*sample
,
1081 struct machine
*machine
)
1083 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
1084 struct work_atoms
*atoms
;
1085 struct work_atom
*atom
;
1086 struct thread
*wakee
;
1087 u64 timestamp
= sample
->time
;
1090 wakee
= machine__findnew_thread(machine
, -1, pid
);
1093 atoms
= thread_atoms_search(&sched
->atom_root
, wakee
, &sched
->cmp_pid
);
1095 if (thread_atoms_insert(sched
, wakee
))
1097 atoms
= thread_atoms_search(&sched
->atom_root
, wakee
, &sched
->cmp_pid
);
1099 pr_err("wakeup-event: Internal tree error");
1102 if (add_sched_out_event(atoms
, 'S', timestamp
))
1106 BUG_ON(list_empty(&atoms
->work_list
));
1108 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
1111 * As we do not guarantee the wakeup event happens when
1112 * task is out of run queue, also may happen when task is
1113 * on run queue and wakeup only change ->state to TASK_RUNNING,
1114 * then we should not set the ->wake_up_time when wake up a
1115 * task which is on run queue.
1117 * You WILL be missing events if you've recorded only
1118 * one CPU, or are only looking at only one, so don't
1119 * skip in this case.
1121 if (sched
->profile_cpu
== -1 && atom
->state
!= THREAD_SLEEPING
)
1124 sched
->nr_timestamps
++;
1125 if (atom
->sched_out_time
> timestamp
) {
1126 sched
->nr_unordered_timestamps
++;
1130 atom
->state
= THREAD_WAIT_CPU
;
1131 atom
->wake_up_time
= timestamp
;
1139 static int latency_migrate_task_event(struct perf_sched
*sched
,
1140 struct perf_evsel
*evsel
,
1141 struct perf_sample
*sample
,
1142 struct machine
*machine
)
1144 const u32 pid
= perf_evsel__intval(evsel
, sample
, "pid");
1145 u64 timestamp
= sample
->time
;
1146 struct work_atoms
*atoms
;
1147 struct work_atom
*atom
;
1148 struct thread
*migrant
;
1152 * Only need to worry about migration when profiling one CPU.
1154 if (sched
->profile_cpu
== -1)
1157 migrant
= machine__findnew_thread(machine
, -1, pid
);
1158 if (migrant
== NULL
)
1160 atoms
= thread_atoms_search(&sched
->atom_root
, migrant
, &sched
->cmp_pid
);
1162 if (thread_atoms_insert(sched
, migrant
))
1164 register_pid(sched
, migrant
->tid
, thread__comm_str(migrant
));
1165 atoms
= thread_atoms_search(&sched
->atom_root
, migrant
, &sched
->cmp_pid
);
1167 pr_err("migration-event: Internal tree error");
1170 if (add_sched_out_event(atoms
, 'R', timestamp
))
1174 BUG_ON(list_empty(&atoms
->work_list
));
1176 atom
= list_entry(atoms
->work_list
.prev
, struct work_atom
, list
);
1177 atom
->sched_in_time
= atom
->sched_out_time
= atom
->wake_up_time
= timestamp
;
1179 sched
->nr_timestamps
++;
1181 if (atom
->sched_out_time
> timestamp
)
1182 sched
->nr_unordered_timestamps
++;
1185 thread__put(migrant
);
1189 static void output_lat_thread(struct perf_sched
*sched
, struct work_atoms
*work_list
)
1195 if (!work_list
->nb_atoms
)
1198 * Ignore idle threads:
1200 if (!strcmp(thread__comm_str(work_list
->thread
), "swapper"))
1203 sched
->all_runtime
+= work_list
->total_runtime
;
1204 sched
->all_count
+= work_list
->nb_atoms
;
1206 if (work_list
->num_merged
> 1)
1207 ret
= printf(" %s:(%d) ", thread__comm_str(work_list
->thread
), work_list
->num_merged
);
1209 ret
= printf(" %s:%d ", thread__comm_str(work_list
->thread
), work_list
->thread
->tid
);
1211 for (i
= 0; i
< 24 - ret
; i
++)
1214 avg
= work_list
->total_lat
/ work_list
->nb_atoms
;
1216 printf("|%11.3f ms |%9" PRIu64
" | avg:%9.3f ms | max:%9.3f ms | max at: %13.6f s\n",
1217 (double)work_list
->total_runtime
/ NSEC_PER_MSEC
,
1218 work_list
->nb_atoms
, (double)avg
/ NSEC_PER_MSEC
,
1219 (double)work_list
->max_lat
/ NSEC_PER_MSEC
,
1220 (double)work_list
->max_lat_at
/ NSEC_PER_SEC
);
1223 static int pid_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1225 if (l
->thread
== r
->thread
)
1227 if (l
->thread
->tid
< r
->thread
->tid
)
1229 if (l
->thread
->tid
> r
->thread
->tid
)
1231 return (int)(l
->thread
- r
->thread
);
1234 static int avg_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1244 avgl
= l
->total_lat
/ l
->nb_atoms
;
1245 avgr
= r
->total_lat
/ r
->nb_atoms
;
1255 static int max_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1257 if (l
->max_lat
< r
->max_lat
)
1259 if (l
->max_lat
> r
->max_lat
)
1265 static int switch_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1267 if (l
->nb_atoms
< r
->nb_atoms
)
1269 if (l
->nb_atoms
> r
->nb_atoms
)
1275 static int runtime_cmp(struct work_atoms
*l
, struct work_atoms
*r
)
1277 if (l
->total_runtime
< r
->total_runtime
)
1279 if (l
->total_runtime
> r
->total_runtime
)
1285 static int sort_dimension__add(const char *tok
, struct list_head
*list
)
1288 static struct sort_dimension avg_sort_dimension
= {
1292 static struct sort_dimension max_sort_dimension
= {
1296 static struct sort_dimension pid_sort_dimension
= {
1300 static struct sort_dimension runtime_sort_dimension
= {
1304 static struct sort_dimension switch_sort_dimension
= {
1308 struct sort_dimension
*available_sorts
[] = {
1309 &pid_sort_dimension
,
1310 &avg_sort_dimension
,
1311 &max_sort_dimension
,
1312 &switch_sort_dimension
,
1313 &runtime_sort_dimension
,
1316 for (i
= 0; i
< ARRAY_SIZE(available_sorts
); i
++) {
1317 if (!strcmp(available_sorts
[i
]->name
, tok
)) {
1318 list_add_tail(&available_sorts
[i
]->list
, list
);
1327 static void perf_sched__sort_lat(struct perf_sched
*sched
)
1329 struct rb_node
*node
;
1330 struct rb_root
*root
= &sched
->atom_root
;
1333 struct work_atoms
*data
;
1334 node
= rb_first(root
);
1338 rb_erase(node
, root
);
1339 data
= rb_entry(node
, struct work_atoms
, node
);
1340 __thread_latency_insert(&sched
->sorted_atom_root
, data
, &sched
->sort_list
);
1342 if (root
== &sched
->atom_root
) {
1343 root
= &sched
->merged_atom_root
;
1348 static int process_sched_wakeup_event(struct perf_tool
*tool
,
1349 struct perf_evsel
*evsel
,
1350 struct perf_sample
*sample
,
1351 struct machine
*machine
)
1353 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1355 if (sched
->tp_handler
->wakeup_event
)
1356 return sched
->tp_handler
->wakeup_event(sched
, evsel
, sample
, machine
);
1366 static bool thread__has_color(struct thread
*thread
)
1368 union map_priv priv
= {
1369 .ptr
= thread__priv(thread
),
1375 static struct thread
*
1376 map__findnew_thread(struct perf_sched
*sched
, struct machine
*machine
, pid_t pid
, pid_t tid
)
1378 struct thread
*thread
= machine__findnew_thread(machine
, pid
, tid
);
1379 union map_priv priv
= {
1383 if (!sched
->map
.color_pids
|| !thread
|| thread__priv(thread
))
1386 if (thread_map__has(sched
->map
.color_pids
, tid
))
1389 thread__set_priv(thread
, priv
.ptr
);
1393 static int map_switch_event(struct perf_sched
*sched
, struct perf_evsel
*evsel
,
1394 struct perf_sample
*sample
, struct machine
*machine
)
1396 const u32 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
1397 struct thread
*sched_in
;
1399 u64 timestamp0
, timestamp
= sample
->time
;
1401 int i
, this_cpu
= sample
->cpu
;
1403 bool new_cpu
= false;
1404 const char *color
= PERF_COLOR_NORMAL
;
1406 BUG_ON(this_cpu
>= MAX_CPUS
|| this_cpu
< 0);
1408 if (this_cpu
> sched
->max_cpu
)
1409 sched
->max_cpu
= this_cpu
;
1411 if (sched
->map
.comp
) {
1412 cpus_nr
= bitmap_weight(sched
->map
.comp_cpus_mask
, MAX_CPUS
);
1413 if (!test_and_set_bit(this_cpu
, sched
->map
.comp_cpus_mask
)) {
1414 sched
->map
.comp_cpus
[cpus_nr
++] = this_cpu
;
1418 cpus_nr
= sched
->max_cpu
;
1420 timestamp0
= sched
->cpu_last_switched
[this_cpu
];
1421 sched
->cpu_last_switched
[this_cpu
] = timestamp
;
1423 delta
= timestamp
- timestamp0
;
1428 pr_err("hm, delta: %" PRIu64
" < 0 ?\n", delta
);
1432 sched_in
= map__findnew_thread(sched
, machine
, -1, next_pid
);
1433 if (sched_in
== NULL
)
1436 sched
->curr_thread
[this_cpu
] = thread__get(sched_in
);
1441 if (!sched_in
->shortname
[0]) {
1442 if (!strcmp(thread__comm_str(sched_in
), "swapper")) {
1444 * Don't allocate a letter-number for swapper:0
1445 * as a shortname. Instead, we use '.' for it.
1447 sched_in
->shortname
[0] = '.';
1448 sched_in
->shortname
[1] = ' ';
1450 sched_in
->shortname
[0] = sched
->next_shortname1
;
1451 sched_in
->shortname
[1] = sched
->next_shortname2
;
1453 if (sched
->next_shortname1
< 'Z') {
1454 sched
->next_shortname1
++;
1456 sched
->next_shortname1
= 'A';
1457 if (sched
->next_shortname2
< '9')
1458 sched
->next_shortname2
++;
1460 sched
->next_shortname2
= '0';
1466 for (i
= 0; i
< cpus_nr
; i
++) {
1467 int cpu
= sched
->map
.comp
? sched
->map
.comp_cpus
[i
] : i
;
1468 struct thread
*curr_thread
= sched
->curr_thread
[cpu
];
1469 const char *pid_color
= color
;
1470 const char *cpu_color
= color
;
1472 if (curr_thread
&& thread__has_color(curr_thread
))
1473 pid_color
= COLOR_PIDS
;
1475 if (sched
->map
.cpus
&& !cpu_map__has(sched
->map
.cpus
, cpu
))
1478 if (sched
->map
.color_cpus
&& cpu_map__has(sched
->map
.color_cpus
, cpu
))
1479 cpu_color
= COLOR_CPUS
;
1481 if (cpu
!= this_cpu
)
1482 color_fprintf(stdout
, cpu_color
, " ");
1484 color_fprintf(stdout
, cpu_color
, "*");
1486 if (sched
->curr_thread
[cpu
])
1487 color_fprintf(stdout
, pid_color
, "%2s ", sched
->curr_thread
[cpu
]->shortname
);
1489 color_fprintf(stdout
, color
, " ");
1492 if (sched
->map
.cpus
&& !cpu_map__has(sched
->map
.cpus
, this_cpu
))
1495 color_fprintf(stdout
, color
, " %12.6f secs ", (double)timestamp
/ NSEC_PER_SEC
);
1496 if (new_shortname
) {
1497 const char *pid_color
= color
;
1499 if (thread__has_color(sched_in
))
1500 pid_color
= COLOR_PIDS
;
1502 color_fprintf(stdout
, pid_color
, "%s => %s:%d",
1503 sched_in
->shortname
, thread__comm_str(sched_in
), sched_in
->tid
);
1506 if (sched
->map
.comp
&& new_cpu
)
1507 color_fprintf(stdout
, color
, " (CPU %d)", this_cpu
);
1510 color_fprintf(stdout
, color
, "\n");
1512 thread__put(sched_in
);
1517 static int process_sched_switch_event(struct perf_tool
*tool
,
1518 struct perf_evsel
*evsel
,
1519 struct perf_sample
*sample
,
1520 struct machine
*machine
)
1522 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1523 int this_cpu
= sample
->cpu
, err
= 0;
1524 u32 prev_pid
= perf_evsel__intval(evsel
, sample
, "prev_pid"),
1525 next_pid
= perf_evsel__intval(evsel
, sample
, "next_pid");
1527 if (sched
->curr_pid
[this_cpu
] != (u32
)-1) {
1529 * Are we trying to switch away a PID that is
1532 if (sched
->curr_pid
[this_cpu
] != prev_pid
)
1533 sched
->nr_context_switch_bugs
++;
1536 if (sched
->tp_handler
->switch_event
)
1537 err
= sched
->tp_handler
->switch_event(sched
, evsel
, sample
, machine
);
1539 sched
->curr_pid
[this_cpu
] = next_pid
;
1543 static int process_sched_runtime_event(struct perf_tool
*tool
,
1544 struct perf_evsel
*evsel
,
1545 struct perf_sample
*sample
,
1546 struct machine
*machine
)
1548 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1550 if (sched
->tp_handler
->runtime_event
)
1551 return sched
->tp_handler
->runtime_event(sched
, evsel
, sample
, machine
);
1556 static int perf_sched__process_fork_event(struct perf_tool
*tool
,
1557 union perf_event
*event
,
1558 struct perf_sample
*sample
,
1559 struct machine
*machine
)
1561 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1563 /* run the fork event through the perf machineruy */
1564 perf_event__process_fork(tool
, event
, sample
, machine
);
1566 /* and then run additional processing needed for this command */
1567 if (sched
->tp_handler
->fork_event
)
1568 return sched
->tp_handler
->fork_event(sched
, event
, machine
);
1573 static int process_sched_migrate_task_event(struct perf_tool
*tool
,
1574 struct perf_evsel
*evsel
,
1575 struct perf_sample
*sample
,
1576 struct machine
*machine
)
1578 struct perf_sched
*sched
= container_of(tool
, struct perf_sched
, tool
);
1580 if (sched
->tp_handler
->migrate_task_event
)
1581 return sched
->tp_handler
->migrate_task_event(sched
, evsel
, sample
, machine
);
1586 typedef int (*tracepoint_handler
)(struct perf_tool
*tool
,
1587 struct perf_evsel
*evsel
,
1588 struct perf_sample
*sample
,
1589 struct machine
*machine
);
1591 static int perf_sched__process_tracepoint_sample(struct perf_tool
*tool __maybe_unused
,
1592 union perf_event
*event __maybe_unused
,
1593 struct perf_sample
*sample
,
1594 struct perf_evsel
*evsel
,
1595 struct machine
*machine
)
1599 if (evsel
->handler
!= NULL
) {
1600 tracepoint_handler f
= evsel
->handler
;
1601 err
= f(tool
, evsel
, sample
, machine
);
1607 static int perf_sched__read_events(struct perf_sched
*sched
)
1609 const struct perf_evsel_str_handler handlers
[] = {
1610 { "sched:sched_switch", process_sched_switch_event
, },
1611 { "sched:sched_stat_runtime", process_sched_runtime_event
, },
1612 { "sched:sched_wakeup", process_sched_wakeup_event
, },
1613 { "sched:sched_wakeup_new", process_sched_wakeup_event
, },
1614 { "sched:sched_migrate_task", process_sched_migrate_task_event
, },
1616 struct perf_session
*session
;
1617 struct perf_data_file file
= {
1619 .mode
= PERF_DATA_MODE_READ
,
1620 .force
= sched
->force
,
1624 session
= perf_session__new(&file
, false, &sched
->tool
);
1625 if (session
== NULL
) {
1626 pr_debug("No Memory for session\n");
1630 symbol__init(&session
->header
.env
);
1632 if (perf_session__set_tracepoints_handlers(session
, handlers
))
1635 if (perf_session__has_traces(session
, "record -R")) {
1636 int err
= perf_session__process_events(session
);
1638 pr_err("Failed to process events, error %d", err
);
1642 sched
->nr_events
= session
->evlist
->stats
.nr_events
[0];
1643 sched
->nr_lost_events
= session
->evlist
->stats
.total_lost
;
1644 sched
->nr_lost_chunks
= session
->evlist
->stats
.nr_events
[PERF_RECORD_LOST
];
1649 perf_session__delete(session
);
1653 static void print_bad_events(struct perf_sched
*sched
)
1655 if (sched
->nr_unordered_timestamps
&& sched
->nr_timestamps
) {
1656 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1657 (double)sched
->nr_unordered_timestamps
/(double)sched
->nr_timestamps
*100.0,
1658 sched
->nr_unordered_timestamps
, sched
->nr_timestamps
);
1660 if (sched
->nr_lost_events
&& sched
->nr_events
) {
1661 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1662 (double)sched
->nr_lost_events
/(double)sched
->nr_events
* 100.0,
1663 sched
->nr_lost_events
, sched
->nr_events
, sched
->nr_lost_chunks
);
1665 if (sched
->nr_context_switch_bugs
&& sched
->nr_timestamps
) {
1666 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1667 (double)sched
->nr_context_switch_bugs
/(double)sched
->nr_timestamps
*100.0,
1668 sched
->nr_context_switch_bugs
, sched
->nr_timestamps
);
1669 if (sched
->nr_lost_events
)
1670 printf(" (due to lost events?)");
1675 static void __merge_work_atoms(struct rb_root
*root
, struct work_atoms
*data
)
1677 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1678 struct work_atoms
*this;
1679 const char *comm
= thread__comm_str(data
->thread
), *this_comm
;
1684 this = container_of(*new, struct work_atoms
, node
);
1687 this_comm
= thread__comm_str(this->thread
);
1688 cmp
= strcmp(comm
, this_comm
);
1690 new = &((*new)->rb_left
);
1691 } else if (cmp
< 0) {
1692 new = &((*new)->rb_right
);
1695 this->total_runtime
+= data
->total_runtime
;
1696 this->nb_atoms
+= data
->nb_atoms
;
1697 this->total_lat
+= data
->total_lat
;
1698 list_splice(&data
->work_list
, &this->work_list
);
1699 if (this->max_lat
< data
->max_lat
) {
1700 this->max_lat
= data
->max_lat
;
1701 this->max_lat_at
= data
->max_lat_at
;
1709 rb_link_node(&data
->node
, parent
, new);
1710 rb_insert_color(&data
->node
, root
);
1713 static void perf_sched__merge_lat(struct perf_sched
*sched
)
1715 struct work_atoms
*data
;
1716 struct rb_node
*node
;
1718 if (sched
->skip_merge
)
1721 while ((node
= rb_first(&sched
->atom_root
))) {
1722 rb_erase(node
, &sched
->atom_root
);
1723 data
= rb_entry(node
, struct work_atoms
, node
);
1724 __merge_work_atoms(&sched
->merged_atom_root
, data
);
1728 static int perf_sched__lat(struct perf_sched
*sched
)
1730 struct rb_node
*next
;
1734 if (perf_sched__read_events(sched
))
1737 perf_sched__merge_lat(sched
);
1738 perf_sched__sort_lat(sched
);
1740 printf("\n -----------------------------------------------------------------------------------------------------------------\n");
1741 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1742 printf(" -----------------------------------------------------------------------------------------------------------------\n");
1744 next
= rb_first(&sched
->sorted_atom_root
);
1747 struct work_atoms
*work_list
;
1749 work_list
= rb_entry(next
, struct work_atoms
, node
);
1750 output_lat_thread(sched
, work_list
);
1751 next
= rb_next(next
);
1752 thread__zput(work_list
->thread
);
1755 printf(" -----------------------------------------------------------------------------------------------------------------\n");
1756 printf(" TOTAL: |%11.3f ms |%9" PRIu64
" |\n",
1757 (double)sched
->all_runtime
/ NSEC_PER_MSEC
, sched
->all_count
);
1759 printf(" ---------------------------------------------------\n");
1761 print_bad_events(sched
);
1767 static int setup_map_cpus(struct perf_sched
*sched
)
1769 struct cpu_map
*map
;
1771 sched
->max_cpu
= sysconf(_SC_NPROCESSORS_CONF
);
1773 if (sched
->map
.comp
) {
1774 sched
->map
.comp_cpus
= zalloc(sched
->max_cpu
* sizeof(int));
1775 if (!sched
->map
.comp_cpus
)
1779 if (!sched
->map
.cpus_str
)
1782 map
= cpu_map__new(sched
->map
.cpus_str
);
1784 pr_err("failed to get cpus map from %s\n", sched
->map
.cpus_str
);
1788 sched
->map
.cpus
= map
;
1792 static int setup_color_pids(struct perf_sched
*sched
)
1794 struct thread_map
*map
;
1796 if (!sched
->map
.color_pids_str
)
1799 map
= thread_map__new_by_tid_str(sched
->map
.color_pids_str
);
1801 pr_err("failed to get thread map from %s\n", sched
->map
.color_pids_str
);
1805 sched
->map
.color_pids
= map
;
1809 static int setup_color_cpus(struct perf_sched
*sched
)
1811 struct cpu_map
*map
;
1813 if (!sched
->map
.color_cpus_str
)
1816 map
= cpu_map__new(sched
->map
.color_cpus_str
);
1818 pr_err("failed to get thread map from %s\n", sched
->map
.color_cpus_str
);
1822 sched
->map
.color_cpus
= map
;
1826 static int perf_sched__map(struct perf_sched
*sched
)
1828 if (setup_map_cpus(sched
))
1831 if (setup_color_pids(sched
))
1834 if (setup_color_cpus(sched
))
1838 if (perf_sched__read_events(sched
))
1840 print_bad_events(sched
);
1844 static int perf_sched__replay(struct perf_sched
*sched
)
1848 calibrate_run_measurement_overhead(sched
);
1849 calibrate_sleep_measurement_overhead(sched
);
1851 test_calibrations(sched
);
1853 if (perf_sched__read_events(sched
))
1856 printf("nr_run_events: %ld\n", sched
->nr_run_events
);
1857 printf("nr_sleep_events: %ld\n", sched
->nr_sleep_events
);
1858 printf("nr_wakeup_events: %ld\n", sched
->nr_wakeup_events
);
1860 if (sched
->targetless_wakeups
)
1861 printf("target-less wakeups: %ld\n", sched
->targetless_wakeups
);
1862 if (sched
->multitarget_wakeups
)
1863 printf("multi-target wakeups: %ld\n", sched
->multitarget_wakeups
);
1864 if (sched
->nr_run_events_optimized
)
1865 printf("run atoms optimized: %ld\n",
1866 sched
->nr_run_events_optimized
);
1868 print_task_traces(sched
);
1869 add_cross_task_wakeups(sched
);
1871 create_tasks(sched
);
1872 printf("------------------------------------------------------------\n");
1873 for (i
= 0; i
< sched
->replay_repeat
; i
++)
1874 run_one_test(sched
);
1879 static void setup_sorting(struct perf_sched
*sched
, const struct option
*options
,
1880 const char * const usage_msg
[])
1882 char *tmp
, *tok
, *str
= strdup(sched
->sort_order
);
1884 for (tok
= strtok_r(str
, ", ", &tmp
);
1885 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1886 if (sort_dimension__add(tok
, &sched
->sort_list
) < 0) {
1887 usage_with_options_msg(usage_msg
, options
,
1888 "Unknown --sort key: `%s'", tok
);
1894 sort_dimension__add("pid", &sched
->cmp_pid
);
1897 static int __cmd_record(int argc
, const char **argv
)
1899 unsigned int rec_argc
, i
, j
;
1900 const char **rec_argv
;
1901 const char * const record_args
[] = {
1907 "-e", "sched:sched_switch",
1908 "-e", "sched:sched_stat_wait",
1909 "-e", "sched:sched_stat_sleep",
1910 "-e", "sched:sched_stat_iowait",
1911 "-e", "sched:sched_stat_runtime",
1912 "-e", "sched:sched_process_fork",
1913 "-e", "sched:sched_wakeup",
1914 "-e", "sched:sched_wakeup_new",
1915 "-e", "sched:sched_migrate_task",
1918 rec_argc
= ARRAY_SIZE(record_args
) + argc
- 1;
1919 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
1921 if (rec_argv
== NULL
)
1924 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
1925 rec_argv
[i
] = strdup(record_args
[i
]);
1927 for (j
= 1; j
< (unsigned int)argc
; j
++, i
++)
1928 rec_argv
[i
] = argv
[j
];
1930 BUG_ON(i
!= rec_argc
);
1932 return cmd_record(i
, rec_argv
, NULL
);
1935 int cmd_sched(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1937 const char default_sort_order
[] = "avg, max, switch, runtime";
1938 struct perf_sched sched
= {
1940 .sample
= perf_sched__process_tracepoint_sample
,
1941 .comm
= perf_event__process_comm
,
1942 .lost
= perf_event__process_lost
,
1943 .fork
= perf_sched__process_fork_event
,
1944 .ordered_events
= true,
1946 .cmp_pid
= LIST_HEAD_INIT(sched
.cmp_pid
),
1947 .sort_list
= LIST_HEAD_INIT(sched
.sort_list
),
1948 .start_work_mutex
= PTHREAD_MUTEX_INITIALIZER
,
1949 .work_done_wait_mutex
= PTHREAD_MUTEX_INITIALIZER
,
1950 .sort_order
= default_sort_order
,
1951 .replay_repeat
= 10,
1953 .next_shortname1
= 'A',
1954 .next_shortname2
= '0',
1957 const struct option latency_options
[] = {
1958 OPT_STRING('s', "sort", &sched
.sort_order
, "key[,key2...]",
1959 "sort by key(s): runtime, switch, avg, max"),
1960 OPT_INCR('v', "verbose", &verbose
,
1961 "be more verbose (show symbol address, etc)"),
1962 OPT_INTEGER('C', "CPU", &sched
.profile_cpu
,
1963 "CPU to profile on"),
1964 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1965 "dump raw trace in ASCII"),
1966 OPT_BOOLEAN('p', "pids", &sched
.skip_merge
,
1967 "latency stats per pid instead of per comm"),
1970 const struct option replay_options
[] = {
1971 OPT_UINTEGER('r', "repeat", &sched
.replay_repeat
,
1972 "repeat the workload replay N times (-1: infinite)"),
1973 OPT_INCR('v', "verbose", &verbose
,
1974 "be more verbose (show symbol address, etc)"),
1975 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1976 "dump raw trace in ASCII"),
1977 OPT_BOOLEAN('f', "force", &sched
.force
, "don't complain, do it"),
1980 const struct option sched_options
[] = {
1981 OPT_STRING('i', "input", &input_name
, "file",
1983 OPT_INCR('v', "verbose", &verbose
,
1984 "be more verbose (show symbol address, etc)"),
1985 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1986 "dump raw trace in ASCII"),
1989 const struct option map_options
[] = {
1990 OPT_BOOLEAN(0, "compact", &sched
.map
.comp
,
1991 "map output in compact mode"),
1992 OPT_STRING(0, "color-pids", &sched
.map
.color_pids_str
, "pids",
1993 "highlight given pids in map"),
1994 OPT_STRING(0, "color-cpus", &sched
.map
.color_cpus_str
, "cpus",
1995 "highlight given CPUs in map"),
1996 OPT_STRING(0, "cpus", &sched
.map
.cpus_str
, "cpus",
1997 "display given CPUs in map"),
2000 const char * const latency_usage
[] = {
2001 "perf sched latency [<options>]",
2004 const char * const replay_usage
[] = {
2005 "perf sched replay [<options>]",
2008 const char * const map_usage
[] = {
2009 "perf sched map [<options>]",
2012 const char *const sched_subcommands
[] = { "record", "latency", "map",
2013 "replay", "script", NULL
};
2014 const char *sched_usage
[] = {
2018 struct trace_sched_handler lat_ops
= {
2019 .wakeup_event
= latency_wakeup_event
,
2020 .switch_event
= latency_switch_event
,
2021 .runtime_event
= latency_runtime_event
,
2022 .migrate_task_event
= latency_migrate_task_event
,
2024 struct trace_sched_handler map_ops
= {
2025 .switch_event
= map_switch_event
,
2027 struct trace_sched_handler replay_ops
= {
2028 .wakeup_event
= replay_wakeup_event
,
2029 .switch_event
= replay_switch_event
,
2030 .fork_event
= replay_fork_event
,
2034 for (i
= 0; i
< ARRAY_SIZE(sched
.curr_pid
); i
++)
2035 sched
.curr_pid
[i
] = -1;
2037 argc
= parse_options_subcommand(argc
, argv
, sched_options
, sched_subcommands
,
2038 sched_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
2040 usage_with_options(sched_usage
, sched_options
);
2043 * Aliased to 'perf script' for now:
2045 if (!strcmp(argv
[0], "script"))
2046 return cmd_script(argc
, argv
, prefix
);
2048 if (!strncmp(argv
[0], "rec", 3)) {
2049 return __cmd_record(argc
, argv
);
2050 } else if (!strncmp(argv
[0], "lat", 3)) {
2051 sched
.tp_handler
= &lat_ops
;
2053 argc
= parse_options(argc
, argv
, latency_options
, latency_usage
, 0);
2055 usage_with_options(latency_usage
, latency_options
);
2057 setup_sorting(&sched
, latency_options
, latency_usage
);
2058 return perf_sched__lat(&sched
);
2059 } else if (!strcmp(argv
[0], "map")) {
2061 argc
= parse_options(argc
, argv
, map_options
, map_usage
, 0);
2063 usage_with_options(map_usage
, map_options
);
2065 sched
.tp_handler
= &map_ops
;
2066 setup_sorting(&sched
, latency_options
, latency_usage
);
2067 return perf_sched__map(&sched
);
2068 } else if (!strncmp(argv
[0], "rep", 3)) {
2069 sched
.tp_handler
= &replay_ops
;
2071 argc
= parse_options(argc
, argv
, replay_options
, replay_usage
, 0);
2073 usage_with_options(replay_usage
, replay_options
);
2075 return perf_sched__replay(&sched
);
2077 usage_with_options(sched_usage
, sched_options
);