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 unsigned long total_comm = 0;
27 static struct perf_header *header;
28 static u64 sample_type;
30 static char default_sort_order[] = "avg, max, switch, runtime";
31 static char *sort_order = default_sort_order;
33 static int profile_cpu = -1;
38 #define PR_SET_NAME 15 /* Set process name */
41 static u64 run_measurement_overhead;
42 static u64 sleep_measurement_overhead;
49 static unsigned long nr_tasks;
58 unsigned long nr_events;
59 unsigned long curr_event;
60 struct sched_atom **atoms;
71 enum sched_event_type {
75 SCHED_EVENT_MIGRATION,
79 enum sched_event_type type;
85 struct task_desc *wakee;
88 static struct task_desc *pid_to_task[MAX_PID];
90 static struct task_desc **tasks;
92 static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
93 static u64 start_time;
95 static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
97 static unsigned long nr_run_events;
98 static unsigned long nr_sleep_events;
99 static unsigned long nr_wakeup_events;
101 static unsigned long nr_sleep_corrections;
102 static unsigned long nr_run_events_optimized;
104 static unsigned long targetless_wakeups;
105 static unsigned long multitarget_wakeups;
107 static u64 cpu_usage;
108 static u64 runavg_cpu_usage;
109 static u64 parent_cpu_usage;
110 static u64 runavg_parent_cpu_usage;
112 static unsigned long nr_runs;
113 static u64 sum_runtime;
114 static u64 sum_fluct;
117 static unsigned long replay_repeat = 10;
118 static unsigned long nr_timestamps;
119 static unsigned long nr_unordered_timestamps;
120 static unsigned long nr_state_machine_bugs;
121 static unsigned long nr_context_switch_bugs;
122 static unsigned long nr_events;
123 static unsigned long nr_lost_chunks;
124 static unsigned long nr_lost_events;
126 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
136 struct list_head list;
137 enum thread_state state;
145 struct list_head work_list;
146 struct thread *thread;
154 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
156 static struct rb_root atom_root, sorted_atom_root;
158 static u64 all_runtime;
159 static u64 all_count;
162 static u64 get_nsecs(void)
166 clock_gettime(CLOCK_MONOTONIC, &ts);
168 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
171 static void burn_nsecs(u64 nsecs)
173 u64 T0 = get_nsecs(), T1;
177 } while (T1 + run_measurement_overhead < T0 + nsecs);
180 static void sleep_nsecs(u64 nsecs)
184 ts.tv_nsec = nsecs % 999999999;
185 ts.tv_sec = nsecs / 999999999;
187 nanosleep(&ts, NULL);
190 static void calibrate_run_measurement_overhead(void)
192 u64 T0, T1, delta, min_delta = 1000000000ULL;
195 for (i = 0; i < 10; i++) {
200 min_delta = min(min_delta, delta);
202 run_measurement_overhead = min_delta;
204 printf("run measurement overhead: %Ld nsecs\n", min_delta);
207 static void calibrate_sleep_measurement_overhead(void)
209 u64 T0, T1, delta, min_delta = 1000000000ULL;
212 for (i = 0; i < 10; i++) {
217 min_delta = min(min_delta, delta);
220 sleep_measurement_overhead = min_delta;
222 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
225 static struct sched_atom *
226 get_new_event(struct task_desc *task, u64 timestamp)
228 struct sched_atom *event = calloc(1, sizeof(*event));
229 unsigned long idx = task->nr_events;
232 event->timestamp = timestamp;
236 size = sizeof(struct sched_atom *) * task->nr_events;
237 task->atoms = realloc(task->atoms, size);
238 BUG_ON(!task->atoms);
240 task->atoms[idx] = event;
245 static struct sched_atom *last_event(struct task_desc *task)
247 if (!task->nr_events)
250 return task->atoms[task->nr_events - 1];
254 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
256 struct sched_atom *event, *curr_event = last_event(task);
259 * optimize an existing RUN event by merging this one
262 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
263 nr_run_events_optimized++;
264 curr_event->duration += duration;
268 event = get_new_event(task, timestamp);
270 event->type = SCHED_EVENT_RUN;
271 event->duration = duration;
277 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
278 struct task_desc *wakee)
280 struct sched_atom *event, *wakee_event;
282 event = get_new_event(task, timestamp);
283 event->type = SCHED_EVENT_WAKEUP;
284 event->wakee = wakee;
286 wakee_event = last_event(wakee);
287 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
288 targetless_wakeups++;
291 if (wakee_event->wait_sem) {
292 multitarget_wakeups++;
296 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
297 sem_init(wakee_event->wait_sem, 0, 0);
298 wakee_event->specific_wait = 1;
299 event->wait_sem = wakee_event->wait_sem;
305 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
306 u64 task_state __used)
308 struct sched_atom *event = get_new_event(task, timestamp);
310 event->type = SCHED_EVENT_SLEEP;
315 static struct task_desc *register_pid(unsigned long pid, const char *comm)
317 struct task_desc *task;
319 BUG_ON(pid >= MAX_PID);
321 task = pid_to_task[pid];
326 task = calloc(1, sizeof(*task));
329 strcpy(task->comm, comm);
331 * every task starts in sleeping state - this gets ignored
332 * if there's no wakeup pointing to this sleep state:
334 add_sched_event_sleep(task, 0, 0);
336 pid_to_task[pid] = task;
338 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
340 tasks[task->nr] = task;
343 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
349 static void print_task_traces(void)
351 struct task_desc *task;
354 for (i = 0; i < nr_tasks; i++) {
356 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
357 task->nr, task->comm, task->pid, task->nr_events);
361 static void add_cross_task_wakeups(void)
363 struct task_desc *task1, *task2;
366 for (i = 0; i < nr_tasks; i++) {
372 add_sched_event_wakeup(task1, 0, task2);
377 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
384 delta = start_time + atom->timestamp - now;
386 switch (atom->type) {
387 case SCHED_EVENT_RUN:
388 burn_nsecs(atom->duration);
390 case SCHED_EVENT_SLEEP:
392 ret = sem_wait(atom->wait_sem);
395 case SCHED_EVENT_WAKEUP:
397 ret = sem_post(atom->wait_sem);
400 case SCHED_EVENT_MIGRATION:
407 static u64 get_cpu_usage_nsec_parent(void)
413 err = getrusage(RUSAGE_SELF, &ru);
416 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
417 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
422 static u64 get_cpu_usage_nsec_self(void)
424 char filename [] = "/proc/1234567890/sched";
425 unsigned long msecs, nsecs;
433 sprintf(filename, "/proc/%d/sched", getpid());
434 file = fopen(filename, "r");
437 while ((chars = getline(&line, &len, file)) != -1) {
438 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
441 total = msecs*1e6 + nsecs;
452 static void *thread_func(void *ctx)
454 struct task_desc *this_task = ctx;
455 u64 cpu_usage_0, cpu_usage_1;
456 unsigned long i, ret;
459 sprintf(comm2, ":%s", this_task->comm);
460 prctl(PR_SET_NAME, comm2);
463 ret = sem_post(&this_task->ready_for_work);
465 ret = pthread_mutex_lock(&start_work_mutex);
467 ret = pthread_mutex_unlock(&start_work_mutex);
470 cpu_usage_0 = get_cpu_usage_nsec_self();
472 for (i = 0; i < this_task->nr_events; i++) {
473 this_task->curr_event = i;
474 process_sched_event(this_task, this_task->atoms[i]);
477 cpu_usage_1 = get_cpu_usage_nsec_self();
478 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
480 ret = sem_post(&this_task->work_done_sem);
483 ret = pthread_mutex_lock(&work_done_wait_mutex);
485 ret = pthread_mutex_unlock(&work_done_wait_mutex);
491 static void create_tasks(void)
493 struct task_desc *task;
498 err = pthread_attr_init(&attr);
500 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
502 err = pthread_mutex_lock(&start_work_mutex);
504 err = pthread_mutex_lock(&work_done_wait_mutex);
506 for (i = 0; i < nr_tasks; i++) {
508 sem_init(&task->sleep_sem, 0, 0);
509 sem_init(&task->ready_for_work, 0, 0);
510 sem_init(&task->work_done_sem, 0, 0);
511 task->curr_event = 0;
512 err = pthread_create(&task->thread, &attr, thread_func, task);
517 static void wait_for_tasks(void)
519 u64 cpu_usage_0, cpu_usage_1;
520 struct task_desc *task;
521 unsigned long i, ret;
523 start_time = get_nsecs();
525 pthread_mutex_unlock(&work_done_wait_mutex);
527 for (i = 0; i < nr_tasks; i++) {
529 ret = sem_wait(&task->ready_for_work);
531 sem_init(&task->ready_for_work, 0, 0);
533 ret = pthread_mutex_lock(&work_done_wait_mutex);
536 cpu_usage_0 = get_cpu_usage_nsec_parent();
538 pthread_mutex_unlock(&start_work_mutex);
540 for (i = 0; i < nr_tasks; i++) {
542 ret = sem_wait(&task->work_done_sem);
544 sem_init(&task->work_done_sem, 0, 0);
545 cpu_usage += task->cpu_usage;
549 cpu_usage_1 = get_cpu_usage_nsec_parent();
550 if (!runavg_cpu_usage)
551 runavg_cpu_usage = cpu_usage;
552 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
554 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
555 if (!runavg_parent_cpu_usage)
556 runavg_parent_cpu_usage = parent_cpu_usage;
557 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
558 parent_cpu_usage)/10;
560 ret = pthread_mutex_lock(&start_work_mutex);
563 for (i = 0; i < nr_tasks; i++) {
565 sem_init(&task->sleep_sem, 0, 0);
566 task->curr_event = 0;
570 static void run_one_test(void)
572 u64 T0, T1, delta, avg_delta, fluct, std_dev;
579 sum_runtime += delta;
582 avg_delta = sum_runtime / nr_runs;
583 if (delta < avg_delta)
584 fluct = avg_delta - delta;
586 fluct = delta - avg_delta;
588 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
591 run_avg = (run_avg*9 + delta)/10;
593 printf("#%-3ld: %0.3f, ",
594 nr_runs, (double)delta/1000000.0);
596 printf("ravg: %0.2f, ",
597 (double)run_avg/1e6);
599 printf("cpu: %0.2f / %0.2f",
600 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
604 * rusage statistics done by the parent, these are less
605 * accurate than the sum_exec_runtime based statistics:
607 printf(" [%0.2f / %0.2f]",
608 (double)parent_cpu_usage/1e6,
609 (double)runavg_parent_cpu_usage/1e6);
614 if (nr_sleep_corrections)
615 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
616 nr_sleep_corrections = 0;
619 static void test_calibrations(void)
627 printf("the run test took %Ld nsecs\n", T1-T0);
633 printf("the sleep test took %Ld nsecs\n", T1-T0);
637 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
639 struct thread *thread = threads__findnew(event->comm.tid);
641 dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
642 (void *)(offset + head),
643 (void *)(long)(event->header.size),
644 event->comm.comm, event->comm.pid);
646 if (thread == NULL ||
647 thread__set_comm(thread, event->comm.comm)) {
648 dump_printf("problem processing perf_event_comm, skipping event.\n");
657 struct raw_event_sample {
662 #define FILL_FIELD(ptr, field, event, data) \
663 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
665 #define FILL_ARRAY(ptr, array, event, data) \
667 void *__array = raw_field_ptr(event, #array, data); \
668 memcpy(ptr.array, __array, sizeof(ptr.array)); \
671 #define FILL_COMMON_FIELDS(ptr, event, data) \
673 FILL_FIELD(ptr, common_type, event, data); \
674 FILL_FIELD(ptr, common_flags, event, data); \
675 FILL_FIELD(ptr, common_preempt_count, event, data); \
676 FILL_FIELD(ptr, common_pid, event, data); \
677 FILL_FIELD(ptr, common_tgid, event, data); \
682 struct trace_switch_event {
687 u8 common_preempt_count;
700 struct trace_runtime_event {
705 u8 common_preempt_count;
715 struct trace_wakeup_event {
720 u8 common_preempt_count;
732 struct trace_fork_event {
737 u8 common_preempt_count;
741 char parent_comm[16];
747 struct trace_migrate_task_event {
752 u8 common_preempt_count;
763 struct trace_sched_handler {
764 void (*switch_event)(struct trace_switch_event *,
768 struct thread *thread);
770 void (*runtime_event)(struct trace_runtime_event *,
774 struct thread *thread);
776 void (*wakeup_event)(struct trace_wakeup_event *,
780 struct thread *thread);
782 void (*fork_event)(struct trace_fork_event *,
786 struct thread *thread);
788 void (*migrate_task_event)(struct trace_migrate_task_event *,
792 struct thread *thread);
797 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
800 u64 timestamp __used,
801 struct thread *thread __used)
803 struct task_desc *waker, *wakee;
806 printf("sched_wakeup event %p\n", event);
808 printf(" ... pid %d woke up %s/%d\n",
809 wakeup_event->common_pid,
814 waker = register_pid(wakeup_event->common_pid, "<unknown>");
815 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
817 add_sched_event_wakeup(waker, timestamp, wakee);
820 static u64 cpu_last_switched[MAX_CPUS];
823 replay_switch_event(struct trace_switch_event *switch_event,
827 struct thread *thread __used)
829 struct task_desc *prev, *next;
834 printf("sched_switch event %p\n", event);
836 if (cpu >= MAX_CPUS || cpu < 0)
839 timestamp0 = cpu_last_switched[cpu];
841 delta = timestamp - timestamp0;
846 die("hm, delta: %Ld < 0 ?\n", delta);
849 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
850 switch_event->prev_comm, switch_event->prev_pid,
851 switch_event->next_comm, switch_event->next_pid,
855 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
856 next = register_pid(switch_event->next_pid, switch_event->next_comm);
858 cpu_last_switched[cpu] = timestamp;
860 add_sched_event_run(prev, timestamp, delta);
861 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
866 replay_fork_event(struct trace_fork_event *fork_event,
869 u64 timestamp __used,
870 struct thread *thread __used)
873 printf("sched_fork event %p\n", event);
874 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
875 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
877 register_pid(fork_event->parent_pid, fork_event->parent_comm);
878 register_pid(fork_event->child_pid, fork_event->child_comm);
881 static struct trace_sched_handler replay_ops = {
882 .wakeup_event = replay_wakeup_event,
883 .switch_event = replay_switch_event,
884 .fork_event = replay_fork_event,
887 struct sort_dimension {
890 struct list_head list;
893 static LIST_HEAD(cmp_pid);
896 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
898 struct sort_dimension *sort;
901 BUG_ON(list_empty(list));
903 list_for_each_entry(sort, list, list) {
904 ret = sort->cmp(l, r);
912 static struct work_atoms *
913 thread_atoms_search(struct rb_root *root, struct thread *thread,
914 struct list_head *sort_list)
916 struct rb_node *node = root->rb_node;
917 struct work_atoms key = { .thread = thread };
920 struct work_atoms *atoms;
923 atoms = container_of(node, struct work_atoms, node);
925 cmp = thread_lat_cmp(sort_list, &key, atoms);
927 node = node->rb_left;
929 node = node->rb_right;
931 BUG_ON(thread != atoms->thread);
939 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
940 struct list_head *sort_list)
942 struct rb_node **new = &(root->rb_node), *parent = NULL;
945 struct work_atoms *this;
948 this = container_of(*new, struct work_atoms, node);
951 cmp = thread_lat_cmp(sort_list, data, this);
954 new = &((*new)->rb_left);
956 new = &((*new)->rb_right);
959 rb_link_node(&data->node, parent, new);
960 rb_insert_color(&data->node, root);
963 static void thread_atoms_insert(struct thread *thread)
965 struct work_atoms *atoms;
967 atoms = calloc(sizeof(*atoms), 1);
971 atoms->thread = thread;
972 INIT_LIST_HEAD(&atoms->work_list);
973 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
977 latency_fork_event(struct trace_fork_event *fork_event __used,
978 struct event *event __used,
980 u64 timestamp __used,
981 struct thread *thread __used)
983 /* should insert the newcomer */
987 static char sched_out_state(struct trace_switch_event *switch_event)
989 const char *str = TASK_STATE_TO_CHAR_STR;
991 return str[switch_event->prev_state];
995 add_sched_out_event(struct work_atoms *atoms,
999 struct work_atom *atom;
1001 atom = calloc(sizeof(*atom), 1);
1005 atom->sched_out_time = timestamp;
1007 if (run_state == 'R') {
1008 atom->state = THREAD_WAIT_CPU;
1009 atom->wake_up_time = atom->sched_out_time;
1012 list_add_tail(&atom->list, &atoms->work_list);
1016 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
1018 struct work_atom *atom;
1020 BUG_ON(list_empty(&atoms->work_list));
1022 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1024 atom->runtime += delta;
1025 atoms->total_runtime += delta;
1029 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1031 struct work_atom *atom;
1034 if (list_empty(&atoms->work_list))
1037 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1039 if (atom->state != THREAD_WAIT_CPU)
1042 if (timestamp < atom->wake_up_time) {
1043 atom->state = THREAD_IGNORE;
1047 atom->state = THREAD_SCHED_IN;
1048 atom->sched_in_time = timestamp;
1050 delta = atom->sched_in_time - atom->wake_up_time;
1051 atoms->total_lat += delta;
1052 if (delta > atoms->max_lat)
1053 atoms->max_lat = delta;
1058 latency_switch_event(struct trace_switch_event *switch_event,
1059 struct event *event __used,
1062 struct thread *thread __used)
1064 struct work_atoms *out_events, *in_events;
1065 struct thread *sched_out, *sched_in;
1069 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1071 timestamp0 = cpu_last_switched[cpu];
1072 cpu_last_switched[cpu] = timestamp;
1074 delta = timestamp - timestamp0;
1079 die("hm, delta: %Ld < 0 ?\n", delta);
1082 sched_out = threads__findnew(switch_event->prev_pid);
1083 sched_in = threads__findnew(switch_event->next_pid);
1085 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1087 thread_atoms_insert(sched_out);
1088 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1090 die("out-event: Internal tree error");
1092 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1094 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1096 thread_atoms_insert(sched_in);
1097 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1099 die("in-event: Internal tree error");
1101 * Take came in we have not heard about yet,
1102 * add in an initial atom in runnable state:
1104 add_sched_out_event(in_events, 'R', timestamp);
1106 add_sched_in_event(in_events, timestamp);
1110 latency_runtime_event(struct trace_runtime_event *runtime_event,
1111 struct event *event __used,
1114 struct thread *this_thread __used)
1116 struct thread *thread = threads__findnew(runtime_event->pid);
1117 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1119 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1121 thread_atoms_insert(thread);
1122 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1124 die("in-event: Internal tree error");
1125 add_sched_out_event(atoms, 'R', timestamp);
1128 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1132 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1133 struct event *__event __used,
1136 struct thread *thread __used)
1138 struct work_atoms *atoms;
1139 struct work_atom *atom;
1140 struct thread *wakee;
1142 /* Note for later, it may be interesting to observe the failing cases */
1143 if (!wakeup_event->success)
1146 wakee = threads__findnew(wakeup_event->pid);
1147 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1149 thread_atoms_insert(wakee);
1150 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1152 die("wakeup-event: Internal tree error");
1153 add_sched_out_event(atoms, 'S', timestamp);
1156 BUG_ON(list_empty(&atoms->work_list));
1158 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1161 * You WILL be missing events if you've recorded only
1162 * one CPU, or are only looking at only one, so don't
1163 * make useless noise.
1165 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1166 nr_state_machine_bugs++;
1169 if (atom->sched_out_time > timestamp) {
1170 nr_unordered_timestamps++;
1174 atom->state = THREAD_WAIT_CPU;
1175 atom->wake_up_time = timestamp;
1179 latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1180 struct event *__event __used,
1183 struct thread *thread __used)
1185 struct work_atoms *atoms;
1186 struct work_atom *atom;
1187 struct thread *migrant;
1190 * Only need to worry about migration when profiling one CPU.
1192 if (profile_cpu == -1)
1195 migrant = threads__findnew(migrate_task_event->pid);
1196 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1198 thread_atoms_insert(migrant);
1199 register_pid(migrant->pid, migrant->comm);
1200 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1202 die("migration-event: Internal tree error");
1203 add_sched_out_event(atoms, 'R', timestamp);
1206 BUG_ON(list_empty(&atoms->work_list));
1208 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1209 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1213 if (atom->sched_out_time > timestamp)
1214 nr_unordered_timestamps++;
1217 static struct trace_sched_handler lat_ops = {
1218 .wakeup_event = latency_wakeup_event,
1219 .switch_event = latency_switch_event,
1220 .runtime_event = latency_runtime_event,
1221 .fork_event = latency_fork_event,
1222 .migrate_task_event = latency_migrate_task_event,
1225 static void output_lat_thread(struct work_atoms *work_list)
1231 if (!work_list->nb_atoms)
1234 * Ignore idle threads:
1236 if (!strcmp(work_list->thread->comm, "swapper"))
1239 all_runtime += work_list->total_runtime;
1240 all_count += work_list->nb_atoms;
1242 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
1244 for (i = 0; i < 24 - ret; i++)
1247 avg = work_list->total_lat / work_list->nb_atoms;
1249 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
1250 (double)work_list->total_runtime / 1e6,
1251 work_list->nb_atoms, (double)avg / 1e6,
1252 (double)work_list->max_lat / 1e6);
1255 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1257 if (l->thread->pid < r->thread->pid)
1259 if (l->thread->pid > r->thread->pid)
1265 static struct sort_dimension pid_sort_dimension = {
1270 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1280 avgl = l->total_lat / l->nb_atoms;
1281 avgr = r->total_lat / r->nb_atoms;
1291 static struct sort_dimension avg_sort_dimension = {
1296 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1298 if (l->max_lat < r->max_lat)
1300 if (l->max_lat > r->max_lat)
1306 static struct sort_dimension max_sort_dimension = {
1311 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1313 if (l->nb_atoms < r->nb_atoms)
1315 if (l->nb_atoms > r->nb_atoms)
1321 static struct sort_dimension switch_sort_dimension = {
1326 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1328 if (l->total_runtime < r->total_runtime)
1330 if (l->total_runtime > r->total_runtime)
1336 static struct sort_dimension runtime_sort_dimension = {
1341 static struct sort_dimension *available_sorts[] = {
1342 &pid_sort_dimension,
1343 &avg_sort_dimension,
1344 &max_sort_dimension,
1345 &switch_sort_dimension,
1346 &runtime_sort_dimension,
1349 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1351 static LIST_HEAD(sort_list);
1353 static int sort_dimension__add(const char *tok, struct list_head *list)
1357 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1358 if (!strcmp(available_sorts[i]->name, tok)) {
1359 list_add_tail(&available_sorts[i]->list, list);
1368 static void setup_sorting(void);
1370 static void sort_lat(void)
1372 struct rb_node *node;
1375 struct work_atoms *data;
1376 node = rb_first(&atom_root);
1380 rb_erase(node, &atom_root);
1381 data = rb_entry(node, struct work_atoms, node);
1382 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1386 static struct trace_sched_handler *trace_handler;
1389 process_sched_wakeup_event(struct raw_event_sample *raw,
1390 struct event *event,
1392 u64 timestamp __used,
1393 struct thread *thread __used)
1395 struct trace_wakeup_event wakeup_event;
1397 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1399 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1400 FILL_FIELD(wakeup_event, pid, event, raw->data);
1401 FILL_FIELD(wakeup_event, prio, event, raw->data);
1402 FILL_FIELD(wakeup_event, success, event, raw->data);
1403 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1405 if (trace_handler->wakeup_event)
1406 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
1410 * Track the current task - that way we can know whether there's any
1411 * weird events, such as a task being switched away that is not current.
1415 static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1417 static struct thread *curr_thread[MAX_CPUS];
1419 static char next_shortname1 = 'A';
1420 static char next_shortname2 = '0';
1423 map_switch_event(struct trace_switch_event *switch_event,
1424 struct event *event __used,
1427 struct thread *thread __used)
1429 struct thread *sched_out, *sched_in;
1435 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1437 if (this_cpu > max_cpu)
1440 timestamp0 = cpu_last_switched[this_cpu];
1441 cpu_last_switched[this_cpu] = timestamp;
1443 delta = timestamp - timestamp0;
1448 die("hm, delta: %Ld < 0 ?\n", delta);
1451 sched_out = threads__findnew(switch_event->prev_pid);
1452 sched_in = threads__findnew(switch_event->next_pid);
1454 curr_thread[this_cpu] = sched_in;
1459 if (!sched_in->shortname[0]) {
1460 sched_in->shortname[0] = next_shortname1;
1461 sched_in->shortname[1] = next_shortname2;
1463 if (next_shortname1 < 'Z') {
1466 next_shortname1='A';
1467 if (next_shortname2 < '9') {
1470 next_shortname2='0';
1476 for (cpu = 0; cpu <= max_cpu; cpu++) {
1477 if (cpu != this_cpu)
1482 if (curr_thread[cpu]) {
1483 if (curr_thread[cpu]->pid)
1484 printf("%2s ", curr_thread[cpu]->shortname);
1491 printf(" %12.6f secs ", (double)timestamp/1e9);
1492 if (new_shortname) {
1493 printf("%s => %s:%d\n",
1494 sched_in->shortname, sched_in->comm, sched_in->pid);
1502 process_sched_switch_event(struct raw_event_sample *raw,
1503 struct event *event,
1505 u64 timestamp __used,
1506 struct thread *thread __used)
1508 struct trace_switch_event switch_event;
1510 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1512 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1513 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1514 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1515 FILL_FIELD(switch_event, prev_state, event, raw->data);
1516 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1517 FILL_FIELD(switch_event, next_pid, event, raw->data);
1518 FILL_FIELD(switch_event, next_prio, event, raw->data);
1520 if (curr_pid[this_cpu] != (u32)-1) {
1522 * Are we trying to switch away a PID that is
1525 if (curr_pid[this_cpu] != switch_event.prev_pid)
1526 nr_context_switch_bugs++;
1528 if (trace_handler->switch_event)
1529 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
1531 curr_pid[this_cpu] = switch_event.next_pid;
1535 process_sched_runtime_event(struct raw_event_sample *raw,
1536 struct event *event,
1538 u64 timestamp __used,
1539 struct thread *thread __used)
1541 struct trace_runtime_event runtime_event;
1543 FILL_ARRAY(runtime_event, comm, event, raw->data);
1544 FILL_FIELD(runtime_event, pid, event, raw->data);
1545 FILL_FIELD(runtime_event, runtime, event, raw->data);
1546 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1548 if (trace_handler->runtime_event)
1549 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
1553 process_sched_fork_event(struct raw_event_sample *raw,
1554 struct event *event,
1556 u64 timestamp __used,
1557 struct thread *thread __used)
1559 struct trace_fork_event fork_event;
1561 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1563 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1564 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1565 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1566 FILL_FIELD(fork_event, child_pid, event, raw->data);
1568 if (trace_handler->fork_event)
1569 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
1573 process_sched_exit_event(struct event *event,
1575 u64 timestamp __used,
1576 struct thread *thread __used)
1579 printf("sched_exit event %p\n", event);
1583 process_sched_migrate_task_event(struct raw_event_sample *raw,
1584 struct event *event,
1586 u64 timestamp __used,
1587 struct thread *thread __used)
1589 struct trace_migrate_task_event migrate_task_event;
1591 FILL_COMMON_FIELDS(migrate_task_event, event, raw->data);
1593 FILL_ARRAY(migrate_task_event, comm, event, raw->data);
1594 FILL_FIELD(migrate_task_event, pid, event, raw->data);
1595 FILL_FIELD(migrate_task_event, prio, event, raw->data);
1596 FILL_FIELD(migrate_task_event, cpu, event, raw->data);
1598 if (trace_handler->migrate_task_event)
1599 trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
1603 process_raw_event(event_t *raw_event __used, void *more_data,
1604 int cpu, u64 timestamp, struct thread *thread)
1606 struct raw_event_sample *raw = more_data;
1607 struct event *event;
1610 type = trace_parse_common_type(raw->data);
1611 event = trace_find_event(type);
1613 if (!strcmp(event->name, "sched_switch"))
1614 process_sched_switch_event(raw, event, cpu, timestamp, thread);
1615 if (!strcmp(event->name, "sched_stat_runtime"))
1616 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
1617 if (!strcmp(event->name, "sched_wakeup"))
1618 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1619 if (!strcmp(event->name, "sched_wakeup_new"))
1620 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1621 if (!strcmp(event->name, "sched_process_fork"))
1622 process_sched_fork_event(raw, event, cpu, timestamp, thread);
1623 if (!strcmp(event->name, "sched_process_exit"))
1624 process_sched_exit_event(event, cpu, timestamp, thread);
1625 if (!strcmp(event->name, "sched_migrate_task"))
1626 process_sched_migrate_task_event(raw, event, cpu, timestamp, thread);
1630 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1632 struct thread *thread;
1633 u64 ip = event->ip.ip;
1637 void *more_data = event->ip.__more_data;
1639 if (!(sample_type & PERF_SAMPLE_RAW))
1642 thread = threads__findnew(event->ip.pid);
1644 if (sample_type & PERF_SAMPLE_TIME) {
1645 timestamp = *(u64 *)more_data;
1646 more_data += sizeof(u64);
1649 if (sample_type & PERF_SAMPLE_CPU) {
1650 cpu = *(u32 *)more_data;
1651 more_data += sizeof(u32);
1652 more_data += sizeof(u32); /* reserved */
1655 if (sample_type & PERF_SAMPLE_PERIOD) {
1656 period = *(u64 *)more_data;
1657 more_data += sizeof(u64);
1660 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1661 (void *)(offset + head),
1662 (void *)(long)(event->header.size),
1664 event->ip.pid, event->ip.tid,
1668 if (thread == NULL) {
1669 pr_debug("problem processing %d event, skipping it.\n",
1670 event->header.type);
1674 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1676 if (profile_cpu != -1 && profile_cpu != (int) cpu)
1679 process_raw_event(event, more_data, cpu, timestamp, thread);
1685 process_lost_event(event_t *event __used,
1686 unsigned long offset __used,
1687 unsigned long head __used)
1690 nr_lost_events += event->lost.lost;
1695 static int sample_type_check(u64 type)
1699 if (!(sample_type & PERF_SAMPLE_RAW)) {
1701 "No trace sample to read. Did you call perf record "
1709 static struct perf_file_handler file_handler = {
1710 .process_sample_event = process_sample_event,
1711 .process_comm_event = process_comm_event,
1712 .process_lost_event = process_lost_event,
1713 .sample_type_check = sample_type_check,
1716 static int read_events(void)
1718 register_idle_thread();
1719 register_perf_file_handler(&file_handler);
1721 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
1724 static void print_bad_events(void)
1726 if (nr_unordered_timestamps && nr_timestamps) {
1727 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1728 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1729 nr_unordered_timestamps, nr_timestamps);
1731 if (nr_lost_events && nr_events) {
1732 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1733 (double)nr_lost_events/(double)nr_events*100.0,
1734 nr_lost_events, nr_events, nr_lost_chunks);
1736 if (nr_state_machine_bugs && nr_timestamps) {
1737 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1738 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1739 nr_state_machine_bugs, nr_timestamps);
1741 printf(" (due to lost events?)");
1744 if (nr_context_switch_bugs && nr_timestamps) {
1745 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1746 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1747 nr_context_switch_bugs, nr_timestamps);
1749 printf(" (due to lost events?)");
1754 static void __cmd_lat(void)
1756 struct rb_node *next;
1762 printf("\n -----------------------------------------------------------------------------------------\n");
1763 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1764 printf(" -----------------------------------------------------------------------------------------\n");
1766 next = rb_first(&sorted_atom_root);
1769 struct work_atoms *work_list;
1771 work_list = rb_entry(next, struct work_atoms, node);
1772 output_lat_thread(work_list);
1773 next = rb_next(next);
1776 printf(" -----------------------------------------------------------------------------------------\n");
1777 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1778 (double)all_runtime/1e6, all_count);
1780 printf(" ---------------------------------------------------\n");
1787 static struct trace_sched_handler map_ops = {
1788 .wakeup_event = NULL,
1789 .switch_event = map_switch_event,
1790 .runtime_event = NULL,
1794 static void __cmd_map(void)
1796 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1803 static void __cmd_replay(void)
1807 calibrate_run_measurement_overhead();
1808 calibrate_sleep_measurement_overhead();
1810 test_calibrations();
1814 printf("nr_run_events: %ld\n", nr_run_events);
1815 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1816 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1818 if (targetless_wakeups)
1819 printf("target-less wakeups: %ld\n", targetless_wakeups);
1820 if (multitarget_wakeups)
1821 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1822 if (nr_run_events_optimized)
1823 printf("run atoms optimized: %ld\n",
1824 nr_run_events_optimized);
1826 print_task_traces();
1827 add_cross_task_wakeups();
1830 printf("------------------------------------------------------------\n");
1831 for (i = 0; i < replay_repeat; i++)
1836 static const char * const sched_usage[] = {
1837 "perf sched [<options>] {record|latency|map|replay|trace}",
1841 static const struct option sched_options[] = {
1842 OPT_STRING('i', "input", &input_name, "file",
1844 OPT_BOOLEAN('v', "verbose", &verbose,
1845 "be more verbose (show symbol address, etc)"),
1846 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1847 "dump raw trace in ASCII"),
1851 static const char * const latency_usage[] = {
1852 "perf sched latency [<options>]",
1856 static const struct option latency_options[] = {
1857 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1858 "sort by key(s): runtime, switch, avg, max"),
1859 OPT_BOOLEAN('v', "verbose", &verbose,
1860 "be more verbose (show symbol address, etc)"),
1861 OPT_INTEGER('C', "CPU", &profile_cpu,
1862 "CPU to profile on"),
1863 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1864 "dump raw trace in ASCII"),
1868 static const char * const replay_usage[] = {
1869 "perf sched replay [<options>]",
1873 static const struct option replay_options[] = {
1874 OPT_INTEGER('r', "repeat", &replay_repeat,
1875 "repeat the workload replay N times (-1: infinite)"),
1876 OPT_BOOLEAN('v', "verbose", &verbose,
1877 "be more verbose (show symbol address, etc)"),
1878 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1879 "dump raw trace in ASCII"),
1883 static void setup_sorting(void)
1885 char *tmp, *tok, *str = strdup(sort_order);
1887 for (tok = strtok_r(str, ", ", &tmp);
1888 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1889 if (sort_dimension__add(tok, &sort_list) < 0) {
1890 error("Unknown --sort key: `%s'", tok);
1891 usage_with_options(latency_usage, latency_options);
1897 sort_dimension__add("pid", &cmp_pid);
1900 static const char *record_args[] = {
1908 "-e", "sched:sched_switch:r",
1909 "-e", "sched:sched_stat_wait:r",
1910 "-e", "sched:sched_stat_sleep:r",
1911 "-e", "sched:sched_stat_iowait:r",
1912 "-e", "sched:sched_stat_runtime:r",
1913 "-e", "sched:sched_process_exit:r",
1914 "-e", "sched:sched_process_fork:r",
1915 "-e", "sched:sched_wakeup:r",
1916 "-e", "sched:sched_migrate_task:r",
1919 static int __cmd_record(int argc, const char **argv)
1921 unsigned int rec_argc, i, j;
1922 const char **rec_argv;
1924 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1925 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1927 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1928 rec_argv[i] = strdup(record_args[i]);
1930 for (j = 1; j < (unsigned int)argc; j++, i++)
1931 rec_argv[i] = argv[j];
1933 BUG_ON(i != rec_argc);
1935 return cmd_record(i, rec_argv, NULL);
1938 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1942 argc = parse_options(argc, argv, sched_options, sched_usage,
1943 PARSE_OPT_STOP_AT_NON_OPTION);
1945 usage_with_options(sched_usage, sched_options);
1947 if (!strncmp(argv[0], "rec", 3)) {
1948 return __cmd_record(argc, argv);
1949 } else if (!strncmp(argv[0], "lat", 3)) {
1950 trace_handler = &lat_ops;
1952 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1954 usage_with_options(latency_usage, latency_options);
1958 } else if (!strcmp(argv[0], "map")) {
1959 trace_handler = &map_ops;
1962 } else if (!strncmp(argv[0], "rep", 3)) {
1963 trace_handler = &replay_ops;
1965 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1967 usage_with_options(replay_usage, replay_options);
1970 } else if (!strcmp(argv[0], "trace")) {
1972 * Aliased to 'perf trace' for now:
1974 return cmd_trace(argc, argv, prefix);
1976 usage_with_options(sched_usage, sched_options);