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"
14 #include "util/data_map.h"
16 #include <sys/types.h>
17 #include <sys/prctl.h>
19 #include <semaphore.h>
23 static char const *input_name = "perf.data";
25 static struct perf_header *header;
26 static u64 sample_type;
28 static char default_sort_order[] = "avg, max, switch, runtime";
29 static char *sort_order = default_sort_order;
31 static int profile_cpu = -1;
33 #define PR_SET_NAME 15 /* Set process name */
36 static u64 run_measurement_overhead;
37 static u64 sleep_measurement_overhead;
44 static unsigned long nr_tasks;
53 unsigned long nr_events;
54 unsigned long curr_event;
55 struct sched_atom **atoms;
66 enum sched_event_type {
70 SCHED_EVENT_MIGRATION,
74 enum sched_event_type type;
80 struct task_desc *wakee;
83 static struct task_desc *pid_to_task[MAX_PID];
85 static struct task_desc **tasks;
87 static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
88 static u64 start_time;
90 static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
92 static unsigned long nr_run_events;
93 static unsigned long nr_sleep_events;
94 static unsigned long nr_wakeup_events;
96 static unsigned long nr_sleep_corrections;
97 static unsigned long nr_run_events_optimized;
99 static unsigned long targetless_wakeups;
100 static unsigned long multitarget_wakeups;
102 static u64 cpu_usage;
103 static u64 runavg_cpu_usage;
104 static u64 parent_cpu_usage;
105 static u64 runavg_parent_cpu_usage;
107 static unsigned long nr_runs;
108 static u64 sum_runtime;
109 static u64 sum_fluct;
112 static unsigned long replay_repeat = 10;
113 static unsigned long nr_timestamps;
114 static unsigned long nr_unordered_timestamps;
115 static unsigned long nr_state_machine_bugs;
116 static unsigned long nr_context_switch_bugs;
117 static unsigned long nr_events;
118 static unsigned long nr_lost_chunks;
119 static unsigned long nr_lost_events;
121 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
131 struct list_head list;
132 enum thread_state state;
140 struct list_head work_list;
141 struct thread *thread;
149 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
151 static struct rb_root atom_root, sorted_atom_root;
153 static u64 all_runtime;
154 static u64 all_count;
157 static u64 get_nsecs(void)
161 clock_gettime(CLOCK_MONOTONIC, &ts);
163 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
166 static void burn_nsecs(u64 nsecs)
168 u64 T0 = get_nsecs(), T1;
172 } while (T1 + run_measurement_overhead < T0 + nsecs);
175 static void sleep_nsecs(u64 nsecs)
179 ts.tv_nsec = nsecs % 999999999;
180 ts.tv_sec = nsecs / 999999999;
182 nanosleep(&ts, NULL);
185 static void calibrate_run_measurement_overhead(void)
187 u64 T0, T1, delta, min_delta = 1000000000ULL;
190 for (i = 0; i < 10; i++) {
195 min_delta = min(min_delta, delta);
197 run_measurement_overhead = min_delta;
199 printf("run measurement overhead: %Ld nsecs\n", min_delta);
202 static void calibrate_sleep_measurement_overhead(void)
204 u64 T0, T1, delta, min_delta = 1000000000ULL;
207 for (i = 0; i < 10; i++) {
212 min_delta = min(min_delta, delta);
215 sleep_measurement_overhead = min_delta;
217 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
220 static struct sched_atom *
221 get_new_event(struct task_desc *task, u64 timestamp)
223 struct sched_atom *event = zalloc(sizeof(*event));
224 unsigned long idx = task->nr_events;
227 event->timestamp = timestamp;
231 size = sizeof(struct sched_atom *) * task->nr_events;
232 task->atoms = realloc(task->atoms, size);
233 BUG_ON(!task->atoms);
235 task->atoms[idx] = event;
240 static struct sched_atom *last_event(struct task_desc *task)
242 if (!task->nr_events)
245 return task->atoms[task->nr_events - 1];
249 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
251 struct sched_atom *event, *curr_event = last_event(task);
254 * optimize an existing RUN event by merging this one
257 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
258 nr_run_events_optimized++;
259 curr_event->duration += duration;
263 event = get_new_event(task, timestamp);
265 event->type = SCHED_EVENT_RUN;
266 event->duration = duration;
272 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
273 struct task_desc *wakee)
275 struct sched_atom *event, *wakee_event;
277 event = get_new_event(task, timestamp);
278 event->type = SCHED_EVENT_WAKEUP;
279 event->wakee = wakee;
281 wakee_event = last_event(wakee);
282 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
283 targetless_wakeups++;
286 if (wakee_event->wait_sem) {
287 multitarget_wakeups++;
291 wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem));
292 sem_init(wakee_event->wait_sem, 0, 0);
293 wakee_event->specific_wait = 1;
294 event->wait_sem = wakee_event->wait_sem;
300 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
301 u64 task_state __used)
303 struct sched_atom *event = get_new_event(task, timestamp);
305 event->type = SCHED_EVENT_SLEEP;
310 static struct task_desc *register_pid(unsigned long pid, const char *comm)
312 struct task_desc *task;
314 BUG_ON(pid >= MAX_PID);
316 task = pid_to_task[pid];
321 task = zalloc(sizeof(*task));
324 strcpy(task->comm, comm);
326 * every task starts in sleeping state - this gets ignored
327 * if there's no wakeup pointing to this sleep state:
329 add_sched_event_sleep(task, 0, 0);
331 pid_to_task[pid] = task;
333 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
335 tasks[task->nr] = task;
338 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
344 static void print_task_traces(void)
346 struct task_desc *task;
349 for (i = 0; i < nr_tasks; i++) {
351 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
352 task->nr, task->comm, task->pid, task->nr_events);
356 static void add_cross_task_wakeups(void)
358 struct task_desc *task1, *task2;
361 for (i = 0; i < nr_tasks; i++) {
367 add_sched_event_wakeup(task1, 0, task2);
372 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
379 delta = start_time + atom->timestamp - now;
381 switch (atom->type) {
382 case SCHED_EVENT_RUN:
383 burn_nsecs(atom->duration);
385 case SCHED_EVENT_SLEEP:
387 ret = sem_wait(atom->wait_sem);
390 case SCHED_EVENT_WAKEUP:
392 ret = sem_post(atom->wait_sem);
395 case SCHED_EVENT_MIGRATION:
402 static u64 get_cpu_usage_nsec_parent(void)
408 err = getrusage(RUSAGE_SELF, &ru);
411 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
412 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
417 static u64 get_cpu_usage_nsec_self(void)
419 char filename [] = "/proc/1234567890/sched";
420 unsigned long msecs, nsecs;
428 sprintf(filename, "/proc/%d/sched", getpid());
429 file = fopen(filename, "r");
432 while ((chars = getline(&line, &len, file)) != -1) {
433 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
436 total = msecs*1e6 + nsecs;
447 static void *thread_func(void *ctx)
449 struct task_desc *this_task = ctx;
450 u64 cpu_usage_0, cpu_usage_1;
451 unsigned long i, ret;
454 sprintf(comm2, ":%s", this_task->comm);
455 prctl(PR_SET_NAME, comm2);
458 ret = sem_post(&this_task->ready_for_work);
460 ret = pthread_mutex_lock(&start_work_mutex);
462 ret = pthread_mutex_unlock(&start_work_mutex);
465 cpu_usage_0 = get_cpu_usage_nsec_self();
467 for (i = 0; i < this_task->nr_events; i++) {
468 this_task->curr_event = i;
469 process_sched_event(this_task, this_task->atoms[i]);
472 cpu_usage_1 = get_cpu_usage_nsec_self();
473 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
475 ret = sem_post(&this_task->work_done_sem);
478 ret = pthread_mutex_lock(&work_done_wait_mutex);
480 ret = pthread_mutex_unlock(&work_done_wait_mutex);
486 static void create_tasks(void)
488 struct task_desc *task;
493 err = pthread_attr_init(&attr);
495 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
497 err = pthread_mutex_lock(&start_work_mutex);
499 err = pthread_mutex_lock(&work_done_wait_mutex);
501 for (i = 0; i < nr_tasks; i++) {
503 sem_init(&task->sleep_sem, 0, 0);
504 sem_init(&task->ready_for_work, 0, 0);
505 sem_init(&task->work_done_sem, 0, 0);
506 task->curr_event = 0;
507 err = pthread_create(&task->thread, &attr, thread_func, task);
512 static void wait_for_tasks(void)
514 u64 cpu_usage_0, cpu_usage_1;
515 struct task_desc *task;
516 unsigned long i, ret;
518 start_time = get_nsecs();
520 pthread_mutex_unlock(&work_done_wait_mutex);
522 for (i = 0; i < nr_tasks; i++) {
524 ret = sem_wait(&task->ready_for_work);
526 sem_init(&task->ready_for_work, 0, 0);
528 ret = pthread_mutex_lock(&work_done_wait_mutex);
531 cpu_usage_0 = get_cpu_usage_nsec_parent();
533 pthread_mutex_unlock(&start_work_mutex);
535 for (i = 0; i < nr_tasks; i++) {
537 ret = sem_wait(&task->work_done_sem);
539 sem_init(&task->work_done_sem, 0, 0);
540 cpu_usage += task->cpu_usage;
544 cpu_usage_1 = get_cpu_usage_nsec_parent();
545 if (!runavg_cpu_usage)
546 runavg_cpu_usage = cpu_usage;
547 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
549 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
550 if (!runavg_parent_cpu_usage)
551 runavg_parent_cpu_usage = parent_cpu_usage;
552 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
553 parent_cpu_usage)/10;
555 ret = pthread_mutex_lock(&start_work_mutex);
558 for (i = 0; i < nr_tasks; i++) {
560 sem_init(&task->sleep_sem, 0, 0);
561 task->curr_event = 0;
565 static void run_one_test(void)
567 u64 T0, T1, delta, avg_delta, fluct, std_dev;
574 sum_runtime += delta;
577 avg_delta = sum_runtime / nr_runs;
578 if (delta < avg_delta)
579 fluct = avg_delta - delta;
581 fluct = delta - avg_delta;
583 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
586 run_avg = (run_avg*9 + delta)/10;
588 printf("#%-3ld: %0.3f, ",
589 nr_runs, (double)delta/1000000.0);
591 printf("ravg: %0.2f, ",
592 (double)run_avg/1e6);
594 printf("cpu: %0.2f / %0.2f",
595 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
599 * rusage statistics done by the parent, these are less
600 * accurate than the sum_exec_runtime based statistics:
602 printf(" [%0.2f / %0.2f]",
603 (double)parent_cpu_usage/1e6,
604 (double)runavg_parent_cpu_usage/1e6);
609 if (nr_sleep_corrections)
610 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
611 nr_sleep_corrections = 0;
614 static void test_calibrations(void)
622 printf("the run test took %Ld nsecs\n", T1-T0);
628 printf("the sleep test took %Ld nsecs\n", T1-T0);
631 #define FILL_FIELD(ptr, field, event, data) \
632 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
634 #define FILL_ARRAY(ptr, array, event, data) \
636 void *__array = raw_field_ptr(event, #array, data); \
637 memcpy(ptr.array, __array, sizeof(ptr.array)); \
640 #define FILL_COMMON_FIELDS(ptr, event, data) \
642 FILL_FIELD(ptr, common_type, event, data); \
643 FILL_FIELD(ptr, common_flags, event, data); \
644 FILL_FIELD(ptr, common_preempt_count, event, data); \
645 FILL_FIELD(ptr, common_pid, event, data); \
646 FILL_FIELD(ptr, common_tgid, event, data); \
651 struct trace_switch_event {
656 u8 common_preempt_count;
669 struct trace_runtime_event {
674 u8 common_preempt_count;
684 struct trace_wakeup_event {
689 u8 common_preempt_count;
701 struct trace_fork_event {
706 u8 common_preempt_count;
710 char parent_comm[16];
716 struct trace_migrate_task_event {
721 u8 common_preempt_count;
732 struct trace_sched_handler {
733 void (*switch_event)(struct trace_switch_event *,
737 struct thread *thread);
739 void (*runtime_event)(struct trace_runtime_event *,
743 struct thread *thread);
745 void (*wakeup_event)(struct trace_wakeup_event *,
749 struct thread *thread);
751 void (*fork_event)(struct trace_fork_event *,
755 struct thread *thread);
757 void (*migrate_task_event)(struct trace_migrate_task_event *,
761 struct thread *thread);
766 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
769 u64 timestamp __used,
770 struct thread *thread __used)
772 struct task_desc *waker, *wakee;
775 printf("sched_wakeup event %p\n", event);
777 printf(" ... pid %d woke up %s/%d\n",
778 wakeup_event->common_pid,
783 waker = register_pid(wakeup_event->common_pid, "<unknown>");
784 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
786 add_sched_event_wakeup(waker, timestamp, wakee);
789 static u64 cpu_last_switched[MAX_CPUS];
792 replay_switch_event(struct trace_switch_event *switch_event,
796 struct thread *thread __used)
798 struct task_desc *prev, *next;
803 printf("sched_switch event %p\n", event);
805 if (cpu >= MAX_CPUS || cpu < 0)
808 timestamp0 = cpu_last_switched[cpu];
810 delta = timestamp - timestamp0;
815 die("hm, delta: %Ld < 0 ?\n", delta);
818 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
819 switch_event->prev_comm, switch_event->prev_pid,
820 switch_event->next_comm, switch_event->next_pid,
824 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
825 next = register_pid(switch_event->next_pid, switch_event->next_comm);
827 cpu_last_switched[cpu] = timestamp;
829 add_sched_event_run(prev, timestamp, delta);
830 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
835 replay_fork_event(struct trace_fork_event *fork_event,
838 u64 timestamp __used,
839 struct thread *thread __used)
842 printf("sched_fork event %p\n", event);
843 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
844 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
846 register_pid(fork_event->parent_pid, fork_event->parent_comm);
847 register_pid(fork_event->child_pid, fork_event->child_comm);
850 static struct trace_sched_handler replay_ops = {
851 .wakeup_event = replay_wakeup_event,
852 .switch_event = replay_switch_event,
853 .fork_event = replay_fork_event,
856 struct sort_dimension {
859 struct list_head list;
862 static LIST_HEAD(cmp_pid);
865 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
867 struct sort_dimension *sort;
870 BUG_ON(list_empty(list));
872 list_for_each_entry(sort, list, list) {
873 ret = sort->cmp(l, r);
881 static struct work_atoms *
882 thread_atoms_search(struct rb_root *root, struct thread *thread,
883 struct list_head *sort_list)
885 struct rb_node *node = root->rb_node;
886 struct work_atoms key = { .thread = thread };
889 struct work_atoms *atoms;
892 atoms = container_of(node, struct work_atoms, node);
894 cmp = thread_lat_cmp(sort_list, &key, atoms);
896 node = node->rb_left;
898 node = node->rb_right;
900 BUG_ON(thread != atoms->thread);
908 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
909 struct list_head *sort_list)
911 struct rb_node **new = &(root->rb_node), *parent = NULL;
914 struct work_atoms *this;
917 this = container_of(*new, struct work_atoms, node);
920 cmp = thread_lat_cmp(sort_list, data, this);
923 new = &((*new)->rb_left);
925 new = &((*new)->rb_right);
928 rb_link_node(&data->node, parent, new);
929 rb_insert_color(&data->node, root);
932 static void thread_atoms_insert(struct thread *thread)
934 struct work_atoms *atoms = zalloc(sizeof(*atoms));
938 atoms->thread = thread;
939 INIT_LIST_HEAD(&atoms->work_list);
940 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
944 latency_fork_event(struct trace_fork_event *fork_event __used,
945 struct event *event __used,
947 u64 timestamp __used,
948 struct thread *thread __used)
950 /* should insert the newcomer */
954 static char sched_out_state(struct trace_switch_event *switch_event)
956 const char *str = TASK_STATE_TO_CHAR_STR;
958 return str[switch_event->prev_state];
962 add_sched_out_event(struct work_atoms *atoms,
966 struct work_atom *atom = zalloc(sizeof(*atom));
970 atom->sched_out_time = timestamp;
972 if (run_state == 'R') {
973 atom->state = THREAD_WAIT_CPU;
974 atom->wake_up_time = atom->sched_out_time;
977 list_add_tail(&atom->list, &atoms->work_list);
981 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
983 struct work_atom *atom;
985 BUG_ON(list_empty(&atoms->work_list));
987 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
989 atom->runtime += delta;
990 atoms->total_runtime += delta;
994 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
996 struct work_atom *atom;
999 if (list_empty(&atoms->work_list))
1002 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1004 if (atom->state != THREAD_WAIT_CPU)
1007 if (timestamp < atom->wake_up_time) {
1008 atom->state = THREAD_IGNORE;
1012 atom->state = THREAD_SCHED_IN;
1013 atom->sched_in_time = timestamp;
1015 delta = atom->sched_in_time - atom->wake_up_time;
1016 atoms->total_lat += delta;
1017 if (delta > atoms->max_lat)
1018 atoms->max_lat = delta;
1023 latency_switch_event(struct trace_switch_event *switch_event,
1024 struct event *event __used,
1027 struct thread *thread __used)
1029 struct work_atoms *out_events, *in_events;
1030 struct thread *sched_out, *sched_in;
1034 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1036 timestamp0 = cpu_last_switched[cpu];
1037 cpu_last_switched[cpu] = timestamp;
1039 delta = timestamp - timestamp0;
1044 die("hm, delta: %Ld < 0 ?\n", delta);
1047 sched_out = threads__findnew(switch_event->prev_pid);
1048 sched_in = threads__findnew(switch_event->next_pid);
1050 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1052 thread_atoms_insert(sched_out);
1053 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1055 die("out-event: Internal tree error");
1057 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1059 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1061 thread_atoms_insert(sched_in);
1062 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1064 die("in-event: Internal tree error");
1066 * Take came in we have not heard about yet,
1067 * add in an initial atom in runnable state:
1069 add_sched_out_event(in_events, 'R', timestamp);
1071 add_sched_in_event(in_events, timestamp);
1075 latency_runtime_event(struct trace_runtime_event *runtime_event,
1076 struct event *event __used,
1079 struct thread *this_thread __used)
1081 struct thread *thread = threads__findnew(runtime_event->pid);
1082 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1084 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1086 thread_atoms_insert(thread);
1087 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1089 die("in-event: Internal tree error");
1090 add_sched_out_event(atoms, 'R', timestamp);
1093 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1097 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1098 struct event *__event __used,
1101 struct thread *thread __used)
1103 struct work_atoms *atoms;
1104 struct work_atom *atom;
1105 struct thread *wakee;
1107 /* Note for later, it may be interesting to observe the failing cases */
1108 if (!wakeup_event->success)
1111 wakee = threads__findnew(wakeup_event->pid);
1112 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1114 thread_atoms_insert(wakee);
1115 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1117 die("wakeup-event: Internal tree error");
1118 add_sched_out_event(atoms, 'S', timestamp);
1121 BUG_ON(list_empty(&atoms->work_list));
1123 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1126 * You WILL be missing events if you've recorded only
1127 * one CPU, or are only looking at only one, so don't
1128 * make useless noise.
1130 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1131 nr_state_machine_bugs++;
1134 if (atom->sched_out_time > timestamp) {
1135 nr_unordered_timestamps++;
1139 atom->state = THREAD_WAIT_CPU;
1140 atom->wake_up_time = timestamp;
1144 latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1145 struct event *__event __used,
1148 struct thread *thread __used)
1150 struct work_atoms *atoms;
1151 struct work_atom *atom;
1152 struct thread *migrant;
1155 * Only need to worry about migration when profiling one CPU.
1157 if (profile_cpu == -1)
1160 migrant = threads__findnew(migrate_task_event->pid);
1161 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1163 thread_atoms_insert(migrant);
1164 register_pid(migrant->pid, migrant->comm);
1165 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1167 die("migration-event: Internal tree error");
1168 add_sched_out_event(atoms, 'R', timestamp);
1171 BUG_ON(list_empty(&atoms->work_list));
1173 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1174 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1178 if (atom->sched_out_time > timestamp)
1179 nr_unordered_timestamps++;
1182 static struct trace_sched_handler lat_ops = {
1183 .wakeup_event = latency_wakeup_event,
1184 .switch_event = latency_switch_event,
1185 .runtime_event = latency_runtime_event,
1186 .fork_event = latency_fork_event,
1187 .migrate_task_event = latency_migrate_task_event,
1190 static void output_lat_thread(struct work_atoms *work_list)
1196 if (!work_list->nb_atoms)
1199 * Ignore idle threads:
1201 if (!strcmp(work_list->thread->comm, "swapper"))
1204 all_runtime += work_list->total_runtime;
1205 all_count += work_list->nb_atoms;
1207 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
1209 for (i = 0; i < 24 - ret; i++)
1212 avg = work_list->total_lat / work_list->nb_atoms;
1214 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
1215 (double)work_list->total_runtime / 1e6,
1216 work_list->nb_atoms, (double)avg / 1e6,
1217 (double)work_list->max_lat / 1e6);
1220 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1222 if (l->thread->pid < r->thread->pid)
1224 if (l->thread->pid > r->thread->pid)
1230 static struct sort_dimension pid_sort_dimension = {
1235 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1245 avgl = l->total_lat / l->nb_atoms;
1246 avgr = r->total_lat / r->nb_atoms;
1256 static struct sort_dimension avg_sort_dimension = {
1261 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1263 if (l->max_lat < r->max_lat)
1265 if (l->max_lat > r->max_lat)
1271 static struct sort_dimension max_sort_dimension = {
1276 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1278 if (l->nb_atoms < r->nb_atoms)
1280 if (l->nb_atoms > r->nb_atoms)
1286 static struct sort_dimension switch_sort_dimension = {
1291 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1293 if (l->total_runtime < r->total_runtime)
1295 if (l->total_runtime > r->total_runtime)
1301 static struct sort_dimension runtime_sort_dimension = {
1306 static struct sort_dimension *available_sorts[] = {
1307 &pid_sort_dimension,
1308 &avg_sort_dimension,
1309 &max_sort_dimension,
1310 &switch_sort_dimension,
1311 &runtime_sort_dimension,
1314 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1316 static LIST_HEAD(sort_list);
1318 static int sort_dimension__add(const char *tok, struct list_head *list)
1322 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1323 if (!strcmp(available_sorts[i]->name, tok)) {
1324 list_add_tail(&available_sorts[i]->list, list);
1333 static void setup_sorting(void);
1335 static void sort_lat(void)
1337 struct rb_node *node;
1340 struct work_atoms *data;
1341 node = rb_first(&atom_root);
1345 rb_erase(node, &atom_root);
1346 data = rb_entry(node, struct work_atoms, node);
1347 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1351 static struct trace_sched_handler *trace_handler;
1354 process_sched_wakeup_event(void *data,
1355 struct event *event,
1357 u64 timestamp __used,
1358 struct thread *thread __used)
1360 struct trace_wakeup_event wakeup_event;
1362 FILL_COMMON_FIELDS(wakeup_event, event, data);
1364 FILL_ARRAY(wakeup_event, comm, event, data);
1365 FILL_FIELD(wakeup_event, pid, event, data);
1366 FILL_FIELD(wakeup_event, prio, event, data);
1367 FILL_FIELD(wakeup_event, success, event, data);
1368 FILL_FIELD(wakeup_event, cpu, event, data);
1370 if (trace_handler->wakeup_event)
1371 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
1375 * Track the current task - that way we can know whether there's any
1376 * weird events, such as a task being switched away that is not current.
1380 static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1382 static struct thread *curr_thread[MAX_CPUS];
1384 static char next_shortname1 = 'A';
1385 static char next_shortname2 = '0';
1388 map_switch_event(struct trace_switch_event *switch_event,
1389 struct event *event __used,
1392 struct thread *thread __used)
1394 struct thread *sched_out, *sched_in;
1400 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1402 if (this_cpu > max_cpu)
1405 timestamp0 = cpu_last_switched[this_cpu];
1406 cpu_last_switched[this_cpu] = timestamp;
1408 delta = timestamp - timestamp0;
1413 die("hm, delta: %Ld < 0 ?\n", delta);
1416 sched_out = threads__findnew(switch_event->prev_pid);
1417 sched_in = threads__findnew(switch_event->next_pid);
1419 curr_thread[this_cpu] = sched_in;
1424 if (!sched_in->shortname[0]) {
1425 sched_in->shortname[0] = next_shortname1;
1426 sched_in->shortname[1] = next_shortname2;
1428 if (next_shortname1 < 'Z') {
1431 next_shortname1='A';
1432 if (next_shortname2 < '9') {
1435 next_shortname2='0';
1441 for (cpu = 0; cpu <= max_cpu; cpu++) {
1442 if (cpu != this_cpu)
1447 if (curr_thread[cpu]) {
1448 if (curr_thread[cpu]->pid)
1449 printf("%2s ", curr_thread[cpu]->shortname);
1456 printf(" %12.6f secs ", (double)timestamp/1e9);
1457 if (new_shortname) {
1458 printf("%s => %s:%d\n",
1459 sched_in->shortname, sched_in->comm, sched_in->pid);
1467 process_sched_switch_event(void *data,
1468 struct event *event,
1470 u64 timestamp __used,
1471 struct thread *thread __used)
1473 struct trace_switch_event switch_event;
1475 FILL_COMMON_FIELDS(switch_event, event, data);
1477 FILL_ARRAY(switch_event, prev_comm, event, data);
1478 FILL_FIELD(switch_event, prev_pid, event, data);
1479 FILL_FIELD(switch_event, prev_prio, event, data);
1480 FILL_FIELD(switch_event, prev_state, event, data);
1481 FILL_ARRAY(switch_event, next_comm, event, data);
1482 FILL_FIELD(switch_event, next_pid, event, data);
1483 FILL_FIELD(switch_event, next_prio, event, data);
1485 if (curr_pid[this_cpu] != (u32)-1) {
1487 * Are we trying to switch away a PID that is
1490 if (curr_pid[this_cpu] != switch_event.prev_pid)
1491 nr_context_switch_bugs++;
1493 if (trace_handler->switch_event)
1494 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
1496 curr_pid[this_cpu] = switch_event.next_pid;
1500 process_sched_runtime_event(void *data,
1501 struct event *event,
1503 u64 timestamp __used,
1504 struct thread *thread __used)
1506 struct trace_runtime_event runtime_event;
1508 FILL_ARRAY(runtime_event, comm, event, data);
1509 FILL_FIELD(runtime_event, pid, event, data);
1510 FILL_FIELD(runtime_event, runtime, event, data);
1511 FILL_FIELD(runtime_event, vruntime, event, data);
1513 if (trace_handler->runtime_event)
1514 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
1518 process_sched_fork_event(void *data,
1519 struct event *event,
1521 u64 timestamp __used,
1522 struct thread *thread __used)
1524 struct trace_fork_event fork_event;
1526 FILL_COMMON_FIELDS(fork_event, event, data);
1528 FILL_ARRAY(fork_event, parent_comm, event, data);
1529 FILL_FIELD(fork_event, parent_pid, event, data);
1530 FILL_ARRAY(fork_event, child_comm, event, data);
1531 FILL_FIELD(fork_event, child_pid, event, data);
1533 if (trace_handler->fork_event)
1534 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
1538 process_sched_exit_event(struct event *event,
1540 u64 timestamp __used,
1541 struct thread *thread __used)
1544 printf("sched_exit event %p\n", event);
1548 process_sched_migrate_task_event(void *data,
1549 struct event *event,
1551 u64 timestamp __used,
1552 struct thread *thread __used)
1554 struct trace_migrate_task_event migrate_task_event;
1556 FILL_COMMON_FIELDS(migrate_task_event, event, data);
1558 FILL_ARRAY(migrate_task_event, comm, event, data);
1559 FILL_FIELD(migrate_task_event, pid, event, data);
1560 FILL_FIELD(migrate_task_event, prio, event, data);
1561 FILL_FIELD(migrate_task_event, cpu, event, data);
1563 if (trace_handler->migrate_task_event)
1564 trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
1568 process_raw_event(event_t *raw_event __used, void *data,
1569 int cpu, u64 timestamp, struct thread *thread)
1571 struct event *event;
1575 type = trace_parse_common_type(data);
1576 event = trace_find_event(type);
1578 if (!strcmp(event->name, "sched_switch"))
1579 process_sched_switch_event(data, event, cpu, timestamp, thread);
1580 if (!strcmp(event->name, "sched_stat_runtime"))
1581 process_sched_runtime_event(data, event, cpu, timestamp, thread);
1582 if (!strcmp(event->name, "sched_wakeup"))
1583 process_sched_wakeup_event(data, event, cpu, timestamp, thread);
1584 if (!strcmp(event->name, "sched_wakeup_new"))
1585 process_sched_wakeup_event(data, event, cpu, timestamp, thread);
1586 if (!strcmp(event->name, "sched_process_fork"))
1587 process_sched_fork_event(data, event, cpu, timestamp, thread);
1588 if (!strcmp(event->name, "sched_process_exit"))
1589 process_sched_exit_event(event, cpu, timestamp, thread);
1590 if (!strcmp(event->name, "sched_migrate_task"))
1591 process_sched_migrate_task_event(data, event, cpu, timestamp, thread);
1594 static int process_sample_event(event_t *event)
1596 struct sample_data data;
1597 struct thread *thread;
1599 if (!(sample_type & PERF_SAMPLE_RAW))
1602 memset(&data, 0, sizeof(data));
1607 event__parse_sample(event, sample_type, &data);
1609 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
1612 (void *)(long)data.ip,
1613 (long long)data.period);
1615 thread = threads__findnew(data.pid);
1616 if (thread == NULL) {
1617 pr_debug("problem processing %d event, skipping it.\n",
1618 event->header.type);
1622 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1624 if (profile_cpu != -1 && profile_cpu != (int)data.cpu)
1627 process_raw_event(event, data.raw_data, data.cpu, data.time, thread);
1632 static int process_lost_event(event_t *event __used)
1635 nr_lost_events += event->lost.lost;
1640 static int sample_type_check(u64 type)
1644 if (!(sample_type & PERF_SAMPLE_RAW)) {
1646 "No trace sample to read. Did you call perf record "
1654 static struct perf_file_handler file_handler = {
1655 .process_sample_event = process_sample_event,
1656 .process_comm_event = event__process_comm,
1657 .process_lost_event = process_lost_event,
1658 .sample_type_check = sample_type_check,
1661 static int read_events(void)
1663 register_idle_thread();
1664 register_perf_file_handler(&file_handler);
1666 return mmap_dispatch_perf_file(&header, input_name, 0, 0,
1667 &event__cwdlen, &event__cwd);
1670 static void print_bad_events(void)
1672 if (nr_unordered_timestamps && nr_timestamps) {
1673 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1674 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1675 nr_unordered_timestamps, nr_timestamps);
1677 if (nr_lost_events && nr_events) {
1678 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1679 (double)nr_lost_events/(double)nr_events*100.0,
1680 nr_lost_events, nr_events, nr_lost_chunks);
1682 if (nr_state_machine_bugs && nr_timestamps) {
1683 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1684 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1685 nr_state_machine_bugs, nr_timestamps);
1687 printf(" (due to lost events?)");
1690 if (nr_context_switch_bugs && nr_timestamps) {
1691 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1692 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1693 nr_context_switch_bugs, nr_timestamps);
1695 printf(" (due to lost events?)");
1700 static void __cmd_lat(void)
1702 struct rb_node *next;
1708 printf("\n -----------------------------------------------------------------------------------------\n");
1709 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1710 printf(" -----------------------------------------------------------------------------------------\n");
1712 next = rb_first(&sorted_atom_root);
1715 struct work_atoms *work_list;
1717 work_list = rb_entry(next, struct work_atoms, node);
1718 output_lat_thread(work_list);
1719 next = rb_next(next);
1722 printf(" -----------------------------------------------------------------------------------------\n");
1723 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1724 (double)all_runtime/1e6, all_count);
1726 printf(" ---------------------------------------------------\n");
1733 static struct trace_sched_handler map_ops = {
1734 .wakeup_event = NULL,
1735 .switch_event = map_switch_event,
1736 .runtime_event = NULL,
1740 static void __cmd_map(void)
1742 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1749 static void __cmd_replay(void)
1753 calibrate_run_measurement_overhead();
1754 calibrate_sleep_measurement_overhead();
1756 test_calibrations();
1760 printf("nr_run_events: %ld\n", nr_run_events);
1761 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1762 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1764 if (targetless_wakeups)
1765 printf("target-less wakeups: %ld\n", targetless_wakeups);
1766 if (multitarget_wakeups)
1767 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1768 if (nr_run_events_optimized)
1769 printf("run atoms optimized: %ld\n",
1770 nr_run_events_optimized);
1772 print_task_traces();
1773 add_cross_task_wakeups();
1776 printf("------------------------------------------------------------\n");
1777 for (i = 0; i < replay_repeat; i++)
1782 static const char * const sched_usage[] = {
1783 "perf sched [<options>] {record|latency|map|replay|trace}",
1787 static const struct option sched_options[] = {
1788 OPT_STRING('i', "input", &input_name, "file",
1790 OPT_BOOLEAN('v', "verbose", &verbose,
1791 "be more verbose (show symbol address, etc)"),
1792 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1793 "dump raw trace in ASCII"),
1797 static const char * const latency_usage[] = {
1798 "perf sched latency [<options>]",
1802 static const struct option latency_options[] = {
1803 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1804 "sort by key(s): runtime, switch, avg, max"),
1805 OPT_BOOLEAN('v', "verbose", &verbose,
1806 "be more verbose (show symbol address, etc)"),
1807 OPT_INTEGER('C', "CPU", &profile_cpu,
1808 "CPU to profile on"),
1809 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1810 "dump raw trace in ASCII"),
1814 static const char * const replay_usage[] = {
1815 "perf sched replay [<options>]",
1819 static const struct option replay_options[] = {
1820 OPT_INTEGER('r', "repeat", &replay_repeat,
1821 "repeat the workload replay N times (-1: infinite)"),
1822 OPT_BOOLEAN('v', "verbose", &verbose,
1823 "be more verbose (show symbol address, etc)"),
1824 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1825 "dump raw trace in ASCII"),
1829 static void setup_sorting(void)
1831 char *tmp, *tok, *str = strdup(sort_order);
1833 for (tok = strtok_r(str, ", ", &tmp);
1834 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1835 if (sort_dimension__add(tok, &sort_list) < 0) {
1836 error("Unknown --sort key: `%s'", tok);
1837 usage_with_options(latency_usage, latency_options);
1843 sort_dimension__add("pid", &cmp_pid);
1846 static const char *record_args[] = {
1854 "-e", "sched:sched_switch:r",
1855 "-e", "sched:sched_stat_wait:r",
1856 "-e", "sched:sched_stat_sleep:r",
1857 "-e", "sched:sched_stat_iowait:r",
1858 "-e", "sched:sched_stat_runtime:r",
1859 "-e", "sched:sched_process_exit:r",
1860 "-e", "sched:sched_process_fork:r",
1861 "-e", "sched:sched_wakeup:r",
1862 "-e", "sched:sched_migrate_task:r",
1865 static int __cmd_record(int argc, const char **argv)
1867 unsigned int rec_argc, i, j;
1868 const char **rec_argv;
1870 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1871 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1873 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1874 rec_argv[i] = strdup(record_args[i]);
1876 for (j = 1; j < (unsigned int)argc; j++, i++)
1877 rec_argv[i] = argv[j];
1879 BUG_ON(i != rec_argc);
1881 return cmd_record(i, rec_argv, NULL);
1884 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1886 argc = parse_options(argc, argv, sched_options, sched_usage,
1887 PARSE_OPT_STOP_AT_NON_OPTION);
1889 usage_with_options(sched_usage, sched_options);
1892 * Aliased to 'perf trace' for now:
1894 if (!strcmp(argv[0], "trace"))
1895 return cmd_trace(argc, argv, prefix);
1898 if (!strncmp(argv[0], "rec", 3)) {
1899 return __cmd_record(argc, argv);
1900 } else if (!strncmp(argv[0], "lat", 3)) {
1901 trace_handler = &lat_ops;
1903 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1905 usage_with_options(latency_usage, latency_options);
1909 } else if (!strcmp(argv[0], "map")) {
1910 trace_handler = &map_ops;
1913 } else if (!strncmp(argv[0], "rep", 3)) {
1914 trace_handler = &replay_ops;
1916 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1918 usage_with_options(replay_usage, replay_options);
1922 usage_with_options(sched_usage, sched_options);