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 #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 {
77 SCHED_EVENT_MIGRATION,
81 enum sched_event_type type;
87 struct task_desc *wakee;
90 static struct task_desc *pid_to_task[MAX_PID];
92 static struct task_desc **tasks;
94 static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER;
95 static u64 start_time;
97 static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
99 static unsigned long nr_run_events;
100 static unsigned long nr_sleep_events;
101 static unsigned long nr_wakeup_events;
103 static unsigned long nr_sleep_corrections;
104 static unsigned long nr_run_events_optimized;
106 static unsigned long targetless_wakeups;
107 static unsigned long multitarget_wakeups;
109 static u64 cpu_usage;
110 static u64 runavg_cpu_usage;
111 static u64 parent_cpu_usage;
112 static u64 runavg_parent_cpu_usage;
114 static unsigned long nr_runs;
115 static u64 sum_runtime;
116 static u64 sum_fluct;
119 static unsigned long replay_repeat = 10;
120 static unsigned long nr_timestamps;
121 static unsigned long nr_unordered_timestamps;
122 static unsigned long nr_state_machine_bugs;
123 static unsigned long nr_context_switch_bugs;
124 static unsigned long nr_events;
125 static unsigned long nr_lost_chunks;
126 static unsigned long nr_lost_events;
128 #define TASK_STATE_TO_CHAR_STR "RSDTtZX"
138 struct list_head list;
139 enum thread_state state;
147 struct list_head work_list;
148 struct thread *thread;
156 typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *);
158 static struct rb_root atom_root, sorted_atom_root;
160 static u64 all_runtime;
161 static u64 all_count;
164 static u64 get_nsecs(void)
168 clock_gettime(CLOCK_MONOTONIC, &ts);
170 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
173 static void burn_nsecs(u64 nsecs)
175 u64 T0 = get_nsecs(), T1;
179 } while (T1 + run_measurement_overhead < T0 + nsecs);
182 static void sleep_nsecs(u64 nsecs)
186 ts.tv_nsec = nsecs % 999999999;
187 ts.tv_sec = nsecs / 999999999;
189 nanosleep(&ts, NULL);
192 static void calibrate_run_measurement_overhead(void)
194 u64 T0, T1, delta, min_delta = 1000000000ULL;
197 for (i = 0; i < 10; i++) {
202 min_delta = min(min_delta, delta);
204 run_measurement_overhead = min_delta;
206 printf("run measurement overhead: %Ld nsecs\n", min_delta);
209 static void calibrate_sleep_measurement_overhead(void)
211 u64 T0, T1, delta, min_delta = 1000000000ULL;
214 for (i = 0; i < 10; i++) {
219 min_delta = min(min_delta, delta);
222 sleep_measurement_overhead = min_delta;
224 printf("sleep measurement overhead: %Ld nsecs\n", min_delta);
227 static struct sched_atom *
228 get_new_event(struct task_desc *task, u64 timestamp)
230 struct sched_atom *event = calloc(1, sizeof(*event));
231 unsigned long idx = task->nr_events;
234 event->timestamp = timestamp;
238 size = sizeof(struct sched_atom *) * task->nr_events;
239 task->atoms = realloc(task->atoms, size);
240 BUG_ON(!task->atoms);
242 task->atoms[idx] = event;
247 static struct sched_atom *last_event(struct task_desc *task)
249 if (!task->nr_events)
252 return task->atoms[task->nr_events - 1];
256 add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration)
258 struct sched_atom *event, *curr_event = last_event(task);
261 * optimize an existing RUN event by merging this one
264 if (curr_event && curr_event->type == SCHED_EVENT_RUN) {
265 nr_run_events_optimized++;
266 curr_event->duration += duration;
270 event = get_new_event(task, timestamp);
272 event->type = SCHED_EVENT_RUN;
273 event->duration = duration;
279 add_sched_event_wakeup(struct task_desc *task, u64 timestamp,
280 struct task_desc *wakee)
282 struct sched_atom *event, *wakee_event;
284 event = get_new_event(task, timestamp);
285 event->type = SCHED_EVENT_WAKEUP;
286 event->wakee = wakee;
288 wakee_event = last_event(wakee);
289 if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) {
290 targetless_wakeups++;
293 if (wakee_event->wait_sem) {
294 multitarget_wakeups++;
298 wakee_event->wait_sem = calloc(1, sizeof(*wakee_event->wait_sem));
299 sem_init(wakee_event->wait_sem, 0, 0);
300 wakee_event->specific_wait = 1;
301 event->wait_sem = wakee_event->wait_sem;
307 add_sched_event_sleep(struct task_desc *task, u64 timestamp,
308 u64 task_state __used)
310 struct sched_atom *event = get_new_event(task, timestamp);
312 event->type = SCHED_EVENT_SLEEP;
317 static struct task_desc *register_pid(unsigned long pid, const char *comm)
319 struct task_desc *task;
321 BUG_ON(pid >= MAX_PID);
323 task = pid_to_task[pid];
328 task = calloc(1, sizeof(*task));
331 strcpy(task->comm, comm);
333 * every task starts in sleeping state - this gets ignored
334 * if there's no wakeup pointing to this sleep state:
336 add_sched_event_sleep(task, 0, 0);
338 pid_to_task[pid] = task;
340 tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *));
342 tasks[task->nr] = task;
345 printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm);
351 static void print_task_traces(void)
353 struct task_desc *task;
356 for (i = 0; i < nr_tasks; i++) {
358 printf("task %6ld (%20s:%10ld), nr_events: %ld\n",
359 task->nr, task->comm, task->pid, task->nr_events);
363 static void add_cross_task_wakeups(void)
365 struct task_desc *task1, *task2;
368 for (i = 0; i < nr_tasks; i++) {
374 add_sched_event_wakeup(task1, 0, task2);
379 process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom)
386 delta = start_time + atom->timestamp - now;
388 switch (atom->type) {
389 case SCHED_EVENT_RUN:
390 burn_nsecs(atom->duration);
392 case SCHED_EVENT_SLEEP:
394 ret = sem_wait(atom->wait_sem);
397 case SCHED_EVENT_WAKEUP:
399 ret = sem_post(atom->wait_sem);
402 case SCHED_EVENT_MIGRATION:
409 static u64 get_cpu_usage_nsec_parent(void)
415 err = getrusage(RUSAGE_SELF, &ru);
418 sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3;
419 sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3;
424 static u64 get_cpu_usage_nsec_self(void)
426 char filename [] = "/proc/1234567890/sched";
427 unsigned long msecs, nsecs;
435 sprintf(filename, "/proc/%d/sched", getpid());
436 file = fopen(filename, "r");
439 while ((chars = getline(&line, &len, file)) != -1) {
440 ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
443 total = msecs*1e6 + nsecs;
454 static void *thread_func(void *ctx)
456 struct task_desc *this_task = ctx;
457 u64 cpu_usage_0, cpu_usage_1;
458 unsigned long i, ret;
461 sprintf(comm2, ":%s", this_task->comm);
462 prctl(PR_SET_NAME, comm2);
465 ret = sem_post(&this_task->ready_for_work);
467 ret = pthread_mutex_lock(&start_work_mutex);
469 ret = pthread_mutex_unlock(&start_work_mutex);
472 cpu_usage_0 = get_cpu_usage_nsec_self();
474 for (i = 0; i < this_task->nr_events; i++) {
475 this_task->curr_event = i;
476 process_sched_event(this_task, this_task->atoms[i]);
479 cpu_usage_1 = get_cpu_usage_nsec_self();
480 this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
482 ret = sem_post(&this_task->work_done_sem);
485 ret = pthread_mutex_lock(&work_done_wait_mutex);
487 ret = pthread_mutex_unlock(&work_done_wait_mutex);
493 static void create_tasks(void)
495 struct task_desc *task;
500 err = pthread_attr_init(&attr);
502 err = pthread_attr_setstacksize(&attr, (size_t)(16*1024));
504 err = pthread_mutex_lock(&start_work_mutex);
506 err = pthread_mutex_lock(&work_done_wait_mutex);
508 for (i = 0; i < nr_tasks; i++) {
510 sem_init(&task->sleep_sem, 0, 0);
511 sem_init(&task->ready_for_work, 0, 0);
512 sem_init(&task->work_done_sem, 0, 0);
513 task->curr_event = 0;
514 err = pthread_create(&task->thread, &attr, thread_func, task);
519 static void wait_for_tasks(void)
521 u64 cpu_usage_0, cpu_usage_1;
522 struct task_desc *task;
523 unsigned long i, ret;
525 start_time = get_nsecs();
527 pthread_mutex_unlock(&work_done_wait_mutex);
529 for (i = 0; i < nr_tasks; i++) {
531 ret = sem_wait(&task->ready_for_work);
533 sem_init(&task->ready_for_work, 0, 0);
535 ret = pthread_mutex_lock(&work_done_wait_mutex);
538 cpu_usage_0 = get_cpu_usage_nsec_parent();
540 pthread_mutex_unlock(&start_work_mutex);
542 for (i = 0; i < nr_tasks; i++) {
544 ret = sem_wait(&task->work_done_sem);
546 sem_init(&task->work_done_sem, 0, 0);
547 cpu_usage += task->cpu_usage;
551 cpu_usage_1 = get_cpu_usage_nsec_parent();
552 if (!runavg_cpu_usage)
553 runavg_cpu_usage = cpu_usage;
554 runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10;
556 parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
557 if (!runavg_parent_cpu_usage)
558 runavg_parent_cpu_usage = parent_cpu_usage;
559 runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 +
560 parent_cpu_usage)/10;
562 ret = pthread_mutex_lock(&start_work_mutex);
565 for (i = 0; i < nr_tasks; i++) {
567 sem_init(&task->sleep_sem, 0, 0);
568 task->curr_event = 0;
572 static void run_one_test(void)
574 u64 T0, T1, delta, avg_delta, fluct, std_dev;
581 sum_runtime += delta;
584 avg_delta = sum_runtime / nr_runs;
585 if (delta < avg_delta)
586 fluct = avg_delta - delta;
588 fluct = delta - avg_delta;
590 std_dev = sum_fluct / nr_runs / sqrt(nr_runs);
593 run_avg = (run_avg*9 + delta)/10;
595 printf("#%-3ld: %0.3f, ",
596 nr_runs, (double)delta/1000000.0);
598 printf("ravg: %0.2f, ",
599 (double)run_avg/1e6);
601 printf("cpu: %0.2f / %0.2f",
602 (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6);
606 * rusage statistics done by the parent, these are less
607 * accurate than the sum_exec_runtime based statistics:
609 printf(" [%0.2f / %0.2f]",
610 (double)parent_cpu_usage/1e6,
611 (double)runavg_parent_cpu_usage/1e6);
616 if (nr_sleep_corrections)
617 printf(" (%ld sleep corrections)\n", nr_sleep_corrections);
618 nr_sleep_corrections = 0;
621 static void test_calibrations(void)
629 printf("the run test took %Ld nsecs\n", T1-T0);
635 printf("the sleep test took %Ld nsecs\n", T1-T0);
639 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
641 struct thread *thread = threads__findnew(event->comm.tid);
643 dump_printf("%p [%p]: perf_event_comm: %s:%d\n",
644 (void *)(offset + head),
645 (void *)(long)(event->header.size),
646 event->comm.comm, event->comm.pid);
648 if (thread == NULL ||
649 thread__set_comm(thread, event->comm.comm)) {
650 dump_printf("problem processing perf_event_comm, skipping event.\n");
659 struct raw_event_sample {
664 #define FILL_FIELD(ptr, field, event, data) \
665 ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data)
667 #define FILL_ARRAY(ptr, array, event, data) \
669 void *__array = raw_field_ptr(event, #array, data); \
670 memcpy(ptr.array, __array, sizeof(ptr.array)); \
673 #define FILL_COMMON_FIELDS(ptr, event, data) \
675 FILL_FIELD(ptr, common_type, event, data); \
676 FILL_FIELD(ptr, common_flags, event, data); \
677 FILL_FIELD(ptr, common_preempt_count, event, data); \
678 FILL_FIELD(ptr, common_pid, event, data); \
679 FILL_FIELD(ptr, common_tgid, event, data); \
684 struct trace_switch_event {
689 u8 common_preempt_count;
702 struct trace_runtime_event {
707 u8 common_preempt_count;
717 struct trace_wakeup_event {
722 u8 common_preempt_count;
734 struct trace_fork_event {
739 u8 common_preempt_count;
743 char parent_comm[16];
749 struct trace_migrate_task_event {
754 u8 common_preempt_count;
765 struct trace_sched_handler {
766 void (*switch_event)(struct trace_switch_event *,
770 struct thread *thread);
772 void (*runtime_event)(struct trace_runtime_event *,
776 struct thread *thread);
778 void (*wakeup_event)(struct trace_wakeup_event *,
782 struct thread *thread);
784 void (*fork_event)(struct trace_fork_event *,
788 struct thread *thread);
790 void (*migrate_task_event)(struct trace_migrate_task_event *,
794 struct thread *thread);
799 replay_wakeup_event(struct trace_wakeup_event *wakeup_event,
802 u64 timestamp __used,
803 struct thread *thread __used)
805 struct task_desc *waker, *wakee;
808 printf("sched_wakeup event %p\n", event);
810 printf(" ... pid %d woke up %s/%d\n",
811 wakeup_event->common_pid,
816 waker = register_pid(wakeup_event->common_pid, "<unknown>");
817 wakee = register_pid(wakeup_event->pid, wakeup_event->comm);
819 add_sched_event_wakeup(waker, timestamp, wakee);
822 static u64 cpu_last_switched[MAX_CPUS];
825 replay_switch_event(struct trace_switch_event *switch_event,
829 struct thread *thread __used)
831 struct task_desc *prev, *next;
836 printf("sched_switch event %p\n", event);
838 if (cpu >= MAX_CPUS || cpu < 0)
841 timestamp0 = cpu_last_switched[cpu];
843 delta = timestamp - timestamp0;
848 die("hm, delta: %Ld < 0 ?\n", delta);
851 printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n",
852 switch_event->prev_comm, switch_event->prev_pid,
853 switch_event->next_comm, switch_event->next_pid,
857 prev = register_pid(switch_event->prev_pid, switch_event->prev_comm);
858 next = register_pid(switch_event->next_pid, switch_event->next_comm);
860 cpu_last_switched[cpu] = timestamp;
862 add_sched_event_run(prev, timestamp, delta);
863 add_sched_event_sleep(prev, timestamp, switch_event->prev_state);
868 replay_fork_event(struct trace_fork_event *fork_event,
871 u64 timestamp __used,
872 struct thread *thread __used)
875 printf("sched_fork event %p\n", event);
876 printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid);
877 printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid);
879 register_pid(fork_event->parent_pid, fork_event->parent_comm);
880 register_pid(fork_event->child_pid, fork_event->child_comm);
883 static struct trace_sched_handler replay_ops = {
884 .wakeup_event = replay_wakeup_event,
885 .switch_event = replay_switch_event,
886 .fork_event = replay_fork_event,
889 struct sort_dimension {
892 struct list_head list;
895 static LIST_HEAD(cmp_pid);
898 thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r)
900 struct sort_dimension *sort;
903 BUG_ON(list_empty(list));
905 list_for_each_entry(sort, list, list) {
906 ret = sort->cmp(l, r);
914 static struct work_atoms *
915 thread_atoms_search(struct rb_root *root, struct thread *thread,
916 struct list_head *sort_list)
918 struct rb_node *node = root->rb_node;
919 struct work_atoms key = { .thread = thread };
922 struct work_atoms *atoms;
925 atoms = container_of(node, struct work_atoms, node);
927 cmp = thread_lat_cmp(sort_list, &key, atoms);
929 node = node->rb_left;
931 node = node->rb_right;
933 BUG_ON(thread != atoms->thread);
941 __thread_latency_insert(struct rb_root *root, struct work_atoms *data,
942 struct list_head *sort_list)
944 struct rb_node **new = &(root->rb_node), *parent = NULL;
947 struct work_atoms *this;
950 this = container_of(*new, struct work_atoms, node);
953 cmp = thread_lat_cmp(sort_list, data, this);
956 new = &((*new)->rb_left);
958 new = &((*new)->rb_right);
961 rb_link_node(&data->node, parent, new);
962 rb_insert_color(&data->node, root);
965 static void thread_atoms_insert(struct thread *thread)
967 struct work_atoms *atoms;
969 atoms = calloc(sizeof(*atoms), 1);
973 atoms->thread = thread;
974 INIT_LIST_HEAD(&atoms->work_list);
975 __thread_latency_insert(&atom_root, atoms, &cmp_pid);
979 latency_fork_event(struct trace_fork_event *fork_event __used,
980 struct event *event __used,
982 u64 timestamp __used,
983 struct thread *thread __used)
985 /* should insert the newcomer */
989 static char sched_out_state(struct trace_switch_event *switch_event)
991 const char *str = TASK_STATE_TO_CHAR_STR;
993 return str[switch_event->prev_state];
997 add_sched_out_event(struct work_atoms *atoms,
1001 struct work_atom *atom;
1003 atom = calloc(sizeof(*atom), 1);
1007 atom->sched_out_time = timestamp;
1009 if (run_state == 'R') {
1010 atom->state = THREAD_WAIT_CPU;
1011 atom->wake_up_time = atom->sched_out_time;
1014 list_add_tail(&atom->list, &atoms->work_list);
1018 add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used)
1020 struct work_atom *atom;
1022 BUG_ON(list_empty(&atoms->work_list));
1024 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1026 atom->runtime += delta;
1027 atoms->total_runtime += delta;
1031 add_sched_in_event(struct work_atoms *atoms, u64 timestamp)
1033 struct work_atom *atom;
1036 if (list_empty(&atoms->work_list))
1039 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1041 if (atom->state != THREAD_WAIT_CPU)
1044 if (timestamp < atom->wake_up_time) {
1045 atom->state = THREAD_IGNORE;
1049 atom->state = THREAD_SCHED_IN;
1050 atom->sched_in_time = timestamp;
1052 delta = atom->sched_in_time - atom->wake_up_time;
1053 atoms->total_lat += delta;
1054 if (delta > atoms->max_lat)
1055 atoms->max_lat = delta;
1060 latency_switch_event(struct trace_switch_event *switch_event,
1061 struct event *event __used,
1064 struct thread *thread __used)
1066 struct work_atoms *out_events, *in_events;
1067 struct thread *sched_out, *sched_in;
1071 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1073 timestamp0 = cpu_last_switched[cpu];
1074 cpu_last_switched[cpu] = timestamp;
1076 delta = timestamp - timestamp0;
1081 die("hm, delta: %Ld < 0 ?\n", delta);
1084 sched_out = threads__findnew(switch_event->prev_pid);
1085 sched_in = threads__findnew(switch_event->next_pid);
1087 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1089 thread_atoms_insert(sched_out);
1090 out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid);
1092 die("out-event: Internal tree error");
1094 add_sched_out_event(out_events, sched_out_state(switch_event), timestamp);
1096 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1098 thread_atoms_insert(sched_in);
1099 in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid);
1101 die("in-event: Internal tree error");
1103 * Take came in we have not heard about yet,
1104 * add in an initial atom in runnable state:
1106 add_sched_out_event(in_events, 'R', timestamp);
1108 add_sched_in_event(in_events, timestamp);
1112 latency_runtime_event(struct trace_runtime_event *runtime_event,
1113 struct event *event __used,
1116 struct thread *this_thread __used)
1118 struct thread *thread = threads__findnew(runtime_event->pid);
1119 struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1121 BUG_ON(cpu >= MAX_CPUS || cpu < 0);
1123 thread_atoms_insert(thread);
1124 atoms = thread_atoms_search(&atom_root, thread, &cmp_pid);
1126 die("in-event: Internal tree error");
1127 add_sched_out_event(atoms, 'R', timestamp);
1130 add_runtime_event(atoms, runtime_event->runtime, timestamp);
1134 latency_wakeup_event(struct trace_wakeup_event *wakeup_event,
1135 struct event *__event __used,
1138 struct thread *thread __used)
1140 struct work_atoms *atoms;
1141 struct work_atom *atom;
1142 struct thread *wakee;
1144 /* Note for later, it may be interesting to observe the failing cases */
1145 if (!wakeup_event->success)
1148 wakee = threads__findnew(wakeup_event->pid);
1149 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1151 thread_atoms_insert(wakee);
1152 atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid);
1154 die("wakeup-event: Internal tree error");
1155 add_sched_out_event(atoms, 'S', timestamp);
1158 BUG_ON(list_empty(&atoms->work_list));
1160 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1163 * You WILL be missing events if you've recorded only
1164 * one CPU, or are only looking at only one, so don't
1165 * make useless noise.
1167 if (profile_cpu == -1 && atom->state != THREAD_SLEEPING)
1168 nr_state_machine_bugs++;
1171 if (atom->sched_out_time > timestamp) {
1172 nr_unordered_timestamps++;
1176 atom->state = THREAD_WAIT_CPU;
1177 atom->wake_up_time = timestamp;
1181 latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event,
1182 struct event *__event __used,
1185 struct thread *thread __used)
1187 struct work_atoms *atoms;
1188 struct work_atom *atom;
1189 struct thread *migrant;
1192 * Only need to worry about migration when profiling one CPU.
1194 if (profile_cpu == -1)
1197 migrant = threads__findnew(migrate_task_event->pid);
1198 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1200 thread_atoms_insert(migrant);
1201 register_pid(migrant->pid, migrant->comm);
1202 atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid);
1204 die("migration-event: Internal tree error");
1205 add_sched_out_event(atoms, 'R', timestamp);
1208 BUG_ON(list_empty(&atoms->work_list));
1210 atom = list_entry(atoms->work_list.prev, struct work_atom, list);
1211 atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp;
1215 if (atom->sched_out_time > timestamp)
1216 nr_unordered_timestamps++;
1219 static struct trace_sched_handler lat_ops = {
1220 .wakeup_event = latency_wakeup_event,
1221 .switch_event = latency_switch_event,
1222 .runtime_event = latency_runtime_event,
1223 .fork_event = latency_fork_event,
1224 .migrate_task_event = latency_migrate_task_event,
1227 static void output_lat_thread(struct work_atoms *work_list)
1233 if (!work_list->nb_atoms)
1236 * Ignore idle threads:
1238 if (!strcmp(work_list->thread->comm, "swapper"))
1241 all_runtime += work_list->total_runtime;
1242 all_count += work_list->nb_atoms;
1244 ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid);
1246 for (i = 0; i < 24 - ret; i++)
1249 avg = work_list->total_lat / work_list->nb_atoms;
1251 printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n",
1252 (double)work_list->total_runtime / 1e6,
1253 work_list->nb_atoms, (double)avg / 1e6,
1254 (double)work_list->max_lat / 1e6);
1257 static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
1259 if (l->thread->pid < r->thread->pid)
1261 if (l->thread->pid > r->thread->pid)
1267 static struct sort_dimension pid_sort_dimension = {
1272 static int avg_cmp(struct work_atoms *l, struct work_atoms *r)
1282 avgl = l->total_lat / l->nb_atoms;
1283 avgr = r->total_lat / r->nb_atoms;
1293 static struct sort_dimension avg_sort_dimension = {
1298 static int max_cmp(struct work_atoms *l, struct work_atoms *r)
1300 if (l->max_lat < r->max_lat)
1302 if (l->max_lat > r->max_lat)
1308 static struct sort_dimension max_sort_dimension = {
1313 static int switch_cmp(struct work_atoms *l, struct work_atoms *r)
1315 if (l->nb_atoms < r->nb_atoms)
1317 if (l->nb_atoms > r->nb_atoms)
1323 static struct sort_dimension switch_sort_dimension = {
1328 static int runtime_cmp(struct work_atoms *l, struct work_atoms *r)
1330 if (l->total_runtime < r->total_runtime)
1332 if (l->total_runtime > r->total_runtime)
1338 static struct sort_dimension runtime_sort_dimension = {
1343 static struct sort_dimension *available_sorts[] = {
1344 &pid_sort_dimension,
1345 &avg_sort_dimension,
1346 &max_sort_dimension,
1347 &switch_sort_dimension,
1348 &runtime_sort_dimension,
1351 #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *))
1353 static LIST_HEAD(sort_list);
1355 static int sort_dimension__add(const char *tok, struct list_head *list)
1359 for (i = 0; i < NB_AVAILABLE_SORTS; i++) {
1360 if (!strcmp(available_sorts[i]->name, tok)) {
1361 list_add_tail(&available_sorts[i]->list, list);
1370 static void setup_sorting(void);
1372 static void sort_lat(void)
1374 struct rb_node *node;
1377 struct work_atoms *data;
1378 node = rb_first(&atom_root);
1382 rb_erase(node, &atom_root);
1383 data = rb_entry(node, struct work_atoms, node);
1384 __thread_latency_insert(&sorted_atom_root, data, &sort_list);
1388 static struct trace_sched_handler *trace_handler;
1391 process_sched_wakeup_event(struct raw_event_sample *raw,
1392 struct event *event,
1394 u64 timestamp __used,
1395 struct thread *thread __used)
1397 struct trace_wakeup_event wakeup_event;
1399 FILL_COMMON_FIELDS(wakeup_event, event, raw->data);
1401 FILL_ARRAY(wakeup_event, comm, event, raw->data);
1402 FILL_FIELD(wakeup_event, pid, event, raw->data);
1403 FILL_FIELD(wakeup_event, prio, event, raw->data);
1404 FILL_FIELD(wakeup_event, success, event, raw->data);
1405 FILL_FIELD(wakeup_event, cpu, event, raw->data);
1407 if (trace_handler->wakeup_event)
1408 trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread);
1412 * Track the current task - that way we can know whether there's any
1413 * weird events, such as a task being switched away that is not current.
1417 static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 };
1419 static struct thread *curr_thread[MAX_CPUS];
1421 static char next_shortname1 = 'A';
1422 static char next_shortname2 = '0';
1425 map_switch_event(struct trace_switch_event *switch_event,
1426 struct event *event __used,
1429 struct thread *thread __used)
1431 struct thread *sched_out, *sched_in;
1437 BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0);
1439 if (this_cpu > max_cpu)
1442 timestamp0 = cpu_last_switched[this_cpu];
1443 cpu_last_switched[this_cpu] = timestamp;
1445 delta = timestamp - timestamp0;
1450 die("hm, delta: %Ld < 0 ?\n", delta);
1453 sched_out = threads__findnew(switch_event->prev_pid);
1454 sched_in = threads__findnew(switch_event->next_pid);
1456 curr_thread[this_cpu] = sched_in;
1461 if (!sched_in->shortname[0]) {
1462 sched_in->shortname[0] = next_shortname1;
1463 sched_in->shortname[1] = next_shortname2;
1465 if (next_shortname1 < 'Z') {
1468 next_shortname1='A';
1469 if (next_shortname2 < '9') {
1472 next_shortname2='0';
1478 for (cpu = 0; cpu <= max_cpu; cpu++) {
1479 if (cpu != this_cpu)
1484 if (curr_thread[cpu]) {
1485 if (curr_thread[cpu]->pid)
1486 printf("%2s ", curr_thread[cpu]->shortname);
1493 printf(" %12.6f secs ", (double)timestamp/1e9);
1494 if (new_shortname) {
1495 printf("%s => %s:%d\n",
1496 sched_in->shortname, sched_in->comm, sched_in->pid);
1504 process_sched_switch_event(struct raw_event_sample *raw,
1505 struct event *event,
1507 u64 timestamp __used,
1508 struct thread *thread __used)
1510 struct trace_switch_event switch_event;
1512 FILL_COMMON_FIELDS(switch_event, event, raw->data);
1514 FILL_ARRAY(switch_event, prev_comm, event, raw->data);
1515 FILL_FIELD(switch_event, prev_pid, event, raw->data);
1516 FILL_FIELD(switch_event, prev_prio, event, raw->data);
1517 FILL_FIELD(switch_event, prev_state, event, raw->data);
1518 FILL_ARRAY(switch_event, next_comm, event, raw->data);
1519 FILL_FIELD(switch_event, next_pid, event, raw->data);
1520 FILL_FIELD(switch_event, next_prio, event, raw->data);
1522 if (curr_pid[this_cpu] != (u32)-1) {
1524 * Are we trying to switch away a PID that is
1527 if (curr_pid[this_cpu] != switch_event.prev_pid)
1528 nr_context_switch_bugs++;
1530 if (trace_handler->switch_event)
1531 trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread);
1533 curr_pid[this_cpu] = switch_event.next_pid;
1537 process_sched_runtime_event(struct raw_event_sample *raw,
1538 struct event *event,
1540 u64 timestamp __used,
1541 struct thread *thread __used)
1543 struct trace_runtime_event runtime_event;
1545 FILL_ARRAY(runtime_event, comm, event, raw->data);
1546 FILL_FIELD(runtime_event, pid, event, raw->data);
1547 FILL_FIELD(runtime_event, runtime, event, raw->data);
1548 FILL_FIELD(runtime_event, vruntime, event, raw->data);
1550 if (trace_handler->runtime_event)
1551 trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread);
1555 process_sched_fork_event(struct raw_event_sample *raw,
1556 struct event *event,
1558 u64 timestamp __used,
1559 struct thread *thread __used)
1561 struct trace_fork_event fork_event;
1563 FILL_COMMON_FIELDS(fork_event, event, raw->data);
1565 FILL_ARRAY(fork_event, parent_comm, event, raw->data);
1566 FILL_FIELD(fork_event, parent_pid, event, raw->data);
1567 FILL_ARRAY(fork_event, child_comm, event, raw->data);
1568 FILL_FIELD(fork_event, child_pid, event, raw->data);
1570 if (trace_handler->fork_event)
1571 trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread);
1575 process_sched_exit_event(struct event *event,
1577 u64 timestamp __used,
1578 struct thread *thread __used)
1581 printf("sched_exit event %p\n", event);
1585 process_sched_migrate_task_event(struct raw_event_sample *raw,
1586 struct event *event,
1588 u64 timestamp __used,
1589 struct thread *thread __used)
1591 struct trace_migrate_task_event migrate_task_event;
1593 FILL_COMMON_FIELDS(migrate_task_event, event, raw->data);
1595 FILL_ARRAY(migrate_task_event, comm, event, raw->data);
1596 FILL_FIELD(migrate_task_event, pid, event, raw->data);
1597 FILL_FIELD(migrate_task_event, prio, event, raw->data);
1598 FILL_FIELD(migrate_task_event, cpu, event, raw->data);
1600 if (trace_handler->migrate_task_event)
1601 trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread);
1605 process_raw_event(event_t *raw_event __used, void *more_data,
1606 int cpu, u64 timestamp, struct thread *thread)
1608 struct raw_event_sample *raw = more_data;
1609 struct event *event;
1612 type = trace_parse_common_type(raw->data);
1613 event = trace_find_event(type);
1615 if (!strcmp(event->name, "sched_switch"))
1616 process_sched_switch_event(raw, event, cpu, timestamp, thread);
1617 if (!strcmp(event->name, "sched_stat_runtime"))
1618 process_sched_runtime_event(raw, event, cpu, timestamp, thread);
1619 if (!strcmp(event->name, "sched_wakeup"))
1620 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1621 if (!strcmp(event->name, "sched_wakeup_new"))
1622 process_sched_wakeup_event(raw, event, cpu, timestamp, thread);
1623 if (!strcmp(event->name, "sched_process_fork"))
1624 process_sched_fork_event(raw, event, cpu, timestamp, thread);
1625 if (!strcmp(event->name, "sched_process_exit"))
1626 process_sched_exit_event(event, cpu, timestamp, thread);
1627 if (!strcmp(event->name, "sched_migrate_task"))
1628 process_sched_migrate_task_event(raw, event, cpu, timestamp, thread);
1632 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1634 struct thread *thread;
1635 u64 ip = event->ip.ip;
1639 void *more_data = event->ip.__more_data;
1641 if (!(sample_type & PERF_SAMPLE_RAW))
1644 thread = threads__findnew(event->ip.pid);
1646 if (sample_type & PERF_SAMPLE_TIME) {
1647 timestamp = *(u64 *)more_data;
1648 more_data += sizeof(u64);
1651 if (sample_type & PERF_SAMPLE_CPU) {
1652 cpu = *(u32 *)more_data;
1653 more_data += sizeof(u32);
1654 more_data += sizeof(u32); /* reserved */
1657 if (sample_type & PERF_SAMPLE_PERIOD) {
1658 period = *(u64 *)more_data;
1659 more_data += sizeof(u64);
1662 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1663 (void *)(offset + head),
1664 (void *)(long)(event->header.size),
1666 event->ip.pid, event->ip.tid,
1670 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1672 if (thread == NULL) {
1673 eprintf("problem processing %d event, skipping it.\n",
1674 event->header.type);
1678 if (profile_cpu != -1 && profile_cpu != (int) cpu)
1681 process_raw_event(event, more_data, cpu, timestamp, thread);
1687 process_lost_event(event_t *event __used,
1688 unsigned long offset __used,
1689 unsigned long head __used)
1692 nr_lost_events += event->lost.lost;
1697 static int sample_type_check(u64 type)
1701 if (!(sample_type & PERF_SAMPLE_RAW)) {
1703 "No trace sample to read. Did you call perf record "
1711 static struct perf_file_handler file_handler = {
1712 .process_sample_event = process_sample_event,
1713 .process_comm_event = process_comm_event,
1714 .process_lost_event = process_lost_event,
1715 .sample_type_check = sample_type_check,
1718 static int read_events(void)
1720 register_idle_thread();
1721 register_perf_file_handler(&file_handler);
1723 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
1726 static void print_bad_events(void)
1728 if (nr_unordered_timestamps && nr_timestamps) {
1729 printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n",
1730 (double)nr_unordered_timestamps/(double)nr_timestamps*100.0,
1731 nr_unordered_timestamps, nr_timestamps);
1733 if (nr_lost_events && nr_events) {
1734 printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n",
1735 (double)nr_lost_events/(double)nr_events*100.0,
1736 nr_lost_events, nr_events, nr_lost_chunks);
1738 if (nr_state_machine_bugs && nr_timestamps) {
1739 printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)",
1740 (double)nr_state_machine_bugs/(double)nr_timestamps*100.0,
1741 nr_state_machine_bugs, nr_timestamps);
1743 printf(" (due to lost events?)");
1746 if (nr_context_switch_bugs && nr_timestamps) {
1747 printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)",
1748 (double)nr_context_switch_bugs/(double)nr_timestamps*100.0,
1749 nr_context_switch_bugs, nr_timestamps);
1751 printf(" (due to lost events?)");
1756 static void __cmd_lat(void)
1758 struct rb_node *next;
1764 printf("\n -----------------------------------------------------------------------------------------\n");
1765 printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n");
1766 printf(" -----------------------------------------------------------------------------------------\n");
1768 next = rb_first(&sorted_atom_root);
1771 struct work_atoms *work_list;
1773 work_list = rb_entry(next, struct work_atoms, node);
1774 output_lat_thread(work_list);
1775 next = rb_next(next);
1778 printf(" -----------------------------------------------------------------------------------------\n");
1779 printf(" TOTAL: |%11.3f ms |%9Ld |\n",
1780 (double)all_runtime/1e6, all_count);
1782 printf(" ---------------------------------------------------\n");
1789 static struct trace_sched_handler map_ops = {
1790 .wakeup_event = NULL,
1791 .switch_event = map_switch_event,
1792 .runtime_event = NULL,
1796 static void __cmd_map(void)
1798 max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1805 static void __cmd_replay(void)
1809 calibrate_run_measurement_overhead();
1810 calibrate_sleep_measurement_overhead();
1812 test_calibrations();
1816 printf("nr_run_events: %ld\n", nr_run_events);
1817 printf("nr_sleep_events: %ld\n", nr_sleep_events);
1818 printf("nr_wakeup_events: %ld\n", nr_wakeup_events);
1820 if (targetless_wakeups)
1821 printf("target-less wakeups: %ld\n", targetless_wakeups);
1822 if (multitarget_wakeups)
1823 printf("multi-target wakeups: %ld\n", multitarget_wakeups);
1824 if (nr_run_events_optimized)
1825 printf("run atoms optimized: %ld\n",
1826 nr_run_events_optimized);
1828 print_task_traces();
1829 add_cross_task_wakeups();
1832 printf("------------------------------------------------------------\n");
1833 for (i = 0; i < replay_repeat; i++)
1838 static const char * const sched_usage[] = {
1839 "perf sched [<options>] {record|latency|map|replay|trace}",
1843 static const struct option sched_options[] = {
1844 OPT_STRING('i', "input", &input_name, "file",
1846 OPT_BOOLEAN('v', "verbose", &verbose,
1847 "be more verbose (show symbol address, etc)"),
1848 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1849 "dump raw trace in ASCII"),
1853 static const char * const latency_usage[] = {
1854 "perf sched latency [<options>]",
1858 static const struct option latency_options[] = {
1859 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1860 "sort by key(s): runtime, switch, avg, max"),
1861 OPT_BOOLEAN('v', "verbose", &verbose,
1862 "be more verbose (show symbol address, etc)"),
1863 OPT_INTEGER('C', "CPU", &profile_cpu,
1864 "CPU to profile on"),
1865 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1866 "dump raw trace in ASCII"),
1870 static const char * const replay_usage[] = {
1871 "perf sched replay [<options>]",
1875 static const struct option replay_options[] = {
1876 OPT_INTEGER('r', "repeat", &replay_repeat,
1877 "repeat the workload replay N times (-1: infinite)"),
1878 OPT_BOOLEAN('v', "verbose", &verbose,
1879 "be more verbose (show symbol address, etc)"),
1880 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1881 "dump raw trace in ASCII"),
1885 static void setup_sorting(void)
1887 char *tmp, *tok, *str = strdup(sort_order);
1889 for (tok = strtok_r(str, ", ", &tmp);
1890 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1891 if (sort_dimension__add(tok, &sort_list) < 0) {
1892 error("Unknown --sort key: `%s'", tok);
1893 usage_with_options(latency_usage, latency_options);
1899 sort_dimension__add("pid", &cmp_pid);
1902 static const char *record_args[] = {
1910 "-e", "sched:sched_switch:r",
1911 "-e", "sched:sched_stat_wait:r",
1912 "-e", "sched:sched_stat_sleep:r",
1913 "-e", "sched:sched_stat_iowait:r",
1914 "-e", "sched:sched_stat_runtime:r",
1915 "-e", "sched:sched_process_exit:r",
1916 "-e", "sched:sched_process_fork:r",
1917 "-e", "sched:sched_wakeup:r",
1918 "-e", "sched:sched_migrate_task:r",
1921 static int __cmd_record(int argc, const char **argv)
1923 unsigned int rec_argc, i, j;
1924 const char **rec_argv;
1926 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1927 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1929 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1930 rec_argv[i] = strdup(record_args[i]);
1932 for (j = 1; j < (unsigned int)argc; j++, i++)
1933 rec_argv[i] = argv[j];
1935 BUG_ON(i != rec_argc);
1937 return cmd_record(i, rec_argv, NULL);
1940 int cmd_sched(int argc, const char **argv, const char *prefix __used)
1944 argc = parse_options(argc, argv, sched_options, sched_usage,
1945 PARSE_OPT_STOP_AT_NON_OPTION);
1947 usage_with_options(sched_usage, sched_options);
1949 if (!strncmp(argv[0], "rec", 3)) {
1950 return __cmd_record(argc, argv);
1951 } else if (!strncmp(argv[0], "lat", 3)) {
1952 trace_handler = &lat_ops;
1954 argc = parse_options(argc, argv, latency_options, latency_usage, 0);
1956 usage_with_options(latency_usage, latency_options);
1960 } else if (!strcmp(argv[0], "map")) {
1961 trace_handler = &map_ops;
1964 } else if (!strncmp(argv[0], "rep", 3)) {
1965 trace_handler = &replay_ops;
1967 argc = parse_options(argc, argv, replay_options, replay_usage, 0);
1969 usage_with_options(replay_usage, replay_options);
1972 } else if (!strcmp(argv[0], "trace")) {
1974 * Aliased to 'perf trace' for now:
1976 return cmd_trace(argc, argv, prefix);
1978 usage_with_options(sched_usage, sched_options);