5 #include "util/cache.h"
6 #include "util/evsel.h"
7 #include "util/symbol.h"
8 #include "util/thread.h"
9 #include "util/header.h"
10 #include "util/session.h"
12 #include "util/parse-options.h"
13 #include "util/trace-event.h"
15 #include "util/debug.h"
17 #include <sys/prctl.h>
19 #include <semaphore.h>
23 static char const *input_name = "perf.data";
25 static char default_sort_order[] = "avg, max, switch, runtime";
26 static const char *sort_order = default_sort_order;
28 static int profile_cpu = -1;
30 #define PR_SET_NAME 15 /* Set process name */
33 static u64 run_measurement_overhead;
34 static u64 sleep_measurement_overhead;
41 static unsigned long nr_tasks;
50 unsigned long nr_events;
51 unsigned long curr_event;
52 struct sched_atom **atoms;
63 enum sched_event_type {
67 SCHED_EVENT_MIGRATION,
71 enum sched_event_type type;
77 struct task_desc *wakee;
80 static struct task_desc *pid_to_task[MAX_PID];
82 static struct task_desc **tasks;
84 static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
85 static u64 start_time;
87 static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
89 static unsigned long nr_run_events;
90 static unsigned long nr_sleep_events;
91 static unsigned long nr_wakeup_events;
93 static unsigned long nr_sleep_corrections;
94 static unsigned long nr_run_events_optimized;
96 static unsigned long targetless_wakeups;
97 static unsigned long multitarget_wakeups;
100 static u64 runavg_cpu_usage;
101 static u64 parent_cpu_usage;
102 static u64 runavg_parent_cpu_usage;
104 static unsigned long nr_runs;
105 static u64 sum_runtime;
106 static u64 sum_fluct;
109 static unsigned int replay_repeat = 10;
110 static unsigned long nr_timestamps;
111 static unsigned long nr_unordered_timestamps;
112 static unsigned long nr_state_machine_bugs;
113 static unsigned long nr_context_switch_bugs;
114 static unsigned long nr_events;
115 static unsigned long nr_lost_chunks;
116 static unsigned long nr_lost_events;
118 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
128 struct list_head list;
129 enum thread_state state;
137 struct list_head work_list;
138 struct thread *thread;
147 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
149 static struct rb_root atom_root, sorted_atom_root;
151 static u64 all_runtime;
152 static u64 all_count;
155 static u64 get_nsecs(void)
159 clock_gettime(CLOCK_MONOTONIC, &ts);
161 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
164 static void burn_nsecs(u64 nsecs)
166 u64 T0 = get_nsecs(), T1;
170 } while (T1 + run_measurement_overhead < T0 + nsecs);
173 static void sleep_nsecs(u64 nsecs)
177 ts.tv_nsec = nsecs % 999999999;
178 ts.tv_sec = nsecs / 999999999;
180 nanosleep(&ts, NULL);
183 static void calibrate_run_measurement_overhead(void)
185 u64 T0, T1, delta, min_delta = 1000000000ULL;
188 for (i = 0; i < 10; i++) {
193 min_delta = min(min_delta, delta);
195 run_measurement_overhead = min_delta;
197 printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta);
200 static void calibrate_sleep_measurement_overhead(void)
202 u64 T0, T1, delta, min_delta = 1000000000ULL;
205 for (i = 0; i < 10; i++) {
210 min_delta = min(min_delta, delta);
213 sleep_measurement_overhead = min_delta;
215 printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta);
218 static struct sched_atom *
219 get_new_event(struct task_desc *task, u64 timestamp)
221 struct sched_atom *event = zalloc(sizeof(*event));
222 unsigned long idx = task->nr_events;
225 event->timestamp = timestamp;
229 size = sizeof(struct sched_atom *) * task->nr_events;
230 task->atoms = realloc(task->atoms, size);
231 BUG_ON(!task->atoms);
233 task->atoms[idx] = event;
238 static struct sched_atom *last_event(struct task_desc *task)
240 if (!task->nr_events)
243 return task->atoms[task->nr_events - 1];
247 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
249 struct sched_atom *event, *curr_event = last_event(task);
252 * optimize an existing RUN event by merging this one
255 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
256 nr_run_events_optimized++;
257 curr_event->duration += duration;
261 event = get_new_event(task, timestamp);
263 event->type = SCHED_EVENT_RUN;
264 event->duration = duration;
270 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
271 struct task_desc *wakee)
273 struct sched_atom *event, *wakee_event;
275 event = get_new_event(task, timestamp);
276 event->type = SCHED_EVENT_WAKEUP;
277 event->wakee = wakee;
279 wakee_event = last_event(wakee);
280 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
281 targetless_wakeups++;
284 if (wakee_event->wait_sem) {
285 multitarget_wakeups++;
289 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
290 sem_init(wakee_event->wait_sem, 0, 0);
291 wakee_event->specific_wait = 1;
292 event->wait_sem = wakee_event->wait_sem;
298 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
299 u64 task_state __used)
301 struct sched_atom *event = get_new_event(task, timestamp);
303 event->type = SCHED_EVENT_SLEEP;
308 static struct task_desc *register_pid(unsigned long pid, const char *comm)
310 struct task_desc *task;
312 BUG_ON(pid >= MAX_PID);
314 task = pid_to_task[pid];
319 task = zalloc(sizeof(*task));
322 strcpy(task->comm, comm);
324 * every task starts in sleeping state - this gets ignored
325 * if there's no wakeup pointing to this sleep state:
327 add_sched_event_sleep(task, 0, 0);
329 pid_to_task[pid] = task;
331 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
333 tasks[task->nr] = task;
336 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
342 static void print_task_traces(void)
344 struct task_desc *task;
347 for (i = 0; i < nr_tasks; i++) {
349 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
350 task->nr, task->comm, task->pid, task->nr_events);
354 static void add_cross_task_wakeups(void)
356 struct task_desc *task1, *task2;
359 for (i = 0; i < nr_tasks; i++) {
365 add_sched_event_wakeup(task1, 0, task2);
370 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
374 switch (atom->type) {
375 case SCHED_EVENT_RUN:
376 burn_nsecs(atom->duration);
378 case SCHED_EVENT_SLEEP:
380 ret = sem_wait(atom->wait_sem);
383 case SCHED_EVENT_WAKEUP:
385 ret = sem_post(atom->wait_sem);
388 case SCHED_EVENT_MIGRATION:
395 static u64 get_cpu_usage_nsec_parent(void)
401 err = getrusage(RUSAGE_SELF, &ru);
404 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
405 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
410 static int self_open_counters(void)
412 struct perf_event_attr attr;
415 memset(&attr, 0, sizeof(attr));
417 attr.type = PERF_TYPE_SOFTWARE;
418 attr.config = PERF_COUNT_SW_TASK_CLOCK;
420 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
423 die("Error: sys_perf_event_open() syscall returned"
424 "with %d (%s)\n", fd, strerror(errno));
428 static u64 get_cpu_usage_nsec_self(int fd)
433 ret = read(fd, &runtime, sizeof(runtime));
434 BUG_ON(ret != sizeof(runtime));
439 static void *thread_func(void *ctx)
441 struct task_desc *this_task = ctx;
442 u64 cpu_usage_0, cpu_usage_1;
443 unsigned long i, ret;
447 sprintf(comm2, ":%s", this_task->comm);
448 prctl(PR_SET_NAME, comm2);
449 fd = self_open_counters();
452 ret = sem_post(&this_task->ready_for_work);
454 ret = pthread_mutex_lock(&start_work_mutex);
456 ret = pthread_mutex_unlock(&start_work_mutex);
459 cpu_usage_0 = get_cpu_usage_nsec_self(fd);
461 for (i = 0; i < this_task->nr_events; i++) {
462 this_task->curr_event = i;
463 process_sched_event(this_task, this_task->atoms[i]);
466 cpu_usage_1 = get_cpu_usage_nsec_self(fd);
467 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
468 ret = sem_post(&this_task->work_done_sem);
471 ret = pthread_mutex_lock(&work_done_wait_mutex);
473 ret = pthread_mutex_unlock(&work_done_wait_mutex);
479 static void create_tasks(void)
481 struct task_desc *task;
486 err = pthread_attr_init(&attr);
488 err = pthread_attr_setstacksize(&attr,
489 (size_t) max(16 * 1024, PTHREAD_STACK_MIN));
491 err = pthread_mutex_lock(&start_work_mutex);
493 err = pthread_mutex_lock(&work_done_wait_mutex);
495 for (i = 0; i < nr_tasks; i++) {
497 sem_init(&task->sleep_sem, 0, 0);
498 sem_init(&task->ready_for_work, 0, 0);
499 sem_init(&task->work_done_sem, 0, 0);
500 task->curr_event = 0;
501 err = pthread_create(&task->thread, &attr, thread_func, task);
506 static void wait_for_tasks(void)
508 u64 cpu_usage_0, cpu_usage_1;
509 struct task_desc *task;
510 unsigned long i, ret;
512 start_time = get_nsecs();
514 pthread_mutex_unlock(&work_done_wait_mutex);
516 for (i = 0; i < nr_tasks; i++) {
518 ret = sem_wait(&task->ready_for_work);
520 sem_init(&task->ready_for_work, 0, 0);
522 ret = pthread_mutex_lock(&work_done_wait_mutex);
525 cpu_usage_0 = get_cpu_usage_nsec_parent();
527 pthread_mutex_unlock(&start_work_mutex);
529 for (i = 0; i < nr_tasks; i++) {
531 ret = sem_wait(&task->work_done_sem);
533 sem_init(&task->work_done_sem, 0, 0);
534 cpu_usage += task->cpu_usage;
538 cpu_usage_1 = get_cpu_usage_nsec_parent();
539 if (!runavg_cpu_usage)
540 runavg_cpu_usage = cpu_usage;
541 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
543 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
544 if (!runavg_parent_cpu_usage)
545 runavg_parent_cpu_usage = parent_cpu_usage;
546 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
547 parent_cpu_usage)/10;
549 ret = pthread_mutex_lock(&start_work_mutex);
552 for (i = 0; i < nr_tasks; i++) {
554 sem_init(&task->sleep_sem, 0, 0);
555 task->curr_event = 0;
559 static void run_one_test(void)
561 u64 T0, T1, delta, avg_delta, fluct;
568 sum_runtime += delta;
571 avg_delta = sum_runtime / nr_runs;
572 if (delta < avg_delta)
573 fluct = avg_delta - delta;
575 fluct = delta - avg_delta;
579 run_avg = (run_avg*9 + delta)/10;
581 printf("#%-3ld: %0.3f, ",
582 nr_runs, (double)delta/1000000.0);
584 printf("ravg: %0.2f, ",
585 (double)run_avg/1e6);
587 printf("cpu: %0.2f / %0.2f",
588 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
592 * rusage statistics done by the parent, these are less
593 * accurate than the sum_exec_runtime based statistics:
595 printf(" [%0.2f / %0.2f]",
596 (double)parent_cpu_usage/1e6,
597 (double)runavg_parent_cpu_usage/1e6);
602 if (nr_sleep_corrections)
603 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
604 nr_sleep_corrections = 0;
607 static void test_calibrations(void)
615 printf("the run test took %" PRIu64 " nsecs\n", T1 - T0);
621 printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0);
624 #define FILL_FIELD(ptr, field, event, data) \
625 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
627 #define FILL_ARRAY(ptr, array, event, data) \
629 void *__array = raw_field_ptr(event, #array, data); \
630 memcpy(ptr.array, __array, sizeof(ptr.array)); \
633 #define FILL_COMMON_FIELDS(ptr, event, data) \
635 FILL_FIELD(ptr, common_type, event, data); \
636 FILL_FIELD(ptr, common_flags, event, data); \
637 FILL_FIELD(ptr, common_preempt_count, event, data); \
638 FILL_FIELD(ptr, common_pid, event, data); \
639 FILL_FIELD(ptr, common_tgid, event, data); \
644 struct trace_switch_event {
649 u8 common_preempt_count;
662 struct trace_runtime_event {
667 u8 common_preempt_count;
677 struct trace_wakeup_event {
682 u8 common_preempt_count;
694 struct trace_fork_event {
699 u8 common_preempt_count;
703 char parent_comm[16];
709 struct trace_migrate_task_event {
714 u8 common_preempt_count;
725 struct trace_sched_handler {
726 void (*switch_event)(struct trace_switch_event *,
731 struct thread *thread);
733 void (*runtime_event)(struct trace_runtime_event *,
738 struct thread *thread);
740 void (*wakeup_event)(struct trace_wakeup_event *,
745 struct thread *thread);
747 void (*fork_event)(struct trace_fork_event *,
751 struct thread *thread);
753 void (*migrate_task_event)(struct trace_migrate_task_event *,
754 struct machine *machine,
758 struct thread *thread);
763 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
764 struct machine *machine __used,
767 u64 timestamp __used,
768 struct thread *thread __used)
770 struct task_desc *waker, *wakee;
773 printf("sched_wakeup event %p\n", event);
775 printf(" ... pid %d woke up %s/%d\n",
776 wakeup_event->common_pid,
781 waker = register_pid(wakeup_event->common_pid, "<unknown>");
782 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
784 add_sched_event_wakeup(waker, timestamp, wakee);
787 static u64 cpu_last_switched[MAX_CPUS];
790 replay_switch_event(struct trace_switch_event *switch_event,
791 struct machine *machine __used,
795 struct thread *thread __used)
797 struct task_desc *prev, __used *next;
802 printf("sched_switch event %p\n", event);
804 if (cpu >= MAX_CPUS || cpu < 0)
807 timestamp0 = cpu_last_switched[cpu];
809 delta = timestamp - timestamp0;
814 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
817 printf(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n",
818 switch_event->prev_comm, switch_event->prev_pid,
819 switch_event->next_comm, switch_event->next_pid,
823 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
824 next = register_pid(switch_event->next_pid, switch_event->next_comm);
826 cpu_last_switched[cpu] = timestamp;
828 add_sched_event_run(prev, timestamp, delta);
829 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
834 replay_fork_event(struct trace_fork_event *fork_event,
837 u64 timestamp __used,
838 struct thread *thread __used)
841 printf("sched_fork event %p\n", event);
842 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
843 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
845 register_pid(fork_event->parent_pid, fork_event->parent_comm);
846 register_pid(fork_event->child_pid, fork_event->child_comm);
849 static struct trace_sched_handler replay_ops = {
850 .wakeup_event = replay_wakeup_event,
851 .switch_event = replay_switch_event,
852 .fork_event = replay_fork_event,
855 struct sort_dimension {
858 struct list_head list;
861 static LIST_HEAD(cmp_pid);
864 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
866 struct sort_dimension *sort;
869 BUG_ON(list_empty(list));
871 list_for_each_entry(sort, list, list) {
872 ret = sort->cmp(l, r);
880 static struct work_atoms *
881 thread_atoms_search(struct rb_root *root, struct thread *thread,
882 struct list_head *sort_list)
884 struct rb_node *node = root->rb_node;
885 struct work_atoms key = { .thread = thread };
888 struct work_atoms *atoms;
891 atoms = container_of(node, struct work_atoms, node);
893 cmp = thread_lat_cmp(sort_list, &key, atoms);
895 node = node->rb_left;
897 node = node->rb_right;
899 BUG_ON(thread != atoms->thread);
907 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
908 struct list_head *sort_list)
910 struct rb_node **new = &(root->rb_node), *parent = NULL;
913 struct work_atoms *this;
916 this = container_of(*new, struct work_atoms, node);
919 cmp = thread_lat_cmp(sort_list, data, this);
922 new = &((*new)->rb_left);
924 new = &((*new)->rb_right);
927 rb_link_node(&data->node, parent, new);
928 rb_insert_color(&data->node, root);
931 static void thread_atoms_insert(struct thread *thread)
933 struct work_atoms *atoms = zalloc(sizeof(*atoms));
937 atoms->thread = thread;
938 INIT_LIST_HEAD(&atoms->work_list);
939 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
943 latency_fork_event(struct trace_fork_event *fork_event __used,
944 struct event *event __used,
946 u64 timestamp __used,
947 struct thread *thread __used)
949 /* should insert the newcomer */
953 static char sched_out_state(struct trace_switch_event *switch_event)
955 const char *str = TASK_STATE_TO_CHAR_STR;
957 return str[switch_event->prev_state];
961 add_sched_out_event(struct work_atoms *atoms,
965 struct work_atom *atom = zalloc(sizeof(*atom));
969 atom->sched_out_time = timestamp;
971 if (run_state == 'R') {
972 atom->state = THREAD_WAIT_CPU;
973 atom->wake_up_time = atom->sched_out_time;
976 list_add_tail(&atom->list, &atoms->work_list);
980 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
982 struct work_atom *atom;
984 BUG_ON(list_empty(&atoms->work_list));
986 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
988 atom->runtime += delta;
989 atoms->total_runtime += delta;
993 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
995 struct work_atom *atom;
998 if (list_empty(&atoms->work_list))
1001 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1003 if (atom->state != THREAD_WAIT_CPU)
1006 if (timestamp < atom->wake_up_time) {
1007 atom->state = THREAD_IGNORE;
1011 atom->state = THREAD_SCHED_IN;
1012 atom->sched_in_time = timestamp;
1014 delta = atom->sched_in_time - atom->wake_up_time;
1015 atoms->total_lat += delta;
1016 if (delta > atoms->max_lat) {
1017 atoms->max_lat = delta;
1018 atoms->max_lat_at = timestamp;
1024 latency_switch_event(struct trace_switch_event *switch_event,
1025 struct machine *machine,
1026 struct event *event __used,
1029 struct thread *thread __used)
1031 struct work_atoms *out_events, *in_events;
1032 struct thread *sched_out, *sched_in;
1036 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1038 timestamp0 = cpu_last_switched[cpu];
1039 cpu_last_switched[cpu] = timestamp;
1041 delta = timestamp - timestamp0;
1046 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1049 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1050 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
1052 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1054 thread_atoms_insert(sched_out);
1055 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1057 die("out-event: Internal tree error");
1059 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1061 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1063 thread_atoms_insert(sched_in);
1064 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1066 die("in-event: Internal tree error");
1068 * Take came in we have not heard about yet,
1069 * add in an initial atom in runnable state:
1071 add_sched_out_event(in_events, 'R', timestamp);
1073 add_sched_in_event(in_events, timestamp);
1077 latency_runtime_event(struct trace_runtime_event *runtime_event,
1078 struct machine *machine,
1079 struct event *event __used,
1082 struct thread *this_thread __used)
1084 struct thread *thread = machine__findnew_thread(machine, runtime_event->pid);
1085 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1087 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1089 thread_atoms_insert(thread);
1090 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1092 die("in-event: Internal tree error");
1093 add_sched_out_event(atoms, 'R', timestamp);
1096 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1100 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1101 struct machine *machine,
1102 struct event *__event __used,
1105 struct thread *thread __used)
1107 struct work_atoms *atoms;
1108 struct work_atom *atom;
1109 struct thread *wakee;
1111 /* Note for later, it may be interesting to observe the failing cases */
1112 if (!wakeup_event->success)
1115 wakee = machine__findnew_thread(machine, wakeup_event->pid);
1116 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1118 thread_atoms_insert(wakee);
1119 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1121 die("wakeup-event: Internal tree error");
1122 add_sched_out_event(atoms, 'S', timestamp);
1125 BUG_ON(list_empty(&atoms->work_list));
1127 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1130 * You WILL be missing events if you've recorded only
1131 * one CPU, or are only looking at only one, so don't
1132 * make useless noise.
1134 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1135 nr_state_machine_bugs++;
1138 if (atom->sched_out_time > timestamp) {
1139 nr_unordered_timestamps++;
1143 atom->state = THREAD_WAIT_CPU;
1144 atom->wake_up_time = timestamp;
1148 latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1149 struct machine *machine,
1150 struct event *__event __used,
1153 struct thread *thread __used)
1155 struct work_atoms *atoms;
1156 struct work_atom *atom;
1157 struct thread *migrant;
1160 * Only need to worry about migration when profiling one CPU.
1162 if (profile_cpu == -1)
1165 migrant = machine__findnew_thread(machine, migrate_task_event->pid);
1166 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1168 thread_atoms_insert(migrant);
1169 register_pid(migrant->pid, migrant->comm);
1170 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1172 die("migration-event: Internal tree error");
1173 add_sched_out_event(atoms, 'R', timestamp);
1176 BUG_ON(list_empty(&atoms->work_list));
1178 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1179 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1183 if (atom->sched_out_time > timestamp)
1184 nr_unordered_timestamps++;
1187 static struct trace_sched_handler lat_ops = {
1188 .wakeup_event = latency_wakeup_event,
1189 .switch_event = latency_switch_event,
1190 .runtime_event = latency_runtime_event,
1191 .fork_event = latency_fork_event,
1192 .migrate_task_event = latency_migrate_task_event,
1195 static void output_lat_thread(struct work_atoms *work_list)
1201 if (!work_list->nb_atoms)
1204 * Ignore idle threads:
1206 if (!strcmp(work_list->thread->comm, "swapper"))
1209 all_runtime += work_list->total_runtime;
1210 all_count += work_list->nb_atoms;
1212 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
1214 for (i = 0; i < 24 - ret; i++)
1217 avg = work_list->total_lat / work_list->nb_atoms;
1219 printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %9.6f s\n",
1220 (double)work_list->total_runtime / 1e6,
1221 work_list->nb_atoms, (double)avg / 1e6,
1222 (double)work_list->max_lat / 1e6,
1223 (double)work_list->max_lat_at / 1e9);
1226 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1228 if (l->thread->pid < r->thread->pid)
1230 if (l->thread->pid > r->thread->pid)
1236 static struct sort_dimension pid_sort_dimension = {
1241 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1251 avgl = l->total_lat / l->nb_atoms;
1252 avgr = r->total_lat / r->nb_atoms;
1262 static struct sort_dimension avg_sort_dimension = {
1267 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1269 if (l->max_lat < r->max_lat)
1271 if (l->max_lat > r->max_lat)
1277 static struct sort_dimension max_sort_dimension = {
1282 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1284 if (l->nb_atoms < r->nb_atoms)
1286 if (l->nb_atoms > r->nb_atoms)
1292 static struct sort_dimension switch_sort_dimension = {
1297 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1299 if (l->total_runtime < r->total_runtime)
1301 if (l->total_runtime > r->total_runtime)
1307 static struct sort_dimension runtime_sort_dimension = {
1312 static struct sort_dimension *available_sorts[] = {
1313 &pid_sort_dimension,
1314 &avg_sort_dimension,
1315 &max_sort_dimension,
1316 &switch_sort_dimension,
1317 &runtime_sort_dimension,
1320 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1322 static LIST_HEAD(sort_list);
1324 static int sort_dimension__add(const char *tok, struct list_head *list)
1328 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1329 if (!strcmp(available_sorts[i]->name, tok)) {
1330 list_add_tail(&available_sorts[i]->list, list);
1339 static void setup_sorting(void);
1341 static void sort_lat(void)
1343 struct rb_node *node;
1346 struct work_atoms *data;
1347 node = rb_first(&atom_root);
1351 rb_erase(node, &atom_root);
1352 data = rb_entry(node, struct work_atoms, node);
1353 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1357 static struct trace_sched_handler *trace_handler;
1360 process_sched_wakeup_event(void *data, struct machine *machine,
1361 struct event *event,
1363 u64 timestamp __used,
1364 struct thread *thread __used)
1366 struct trace_wakeup_event wakeup_event;
1368 FILL_COMMON_FIELDS(wakeup_event, event, data);
1370 FILL_ARRAY(wakeup_event, comm, event, data);
1371 FILL_FIELD(wakeup_event, pid, event, data);
1372 FILL_FIELD(wakeup_event, prio, event, data);
1373 FILL_FIELD(wakeup_event, success, event, data);
1374 FILL_FIELD(wakeup_event, cpu, event, data);
1376 if (trace_handler->wakeup_event)
1377 trace_handler->wakeup_event(&wakeup_event, machine, event,
1378 cpu, timestamp, thread);
1382 * Track the current task - that way we can know whether there's any
1383 * weird events, such as a task being switched away that is not current.
1387 static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1389 static struct thread *curr_thread[MAX_CPUS];
1391 static char next_shortname1 = 'A';
1392 static char next_shortname2 = '0';
1395 map_switch_event(struct trace_switch_event *switch_event,
1396 struct machine *machine,
1397 struct event *event __used,
1400 struct thread *thread __used)
1402 struct thread *sched_out __used, *sched_in;
1408 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1410 if (this_cpu > max_cpu)
1413 timestamp0 = cpu_last_switched[this_cpu];
1414 cpu_last_switched[this_cpu] = timestamp;
1416 delta = timestamp - timestamp0;
1421 die("hm, delta: %" PRIu64 " < 0 ?\n", delta);
1424 sched_out = machine__findnew_thread(machine, switch_event->prev_pid);
1425 sched_in = machine__findnew_thread(machine, switch_event->next_pid);
1427 curr_thread[this_cpu] = sched_in;
1432 if (!sched_in->shortname[0]) {
1433 sched_in->shortname[0] = next_shortname1;
1434 sched_in->shortname[1] = next_shortname2;
1436 if (next_shortname1 < 'Z') {
1439 next_shortname1='A';
1440 if (next_shortname2 < '9') {
1443 next_shortname2='0';
1449 for (cpu = 0; cpu <= max_cpu; cpu++) {
1450 if (cpu != this_cpu)
1455 if (curr_thread[cpu]) {
1456 if (curr_thread[cpu]->pid)
1457 printf("%2s ", curr_thread[cpu]->shortname);
1464 printf(" %12.6f secs ", (double)timestamp/1e9);
1465 if (new_shortname) {
1466 printf("%s => %s:%d\n",
1467 sched_in->shortname, sched_in->comm, sched_in->pid);
1475 process_sched_switch_event(void *data, struct machine *machine,
1476 struct event *event,
1478 u64 timestamp __used,
1479 struct thread *thread __used)
1481 struct trace_switch_event switch_event;
1483 FILL_COMMON_FIELDS(switch_event, event, data);
1485 FILL_ARRAY(switch_event, prev_comm, event, data);
1486 FILL_FIELD(switch_event, prev_pid, event, data);
1487 FILL_FIELD(switch_event, prev_prio, event, data);
1488 FILL_FIELD(switch_event, prev_state, event, data);
1489 FILL_ARRAY(switch_event, next_comm, event, data);
1490 FILL_FIELD(switch_event, next_pid, event, data);
1491 FILL_FIELD(switch_event, next_prio, event, data);
1493 if (curr_pid[this_cpu] != (u32)-1) {
1495 * Are we trying to switch away a PID that is
1498 if (curr_pid[this_cpu] != switch_event.prev_pid)
1499 nr_context_switch_bugs++;
1501 if (trace_handler->switch_event)
1502 trace_handler->switch_event(&switch_event, machine, event,
1503 this_cpu, timestamp, thread);
1505 curr_pid[this_cpu] = switch_event.next_pid;
1509 process_sched_runtime_event(void *data, struct machine *machine,
1510 struct event *event,
1512 u64 timestamp __used,
1513 struct thread *thread __used)
1515 struct trace_runtime_event runtime_event;
1517 FILL_ARRAY(runtime_event, comm, event, data);
1518 FILL_FIELD(runtime_event, pid, event, data);
1519 FILL_FIELD(runtime_event, runtime, event, data);
1520 FILL_FIELD(runtime_event, vruntime, event, data);
1522 if (trace_handler->runtime_event)
1523 trace_handler->runtime_event(&runtime_event, machine, event, cpu, timestamp, thread);
1527 process_sched_fork_event(void *data,
1528 struct event *event,
1530 u64 timestamp __used,
1531 struct thread *thread __used)
1533 struct trace_fork_event fork_event;
1535 FILL_COMMON_FIELDS(fork_event, event, data);
1537 FILL_ARRAY(fork_event, parent_comm, event, data);
1538 FILL_FIELD(fork_event, parent_pid, event, data);
1539 FILL_ARRAY(fork_event, child_comm, event, data);
1540 FILL_FIELD(fork_event, child_pid, event, data);
1542 if (trace_handler->fork_event)
1543 trace_handler->fork_event(&fork_event, event,
1544 cpu, timestamp, thread);
1548 process_sched_exit_event(struct event *event,
1550 u64 timestamp __used,
1551 struct thread *thread __used)
1554 printf("sched_exit event %p\n", event);
1558 process_sched_migrate_task_event(void *data, struct machine *machine,
1559 struct event *event,
1561 u64 timestamp __used,
1562 struct thread *thread __used)
1564 struct trace_migrate_task_event migrate_task_event;
1566 FILL_COMMON_FIELDS(migrate_task_event, event, data);
1568 FILL_ARRAY(migrate_task_event, comm, event, data);
1569 FILL_FIELD(migrate_task_event, pid, event, data);
1570 FILL_FIELD(migrate_task_event, prio, event, data);
1571 FILL_FIELD(migrate_task_event, cpu, event, data);
1573 if (trace_handler->migrate_task_event)
1574 trace_handler->migrate_task_event(&migrate_task_event, machine,
1575 event, cpu, timestamp, thread);
1578 static void process_raw_event(union perf_event *raw_event __used,
1579 struct machine *machine, void *data, int cpu,
1580 u64 timestamp, struct thread *thread)
1582 struct event *event;
1586 type = trace_parse_common_type(data);
1587 event = trace_find_event(type);
1589 if (!strcmp(event->name, "sched_switch"))
1590 process_sched_switch_event(data, machine, event, cpu, timestamp, thread);
1591 if (!strcmp(event->name, "sched_stat_runtime"))
1592 process_sched_runtime_event(data, machine, event, cpu, timestamp, thread);
1593 if (!strcmp(event->name, "sched_wakeup"))
1594 process_sched_wakeup_event(data, machine, event, cpu, timestamp, thread);
1595 if (!strcmp(event->name, "sched_wakeup_new"))
1596 process_sched_wakeup_event(data, machine, event, cpu, timestamp, thread);
1597 if (!strcmp(event->name, "sched_process_fork"))
1598 process_sched_fork_event(data, event, cpu, timestamp, thread);
1599 if (!strcmp(event->name, "sched_process_exit"))
1600 process_sched_exit_event(event, cpu, timestamp, thread);
1601 if (!strcmp(event->name, "sched_migrate_task"))
1602 process_sched_migrate_task_event(data, machine, event, cpu, timestamp, thread);
1605 static int process_sample_event(struct perf_event_ops *ops __used,
1606 union perf_event *event,
1607 struct perf_sample *sample,
1608 struct perf_evsel *evsel,
1609 struct machine *machine)
1611 struct thread *thread;
1613 if (!(evsel->attr.sample_type & PERF_SAMPLE_RAW))
1616 thread = machine__findnew_thread(machine, sample->pid);
1617 if (thread == NULL) {
1618 pr_debug("problem processing %d event, skipping it.\n",
1619 event->header.type);
1623 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1625 if (profile_cpu != -1 && profile_cpu != (int)sample->cpu)
1628 process_raw_event(event, machine, sample->raw_data, sample->cpu,
1629 sample->time, thread);
1634 static struct perf_event_ops event_ops = {
1635 .sample = process_sample_event,
1636 .comm = perf_event__process_comm,
1637 .lost = perf_event__process_lost,
1638 .fork = perf_event__process_task,
1639 .ordered_samples = true,
1642 static void read_events(bool destroy, struct perf_session **psession)
1645 struct perf_session *session = perf_session__new(input_name, O_RDONLY,
1646 0, false, &event_ops);
1647 if (session == NULL)
1650 if (perf_session__has_traces(session, "record -R")) {
1651 err = perf_session__process_events(session, &event_ops);
1653 die("Failed to process events, error %d", err);
1655 nr_events = session->hists.stats.nr_events[0];
1656 nr_lost_events = session->hists.stats.total_lost;
1657 nr_lost_chunks = session->hists.stats.nr_events[PERF_RECORD_LOST];
1661 perf_session__delete(session);
1664 *psession = session;
1667 static void print_bad_events(void)
1669 if (nr_unordered_timestamps && nr_timestamps) {
1670 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1671 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1672 nr_unordered_timestamps, nr_timestamps);
1674 if (nr_lost_events && nr_events) {
1675 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1676 (double)nr_lost_events/(double)nr_events*100.0,
1677 nr_lost_events, nr_events, nr_lost_chunks);
1679 if (nr_state_machine_bugs && nr_timestamps) {
1680 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1681 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1682 nr_state_machine_bugs, nr_timestamps);
1684 printf(" (due to lost events?)");
1687 if (nr_context_switch_bugs && nr_timestamps) {
1688 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1689 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1690 nr_context_switch_bugs, nr_timestamps);
1692 printf(" (due to lost events?)");
1697 static void __cmd_lat(void)
1699 struct rb_node *next;
1700 struct perf_session *session;
1703 read_events(false, &session);
1706 printf("\n ---------------------------------------------------------------------------------------------------------------\n");
1707 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n");
1708 printf(" ---------------------------------------------------------------------------------------------------------------\n");
1710 next = rb_first(&sorted_atom_root);
1713 struct work_atoms *work_list;
1715 work_list = rb_entry(next, struct work_atoms, node);
1716 output_lat_thread(work_list);
1717 next = rb_next(next);
1720 printf(" -----------------------------------------------------------------------------------------\n");
1721 printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n",
1722 (double)all_runtime/1e6, all_count);
1724 printf(" ---------------------------------------------------\n");
1729 perf_session__delete(session);
1732 static struct trace_sched_handler map_ops = {
1733 .wakeup_event = NULL,
1734 .switch_event = map_switch_event,
1735 .runtime_event = NULL,
1739 static void __cmd_map(void)
1741 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1744 read_events(true, NULL);
1748 static void __cmd_replay(void)
1752 calibrate_run_measurement_overhead();
1753 calibrate_sleep_measurement_overhead();
1755 test_calibrations();
1757 read_events(true, NULL);
1759 printf("nr_run_events: %ld\n", nr_run_events);
1760 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1761 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1763 if (targetless_wakeups)
1764 printf("target-less wakeups: %ld\n", targetless_wakeups);
1765 if (multitarget_wakeups)
1766 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1767 if (nr_run_events_optimized)
1768 printf("run atoms optimized: %ld\n",
1769 nr_run_events_optimized);
1771 print_task_traces();
1772 add_cross_task_wakeups();
1775 printf("------------------------------------------------------------\n");
1776 for (i = 0; i < replay_repeat; i++)
1781 static const char * const sched_usage[] = {
1782 "perf sched [<options>] {record|latency|map|replay|script}",
1786 static const struct option sched_options[] = {
1787 OPT_STRING('i', "input", &input_name, "file",
1789 OPT_INCR('v', "verbose", &verbose,
1790 "be more verbose (show symbol address, etc)"),
1791 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1792 "dump raw trace in ASCII"),
1796 static const char * const latency_usage[] = {
1797 "perf sched latency [<options>]",
1801 static const struct option latency_options[] = {
1802 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1803 "sort by key(s): runtime, switch, avg, max"),
1804 OPT_INCR('v', "verbose", &verbose,
1805 "be more verbose (show symbol address, etc)"),
1806 OPT_INTEGER('C', "CPU", &profile_cpu,
1807 "CPU to profile on"),
1808 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1809 "dump raw trace in ASCII"),
1813 static const char * const replay_usage[] = {
1814 "perf sched replay [<options>]",
1818 static const struct option replay_options[] = {
1819 OPT_UINTEGER('r', "repeat", &replay_repeat,
1820 "repeat the workload replay N times (-1: infinite)"),
1821 OPT_INCR('v', "verbose", &verbose,
1822 "be more verbose (show symbol address, etc)"),
1823 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1824 "dump raw trace in ASCII"),
1828 static void setup_sorting(void)
1830 char *tmp, *tok, *str = strdup(sort_order);
1832 for (tok = strtok_r(str, ", ", &tmp);
1833 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1834 if (sort_dimension__add(tok, &sort_list) < 0) {
1835 error("Unknown --sort key: `%s'", tok);
1836 usage_with_options(latency_usage, latency_options);
1842 sort_dimension__add("pid", &cmp_pid);
1845 static const char *record_args[] = {
1852 "-e", "sched:sched_switch",
1853 "-e", "sched:sched_stat_wait",
1854 "-e", "sched:sched_stat_sleep",
1855 "-e", "sched:sched_stat_iowait",
1856 "-e", "sched:sched_stat_runtime",
1857 "-e", "sched:sched_process_exit",
1858 "-e", "sched:sched_process_fork",
1859 "-e", "sched:sched_wakeup",
1860 "-e", "sched:sched_migrate_task",
1863 static int __cmd_record(int argc, const char **argv)
1865 unsigned int rec_argc, i, j;
1866 const char **rec_argv;
1868 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1869 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1871 if (rec_argv == NULL)
1874 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1875 rec_argv[i] = strdup(record_args[i]);
1877 for (j = 1; j < (unsigned int)argc; j++, i++)
1878 rec_argv[i] = argv[j];
1880 BUG_ON(i != rec_argc);
1882 return cmd_record(i, rec_argv, NULL);
1885 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1887 argc = parse_options(argc, argv, sched_options, sched_usage,
1888 PARSE_OPT_STOP_AT_NON_OPTION);
1890 usage_with_options(sched_usage, sched_options);
1893 * Aliased to 'perf script' for now:
1895 if (!strcmp(argv[0], "script"))
1896 return cmd_script(argc, argv, prefix);
1899 if (!strncmp(argv[0], "rec", 3)) {
1900 return __cmd_record(argc, argv);
1901 } else if (!strncmp(argv[0], "lat", 3)) {
1902 trace_handler = &lat_ops;
1904 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1906 usage_with_options(latency_usage, latency_options);
1910 } else if (!strcmp(argv[0], "map")) {
1911 trace_handler = &map_ops;
1914 } else if (!strncmp(argv[0], "rep", 3)) {
1915 trace_handler = &replay_ops;
1917 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1919 usage_with_options(replay_usage, replay_options);
1923 usage_with_options(sched_usage, sched_options);