Lines Matching +full:sample +full:- +full:time
1 // SPDX-License-Identifier: GPL-2.0
3 #include "perf-sys.h"
21 #include "util/time-utils.h"
24 #include <subcmd/parse-options.h>
25 #include "util/trace-event.h"
103 /* in tsk->exit_state */
107 /* in tsk->state again */
148 struct perf_sample *sample, struct machine *machine);
151 struct perf_sample *sample, struct machine *machine);
154 struct perf_sample *sample, struct machine *machine);
162 struct perf_sample *sample,
192 * Track the current task - that way we can know whether there's any
252 /* per thread run time data */
254 u64 last_time; /* time of previous sched in/out event */
255 u64 dt_run; /* run time */
256 u64 dt_sleep; /* time between CPU access by sleep (off cpu) */
257 u64 dt_iowait; /* time between CPU access by iowait (off cpu) */
258 u64 dt_preempt; /* time between CPU access by preempt (off cpu) */
259 u64 dt_delay; /* time between wakeup and sched-in */
260 u64 ready_to_run; /* time of wakeup */
277 /* per event run time data */
279 u64 *last_time; /* time this event was last seen per cpu */
283 /* per cpu idle time data */
312 } while (T1 + sched->run_measurement_overhead < T0 + nsecs); in burn_nsecs()
334 delta = T1-T0; in calibrate_run_measurement_overhead()
337 sched->run_measurement_overhead = min_delta; in calibrate_run_measurement_overhead()
351 delta = T1-T0; in calibrate_sleep_measurement_overhead()
354 min_delta -= 10000; in calibrate_sleep_measurement_overhead()
355 sched->sleep_measurement_overhead = min_delta; in calibrate_sleep_measurement_overhead()
364 unsigned long idx = task->nr_events; in get_new_event()
367 event->timestamp = timestamp; in get_new_event()
368 event->nr = idx; in get_new_event()
370 task->nr_events++; in get_new_event()
371 size = sizeof(struct sched_atom *) * task->nr_events; in get_new_event()
372 task->atoms = realloc(task->atoms, size); in get_new_event()
373 BUG_ON(!task->atoms); in get_new_event()
375 task->atoms[idx] = event; in get_new_event()
382 if (!task->nr_events) in last_event()
385 return task->atoms[task->nr_events - 1]; in last_event()
397 if (curr_event && curr_event->type == SCHED_EVENT_RUN) { in add_sched_event_run()
398 sched->nr_run_events_optimized++; in add_sched_event_run()
399 curr_event->duration += duration; in add_sched_event_run()
405 event->type = SCHED_EVENT_RUN; in add_sched_event_run()
406 event->duration = duration; in add_sched_event_run()
408 sched->nr_run_events++; in add_sched_event_run()
417 event->type = SCHED_EVENT_WAKEUP; in add_sched_event_wakeup()
418 event->wakee = wakee; in add_sched_event_wakeup()
421 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { in add_sched_event_wakeup()
422 sched->targetless_wakeups++; in add_sched_event_wakeup()
425 if (wakee_event->wait_sem) { in add_sched_event_wakeup()
426 sched->multitarget_wakeups++; in add_sched_event_wakeup()
430 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); in add_sched_event_wakeup()
431 sem_init(wakee_event->wait_sem, 0, 0); in add_sched_event_wakeup()
432 wakee_event->specific_wait = 1; in add_sched_event_wakeup()
433 event->wait_sem = wakee_event->wait_sem; in add_sched_event_wakeup()
435 sched->nr_wakeup_events++; in add_sched_event_wakeup()
443 event->type = SCHED_EVENT_SLEEP; in add_sched_event_sleep()
445 sched->nr_sleep_events++; in add_sched_event_sleep()
454 if (sched->pid_to_task == NULL) { in register_pid()
457 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); in register_pid()
460 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * in register_pid()
463 sched->pid_to_task[pid_max++] = NULL; in register_pid()
466 task = sched->pid_to_task[pid]; in register_pid()
472 task->pid = pid; in register_pid()
473 task->nr = sched->nr_tasks; in register_pid()
474 strcpy(task->comm, comm); in register_pid()
476 * every task starts in sleeping state - this gets ignored in register_pid()
481 sched->pid_to_task[pid] = task; in register_pid()
482 sched->nr_tasks++; in register_pid()
483 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *)); in register_pid()
484 BUG_ON(!sched->tasks); in register_pid()
485 sched->tasks[task->nr] = task; in register_pid()
488 printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm); in register_pid()
499 for (i = 0; i < sched->nr_tasks; i++) { in print_task_traces()
500 task = sched->tasks[i]; in print_task_traces()
502 task->nr, task->comm, task->pid, task->nr_events); in print_task_traces()
511 for (i = 0; i < sched->nr_tasks; i++) { in add_cross_task_wakeups()
512 task1 = sched->tasks[i]; in add_cross_task_wakeups()
514 if (j == sched->nr_tasks) in add_cross_task_wakeups()
516 task2 = sched->tasks[j]; in add_cross_task_wakeups()
526 switch (atom->type) { in perf_sched__process_event()
528 burn_nsecs(sched, atom->duration); in perf_sched__process_event()
531 if (atom->wait_sem) in perf_sched__process_event()
532 ret = sem_wait(atom->wait_sem); in perf_sched__process_event()
536 if (atom->wait_sem) in perf_sched__process_event()
537 ret = sem_post(atom->wait_sem); in perf_sched__process_event()
576 fd = sys_perf_event_open(&attr, 0, -1, -1, in self_open_counters()
581 if (sched->force) { in self_open_counters()
582 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); in self_open_counters()
583 limit.rlim_cur += sched->nr_tasks - cur_task; in self_open_counters()
588 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { in self_open_counters()
594 strcpy(info, "Have a try with -f option\n"); in self_open_counters()
624 struct task_desc *this_task = parms->task; in thread_func()
625 struct perf_sched *sched = parms->sched; in thread_func()
629 int fd = parms->fd; in thread_func()
633 sprintf(comm2, ":%s", this_task->comm); in thread_func()
638 while (!sched->thread_funcs_exit) { in thread_func()
639 ret = sem_post(&this_task->ready_for_work); in thread_func()
641 mutex_lock(&sched->start_work_mutex); in thread_func()
642 mutex_unlock(&sched->start_work_mutex); in thread_func()
646 for (i = 0; i < this_task->nr_events; i++) { in thread_func()
647 this_task->curr_event = i; in thread_func()
648 perf_sched__process_event(sched, this_task->atoms[i]); in thread_func()
652 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; in thread_func()
653 ret = sem_post(&this_task->work_done_sem); in thread_func()
656 mutex_lock(&sched->work_done_wait_mutex); in thread_func()
657 mutex_unlock(&sched->work_done_wait_mutex); in thread_func()
663 EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex) in create_tasks()
664 EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex) in create_tasks()
676 mutex_lock(&sched->start_work_mutex); in create_tasks()
677 mutex_lock(&sched->work_done_wait_mutex); in create_tasks()
678 for (i = 0; i < sched->nr_tasks; i++) { in create_tasks()
681 parms->task = task = sched->tasks[i]; in create_tasks()
682 parms->sched = sched; in create_tasks()
683 parms->fd = self_open_counters(sched, i); in create_tasks()
684 sem_init(&task->sleep_sem, 0, 0); in create_tasks()
685 sem_init(&task->ready_for_work, 0, 0); in create_tasks()
686 sem_init(&task->work_done_sem, 0, 0); in create_tasks()
687 task->curr_event = 0; in create_tasks()
688 err = pthread_create(&task->thread, &attr, thread_func, parms); in create_tasks()
694 UNLOCK_FUNCTION(sched->start_work_mutex) in destroy_tasks()
695 UNLOCK_FUNCTION(sched->work_done_wait_mutex) in destroy_tasks()
701 mutex_unlock(&sched->start_work_mutex); in destroy_tasks()
702 mutex_unlock(&sched->work_done_wait_mutex); in destroy_tasks()
704 for (i = 0; i < sched->nr_tasks; i++) { in destroy_tasks()
705 task = sched->tasks[i]; in destroy_tasks()
706 err = pthread_join(task->thread, NULL); in destroy_tasks()
708 sem_destroy(&task->sleep_sem); in destroy_tasks()
709 sem_destroy(&task->ready_for_work); in destroy_tasks()
710 sem_destroy(&task->work_done_sem); in destroy_tasks()
715 EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) in wait_for_tasks()
716 EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) in wait_for_tasks()
722 sched->start_time = get_nsecs(); in wait_for_tasks()
723 sched->cpu_usage = 0; in wait_for_tasks()
724 mutex_unlock(&sched->work_done_wait_mutex); in wait_for_tasks()
726 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
727 task = sched->tasks[i]; in wait_for_tasks()
728 ret = sem_wait(&task->ready_for_work); in wait_for_tasks()
730 sem_init(&task->ready_for_work, 0, 0); in wait_for_tasks()
732 mutex_lock(&sched->work_done_wait_mutex); in wait_for_tasks()
736 mutex_unlock(&sched->start_work_mutex); in wait_for_tasks()
738 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
739 task = sched->tasks[i]; in wait_for_tasks()
740 ret = sem_wait(&task->work_done_sem); in wait_for_tasks()
742 sem_init(&task->work_done_sem, 0, 0); in wait_for_tasks()
743 sched->cpu_usage += task->cpu_usage; in wait_for_tasks()
744 task->cpu_usage = 0; in wait_for_tasks()
748 if (!sched->runavg_cpu_usage) in wait_for_tasks()
749 sched->runavg_cpu_usage = sched->cpu_usage; in wait_for_tasks()
750 …sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage… in wait_for_tasks()
752 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; in wait_for_tasks()
753 if (!sched->runavg_parent_cpu_usage) in wait_for_tasks()
754 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; in wait_for_tasks()
755 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + in wait_for_tasks()
756 sched->parent_cpu_usage)/sched->replay_repeat; in wait_for_tasks()
758 mutex_lock(&sched->start_work_mutex); in wait_for_tasks()
760 for (i = 0; i < sched->nr_tasks; i++) { in wait_for_tasks()
761 task = sched->tasks[i]; in wait_for_tasks()
762 sem_init(&task->sleep_sem, 0, 0); in wait_for_tasks()
763 task->curr_event = 0; in wait_for_tasks()
768 EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) in run_one_test()
769 EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) in run_one_test()
777 delta = T1 - T0; in run_one_test()
778 sched->sum_runtime += delta; in run_one_test()
779 sched->nr_runs++; in run_one_test()
781 avg_delta = sched->sum_runtime / sched->nr_runs; in run_one_test()
783 fluct = avg_delta - delta; in run_one_test()
785 fluct = delta - avg_delta; in run_one_test()
786 sched->sum_fluct += fluct; in run_one_test()
787 if (!sched->run_avg) in run_one_test()
788 sched->run_avg = delta; in run_one_test()
789 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat; in run_one_test()
791 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC); in run_one_test()
793 printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC); in run_one_test()
796 (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC); in run_one_test()
801 * accurate than the sched->sum_exec_runtime based statistics: in run_one_test()
804 (double)sched->parent_cpu_usage / NSEC_PER_MSEC, in run_one_test()
805 (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC); in run_one_test()
810 if (sched->nr_sleep_corrections) in run_one_test()
811 printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); in run_one_test()
812 sched->nr_sleep_corrections = 0; in run_one_test()
823 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); in test_calibrations()
829 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); in test_calibrations()
834 struct evsel *evsel, struct perf_sample *sample, in replay_wakeup_event() argument
837 const char *comm = evsel__strval(evsel, sample, "comm"); in replay_wakeup_event()
838 const u32 pid = evsel__intval(evsel, sample, "pid"); in replay_wakeup_event()
844 printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); in replay_wakeup_event()
847 waker = register_pid(sched, sample->tid, "<unknown>"); in replay_wakeup_event()
850 add_sched_event_wakeup(sched, waker, sample->time, wakee); in replay_wakeup_event()
856 struct perf_sample *sample, in replay_switch_event() argument
859 const char *prev_comm = evsel__strval(evsel, sample, "prev_comm"), in replay_switch_event()
860 *next_comm = evsel__strval(evsel, sample, "next_comm"); in replay_switch_event()
861 const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in replay_switch_event()
862 next_pid = evsel__intval(evsel, sample, "next_pid"); in replay_switch_event()
863 const u64 prev_state = evsel__intval(evsel, sample, "prev_state"); in replay_switch_event()
865 u64 timestamp0, timestamp = sample->time; in replay_switch_event()
866 int cpu = sample->cpu; in replay_switch_event()
875 timestamp0 = sched->cpu_last_switched[cpu]; in replay_switch_event()
877 delta = timestamp - timestamp0; in replay_switch_event()
883 return -1; in replay_switch_event()
892 sched->cpu_last_switched[cpu] = timestamp; in replay_switch_event()
906 child = machine__findnew_thread(machine, event->fork.pid, in replay_fork_event()
907 event->fork.tid); in replay_fork_event()
908 parent = machine__findnew_thread(machine, event->fork.ppid, in replay_fork_event()
909 event->fork.ptid); in replay_fork_event()
948 init_stats(&r->run_stats); in thread__init_runtime()
977 ret = sort->cmp(l, r); in thread_lat_cmp()
989 struct rb_node *node = root->rb_root.rb_node; in thread_atoms_search()
1000 node = node->rb_left; in thread_atoms_search()
1002 node = node->rb_right; in thread_atoms_search()
1004 BUG_ON(thread != atoms->thread); in thread_atoms_search()
1015 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in __thread_latency_insert()
1028 new = &((*new)->rb_left); in __thread_latency_insert()
1030 new = &((*new)->rb_right); in __thread_latency_insert()
1035 rb_link_node(&data->node, parent, new); in __thread_latency_insert()
1036 rb_insert_color_cached(&data->node, root, leftmost); in __thread_latency_insert()
1044 return -1; in thread_atoms_insert()
1047 atoms->thread = thread__get(thread); in thread_atoms_insert()
1048 INIT_LIST_HEAD(&atoms->work_list); in thread_atoms_insert()
1049 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); in thread_atoms_insert()
1068 return -1; in add_sched_out_event()
1071 atom->sched_out_time = timestamp; in add_sched_out_event()
1074 atom->state = THREAD_WAIT_CPU; in add_sched_out_event()
1075 atom->wake_up_time = atom->sched_out_time; in add_sched_out_event()
1078 list_add_tail(&atom->list, &atoms->work_list); in add_sched_out_event()
1088 BUG_ON(list_empty(&atoms->work_list)); in add_runtime_event()
1090 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in add_runtime_event()
1092 atom->runtime += delta; in add_runtime_event()
1093 atoms->total_runtime += delta; in add_runtime_event()
1102 if (list_empty(&atoms->work_list)) in add_sched_in_event()
1105 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in add_sched_in_event()
1107 if (atom->state != THREAD_WAIT_CPU) in add_sched_in_event()
1110 if (timestamp < atom->wake_up_time) { in add_sched_in_event()
1111 atom->state = THREAD_IGNORE; in add_sched_in_event()
1115 atom->state = THREAD_SCHED_IN; in add_sched_in_event()
1116 atom->sched_in_time = timestamp; in add_sched_in_event()
1118 delta = atom->sched_in_time - atom->wake_up_time; in add_sched_in_event()
1119 atoms->total_lat += delta; in add_sched_in_event()
1120 if (delta > atoms->max_lat) { in add_sched_in_event()
1121 atoms->max_lat = delta; in add_sched_in_event()
1122 atoms->max_lat_start = atom->wake_up_time; in add_sched_in_event()
1123 atoms->max_lat_end = timestamp; in add_sched_in_event()
1125 atoms->nb_atoms++; in add_sched_in_event()
1130 struct perf_sample *sample, in latency_switch_event() argument
1133 const u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in latency_switch_event()
1134 next_pid = evsel__intval(evsel, sample, "next_pid"); in latency_switch_event()
1135 const u64 prev_state = evsel__intval(evsel, sample, "prev_state"); in latency_switch_event()
1138 u64 timestamp0, timestamp = sample->time; in latency_switch_event()
1139 int cpu = sample->cpu, err = -1; in latency_switch_event()
1144 timestamp0 = sched->cpu_last_switched[cpu]; in latency_switch_event()
1145 sched->cpu_last_switched[cpu] = timestamp; in latency_switch_event()
1147 delta = timestamp - timestamp0; in latency_switch_event()
1153 return -1; in latency_switch_event()
1156 sched_out = machine__findnew_thread(machine, -1, prev_pid); in latency_switch_event()
1157 sched_in = machine__findnew_thread(machine, -1, next_pid); in latency_switch_event()
1161 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); in latency_switch_event()
1165 out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); in latency_switch_event()
1167 pr_err("out-event: Internal tree error"); in latency_switch_event()
1172 return -1; in latency_switch_event()
1174 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); in latency_switch_event()
1178 in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); in latency_switch_event()
1180 pr_err("in-event: Internal tree error"); in latency_switch_event()
1200 struct perf_sample *sample, in latency_runtime_event() argument
1203 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_runtime_event()
1204 const u64 runtime = evsel__intval(evsel, sample, "runtime"); in latency_runtime_event()
1205 struct thread *thread = machine__findnew_thread(machine, -1, pid); in latency_runtime_event()
1206 struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); in latency_runtime_event()
1207 u64 timestamp = sample->time; in latency_runtime_event()
1208 int cpu = sample->cpu, err = -1; in latency_runtime_event()
1211 return -1; in latency_runtime_event()
1217 atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); in latency_runtime_event()
1219 pr_err("in-event: Internal tree error"); in latency_runtime_event()
1235 struct perf_sample *sample, in latency_wakeup_event() argument
1238 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_wakeup_event()
1242 u64 timestamp = sample->time; in latency_wakeup_event()
1243 int err = -1; in latency_wakeup_event()
1245 wakee = machine__findnew_thread(machine, -1, pid); in latency_wakeup_event()
1247 return -1; in latency_wakeup_event()
1248 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); in latency_wakeup_event()
1252 atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); in latency_wakeup_event()
1254 pr_err("wakeup-event: Internal tree error"); in latency_wakeup_event()
1261 BUG_ON(list_empty(&atoms->work_list)); in latency_wakeup_event()
1263 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in latency_wakeup_event()
1268 * on run queue and wakeup only change ->state to TASK_RUNNING, in latency_wakeup_event()
1269 * then we should not set the ->wake_up_time when wake up a in latency_wakeup_event()
1276 if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) in latency_wakeup_event()
1279 sched->nr_timestamps++; in latency_wakeup_event()
1280 if (atom->sched_out_time > timestamp) { in latency_wakeup_event()
1281 sched->nr_unordered_timestamps++; in latency_wakeup_event()
1285 atom->state = THREAD_WAIT_CPU; in latency_wakeup_event()
1286 atom->wake_up_time = timestamp; in latency_wakeup_event()
1296 struct perf_sample *sample, in latency_migrate_task_event() argument
1299 const u32 pid = evsel__intval(evsel, sample, "pid"); in latency_migrate_task_event()
1300 u64 timestamp = sample->time; in latency_migrate_task_event()
1304 int err = -1; in latency_migrate_task_event()
1309 if (sched->profile_cpu == -1) in latency_migrate_task_event()
1312 migrant = machine__findnew_thread(machine, -1, pid); in latency_migrate_task_event()
1314 return -1; in latency_migrate_task_event()
1315 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); in latency_migrate_task_event()
1320 atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); in latency_migrate_task_event()
1322 pr_err("migration-event: Internal tree error"); in latency_migrate_task_event()
1329 BUG_ON(list_empty(&atoms->work_list)); in latency_migrate_task_event()
1331 atom = list_entry(atoms->work_list.prev, struct work_atom, list); in latency_migrate_task_event()
1332 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; in latency_migrate_task_event()
1334 sched->nr_timestamps++; in latency_migrate_task_event()
1336 if (atom->sched_out_time > timestamp) in latency_migrate_task_event()
1337 sched->nr_unordered_timestamps++; in latency_migrate_task_event()
1351 if (!work_list->nb_atoms) in output_lat_thread()
1356 if (!strcmp(thread__comm_str(work_list->thread), "swapper")) in output_lat_thread()
1359 sched->all_runtime += work_list->total_runtime; in output_lat_thread()
1360 sched->all_count += work_list->nb_atoms; in output_lat_thread()
1362 if (work_list->num_merged > 1) { in output_lat_thread()
1363 ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), in output_lat_thread()
1364 work_list->num_merged); in output_lat_thread()
1366 ret = printf(" %s:%d ", thread__comm_str(work_list->thread), in output_lat_thread()
1367 thread__tid(work_list->thread)); in output_lat_thread()
1370 for (i = 0; i < 24 - ret; i++) in output_lat_thread()
1373 avg = work_list->total_lat / work_list->nb_atoms; in output_lat_thread()
1374 timestamp__scnprintf_usec(work_list->max_lat_start, max_lat_start, sizeof(max_lat_start)); in output_lat_thread()
1375 timestamp__scnprintf_usec(work_list->max_lat_end, max_lat_end, sizeof(max_lat_end)); in output_lat_thread()
1378 (double)work_list->total_runtime / NSEC_PER_MSEC, in output_lat_thread()
1379 work_list->nb_atoms, (double)avg / NSEC_PER_MSEC, in output_lat_thread()
1380 (double)work_list->max_lat / NSEC_PER_MSEC, in output_lat_thread()
1388 if (RC_CHK_ACCESS(l->thread) == RC_CHK_ACCESS(r->thread)) in pid_cmp()
1390 l_tid = thread__tid(l->thread); in pid_cmp()
1391 r_tid = thread__tid(r->thread); in pid_cmp()
1393 return -1; in pid_cmp()
1396 return (int)(RC_CHK_ACCESS(l->thread) - RC_CHK_ACCESS(r->thread)); in pid_cmp()
1403 if (!l->nb_atoms) in avg_cmp()
1404 return -1; in avg_cmp()
1406 if (!r->nb_atoms) in avg_cmp()
1409 avgl = l->total_lat / l->nb_atoms; in avg_cmp()
1410 avgr = r->total_lat / r->nb_atoms; in avg_cmp()
1413 return -1; in avg_cmp()
1422 if (l->max_lat < r->max_lat) in max_cmp()
1423 return -1; in max_cmp()
1424 if (l->max_lat > r->max_lat) in max_cmp()
1432 if (l->nb_atoms < r->nb_atoms) in switch_cmp()
1433 return -1; in switch_cmp()
1434 if (l->nb_atoms > r->nb_atoms) in switch_cmp()
1442 if (l->total_runtime < r->total_runtime) in runtime_cmp()
1443 return -1; in runtime_cmp()
1444 if (l->total_runtime > r->total_runtime) in runtime_cmp()
1482 if (!strcmp(available_sorts[i]->name, tok)) { in sort_dimension__add()
1483 list_add_tail(&available_sorts[i]->list, list); in sort_dimension__add()
1489 return -1; in sort_dimension__add()
1495 struct rb_root_cached *root = &sched->atom_root; in perf_sched__sort_lat()
1505 __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); in perf_sched__sort_lat()
1507 if (root == &sched->atom_root) { in perf_sched__sort_lat()
1508 root = &sched->merged_atom_root; in perf_sched__sort_lat()
1515 struct perf_sample *sample, in process_sched_wakeup_event() argument
1520 if (sched->tp_handler->wakeup_event) in process_sched_wakeup_event()
1521 return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); in process_sched_wakeup_event()
1528 struct perf_sample *sample __maybe_unused, in process_sched_wakeup_ignore()
1556 if (!sched->map.color_pids || !thread || thread__priv(thread)) in map__findnew_thread()
1559 if (thread_map__has(sched->map.color_pids, tid)) in map__findnew_thread()
1567 struct perf_sample *sample, struct machine *machine) in map_switch_event() argument
1569 const u32 next_pid = evsel__intval(evsel, sample, "next_pid"); in map_switch_event()
1573 u64 timestamp0, timestamp = sample->time; in map_switch_event()
1577 .cpu = sample->cpu, in map_switch_event()
1586 if (this_cpu.cpu > sched->max_cpu.cpu) in map_switch_event()
1587 sched->max_cpu = this_cpu; in map_switch_event()
1589 if (sched->map.comp) { in map_switch_event()
1590 cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS); in map_switch_event()
1591 if (!__test_and_set_bit(this_cpu.cpu, sched->map.comp_cpus_mask)) { in map_switch_event()
1592 sched->map.comp_cpus[cpus_nr++] = this_cpu; in map_switch_event()
1596 cpus_nr = sched->max_cpu.cpu; in map_switch_event()
1598 timestamp0 = sched->cpu_last_switched[this_cpu.cpu]; in map_switch_event()
1599 sched->cpu_last_switched[this_cpu.cpu] = timestamp; in map_switch_event()
1601 delta = timestamp - timestamp0; in map_switch_event()
1607 return -1; in map_switch_event()
1610 sched_in = map__findnew_thread(sched, machine, -1, next_pid); in map_switch_event()
1612 return -1; in map_switch_event()
1617 return -1; in map_switch_event()
1620 sched->curr_thread[this_cpu.cpu] = thread__get(sched_in); in map_switch_event()
1625 if (!tr->shortname[0]) { in map_switch_event()
1628 * Don't allocate a letter-number for swapper:0 in map_switch_event()
1631 tr->shortname[0] = '.'; in map_switch_event()
1632 tr->shortname[1] = ' '; in map_switch_event()
1634 tr->shortname[0] = sched->next_shortname1; in map_switch_event()
1635 tr->shortname[1] = sched->next_shortname2; in map_switch_event()
1637 if (sched->next_shortname1 < 'Z') { in map_switch_event()
1638 sched->next_shortname1++; in map_switch_event()
1640 sched->next_shortname1 = 'A'; in map_switch_event()
1641 if (sched->next_shortname2 < '9') in map_switch_event()
1642 sched->next_shortname2++; in map_switch_event()
1644 sched->next_shortname2 = '0'; in map_switch_event()
1652 .cpu = sched->map.comp ? sched->map.comp_cpus[i].cpu : i, in map_switch_event()
1654 struct thread *curr_thread = sched->curr_thread[cpu.cpu]; in map_switch_event()
1662 if (sched->map.cpus && !perf_cpu_map__has(sched->map.cpus, cpu)) in map_switch_event()
1665 if (sched->map.color_cpus && perf_cpu_map__has(sched->map.color_cpus, cpu)) in map_switch_event()
1673 if (sched->curr_thread[cpu.cpu]) { in map_switch_event()
1674 curr_tr = thread__get_runtime(sched->curr_thread[cpu.cpu]); in map_switch_event()
1677 return -1; in map_switch_event()
1679 color_fprintf(stdout, pid_color, "%2s ", curr_tr->shortname); in map_switch_event()
1684 if (sched->map.cpus && !perf_cpu_map__has(sched->map.cpus, this_cpu)) in map_switch_event()
1689 if (new_shortname || tr->comm_changed || (verbose > 0 && thread__tid(sched_in))) { in map_switch_event()
1696 tr->shortname, thread__comm_str(sched_in), thread__tid(sched_in)); in map_switch_event()
1697 tr->comm_changed = false; in map_switch_event()
1700 if (sched->map.comp && new_cpu) in map_switch_event()
1713 struct perf_sample *sample, in process_sched_switch_event() argument
1717 int this_cpu = sample->cpu, err = 0; in process_sched_switch_event()
1718 u32 prev_pid = evsel__intval(evsel, sample, "prev_pid"), in process_sched_switch_event()
1719 next_pid = evsel__intval(evsel, sample, "next_pid"); in process_sched_switch_event()
1721 if (sched->curr_pid[this_cpu] != (u32)-1) { in process_sched_switch_event()
1726 if (sched->curr_pid[this_cpu] != prev_pid) in process_sched_switch_event()
1727 sched->nr_context_switch_bugs++; in process_sched_switch_event()
1730 if (sched->tp_handler->switch_event) in process_sched_switch_event()
1731 err = sched->tp_handler->switch_event(sched, evsel, sample, machine); in process_sched_switch_event()
1733 sched->curr_pid[this_cpu] = next_pid; in process_sched_switch_event()
1739 struct perf_sample *sample, in process_sched_runtime_event() argument
1744 if (sched->tp_handler->runtime_event) in process_sched_runtime_event()
1745 return sched->tp_handler->runtime_event(sched, evsel, sample, machine); in process_sched_runtime_event()
1752 struct perf_sample *sample, in perf_sched__process_fork_event() argument
1758 perf_event__process_fork(tool, event, sample, machine); in perf_sched__process_fork_event()
1761 if (sched->tp_handler->fork_event) in perf_sched__process_fork_event()
1762 return sched->tp_handler->fork_event(sched, event, machine); in perf_sched__process_fork_event()
1769 struct perf_sample *sample, in process_sched_migrate_task_event() argument
1774 if (sched->tp_handler->migrate_task_event) in process_sched_migrate_task_event()
1775 return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); in process_sched_migrate_task_event()
1782 struct perf_sample *sample,
1787 struct perf_sample *sample, in perf_sched__process_tracepoint_sample() argument
1793 if (evsel->handler != NULL) { in perf_sched__process_tracepoint_sample()
1794 tracepoint_handler f = evsel->handler; in perf_sched__process_tracepoint_sample()
1795 err = f(tool, evsel, sample, machine); in perf_sched__process_tracepoint_sample()
1803 struct perf_sample *sample, in perf_sched__process_comm() argument
1810 err = perf_event__process_comm(tool, event, sample, machine); in perf_sched__process_comm()
1814 thread = machine__find_thread(machine, sample->pid, sample->tid); in perf_sched__process_comm()
1817 return -1; in perf_sched__process_comm()
1823 return -1; in perf_sched__process_comm()
1826 tr->comm_changed = true; in perf_sched__process_comm()
1846 .force = sched->force, in perf_sched__read_events()
1848 int rc = -1; in perf_sched__read_events()
1850 session = perf_session__new(&data, &sched->tool); in perf_sched__read_events()
1856 symbol__init(&session->header.env); in perf_sched__read_events()
1859 if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking")) in perf_sched__read_events()
1865 if (perf_session__has_traces(session, "record -R")) { in perf_sched__read_events()
1872 sched->nr_events = session->evlist->stats.nr_events[0]; in perf_sched__read_events()
1873 sched->nr_lost_events = session->evlist->stats.total_lost; in perf_sched__read_events()
1874 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; in perf_sched__read_events()
1892 nsecs -= msecs * NSEC_PER_MSEC; in print_sched_time()
1899 * first time it is used.
1903 struct evsel_runtime *r = evsel->priv; in evsel__get_runtime()
1907 evsel->priv = r; in evsel__get_runtime()
1914 * save last time event was seen per cpu
1923 if ((cpu >= r->ncpu) || (r->last_time == NULL)) { in evsel__save_time()
1925 void *p = r->last_time; in evsel__save_time()
1927 p = realloc(r->last_time, n * sizeof(u64)); in evsel__save_time()
1931 r->last_time = p; in evsel__save_time()
1932 for (i = r->ncpu; i < n; ++i) in evsel__save_time()
1933 r->last_time[i] = (u64) 0; in evsel__save_time()
1935 r->ncpu = n; in evsel__save_time()
1938 r->last_time[cpu] = timestamp; in evsel__save_time()
1941 /* returns last time this event was seen on the given cpu */
1946 if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu)) in evsel__get_time()
1949 return r->last_time[cpu]; in evsel__get_time()
1979 u32 ncpus = sched->max_cpu.cpu + 1; in timehist_header()
1982 printf("%15s %6s ", "time", "cpu"); in timehist_header()
1984 if (sched->show_cpu_visual) { in timehist_header()
1994 printf(" %-*s %9s %9s %9s", comm_width, in timehist_header()
1995 "task name", "wait time", "sch delay", "run time"); in timehist_header()
1997 if (sched->show_state) in timehist_header()
2005 printf("%15s %-6s ", "", ""); in timehist_header()
2007 if (sched->show_cpu_visual) in timehist_header()
2010 printf(" %-*s %9s %9s %9s", comm_width, in timehist_header()
2013 if (sched->show_state) in timehist_header()
2023 if (sched->show_cpu_visual) in timehist_header()
2030 if (sched->show_state) in timehist_header()
2045 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; in task_state_char()
2050 struct perf_sample *sample, in timehist_print_sample() argument
2056 const char *next_comm = evsel__strval(evsel, sample, "next_comm"); in timehist_print_sample()
2057 const u32 next_pid = evsel__intval(evsel, sample, "next_pid"); in timehist_print_sample()
2058 u32 max_cpus = sched->max_cpu.cpu + 1; in timehist_print_sample()
2063 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) in timehist_print_sample()
2067 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_sample()
2069 if (sched->show_cpu_visual) { in timehist_print_sample()
2076 if (i == sample->cpu) in timehist_print_sample()
2085 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_sample()
2087 wait_time = tr->dt_sleep + tr->dt_iowait + tr->dt_preempt; in timehist_print_sample()
2090 print_sched_time(tr->dt_delay, 6); in timehist_print_sample()
2091 print_sched_time(tr->dt_run, 6); in timehist_print_sample()
2093 if (sched->show_state) in timehist_print_sample()
2096 if (sched->show_next) { in timehist_print_sample()
2098 printf(" %-*s", comm_width, nstr); in timehist_print_sample()
2101 if (sched->show_wakeups && !sched->show_next) in timehist_print_sample()
2102 printf(" %-*s", comm_width, ""); in timehist_print_sample()
2107 if (sched->show_callchain) in timehist_print_sample()
2110 sample__fprintf_sym(sample, al, 0, in timehist_print_sample()
2121 * Explanation of delta-time stats:
2123 * t = time of current schedule out event
2124 * tprev = time of previous sched out event
2125 * also time of schedule-in event for current task
2126 * last_time = time of last sched change event for current task
2127 * (i.e, time process was last scheduled out)
2128 * ready_to_run = time of wakeup for current task
2130 * -----|------------|------------|------------|------
2132 * time to run
2134 * |-------- dt_wait --------|
2135 * |- dt_delay -|-- dt_run --|
2137 * dt_run = run time of current task
2138 * dt_wait = time between last schedule out event for task and tprev
2139 * represents time spent off the cpu
2140 * dt_delay = time between wakeup and schedule-in of task
2146 r->dt_delay = 0; in timehist_update_runtime_stats()
2147 r->dt_sleep = 0; in timehist_update_runtime_stats()
2148 r->dt_iowait = 0; in timehist_update_runtime_stats()
2149 r->dt_preempt = 0; in timehist_update_runtime_stats()
2150 r->dt_run = 0; in timehist_update_runtime_stats()
2153 r->dt_run = t - tprev; in timehist_update_runtime_stats()
2154 if (r->ready_to_run) { in timehist_update_runtime_stats()
2155 if (r->ready_to_run > tprev) in timehist_update_runtime_stats()
2156 pr_debug("time travel: wakeup time for task > previous sched_switch event\n"); in timehist_update_runtime_stats()
2158 r->dt_delay = tprev - r->ready_to_run; in timehist_update_runtime_stats()
2161 if (r->last_time > tprev) in timehist_update_runtime_stats()
2162 pr_debug("time travel: last sched out time for task > previous sched_switch event\n"); in timehist_update_runtime_stats()
2163 else if (r->last_time) { in timehist_update_runtime_stats()
2164 u64 dt_wait = tprev - r->last_time; in timehist_update_runtime_stats()
2166 if (r->last_state == TASK_RUNNING) in timehist_update_runtime_stats()
2167 r->dt_preempt = dt_wait; in timehist_update_runtime_stats()
2168 else if (r->last_state == TASK_UNINTERRUPTIBLE) in timehist_update_runtime_stats()
2169 r->dt_iowait = dt_wait; in timehist_update_runtime_stats()
2171 r->dt_sleep = dt_wait; in timehist_update_runtime_stats()
2175 update_stats(&r->run_stats, r->dt_run); in timehist_update_runtime_stats()
2177 r->total_run_time += r->dt_run; in timehist_update_runtime_stats()
2178 r->total_delay_time += r->dt_delay; in timehist_update_runtime_stats()
2179 r->total_sleep_time += r->dt_sleep; in timehist_update_runtime_stats()
2180 r->total_iowait_time += r->dt_iowait; in timehist_update_runtime_stats()
2181 r->total_preempt_time += r->dt_preempt; in timehist_update_runtime_stats()
2184 static bool is_idle_sample(struct perf_sample *sample, in is_idle_sample() argument
2189 return evsel__intval(evsel, sample, "prev_pid") == 0; in is_idle_sample()
2191 return sample->pid == 0; in is_idle_sample()
2195 struct perf_sample *sample, in save_task_callchain() argument
2202 /* want main thread for process - has maps */ in save_task_callchain()
2203 thread = machine__findnew_thread(machine, sample->pid, sample->pid); in save_task_callchain()
2205 pr_debug("Failed to get thread for pid %d.\n", sample->pid); in save_task_callchain()
2209 if (!sched->show_callchain || sample->callchain == NULL) in save_task_callchain()
2214 if (thread__resolve_callchain(thread, cursor, evsel, sample, in save_task_callchain()
2215 NULL, NULL, sched->max_stack + 2) != 0) { in save_task_callchain()
2232 sym = node->ms.sym; in save_task_callchain()
2234 if (!strcmp(sym->name, "schedule") || in save_task_callchain()
2235 !strcmp(sym->name, "__schedule") || in save_task_callchain()
2236 !strcmp(sym->name, "preempt_schedule")) in save_task_callchain()
2237 sym->ignore = 1; in save_task_callchain()
2252 return -ENOMEM; in init_idle_thread()
2254 init_stats(&itr->tr.run_stats); in init_idle_thread()
2255 callchain_init(&itr->callchain); in init_idle_thread()
2256 callchain_cursor_reset(&itr->cursor); in init_idle_thread()
2272 return -ENOMEM; in init_idle_threads()
2280 return -ENOMEM; in init_idle_threads()
2341 struct perf_sample *sample) in save_idle_callchain() argument
2345 if (!sched->show_callchain || sample->callchain == NULL) in save_idle_callchain()
2352 callchain_cursor__copy(&itr->cursor, cursor); in save_idle_callchain()
2356 struct perf_sample *sample, in timehist_get_thread() argument
2362 if (is_idle_sample(sample, evsel)) { in timehist_get_thread()
2363 thread = get_idle_thread(sample->cpu); in timehist_get_thread()
2365 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); in timehist_get_thread()
2368 /* there were samples with tid 0 but non-zero pid */ in timehist_get_thread()
2369 thread = machine__findnew_thread(machine, sample->pid, in timehist_get_thread()
2370 sample->tid ?: sample->pid); in timehist_get_thread()
2372 pr_debug("Failed to get thread for tid %d. skipping sample.\n", in timehist_get_thread()
2373 sample->tid); in timehist_get_thread()
2376 save_task_callchain(sched, sample, evsel, machine); in timehist_get_thread()
2377 if (sched->idle_hist) { in timehist_get_thread()
2381 idle = get_idle_thread(sample->cpu); in timehist_get_thread()
2383 pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); in timehist_get_thread()
2391 itr->last_thread = thread; in timehist_get_thread()
2394 if (evsel__intval(evsel, sample, "next_pid") == 0) in timehist_get_thread()
2395 save_idle_callchain(sched, itr, sample); in timehist_get_thread()
2405 struct perf_sample *sample) in timehist_skip_sample() argument
2411 sched->skipped_samples++; in timehist_skip_sample()
2414 if (sched->idle_hist) { in timehist_skip_sample()
2417 else if (evsel__intval(evsel, sample, "prev_pid") != 0 && in timehist_skip_sample()
2418 evsel__intval(evsel, sample, "next_pid") != 0) in timehist_skip_sample()
2427 struct perf_sample *sample, in timehist_print_wakeup_event() argument
2434 thread = machine__findnew_thread(machine, sample->pid, sample->tid); in timehist_print_wakeup_event()
2439 if (timehist_skip_sample(sched, thread, evsel, sample) && in timehist_print_wakeup_event()
2440 timehist_skip_sample(sched, awakened, evsel, sample)) { in timehist_print_wakeup_event()
2444 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in timehist_print_wakeup_event()
2445 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_wakeup_event()
2446 if (sched->show_cpu_visual) in timehist_print_wakeup_event()
2447 printf(" %*s ", sched->max_cpu.cpu + 1, ""); in timehist_print_wakeup_event()
2449 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_wakeup_event()
2462 struct perf_sample *sample __maybe_unused, in timehist_sched_wakeup_ignore()
2471 struct perf_sample *sample, in timehist_sched_wakeup_event() argument
2477 /* want pid of awakened task not pid in sample */ in timehist_sched_wakeup_event()
2478 const u32 pid = evsel__intval(evsel, sample, "pid"); in timehist_sched_wakeup_event()
2482 return -1; in timehist_sched_wakeup_event()
2486 return -1; in timehist_sched_wakeup_event()
2488 if (tr->ready_to_run == 0) in timehist_sched_wakeup_event()
2489 tr->ready_to_run = sample->time; in timehist_sched_wakeup_event()
2492 if (sched->show_wakeups && in timehist_sched_wakeup_event()
2493 !perf_time__skip_sample(&sched->ptime, sample->time)) in timehist_sched_wakeup_event()
2494 timehist_print_wakeup_event(sched, evsel, sample, machine, thread); in timehist_sched_wakeup_event()
2501 struct perf_sample *sample, in timehist_print_migration_event() argument
2510 if (sched->summary_only) in timehist_print_migration_event()
2513 max_cpus = sched->max_cpu.cpu + 1; in timehist_print_migration_event()
2514 ocpu = evsel__intval(evsel, sample, "orig_cpu"); in timehist_print_migration_event()
2515 dcpu = evsel__intval(evsel, sample, "dest_cpu"); in timehist_print_migration_event()
2517 thread = machine__findnew_thread(machine, sample->pid, sample->tid); in timehist_print_migration_event()
2521 if (timehist_skip_sample(sched, thread, evsel, sample) && in timehist_print_migration_event()
2522 timehist_skip_sample(sched, migrated, evsel, sample)) { in timehist_print_migration_event()
2526 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in timehist_print_migration_event()
2527 printf("%15s [%04d] ", tstr, sample->cpu); in timehist_print_migration_event()
2529 if (sched->show_cpu_visual) { in timehist_print_migration_event()
2535 c = (i == sample->cpu) ? 'm' : ' '; in timehist_print_migration_event()
2541 printf(" %-*s ", comm_width, timehist_get_commstr(thread)); in timehist_print_migration_event()
2555 struct perf_sample *sample, in timehist_migrate_task_event() argument
2561 /* want pid of migrated task not pid in sample */ in timehist_migrate_task_event()
2562 const u32 pid = evsel__intval(evsel, sample, "pid"); in timehist_migrate_task_event()
2566 return -1; in timehist_migrate_task_event()
2570 return -1; in timehist_migrate_task_event()
2572 tr->migrations++; in timehist_migrate_task_event()
2575 timehist_print_migration_event(sched, evsel, sample, machine, thread); in timehist_migrate_task_event()
2583 struct perf_sample *sample, in timehist_sched_change_event() argument
2587 struct perf_time_interval *ptime = &sched->ptime; in timehist_sched_change_event()
2591 u64 tprev, t = sample->time; in timehist_sched_change_event()
2593 int state = evsel__intval(evsel, sample, "prev_state"); in timehist_sched_change_event()
2596 if (machine__resolve(machine, &al, sample) < 0) { in timehist_sched_change_event()
2598 event->header.type); in timehist_sched_change_event()
2599 rc = -1; in timehist_sched_change_event()
2603 thread = timehist_get_thread(sched, sample, machine, evsel); in timehist_sched_change_event()
2605 rc = -1; in timehist_sched_change_event()
2609 if (timehist_skip_sample(sched, thread, evsel, sample)) in timehist_sched_change_event()
2614 rc = -1; in timehist_sched_change_event()
2618 tprev = evsel__get_time(evsel, sample->cpu); in timehist_sched_change_event()
2621 * If start time given: in timehist_sched_change_event()
2622 * - sample time is under window user cares about - skip sample in timehist_sched_change_event()
2623 * - tprev is under window user cares about - reset to start of window in timehist_sched_change_event()
2625 if (ptime->start && ptime->start > t) in timehist_sched_change_event()
2628 if (tprev && ptime->start > tprev) in timehist_sched_change_event()
2629 tprev = ptime->start; in timehist_sched_change_event()
2632 * If end time given: in timehist_sched_change_event()
2633 * - previous sched event is out of window - we are done in timehist_sched_change_event()
2634 * - sample time is beyond window user cares about - reset it in timehist_sched_change_event()
2635 * to close out stats for time window interest in timehist_sched_change_event()
2636 * - If tprev is 0, that is, sched_in event for current task is in timehist_sched_change_event()
2638 * within time window interest - ignore it in timehist_sched_change_event()
2640 if (ptime->end) { in timehist_sched_change_event()
2641 if (!tprev || tprev > ptime->end) in timehist_sched_change_event()
2644 if (t > ptime->end) in timehist_sched_change_event()
2645 t = ptime->end; in timehist_sched_change_event()
2648 if (!sched->idle_hist || thread__tid(thread) == 0) { in timehist_sched_change_event()
2649 if (!cpu_list || test_bit(sample->cpu, cpu_bitmap)) in timehist_sched_change_event()
2652 if (sched->idle_hist) { in timehist_sched_change_event()
2658 if (itr->last_thread == NULL) in timehist_sched_change_event()
2661 /* add current idle time as last thread's runtime */ in timehist_sched_change_event()
2662 last_tr = thread__get_runtime(itr->last_thread); in timehist_sched_change_event()
2668 * remove delta time of last thread as it's not updated in timehist_sched_change_event()
2670 * time. we only care total run time and run stat. in timehist_sched_change_event()
2672 last_tr->dt_run = 0; in timehist_sched_change_event()
2673 last_tr->dt_delay = 0; in timehist_sched_change_event()
2674 last_tr->dt_sleep = 0; in timehist_sched_change_event()
2675 last_tr->dt_iowait = 0; in timehist_sched_change_event()
2676 last_tr->dt_preempt = 0; in timehist_sched_change_event()
2678 if (itr->cursor.nr) in timehist_sched_change_event()
2679 callchain_append(&itr->callchain, &itr->cursor, t - tprev); in timehist_sched_change_event()
2681 itr->last_thread = NULL; in timehist_sched_change_event()
2685 if (!sched->summary_only) in timehist_sched_change_event()
2686 timehist_print_sample(sched, evsel, sample, &al, thread, t, state); in timehist_sched_change_event()
2689 if (sched->hist_time.start == 0 && t >= ptime->start) in timehist_sched_change_event()
2690 sched->hist_time.start = t; in timehist_sched_change_event()
2691 if (ptime->end == 0 || t <= ptime->end) in timehist_sched_change_event()
2692 sched->hist_time.end = t; in timehist_sched_change_event()
2695 /* time of this sched_switch event becomes last time task seen */ in timehist_sched_change_event()
2696 tr->last_time = sample->time; in timehist_sched_change_event()
2698 /* last state is used to determine where to account wait time */ in timehist_sched_change_event()
2699 tr->last_state = state; in timehist_sched_change_event()
2701 /* sched out event for task so reset ready to run time */ in timehist_sched_change_event()
2702 tr->ready_to_run = 0; in timehist_sched_change_event()
2705 evsel__save_time(evsel, sample->time, sample->cpu); in timehist_sched_change_event()
2714 struct perf_sample *sample, in timehist_sched_switch_event() argument
2717 return timehist_sched_change_event(tool, event, evsel, sample, machine); in timehist_sched_switch_event()
2722 struct perf_sample *sample, in process_lost() argument
2727 timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); in process_lost()
2729 printf("lost %" PRI_lu64 " events on cpu %d\n", event->lost.lost, sample->cpu); in process_lost()
2738 double mean = avg_stats(&r->run_stats); in print_thread_runtime()
2743 (u64) r->run_stats.n); in print_thread_runtime()
2745 print_sched_time(r->total_run_time, 8); in print_thread_runtime()
2746 stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean); in print_thread_runtime()
2747 print_sched_time(r->run_stats.min, 6); in print_thread_runtime()
2751 print_sched_time(r->run_stats.max, 6); in print_thread_runtime()
2754 printf(" %5" PRIu64, r->migrations); in print_thread_runtime()
2763 (u64) r->run_stats.n); in print_thread_waittime()
2765 print_sched_time(r->total_run_time, 8); in print_thread_waittime()
2766 print_sched_time(r->total_sleep_time, 6); in print_thread_waittime()
2768 print_sched_time(r->total_iowait_time, 6); in print_thread_waittime()
2770 print_sched_time(r->total_preempt_time, 6); in print_thread_waittime()
2772 print_sched_time(r->total_delay_time, 6); in print_thread_waittime()
2792 if (r && r->run_stats.n) { in show_thread_runtime()
2793 stats->task_count++; in show_thread_runtime()
2794 stats->sched_count += r->run_stats.n; in show_thread_runtime()
2795 stats->total_run_time += r->total_run_time; in show_thread_runtime()
2797 if (stats->sched->show_state) in show_thread_runtime()
2808 const char *sep = " <- "; in callchain__fprintf_folded()
2817 ret = callchain__fprintf_folded(fp, node->parent); in callchain__fprintf_folded()
2820 list_for_each_entry(chain, &node->val, list) { in callchain__fprintf_folded()
2821 if (chain->ip >= PERF_CONTEXT_MAX) in callchain__fprintf_folded()
2823 if (chain->ms.sym && chain->ms.sym->ignore) in callchain__fprintf_folded()
2841 printf(" %16s %8s %s\n", "Idle time (msec)", "Count", "Callchains"); in timehist_print_idlehist_callchain()
2850 print_sched_time(chain->hit, 12); in timehist_print_idlehist_callchain()
2852 ret += fprintf(fp, " %8d ", chain->count); in timehist_print_idlehist_callchain()
2863 struct machine *m = &session->machines.host; in timehist_print_summary()
2869 u64 hist_time = sched->hist_time.end - sched->hist_time.start; in timehist_print_summary()
2874 if (sched->idle_hist) { in timehist_print_summary()
2875 printf("\nIdle-time summary\n"); in timehist_print_summary()
2876 printf("%*s parent sched-out ", comm_width, "comm"); in timehist_print_summary()
2877 printf(" idle-time min-idle avg-idle max-idle stddev migrations\n"); in timehist_print_summary()
2878 } else if (sched->show_state) { in timehist_print_summary()
2879 printf("\nWait-time summary\n"); in timehist_print_summary()
2880 printf("%*s parent sched-in ", comm_width, "comm"); in timehist_print_summary()
2881 printf(" run-time sleep iowait preempt delay\n"); in timehist_print_summary()
2884 printf("%*s parent sched-in ", comm_width, "comm"); in timehist_print_summary()
2885 printf(" run-time min-run avg-run max-run stddev migrations\n"); in timehist_print_summary()
2889 sched->show_state ? "(msec)" : "%"); in timehist_print_summary()
2898 if (sched->skipped_samples && !sched->idle_hist) in timehist_print_summary()
2911 if (r && r->run_stats.n) { in timehist_print_summary()
2912 totals.sched_count += r->run_stats.n; in timehist_print_summary()
2914 print_sched_time(r->total_run_time, 6); in timehist_print_summary()
2915 printf(" msec (%6.2f%%)\n", 100.0 * r->total_run_time / hist_time); in timehist_print_summary()
2917 printf(" CPU %2d idle entire time window\n", i); in timehist_print_summary()
2920 if (sched->idle_hist && sched->show_callchain) { in timehist_print_summary()
2938 callchain_param.sort(&itr->sorted_root.rb_root, &itr->callchain, in timehist_print_summary()
2942 print_sched_time(itr->tr.total_run_time, 6); in timehist_print_summary()
2944 timehist_print_idlehist_callchain(&itr->sorted_root); in timehist_print_summary()
2954 printf(" Total run time (msec): "); in timehist_print_summary()
2958 printf(" Total scheduling time (msec): "); in timehist_print_summary()
2960 printf(" (x %d)\n", sched->max_cpu.cpu); in timehist_print_summary()
2966 struct perf_sample *sample,
2971 struct perf_sample *sample, in perf_timehist__process_sample() argument
2978 .cpu = sample->cpu, in perf_timehist__process_sample()
2981 if (this_cpu.cpu > sched->max_cpu.cpu) in perf_timehist__process_sample()
2982 sched->max_cpu = this_cpu; in perf_timehist__process_sample()
2984 if (evsel->handler != NULL) { in perf_timehist__process_sample()
2985 sched_handler f = evsel->handler; in perf_timehist__process_sample()
2987 err = f(tool, event, evsel, sample, machine); in perf_timehist__process_sample()
2999 list_for_each_entry(evsel, &evlist->core.entries, core.node) { in timehist_check_attr()
3003 return -1; in timehist_check_attr()
3007 if (sched->show_callchain && in timehist_check_attr()
3011 sched->show_callchain = 0; in timehist_check_attr()
3033 .force = sched->force, in perf_sched__timehist()
3038 int err = -1; in perf_sched__timehist()
3043 sched->tool.sample = perf_timehist__process_sample; in perf_sched__timehist()
3044 sched->tool.mmap = perf_event__process_mmap; in perf_sched__timehist()
3045 sched->tool.comm = perf_event__process_comm; in perf_sched__timehist()
3046 sched->tool.exit = perf_event__process_exit; in perf_sched__timehist()
3047 sched->tool.fork = perf_event__process_fork; in perf_sched__timehist()
3048 sched->tool.lost = process_lost; in perf_sched__timehist()
3049 sched->tool.attr = perf_event__process_attr; in perf_sched__timehist()
3050 sched->tool.tracing_data = perf_event__process_tracing_data; in perf_sched__timehist()
3051 sched->tool.build_id = perf_event__process_build_id; in perf_sched__timehist()
3053 sched->tool.ordered_events = true; in perf_sched__timehist()
3054 sched->tool.ordering_requires_timestamps = true; in perf_sched__timehist()
3056 symbol_conf.use_callchain = sched->show_callchain; in perf_sched__timehist()
3058 session = perf_session__new(&data, &sched->tool); in perf_sched__timehist()
3068 evlist = session->evlist; in perf_sched__timehist()
3070 symbol__init(&session->header.env); in perf_sched__timehist()
3072 if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) { in perf_sched__timehist()
3073 pr_err("Invalid time string\n"); in perf_sched__timehist()
3074 err = -EINVAL; in perf_sched__timehist()
3084 if (evlist__find_tracepoint_by_name(session->evlist, "sched:sched_waking")) in perf_sched__timehist()
3087 /* setup per-evsel handlers */ in perf_sched__timehist()
3092 if (!evlist__find_tracepoint_by_name(session->evlist, "sched:sched_switch")) { in perf_sched__timehist()
3097 if (sched->show_migrations && in perf_sched__timehist()
3101 /* pre-allocate struct for per-CPU idle stats */ in perf_sched__timehist()
3102 sched->max_cpu.cpu = session->header.env.nr_cpus_online; in perf_sched__timehist()
3103 if (sched->max_cpu.cpu == 0) in perf_sched__timehist()
3104 sched->max_cpu.cpu = 4; in perf_sched__timehist()
3105 if (init_idle_threads(sched->max_cpu.cpu)) in perf_sched__timehist()
3109 if (sched->summary_only) in perf_sched__timehist()
3110 sched->summary = sched->summary_only; in perf_sched__timehist()
3112 if (!sched->summary_only) in perf_sched__timehist()
3121 sched->nr_events = evlist->stats.nr_events[0]; in perf_sched__timehist()
3122 sched->nr_lost_events = evlist->stats.total_lost; in perf_sched__timehist()
3123 sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST]; in perf_sched__timehist()
3125 if (sched->summary) in perf_sched__timehist()
3138 if (sched->nr_unordered_timestamps && sched->nr_timestamps) { in print_bad_events()
3140 (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, in print_bad_events()
3141 sched->nr_unordered_timestamps, sched->nr_timestamps); in print_bad_events()
3143 if (sched->nr_lost_events && sched->nr_events) { in print_bad_events()
3145 (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, in print_bad_events()
3146 sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); in print_bad_events()
3148 if (sched->nr_context_switch_bugs && sched->nr_timestamps) { in print_bad_events()
3150 (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, in print_bad_events()
3151 sched->nr_context_switch_bugs, sched->nr_timestamps); in print_bad_events()
3152 if (sched->nr_lost_events) in print_bad_events()
3160 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in __merge_work_atoms()
3162 const char *comm = thread__comm_str(data->thread), *this_comm; in __merge_work_atoms()
3171 this_comm = thread__comm_str(this->thread); in __merge_work_atoms()
3174 new = &((*new)->rb_left); in __merge_work_atoms()
3176 new = &((*new)->rb_right); in __merge_work_atoms()
3179 this->num_merged++; in __merge_work_atoms()
3180 this->total_runtime += data->total_runtime; in __merge_work_atoms()
3181 this->nb_atoms += data->nb_atoms; in __merge_work_atoms()
3182 this->total_lat += data->total_lat; in __merge_work_atoms()
3183 list_splice(&data->work_list, &this->work_list); in __merge_work_atoms()
3184 if (this->max_lat < data->max_lat) { in __merge_work_atoms()
3185 this->max_lat = data->max_lat; in __merge_work_atoms()
3186 this->max_lat_start = data->max_lat_start; in __merge_work_atoms()
3187 this->max_lat_end = data->max_lat_end; in __merge_work_atoms()
3194 data->num_merged++; in __merge_work_atoms()
3195 rb_link_node(&data->node, parent, new); in __merge_work_atoms()
3196 rb_insert_color_cached(&data->node, root, leftmost); in __merge_work_atoms()
3204 if (sched->skip_merge) in perf_sched__merge_lat()
3207 while ((node = rb_first_cached(&sched->atom_root))) { in perf_sched__merge_lat()
3208 rb_erase_cached(node, &sched->atom_root); in perf_sched__merge_lat()
3210 __merge_work_atoms(&sched->merged_atom_root, data); in perf_sched__merge_lat()
3218 sched->cpu_last_switched = calloc(MAX_CPUS, sizeof(*(sched->cpu_last_switched))); in setup_cpus_switch_event()
3219 if (!sched->cpu_last_switched) in setup_cpus_switch_event()
3220 return -1; in setup_cpus_switch_event()
3222 sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid))); in setup_cpus_switch_event()
3223 if (!sched->curr_pid) { in setup_cpus_switch_event()
3224 zfree(&sched->cpu_last_switched); in setup_cpus_switch_event()
3225 return -1; in setup_cpus_switch_event()
3229 sched->curr_pid[i] = -1; in setup_cpus_switch_event()
3236 zfree(&sched->curr_pid); in free_cpus_switch_event()
3237 zfree(&sched->cpu_last_switched); in free_cpus_switch_event()
3242 int rc = -1; in perf_sched__lat()
3256 …tf("\n -------------------------------------------------------------------------------------------… in perf_sched__lat()
3258 …intf(" -------------------------------------------------------------------------------------------… in perf_sched__lat()
3260 next = rb_first_cached(&sched->sorted_atom_root); in perf_sched__lat()
3268 thread__zput(work_list->thread); in perf_sched__lat()
3271 …printf(" -----------------------------------------------------------------------------------------… in perf_sched__lat()
3273 (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count); in perf_sched__lat()
3275 printf(" ---------------------------------------------------\n"); in perf_sched__lat()
3289 sched->max_cpu.cpu = sysconf(_SC_NPROCESSORS_CONF); in setup_map_cpus()
3291 if (sched->map.comp) { in setup_map_cpus()
3292 sched->map.comp_cpus = zalloc(sched->max_cpu.cpu * sizeof(int)); in setup_map_cpus()
3293 if (!sched->map.comp_cpus) in setup_map_cpus()
3294 return -1; in setup_map_cpus()
3297 if (sched->map.cpus_str) { in setup_map_cpus()
3298 sched->map.cpus = perf_cpu_map__new(sched->map.cpus_str); in setup_map_cpus()
3299 if (!sched->map.cpus) { in setup_map_cpus()
3300 pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); in setup_map_cpus()
3301 zfree(&sched->map.comp_cpus); in setup_map_cpus()
3302 return -1; in setup_map_cpus()
3313 if (!sched->map.color_pids_str) in setup_color_pids()
3316 map = thread_map__new_by_tid_str(sched->map.color_pids_str); in setup_color_pids()
3318 pr_err("failed to get thread map from %s\n", sched->map.color_pids_str); in setup_color_pids()
3319 return -1; in setup_color_pids()
3322 sched->map.color_pids = map; in setup_color_pids()
3330 if (!sched->map.color_cpus_str) in setup_color_cpus()
3333 map = perf_cpu_map__new(sched->map.color_cpus_str); in setup_color_cpus()
3335 pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str); in setup_color_cpus()
3336 return -1; in setup_color_cpus()
3339 sched->map.color_cpus = map; in setup_color_cpus()
3345 int rc = -1; in perf_sched__map()
3347 sched->curr_thread = calloc(MAX_CPUS, sizeof(*(sched->curr_thread))); in perf_sched__map()
3348 if (!sched->curr_thread) in perf_sched__map()
3371 perf_cpu_map__put(sched->map.color_cpus); in perf_sched__map()
3374 perf_thread_map__put(sched->map.color_pids); in perf_sched__map()
3377 zfree(&sched->map.comp_cpus); in perf_sched__map()
3378 perf_cpu_map__put(sched->map.cpus); in perf_sched__map()
3384 zfree(&sched->curr_thread); in perf_sched__map()
3393 mutex_init(&sched->start_work_mutex); in perf_sched__replay()
3394 mutex_init(&sched->work_done_wait_mutex); in perf_sched__replay()
3409 printf("nr_run_events: %ld\n", sched->nr_run_events); in perf_sched__replay()
3410 printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); in perf_sched__replay()
3411 printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); in perf_sched__replay()
3413 if (sched->targetless_wakeups) in perf_sched__replay()
3414 printf("target-less wakeups: %ld\n", sched->targetless_wakeups); in perf_sched__replay()
3415 if (sched->multitarget_wakeups) in perf_sched__replay()
3416 printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); in perf_sched__replay()
3417 if (sched->nr_run_events_optimized) in perf_sched__replay()
3419 sched->nr_run_events_optimized); in perf_sched__replay()
3424 sched->thread_funcs_exit = false; in perf_sched__replay()
3426 printf("------------------------------------------------------------\n"); in perf_sched__replay()
3427 for (i = 0; i < sched->replay_repeat; i++) in perf_sched__replay()
3430 sched->thread_funcs_exit = true; in perf_sched__replay()
3437 mutex_destroy(&sched->start_work_mutex); in perf_sched__replay()
3438 mutex_destroy(&sched->work_done_wait_mutex); in perf_sched__replay()
3445 char *tmp, *tok, *str = strdup(sched->sort_order); in setup_sorting()
3449 if (sort_dimension__add(tok, &sched->sort_list) < 0) { in setup_sorting()
3451 "Unknown --sort key: `%s'", tok); in setup_sorting()
3457 sort_dimension__add("pid", &sched->cmp_pid); in setup_sorting()
3477 "-a", in __cmd_record()
3478 "-R", in __cmd_record()
3479 "-m", "1024", in __cmd_record()
3480 "-c", "1", in __cmd_record()
3481 "-e", "sched:sched_switch", in __cmd_record()
3482 "-e", "sched:sched_stat_runtime", in __cmd_record()
3483 "-e", "sched:sched_process_fork", in __cmd_record()
3484 "-e", "sched:sched_wakeup_new", in __cmd_record()
3485 "-e", "sched:sched_migrate_task", in __cmd_record()
3495 "-e", "sched:sched_stat_wait", in __cmd_record()
3496 "-e", "sched:sched_stat_sleep", in __cmd_record()
3497 "-e", "sched:sched_stat_iowait", in __cmd_record()
3506 * +2 for either "-e", "sched:sched_wakeup" or in __cmd_record()
3507 * "-e", "sched:sched_waking" in __cmd_record()
3509 rec_argc = ARRAY_SIZE(record_args) + 2 + schedstat_argc + argc - 1; in __cmd_record()
3512 return -ENOMEM; in __cmd_record()
3516 return -ENOMEM; in __cmd_record()
3522 rec_argv[i++] = strdup("-e"); in __cmd_record()
3553 .sample = perf_sched__process_tracepoint_sample, in cmd_sched()
3564 .profile_cpu = -1, in cmd_sched()
3576 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, in cmd_sched()
3592 "repeat the workload replay N times (-1: infinite)"), in cmd_sched()
3598 OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids", in cmd_sched()
3600 OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus", in cmd_sched()
3611 OPT_BOOLEAN('g', "call-graph", &sched.show_callchain, in cmd_sched()
3613 OPT_UINTEGER(0, "max-stack", &sched.max_stack, in cmd_sched()
3619 OPT_BOOLEAN('S', "with-summary", &sched.summary, in cmd_sched()
3624 OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"), in cmd_sched()
3625 OPT_BOOLEAN('I', "idle-hist", &sched.idle_hist, "Show idle events only"), in cmd_sched()
3626 OPT_STRING(0, "time", &sched.time_str, "str", in cmd_sched()
3627 "Time span for analysis (start,stop)"), in cmd_sched()
3628 OPT_BOOLEAN(0, "state", &sched.show_state, "Show task state when sched-out"), in cmd_sched()
3723 pr_err(" Error: -s and -[n|w] are mutually exclusive.\n"); in cmd_sched()
3729 return -EINVAL; in cmd_sched()