Lines Matching +full:min +full:- +full:sample +full:- +full:time

1 // SPDX-License-Identifier: GPL-2.0
4 #include "perf-sys.h"
21 #include "util/time-utils.h"
24 #include <subcmd/parse-options.h>
25 #include "util/trace-event.h"
102 /* in tsk->exit_state */
106 /* in tsk->state again */
147 struct perf_sample *sample, struct machine *machine);
150 struct perf_sample *sample, struct machine *machine);
153 struct perf_sample *sample, struct machine *machine);
161 struct perf_sample *sample,
191 * Track the current task - that way we can know whether there's any
250 /* per thread run time data */
252 u64 last_time; /* time of previous sched in/out event */
253 u64 dt_run; /* run time */
254 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
255 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
256 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
257 u64 dt_delay; /* time between wakeup and sched-in */
258 u64 ready_to_run; /* time of wakeup */
275 /* per event run time data */
277 u64 *last_time; /* time this event was last seen per cpu */
281 /* per cpu idle time data */
310 } while (T1 + sched->run_measurement_overhead < T0 + nsecs); in burn_nsecs()
332 delta = T1-T0; in calibrate_run_measurement_overhead()
333 min_delta = min(min_delta, delta); in calibrate_run_measurement_overhead()
335 sched->run_measurement_overhead = min_delta; in calibrate_run_measurement_overhead()
349 delta = T1-T0; in calibrate_sleep_measurement_overhead()
350 min_delta = min(min_delta, delta); in calibrate_sleep_measurement_overhead()
352 min_delta -= 10000; in calibrate_sleep_measurement_overhead()
353 sched->sleep_measurement_overhead = min_delta; in calibrate_sleep_measurement_overhead()
362 unsigned long idx = task->nr_events; in get_new_event()
365 event->timestamp = timestamp; in get_new_event()
366 event->nr = idx; in get_new_event()
368 task->nr_events++; in get_new_event()
369 size = sizeof(struct sched_atom *) * task->nr_events; in get_new_event()
370 task->atoms = realloc(task->atoms, size); in get_new_event()
371 BUG_ON(!task->atoms); in get_new_event()
373 task->atoms[idx] = event; in get_new_event()
380 if (!task->nr_events) in last_event()
383 return task->atoms[task->nr_events - 1]; in last_event()
395 if (curr_event && curr_event->type == SCHED_EVENT_RUN) { in add_sched_event_run()
396 sched->nr_run_events_optimized++; in add_sched_event_run()
397 curr_event->duration += duration; in add_sched_event_run()
403 event->type = SCHED_EVENT_RUN; in add_sched_event_run()
404 event->duration = duration; in add_sched_event_run()
406 sched->nr_run_events++; in add_sched_event_run()
415 event->type = SCHED_EVENT_WAKEUP; in add_sched_event_wakeup()
416 event->wakee = wakee; in add_sched_event_wakeup()
419 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { in add_sched_event_wakeup()
420 sched->targetless_wakeups++; in add_sched_event_wakeup()
423 if (wakee_event->wait_sem) { in add_sched_event_wakeup()
424 sched->multitarget_wakeups++; in add_sched_event_wakeup()
428 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); in add_sched_event_wakeup()
429 sem_init(wakee_event->wait_sem, 0, 0); in add_sched_event_wakeup()
430 wakee_event->specific_wait = 1; in add_sched_event_wakeup()
431 event->wait_sem = wakee_event->wait_sem; in add_sched_event_wakeup()
433 sched->nr_wakeup_events++; in add_sched_event_wakeup()
441 event->type = SCHED_EVENT_SLEEP; in add_sched_event_sleep()
443 sched->nr_sleep_events++; in add_sched_event_sleep()
452 if (sched->pid_to_task == NULL) { in register_pid()
455 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); in register_pid()
458 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * in register_pid()
461 sched->pid_to_task[pid_max++] = NULL; in register_pid()
464 task = sched->pid_to_task[pid]; in register_pid()
470 task->pid = pid; in register_pid()
471 task->nr = sched->nr_tasks; in register_pid()
472 strcpy(task->comm, comm); in register_pid()
474 * every task starts in sleeping state - this gets ignored in register_pid()
479 sched->pid_to_task[pid] = task; in register_pid()
480 sched->nr_tasks++; in register_pid()
481 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *)); in register_pid()
482 BUG_ON(!sched->tasks); in register_pid()
483 sched->tasks[task->nr] = task; in register_pid()
486 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm); in register_pid()
497 for (i = 0; i < sched->nr_tasks; i++) { in print_task_traces()
498 task = sched->tasks[i]; in print_task_traces()
500 task->nr, task->comm, task->pid, task->nr_events); in print_task_traces()
509 for (i = 0; i < sched->nr_tasks; i++) { in add_cross_task_wakeups()
510 task1 = sched->tasks[i]; in add_cross_task_wakeups()
512 if (j == sched->nr_tasks) in add_cross_task_wakeups()
514 task2 = sched->tasks[j]; in add_cross_task_wakeups()
524 switch (atom->type) { in perf_sched__process_event()
526 burn_nsecs(sched, atom->duration); in perf_sched__process_event()
529 if (atom->wait_sem) in perf_sched__process_event()
530 ret = sem_wait(atom->wait_sem); in perf_sched__process_event()
534 if (atom->wait_sem) in perf_sched__process_event()
535 ret = sem_post(atom->wait_sem); in perf_sched__process_event()
574 fd = sys_perf_event_open(&attr, 0, -1, -1, in self_open_counters()
579 if (sched->force) { in self_open_counters()
580 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); in self_open_counters()
581 limit.rlim_cur += sched->nr_tasks - cur_task; in self_open_counters()
586 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { in self_open_counters()
592 strcpy(info, "Have a try with -f option\n"); in self_open_counters()
622 struct task_desc *this_task = parms->task; in thread_func()
623 struct perf_sched *sched = parms->sched; in thread_func()
627 int fd = parms->fd; in thread_func()
631 sprintf(comm2, ":%s", this_task->comm); in thread_func()
636 ret = sem_post(&this_task->ready_for_work); in thread_func()
638 ret = pthread_mutex_lock(&sched->start_work_mutex); in thread_func()
640 ret = pthread_mutex_unlock(&sched->start_work_mutex); in thread_func()
645 for (i = 0; i < this_task->nr_events; i++) { in thread_func()
646 this_task->curr_event = i; in thread_func()
647 perf_sched__process_event(sched, this_task->atoms[i]); in thread_func()
651 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; in thread_func()
652 ret = sem_post(&this_task->work_done_sem); in thread_func()
655 ret = pthread_mutex_lock(&sched->work_done_wait_mutex); in thread_func()
657 ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); in thread_func()
675 err = pthread_mutex_lock(&sched->start_work_mutex); in create_tasks()
677 err = pthread_mutex_lock(&sched->work_done_wait_mutex); in create_tasks()
679 for (i = 0; i < sched->nr_tasks; i++) { in create_tasks()
682 parms->task = task = sched->tasks[i]; in create_tasks()
683 parms->sched = sched; in create_tasks()
684 parms->fd = self_open_counters(sched, i); in create_tasks()
685 sem_init(&task->sleep_sem, 0, 0); in create_tasks()
686 sem_init(&task->ready_for_work, 0, 0); in create_tasks()
687 sem_init(&task->work_done_sem, 0, 0); in create_tasks()
688 task->curr_event = 0; in create_tasks()
689 err = pthread_create(&task->thread, &attr, thread_func, parms); in create_tasks()
700 sched->start_time = get_nsecs(); in wait_for_tasks()
701 sched->cpu_usage = 0; in wait_for_tasks()
702 pthread_mutex_unlock(&sched->work_done_wait_mutex); in wait_for_tasks()
704 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
705 task = sched->tasks[i]; in wait_for_tasks()
706 ret = sem_wait(&task->ready_for_work); in wait_for_tasks()
708 sem_init(&task->ready_for_work, 0, 0); in wait_for_tasks()
710 ret = pthread_mutex_lock(&sched->work_done_wait_mutex); in wait_for_tasks()
715 pthread_mutex_unlock(&sched->start_work_mutex); in wait_for_tasks()
717 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
718 task = sched->tasks[i]; in wait_for_tasks()
719 ret = sem_wait(&task->work_done_sem); in wait_for_tasks()
721 sem_init(&task->work_done_sem, 0, 0); in wait_for_tasks()
722 sched->cpu_usage += task->cpu_usage; in wait_for_tasks()
723 task->cpu_usage = 0; in wait_for_tasks()
727 if (!sched->runavg_cpu_usage) in wait_for_tasks()
728 sched->runavg_cpu_usage = sched->cpu_usage; in wait_for_tasks()
729 …sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage… in wait_for_tasks()
731 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; in wait_for_tasks()
732 if (!sched->runavg_parent_cpu_usage) in wait_for_tasks()
733 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; in wait_for_tasks()
734 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + in wait_for_tasks()
735 sched->parent_cpu_usage)/sched->replay_repeat; in wait_for_tasks()
737 ret = pthread_mutex_lock(&sched->start_work_mutex); in wait_for_tasks()
740 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
741 task = sched->tasks[i]; in wait_for_tasks()
742 sem_init(&task->sleep_sem, 0, 0); in wait_for_tasks()
743 task->curr_event = 0; in wait_for_tasks()
755 delta = T1 - T0; in run_one_test()
756 sched->sum_runtime += delta; in run_one_test()
757 sched->nr_runs++; in run_one_test()
759 avg_delta = sched->sum_runtime / sched->nr_runs; in run_one_test()
761 fluct = avg_delta - delta; in run_one_test()
763 fluct = delta - avg_delta; in run_one_test()
764 sched->sum_fluct += fluct; in run_one_test()
765 if (!sched->run_avg) in run_one_test()
766 sched->run_avg = delta; in run_one_test()
767 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat; in run_one_test()
769 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC); in run_one_test()
771 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC); in run_one_test()
774 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC); in run_one_test()
779 * accurate than the sched->sum_exec_runtime based statistics: in run_one_test()
782 (double)sched->parent_cpu_usage / NSEC_PER_MSEC, in run_one_test()
783 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC); in run_one_test()
788 if (sched->nr_sleep_corrections) in run_one_test()
789 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); in run_one_test()
790 sched->nr_sleep_corrections = 0; in run_one_test()
801 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); in test_calibrations()
807 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); in test_calibrations()
812 struct evsel *evsel, struct perf_sample *sample, in replay_wakeup_event() argument
815 const char *comm = evsel__strval(evsel, sample, "comm"); in replay_wakeup_event()
816 const u32 pid = evsel__intval(evsel, sample, "pid"); in replay_wakeup_event()
822 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); in replay_wakeup_event()
825 waker = register_pid(sched, sample->tid, "<unknown>"); in replay_wakeup_event()
828 add_sched_event_wakeup(sched, waker, sample->time, wakee); in replay_wakeup_event()
834 struct perf_sample *sample, in replay_switch_event() argument
837 const char *prev_comm = evsel__strval(evsel, sample, "prev_comm"), in replay_switch_event()
838 *next_comm = evsel__strval(evsel, sample, "next_comm"); in replay_switch_event()
839 const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in replay_switch_event()
840 next_pid = evsel__intval(evsel, sample, "next_pid"); in replay_switch_event()
841 const u64 prev_state = evsel__intval(evsel, sample, "prev_state"); in replay_switch_event()
843 u64 timestamp0, timestamp = sample->time; in replay_switch_event()
844 int cpu = sample->cpu; in replay_switch_event()
853 timestamp0 = sched->cpu_last_switched[cpu]; in replay_switch_event()
855 delta = timestamp - timestamp0; in replay_switch_event()
861 return -1; in replay_switch_event()
870 sched->cpu_last_switched[cpu] = timestamp; in replay_switch_event()
884 child = machine__findnew_thread(machine, event->fork.pid, in replay_fork_event()
885 event->fork.tid); in replay_fork_event()
886 parent = machine__findnew_thread(machine, event->fork.ppid, in replay_fork_event()
887 event->fork.ptid); in replay_fork_event()
897 printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); in replay_fork_event()
898 printf("... child: %s/%d\n", thread__comm_str(child), child->tid); in replay_fork_event()
901 register_pid(sched, parent->tid, thread__comm_str(parent)); in replay_fork_event()
902 register_pid(sched, child->tid, thread__comm_str(child)); in replay_fork_event()
926 init_stats(&r->run_stats); in thread__init_runtime()
955 ret = sort->cmp(l, r); in thread_lat_cmp()
967 struct rb_node *node = root->rb_root.rb_node; in thread_atoms_search()
978 node = node->rb_left; in thread_atoms_search()
980 node = node->rb_right; in thread_atoms_search()
982 BUG_ON(thread != atoms->thread); in thread_atoms_search()
993 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in __thread_latency_insert()
1006 new = &((*new)->rb_left); in __thread_latency_insert()
1008 new = &((*new)->rb_right); in __thread_latency_insert()
1013 rb_link_node(&data->node, parent, new); in __thread_latency_insert()
1014 rb_insert_color_cached(&data->node, root, leftmost); in __thread_latency_insert()
1022 return -1; in thread_atoms_insert()
1025 atoms->thread = thread__get(thread); in thread_atoms_insert()
1026 INIT_LIST_HEAD(&atoms->work_list); in thread_atoms_insert()
1027 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); in thread_atoms_insert()
1046 return -1; in add_sched_out_event()
1049 atom->sched_out_time = timestamp; in add_sched_out_event()
1052 atom->state = THREAD_WAIT_CPU; in add_sched_out_event()
1053 atom->wake_up_time = atom->sched_out_time; in add_sched_out_event()
1056 list_add_tail(&atom->list, &atoms->work_list); in add_sched_out_event()
1066 BUG_ON(list_empty(&atoms->work_list)); in add_runtime_event()
1068 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in add_runtime_event()
1070 atom->runtime += delta; in add_runtime_event()
1071 atoms->total_runtime += delta; in add_runtime_event()
1080 if (list_empty(&atoms->work_list)) in add_sched_in_event()
1083 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in add_sched_in_event()
1085 if (atom->state != THREAD_WAIT_CPU) in add_sched_in_event()
1088 if (timestamp < atom->wake_up_time) { in add_sched_in_event()
1089 atom->state = THREAD_IGNORE; in add_sched_in_event()
1093 atom->state = THREAD_SCHED_IN; in add_sched_in_event()
1094 atom->sched_in_time = timestamp; in add_sched_in_event()
1096 delta = atom->sched_in_time - atom->wake_up_time; in add_sched_in_event()
1097 atoms->total_lat += delta; in add_sched_in_event()
1098 if (delta > atoms->max_lat) { in add_sched_in_event()
1099 atoms->max_lat = delta; in add_sched_in_event()
1100 atoms->max_lat_start = atom->wake_up_time; in add_sched_in_event()
1101 atoms->max_lat_end = timestamp; in add_sched_in_event()
1103 atoms->nb_atoms++; in add_sched_in_event()
1108 struct perf_sample *sample, in latency_switch_event() argument
1111 const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in latency_switch_event()
1112 next_pid = evsel__intval(evsel, sample, "next_pid"); in latency_switch_event()
1113 const u64 prev_state = evsel__intval(evsel, sample, "prev_state"); in latency_switch_event()
1116 u64 timestamp0, timestamp = sample->time; in latency_switch_event()
1117 int cpu = sample->cpu, err = -1; in latency_switch_event()
1122 timestamp0 = sched->cpu_last_switched[cpu]; in latency_switch_event()
1123 sched->cpu_last_switched[cpu] = timestamp; in latency_switch_event()
1125 delta = timestamp - timestamp0; in latency_switch_event()
1131 return -1; in latency_switch_event()
1134 sched_out = machine__findnew_thread(machine, -1, prev_pid); in latency_switch_event()
1135 sched_in = machine__findnew_thread(machine, -1, next_pid); in latency_switch_event()
1139 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); in latency_switch_event()
1143 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); in latency_switch_event()
1145 pr_err("out-event: Internal tree error"); in latency_switch_event()
1150 return -1; in latency_switch_event()
1152 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); in latency_switch_event()
1156 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); in latency_switch_event()
1158 pr_err("in-event: Internal tree error"); in latency_switch_event()
1178 struct perf_sample *sample, in latency_runtime_event() argument
1181 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_runtime_event()
1182 const u64 runtime = evsel__intval(evsel, sample, "runtime"); in latency_runtime_event()
1183 struct thread *thread = machine__findnew_thread(machine, -1, pid); in latency_runtime_event()
1184 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); in latency_runtime_event()
1185 u64 timestamp = sample->time; in latency_runtime_event()
1186 int cpu = sample->cpu, err = -1; in latency_runtime_event()
1189 return -1; in latency_runtime_event()
1195 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); in latency_runtime_event()
1197 pr_err("in-event: Internal tree error"); in latency_runtime_event()
1213 struct perf_sample *sample, in latency_wakeup_event() argument
1216 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_wakeup_event()
1220 u64 timestamp = sample->time; in latency_wakeup_event()
1221 int err = -1; in latency_wakeup_event()
1223 wakee = machine__findnew_thread(machine, -1, pid); in latency_wakeup_event()
1225 return -1; in latency_wakeup_event()
1226 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); in latency_wakeup_event()
1230 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); in latency_wakeup_event()
1232 pr_err("wakeup-event: Internal tree error"); in latency_wakeup_event()
1239 BUG_ON(list_empty(&atoms->work_list)); in latency_wakeup_event()
1241 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in latency_wakeup_event()
1246 * on run queue and wakeup only change ->state to TASK_RUNNING, in latency_wakeup_event()
1247 * then we should not set the ->wake_up_time when wake up a in latency_wakeup_event()
1254 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) in latency_wakeup_event()
1257 sched->nr_timestamps++; in latency_wakeup_event()
1258 if (atom->sched_out_time > timestamp) { in latency_wakeup_event()
1259 sched->nr_unordered_timestamps++; in latency_wakeup_event()
1263 atom->state = THREAD_WAIT_CPU; in latency_wakeup_event()
1264 atom->wake_up_time = timestamp; in latency_wakeup_event()
1274 struct perf_sample *sample, in latency_migrate_task_event() argument
1277 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_migrate_task_event()
1278 u64 timestamp = sample->time; in latency_migrate_task_event()
1282 int err = -1; in latency_migrate_task_event()
1287 if (sched->profile_cpu == -1) in latency_migrate_task_event()
1290 migrant = machine__findnew_thread(machine, -1, pid); in latency_migrate_task_event()
1292 return -1; in latency_migrate_task_event()
1293 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); in latency_migrate_task_event()
1297 register_pid(sched, migrant->tid, thread__comm_str(migrant)); in latency_migrate_task_event()
1298 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); in latency_migrate_task_event()
1300 pr_err("migration-event: Internal tree error"); in latency_migrate_task_event()
1307 BUG_ON(list_empty(&atoms->work_list)); in latency_migrate_task_event()
1309 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in latency_migrate_task_event()
1310 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; in latency_migrate_task_event()
1312 sched->nr_timestamps++; in latency_migrate_task_event()
1314 if (atom->sched_out_time > timestamp) in latency_migrate_task_event()
1315 sched->nr_unordered_timestamps++; in latency_migrate_task_event()
1329 if (!work_list->nb_atoms) in output_lat_thread()
1334 if (!strcmp(thread__comm_str(work_list->thread), "swapper")) in output_lat_thread()
1337 sched->all_runtime += work_list->total_runtime; in output_lat_thread()
1338 sched->all_count += work_list->nb_atoms; in output_lat_thread()
1340 if (work_list->num_merged > 1) in output_lat_thread()
1341 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged); in output_lat_thread()
1343 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid); in output_lat_thread()
1345 for (i = 0; i < 24 - ret; i++) in output_lat_thread()
1348 avg = work_list->total_lat / work_list->nb_atoms; in output_lat_thread()
1349 timestamp__scnprintf_usec(work_list->max_lat_start, max_lat_start, sizeof(max_lat_start)); in output_lat_thread()
1350 timestamp__scnprintf_usec(work_list->max_lat_end, max_lat_end, sizeof(max_lat_end)); in output_lat_thread()
1353 (double)work_list->total_runtime / NSEC_PER_MSEC, in output_lat_thread()
1354 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC, in output_lat_thread()
1355 (double)work_list->max_lat / NSEC_PER_MSEC, in output_lat_thread()
1361 if (l->thread == r->thread) in pid_cmp()
1363 if (l->thread->tid < r->thread->tid) in pid_cmp()
1364 return -1; in pid_cmp()
1365 if (l->thread->tid > r->thread->tid) in pid_cmp()
1367 return (int)(l->thread - r->thread); in pid_cmp()
1374 if (!l->nb_atoms) in avg_cmp()
1375 return -1; in avg_cmp()
1377 if (!r->nb_atoms) in avg_cmp()
1380 avgl = l->total_lat / l->nb_atoms; in avg_cmp()
1381 avgr = r->total_lat / r->nb_atoms; in avg_cmp()
1384 return -1; in avg_cmp()
1393 if (l->max_lat < r->max_lat) in max_cmp()
1394 return -1; in max_cmp()
1395 if (l->max_lat > r->max_lat) in max_cmp()
1403 if (l->nb_atoms < r->nb_atoms) in switch_cmp()
1404 return -1; in switch_cmp()
1405 if (l->nb_atoms > r->nb_atoms) in switch_cmp()
1413 if (l->total_runtime < r->total_runtime) in runtime_cmp()
1414 return -1; in runtime_cmp()
1415 if (l->total_runtime > r->total_runtime) in runtime_cmp()
1453 if (!strcmp(available_sorts[i]->name, tok)) { in sort_dimension__add()
1454 list_add_tail(&available_sorts[i]->list, list); in sort_dimension__add()
1460 return -1; in sort_dimension__add()
1466 struct rb_root_cached *root = &sched->atom_root; in perf_sched__sort_lat()
1476 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); in perf_sched__sort_lat()
1478 if (root == &sched->atom_root) { in perf_sched__sort_lat()
1479 root = &sched->merged_atom_root; in perf_sched__sort_lat()
1486 struct perf_sample *sample, in process_sched_wakeup_event() argument
1491 if (sched->tp_handler->wakeup_event) in process_sched_wakeup_event()
1492 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); in process_sched_wakeup_event()
1519 if (!sched->map.color_pids || !thread || thread__priv(thread)) in map__findnew_thread()
1522 if (thread_map__has(sched->map.color_pids, tid)) in map__findnew_thread()
1530 struct perf_sample *sample, struct machine *machine) in map_switch_event() argument
1532 const u32 next_pid = evsel__intval(evsel, sample, "next_pid"); in map_switch_event()
1536 u64 timestamp0, timestamp = sample->time; in map_switch_event()
1538 int i, this_cpu = sample->cpu; in map_switch_event()
1546 if (this_cpu > sched->max_cpu) in map_switch_event()
1547 sched->max_cpu = this_cpu; in map_switch_event()
1549 if (sched->map.comp) { in map_switch_event()
1550 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS); in map_switch_event()
1551 if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) { in map_switch_event()
1552 sched->map.comp_cpus[cpus_nr++] = this_cpu; in map_switch_event()
1556 cpus_nr = sched->max_cpu; in map_switch_event()
1558 timestamp0 = sched->cpu_last_switched[this_cpu]; in map_switch_event()
1559 sched->cpu_last_switched[this_cpu] = timestamp; in map_switch_event()
1561 delta = timestamp - timestamp0; in map_switch_event()
1567 return -1; in map_switch_event()
1570 sched_in = map__findnew_thread(sched, machine, -1, next_pid); in map_switch_event()
1572 return -1; in map_switch_event()
1577 return -1; in map_switch_event()
1580 sched->curr_thread[this_cpu] = thread__get(sched_in); in map_switch_event()
1585 if (!tr->shortname[0]) { in map_switch_event()
1588 * Don't allocate a letter-number for swapper:0 in map_switch_event()
1591 tr->shortname[0] = '.'; in map_switch_event()
1592 tr->shortname[1] = ' '; in map_switch_event()
1594 tr->shortname[0] = sched->next_shortname1; in map_switch_event()
1595 tr->shortname[1] = sched->next_shortname2; in map_switch_event()
1597 if (sched->next_shortname1 < 'Z') { in map_switch_event()
1598 sched->next_shortname1++; in map_switch_event()
1600 sched->next_shortname1 = 'A'; in map_switch_event()
1601 if (sched->next_shortname2 < '9') in map_switch_event()
1602 sched->next_shortname2++; in map_switch_event()
1604 sched->next_shortname2 = '0'; in map_switch_event()
1611 int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i; in map_switch_event()
1612 struct thread *curr_thread = sched->curr_thread[cpu]; in map_switch_event()
1620 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu)) in map_switch_event()
1623 if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu)) in map_switch_event()
1631 if (sched->curr_thread[cpu]) { in map_switch_event()
1632 curr_tr = thread__get_runtime(sched->curr_thread[cpu]); in map_switch_event()
1635 return -1; in map_switch_event()
1637 color_fprintf(stdout, pid_color, "%2s ", curr_tr->shortname); in map_switch_event()
1642 if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu)) in map_switch_event()
1647 if (new_shortname || tr->comm_changed || (verbose > 0 && sched_in->tid)) { in map_switch_event()
1654 tr->shortname, thread__comm_str(sched_in), sched_in->tid); in map_switch_event()
1655 tr->comm_changed = false; in map_switch_event()
1658 if (sched->map.comp && new_cpu) in map_switch_event()
1671 struct perf_sample *sample, in process_sched_switch_event() argument
1675 int this_cpu = sample->cpu, err = 0; in process_sched_switch_event()
1676 u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in process_sched_switch_event()
1677 next_pid = evsel__intval(evsel, sample, "next_pid"); in process_sched_switch_event()
1679 if (sched->curr_pid[this_cpu] != (u32)-1) { in process_sched_switch_event()
1684 if (sched->curr_pid[this_cpu] != prev_pid) in process_sched_switch_event()
1685 sched->nr_context_switch_bugs++; in process_sched_switch_event()
1688 if (sched->tp_handler->switch_event) in process_sched_switch_event()
1689 err = sched->tp_handler->switch_event(sched, evsel, sample, machine); in process_sched_switch_event()
1691 sched->curr_pid[this_cpu] = next_pid; in process_sched_switch_event()
1697 struct perf_sample *sample, in process_sched_runtime_event() argument
1702 if (sched->tp_handler->runtime_event) in process_sched_runtime_event()
1703 return sched->tp_handler->runtime_event(sched, evsel, sample, machine); in process_sched_runtime_event()
1710 struct perf_sample *sample, in perf_sched__process_fork_event() argument
1716 perf_event__process_fork(tool, event, sample, machine); in perf_sched__process_fork_event()
1719 if (sched->tp_handler->fork_event) in perf_sched__process_fork_event()
1720 return sched->tp_handler->fork_event(sched, event, machine); in perf_sched__process_fork_event()
1727 struct perf_sample *sample, in process_sched_migrate_task_event() argument
1732 if (sched->tp_handler->migrate_task_event) in process_sched_migrate_task_event()
1733 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); in process_sched_migrate_task_event()
1740 struct perf_sample *sample,
1745 struct perf_sample *sample, in perf_sched__process_tracepoint_sample() argument
1751 if (evsel->handler != NULL) { in perf_sched__process_tracepoint_sample()
1752 tracepoint_handler f = evsel->handler; in perf_sched__process_tracepoint_sample()
1753 err = f(tool, evsel, sample, machine); in perf_sched__process_tracepoint_sample()
1761 struct perf_sample *sample, in perf_sched__process_comm() argument
1768 err = perf_event__process_comm(tool, event, sample, machine); in perf_sched__process_comm()
1772 thread = machine__find_thread(machine, sample->pid, sample->tid); in perf_sched__process_comm()
1775 return -1; in perf_sched__process_comm()
1781 return -1; in perf_sched__process_comm()
1784 tr->comm_changed = true; in perf_sched__process_comm()
1803 .force = sched->force, in perf_sched__read_events()
1805 int rc = -1; in perf_sched__read_events()
1807 session = perf_session__new(&data, false, &sched->tool); in perf_sched__read_events()
1813 symbol__init(&session->header.env); in perf_sched__read_events()
1818 if (perf_session__has_traces(session, "record -R")) { in perf_sched__read_events()
1825 sched->nr_events = session->evlist->stats.nr_events[0]; in perf_sched__read_events()
1826 sched->nr_lost_events = session->evlist->stats.total_lost; in perf_sched__read_events()
1827 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; in perf_sched__read_events()
1845 nsecs -= msecs * NSEC_PER_MSEC; in print_sched_time()
1852 * first time it is used.
1856 struct evsel_runtime *r = evsel->priv; in evsel__get_runtime()
1860 evsel->priv = r; in evsel__get_runtime()
1867 * save last time event was seen per cpu
1876 if ((cpu >= r->ncpu) || (r->last_time == NULL)) { in evsel__save_time()
1878 void *p = r->last_time; in evsel__save_time()
1880 p = realloc(r->last_time, n * sizeof(u64)); in evsel__save_time()
1884 r->last_time = p; in evsel__save_time()
1885 for (i = r->ncpu; i < n; ++i) in evsel__save_time()
1886 r->last_time[i] = (u64) 0; in evsel__save_time()
1888 r->ncpu = n; in evsel__save_time()
1891 r->last_time[cpu] = timestamp; in evsel__save_time()
1894 /* returns last time this event was seen on the given cpu */
1899 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu)) in evsel__get_time()
1902 return r->last_time[cpu]; in evsel__get_time()
1911 pid_t tid = thread->tid; in timehist_get_commstr()
1912 pid_t pid = thread->pid_; in timehist_get_commstr()
1932 u32 ncpus = sched->max_cpu + 1; in timehist_header()
1935 printf("%15s %6s ", "time", "cpu"); in timehist_header()
1937 if (sched->show_cpu_visual) { in timehist_header()
1947 printf(" %-*s %9s %9s %9s", comm_width, in timehist_header()
1948 "task name", "wait time", "sch delay", "run time"); in timehist_header()
1950 if (sched->show_state) in timehist_header()
1958 printf("%15s %-6s ", "", ""); in timehist_header()
1960 if (sched->show_cpu_visual) in timehist_header()
1963 printf(" %-*s %9s %9s %9s", comm_width, in timehist_header()
1966 if (sched->show_state) in timehist_header()
1976 if (sched->show_cpu_visual) in timehist_header()
1983 if (sched->show_state) in timehist_header()
1995 if (thread->tid == 0) in task_state_char()
1998 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; in task_state_char()
2003 struct perf_sample *sample, in timehist_print_sample() argument
2009 const char *next_comm = evsel__strval(evsel, sample, "next_comm"); in timehist_print_sample()
2010 const u32 next_pid = evsel__intval(evsel, sample, "next_pid"); in timehist_print_sample()
2011 u32 max_cpus = sched->max_cpu + 1; in timehist_print_sample()
2016 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) in timehist_print_sample()
2020 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_sample()
2022 if (sched->show_cpu_visual) { in timehist_print_sample()
2029 if (i == sample->cpu) in timehist_print_sample()
2030 c = (thread->tid == 0) ? 'i' : 's'; in timehist_print_sample()
2038 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_sample()
2040 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt; in timehist_print_sample()
2043 print_sched_time(tr->dt_delay, 6); in timehist_print_sample()
2044 print_sched_time(tr->dt_run, 6); in timehist_print_sample()
2046 if (sched->show_state) in timehist_print_sample()
2049 if (sched->show_next) { in timehist_print_sample()
2051 printf(" %-*s", comm_width, nstr); in timehist_print_sample()
2054 if (sched->show_wakeups && !sched->show_next) in timehist_print_sample()
2055 printf(" %-*s", comm_width, ""); in timehist_print_sample()
2057 if (thread->tid == 0) in timehist_print_sample()
2060 if (sched->show_callchain) in timehist_print_sample()
2063 sample__fprintf_sym(sample, al, 0, in timehist_print_sample()
2074 * Explanation of delta-time stats:
2076 * t = time of current schedule out event
2077 * tprev = time of previous sched out event
2078 * also time of schedule-in event for current task
2079 * last_time = time of last sched change event for current task
2080 * (i.e, time process was last scheduled out)
2081 * ready_to_run = time of wakeup for current task
2083 * -----|------------|------------|------------|------
2085 * time to run
2087 * |-------- dt_wait --------|
2088 * |- dt_delay -|-- dt_run --|
2090 * dt_run = run time of current task
2091 * dt_wait = time between last schedule out event for task and tprev
2092 * represents time spent off the cpu
2093 * dt_delay = time between wakeup and schedule-in of task
2099 r->dt_delay = 0; in timehist_update_runtime_stats()
2100 r->dt_sleep = 0; in timehist_update_runtime_stats()
2101 r->dt_iowait = 0; in timehist_update_runtime_stats()
2102 r->dt_preempt = 0; in timehist_update_runtime_stats()
2103 r->dt_run = 0; in timehist_update_runtime_stats()
2106 r->dt_run = t - tprev; in timehist_update_runtime_stats()
2107 if (r->ready_to_run) { in timehist_update_runtime_stats()
2108 if (r->ready_to_run > tprev) in timehist_update_runtime_stats()
2109 pr_debug("time travel: wakeup time for task > previous sched_switch event\n"); in timehist_update_runtime_stats()
2111 r->dt_delay = tprev - r->ready_to_run; in timehist_update_runtime_stats()
2114 if (r->last_time > tprev) in timehist_update_runtime_stats()
2115 pr_debug("time travel: last sched out time for task > previous sched_switch event\n"); in timehist_update_runtime_stats()
2116 else if (r->last_time) { in timehist_update_runtime_stats()
2117 u64 dt_wait = tprev - r->last_time; in timehist_update_runtime_stats()
2119 if (r->last_state == TASK_RUNNING) in timehist_update_runtime_stats()
2120 r->dt_preempt = dt_wait; in timehist_update_runtime_stats()
2121 else if (r->last_state == TASK_UNINTERRUPTIBLE) in timehist_update_runtime_stats()
2122 r->dt_iowait = dt_wait; in timehist_update_runtime_stats()
2124 r->dt_sleep = dt_wait; in timehist_update_runtime_stats()
2128 update_stats(&r->run_stats, r->dt_run); in timehist_update_runtime_stats()
2130 r->total_run_time += r->dt_run; in timehist_update_runtime_stats()
2131 r->total_delay_time += r->dt_delay; in timehist_update_runtime_stats()
2132 r->total_sleep_time += r->dt_sleep; in timehist_update_runtime_stats()
2133 r->total_iowait_time += r->dt_iowait; in timehist_update_runtime_stats()
2134 r->total_preempt_time += r->dt_preempt; in timehist_update_runtime_stats()
2137 static bool is_idle_sample(struct perf_sample *sample, in is_idle_sample() argument
2142 return evsel__intval(evsel, sample, "prev_pid") == 0; in is_idle_sample()
2144 return sample->pid == 0; in is_idle_sample()
2148 struct perf_sample *sample, in save_task_callchain() argument
2155 /* want main thread for process - has maps */ in save_task_callchain()
2156 thread = machine__findnew_thread(machine, sample->pid, sample->pid); in save_task_callchain()
2158 pr_debug("Failed to get thread for pid %d.\n", sample->pid); in save_task_callchain()
2162 if (!sched->show_callchain || sample->callchain == NULL) in save_task_callchain()
2165 if (thread__resolve_callchain(thread, cursor, evsel, sample, in save_task_callchain()
2166 NULL, NULL, sched->max_stack + 2) != 0) { in save_task_callchain()
2183 sym = node->ms.sym; in save_task_callchain()
2185 if (!strcmp(sym->name, "schedule") || in save_task_callchain()
2186 !strcmp(sym->name, "__schedule") || in save_task_callchain()
2187 !strcmp(sym->name, "preempt_schedule")) in save_task_callchain()
2188 sym->ignore = 1; in save_task_callchain()
2203 return -ENOMEM; in init_idle_thread()
2205 init_stats(&itr->tr.run_stats); in init_idle_thread()
2206 callchain_init(&itr->callchain); in init_idle_thread()
2207 callchain_cursor_reset(&itr->cursor); in init_idle_thread()
2223 return -ENOMEM; in init_idle_threads()
2231 return -ENOMEM; in init_idle_threads()
2292 struct perf_sample *sample) in save_idle_callchain() argument
2294 if (!sched->show_callchain || sample->callchain == NULL) in save_idle_callchain()
2297 callchain_cursor__copy(&itr->cursor, &callchain_cursor); in save_idle_callchain()
2301 struct perf_sample *sample, in timehist_get_thread() argument
2307 if (is_idle_sample(sample, evsel)) { in timehist_get_thread()
2308 thread = get_idle_thread(sample->cpu); in timehist_get_thread()
2310 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); in timehist_get_thread()
2313 /* there were samples with tid 0 but non-zero pid */ in timehist_get_thread()
2314 thread = machine__findnew_thread(machine, sample->pid, in timehist_get_thread()
2315 sample->tid ?: sample->pid); in timehist_get_thread()
2317 pr_debug("Failed to get thread for tid %d. skipping sample.\n", in timehist_get_thread()
2318 sample->tid); in timehist_get_thread()
2321 save_task_callchain(sched, sample, evsel, machine); in timehist_get_thread()
2322 if (sched->idle_hist) { in timehist_get_thread()
2326 idle = get_idle_thread(sample->cpu); in timehist_get_thread()
2328 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); in timehist_get_thread()
2336 itr->last_thread = thread; in timehist_get_thread()
2339 if (evsel__intval(evsel, sample, "next_pid") == 0) in timehist_get_thread()
2340 save_idle_callchain(sched, itr, sample); in timehist_get_thread()
2350 struct perf_sample *sample) in timehist_skip_sample() argument
2356 sched->skipped_samples++; in timehist_skip_sample()
2359 if (sched->idle_hist) { in timehist_skip_sample()
2362 else if (evsel__intval(evsel, sample, "prev_pid") != 0 && in timehist_skip_sample()
2363 evsel__intval(evsel, sample, "next_pid") != 0) in timehist_skip_sample()
2372 struct perf_sample *sample, in timehist_print_wakeup_event() argument
2379 thread = machine__findnew_thread(machine, sample->pid, sample->tid); in timehist_print_wakeup_event()
2384 if (timehist_skip_sample(sched, thread, evsel, sample) && in timehist_print_wakeup_event()
2385 timehist_skip_sample(sched, awakened, evsel, sample)) { in timehist_print_wakeup_event()
2389 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in timehist_print_wakeup_event()
2390 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_wakeup_event()
2391 if (sched->show_cpu_visual) in timehist_print_wakeup_event()
2392 printf(" %*s ", sched->max_cpu + 1, ""); in timehist_print_wakeup_event()
2394 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_wakeup_event()
2407 struct perf_sample *sample __maybe_unused, in timehist_sched_wakeup_ignore()
2416 struct perf_sample *sample, in timehist_sched_wakeup_event() argument
2422 /* want pid of awakened task not pid in sample */ in timehist_sched_wakeup_event()
2423 const u32 pid = evsel__intval(evsel, sample, "pid"); in timehist_sched_wakeup_event()
2427 return -1; in timehist_sched_wakeup_event()
2431 return -1; in timehist_sched_wakeup_event()
2433 if (tr->ready_to_run == 0) in timehist_sched_wakeup_event()
2434 tr->ready_to_run = sample->time; in timehist_sched_wakeup_event()
2437 if (sched->show_wakeups && in timehist_sched_wakeup_event()
2438 !perf_time__skip_sample(&sched->ptime, sample->time)) in timehist_sched_wakeup_event()
2439 timehist_print_wakeup_event(sched, evsel, sample, machine, thread); in timehist_sched_wakeup_event()
2446 struct perf_sample *sample, in timehist_print_migration_event() argument
2452 u32 max_cpus = sched->max_cpu + 1; in timehist_print_migration_event()
2455 if (sched->summary_only) in timehist_print_migration_event()
2458 max_cpus = sched->max_cpu + 1; in timehist_print_migration_event()
2459 ocpu = evsel__intval(evsel, sample, "orig_cpu"); in timehist_print_migration_event()
2460 dcpu = evsel__intval(evsel, sample, "dest_cpu"); in timehist_print_migration_event()
2462 thread = machine__findnew_thread(machine, sample->pid, sample->tid); in timehist_print_migration_event()
2466 if (timehist_skip_sample(sched, thread, evsel, sample) && in timehist_print_migration_event()
2467 timehist_skip_sample(sched, migrated, evsel, sample)) { in timehist_print_migration_event()
2471 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in timehist_print_migration_event()
2472 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_migration_event()
2474 if (sched->show_cpu_visual) { in timehist_print_migration_event()
2480 c = (i == sample->cpu) ? 'm' : ' '; in timehist_print_migration_event()
2486 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_migration_event()
2500 struct perf_sample *sample, in timehist_migrate_task_event() argument
2506 /* want pid of migrated task not pid in sample */ in timehist_migrate_task_event()
2507 const u32 pid = evsel__intval(evsel, sample, "pid"); in timehist_migrate_task_event()
2511 return -1; in timehist_migrate_task_event()
2515 return -1; in timehist_migrate_task_event()
2517 tr->migrations++; in timehist_migrate_task_event()
2520 timehist_print_migration_event(sched, evsel, sample, machine, thread); in timehist_migrate_task_event()
2528 struct perf_sample *sample, in timehist_sched_change_event() argument
2532 struct perf_time_interval *ptime = &sched->ptime; in timehist_sched_change_event()
2536 u64 tprev, t = sample->time; in timehist_sched_change_event()
2538 int state = evsel__intval(evsel, sample, "prev_state"); in timehist_sched_change_event()
2540 if (machine__resolve(machine, &al, sample) < 0) { in timehist_sched_change_event()
2542 event->header.type); in timehist_sched_change_event()
2543 rc = -1; in timehist_sched_change_event()
2547 thread = timehist_get_thread(sched, sample, machine, evsel); in timehist_sched_change_event()
2549 rc = -1; in timehist_sched_change_event()
2553 if (timehist_skip_sample(sched, thread, evsel, sample)) in timehist_sched_change_event()
2558 rc = -1; in timehist_sched_change_event()
2562 tprev = evsel__get_time(evsel, sample->cpu); in timehist_sched_change_event()
2565 * If start time given: in timehist_sched_change_event()
2566 * - sample time is under window user cares about - skip sample in timehist_sched_change_event()
2567 * - tprev is under window user cares about - reset to start of window in timehist_sched_change_event()
2569 if (ptime->start && ptime->start > t) in timehist_sched_change_event()
2572 if (tprev && ptime->start > tprev) in timehist_sched_change_event()
2573 tprev = ptime->start; in timehist_sched_change_event()
2576 * If end time given: in timehist_sched_change_event()
2577 * - previous sched event is out of window - we are done in timehist_sched_change_event()
2578 * - sample time is beyond window user cares about - reset it in timehist_sched_change_event()
2579 * to close out stats for time window interest in timehist_sched_change_event()
2581 if (ptime->end) { in timehist_sched_change_event()
2582 if (tprev > ptime->end) in timehist_sched_change_event()
2585 if (t > ptime->end) in timehist_sched_change_event()
2586 t = ptime->end; in timehist_sched_change_event()
2589 if (!sched->idle_hist || thread->tid == 0) { in timehist_sched_change_event()
2590 if (!cpu_list || test_bit(sample->cpu, cpu_bitmap)) in timehist_sched_change_event()
2593 if (sched->idle_hist) { in timehist_sched_change_event()
2597 BUG_ON(thread->tid != 0); in timehist_sched_change_event()
2599 if (itr->last_thread == NULL) in timehist_sched_change_event()
2602 /* add current idle time as last thread's runtime */ in timehist_sched_change_event()
2603 last_tr = thread__get_runtime(itr->last_thread); in timehist_sched_change_event()
2609 * remove delta time of last thread as it's not updated in timehist_sched_change_event()
2611 * time. we only care total run time and run stat. in timehist_sched_change_event()
2613 last_tr->dt_run = 0; in timehist_sched_change_event()
2614 last_tr->dt_delay = 0; in timehist_sched_change_event()
2615 last_tr->dt_sleep = 0; in timehist_sched_change_event()
2616 last_tr->dt_iowait = 0; in timehist_sched_change_event()
2617 last_tr->dt_preempt = 0; in timehist_sched_change_event()
2619 if (itr->cursor.nr) in timehist_sched_change_event()
2620 callchain_append(&itr->callchain, &itr->cursor, t - tprev); in timehist_sched_change_event()
2622 itr->last_thread = NULL; in timehist_sched_change_event()
2626 if (!sched->summary_only) in timehist_sched_change_event()
2627 timehist_print_sample(sched, evsel, sample, &al, thread, t, state); in timehist_sched_change_event()
2630 if (sched->hist_time.start == 0 && t >= ptime->start) in timehist_sched_change_event()
2631 sched->hist_time.start = t; in timehist_sched_change_event()
2632 if (ptime->end == 0 || t <= ptime->end) in timehist_sched_change_event()
2633 sched->hist_time.end = t; in timehist_sched_change_event()
2636 /* time of this sched_switch event becomes last time task seen */ in timehist_sched_change_event()
2637 tr->last_time = sample->time; in timehist_sched_change_event()
2639 /* last state is used to determine where to account wait time */ in timehist_sched_change_event()
2640 tr->last_state = state; in timehist_sched_change_event()
2642 /* sched out event for task so reset ready to run time */ in timehist_sched_change_event()
2643 tr->ready_to_run = 0; in timehist_sched_change_event()
2646 evsel__save_time(evsel, sample->time, sample->cpu); in timehist_sched_change_event()
2654 struct perf_sample *sample, in timehist_sched_switch_event() argument
2657 return timehist_sched_change_event(tool, event, evsel, sample, machine); in timehist_sched_switch_event()
2662 struct perf_sample *sample, in process_lost() argument
2667 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in process_lost()
2669 printf("lost %" PRI_lu64 " events on cpu %d\n", event->lost.lost, sample->cpu); in process_lost()
2678 double mean = avg_stats(&r->run_stats); in print_thread_runtime()
2682 comm_width, timehist_get_commstr(t), t->ppid, in print_thread_runtime()
2683 (u64) r->run_stats.n); in print_thread_runtime()
2685 print_sched_time(r->total_run_time, 8); in print_thread_runtime()
2686 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean); in print_thread_runtime()
2687 print_sched_time(r->run_stats.min, 6); in print_thread_runtime()
2691 print_sched_time(r->run_stats.max, 6); in print_thread_runtime()
2694 printf(" %5" PRIu64, r->migrations); in print_thread_runtime()
2702 comm_width, timehist_get_commstr(t), t->ppid, in print_thread_waittime()
2703 (u64) r->run_stats.n); in print_thread_waittime()
2705 print_sched_time(r->total_run_time, 8); in print_thread_waittime()
2706 print_sched_time(r->total_sleep_time, 6); in print_thread_waittime()
2708 print_sched_time(r->total_iowait_time, 6); in print_thread_waittime()
2710 print_sched_time(r->total_preempt_time, 6); in print_thread_waittime()
2712 print_sched_time(r->total_delay_time, 6); in print_thread_waittime()
2732 if (r && r->run_stats.n) { in __show_thread_runtime()
2733 stats->task_count++; in __show_thread_runtime()
2734 stats->sched_count += r->run_stats.n; in __show_thread_runtime()
2735 stats->total_run_time += r->total_run_time; in __show_thread_runtime()
2737 if (stats->sched->show_state) in __show_thread_runtime()
2748 if (t->dead) in show_thread_runtime()
2756 if (!t->dead) in show_deadthread_runtime()
2764 const char *sep = " <- "; in callchain__fprintf_folded()
2773 ret = callchain__fprintf_folded(fp, node->parent); in callchain__fprintf_folded()
2776 list_for_each_entry(chain, &node->val, list) { in callchain__fprintf_folded()
2777 if (chain->ip >= PERF_CONTEXT_MAX) in callchain__fprintf_folded()
2779 if (chain->ms.sym && chain->ms.sym->ignore) in callchain__fprintf_folded()
2797 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains"); in timehist_print_idlehist_callchain()
2806 print_sched_time(chain->hit, 12); in timehist_print_idlehist_callchain()
2808 ret += fprintf(fp, " %8d ", chain->count); in timehist_print_idlehist_callchain()
2819 struct machine *m = &session->machines.host; in timehist_print_summary()
2825 u64 hist_time = sched->hist_time.end - sched->hist_time.start; in timehist_print_summary()
2830 if (sched->idle_hist) { in timehist_print_summary()
2831 printf("\nIdle-time summary\n"); in timehist_print_summary()
2832 printf("%*s parent sched-out ", comm_width, "comm"); in timehist_print_summary()
2833 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n"); in timehist_print_summary()
2834 } else if (sched->show_state) { in timehist_print_summary()
2835 printf("\nWait-time summary\n"); in timehist_print_summary()
2836 printf("%*s parent sched-in ", comm_width, "comm"); in timehist_print_summary()
2837 printf(" run-time sleep iowait preempt delay\n"); in timehist_print_summary()
2840 printf("%*s parent sched-in ", comm_width, "comm"); in timehist_print_summary()
2841 printf(" run-time min-run avg-run max-run stddev migrations\n"); in timehist_print_summary()
2845 sched->show_state ? "(msec)" : "%"); in timehist_print_summary()
2859 if (sched->skipped_samples && !sched->idle_hist) in timehist_print_summary()
2872 if (r && r->run_stats.n) { in timehist_print_summary()
2873 totals.sched_count += r->run_stats.n; in timehist_print_summary()
2875 print_sched_time(r->total_run_time, 6); in timehist_print_summary()
2876 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time); in timehist_print_summary()
2878 printf(" CPU %2d idle entire time window\n", i); in timehist_print_summary()
2881 if (sched->idle_hist && sched->show_callchain) { in timehist_print_summary()
2899 callchain_param.sort(&itr->sorted_root.rb_root, &itr->callchain, in timehist_print_summary()
2903 print_sched_time(itr->tr.total_run_time, 6); in timehist_print_summary()
2905 timehist_print_idlehist_callchain(&itr->sorted_root); in timehist_print_summary()
2915 printf(" Total run time (msec): "); in timehist_print_summary()
2919 printf(" Total scheduling time (msec): "); in timehist_print_summary()
2921 printf(" (x %d)\n", sched->max_cpu); in timehist_print_summary()
2927 struct perf_sample *sample,
2932 struct perf_sample *sample, in perf_timehist__process_sample() argument
2938 int this_cpu = sample->cpu; in perf_timehist__process_sample()
2940 if (this_cpu > sched->max_cpu) in perf_timehist__process_sample()
2941 sched->max_cpu = this_cpu; in perf_timehist__process_sample()
2943 if (evsel->handler != NULL) { in perf_timehist__process_sample()
2944 sched_handler f = evsel->handler; in perf_timehist__process_sample()
2946 err = f(tool, event, evsel, sample, machine); in perf_timehist__process_sample()
2958 list_for_each_entry(evsel, &evlist->core.entries, core.node) { in timehist_check_attr()
2962 return -1; in timehist_check_attr()
2965 if (sched->show_callchain && !evsel__has_callchain(evsel)) { in timehist_check_attr()
2967 sched->show_callchain = 0; in timehist_check_attr()
2989 .force = sched->force, in perf_sched__timehist()
2994 int err = -1; in perf_sched__timehist()
2999 sched->tool.sample = perf_timehist__process_sample; in perf_sched__timehist()
3000 sched->tool.mmap = perf_event__process_mmap; in perf_sched__timehist()
3001 sched->tool.comm = perf_event__process_comm; in perf_sched__timehist()
3002 sched->tool.exit = perf_event__process_exit; in perf_sched__timehist()
3003 sched->tool.fork = perf_event__process_fork; in perf_sched__timehist()
3004 sched->tool.lost = process_lost; in perf_sched__timehist()
3005 sched->tool.attr = perf_event__process_attr; in perf_sched__timehist()
3006 sched->tool.tracing_data = perf_event__process_tracing_data; in perf_sched__timehist()
3007 sched->tool.build_id = perf_event__process_build_id; in perf_sched__timehist()
3009 sched->tool.ordered_events = true; in perf_sched__timehist()
3010 sched->tool.ordering_requires_timestamps = true; in perf_sched__timehist()
3012 symbol_conf.use_callchain = sched->show_callchain; in perf_sched__timehist()
3014 session = perf_session__new(&data, false, &sched->tool); in perf_sched__timehist()
3024 evlist = session->evlist; in perf_sched__timehist()
3026 symbol__init(&session->header.env); in perf_sched__timehist()
3028 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) { in perf_sched__timehist()
3029 pr_err("Invalid time string\n"); in perf_sched__timehist()
3030 return -EINVAL; in perf_sched__timehist()
3039 if (perf_evlist__find_tracepoint_by_name(session->evlist, in perf_sched__timehist()
3043 /* setup per-evsel handlers */ in perf_sched__timehist()
3048 if (!perf_evlist__find_tracepoint_by_name(session->evlist, in perf_sched__timehist()
3054 if (sched->show_migrations && in perf_sched__timehist()
3058 /* pre-allocate struct for per-CPU idle stats */ in perf_sched__timehist()
3059 sched->max_cpu = session->header.env.nr_cpus_online; in perf_sched__timehist()
3060 if (sched->max_cpu == 0) in perf_sched__timehist()
3061 sched->max_cpu = 4; in perf_sched__timehist()
3062 if (init_idle_threads(sched->max_cpu)) in perf_sched__timehist()
3066 if (sched->summary_only) in perf_sched__timehist()
3067 sched->summary = sched->summary_only; in perf_sched__timehist()
3069 if (!sched->summary_only) in perf_sched__timehist()
3078 sched->nr_events = evlist->stats.nr_events[0]; in perf_sched__timehist()
3079 sched->nr_lost_events = evlist->stats.total_lost; in perf_sched__timehist()
3080 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST]; in perf_sched__timehist()
3082 if (sched->summary) in perf_sched__timehist()
3095 if (sched->nr_unordered_timestamps && sched->nr_timestamps) { in print_bad_events()
3097 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, in print_bad_events()
3098 sched->nr_unordered_timestamps, sched->nr_timestamps); in print_bad_events()
3100 if (sched->nr_lost_events && sched->nr_events) { in print_bad_events()
3102 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, in print_bad_events()
3103 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); in print_bad_events()
3105 if (sched->nr_context_switch_bugs && sched->nr_timestamps) { in print_bad_events()
3107 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, in print_bad_events()
3108 sched->nr_context_switch_bugs, sched->nr_timestamps); in print_bad_events()
3109 if (sched->nr_lost_events) in print_bad_events()
3117 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in __merge_work_atoms()
3119 const char *comm = thread__comm_str(data->thread), *this_comm; in __merge_work_atoms()
3128 this_comm = thread__comm_str(this->thread); in __merge_work_atoms()
3131 new = &((*new)->rb_left); in __merge_work_atoms()
3133 new = &((*new)->rb_right); in __merge_work_atoms()
3136 this->num_merged++; in __merge_work_atoms()
3137 this->total_runtime += data->total_runtime; in __merge_work_atoms()
3138 this->nb_atoms += data->nb_atoms; in __merge_work_atoms()
3139 this->total_lat += data->total_lat; in __merge_work_atoms()
3140 list_splice(&data->work_list, &this->work_list); in __merge_work_atoms()
3141 if (this->max_lat < data->max_lat) { in __merge_work_atoms()
3142 this->max_lat = data->max_lat; in __merge_work_atoms()
3143 this->max_lat_start = data->max_lat_start; in __merge_work_atoms()
3144 this->max_lat_end = data->max_lat_end; in __merge_work_atoms()
3151 data->num_merged++; in __merge_work_atoms()
3152 rb_link_node(&data->node, parent, new); in __merge_work_atoms()
3153 rb_insert_color_cached(&data->node, root, leftmost); in __merge_work_atoms()
3161 if (sched->skip_merge) in perf_sched__merge_lat()
3164 while ((node = rb_first_cached(&sched->atom_root))) { in perf_sched__merge_lat()
3165 rb_erase_cached(node, &sched->atom_root); in perf_sched__merge_lat()
3167 __merge_work_atoms(&sched->merged_atom_root, data); in perf_sched__merge_lat()
3178 return -1; in perf_sched__lat()
3183 …tf("\n -------------------------------------------------------------------------------------------… in perf_sched__lat()
3185 …intf(" -------------------------------------------------------------------------------------------… in perf_sched__lat()
3187 next = rb_first_cached(&sched->sorted_atom_root); in perf_sched__lat()
3195 thread__zput(work_list->thread); in perf_sched__lat()
3198 …printf(" -----------------------------------------------------------------------------------------… in perf_sched__lat()
3200 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count); in perf_sched__lat()
3202 printf(" ---------------------------------------------------\n"); in perf_sched__lat()
3214 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); in setup_map_cpus()
3216 if (sched->map.comp) { in setup_map_cpus()
3217 sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int)); in setup_map_cpus()
3218 if (!sched->map.comp_cpus) in setup_map_cpus()
3219 return -1; in setup_map_cpus()
3222 if (!sched->map.cpus_str) in setup_map_cpus()
3225 map = perf_cpu_map__new(sched->map.cpus_str); in setup_map_cpus()
3227 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); in setup_map_cpus()
3228 return -1; in setup_map_cpus()
3231 sched->map.cpus = map; in setup_map_cpus()
3239 if (!sched->map.color_pids_str) in setup_color_pids()
3242 map = thread_map__new_by_tid_str(sched->map.color_pids_str); in setup_color_pids()
3244 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str); in setup_color_pids()
3245 return -1; in setup_color_pids()
3248 sched->map.color_pids = map; in setup_color_pids()
3256 if (!sched->map.color_cpus_str) in setup_color_cpus()
3259 map = perf_cpu_map__new(sched->map.color_cpus_str); in setup_color_cpus()
3261 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str); in setup_color_cpus()
3262 return -1; in setup_color_cpus()
3265 sched->map.color_cpus = map; in setup_color_cpus()
3272 return -1; in perf_sched__map()
3275 return -1; in perf_sched__map()
3278 return -1; in perf_sched__map()
3282 return -1; in perf_sched__map()
3297 return -1; in perf_sched__replay()
3299 printf("nr_run_events: %ld\n", sched->nr_run_events); in perf_sched__replay()
3300 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); in perf_sched__replay()
3301 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); in perf_sched__replay()
3303 if (sched->targetless_wakeups) in perf_sched__replay()
3304 printf("target-less wakeups: %ld\n", sched->targetless_wakeups); in perf_sched__replay()
3305 if (sched->multitarget_wakeups) in perf_sched__replay()
3306 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); in perf_sched__replay()
3307 if (sched->nr_run_events_optimized) in perf_sched__replay()
3309 sched->nr_run_events_optimized); in perf_sched__replay()
3315 printf("------------------------------------------------------------\n"); in perf_sched__replay()
3316 for (i = 0; i < sched->replay_repeat; i++) in perf_sched__replay()
3325 char *tmp, *tok, *str = strdup(sched->sort_order); in setup_sorting()
3329 if (sort_dimension__add(tok, &sched->sort_list) < 0) { in setup_sorting()
3331 "Unknown --sort key: `%s'", tok); in setup_sorting()
3337 sort_dimension__add("pid", &sched->cmp_pid); in setup_sorting()
3346 "-a", in __cmd_record()
3347 "-R", in __cmd_record()
3348 "-m", "1024", in __cmd_record()
3349 "-c", "1", in __cmd_record()
3350 "-e", "sched:sched_switch", in __cmd_record()
3351 "-e", "sched:sched_stat_wait", in __cmd_record()
3352 "-e", "sched:sched_stat_sleep", in __cmd_record()
3353 "-e", "sched:sched_stat_iowait", in __cmd_record()
3354 "-e", "sched:sched_stat_runtime", in __cmd_record()
3355 "-e", "sched:sched_process_fork", in __cmd_record()
3356 "-e", "sched:sched_wakeup_new", in __cmd_record()
3357 "-e", "sched:sched_migrate_task", in __cmd_record()
3362 * +2 for either "-e", "sched:sched_wakeup" or in __cmd_record()
3363 * "-e", "sched:sched_waking" in __cmd_record()
3365 rec_argc = ARRAY_SIZE(record_args) + 2 + argc - 1; in __cmd_record()
3369 return -ENOMEM; in __cmd_record()
3374 rec_argv[i++] = "-e"; in __cmd_record()
3394 .sample = perf_sched__process_tracepoint_sample, in cmd_sched()
3407 .profile_cpu = -1, in cmd_sched()
3419 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, in cmd_sched()
3435 "repeat the workload replay N times (-1: infinite)"), in cmd_sched()
3441 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids", in cmd_sched()
3443 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus", in cmd_sched()
3454 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain, in cmd_sched()
3456 OPT_UINTEGER(0, "max-stack", &sched.max_stack, in cmd_sched()
3462 OPT_BOOLEAN('S', "with-summary", &sched.summary, in cmd_sched()
3467 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"), in cmd_sched()
3468 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"), in cmd_sched()
3469 OPT_STRING(0, "time", &sched.time_str, "str", in cmd_sched()
3470 "Time span for analysis (start,stop)"), in cmd_sched()
3471 OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"), in cmd_sched()
3520 sched.curr_pid[i] = -1; in cmd_sched()
3570 pr_err(" Error: -s and -[n|w] are mutually exclusive.\n"); in cmd_sched()
3576 return -EINVAL; in cmd_sched()