5 #include "util/cache.h"
6 #include "util/symbol.h"
7 #include "util/thread.h"
8 #include "util/header.h"
10 #include "util/parse-options.h"
11 #include "util/trace-event.h"
13 #include "util/debug.h"
15 #include <sys/types.h>
16 #include <sys/prctl.h>
18 #include <semaphore.h>
22 static char const *input_name = "perf.data";
24 static unsigned long page_size;
25 static unsigned long mmap_window = 32;
27 static unsigned long total_comm = 0;
29 static struct rb_root threads;
30 static struct thread *last_match;
32 static struct perf_header *header;
33 static u64 sample_type;
35 static char default_sort_order[] = "avg, max, switch, runtime";
36 static char *sort_order = default_sort_order;
38 #define PR_SET_NAME 15 /* Set process name */
41 #define BUG_ON(x) assert(!(x))
43 static u64 run_measurement_overhead;
44 static u64 sleep_measurement_overhead;
51 static unsigned long nr_tasks;
60 unsigned long nr_events;
61 unsigned long curr_event;
62 struct sched_atom **atoms;
73 enum sched_event_type {
80 enum sched_event_type type;
86 struct task_desc *wakee;
89 static struct task_desc *pid_to_task[MAX_PID];
91 static struct task_desc **tasks;
93 static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
94 static u64 start_time;
96 static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
98 static unsigned long nr_run_events;
99 static unsigned long nr_sleep_events;
100 static unsigned long nr_wakeup_events;
102 static unsigned long nr_sleep_corrections;
103 static unsigned long nr_run_events_optimized;
105 static unsigned long targetless_wakeups;
106 static unsigned long multitarget_wakeups;
108 static u64 cpu_usage;
109 static u64 runavg_cpu_usage;
110 static u64 parent_cpu_usage;
111 static u64 runavg_parent_cpu_usage;
113 static unsigned long nr_runs;
114 static u64 sum_runtime;
115 static u64 sum_fluct;
118 static unsigned long replay_repeat = 10;
119 static unsigned long nr_timestamps;
120 static unsigned long nr_unordered_timestamps;
121 static unsigned long nr_state_machine_bugs;
122 static unsigned long nr_context_switch_bugs;
123 static unsigned long nr_events;
124 static unsigned long nr_lost_chunks;
125 static unsigned long nr_lost_events;
127 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
137 struct list_head list;
138 enum thread_state state;
146 struct list_head work_list;
147 struct thread *thread;
155 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
157 static struct rb_root atom_root, sorted_atom_root;
159 static u64 all_runtime;
160 static u64 all_count;
163 static u64 get_nsecs(void)
167 clock_gettime(CLOCK_MONOTONIC, &ts);
169 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
172 static void burn_nsecs(u64 nsecs)
174 u64 T0 = get_nsecs(), T1;
178 } while (T1 + run_measurement_overhead < T0 + nsecs);
181 static void sleep_nsecs(u64 nsecs)
185 ts.tv_nsec = nsecs % 999999999;
186 ts.tv_sec = nsecs / 999999999;
188 nanosleep(&ts, NULL);
191 static void calibrate_run_measurement_overhead(void)
193 u64 T0, T1, delta, min_delta = 1000000000ULL;
196 for (i = 0; i < 10; i++) {
201 min_delta = min(min_delta, delta);
203 run_measurement_overhead = min_delta;
205 printf("run measurement overhead: %Ld nsecs\n", min_delta);
208 static void calibrate_sleep_measurement_overhead(void)
210 u64 T0, T1, delta, min_delta = 1000000000ULL;
213 for (i = 0; i < 10; i++) {
218 min_delta = min(min_delta, delta);
221 sleep_measurement_overhead = min_delta;
223 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
226 static struct sched_atom *
227 get_new_event(struct task_desc *task, u64 timestamp)
229 struct sched_atom *event = calloc(1, sizeof(*event));
230 unsigned long idx = task->nr_events;
233 event->timestamp = timestamp;
237 size = sizeof(struct sched_atom *) * task->nr_events;
238 task->atoms = realloc(task->atoms, size);
239 BUG_ON(!task->atoms);
241 task->atoms[idx] = event;
246 static struct sched_atom *last_event(struct task_desc *task)
248 if (!task->nr_events)
251 return task->atoms[task->nr_events - 1];
255 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
257 struct sched_atom *event, *curr_event = last_event(task);
260 * optimize an existing RUN event by merging this one
263 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
264 nr_run_events_optimized++;
265 curr_event->duration += duration;
269 event = get_new_event(task, timestamp);
271 event->type = SCHED_EVENT_RUN;
272 event->duration = duration;
278 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
279 struct task_desc *wakee)
281 struct sched_atom *event, *wakee_event;
283 event = get_new_event(task, timestamp);
284 event->type = SCHED_EVENT_WAKEUP;
285 event->wakee = wakee;
287 wakee_event = last_event(wakee);
288 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
289 targetless_wakeups++;
292 if (wakee_event->wait_sem) {
293 multitarget_wakeups++;
297 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
298 sem_init(wakee_event->wait_sem, 0, 0);
299 wakee_event->specific_wait = 1;
300 event->wait_sem = wakee_event->wait_sem;
306 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
307 u64 task_state __used)
309 struct sched_atom *event = get_new_event(task, timestamp);
311 event->type = SCHED_EVENT_SLEEP;
316 static struct task_desc *register_pid(unsigned long pid, const char *comm)
318 struct task_desc *task;
320 BUG_ON(pid >= MAX_PID);
322 task = pid_to_task[pid];
327 task = calloc(1, sizeof(*task));
330 strcpy(task->comm, comm);
332 * every task starts in sleeping state - this gets ignored
333 * if there's no wakeup pointing to this sleep state:
335 add_sched_event_sleep(task, 0, 0);
337 pid_to_task[pid] = task;
339 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
341 tasks[task->nr] = task;
344 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
350 static void print_task_traces(void)
352 struct task_desc *task;
355 for (i = 0; i < nr_tasks; i++) {
357 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
358 task->nr, task->comm, task->pid, task->nr_events);
362 static void add_cross_task_wakeups(void)
364 struct task_desc *task1, *task2;
367 for (i = 0; i < nr_tasks; i++) {
373 add_sched_event_wakeup(task1, 0, task2);
378 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
385 delta = start_time + atom->timestamp - now;
387 switch (atom->type) {
388 case SCHED_EVENT_RUN:
389 burn_nsecs(atom->duration);
391 case SCHED_EVENT_SLEEP:
393 ret = sem_wait(atom->wait_sem);
396 case SCHED_EVENT_WAKEUP:
398 ret = sem_post(atom->wait_sem);
406 static u64 get_cpu_usage_nsec_parent(void)
412 err = getrusage(RUSAGE_SELF, &ru);
415 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
416 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
421 static u64 get_cpu_usage_nsec_self(void)
423 char filename [] = "/proc/1234567890/sched";
424 unsigned long msecs, nsecs;
432 sprintf(filename, "/proc/%d/sched", getpid());
433 file = fopen(filename, "r");
436 while ((chars = getline(&line, &len, file)) != -1) {
437 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
440 total = msecs*1e6 + nsecs;
451 static void *thread_func(void *ctx)
453 struct task_desc *this_task = ctx;
454 u64 cpu_usage_0, cpu_usage_1;
455 unsigned long i, ret;
458 sprintf(comm2, ":%s", this_task->comm);
459 prctl(PR_SET_NAME, comm2);
462 ret = sem_post(&this_task->ready_for_work);
464 ret = pthread_mutex_lock(&start_work_mutex);
466 ret = pthread_mutex_unlock(&start_work_mutex);
469 cpu_usage_0 = get_cpu_usage_nsec_self();
471 for (i = 0; i < this_task->nr_events; i++) {
472 this_task->curr_event = i;
473 process_sched_event(this_task, this_task->atoms[i]);
476 cpu_usage_1 = get_cpu_usage_nsec_self();
477 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
479 ret = sem_post(&this_task->work_done_sem);
482 ret = pthread_mutex_lock(&work_done_wait_mutex);
484 ret = pthread_mutex_unlock(&work_done_wait_mutex);
490 static void create_tasks(void)
492 struct task_desc *task;
497 err = pthread_attr_init(&attr);
499 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
501 err = pthread_mutex_lock(&start_work_mutex);
503 err = pthread_mutex_lock(&work_done_wait_mutex);
505 for (i = 0; i < nr_tasks; i++) {
507 sem_init(&task->sleep_sem, 0, 0);
508 sem_init(&task->ready_for_work, 0, 0);
509 sem_init(&task->work_done_sem, 0, 0);
510 task->curr_event = 0;
511 err = pthread_create(&task->thread, &attr, thread_func, task);
516 static void wait_for_tasks(void)
518 u64 cpu_usage_0, cpu_usage_1;
519 struct task_desc *task;
520 unsigned long i, ret;
522 start_time = get_nsecs();
524 pthread_mutex_unlock(&work_done_wait_mutex);
526 for (i = 0; i < nr_tasks; i++) {
528 ret = sem_wait(&task->ready_for_work);
530 sem_init(&task->ready_for_work, 0, 0);
532 ret = pthread_mutex_lock(&work_done_wait_mutex);
535 cpu_usage_0 = get_cpu_usage_nsec_parent();
537 pthread_mutex_unlock(&start_work_mutex);
539 for (i = 0; i < nr_tasks; i++) {
541 ret = sem_wait(&task->work_done_sem);
543 sem_init(&task->work_done_sem, 0, 0);
544 cpu_usage += task->cpu_usage;
548 cpu_usage_1 = get_cpu_usage_nsec_parent();
549 if (!runavg_cpu_usage)
550 runavg_cpu_usage = cpu_usage;
551 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
553 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
554 if (!runavg_parent_cpu_usage)
555 runavg_parent_cpu_usage = parent_cpu_usage;
556 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
557 parent_cpu_usage)/10;
559 ret = pthread_mutex_lock(&start_work_mutex);
562 for (i = 0; i < nr_tasks; i++) {
564 sem_init(&task->sleep_sem, 0, 0);
565 task->curr_event = 0;
569 static void run_one_test(void)
571 u64 T0, T1, delta, avg_delta, fluct, std_dev;
578 sum_runtime += delta;
581 avg_delta = sum_runtime / nr_runs;
582 if (delta < avg_delta)
583 fluct = avg_delta - delta;
585 fluct = delta - avg_delta;
587 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
590 run_avg = (run_avg*9 + delta)/10;
592 printf("#%-3ld: %0.3f, ",
593 nr_runs, (double)delta/1000000.0);
595 printf("ravg: %0.2f, ",
596 (double)run_avg/1e6);
598 printf("cpu: %0.2f / %0.2f",
599 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
603 * rusage statistics done by the parent, these are less
604 * accurate than the sum_exec_runtime based statistics:
606 printf(" [%0.2f / %0.2f]",
607 (double)parent_cpu_usage/1e6,
608 (double)runavg_parent_cpu_usage/1e6);
613 if (nr_sleep_corrections)
614 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
615 nr_sleep_corrections = 0;
618 static void test_calibrations(void)
626 printf("the run test took %Ld nsecs\n", T1-T0);
632 printf("the sleep test took %Ld nsecs\n", T1-T0);
636 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
638 struct thread *thread;
640 thread = threads__findnew(event->comm.pid, &threads, &last_match);
642 dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
643 (void *)(offset + head),
644 (void *)(long)(event->header.size),
645 event->comm.comm, event->comm.pid);
647 if (thread == NULL ||
648 thread__set_comm(thread, event->comm.comm)) {
649 dump_printf("problem processing perf_event_comm, skipping event.\n");
658 struct raw_event_sample {
663 #define FILL_FIELD(ptr, field, event, data) \
664 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
666 #define FILL_ARRAY(ptr, array, event, data) \
668 void *__array = raw_field_ptr(event, #array, data); \
669 memcpy(ptr.array, __array, sizeof(ptr.array)); \
672 #define FILL_COMMON_FIELDS(ptr, event, data) \
674 FILL_FIELD(ptr, common_type, event, data); \
675 FILL_FIELD(ptr, common_flags, event, data); \
676 FILL_FIELD(ptr, common_preempt_count, event, data); \
677 FILL_FIELD(ptr, common_pid, event, data); \
678 FILL_FIELD(ptr, common_tgid, event, data); \
683 struct trace_switch_event {
688 u8 common_preempt_count;
701 struct trace_runtime_event {
706 u8 common_preempt_count;
716 struct trace_wakeup_event {
721 u8 common_preempt_count;
733 struct trace_fork_event {
738 u8 common_preempt_count;
742 char parent_comm[16];
748 struct trace_sched_handler {
749 void (*switch_event)(struct trace_switch_event *,
753 struct thread *thread);
755 void (*runtime_event)(struct trace_runtime_event *,
759 struct thread *thread);
761 void (*wakeup_event)(struct trace_wakeup_event *,
765 struct thread *thread);
767 void (*fork_event)(struct trace_fork_event *,
771 struct thread *thread);
776 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
779 u64 timestamp __used,
780 struct thread *thread __used)
782 struct task_desc *waker, *wakee;
785 printf("sched_wakeup event %p\n", event);
787 printf(" ... pid %d woke up %s/%d\n",
788 wakeup_event->common_pid,
793 waker = register_pid(wakeup_event->common_pid, "<unknown>");
794 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
796 add_sched_event_wakeup(waker, timestamp, wakee);
799 static u64 cpu_last_switched[MAX_CPUS];
802 replay_switch_event(struct trace_switch_event *switch_event,
806 struct thread *thread __used)
808 struct task_desc *prev, *next;
813 printf("sched_switch event %p\n", event);
815 if (cpu >= MAX_CPUS || cpu < 0)
818 timestamp0 = cpu_last_switched[cpu];
820 delta = timestamp - timestamp0;
825 die("hm, delta: %Ld < 0 ?\n", delta);
828 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
829 switch_event->prev_comm, switch_event->prev_pid,
830 switch_event->next_comm, switch_event->next_pid,
834 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
835 next = register_pid(switch_event->next_pid, switch_event->next_comm);
837 cpu_last_switched[cpu] = timestamp;
839 add_sched_event_run(prev, timestamp, delta);
840 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
845 replay_fork_event(struct trace_fork_event *fork_event,
848 u64 timestamp __used,
849 struct thread *thread __used)
852 printf("sched_fork event %p\n", event);
853 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
854 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
856 register_pid(fork_event->parent_pid, fork_event->parent_comm);
857 register_pid(fork_event->child_pid, fork_event->child_comm);
860 static struct trace_sched_handler replay_ops = {
861 .wakeup_event = replay_wakeup_event,
862 .switch_event = replay_switch_event,
863 .fork_event = replay_fork_event,
866 struct sort_dimension {
869 struct list_head list;
872 static LIST_HEAD(cmp_pid);
875 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
877 struct sort_dimension *sort;
880 BUG_ON(list_empty(list));
882 list_for_each_entry(sort, list, list) {
883 ret = sort->cmp(l, r);
891 static struct work_atoms *
892 thread_atoms_search(struct rb_root *root, struct thread *thread,
893 struct list_head *sort_list)
895 struct rb_node *node = root->rb_node;
896 struct work_atoms key = { .thread = thread };
899 struct work_atoms *atoms;
902 atoms = container_of(node, struct work_atoms, node);
904 cmp = thread_lat_cmp(sort_list, &key, atoms);
906 node = node->rb_left;
908 node = node->rb_right;
910 BUG_ON(thread != atoms->thread);
918 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
919 struct list_head *sort_list)
921 struct rb_node **new = &(root->rb_node), *parent = NULL;
924 struct work_atoms *this;
927 this = container_of(*new, struct work_atoms, node);
930 cmp = thread_lat_cmp(sort_list, data, this);
933 new = &((*new)->rb_left);
935 new = &((*new)->rb_right);
938 rb_link_node(&data->node, parent, new);
939 rb_insert_color(&data->node, root);
942 static void thread_atoms_insert(struct thread *thread)
944 struct work_atoms *atoms;
946 atoms = calloc(sizeof(*atoms), 1);
950 atoms->thread = thread;
951 INIT_LIST_HEAD(&atoms->work_list);
952 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
956 latency_fork_event(struct trace_fork_event *fork_event __used,
957 struct event *event __used,
959 u64 timestamp __used,
960 struct thread *thread __used)
962 /* should insert the newcomer */
966 static char sched_out_state(struct trace_switch_event *switch_event)
968 const char *str = TASK_STATE_TO_CHAR_STR;
970 return str[switch_event->prev_state];
974 add_sched_out_event(struct work_atoms *atoms,
978 struct work_atom *atom;
980 atom = calloc(sizeof(*atom), 1);
984 atom->sched_out_time = timestamp;
986 if (run_state == 'R') {
987 atom->state = THREAD_WAIT_CPU;
988 atom->wake_up_time = atom->sched_out_time;
991 list_add_tail(&atom->list, &atoms->work_list);
995 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
997 struct work_atom *atom;
999 BUG_ON(list_empty(&atoms->work_list));
1001 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1003 atom->runtime += delta;
1004 atoms->total_runtime += delta;
1008 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1010 struct work_atom *atom;
1013 if (list_empty(&atoms->work_list))
1016 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1018 if (atom->state != THREAD_WAIT_CPU)
1021 if (timestamp < atom->wake_up_time) {
1022 atom->state = THREAD_IGNORE;
1026 atom->state = THREAD_SCHED_IN;
1027 atom->sched_in_time = timestamp;
1029 delta = atom->sched_in_time - atom->wake_up_time;
1030 atoms->total_lat += delta;
1031 if (delta > atoms->max_lat)
1032 atoms->max_lat = delta;
1037 latency_switch_event(struct trace_switch_event *switch_event,
1038 struct event *event __used,
1041 struct thread *thread __used)
1043 struct work_atoms *out_events, *in_events;
1044 struct thread *sched_out, *sched_in;
1048 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1050 timestamp0 = cpu_last_switched[cpu];
1051 cpu_last_switched[cpu] = timestamp;
1053 delta = timestamp - timestamp0;
1058 die("hm, delta: %Ld < 0 ?\n", delta);
1061 sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match);
1062 sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
1064 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1066 thread_atoms_insert(sched_out);
1067 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1069 die("out-event: Internal tree error");
1071 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1073 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1075 thread_atoms_insert(sched_in);
1076 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1078 die("in-event: Internal tree error");
1080 * Take came in we have not heard about yet,
1081 * add in an initial atom in runnable state:
1083 add_sched_out_event(in_events, 'R', timestamp);
1085 add_sched_in_event(in_events, timestamp);
1089 latency_runtime_event(struct trace_runtime_event *runtime_event,
1090 struct event *event __used,
1093 struct thread *this_thread __used)
1095 struct work_atoms *atoms;
1096 struct thread *thread;
1098 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1100 thread = threads__findnew(runtime_event->pid, &threads, &last_match);
1101 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1103 thread_atoms_insert(thread);
1104 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1106 die("in-event: Internal tree error");
1107 add_sched_out_event(atoms, 'R', timestamp);
1110 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1114 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1115 struct event *__event __used,
1118 struct thread *thread __used)
1120 struct work_atoms *atoms;
1121 struct work_atom *atom;
1122 struct thread *wakee;
1124 /* Note for later, it may be interesting to observe the failing cases */
1125 if (!wakeup_event->success)
1128 wakee = threads__findnew(wakeup_event->pid, &threads, &last_match);
1129 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1131 thread_atoms_insert(wakee);
1132 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1134 die("wakeup-event: Internal tree error");
1135 add_sched_out_event(atoms, 'S', timestamp);
1138 BUG_ON(list_empty(&atoms->work_list));
1140 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1142 if (atom->state != THREAD_SLEEPING)
1143 nr_state_machine_bugs++;
1146 if (atom->sched_out_time > timestamp) {
1147 nr_unordered_timestamps++;
1151 atom->state = THREAD_WAIT_CPU;
1152 atom->wake_up_time = timestamp;
1155 static struct trace_sched_handler lat_ops = {
1156 .wakeup_event = latency_wakeup_event,
1157 .switch_event = latency_switch_event,
1158 .runtime_event = latency_runtime_event,
1159 .fork_event = latency_fork_event,
1162 static void output_lat_thread(struct work_atoms *work_list)
1168 if (!work_list->nb_atoms)
1171 * Ignore idle threads:
1173 if (!strcmp(work_list->thread->comm, "swapper"))
1176 all_runtime += work_list->total_runtime;
1177 all_count += work_list->nb_atoms;
1179 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
1181 for (i = 0; i < 24 - ret; i++)
1184 avg = work_list->total_lat / work_list->nb_atoms;
1186 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
1187 (double)work_list->total_runtime / 1e6,
1188 work_list->nb_atoms, (double)avg / 1e6,
1189 (double)work_list->max_lat / 1e6);
1192 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1194 if (l->thread->pid < r->thread->pid)
1196 if (l->thread->pid > r->thread->pid)
1202 static struct sort_dimension pid_sort_dimension = {
1207 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1217 avgl = l->total_lat / l->nb_atoms;
1218 avgr = r->total_lat / r->nb_atoms;
1228 static struct sort_dimension avg_sort_dimension = {
1233 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1235 if (l->max_lat < r->max_lat)
1237 if (l->max_lat > r->max_lat)
1243 static struct sort_dimension max_sort_dimension = {
1248 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1250 if (l->nb_atoms < r->nb_atoms)
1252 if (l->nb_atoms > r->nb_atoms)
1258 static struct sort_dimension switch_sort_dimension = {
1263 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1265 if (l->total_runtime < r->total_runtime)
1267 if (l->total_runtime > r->total_runtime)
1273 static struct sort_dimension runtime_sort_dimension = {
1278 static struct sort_dimension *available_sorts[] = {
1279 &pid_sort_dimension,
1280 &avg_sort_dimension,
1281 &max_sort_dimension,
1282 &switch_sort_dimension,
1283 &runtime_sort_dimension,
1286 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1288 static LIST_HEAD(sort_list);
1290 static int sort_dimension__add(char *tok, struct list_head *list)
1294 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1295 if (!strcmp(available_sorts[i]->name, tok)) {
1296 list_add_tail(&available_sorts[i]->list, list);
1305 static void setup_sorting(void);
1307 static void sort_lat(void)
1309 struct rb_node *node;
1312 struct work_atoms *data;
1313 node = rb_first(&atom_root);
1317 rb_erase(node, &atom_root);
1318 data = rb_entry(node, struct work_atoms, node);
1319 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1323 static struct trace_sched_handler *trace_handler;
1326 process_sched_wakeup_event(struct raw_event_sample *raw,
1327 struct event *event,
1329 u64 timestamp __used,
1330 struct thread *thread __used)
1332 struct trace_wakeup_event wakeup_event;
1334 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1336 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1337 FILL_FIELD(wakeup_event, pid, event, raw->data);
1338 FILL_FIELD(wakeup_event, prio, event, raw->data);
1339 FILL_FIELD(wakeup_event, success, event, raw->data);
1340 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1342 if (trace_handler->wakeup_event)
1343 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
1347 * Track the current task - that way we can know whether there's any
1348 * weird events, such as a task being switched away that is not current.
1352 static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1354 static struct thread *curr_thread[MAX_CPUS];
1356 static char next_shortname1 = 'A';
1357 static char next_shortname2 = '0';
1360 map_switch_event(struct trace_switch_event *switch_event,
1361 struct event *event __used,
1364 struct thread *thread __used)
1366 struct thread *sched_out, *sched_in;
1372 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1374 if (this_cpu > max_cpu)
1377 timestamp0 = cpu_last_switched[this_cpu];
1378 cpu_last_switched[this_cpu] = timestamp;
1380 delta = timestamp - timestamp0;
1385 die("hm, delta: %Ld < 0 ?\n", delta);
1388 sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match);
1389 sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
1391 curr_thread[this_cpu] = sched_in;
1396 if (!sched_in->shortname[0]) {
1397 sched_in->shortname[0] = next_shortname1;
1398 sched_in->shortname[1] = next_shortname2;
1400 if (next_shortname1 < 'Z') {
1403 next_shortname1='A';
1404 if (next_shortname2 < '9') {
1407 next_shortname2='0';
1413 for (cpu = 0; cpu <= max_cpu; cpu++) {
1414 if (cpu != this_cpu)
1419 if (curr_thread[cpu]) {
1420 if (curr_thread[cpu]->pid)
1421 printf("%2s ", curr_thread[cpu]->shortname);
1428 printf(" %12.6f secs ", (double)timestamp/1e9);
1429 if (new_shortname) {
1430 printf("%s => %s:%d\n",
1431 sched_in->shortname, sched_in->comm, sched_in->pid);
1439 process_sched_switch_event(struct raw_event_sample *raw,
1440 struct event *event,
1442 u64 timestamp __used,
1443 struct thread *thread __used)
1445 struct trace_switch_event switch_event;
1447 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1449 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1450 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1451 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1452 FILL_FIELD(switch_event, prev_state, event, raw->data);
1453 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1454 FILL_FIELD(switch_event, next_pid, event, raw->data);
1455 FILL_FIELD(switch_event, next_prio, event, raw->data);
1457 if (curr_pid[this_cpu] != (u32)-1) {
1459 * Are we trying to switch away a PID that is
1462 if (curr_pid[this_cpu] != switch_event.prev_pid)
1463 nr_context_switch_bugs++;
1465 if (trace_handler->switch_event)
1466 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
1468 curr_pid[this_cpu] = switch_event.next_pid;
1472 process_sched_runtime_event(struct raw_event_sample *raw,
1473 struct event *event,
1475 u64 timestamp __used,
1476 struct thread *thread __used)
1478 struct trace_runtime_event runtime_event;
1480 FILL_ARRAY(runtime_event, comm, event, raw->data);
1481 FILL_FIELD(runtime_event, pid, event, raw->data);
1482 FILL_FIELD(runtime_event, runtime, event, raw->data);
1483 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1485 if (trace_handler->runtime_event)
1486 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
1490 process_sched_fork_event(struct raw_event_sample *raw,
1491 struct event *event,
1493 u64 timestamp __used,
1494 struct thread *thread __used)
1496 struct trace_fork_event fork_event;
1498 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1500 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1501 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1502 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1503 FILL_FIELD(fork_event, child_pid, event, raw->data);
1505 if (trace_handler->fork_event)
1506 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
1510 process_sched_exit_event(struct event *event,
1512 u64 timestamp __used,
1513 struct thread *thread __used)
1516 printf("sched_exit event %p\n", event);
1520 process_raw_event(event_t *raw_event __used, void *more_data,
1521 int cpu, u64 timestamp, struct thread *thread)
1523 struct raw_event_sample *raw = more_data;
1524 struct event *event;
1527 type = trace_parse_common_type(raw->data);
1528 event = trace_find_event(type);
1530 if (!strcmp(event->name, "sched_switch"))
1531 process_sched_switch_event(raw, event, cpu, timestamp, thread);
1532 if (!strcmp(event->name, "sched_stat_runtime"))
1533 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
1534 if (!strcmp(event->name, "sched_wakeup"))
1535 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1536 if (!strcmp(event->name, "sched_wakeup_new"))
1537 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1538 if (!strcmp(event->name, "sched_process_fork"))
1539 process_sched_fork_event(raw, event, cpu, timestamp, thread);
1540 if (!strcmp(event->name, "sched_process_exit"))
1541 process_sched_exit_event(event, cpu, timestamp, thread);
1545 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1549 struct dso *dso = NULL;
1550 struct thread *thread;
1551 u64 ip = event->ip.ip;
1555 void *more_data = event->ip.__more_data;
1558 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1560 if (sample_type & PERF_SAMPLE_TIME) {
1561 timestamp = *(u64 *)more_data;
1562 more_data += sizeof(u64);
1565 if (sample_type & PERF_SAMPLE_CPU) {
1566 cpu = *(u32 *)more_data;
1567 more_data += sizeof(u32);
1568 more_data += sizeof(u32); /* reserved */
1571 if (sample_type & PERF_SAMPLE_PERIOD) {
1572 period = *(u64 *)more_data;
1573 more_data += sizeof(u64);
1576 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1577 (void *)(offset + head),
1578 (void *)(long)(event->header.size),
1580 event->ip.pid, event->ip.tid,
1584 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1586 if (thread == NULL) {
1587 eprintf("problem processing %d event, skipping it.\n",
1588 event->header.type);
1592 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1594 if (cpumode == PERF_RECORD_MISC_KERNEL) {
1600 dump_printf(" ...... dso: %s\n", dso->name);
1602 } else if (cpumode == PERF_RECORD_MISC_USER) {
1611 dso = hypervisor_dso;
1613 dump_printf(" ...... dso: [hypervisor]\n");
1616 if (sample_type & PERF_SAMPLE_RAW)
1617 process_raw_event(event, more_data, cpu, timestamp, thread);
1623 process_event(event_t *event, unsigned long offset, unsigned long head)
1628 switch (event->header.type) {
1629 case PERF_RECORD_MMAP:
1631 case PERF_RECORD_LOST:
1633 nr_lost_events += event->lost.lost;
1636 case PERF_RECORD_COMM:
1637 return process_comm_event(event, offset, head);
1639 case PERF_RECORD_EXIT ... PERF_RECORD_READ:
1642 case PERF_RECORD_SAMPLE:
1643 return process_sample_event(event, offset, head);
1645 case PERF_RECORD_MAX:
1653 static int read_events(void)
1655 int ret, rc = EXIT_FAILURE;
1656 unsigned long offset = 0;
1657 unsigned long head = 0;
1658 struct stat perf_stat;
1664 register_idle_thread(&threads, &last_match);
1666 input = open(input_name, O_RDONLY);
1668 perror("failed to open file");
1672 ret = fstat(input, &perf_stat);
1674 perror("failed to stat file");
1678 if (!perf_stat.st_size) {
1679 fprintf(stderr, "zero-sized file, nothing to do!\n");
1682 header = perf_header__read(input);
1683 head = header->data_offset;
1684 sample_type = perf_header__sample_type(header);
1686 if (!(sample_type & PERF_SAMPLE_RAW))
1687 die("No trace sample to read. Did you call perf record "
1690 if (load_kernel() < 0) {
1691 perror("failed to load kernel symbols");
1692 return EXIT_FAILURE;
1696 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1697 MAP_SHARED, input, offset);
1698 if (buf == MAP_FAILED) {
1699 perror("failed to mmap file");
1704 event = (event_t *)(buf + head);
1706 size = event->header.size;
1710 if (head + event->header.size >= page_size * mmap_window) {
1711 unsigned long shift = page_size * (head / page_size);
1714 res = munmap(buf, page_size * mmap_window);
1722 size = event->header.size;
1725 if (!size || process_event(event, offset, head) < 0) {
1728 * assume we lost track of the stream, check alignment, and
1729 * increment a single u64 in the hope to catch on again 'soon'.
1732 if (unlikely(head & 7))
1740 if (offset + head < (unsigned long)perf_stat.st_size)
1749 static void print_bad_events(void)
1751 if (nr_unordered_timestamps && nr_timestamps) {
1752 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1753 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1754 nr_unordered_timestamps, nr_timestamps);
1756 if (nr_lost_events && nr_events) {
1757 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1758 (double)nr_lost_events/(double)nr_events*100.0,
1759 nr_lost_events, nr_events, nr_lost_chunks);
1761 if (nr_state_machine_bugs && nr_timestamps) {
1762 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1763 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1764 nr_state_machine_bugs, nr_timestamps);
1766 printf(" (due to lost events?)");
1769 if (nr_context_switch_bugs && nr_timestamps) {
1770 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1771 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1772 nr_context_switch_bugs, nr_timestamps);
1774 printf(" (due to lost events?)");
1779 static void __cmd_lat(void)
1781 struct rb_node *next;
1787 printf("\n -----------------------------------------------------------------------------------------\n");
1788 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1789 printf(" -----------------------------------------------------------------------------------------\n");
1791 next = rb_first(&sorted_atom_root);
1794 struct work_atoms *work_list;
1796 work_list = rb_entry(next, struct work_atoms, node);
1797 output_lat_thread(work_list);
1798 next = rb_next(next);
1801 printf(" -----------------------------------------------------------------------------------------\n");
1802 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1803 (double)all_runtime/1e6, all_count);
1805 printf(" ---------------------------------------------------\n");
1812 static struct trace_sched_handler map_ops = {
1813 .wakeup_event = NULL,
1814 .switch_event = map_switch_event,
1815 .runtime_event = NULL,
1819 static void __cmd_map(void)
1821 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1828 static void __cmd_replay(void)
1832 calibrate_run_measurement_overhead();
1833 calibrate_sleep_measurement_overhead();
1835 test_calibrations();
1839 printf("nr_run_events: %ld\n", nr_run_events);
1840 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1841 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1843 if (targetless_wakeups)
1844 printf("target-less wakeups: %ld\n", targetless_wakeups);
1845 if (multitarget_wakeups)
1846 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1847 if (nr_run_events_optimized)
1848 printf("run atoms optimized: %ld\n",
1849 nr_run_events_optimized);
1851 print_task_traces();
1852 add_cross_task_wakeups();
1855 printf("------------------------------------------------------------\n");
1856 for (i = 0; i < replay_repeat; i++)
1861 static const char * const sched_usage[] = {
1862 "perf sched [<options>] {record|latency|map|replay|trace}",
1866 static const struct option sched_options[] = {
1867 OPT_STRING('i', "input", &input_name, "file",
1869 OPT_BOOLEAN('v', "verbose", &verbose,
1870 "be more verbose (show symbol address, etc)"),
1871 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1872 "dump raw trace in ASCII"),
1876 static const char * const latency_usage[] = {
1877 "perf sched latency [<options>]",
1881 static const struct option latency_options[] = {
1882 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1883 "sort by key(s): runtime, switch, avg, max"),
1884 OPT_BOOLEAN('v', "verbose", &verbose,
1885 "be more verbose (show symbol address, etc)"),
1886 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1887 "dump raw trace in ASCII"),
1891 static const char * const replay_usage[] = {
1892 "perf sched replay [<options>]",
1896 static const struct option replay_options[] = {
1897 OPT_INTEGER('r', "repeat", &replay_repeat,
1898 "repeat the workload replay N times (-1: infinite)"),
1899 OPT_BOOLEAN('v', "verbose", &verbose,
1900 "be more verbose (show symbol address, etc)"),
1901 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1902 "dump raw trace in ASCII"),
1906 static void setup_sorting(void)
1908 char *tmp, *tok, *str = strdup(sort_order);
1910 for (tok = strtok_r(str, ", ", &tmp);
1911 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1912 if (sort_dimension__add(tok, &sort_list) < 0) {
1913 error("Unknown --sort key: `%s'", tok);
1914 usage_with_options(latency_usage, latency_options);
1920 sort_dimension__add((char *)"pid", &cmp_pid);
1923 static const char *record_args[] = {
1931 "-e", "sched:sched_switch:r",
1932 "-e", "sched:sched_stat_wait:r",
1933 "-e", "sched:sched_stat_sleep:r",
1934 "-e", "sched:sched_stat_iowait:r",
1935 "-e", "sched:sched_stat_runtime:r",
1936 "-e", "sched:sched_process_exit:r",
1937 "-e", "sched:sched_process_fork:r",
1938 "-e", "sched:sched_wakeup:r",
1939 "-e", "sched:sched_migrate_task:r",
1942 static int __cmd_record(int argc, const char **argv)
1944 unsigned int rec_argc, i, j;
1945 const char **rec_argv;
1947 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1948 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1950 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1951 rec_argv[i] = strdup(record_args[i]);
1953 for (j = 1; j < (unsigned int)argc; j++, i++)
1954 rec_argv[i] = argv[j];
1956 BUG_ON(i != rec_argc);
1958 return cmd_record(i, rec_argv, NULL);
1961 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1964 page_size = getpagesize();
1966 argc = parse_options(argc, argv, sched_options, sched_usage,
1967 PARSE_OPT_STOP_AT_NON_OPTION);
1969 usage_with_options(sched_usage, sched_options);
1971 if (!strncmp(argv[0], "rec", 3)) {
1972 return __cmd_record(argc, argv);
1973 } else if (!strncmp(argv[0], "lat", 3)) {
1974 trace_handler = &lat_ops;
1976 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1978 usage_with_options(latency_usage, latency_options);
1982 } else if (!strcmp(argv[0], "map")) {
1983 trace_handler = &map_ops;
1986 } else if (!strncmp(argv[0], "rep", 3)) {
1987 trace_handler = &replay_ops;
1989 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1991 usage_with_options(replay_usage, replay_options);
1994 } else if (!strcmp(argv[0], "trace")) {
1996 * Aliased to 'perf trace' for now:
1998 return cmd_trace(argc, argv, prefix);
2000 usage_with_options(sched_usage, sched_options);