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 unordered_timestamps;
122 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
132 struct list_head list;
133 enum thread_state state;
141 struct list_head work_list;
142 struct thread *thread;
150 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
152 static struct rb_root atom_root, sorted_atom_root;
154 static u64 all_runtime;
155 static u64 all_count;
157 static int read_events(void);
160 static u64 get_nsecs(void)
164 clock_gettime(CLOCK_MONOTONIC, &ts);
166 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
169 static void burn_nsecs(u64 nsecs)
171 u64 T0 = get_nsecs(), T1;
175 } while (T1 + run_measurement_overhead < T0 + nsecs);
178 static void sleep_nsecs(u64 nsecs)
182 ts.tv_nsec = nsecs % 999999999;
183 ts.tv_sec = nsecs / 999999999;
185 nanosleep(&ts, NULL);
188 static void calibrate_run_measurement_overhead(void)
190 u64 T0, T1, delta, min_delta = 1000000000ULL;
193 for (i = 0; i < 10; i++) {
198 min_delta = min(min_delta, delta);
200 run_measurement_overhead = min_delta;
202 printf("run measurement overhead: %Ld nsecs\n", min_delta);
205 static void calibrate_sleep_measurement_overhead(void)
207 u64 T0, T1, delta, min_delta = 1000000000ULL;
210 for (i = 0; i < 10; i++) {
215 min_delta = min(min_delta, delta);
218 sleep_measurement_overhead = min_delta;
220 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
223 static struct sched_atom *
224 get_new_event(struct task_desc *task, u64 timestamp)
226 struct sched_atom *event = calloc(1, sizeof(*event));
227 unsigned long idx = task->nr_events;
230 event->timestamp = timestamp;
234 size = sizeof(struct sched_atom *) * task->nr_events;
235 task->atoms = realloc(task->atoms, size);
236 BUG_ON(!task->atoms);
238 task->atoms[idx] = event;
243 static struct sched_atom *last_event(struct task_desc *task)
245 if (!task->nr_events)
248 return task->atoms[task->nr_events - 1];
252 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
254 struct sched_atom *event, *curr_event = last_event(task);
257 * optimize an existing RUN event by merging this one
260 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
261 nr_run_events_optimized++;
262 curr_event->duration += duration;
266 event = get_new_event(task, timestamp);
268 event->type = SCHED_EVENT_RUN;
269 event->duration = duration;
275 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
276 struct task_desc *wakee)
278 struct sched_atom *event, *wakee_event;
280 event = get_new_event(task, timestamp);
281 event->type = SCHED_EVENT_WAKEUP;
282 event->wakee = wakee;
284 wakee_event = last_event(wakee);
285 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
286 targetless_wakeups++;
289 if (wakee_event->wait_sem) {
290 multitarget_wakeups++;
294 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
295 sem_init(wakee_event->wait_sem, 0, 0);
296 wakee_event->specific_wait = 1;
297 event->wait_sem = wakee_event->wait_sem;
303 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
304 u64 task_state __used)
306 struct sched_atom *event = get_new_event(task, timestamp);
308 event->type = SCHED_EVENT_SLEEP;
313 static struct task_desc *register_pid(unsigned long pid, const char *comm)
315 struct task_desc *task;
317 BUG_ON(pid >= MAX_PID);
319 task = pid_to_task[pid];
324 task = calloc(1, sizeof(*task));
327 strcpy(task->comm, comm);
329 * every task starts in sleeping state - this gets ignored
330 * if there's no wakeup pointing to this sleep state:
332 add_sched_event_sleep(task, 0, 0);
334 pid_to_task[pid] = task;
336 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
338 tasks[task->nr] = task;
341 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
347 static void print_task_traces(void)
349 struct task_desc *task;
352 for (i = 0; i < nr_tasks; i++) {
354 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
355 task->nr, task->comm, task->pid, task->nr_events);
359 static void add_cross_task_wakeups(void)
361 struct task_desc *task1, *task2;
364 for (i = 0; i < nr_tasks; i++) {
370 add_sched_event_wakeup(task1, 0, task2);
375 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
382 delta = start_time + atom->timestamp - now;
384 switch (atom->type) {
385 case SCHED_EVENT_RUN:
386 burn_nsecs(atom->duration);
388 case SCHED_EVENT_SLEEP:
390 ret = sem_wait(atom->wait_sem);
393 case SCHED_EVENT_WAKEUP:
395 ret = sem_post(atom->wait_sem);
403 static u64 get_cpu_usage_nsec_parent(void)
409 err = getrusage(RUSAGE_SELF, &ru);
412 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
413 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
418 static u64 get_cpu_usage_nsec_self(void)
420 char filename [] = "/proc/1234567890/sched";
421 unsigned long msecs, nsecs;
429 sprintf(filename, "/proc/%d/sched", getpid());
430 file = fopen(filename, "r");
433 while ((chars = getline(&line, &len, file)) != -1) {
434 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
437 total = msecs*1e6 + nsecs;
448 static void *thread_func(void *ctx)
450 struct task_desc *this_task = ctx;
451 u64 cpu_usage_0, cpu_usage_1;
452 unsigned long i, ret;
455 sprintf(comm2, ":%s", this_task->comm);
456 prctl(PR_SET_NAME, comm2);
459 ret = sem_post(&this_task->ready_for_work);
461 ret = pthread_mutex_lock(&start_work_mutex);
463 ret = pthread_mutex_unlock(&start_work_mutex);
466 cpu_usage_0 = get_cpu_usage_nsec_self();
468 for (i = 0; i < this_task->nr_events; i++) {
469 this_task->curr_event = i;
470 process_sched_event(this_task, this_task->atoms[i]);
473 cpu_usage_1 = get_cpu_usage_nsec_self();
474 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
476 ret = sem_post(&this_task->work_done_sem);
479 ret = pthread_mutex_lock(&work_done_wait_mutex);
481 ret = pthread_mutex_unlock(&work_done_wait_mutex);
487 static void create_tasks(void)
489 struct task_desc *task;
494 err = pthread_attr_init(&attr);
496 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
498 err = pthread_mutex_lock(&start_work_mutex);
500 err = pthread_mutex_lock(&work_done_wait_mutex);
502 for (i = 0; i < nr_tasks; i++) {
504 sem_init(&task->sleep_sem, 0, 0);
505 sem_init(&task->ready_for_work, 0, 0);
506 sem_init(&task->work_done_sem, 0, 0);
507 task->curr_event = 0;
508 err = pthread_create(&task->thread, &attr, thread_func, task);
513 static void wait_for_tasks(void)
515 u64 cpu_usage_0, cpu_usage_1;
516 struct task_desc *task;
517 unsigned long i, ret;
519 start_time = get_nsecs();
521 pthread_mutex_unlock(&work_done_wait_mutex);
523 for (i = 0; i < nr_tasks; i++) {
525 ret = sem_wait(&task->ready_for_work);
527 sem_init(&task->ready_for_work, 0, 0);
529 ret = pthread_mutex_lock(&work_done_wait_mutex);
532 cpu_usage_0 = get_cpu_usage_nsec_parent();
534 pthread_mutex_unlock(&start_work_mutex);
536 for (i = 0; i < nr_tasks; i++) {
538 ret = sem_wait(&task->work_done_sem);
540 sem_init(&task->work_done_sem, 0, 0);
541 cpu_usage += task->cpu_usage;
545 cpu_usage_1 = get_cpu_usage_nsec_parent();
546 if (!runavg_cpu_usage)
547 runavg_cpu_usage = cpu_usage;
548 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
550 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
551 if (!runavg_parent_cpu_usage)
552 runavg_parent_cpu_usage = parent_cpu_usage;
553 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
554 parent_cpu_usage)/10;
556 ret = pthread_mutex_lock(&start_work_mutex);
559 for (i = 0; i < nr_tasks; i++) {
561 sem_init(&task->sleep_sem, 0, 0);
562 task->curr_event = 0;
566 static void run_one_test(void)
568 u64 T0, T1, delta, avg_delta, fluct, std_dev;
575 sum_runtime += delta;
578 avg_delta = sum_runtime / nr_runs;
579 if (delta < avg_delta)
580 fluct = avg_delta - delta;
582 fluct = delta - avg_delta;
584 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
587 run_avg = (run_avg*9 + delta)/10;
589 printf("#%-3ld: %0.3f, ",
590 nr_runs, (double)delta/1000000.0);
592 printf("ravg: %0.2f, ",
593 (double)run_avg/1e6);
595 printf("cpu: %0.2f / %0.2f",
596 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
600 * rusage statistics done by the parent, these are less
601 * accurate than the sum_exec_runtime based statistics:
603 printf(" [%0.2f / %0.2f]",
604 (double)parent_cpu_usage/1e6,
605 (double)runavg_parent_cpu_usage/1e6);
610 if (nr_sleep_corrections)
611 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
612 nr_sleep_corrections = 0;
615 static void test_calibrations(void)
623 printf("the run test took %Ld nsecs\n", T1-T0);
629 printf("the sleep test took %Ld nsecs\n", T1-T0);
632 static void __cmd_replay(void)
636 calibrate_run_measurement_overhead();
637 calibrate_sleep_measurement_overhead();
643 printf("nr_run_events: %ld\n", nr_run_events);
644 printf("nr_sleep_events: %ld\n", nr_sleep_events);
645 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
647 if (targetless_wakeups)
648 printf("target-less wakeups: %ld\n", targetless_wakeups);
649 if (multitarget_wakeups)
650 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
651 if (nr_run_events_optimized)
652 printf("run atoms optimized: %ld\n",
653 nr_run_events_optimized);
656 add_cross_task_wakeups();
659 printf("------------------------------------------------------------\n");
660 for (i = 0; i < replay_repeat; i++)
665 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
667 struct thread *thread;
669 thread = threads__findnew(event->comm.pid, &threads, &last_match);
671 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
672 (void *)(offset + head),
673 (void *)(long)(event->header.size),
674 event->comm.comm, event->comm.pid);
676 if (thread == NULL ||
677 thread__set_comm(thread, event->comm.comm)) {
678 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
687 struct raw_event_sample {
692 #define FILL_FIELD(ptr, field, event, data) \
693 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
695 #define FILL_ARRAY(ptr, array, event, data) \
697 void *__array = raw_field_ptr(event, #array, data); \
698 memcpy(ptr.array, __array, sizeof(ptr.array)); \
701 #define FILL_COMMON_FIELDS(ptr, event, data) \
703 FILL_FIELD(ptr, common_type, event, data); \
704 FILL_FIELD(ptr, common_flags, event, data); \
705 FILL_FIELD(ptr, common_preempt_count, event, data); \
706 FILL_FIELD(ptr, common_pid, event, data); \
707 FILL_FIELD(ptr, common_tgid, event, data); \
712 struct trace_switch_event {
717 u8 common_preempt_count;
730 struct trace_runtime_event {
735 u8 common_preempt_count;
745 struct trace_wakeup_event {
750 u8 common_preempt_count;
762 struct trace_fork_event {
767 u8 common_preempt_count;
771 char parent_comm[16];
777 struct trace_sched_handler {
778 void (*switch_event)(struct trace_switch_event *,
782 struct thread *thread);
784 void (*runtime_event)(struct trace_runtime_event *,
788 struct thread *thread);
790 void (*wakeup_event)(struct trace_wakeup_event *,
794 struct thread *thread);
796 void (*fork_event)(struct trace_fork_event *,
800 struct thread *thread);
805 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
808 u64 timestamp __used,
809 struct thread *thread __used)
811 struct task_desc *waker, *wakee;
814 printf("sched_wakeup event %p\n", event);
816 printf(" ... pid %d woke up %s/%d\n",
817 wakeup_event->common_pid,
822 waker = register_pid(wakeup_event->common_pid, "<unknown>");
823 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
825 add_sched_event_wakeup(waker, timestamp, wakee);
828 static u64 cpu_last_switched[MAX_CPUS];
831 replay_switch_event(struct trace_switch_event *switch_event,
835 struct thread *thread __used)
837 struct task_desc *prev, *next;
842 printf("sched_switch event %p\n", event);
844 if (cpu >= MAX_CPUS || cpu < 0)
847 timestamp0 = cpu_last_switched[cpu];
849 delta = timestamp - timestamp0;
854 die("hm, delta: %Ld < 0 ?\n", delta);
857 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
858 switch_event->prev_comm, switch_event->prev_pid,
859 switch_event->next_comm, switch_event->next_pid,
863 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
864 next = register_pid(switch_event->next_pid, switch_event->next_comm);
866 cpu_last_switched[cpu] = timestamp;
868 add_sched_event_run(prev, timestamp, delta);
869 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
874 replay_fork_event(struct trace_fork_event *fork_event,
877 u64 timestamp __used,
878 struct thread *thread __used)
881 printf("sched_fork event %p\n", event);
882 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
883 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
885 register_pid(fork_event->parent_pid, fork_event->parent_comm);
886 register_pid(fork_event->child_pid, fork_event->child_comm);
889 static struct trace_sched_handler replay_ops = {
890 .wakeup_event = replay_wakeup_event,
891 .switch_event = replay_switch_event,
892 .fork_event = replay_fork_event,
895 struct sort_dimension {
898 struct list_head list;
901 static LIST_HEAD(cmp_pid);
904 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
906 struct sort_dimension *sort;
909 BUG_ON(list_empty(list));
911 list_for_each_entry(sort, list, list) {
912 ret = sort->cmp(l, r);
920 static struct work_atoms *
921 thread_atoms_search(struct rb_root *root, struct thread *thread,
922 struct list_head *sort_list)
924 struct rb_node *node = root->rb_node;
925 struct work_atoms key = { .thread = thread };
928 struct work_atoms *atoms;
931 atoms = container_of(node, struct work_atoms, node);
933 cmp = thread_lat_cmp(sort_list, &key, atoms);
935 node = node->rb_left;
937 node = node->rb_right;
939 BUG_ON(thread != atoms->thread);
947 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
948 struct list_head *sort_list)
950 struct rb_node **new = &(root->rb_node), *parent = NULL;
953 struct work_atoms *this;
956 this = container_of(*new, struct work_atoms, node);
959 cmp = thread_lat_cmp(sort_list, data, this);
962 new = &((*new)->rb_left);
964 new = &((*new)->rb_right);
967 rb_link_node(&data->node, parent, new);
968 rb_insert_color(&data->node, root);
971 static void thread_atoms_insert(struct thread *thread)
973 struct work_atoms *atoms;
975 atoms = calloc(sizeof(*atoms), 1);
979 atoms->thread = thread;
980 INIT_LIST_HEAD(&atoms->work_list);
981 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
985 latency_fork_event(struct trace_fork_event *fork_event __used,
986 struct event *event __used,
988 u64 timestamp __used,
989 struct thread *thread __used)
991 /* should insert the newcomer */
995 static char sched_out_state(struct trace_switch_event *switch_event)
997 const char *str = TASK_STATE_TO_CHAR_STR;
999 return str[switch_event->prev_state];
1003 add_sched_out_event(struct work_atoms *atoms,
1007 struct work_atom *atom;
1009 atom = calloc(sizeof(*atom), 1);
1013 atom->sched_out_time = timestamp;
1015 if (run_state == 'R') {
1016 atom->state = THREAD_WAIT_CPU;
1017 atom->wake_up_time = atom->sched_out_time;
1020 list_add_tail(&atom->list, &atoms->work_list);
1024 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
1026 struct work_atom *atom;
1028 BUG_ON(list_empty(&atoms->work_list));
1030 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1032 atom->runtime += delta;
1033 atoms->total_runtime += delta;
1037 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1039 struct work_atom *atom;
1042 if (list_empty(&atoms->work_list))
1045 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1047 if (atom->state != THREAD_WAIT_CPU)
1050 if (timestamp < atom->wake_up_time) {
1051 atom->state = THREAD_IGNORE;
1055 atom->state = THREAD_SCHED_IN;
1056 atom->sched_in_time = timestamp;
1058 delta = atom->sched_in_time - atom->wake_up_time;
1059 atoms->total_lat += delta;
1060 if (delta > atoms->max_lat)
1061 atoms->max_lat = delta;
1066 latency_switch_event(struct trace_switch_event *switch_event,
1067 struct event *event __used,
1070 struct thread *thread __used)
1072 struct work_atoms *out_events, *in_events;
1073 struct thread *sched_out, *sched_in;
1077 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1079 timestamp0 = cpu_last_switched[cpu];
1080 cpu_last_switched[cpu] = timestamp;
1082 delta = timestamp - timestamp0;
1087 die("hm, delta: %Ld < 0 ?\n", delta);
1090 sched_out = threads__findnew(switch_event->prev_pid, &threads, &last_match);
1091 sched_in = threads__findnew(switch_event->next_pid, &threads, &last_match);
1093 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1095 thread_atoms_insert(sched_out);
1096 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1098 die("out-event: Internal tree error");
1100 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1102 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1104 thread_atoms_insert(sched_in);
1105 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1107 die("in-event: Internal tree error");
1109 * Take came in we have not heard about yet,
1110 * add in an initial atom in runnable state:
1112 add_sched_out_event(in_events, 'R', timestamp);
1114 add_sched_in_event(in_events, timestamp);
1118 latency_runtime_event(struct trace_runtime_event *runtime_event,
1119 struct event *event __used,
1122 struct thread *this_thread __used)
1124 struct work_atoms *atoms;
1125 struct thread *thread;
1127 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1129 thread = threads__findnew(runtime_event->pid, &threads, &last_match);
1130 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1132 thread_atoms_insert(thread);
1133 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1135 die("in-event: Internal tree error");
1136 add_sched_out_event(atoms, 'R', timestamp);
1139 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1143 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1144 struct event *__event __used,
1147 struct thread *thread __used)
1149 struct work_atoms *atoms;
1150 struct work_atom *atom;
1151 struct thread *wakee;
1153 /* Note for later, it may be interesting to observe the failing cases */
1154 if (!wakeup_event->success)
1157 wakee = threads__findnew(wakeup_event->pid, &threads, &last_match);
1158 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1160 thread_atoms_insert(wakee);
1161 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1163 die("wakeup-event: Internal tree error");
1164 add_sched_out_event(atoms, 'S', timestamp);
1167 BUG_ON(list_empty(&atoms->work_list));
1169 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1171 if (atom->state != THREAD_SLEEPING) {
1177 if (atom->sched_out_time > timestamp) {
1178 unordered_timestamps++;
1182 atom->state = THREAD_WAIT_CPU;
1183 atom->wake_up_time = timestamp;
1186 static struct trace_sched_handler lat_ops = {
1187 .wakeup_event = latency_wakeup_event,
1188 .switch_event = latency_switch_event,
1189 .runtime_event = latency_runtime_event,
1190 .fork_event = latency_fork_event,
1193 static void output_lat_thread(struct work_atoms *work_list)
1199 if (!work_list->nb_atoms)
1202 * Ignore idle threads:
1204 if (!work_list->thread->pid)
1207 all_runtime += work_list->total_runtime;
1208 all_count += work_list->nb_atoms;
1210 ret = printf(" %s-%d ", work_list->thread->comm, work_list->thread->pid);
1212 for (i = 0; i < 24 - ret; i++)
1215 avg = work_list->total_lat / work_list->nb_atoms;
1217 printf("|%9.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
1218 (double)work_list->total_runtime / 1e6,
1219 work_list->nb_atoms, (double)avg / 1e6,
1220 (double)work_list->max_lat / 1e6);
1223 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1225 if (l->thread->pid < r->thread->pid)
1227 if (l->thread->pid > r->thread->pid)
1233 static struct sort_dimension pid_sort_dimension = {
1238 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1248 avgl = l->total_lat / l->nb_atoms;
1249 avgr = r->total_lat / r->nb_atoms;
1259 static struct sort_dimension avg_sort_dimension = {
1264 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1266 if (l->max_lat < r->max_lat)
1268 if (l->max_lat > r->max_lat)
1274 static struct sort_dimension max_sort_dimension = {
1279 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1281 if (l->nb_atoms < r->nb_atoms)
1283 if (l->nb_atoms > r->nb_atoms)
1289 static struct sort_dimension switch_sort_dimension = {
1294 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1296 if (l->total_runtime < r->total_runtime)
1298 if (l->total_runtime > r->total_runtime)
1304 static struct sort_dimension runtime_sort_dimension = {
1309 static struct sort_dimension *available_sorts[] = {
1310 &pid_sort_dimension,
1311 &avg_sort_dimension,
1312 &max_sort_dimension,
1313 &switch_sort_dimension,
1314 &runtime_sort_dimension,
1317 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1319 static LIST_HEAD(sort_list);
1321 static int sort_dimension__add(char *tok, struct list_head *list)
1325 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1326 if (!strcmp(available_sorts[i]->name, tok)) {
1327 list_add_tail(&available_sorts[i]->list, list);
1336 static void setup_sorting(void);
1338 static void sort_lat(void)
1340 struct rb_node *node;
1343 struct work_atoms *data;
1344 node = rb_first(&atom_root);
1348 rb_erase(node, &atom_root);
1349 data = rb_entry(node, struct work_atoms, node);
1350 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1354 static void __cmd_lat(void)
1356 struct rb_node *next;
1362 printf("\n ---------------------------------------------------------------------------------------\n");
1363 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1364 printf(" ---------------------------------------------------------------------------------------\n");
1366 next = rb_first(&sorted_atom_root);
1369 struct work_atoms *work_list;
1371 work_list = rb_entry(next, struct work_atoms, node);
1372 output_lat_thread(work_list);
1373 next = rb_next(next);
1376 printf(" ---------------------------------------------------------------------------------------\n");
1377 printf(" TOTAL: |%9.3f ms |%9Ld |",
1378 (double)all_runtime/1e6, all_count);
1380 if (unordered_timestamps && nr_timestamps) {
1381 printf(" INFO: %.2f%% unordered events.\n",
1382 (double)unordered_timestamps/(double)nr_timestamps*100.0);
1387 printf(" -------------------------------------------------\n\n");
1390 static struct trace_sched_handler *trace_handler;
1393 process_sched_wakeup_event(struct raw_event_sample *raw,
1394 struct event *event,
1396 u64 timestamp __used,
1397 struct thread *thread __used)
1399 struct trace_wakeup_event wakeup_event;
1401 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1403 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1404 FILL_FIELD(wakeup_event, pid, event, raw->data);
1405 FILL_FIELD(wakeup_event, prio, event, raw->data);
1406 FILL_FIELD(wakeup_event, success, event, raw->data);
1407 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1409 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
1413 process_sched_switch_event(struct raw_event_sample *raw,
1414 struct event *event,
1416 u64 timestamp __used,
1417 struct thread *thread __used)
1419 struct trace_switch_event switch_event;
1421 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1423 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1424 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1425 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1426 FILL_FIELD(switch_event, prev_state, event, raw->data);
1427 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1428 FILL_FIELD(switch_event, next_pid, event, raw->data);
1429 FILL_FIELD(switch_event, next_prio, event, raw->data);
1431 trace_handler->switch_event(&switch_event, event, cpu, timestamp, thread);
1435 process_sched_runtime_event(struct raw_event_sample *raw,
1436 struct event *event,
1438 u64 timestamp __used,
1439 struct thread *thread __used)
1441 struct trace_runtime_event runtime_event;
1443 FILL_ARRAY(runtime_event, comm, event, raw->data);
1444 FILL_FIELD(runtime_event, pid, event, raw->data);
1445 FILL_FIELD(runtime_event, runtime, event, raw->data);
1446 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1448 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
1452 process_sched_fork_event(struct raw_event_sample *raw,
1453 struct event *event,
1455 u64 timestamp __used,
1456 struct thread *thread __used)
1458 struct trace_fork_event fork_event;
1460 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1462 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1463 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1464 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1465 FILL_FIELD(fork_event, child_pid, event, raw->data);
1467 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
1471 process_sched_exit_event(struct event *event,
1473 u64 timestamp __used,
1474 struct thread *thread __used)
1477 printf("sched_exit event %p\n", event);
1481 process_raw_event(event_t *raw_event __used, void *more_data,
1482 int cpu, u64 timestamp, struct thread *thread)
1484 struct raw_event_sample *raw = more_data;
1485 struct event *event;
1488 type = trace_parse_common_type(raw->data);
1489 event = trace_find_event(type);
1491 if (!strcmp(event->name, "sched_switch"))
1492 process_sched_switch_event(raw, event, cpu, timestamp, thread);
1493 if (!strcmp(event->name, "sched_stat_runtime"))
1494 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
1495 if (!strcmp(event->name, "sched_wakeup"))
1496 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1497 if (!strcmp(event->name, "sched_wakeup_new"))
1498 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1499 if (!strcmp(event->name, "sched_process_fork"))
1500 process_sched_fork_event(raw, event, cpu, timestamp, thread);
1501 if (!strcmp(event->name, "sched_process_exit"))
1502 process_sched_exit_event(event, cpu, timestamp, thread);
1506 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1510 struct dso *dso = NULL;
1511 struct thread *thread;
1512 u64 ip = event->ip.ip;
1516 void *more_data = event->ip.__more_data;
1519 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1521 if (sample_type & PERF_SAMPLE_TIME) {
1522 timestamp = *(u64 *)more_data;
1523 more_data += sizeof(u64);
1526 if (sample_type & PERF_SAMPLE_CPU) {
1527 cpu = *(u32 *)more_data;
1528 more_data += sizeof(u32);
1529 more_data += sizeof(u32); /* reserved */
1532 if (sample_type & PERF_SAMPLE_PERIOD) {
1533 period = *(u64 *)more_data;
1534 more_data += sizeof(u64);
1537 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1538 (void *)(offset + head),
1539 (void *)(long)(event->header.size),
1541 event->ip.pid, event->ip.tid,
1545 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1547 if (thread == NULL) {
1548 eprintf("problem processing %d event, skipping it.\n",
1549 event->header.type);
1553 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1555 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1561 dump_printf(" ...... dso: %s\n", dso->name);
1563 } else if (cpumode == PERF_EVENT_MISC_USER) {
1572 dso = hypervisor_dso;
1574 dump_printf(" ...... dso: [hypervisor]\n");
1577 if (sample_type & PERF_SAMPLE_RAW)
1578 process_raw_event(event, more_data, cpu, timestamp, thread);
1584 process_event(event_t *event, unsigned long offset, unsigned long head)
1588 switch (event->header.type) {
1589 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
1592 case PERF_EVENT_COMM:
1593 return process_comm_event(event, offset, head);
1595 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
1598 case PERF_EVENT_SAMPLE:
1599 return process_sample_event(event, offset, head);
1601 case PERF_EVENT_MAX:
1609 static int read_events(void)
1611 int ret, rc = EXIT_FAILURE;
1612 unsigned long offset = 0;
1613 unsigned long head = 0;
1614 struct stat perf_stat;
1620 register_idle_thread(&threads, &last_match);
1622 input = open(input_name, O_RDONLY);
1624 perror("failed to open file");
1628 ret = fstat(input, &perf_stat);
1630 perror("failed to stat file");
1634 if (!perf_stat.st_size) {
1635 fprintf(stderr, "zero-sized file, nothing to do!\n");
1638 header = perf_header__read(input);
1639 head = header->data_offset;
1640 sample_type = perf_header__sample_type(header);
1642 if (!(sample_type & PERF_SAMPLE_RAW))
1643 die("No trace sample to read. Did you call perf record "
1646 if (load_kernel() < 0) {
1647 perror("failed to load kernel symbols");
1648 return EXIT_FAILURE;
1652 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1653 MAP_SHARED, input, offset);
1654 if (buf == MAP_FAILED) {
1655 perror("failed to mmap file");
1660 event = (event_t *)(buf + head);
1662 size = event->header.size;
1666 if (head + event->header.size >= page_size * mmap_window) {
1667 unsigned long shift = page_size * (head / page_size);
1670 res = munmap(buf, page_size * mmap_window);
1678 size = event->header.size;
1681 if (!size || process_event(event, offset, head) < 0) {
1684 * assume we lost track of the stream, check alignment, and
1685 * increment a single u64 in the hope to catch on again 'soon'.
1688 if (unlikely(head & 7))
1696 if (offset + head < (unsigned long)perf_stat.st_size)
1705 static const char * const sched_usage[] = {
1706 "perf sched [<options>] {record|latency|replay|trace}",
1710 static const struct option sched_options[] = {
1711 OPT_BOOLEAN('v', "verbose", &verbose,
1712 "be more verbose (show symbol address, etc)"),
1713 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1714 "dump raw trace in ASCII"),
1718 static const char * const latency_usage[] = {
1719 "perf sched latency [<options>]",
1723 static const struct option latency_options[] = {
1724 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1725 "sort by key(s): runtime, switch, avg, max"),
1726 OPT_BOOLEAN('v', "verbose", &verbose,
1727 "be more verbose (show symbol address, etc)"),
1728 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1729 "dump raw trace in ASCII"),
1733 static const char * const replay_usage[] = {
1734 "perf sched replay [<options>]",
1738 static const struct option replay_options[] = {
1739 OPT_INTEGER('r', "repeat", &replay_repeat,
1740 "repeat the workload replay N times (-1: infinite)"),
1741 OPT_BOOLEAN('v', "verbose", &verbose,
1742 "be more verbose (show symbol address, etc)"),
1743 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1744 "dump raw trace in ASCII"),
1748 static void setup_sorting(void)
1750 char *tmp, *tok, *str = strdup(sort_order);
1752 for (tok = strtok_r(str, ", ", &tmp);
1753 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1754 if (sort_dimension__add(tok, &sort_list) < 0) {
1755 error("Unknown --sort key: `%s'", tok);
1756 usage_with_options(latency_usage, latency_options);
1762 sort_dimension__add((char *)"pid", &cmp_pid);
1765 static const char *record_args[] = {
1772 "-e", "sched:sched_switch:r",
1773 "-e", "sched:sched_stat_wait:r",
1774 "-e", "sched:sched_stat_sleep:r",
1775 "-e", "sched:sched_stat_iowait:r",
1776 "-e", "sched:sched_stat_runtime:r",
1777 "-e", "sched:sched_process_exit:r",
1778 "-e", "sched:sched_process_fork:r",
1779 "-e", "sched:sched_wakeup:r",
1780 "-e", "sched:sched_migrate_task:r",
1783 static int __cmd_record(int argc, const char **argv)
1785 unsigned int rec_argc, i, j;
1786 const char **rec_argv;
1788 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1789 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1791 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1792 rec_argv[i] = strdup(record_args[i]);
1794 for (j = 1; j < (unsigned int)argc; j++, i++)
1795 rec_argv[i] = argv[j];
1797 BUG_ON(i != rec_argc);
1799 return cmd_record(i, rec_argv, NULL);
1802 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1805 page_size = getpagesize();
1807 argc = parse_options(argc, argv, sched_options, sched_usage,
1808 PARSE_OPT_STOP_AT_NON_OPTION);
1810 usage_with_options(sched_usage, sched_options);
1812 if (!strncmp(argv[0], "rec", 3)) {
1813 return __cmd_record(argc, argv);
1814 } else if (!strncmp(argv[0], "lat", 3)) {
1815 trace_handler = &lat_ops;
1817 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1819 usage_with_options(latency_usage, latency_options);
1823 } else if (!strncmp(argv[0], "rep", 3)) {
1824 trace_handler = &replay_ops;
1826 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1828 usage_with_options(replay_usage, replay_options);
1831 } else if (!strcmp(argv[0], "trace")) {
1833 * Aliased to 'perf trace' for now:
1835 return cmd_trace(argc, argv, prefix);
1837 usage_with_options(sched_usage, sched_options);