Commit | Line | Data |
---|---|---|
0a02ad93 | 1 | #include "builtin.h" |
b1ffe8f3 | 2 | #include "perf.h" |
0a02ad93 IM |
3 | |
4 | #include "util/util.h" | |
ee29be62 | 5 | #include "util/evlist.h" |
0a02ad93 | 6 | #include "util/cache.h" |
e3f42609 | 7 | #include "util/evsel.h" |
0a02ad93 IM |
8 | #include "util/symbol.h" |
9 | #include "util/thread.h" | |
10 | #include "util/header.h" | |
94c744b6 | 11 | #include "util/session.h" |
45694aa7 | 12 | #include "util/tool.h" |
57480d2c | 13 | #include "util/cloexec.h" |
a151a37a | 14 | #include "util/thread_map.h" |
8cd91195 | 15 | #include "util/color.h" |
49394a2a | 16 | #include "util/stat.h" |
6c973c90 | 17 | #include "util/callchain.h" |
853b7407 | 18 | #include "util/time-utils.h" |
0a02ad93 | 19 | |
4b6ab94e | 20 | #include <subcmd/parse-options.h> |
b1ffe8f3 | 21 | #include "util/trace-event.h" |
0a02ad93 | 22 | |
0a02ad93 IM |
23 | #include "util/debug.h" |
24 | ||
49394a2a | 25 | #include <linux/log2.h> |
b1ffe8f3 | 26 | #include <sys/prctl.h> |
7b78f136 | 27 | #include <sys/resource.h> |
0a02ad93 | 28 | |
b1ffe8f3 IM |
29 | #include <semaphore.h> |
30 | #include <pthread.h> | |
31 | #include <math.h> | |
cb06ac25 | 32 | #include <api/fs/fs.h> |
4fc76e49 | 33 | #include <linux/time64.h> |
419ab0d6 | 34 | |
b1ffe8f3 IM |
35 | #define PR_SET_NAME 15 /* Set process name */ |
36 | #define MAX_CPUS 4096 | |
b1ffe8f3 IM |
37 | #define COMM_LEN 20 |
38 | #define SYM_LEN 129 | |
a35e27d0 | 39 | #define MAX_PID 1024000 |
ec156764 | 40 | |
39aeb52f | 41 | struct sched_atom; |
ec156764 | 42 | |
b1ffe8f3 IM |
43 | struct task_desc { |
44 | unsigned long nr; | |
45 | unsigned long pid; | |
46 | char comm[COMM_LEN]; | |
ec156764 | 47 | |
b1ffe8f3 IM |
48 | unsigned long nr_events; |
49 | unsigned long curr_event; | |
39aeb52f | 50 | struct sched_atom **atoms; |
b1ffe8f3 IM |
51 | |
52 | pthread_t thread; | |
53 | sem_t sleep_sem; | |
ec156764 | 54 | |
b1ffe8f3 IM |
55 | sem_t ready_for_work; |
56 | sem_t work_done_sem; | |
57 | ||
58 | u64 cpu_usage; | |
59 | }; | |
60 | ||
61 | enum sched_event_type { | |
62 | SCHED_EVENT_RUN, | |
63 | SCHED_EVENT_SLEEP, | |
64 | SCHED_EVENT_WAKEUP, | |
55ffb7a6 | 65 | SCHED_EVENT_MIGRATION, |
b1ffe8f3 IM |
66 | }; |
67 | ||
39aeb52f | 68 | struct sched_atom { |
b1ffe8f3 | 69 | enum sched_event_type type; |
eed05fe7 | 70 | int specific_wait; |
b1ffe8f3 IM |
71 | u64 timestamp; |
72 | u64 duration; | |
73 | unsigned long nr; | |
b1ffe8f3 IM |
74 | sem_t *wait_sem; |
75 | struct task_desc *wakee; | |
76 | }; | |
77 | ||
e936e8e4 | 78 | #define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP" |
b1ffe8f3 IM |
79 | |
80 | enum thread_state { | |
81 | THREAD_SLEEPING = 0, | |
82 | THREAD_WAIT_CPU, | |
83 | THREAD_SCHED_IN, | |
84 | THREAD_IGNORE | |
85 | }; | |
86 | ||
87 | struct work_atom { | |
88 | struct list_head list; | |
89 | enum thread_state state; | |
aa1ab9d2 | 90 | u64 sched_out_time; |
b1ffe8f3 IM |
91 | u64 wake_up_time; |
92 | u64 sched_in_time; | |
93 | u64 runtime; | |
94 | }; | |
95 | ||
39aeb52f | 96 | struct work_atoms { |
97 | struct list_head work_list; | |
b1ffe8f3 IM |
98 | struct thread *thread; |
99 | struct rb_node node; | |
100 | u64 max_lat; | |
3786310a | 101 | u64 max_lat_at; |
b1ffe8f3 IM |
102 | u64 total_lat; |
103 | u64 nb_atoms; | |
104 | u64 total_runtime; | |
2f80dd44 | 105 | int num_merged; |
b1ffe8f3 IM |
106 | }; |
107 | ||
39aeb52f | 108 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); |
b1ffe8f3 | 109 | |
9ec3f4e4 | 110 | struct perf_sched; |
0e9b07e5 | 111 | |
9ec3f4e4 ACM |
112 | struct trace_sched_handler { |
113 | int (*switch_event)(struct perf_sched *sched, struct perf_evsel *evsel, | |
114 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 115 | |
9ec3f4e4 ACM |
116 | int (*runtime_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
117 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 118 | |
9ec3f4e4 ACM |
119 | int (*wakeup_event)(struct perf_sched *sched, struct perf_evsel *evsel, |
120 | struct perf_sample *sample, struct machine *machine); | |
0e9b07e5 | 121 | |
cb627505 DA |
122 | /* PERF_RECORD_FORK event, not sched_process_fork tracepoint */ |
123 | int (*fork_event)(struct perf_sched *sched, union perf_event *event, | |
124 | struct machine *machine); | |
0e9b07e5 ACM |
125 | |
126 | int (*migrate_task_event)(struct perf_sched *sched, | |
9ec3f4e4 ACM |
127 | struct perf_evsel *evsel, |
128 | struct perf_sample *sample, | |
129 | struct machine *machine); | |
0e9b07e5 ACM |
130 | }; |
131 | ||
a151a37a | 132 | #define COLOR_PIDS PERF_COLOR_BLUE |
cf294f24 | 133 | #define COLOR_CPUS PERF_COLOR_BG_RED |
a151a37a | 134 | |
99623c62 JO |
135 | struct perf_sched_map { |
136 | DECLARE_BITMAP(comp_cpus_mask, MAX_CPUS); | |
137 | int *comp_cpus; | |
138 | bool comp; | |
a151a37a JO |
139 | struct thread_map *color_pids; |
140 | const char *color_pids_str; | |
cf294f24 JO |
141 | struct cpu_map *color_cpus; |
142 | const char *color_cpus_str; | |
73643bb6 JO |
143 | struct cpu_map *cpus; |
144 | const char *cpus_str; | |
99623c62 JO |
145 | }; |
146 | ||
0e9b07e5 ACM |
147 | struct perf_sched { |
148 | struct perf_tool tool; | |
0e9b07e5 ACM |
149 | const char *sort_order; |
150 | unsigned long nr_tasks; | |
cb06ac25 | 151 | struct task_desc **pid_to_task; |
0e9b07e5 ACM |
152 | struct task_desc **tasks; |
153 | const struct trace_sched_handler *tp_handler; | |
154 | pthread_mutex_t start_work_mutex; | |
155 | pthread_mutex_t work_done_wait_mutex; | |
156 | int profile_cpu; | |
157 | /* | |
158 | * Track the current task - that way we can know whether there's any | |
159 | * weird events, such as a task being switched away that is not current. | |
160 | */ | |
161 | int max_cpu; | |
162 | u32 curr_pid[MAX_CPUS]; | |
163 | struct thread *curr_thread[MAX_CPUS]; | |
164 | char next_shortname1; | |
165 | char next_shortname2; | |
166 | unsigned int replay_repeat; | |
167 | unsigned long nr_run_events; | |
168 | unsigned long nr_sleep_events; | |
169 | unsigned long nr_wakeup_events; | |
170 | unsigned long nr_sleep_corrections; | |
171 | unsigned long nr_run_events_optimized; | |
172 | unsigned long targetless_wakeups; | |
173 | unsigned long multitarget_wakeups; | |
174 | unsigned long nr_runs; | |
175 | unsigned long nr_timestamps; | |
176 | unsigned long nr_unordered_timestamps; | |
0e9b07e5 ACM |
177 | unsigned long nr_context_switch_bugs; |
178 | unsigned long nr_events; | |
179 | unsigned long nr_lost_chunks; | |
180 | unsigned long nr_lost_events; | |
181 | u64 run_measurement_overhead; | |
182 | u64 sleep_measurement_overhead; | |
183 | u64 start_time; | |
184 | u64 cpu_usage; | |
185 | u64 runavg_cpu_usage; | |
186 | u64 parent_cpu_usage; | |
187 | u64 runavg_parent_cpu_usage; | |
188 | u64 sum_runtime; | |
189 | u64 sum_fluct; | |
190 | u64 run_avg; | |
191 | u64 all_runtime; | |
192 | u64 all_count; | |
193 | u64 cpu_last_switched[MAX_CPUS]; | |
2f80dd44 | 194 | struct rb_root atom_root, sorted_atom_root, merged_atom_root; |
0e9b07e5 | 195 | struct list_head sort_list, cmp_pid; |
939cda52 | 196 | bool force; |
2f80dd44 | 197 | bool skip_merge; |
99623c62 | 198 | struct perf_sched_map map; |
52df138c DA |
199 | |
200 | /* options for timehist command */ | |
201 | bool summary; | |
202 | bool summary_only; | |
6c973c90 DA |
203 | bool show_callchain; |
204 | unsigned int max_stack; | |
a407b067 | 205 | bool show_cpu_visual; |
fc1469f1 | 206 | bool show_wakeups; |
350f54fa | 207 | bool show_migrations; |
52df138c | 208 | u64 skipped_samples; |
853b7407 DA |
209 | const char *time_str; |
210 | struct perf_time_interval ptime; | |
0e9b07e5 | 211 | }; |
b1ffe8f3 | 212 | |
49394a2a DA |
213 | /* per thread run time data */ |
214 | struct thread_runtime { | |
215 | u64 last_time; /* time of previous sched in/out event */ | |
216 | u64 dt_run; /* run time */ | |
217 | u64 dt_wait; /* time between CPU access (off cpu) */ | |
218 | u64 dt_delay; /* time between wakeup and sched-in */ | |
219 | u64 ready_to_run; /* time of wakeup */ | |
220 | ||
221 | struct stats run_stats; | |
222 | u64 total_run_time; | |
350f54fa DA |
223 | |
224 | u64 migrations; | |
49394a2a DA |
225 | }; |
226 | ||
227 | /* per event run time data */ | |
228 | struct evsel_runtime { | |
229 | u64 *last_time; /* time this event was last seen per cpu */ | |
230 | u32 ncpu; /* highest cpu slot allocated */ | |
231 | }; | |
232 | ||
233 | /* track idle times per cpu */ | |
234 | static struct thread **idle_threads; | |
235 | static int idle_max_cpu; | |
236 | static char idle_comm[] = "<idle>"; | |
237 | ||
b1ffe8f3 | 238 | static u64 get_nsecs(void) |
ec156764 IM |
239 | { |
240 | struct timespec ts; | |
241 | ||
242 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
243 | ||
4fc76e49 | 244 | return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; |
ec156764 IM |
245 | } |
246 | ||
0e9b07e5 | 247 | static void burn_nsecs(struct perf_sched *sched, u64 nsecs) |
ec156764 | 248 | { |
b1ffe8f3 | 249 | u64 T0 = get_nsecs(), T1; |
ec156764 IM |
250 | |
251 | do { | |
252 | T1 = get_nsecs(); | |
0e9b07e5 | 253 | } while (T1 + sched->run_measurement_overhead < T0 + nsecs); |
ec156764 IM |
254 | } |
255 | ||
b1ffe8f3 | 256 | static void sleep_nsecs(u64 nsecs) |
ec156764 IM |
257 | { |
258 | struct timespec ts; | |
259 | ||
260 | ts.tv_nsec = nsecs % 999999999; | |
261 | ts.tv_sec = nsecs / 999999999; | |
262 | ||
263 | nanosleep(&ts, NULL); | |
264 | } | |
265 | ||
0e9b07e5 | 266 | static void calibrate_run_measurement_overhead(struct perf_sched *sched) |
ec156764 | 267 | { |
4fc76e49 | 268 | u64 T0, T1, delta, min_delta = NSEC_PER_SEC; |
ec156764 IM |
269 | int i; |
270 | ||
271 | for (i = 0; i < 10; i++) { | |
272 | T0 = get_nsecs(); | |
0e9b07e5 | 273 | burn_nsecs(sched, 0); |
ec156764 IM |
274 | T1 = get_nsecs(); |
275 | delta = T1-T0; | |
276 | min_delta = min(min_delta, delta); | |
277 | } | |
0e9b07e5 | 278 | sched->run_measurement_overhead = min_delta; |
ec156764 | 279 | |
9486aa38 | 280 | printf("run measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
ec156764 IM |
281 | } |
282 | ||
0e9b07e5 | 283 | static void calibrate_sleep_measurement_overhead(struct perf_sched *sched) |
ec156764 | 284 | { |
4fc76e49 | 285 | u64 T0, T1, delta, min_delta = NSEC_PER_SEC; |
ec156764 IM |
286 | int i; |
287 | ||
288 | for (i = 0; i < 10; i++) { | |
289 | T0 = get_nsecs(); | |
290 | sleep_nsecs(10000); | |
291 | T1 = get_nsecs(); | |
292 | delta = T1-T0; | |
293 | min_delta = min(min_delta, delta); | |
294 | } | |
295 | min_delta -= 10000; | |
0e9b07e5 | 296 | sched->sleep_measurement_overhead = min_delta; |
ec156764 | 297 | |
9486aa38 | 298 | printf("sleep measurement overhead: %" PRIu64 " nsecs\n", min_delta); |
ec156764 IM |
299 | } |
300 | ||
39aeb52f | 301 | static struct sched_atom * |
b1ffe8f3 | 302 | get_new_event(struct task_desc *task, u64 timestamp) |
ec156764 | 303 | { |
36479484 | 304 | struct sched_atom *event = zalloc(sizeof(*event)); |
ec156764 IM |
305 | unsigned long idx = task->nr_events; |
306 | size_t size; | |
307 | ||
308 | event->timestamp = timestamp; | |
309 | event->nr = idx; | |
310 | ||
311 | task->nr_events++; | |
39aeb52f | 312 | size = sizeof(struct sched_atom *) * task->nr_events; |
313 | task->atoms = realloc(task->atoms, size); | |
314 | BUG_ON(!task->atoms); | |
ec156764 | 315 | |
39aeb52f | 316 | task->atoms[idx] = event; |
ec156764 IM |
317 | |
318 | return event; | |
319 | } | |
320 | ||
39aeb52f | 321 | static struct sched_atom *last_event(struct task_desc *task) |
ec156764 IM |
322 | { |
323 | if (!task->nr_events) | |
324 | return NULL; | |
325 | ||
39aeb52f | 326 | return task->atoms[task->nr_events - 1]; |
ec156764 IM |
327 | } |
328 | ||
0e9b07e5 ACM |
329 | static void add_sched_event_run(struct perf_sched *sched, struct task_desc *task, |
330 | u64 timestamp, u64 duration) | |
ec156764 | 331 | { |
39aeb52f | 332 | struct sched_atom *event, *curr_event = last_event(task); |
ec156764 IM |
333 | |
334 | /* | |
fbf94829 IM |
335 | * optimize an existing RUN event by merging this one |
336 | * to it: | |
337 | */ | |
ec156764 | 338 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { |
0e9b07e5 | 339 | sched->nr_run_events_optimized++; |
ec156764 IM |
340 | curr_event->duration += duration; |
341 | return; | |
342 | } | |
343 | ||
344 | event = get_new_event(task, timestamp); | |
345 | ||
346 | event->type = SCHED_EVENT_RUN; | |
347 | event->duration = duration; | |
348 | ||
0e9b07e5 | 349 | sched->nr_run_events++; |
ec156764 IM |
350 | } |
351 | ||
0e9b07e5 ACM |
352 | static void add_sched_event_wakeup(struct perf_sched *sched, struct task_desc *task, |
353 | u64 timestamp, struct task_desc *wakee) | |
ec156764 | 354 | { |
39aeb52f | 355 | struct sched_atom *event, *wakee_event; |
ec156764 IM |
356 | |
357 | event = get_new_event(task, timestamp); | |
358 | event->type = SCHED_EVENT_WAKEUP; | |
359 | event->wakee = wakee; | |
360 | ||
361 | wakee_event = last_event(wakee); | |
362 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { | |
0e9b07e5 | 363 | sched->targetless_wakeups++; |
ec156764 IM |
364 | return; |
365 | } | |
366 | if (wakee_event->wait_sem) { | |
0e9b07e5 | 367 | sched->multitarget_wakeups++; |
ec156764 IM |
368 | return; |
369 | } | |
370 | ||
36479484 | 371 | wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); |
ec156764 IM |
372 | sem_init(wakee_event->wait_sem, 0, 0); |
373 | wakee_event->specific_wait = 1; | |
374 | event->wait_sem = wakee_event->wait_sem; | |
375 | ||
0e9b07e5 | 376 | sched->nr_wakeup_events++; |
ec156764 IM |
377 | } |
378 | ||
0e9b07e5 ACM |
379 | static void add_sched_event_sleep(struct perf_sched *sched, struct task_desc *task, |
380 | u64 timestamp, u64 task_state __maybe_unused) | |
ec156764 | 381 | { |
39aeb52f | 382 | struct sched_atom *event = get_new_event(task, timestamp); |
ec156764 IM |
383 | |
384 | event->type = SCHED_EVENT_SLEEP; | |
385 | ||
0e9b07e5 | 386 | sched->nr_sleep_events++; |
ec156764 IM |
387 | } |
388 | ||
0e9b07e5 ACM |
389 | static struct task_desc *register_pid(struct perf_sched *sched, |
390 | unsigned long pid, const char *comm) | |
ec156764 IM |
391 | { |
392 | struct task_desc *task; | |
cb06ac25 | 393 | static int pid_max; |
ec156764 | 394 | |
cb06ac25 YS |
395 | if (sched->pid_to_task == NULL) { |
396 | if (sysctl__read_int("kernel/pid_max", &pid_max) < 0) | |
397 | pid_max = MAX_PID; | |
398 | BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL); | |
399 | } | |
3a423a5c YS |
400 | if (pid >= (unsigned long)pid_max) { |
401 | BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) * | |
402 | sizeof(struct task_desc *))) == NULL); | |
403 | while (pid >= (unsigned long)pid_max) | |
404 | sched->pid_to_task[pid_max++] = NULL; | |
405 | } | |
ec156764 | 406 | |
0e9b07e5 | 407 | task = sched->pid_to_task[pid]; |
ec156764 IM |
408 | |
409 | if (task) | |
410 | return task; | |
411 | ||
36479484 | 412 | task = zalloc(sizeof(*task)); |
ec156764 | 413 | task->pid = pid; |
0e9b07e5 | 414 | task->nr = sched->nr_tasks; |
ec156764 IM |
415 | strcpy(task->comm, comm); |
416 | /* | |
417 | * every task starts in sleeping state - this gets ignored | |
418 | * if there's no wakeup pointing to this sleep state: | |
419 | */ | |
0e9b07e5 | 420 | add_sched_event_sleep(sched, task, 0, 0); |
ec156764 | 421 | |
0e9b07e5 ACM |
422 | sched->pid_to_task[pid] = task; |
423 | sched->nr_tasks++; | |
0755bc4d | 424 | sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *)); |
0e9b07e5 ACM |
425 | BUG_ON(!sched->tasks); |
426 | sched->tasks[task->nr] = task; | |
ec156764 | 427 | |
ad236fd2 | 428 | if (verbose) |
0e9b07e5 | 429 | printf("registered task #%ld, PID %ld (%s)\n", sched->nr_tasks, pid, comm); |
ec156764 IM |
430 | |
431 | return task; | |
432 | } | |
433 | ||
434 | ||
0e9b07e5 | 435 | static void print_task_traces(struct perf_sched *sched) |
ec156764 IM |
436 | { |
437 | struct task_desc *task; | |
438 | unsigned long i; | |
439 | ||
0e9b07e5 ACM |
440 | for (i = 0; i < sched->nr_tasks; i++) { |
441 | task = sched->tasks[i]; | |
ad236fd2 | 442 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", |
ec156764 IM |
443 | task->nr, task->comm, task->pid, task->nr_events); |
444 | } | |
445 | } | |
446 | ||
0e9b07e5 | 447 | static void add_cross_task_wakeups(struct perf_sched *sched) |
ec156764 IM |
448 | { |
449 | struct task_desc *task1, *task2; | |
450 | unsigned long i, j; | |
451 | ||
0e9b07e5 ACM |
452 | for (i = 0; i < sched->nr_tasks; i++) { |
453 | task1 = sched->tasks[i]; | |
ec156764 | 454 | j = i + 1; |
0e9b07e5 | 455 | if (j == sched->nr_tasks) |
ec156764 | 456 | j = 0; |
0e9b07e5 ACM |
457 | task2 = sched->tasks[j]; |
458 | add_sched_event_wakeup(sched, task1, 0, task2); | |
ec156764 IM |
459 | } |
460 | } | |
461 | ||
0e9b07e5 ACM |
462 | static void perf_sched__process_event(struct perf_sched *sched, |
463 | struct sched_atom *atom) | |
ec156764 IM |
464 | { |
465 | int ret = 0; | |
ec156764 | 466 | |
39aeb52f | 467 | switch (atom->type) { |
ec156764 | 468 | case SCHED_EVENT_RUN: |
0e9b07e5 | 469 | burn_nsecs(sched, atom->duration); |
ec156764 IM |
470 | break; |
471 | case SCHED_EVENT_SLEEP: | |
39aeb52f | 472 | if (atom->wait_sem) |
473 | ret = sem_wait(atom->wait_sem); | |
ec156764 IM |
474 | BUG_ON(ret); |
475 | break; | |
476 | case SCHED_EVENT_WAKEUP: | |
39aeb52f | 477 | if (atom->wait_sem) |
478 | ret = sem_post(atom->wait_sem); | |
ec156764 IM |
479 | BUG_ON(ret); |
480 | break; | |
55ffb7a6 MG |
481 | case SCHED_EVENT_MIGRATION: |
482 | break; | |
ec156764 IM |
483 | default: |
484 | BUG_ON(1); | |
485 | } | |
486 | } | |
487 | ||
b1ffe8f3 | 488 | static u64 get_cpu_usage_nsec_parent(void) |
ec156764 IM |
489 | { |
490 | struct rusage ru; | |
b1ffe8f3 | 491 | u64 sum; |
ec156764 IM |
492 | int err; |
493 | ||
494 | err = getrusage(RUSAGE_SELF, &ru); | |
495 | BUG_ON(err); | |
496 | ||
4fc76e49 ACM |
497 | sum = ru.ru_utime.tv_sec * NSEC_PER_SEC + ru.ru_utime.tv_usec * NSEC_PER_USEC; |
498 | sum += ru.ru_stime.tv_sec * NSEC_PER_SEC + ru.ru_stime.tv_usec * NSEC_PER_USEC; | |
ec156764 IM |
499 | |
500 | return sum; | |
501 | } | |
502 | ||
939cda52 | 503 | static int self_open_counters(struct perf_sched *sched, unsigned long cur_task) |
ec156764 | 504 | { |
c0c9e721 | 505 | struct perf_event_attr attr; |
939cda52 | 506 | char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE]; |
c0c9e721 | 507 | int fd; |
939cda52 YS |
508 | struct rlimit limit; |
509 | bool need_privilege = false; | |
ec156764 | 510 | |
c0c9e721 | 511 | memset(&attr, 0, sizeof(attr)); |
ec156764 | 512 | |
c0c9e721 XG |
513 | attr.type = PERF_TYPE_SOFTWARE; |
514 | attr.config = PERF_COUNT_SW_TASK_CLOCK; | |
ec156764 | 515 | |
939cda52 | 516 | force_again: |
57480d2c YD |
517 | fd = sys_perf_event_open(&attr, 0, -1, -1, |
518 | perf_event_open_cloexec_flag()); | |
c0c9e721 | 519 | |
1aff59be | 520 | if (fd < 0) { |
939cda52 YS |
521 | if (errno == EMFILE) { |
522 | if (sched->force) { | |
523 | BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1); | |
524 | limit.rlim_cur += sched->nr_tasks - cur_task; | |
525 | if (limit.rlim_cur > limit.rlim_max) { | |
526 | limit.rlim_max = limit.rlim_cur; | |
527 | need_privilege = true; | |
528 | } | |
529 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { | |
530 | if (need_privilege && errno == EPERM) | |
531 | strcpy(info, "Need privilege\n"); | |
532 | } else | |
533 | goto force_again; | |
534 | } else | |
535 | strcpy(info, "Have a try with -f option\n"); | |
536 | } | |
60b7d14a | 537 | pr_err("Error: sys_perf_event_open() syscall returned " |
939cda52 | 538 | "with %d (%s)\n%s", fd, |
c8b5f2c9 | 539 | str_error_r(errno, sbuf, sizeof(sbuf)), info); |
1aff59be YS |
540 | exit(EXIT_FAILURE); |
541 | } | |
c0c9e721 XG |
542 | return fd; |
543 | } | |
544 | ||
545 | static u64 get_cpu_usage_nsec_self(int fd) | |
546 | { | |
547 | u64 runtime; | |
548 | int ret; | |
549 | ||
550 | ret = read(fd, &runtime, sizeof(runtime)); | |
551 | BUG_ON(ret != sizeof(runtime)); | |
552 | ||
553 | return runtime; | |
ec156764 IM |
554 | } |
555 | ||
0e9b07e5 ACM |
556 | struct sched_thread_parms { |
557 | struct task_desc *task; | |
558 | struct perf_sched *sched; | |
08097abc | 559 | int fd; |
0e9b07e5 ACM |
560 | }; |
561 | ||
ec156764 IM |
562 | static void *thread_func(void *ctx) |
563 | { | |
0e9b07e5 ACM |
564 | struct sched_thread_parms *parms = ctx; |
565 | struct task_desc *this_task = parms->task; | |
566 | struct perf_sched *sched = parms->sched; | |
b1ffe8f3 | 567 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
568 | unsigned long i, ret; |
569 | char comm2[22]; | |
08097abc | 570 | int fd = parms->fd; |
ec156764 | 571 | |
74cf249d | 572 | zfree(&parms); |
0e9b07e5 | 573 | |
ec156764 IM |
574 | sprintf(comm2, ":%s", this_task->comm); |
575 | prctl(PR_SET_NAME, comm2); | |
a116e05d ACM |
576 | if (fd < 0) |
577 | return NULL; | |
ec156764 IM |
578 | again: |
579 | ret = sem_post(&this_task->ready_for_work); | |
580 | BUG_ON(ret); | |
0e9b07e5 | 581 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 | 582 | BUG_ON(ret); |
0e9b07e5 | 583 | ret = pthread_mutex_unlock(&sched->start_work_mutex); |
ec156764 | 584 | BUG_ON(ret); |
ec156764 | 585 | |
c0c9e721 | 586 | cpu_usage_0 = get_cpu_usage_nsec_self(fd); |
ec156764 IM |
587 | |
588 | for (i = 0; i < this_task->nr_events; i++) { | |
589 | this_task->curr_event = i; | |
0e9b07e5 | 590 | perf_sched__process_event(sched, this_task->atoms[i]); |
ec156764 IM |
591 | } |
592 | ||
c0c9e721 | 593 | cpu_usage_1 = get_cpu_usage_nsec_self(fd); |
ec156764 | 594 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; |
ec156764 IM |
595 | ret = sem_post(&this_task->work_done_sem); |
596 | BUG_ON(ret); | |
ec156764 | 597 | |
0e9b07e5 | 598 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 | 599 | BUG_ON(ret); |
0e9b07e5 | 600 | ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); |
ec156764 | 601 | BUG_ON(ret); |
ec156764 IM |
602 | |
603 | goto again; | |
604 | } | |
605 | ||
0e9b07e5 | 606 | static void create_tasks(struct perf_sched *sched) |
ec156764 IM |
607 | { |
608 | struct task_desc *task; | |
609 | pthread_attr_t attr; | |
610 | unsigned long i; | |
611 | int err; | |
612 | ||
613 | err = pthread_attr_init(&attr); | |
614 | BUG_ON(err); | |
12f7e036 JP |
615 | err = pthread_attr_setstacksize(&attr, |
616 | (size_t) max(16 * 1024, PTHREAD_STACK_MIN)); | |
ec156764 | 617 | BUG_ON(err); |
0e9b07e5 | 618 | err = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 | 619 | BUG_ON(err); |
0e9b07e5 | 620 | err = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 | 621 | BUG_ON(err); |
0e9b07e5 ACM |
622 | for (i = 0; i < sched->nr_tasks; i++) { |
623 | struct sched_thread_parms *parms = malloc(sizeof(*parms)); | |
624 | BUG_ON(parms == NULL); | |
625 | parms->task = task = sched->tasks[i]; | |
626 | parms->sched = sched; | |
939cda52 | 627 | parms->fd = self_open_counters(sched, i); |
ec156764 IM |
628 | sem_init(&task->sleep_sem, 0, 0); |
629 | sem_init(&task->ready_for_work, 0, 0); | |
630 | sem_init(&task->work_done_sem, 0, 0); | |
631 | task->curr_event = 0; | |
0e9b07e5 | 632 | err = pthread_create(&task->thread, &attr, thread_func, parms); |
ec156764 IM |
633 | BUG_ON(err); |
634 | } | |
635 | } | |
636 | ||
0e9b07e5 | 637 | static void wait_for_tasks(struct perf_sched *sched) |
ec156764 | 638 | { |
b1ffe8f3 | 639 | u64 cpu_usage_0, cpu_usage_1; |
ec156764 IM |
640 | struct task_desc *task; |
641 | unsigned long i, ret; | |
642 | ||
0e9b07e5 ACM |
643 | sched->start_time = get_nsecs(); |
644 | sched->cpu_usage = 0; | |
645 | pthread_mutex_unlock(&sched->work_done_wait_mutex); | |
ec156764 | 646 | |
0e9b07e5 ACM |
647 | for (i = 0; i < sched->nr_tasks; i++) { |
648 | task = sched->tasks[i]; | |
ec156764 IM |
649 | ret = sem_wait(&task->ready_for_work); |
650 | BUG_ON(ret); | |
651 | sem_init(&task->ready_for_work, 0, 0); | |
652 | } | |
0e9b07e5 | 653 | ret = pthread_mutex_lock(&sched->work_done_wait_mutex); |
ec156764 IM |
654 | BUG_ON(ret); |
655 | ||
656 | cpu_usage_0 = get_cpu_usage_nsec_parent(); | |
657 | ||
0e9b07e5 | 658 | pthread_mutex_unlock(&sched->start_work_mutex); |
ec156764 | 659 | |
0e9b07e5 ACM |
660 | for (i = 0; i < sched->nr_tasks; i++) { |
661 | task = sched->tasks[i]; | |
ec156764 IM |
662 | ret = sem_wait(&task->work_done_sem); |
663 | BUG_ON(ret); | |
664 | sem_init(&task->work_done_sem, 0, 0); | |
0e9b07e5 | 665 | sched->cpu_usage += task->cpu_usage; |
ec156764 IM |
666 | task->cpu_usage = 0; |
667 | } | |
668 | ||
669 | cpu_usage_1 = get_cpu_usage_nsec_parent(); | |
0e9b07e5 ACM |
670 | if (!sched->runavg_cpu_usage) |
671 | sched->runavg_cpu_usage = sched->cpu_usage; | |
ff5f3bbd | 672 | sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat; |
ec156764 | 673 | |
0e9b07e5 ACM |
674 | sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; |
675 | if (!sched->runavg_parent_cpu_usage) | |
676 | sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; | |
ff5f3bbd YS |
677 | sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + |
678 | sched->parent_cpu_usage)/sched->replay_repeat; | |
ec156764 | 679 | |
0e9b07e5 | 680 | ret = pthread_mutex_lock(&sched->start_work_mutex); |
ec156764 IM |
681 | BUG_ON(ret); |
682 | ||
0e9b07e5 ACM |
683 | for (i = 0; i < sched->nr_tasks; i++) { |
684 | task = sched->tasks[i]; | |
ec156764 IM |
685 | sem_init(&task->sleep_sem, 0, 0); |
686 | task->curr_event = 0; | |
687 | } | |
688 | } | |
689 | ||
0e9b07e5 | 690 | static void run_one_test(struct perf_sched *sched) |
ec156764 | 691 | { |
fb7d0b3c | 692 | u64 T0, T1, delta, avg_delta, fluct; |
ec156764 IM |
693 | |
694 | T0 = get_nsecs(); | |
0e9b07e5 | 695 | wait_for_tasks(sched); |
ec156764 IM |
696 | T1 = get_nsecs(); |
697 | ||
698 | delta = T1 - T0; | |
0e9b07e5 ACM |
699 | sched->sum_runtime += delta; |
700 | sched->nr_runs++; | |
ec156764 | 701 | |
0e9b07e5 | 702 | avg_delta = sched->sum_runtime / sched->nr_runs; |
ec156764 IM |
703 | if (delta < avg_delta) |
704 | fluct = avg_delta - delta; | |
705 | else | |
706 | fluct = delta - avg_delta; | |
0e9b07e5 ACM |
707 | sched->sum_fluct += fluct; |
708 | if (!sched->run_avg) | |
709 | sched->run_avg = delta; | |
ff5f3bbd | 710 | sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat; |
ec156764 | 711 | |
4fc76e49 | 712 | printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / NSEC_PER_MSEC); |
ec156764 | 713 | |
4fc76e49 | 714 | printf("ravg: %0.2f, ", (double)sched->run_avg / NSEC_PER_MSEC); |
ec156764 | 715 | |
ad236fd2 | 716 | printf("cpu: %0.2f / %0.2f", |
4fc76e49 | 717 | (double)sched->cpu_usage / NSEC_PER_MSEC, (double)sched->runavg_cpu_usage / NSEC_PER_MSEC); |
ec156764 IM |
718 | |
719 | #if 0 | |
720 | /* | |
fbf94829 | 721 | * rusage statistics done by the parent, these are less |
0e9b07e5 | 722 | * accurate than the sched->sum_exec_runtime based statistics: |
fbf94829 | 723 | */ |
ad236fd2 | 724 | printf(" [%0.2f / %0.2f]", |
4fc76e49 ACM |
725 | (double)sched->parent_cpu_usage / NSEC_PER_MSEC, |
726 | (double)sched->runavg_parent_cpu_usage / NSEC_PER_MSEC); | |
ec156764 IM |
727 | #endif |
728 | ||
ad236fd2 | 729 | printf("\n"); |
ec156764 | 730 | |
0e9b07e5 ACM |
731 | if (sched->nr_sleep_corrections) |
732 | printf(" (%ld sleep corrections)\n", sched->nr_sleep_corrections); | |
733 | sched->nr_sleep_corrections = 0; | |
ec156764 IM |
734 | } |
735 | ||
0e9b07e5 | 736 | static void test_calibrations(struct perf_sched *sched) |
ec156764 | 737 | { |
b1ffe8f3 | 738 | u64 T0, T1; |
ec156764 IM |
739 | |
740 | T0 = get_nsecs(); | |
4fc76e49 | 741 | burn_nsecs(sched, NSEC_PER_MSEC); |
ec156764 IM |
742 | T1 = get_nsecs(); |
743 | ||
9486aa38 | 744 | printf("the run test took %" PRIu64 " nsecs\n", T1 - T0); |
ec156764 IM |
745 | |
746 | T0 = get_nsecs(); | |
4fc76e49 | 747 | sleep_nsecs(NSEC_PER_MSEC); |
ec156764 IM |
748 | T1 = get_nsecs(); |
749 | ||
9486aa38 | 750 | printf("the sleep test took %" PRIu64 " nsecs\n", T1 - T0); |
ec156764 IM |
751 | } |
752 | ||
a116e05d | 753 | static int |
0e9b07e5 | 754 | replay_wakeup_event(struct perf_sched *sched, |
9ec3f4e4 ACM |
755 | struct perf_evsel *evsel, struct perf_sample *sample, |
756 | struct machine *machine __maybe_unused) | |
419ab0d6 | 757 | { |
9ec3f4e4 ACM |
758 | const char *comm = perf_evsel__strval(evsel, sample, "comm"); |
759 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); | |
419ab0d6 | 760 | struct task_desc *waker, *wakee; |
fbf94829 | 761 | |
ad236fd2 | 762 | if (verbose) { |
2b7fcbc5 | 763 | printf("sched_wakeup event %p\n", evsel); |
fbf94829 | 764 | |
9ec3f4e4 | 765 | printf(" ... pid %d woke up %s/%d\n", sample->tid, comm, pid); |
ad236fd2 | 766 | } |
fbf94829 | 767 | |
2b7fcbc5 | 768 | waker = register_pid(sched, sample->tid, "<unknown>"); |
9ec3f4e4 | 769 | wakee = register_pid(sched, pid, comm); |
fbf94829 | 770 | |
0e9b07e5 | 771 | add_sched_event_wakeup(sched, waker, sample->time, wakee); |
a116e05d | 772 | return 0; |
ec156764 IM |
773 | } |
774 | ||
9ec3f4e4 ACM |
775 | static int replay_switch_event(struct perf_sched *sched, |
776 | struct perf_evsel *evsel, | |
777 | struct perf_sample *sample, | |
778 | struct machine *machine __maybe_unused) | |
ec156764 | 779 | { |
9ec3f4e4 ACM |
780 | const char *prev_comm = perf_evsel__strval(evsel, sample, "prev_comm"), |
781 | *next_comm = perf_evsel__strval(evsel, sample, "next_comm"); | |
782 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), | |
783 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
784 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); | |
1d037ca1 | 785 | struct task_desc *prev, __maybe_unused *next; |
7f7f8d0b ACM |
786 | u64 timestamp0, timestamp = sample->time; |
787 | int cpu = sample->cpu; | |
fbf94829 IM |
788 | s64 delta; |
789 | ||
ad236fd2 | 790 | if (verbose) |
2b7fcbc5 | 791 | printf("sched_switch event %p\n", evsel); |
ad236fd2 | 792 | |
fbf94829 | 793 | if (cpu >= MAX_CPUS || cpu < 0) |
a116e05d | 794 | return 0; |
fbf94829 | 795 | |
0e9b07e5 | 796 | timestamp0 = sched->cpu_last_switched[cpu]; |
fbf94829 IM |
797 | if (timestamp0) |
798 | delta = timestamp - timestamp0; | |
799 | else | |
800 | delta = 0; | |
801 | ||
a116e05d | 802 | if (delta < 0) { |
60b7d14a | 803 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
a116e05d ACM |
804 | return -1; |
805 | } | |
fbf94829 | 806 | |
9ec3f4e4 ACM |
807 | pr_debug(" ... switch from %s/%d to %s/%d [ran %" PRIu64 " nsecs]\n", |
808 | prev_comm, prev_pid, next_comm, next_pid, delta); | |
fbf94829 | 809 | |
9ec3f4e4 ACM |
810 | prev = register_pid(sched, prev_pid, prev_comm); |
811 | next = register_pid(sched, next_pid, next_comm); | |
fbf94829 | 812 | |
0e9b07e5 | 813 | sched->cpu_last_switched[cpu] = timestamp; |
fbf94829 | 814 | |
0e9b07e5 | 815 | add_sched_event_run(sched, prev, timestamp, delta); |
9ec3f4e4 | 816 | add_sched_event_sleep(sched, prev, timestamp, prev_state); |
a116e05d ACM |
817 | |
818 | return 0; | |
fbf94829 IM |
819 | } |
820 | ||
cb627505 DA |
821 | static int replay_fork_event(struct perf_sched *sched, |
822 | union perf_event *event, | |
823 | struct machine *machine) | |
419ab0d6 | 824 | { |
cb627505 DA |
825 | struct thread *child, *parent; |
826 | ||
314add6b AH |
827 | child = machine__findnew_thread(machine, event->fork.pid, |
828 | event->fork.tid); | |
829 | parent = machine__findnew_thread(machine, event->fork.ppid, | |
830 | event->fork.ptid); | |
cb627505 DA |
831 | |
832 | if (child == NULL || parent == NULL) { | |
833 | pr_debug("thread does not exist on fork event: child %p, parent %p\n", | |
834 | child, parent); | |
b91fc39f | 835 | goto out_put; |
cb627505 | 836 | } |
9ec3f4e4 | 837 | |
419ab0d6 | 838 | if (verbose) { |
cb627505 | 839 | printf("fork event\n"); |
b9c5143a FW |
840 | printf("... parent: %s/%d\n", thread__comm_str(parent), parent->tid); |
841 | printf("... child: %s/%d\n", thread__comm_str(child), child->tid); | |
419ab0d6 | 842 | } |
9ec3f4e4 | 843 | |
b9c5143a FW |
844 | register_pid(sched, parent->tid, thread__comm_str(parent)); |
845 | register_pid(sched, child->tid, thread__comm_str(child)); | |
b91fc39f ACM |
846 | out_put: |
847 | thread__put(child); | |
848 | thread__put(parent); | |
a116e05d | 849 | return 0; |
419ab0d6 | 850 | } |
fbf94829 | 851 | |
b1ffe8f3 IM |
852 | struct sort_dimension { |
853 | const char *name; | |
b5fae128 | 854 | sort_fn_t cmp; |
b1ffe8f3 IM |
855 | struct list_head list; |
856 | }; | |
857 | ||
daa1d7a5 | 858 | static int |
39aeb52f | 859 | thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
860 | { |
861 | struct sort_dimension *sort; | |
862 | int ret = 0; | |
863 | ||
b5fae128 IM |
864 | BUG_ON(list_empty(list)); |
865 | ||
daa1d7a5 FW |
866 | list_for_each_entry(sort, list, list) { |
867 | ret = sort->cmp(l, r); | |
868 | if (ret) | |
869 | return ret; | |
870 | } | |
871 | ||
872 | return ret; | |
873 | } | |
874 | ||
39aeb52f | 875 | static struct work_atoms * |
b5fae128 IM |
876 | thread_atoms_search(struct rb_root *root, struct thread *thread, |
877 | struct list_head *sort_list) | |
878 | { | |
879 | struct rb_node *node = root->rb_node; | |
39aeb52f | 880 | struct work_atoms key = { .thread = thread }; |
b5fae128 IM |
881 | |
882 | while (node) { | |
39aeb52f | 883 | struct work_atoms *atoms; |
b5fae128 IM |
884 | int cmp; |
885 | ||
39aeb52f | 886 | atoms = container_of(node, struct work_atoms, node); |
b5fae128 IM |
887 | |
888 | cmp = thread_lat_cmp(sort_list, &key, atoms); | |
889 | if (cmp > 0) | |
890 | node = node->rb_left; | |
891 | else if (cmp < 0) | |
892 | node = node->rb_right; | |
893 | else { | |
894 | BUG_ON(thread != atoms->thread); | |
895 | return atoms; | |
896 | } | |
897 | } | |
898 | return NULL; | |
899 | } | |
900 | ||
cdce9d73 | 901 | static void |
39aeb52f | 902 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, |
daa1d7a5 | 903 | struct list_head *sort_list) |
cdce9d73 FW |
904 | { |
905 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
906 | ||
907 | while (*new) { | |
39aeb52f | 908 | struct work_atoms *this; |
daa1d7a5 | 909 | int cmp; |
cdce9d73 | 910 | |
39aeb52f | 911 | this = container_of(*new, struct work_atoms, node); |
cdce9d73 | 912 | parent = *new; |
daa1d7a5 FW |
913 | |
914 | cmp = thread_lat_cmp(sort_list, data, this); | |
915 | ||
916 | if (cmp > 0) | |
cdce9d73 | 917 | new = &((*new)->rb_left); |
cdce9d73 | 918 | else |
daa1d7a5 | 919 | new = &((*new)->rb_right); |
cdce9d73 FW |
920 | } |
921 | ||
922 | rb_link_node(&data->node, parent, new); | |
923 | rb_insert_color(&data->node, root); | |
924 | } | |
925 | ||
0e9b07e5 | 926 | static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread) |
cdce9d73 | 927 | { |
36479484 | 928 | struct work_atoms *atoms = zalloc(sizeof(*atoms)); |
a116e05d ACM |
929 | if (!atoms) { |
930 | pr_err("No memory at %s\n", __func__); | |
931 | return -1; | |
932 | } | |
cdce9d73 | 933 | |
f3b623b8 | 934 | atoms->thread = thread__get(thread); |
39aeb52f | 935 | INIT_LIST_HEAD(&atoms->work_list); |
0e9b07e5 | 936 | __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); |
a116e05d | 937 | return 0; |
cdce9d73 FW |
938 | } |
939 | ||
9ec3f4e4 | 940 | static char sched_out_state(u64 prev_state) |
cdce9d73 FW |
941 | { |
942 | const char *str = TASK_STATE_TO_CHAR_STR; | |
943 | ||
9ec3f4e4 | 944 | return str[prev_state]; |
cdce9d73 FW |
945 | } |
946 | ||
a116e05d | 947 | static int |
39aeb52f | 948 | add_sched_out_event(struct work_atoms *atoms, |
949 | char run_state, | |
950 | u64 timestamp) | |
cdce9d73 | 951 | { |
36479484 | 952 | struct work_atom *atom = zalloc(sizeof(*atom)); |
a116e05d ACM |
953 | if (!atom) { |
954 | pr_err("Non memory at %s", __func__); | |
955 | return -1; | |
956 | } | |
cdce9d73 | 957 | |
aa1ab9d2 FW |
958 | atom->sched_out_time = timestamp; |
959 | ||
39aeb52f | 960 | if (run_state == 'R') { |
b1ffe8f3 | 961 | atom->state = THREAD_WAIT_CPU; |
aa1ab9d2 | 962 | atom->wake_up_time = atom->sched_out_time; |
c6ced611 FW |
963 | } |
964 | ||
39aeb52f | 965 | list_add_tail(&atom->list, &atoms->work_list); |
a116e05d | 966 | return 0; |
cdce9d73 FW |
967 | } |
968 | ||
969 | static void | |
1d037ca1 IT |
970 | add_runtime_event(struct work_atoms *atoms, u64 delta, |
971 | u64 timestamp __maybe_unused) | |
39aeb52f | 972 | { |
973 | struct work_atom *atom; | |
974 | ||
975 | BUG_ON(list_empty(&atoms->work_list)); | |
976 | ||
977 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
978 | ||
979 | atom->runtime += delta; | |
980 | atoms->total_runtime += delta; | |
981 | } | |
982 | ||
983 | static void | |
984 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) | |
cdce9d73 | 985 | { |
b1ffe8f3 | 986 | struct work_atom *atom; |
66685678 | 987 | u64 delta; |
cdce9d73 | 988 | |
39aeb52f | 989 | if (list_empty(&atoms->work_list)) |
cdce9d73 FW |
990 | return; |
991 | ||
39aeb52f | 992 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 993 | |
b1ffe8f3 | 994 | if (atom->state != THREAD_WAIT_CPU) |
cdce9d73 FW |
995 | return; |
996 | ||
b1ffe8f3 IM |
997 | if (timestamp < atom->wake_up_time) { |
998 | atom->state = THREAD_IGNORE; | |
cdce9d73 FW |
999 | return; |
1000 | } | |
1001 | ||
b1ffe8f3 IM |
1002 | atom->state = THREAD_SCHED_IN; |
1003 | atom->sched_in_time = timestamp; | |
66685678 | 1004 | |
b1ffe8f3 | 1005 | delta = atom->sched_in_time - atom->wake_up_time; |
66685678 | 1006 | atoms->total_lat += delta; |
3786310a | 1007 | if (delta > atoms->max_lat) { |
66685678 | 1008 | atoms->max_lat = delta; |
3786310a FW |
1009 | atoms->max_lat_at = timestamp; |
1010 | } | |
66685678 | 1011 | atoms->nb_atoms++; |
cdce9d73 FW |
1012 | } |
1013 | ||
9ec3f4e4 ACM |
1014 | static int latency_switch_event(struct perf_sched *sched, |
1015 | struct perf_evsel *evsel, | |
1016 | struct perf_sample *sample, | |
1017 | struct machine *machine) | |
cdce9d73 | 1018 | { |
9ec3f4e4 ACM |
1019 | const u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
1020 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
1021 | const u64 prev_state = perf_evsel__intval(evsel, sample, "prev_state"); | |
39aeb52f | 1022 | struct work_atoms *out_events, *in_events; |
cdce9d73 | 1023 | struct thread *sched_out, *sched_in; |
7f7f8d0b | 1024 | u64 timestamp0, timestamp = sample->time; |
b91fc39f | 1025 | int cpu = sample->cpu, err = -1; |
ea92ed5a IM |
1026 | s64 delta; |
1027 | ||
39aeb52f | 1028 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); |
ea92ed5a | 1029 | |
0e9b07e5 ACM |
1030 | timestamp0 = sched->cpu_last_switched[cpu]; |
1031 | sched->cpu_last_switched[cpu] = timestamp; | |
ea92ed5a IM |
1032 | if (timestamp0) |
1033 | delta = timestamp - timestamp0; | |
1034 | else | |
1035 | delta = 0; | |
1036 | ||
a116e05d ACM |
1037 | if (delta < 0) { |
1038 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); | |
1039 | return -1; | |
1040 | } | |
cdce9d73 | 1041 | |
1fcb8768 AH |
1042 | sched_out = machine__findnew_thread(machine, -1, prev_pid); |
1043 | sched_in = machine__findnew_thread(machine, -1, next_pid); | |
b91fc39f ACM |
1044 | if (sched_out == NULL || sched_in == NULL) |
1045 | goto out_put; | |
cdce9d73 | 1046 | |
0e9b07e5 | 1047 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
39aeb52f | 1048 | if (!out_events) { |
0e9b07e5 | 1049 | if (thread_atoms_insert(sched, sched_out)) |
b91fc39f | 1050 | goto out_put; |
0e9b07e5 | 1051 | out_events = thread_atoms_search(&sched->atom_root, sched_out, &sched->cmp_pid); |
a116e05d ACM |
1052 | if (!out_events) { |
1053 | pr_err("out-event: Internal tree error"); | |
b91fc39f | 1054 | goto out_put; |
a116e05d | 1055 | } |
39aeb52f | 1056 | } |
9ec3f4e4 | 1057 | if (add_sched_out_event(out_events, sched_out_state(prev_state), timestamp)) |
a116e05d | 1058 | return -1; |
39aeb52f | 1059 | |
0e9b07e5 | 1060 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
39aeb52f | 1061 | if (!in_events) { |
0e9b07e5 | 1062 | if (thread_atoms_insert(sched, sched_in)) |
b91fc39f | 1063 | goto out_put; |
0e9b07e5 | 1064 | in_events = thread_atoms_search(&sched->atom_root, sched_in, &sched->cmp_pid); |
a116e05d ACM |
1065 | if (!in_events) { |
1066 | pr_err("in-event: Internal tree error"); | |
b91fc39f | 1067 | goto out_put; |
a116e05d | 1068 | } |
39aeb52f | 1069 | /* |
1070 | * Take came in we have not heard about yet, | |
1071 | * add in an initial atom in runnable state: | |
1072 | */ | |
a116e05d | 1073 | if (add_sched_out_event(in_events, 'R', timestamp)) |
b91fc39f | 1074 | goto out_put; |
cdce9d73 | 1075 | } |
39aeb52f | 1076 | add_sched_in_event(in_events, timestamp); |
b91fc39f ACM |
1077 | err = 0; |
1078 | out_put: | |
1079 | thread__put(sched_out); | |
1080 | thread__put(sched_in); | |
1081 | return err; | |
39aeb52f | 1082 | } |
cdce9d73 | 1083 | |
9ec3f4e4 ACM |
1084 | static int latency_runtime_event(struct perf_sched *sched, |
1085 | struct perf_evsel *evsel, | |
1086 | struct perf_sample *sample, | |
1087 | struct machine *machine) | |
39aeb52f | 1088 | { |
9ec3f4e4 ACM |
1089 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
1090 | const u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
1fcb8768 | 1091 | struct thread *thread = machine__findnew_thread(machine, -1, pid); |
0e9b07e5 | 1092 | struct work_atoms *atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
7f7f8d0b | 1093 | u64 timestamp = sample->time; |
b91fc39f ACM |
1094 | int cpu = sample->cpu, err = -1; |
1095 | ||
1096 | if (thread == NULL) | |
1097 | return -1; | |
39aeb52f | 1098 | |
1099 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); | |
39aeb52f | 1100 | if (!atoms) { |
0e9b07e5 | 1101 | if (thread_atoms_insert(sched, thread)) |
b91fc39f | 1102 | goto out_put; |
0e9b07e5 | 1103 | atoms = thread_atoms_search(&sched->atom_root, thread, &sched->cmp_pid); |
a116e05d | 1104 | if (!atoms) { |
60b7d14a | 1105 | pr_err("in-event: Internal tree error"); |
b91fc39f | 1106 | goto out_put; |
a116e05d ACM |
1107 | } |
1108 | if (add_sched_out_event(atoms, 'R', timestamp)) | |
b91fc39f | 1109 | goto out_put; |
cdce9d73 FW |
1110 | } |
1111 | ||
9ec3f4e4 | 1112 | add_runtime_event(atoms, runtime, timestamp); |
b91fc39f ACM |
1113 | err = 0; |
1114 | out_put: | |
1115 | thread__put(thread); | |
1116 | return err; | |
cdce9d73 FW |
1117 | } |
1118 | ||
9ec3f4e4 ACM |
1119 | static int latency_wakeup_event(struct perf_sched *sched, |
1120 | struct perf_evsel *evsel, | |
1121 | struct perf_sample *sample, | |
1122 | struct machine *machine) | |
cdce9d73 | 1123 | { |
0680ee7d | 1124 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
39aeb52f | 1125 | struct work_atoms *atoms; |
b1ffe8f3 | 1126 | struct work_atom *atom; |
cdce9d73 | 1127 | struct thread *wakee; |
7f7f8d0b | 1128 | u64 timestamp = sample->time; |
b91fc39f | 1129 | int err = -1; |
cdce9d73 | 1130 | |
1fcb8768 | 1131 | wakee = machine__findnew_thread(machine, -1, pid); |
b91fc39f ACM |
1132 | if (wakee == NULL) |
1133 | return -1; | |
0e9b07e5 | 1134 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
17562205 | 1135 | if (!atoms) { |
0e9b07e5 | 1136 | if (thread_atoms_insert(sched, wakee)) |
b91fc39f | 1137 | goto out_put; |
0e9b07e5 | 1138 | atoms = thread_atoms_search(&sched->atom_root, wakee, &sched->cmp_pid); |
a116e05d | 1139 | if (!atoms) { |
60b7d14a | 1140 | pr_err("wakeup-event: Internal tree error"); |
b91fc39f | 1141 | goto out_put; |
a116e05d ACM |
1142 | } |
1143 | if (add_sched_out_event(atoms, 'S', timestamp)) | |
b91fc39f | 1144 | goto out_put; |
cdce9d73 FW |
1145 | } |
1146 | ||
39aeb52f | 1147 | BUG_ON(list_empty(&atoms->work_list)); |
cdce9d73 | 1148 | |
39aeb52f | 1149 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); |
cdce9d73 | 1150 | |
55ffb7a6 | 1151 | /* |
67d6259d DY |
1152 | * As we do not guarantee the wakeup event happens when |
1153 | * task is out of run queue, also may happen when task is | |
1154 | * on run queue and wakeup only change ->state to TASK_RUNNING, | |
1155 | * then we should not set the ->wake_up_time when wake up a | |
1156 | * task which is on run queue. | |
1157 | * | |
55ffb7a6 MG |
1158 | * You WILL be missing events if you've recorded only |
1159 | * one CPU, or are only looking at only one, so don't | |
67d6259d | 1160 | * skip in this case. |
55ffb7a6 | 1161 | */ |
0e9b07e5 | 1162 | if (sched->profile_cpu == -1 && atom->state != THREAD_SLEEPING) |
b91fc39f | 1163 | goto out_ok; |
cdce9d73 | 1164 | |
0e9b07e5 | 1165 | sched->nr_timestamps++; |
ea57c4f5 | 1166 | if (atom->sched_out_time > timestamp) { |
0e9b07e5 | 1167 | sched->nr_unordered_timestamps++; |
b91fc39f | 1168 | goto out_ok; |
ea57c4f5 | 1169 | } |
aa1ab9d2 | 1170 | |
b1ffe8f3 IM |
1171 | atom->state = THREAD_WAIT_CPU; |
1172 | atom->wake_up_time = timestamp; | |
b91fc39f ACM |
1173 | out_ok: |
1174 | err = 0; | |
1175 | out_put: | |
1176 | thread__put(wakee); | |
1177 | return err; | |
cdce9d73 FW |
1178 | } |
1179 | ||
9ec3f4e4 ACM |
1180 | static int latency_migrate_task_event(struct perf_sched *sched, |
1181 | struct perf_evsel *evsel, | |
1182 | struct perf_sample *sample, | |
1183 | struct machine *machine) | |
55ffb7a6 | 1184 | { |
9ec3f4e4 | 1185 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); |
7f7f8d0b | 1186 | u64 timestamp = sample->time; |
55ffb7a6 MG |
1187 | struct work_atoms *atoms; |
1188 | struct work_atom *atom; | |
1189 | struct thread *migrant; | |
b91fc39f | 1190 | int err = -1; |
55ffb7a6 MG |
1191 | |
1192 | /* | |
1193 | * Only need to worry about migration when profiling one CPU. | |
1194 | */ | |
0e9b07e5 | 1195 | if (sched->profile_cpu == -1) |
a116e05d | 1196 | return 0; |
55ffb7a6 | 1197 | |
1fcb8768 | 1198 | migrant = machine__findnew_thread(machine, -1, pid); |
b91fc39f ACM |
1199 | if (migrant == NULL) |
1200 | return -1; | |
0e9b07e5 | 1201 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
55ffb7a6 | 1202 | if (!atoms) { |
0e9b07e5 | 1203 | if (thread_atoms_insert(sched, migrant)) |
b91fc39f | 1204 | goto out_put; |
b9c5143a | 1205 | register_pid(sched, migrant->tid, thread__comm_str(migrant)); |
0e9b07e5 | 1206 | atoms = thread_atoms_search(&sched->atom_root, migrant, &sched->cmp_pid); |
a116e05d | 1207 | if (!atoms) { |
60b7d14a | 1208 | pr_err("migration-event: Internal tree error"); |
b91fc39f | 1209 | goto out_put; |
a116e05d ACM |
1210 | } |
1211 | if (add_sched_out_event(atoms, 'R', timestamp)) | |
b91fc39f | 1212 | goto out_put; |
55ffb7a6 MG |
1213 | } |
1214 | ||
1215 | BUG_ON(list_empty(&atoms->work_list)); | |
1216 | ||
1217 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | |
1218 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; | |
1219 | ||
0e9b07e5 | 1220 | sched->nr_timestamps++; |
55ffb7a6 MG |
1221 | |
1222 | if (atom->sched_out_time > timestamp) | |
0e9b07e5 | 1223 | sched->nr_unordered_timestamps++; |
b91fc39f ACM |
1224 | err = 0; |
1225 | out_put: | |
1226 | thread__put(migrant); | |
1227 | return err; | |
55ffb7a6 MG |
1228 | } |
1229 | ||
0e9b07e5 | 1230 | static void output_lat_thread(struct perf_sched *sched, struct work_atoms *work_list) |
cdce9d73 | 1231 | { |
cdce9d73 FW |
1232 | int i; |
1233 | int ret; | |
66685678 | 1234 | u64 avg; |
99620a5d | 1235 | char max_lat_at[32]; |
cdce9d73 | 1236 | |
39aeb52f | 1237 | if (!work_list->nb_atoms) |
cdce9d73 | 1238 | return; |
ea57c4f5 IM |
1239 | /* |
1240 | * Ignore idle threads: | |
1241 | */ | |
b9c5143a | 1242 | if (!strcmp(thread__comm_str(work_list->thread), "swapper")) |
ea57c4f5 | 1243 | return; |
cdce9d73 | 1244 | |
0e9b07e5 ACM |
1245 | sched->all_runtime += work_list->total_runtime; |
1246 | sched->all_count += work_list->nb_atoms; | |
66685678 | 1247 | |
2f80dd44 JB |
1248 | if (work_list->num_merged > 1) |
1249 | ret = printf(" %s:(%d) ", thread__comm_str(work_list->thread), work_list->num_merged); | |
1250 | else | |
1251 | ret = printf(" %s:%d ", thread__comm_str(work_list->thread), work_list->thread->tid); | |
cdce9d73 | 1252 | |
08f69e6c | 1253 | for (i = 0; i < 24 - ret; i++) |
cdce9d73 FW |
1254 | printf(" "); |
1255 | ||
39aeb52f | 1256 | avg = work_list->total_lat / work_list->nb_atoms; |
99620a5d | 1257 | timestamp__scnprintf_usec(work_list->max_lat_at, max_lat_at, sizeof(max_lat_at)); |
cdce9d73 | 1258 | |
99620a5d | 1259 | printf("|%11.3f ms |%9" PRIu64 " | avg:%9.3f ms | max:%9.3f ms | max at: %13s s\n", |
4fc76e49 ACM |
1260 | (double)work_list->total_runtime / NSEC_PER_MSEC, |
1261 | work_list->nb_atoms, (double)avg / NSEC_PER_MSEC, | |
1262 | (double)work_list->max_lat / NSEC_PER_MSEC, | |
99620a5d | 1263 | max_lat_at); |
cdce9d73 FW |
1264 | } |
1265 | ||
39aeb52f | 1266 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 | 1267 | { |
0014de17 JO |
1268 | if (l->thread == r->thread) |
1269 | return 0; | |
38051234 | 1270 | if (l->thread->tid < r->thread->tid) |
daa1d7a5 | 1271 | return -1; |
38051234 | 1272 | if (l->thread->tid > r->thread->tid) |
daa1d7a5 | 1273 | return 1; |
0014de17 | 1274 | return (int)(l->thread - r->thread); |
daa1d7a5 FW |
1275 | } |
1276 | ||
39aeb52f | 1277 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1278 | { |
1279 | u64 avgl, avgr; | |
1280 | ||
1281 | if (!l->nb_atoms) | |
1282 | return -1; | |
1283 | ||
1284 | if (!r->nb_atoms) | |
1285 | return 1; | |
1286 | ||
1287 | avgl = l->total_lat / l->nb_atoms; | |
1288 | avgr = r->total_lat / r->nb_atoms; | |
1289 | ||
1290 | if (avgl < avgr) | |
1291 | return -1; | |
1292 | if (avgl > avgr) | |
1293 | return 1; | |
1294 | ||
1295 | return 0; | |
1296 | } | |
1297 | ||
39aeb52f | 1298 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1299 | { |
1300 | if (l->max_lat < r->max_lat) | |
1301 | return -1; | |
1302 | if (l->max_lat > r->max_lat) | |
1303 | return 1; | |
1304 | ||
1305 | return 0; | |
1306 | } | |
1307 | ||
39aeb52f | 1308 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1309 | { |
1310 | if (l->nb_atoms < r->nb_atoms) | |
1311 | return -1; | |
1312 | if (l->nb_atoms > r->nb_atoms) | |
1313 | return 1; | |
1314 | ||
1315 | return 0; | |
1316 | } | |
1317 | ||
39aeb52f | 1318 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) |
daa1d7a5 FW |
1319 | { |
1320 | if (l->total_runtime < r->total_runtime) | |
1321 | return -1; | |
1322 | if (l->total_runtime > r->total_runtime) | |
1323 | return 1; | |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
cbef79a8 | 1328 | static int sort_dimension__add(const char *tok, struct list_head *list) |
daa1d7a5 | 1329 | { |
0e9b07e5 ACM |
1330 | size_t i; |
1331 | static struct sort_dimension avg_sort_dimension = { | |
1332 | .name = "avg", | |
1333 | .cmp = avg_cmp, | |
1334 | }; | |
1335 | static struct sort_dimension max_sort_dimension = { | |
1336 | .name = "max", | |
1337 | .cmp = max_cmp, | |
1338 | }; | |
1339 | static struct sort_dimension pid_sort_dimension = { | |
1340 | .name = "pid", | |
1341 | .cmp = pid_cmp, | |
1342 | }; | |
1343 | static struct sort_dimension runtime_sort_dimension = { | |
1344 | .name = "runtime", | |
1345 | .cmp = runtime_cmp, | |
1346 | }; | |
1347 | static struct sort_dimension switch_sort_dimension = { | |
1348 | .name = "switch", | |
1349 | .cmp = switch_cmp, | |
1350 | }; | |
1351 | struct sort_dimension *available_sorts[] = { | |
1352 | &pid_sort_dimension, | |
1353 | &avg_sort_dimension, | |
1354 | &max_sort_dimension, | |
1355 | &switch_sort_dimension, | |
1356 | &runtime_sort_dimension, | |
1357 | }; | |
daa1d7a5 | 1358 | |
0e9b07e5 | 1359 | for (i = 0; i < ARRAY_SIZE(available_sorts); i++) { |
daa1d7a5 FW |
1360 | if (!strcmp(available_sorts[i]->name, tok)) { |
1361 | list_add_tail(&available_sorts[i]->list, list); | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | return -1; | |
1368 | } | |
1369 | ||
0e9b07e5 | 1370 | static void perf_sched__sort_lat(struct perf_sched *sched) |
daa1d7a5 FW |
1371 | { |
1372 | struct rb_node *node; | |
2f80dd44 JB |
1373 | struct rb_root *root = &sched->atom_root; |
1374 | again: | |
daa1d7a5 | 1375 | for (;;) { |
39aeb52f | 1376 | struct work_atoms *data; |
2f80dd44 | 1377 | node = rb_first(root); |
daa1d7a5 FW |
1378 | if (!node) |
1379 | break; | |
1380 | ||
2f80dd44 | 1381 | rb_erase(node, root); |
39aeb52f | 1382 | data = rb_entry(node, struct work_atoms, node); |
0e9b07e5 | 1383 | __thread_latency_insert(&sched->sorted_atom_root, data, &sched->sort_list); |
daa1d7a5 | 1384 | } |
2f80dd44 JB |
1385 | if (root == &sched->atom_root) { |
1386 | root = &sched->merged_atom_root; | |
1387 | goto again; | |
1388 | } | |
daa1d7a5 FW |
1389 | } |
1390 | ||
0e9b07e5 | 1391 | static int process_sched_wakeup_event(struct perf_tool *tool, |
2b7fcbc5 | 1392 | struct perf_evsel *evsel, |
1d037ca1 | 1393 | struct perf_sample *sample, |
4218e673 | 1394 | struct machine *machine) |
419ab0d6 | 1395 | { |
0e9b07e5 | 1396 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
419ab0d6 | 1397 | |
9ec3f4e4 ACM |
1398 | if (sched->tp_handler->wakeup_event) |
1399 | return sched->tp_handler->wakeup_event(sched, evsel, sample, machine); | |
a116e05d | 1400 | |
2b7fcbc5 | 1401 | return 0; |
419ab0d6 FW |
1402 | } |
1403 | ||
a151a37a JO |
1404 | union map_priv { |
1405 | void *ptr; | |
1406 | bool color; | |
1407 | }; | |
1408 | ||
1409 | static bool thread__has_color(struct thread *thread) | |
1410 | { | |
1411 | union map_priv priv = { | |
1412 | .ptr = thread__priv(thread), | |
1413 | }; | |
1414 | ||
1415 | return priv.color; | |
1416 | } | |
1417 | ||
1418 | static struct thread* | |
1419 | map__findnew_thread(struct perf_sched *sched, struct machine *machine, pid_t pid, pid_t tid) | |
1420 | { | |
1421 | struct thread *thread = machine__findnew_thread(machine, pid, tid); | |
1422 | union map_priv priv = { | |
1423 | .color = false, | |
1424 | }; | |
1425 | ||
1426 | if (!sched->map.color_pids || !thread || thread__priv(thread)) | |
1427 | return thread; | |
1428 | ||
1429 | if (thread_map__has(sched->map.color_pids, tid)) | |
1430 | priv.color = true; | |
1431 | ||
1432 | thread__set_priv(thread, priv.ptr); | |
1433 | return thread; | |
1434 | } | |
1435 | ||
9ec3f4e4 ACM |
1436 | static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel, |
1437 | struct perf_sample *sample, struct machine *machine) | |
0ec04e16 | 1438 | { |
9d372ca5 DY |
1439 | const u32 next_pid = perf_evsel__intval(evsel, sample, "next_pid"); |
1440 | struct thread *sched_in; | |
0ec04e16 | 1441 | int new_shortname; |
7f7f8d0b | 1442 | u64 timestamp0, timestamp = sample->time; |
0ec04e16 | 1443 | s64 delta; |
99623c62 JO |
1444 | int i, this_cpu = sample->cpu; |
1445 | int cpus_nr; | |
1446 | bool new_cpu = false; | |
8cd91195 | 1447 | const char *color = PERF_COLOR_NORMAL; |
99620a5d | 1448 | char stimestamp[32]; |
0ec04e16 IM |
1449 | |
1450 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); | |
1451 | ||
0e9b07e5 ACM |
1452 | if (this_cpu > sched->max_cpu) |
1453 | sched->max_cpu = this_cpu; | |
0ec04e16 | 1454 | |
99623c62 JO |
1455 | if (sched->map.comp) { |
1456 | cpus_nr = bitmap_weight(sched->map.comp_cpus_mask, MAX_CPUS); | |
1457 | if (!test_and_set_bit(this_cpu, sched->map.comp_cpus_mask)) { | |
1458 | sched->map.comp_cpus[cpus_nr++] = this_cpu; | |
1459 | new_cpu = true; | |
1460 | } | |
1461 | } else | |
1462 | cpus_nr = sched->max_cpu; | |
1463 | ||
0e9b07e5 ACM |
1464 | timestamp0 = sched->cpu_last_switched[this_cpu]; |
1465 | sched->cpu_last_switched[this_cpu] = timestamp; | |
0ec04e16 IM |
1466 | if (timestamp0) |
1467 | delta = timestamp - timestamp0; | |
1468 | else | |
1469 | delta = 0; | |
1470 | ||
a116e05d | 1471 | if (delta < 0) { |
60b7d14a | 1472 | pr_err("hm, delta: %" PRIu64 " < 0 ?\n", delta); |
a116e05d ACM |
1473 | return -1; |
1474 | } | |
0ec04e16 | 1475 | |
a151a37a | 1476 | sched_in = map__findnew_thread(sched, machine, -1, next_pid); |
b91fc39f ACM |
1477 | if (sched_in == NULL) |
1478 | return -1; | |
0ec04e16 | 1479 | |
b91fc39f | 1480 | sched->curr_thread[this_cpu] = thread__get(sched_in); |
0ec04e16 IM |
1481 | |
1482 | printf(" "); | |
1483 | ||
1484 | new_shortname = 0; | |
1485 | if (!sched_in->shortname[0]) { | |
6bcab4e1 D |
1486 | if (!strcmp(thread__comm_str(sched_in), "swapper")) { |
1487 | /* | |
1488 | * Don't allocate a letter-number for swapper:0 | |
1489 | * as a shortname. Instead, we use '.' for it. | |
1490 | */ | |
1491 | sched_in->shortname[0] = '.'; | |
1492 | sched_in->shortname[1] = ' '; | |
0ec04e16 | 1493 | } else { |
6bcab4e1 D |
1494 | sched_in->shortname[0] = sched->next_shortname1; |
1495 | sched_in->shortname[1] = sched->next_shortname2; | |
1496 | ||
1497 | if (sched->next_shortname1 < 'Z') { | |
1498 | sched->next_shortname1++; | |
0ec04e16 | 1499 | } else { |
6bcab4e1 D |
1500 | sched->next_shortname1 = 'A'; |
1501 | if (sched->next_shortname2 < '9') | |
1502 | sched->next_shortname2++; | |
1503 | else | |
1504 | sched->next_shortname2 = '0'; | |
0ec04e16 IM |
1505 | } |
1506 | } | |
1507 | new_shortname = 1; | |
1508 | } | |
1509 | ||
99623c62 JO |
1510 | for (i = 0; i < cpus_nr; i++) { |
1511 | int cpu = sched->map.comp ? sched->map.comp_cpus[i] : i; | |
a151a37a JO |
1512 | struct thread *curr_thread = sched->curr_thread[cpu]; |
1513 | const char *pid_color = color; | |
cf294f24 | 1514 | const char *cpu_color = color; |
a151a37a JO |
1515 | |
1516 | if (curr_thread && thread__has_color(curr_thread)) | |
1517 | pid_color = COLOR_PIDS; | |
99623c62 | 1518 | |
73643bb6 JO |
1519 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, cpu)) |
1520 | continue; | |
1521 | ||
cf294f24 JO |
1522 | if (sched->map.color_cpus && cpu_map__has(sched->map.color_cpus, cpu)) |
1523 | cpu_color = COLOR_CPUS; | |
1524 | ||
0ec04e16 | 1525 | if (cpu != this_cpu) |
1208bb27 | 1526 | color_fprintf(stdout, color, " "); |
0ec04e16 | 1527 | else |
cf294f24 | 1528 | color_fprintf(stdout, cpu_color, "*"); |
0ec04e16 | 1529 | |
6bcab4e1 | 1530 | if (sched->curr_thread[cpu]) |
a151a37a | 1531 | color_fprintf(stdout, pid_color, "%2s ", sched->curr_thread[cpu]->shortname); |
6bcab4e1 | 1532 | else |
8cd91195 | 1533 | color_fprintf(stdout, color, " "); |
0ec04e16 IM |
1534 | } |
1535 | ||
73643bb6 JO |
1536 | if (sched->map.cpus && !cpu_map__has(sched->map.cpus, this_cpu)) |
1537 | goto out; | |
1538 | ||
99620a5d NK |
1539 | timestamp__scnprintf_usec(timestamp, stimestamp, sizeof(stimestamp)); |
1540 | color_fprintf(stdout, color, " %12s secs ", stimestamp); | |
e107f129 | 1541 | if (new_shortname || (verbose && sched_in->tid)) { |
a151a37a JO |
1542 | const char *pid_color = color; |
1543 | ||
1544 | if (thread__has_color(sched_in)) | |
1545 | pid_color = COLOR_PIDS; | |
1546 | ||
1547 | color_fprintf(stdout, pid_color, "%s => %s:%d", | |
b9c5143a | 1548 | sched_in->shortname, thread__comm_str(sched_in), sched_in->tid); |
0ec04e16 | 1549 | } |
a116e05d | 1550 | |
99623c62 | 1551 | if (sched->map.comp && new_cpu) |
8cd91195 | 1552 | color_fprintf(stdout, color, " (CPU %d)", this_cpu); |
99623c62 | 1553 | |
73643bb6 | 1554 | out: |
8cd91195 | 1555 | color_fprintf(stdout, color, "\n"); |
99623c62 | 1556 | |
b91fc39f ACM |
1557 | thread__put(sched_in); |
1558 | ||
a116e05d | 1559 | return 0; |
0ec04e16 IM |
1560 | } |
1561 | ||
0e9b07e5 | 1562 | static int process_sched_switch_event(struct perf_tool *tool, |
2b7fcbc5 | 1563 | struct perf_evsel *evsel, |
1d037ca1 | 1564 | struct perf_sample *sample, |
4218e673 | 1565 | struct machine *machine) |
419ab0d6 | 1566 | { |
0e9b07e5 | 1567 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
a116e05d | 1568 | int this_cpu = sample->cpu, err = 0; |
2b7fcbc5 ACM |
1569 | u32 prev_pid = perf_evsel__intval(evsel, sample, "prev_pid"), |
1570 | next_pid = perf_evsel__intval(evsel, sample, "next_pid"); | |
419ab0d6 | 1571 | |
0e9b07e5 | 1572 | if (sched->curr_pid[this_cpu] != (u32)-1) { |
c8a37751 IM |
1573 | /* |
1574 | * Are we trying to switch away a PID that is | |
1575 | * not current? | |
1576 | */ | |
2b7fcbc5 | 1577 | if (sched->curr_pid[this_cpu] != prev_pid) |
0e9b07e5 | 1578 | sched->nr_context_switch_bugs++; |
c8a37751 | 1579 | } |
c8a37751 | 1580 | |
9ec3f4e4 ACM |
1581 | if (sched->tp_handler->switch_event) |
1582 | err = sched->tp_handler->switch_event(sched, evsel, sample, machine); | |
2b7fcbc5 ACM |
1583 | |
1584 | sched->curr_pid[this_cpu] = next_pid; | |
a116e05d | 1585 | return err; |
419ab0d6 FW |
1586 | } |
1587 | ||
0e9b07e5 | 1588 | static int process_sched_runtime_event(struct perf_tool *tool, |
2b7fcbc5 | 1589 | struct perf_evsel *evsel, |
1d037ca1 | 1590 | struct perf_sample *sample, |
4218e673 | 1591 | struct machine *machine) |
39aeb52f | 1592 | { |
0e9b07e5 | 1593 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
39aeb52f | 1594 | |
9ec3f4e4 ACM |
1595 | if (sched->tp_handler->runtime_event) |
1596 | return sched->tp_handler->runtime_event(sched, evsel, sample, machine); | |
a116e05d | 1597 | |
2b7fcbc5 | 1598 | return 0; |
39aeb52f | 1599 | } |
1600 | ||
cb627505 DA |
1601 | static int perf_sched__process_fork_event(struct perf_tool *tool, |
1602 | union perf_event *event, | |
1603 | struct perf_sample *sample, | |
1604 | struct machine *machine) | |
fbf94829 | 1605 | { |
0e9b07e5 | 1606 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
46538818 | 1607 | |
cb627505 DA |
1608 | /* run the fork event through the perf machineruy */ |
1609 | perf_event__process_fork(tool, event, sample, machine); | |
1610 | ||
1611 | /* and then run additional processing needed for this command */ | |
9ec3f4e4 | 1612 | if (sched->tp_handler->fork_event) |
cb627505 | 1613 | return sched->tp_handler->fork_event(sched, event, machine); |
a116e05d | 1614 | |
2b7fcbc5 | 1615 | return 0; |
fbf94829 IM |
1616 | } |
1617 | ||
0e9b07e5 | 1618 | static int process_sched_migrate_task_event(struct perf_tool *tool, |
2b7fcbc5 | 1619 | struct perf_evsel *evsel, |
1d037ca1 | 1620 | struct perf_sample *sample, |
4218e673 | 1621 | struct machine *machine) |
55ffb7a6 | 1622 | { |
0e9b07e5 | 1623 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
55ffb7a6 | 1624 | |
9ec3f4e4 ACM |
1625 | if (sched->tp_handler->migrate_task_event) |
1626 | return sched->tp_handler->migrate_task_event(sched, evsel, sample, machine); | |
a116e05d | 1627 | |
2b7fcbc5 | 1628 | return 0; |
55ffb7a6 MG |
1629 | } |
1630 | ||
a116e05d | 1631 | typedef int (*tracepoint_handler)(struct perf_tool *tool, |
2b7fcbc5 | 1632 | struct perf_evsel *evsel, |
a116e05d | 1633 | struct perf_sample *sample, |
4218e673 | 1634 | struct machine *machine); |
ec156764 | 1635 | |
1d037ca1 IT |
1636 | static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_unused, |
1637 | union perf_event *event __maybe_unused, | |
ee29be62 ACM |
1638 | struct perf_sample *sample, |
1639 | struct perf_evsel *evsel, | |
1640 | struct machine *machine) | |
0a02ad93 | 1641 | { |
a116e05d | 1642 | int err = 0; |
0a02ad93 | 1643 | |
744a9719 ACM |
1644 | if (evsel->handler != NULL) { |
1645 | tracepoint_handler f = evsel->handler; | |
2b7fcbc5 | 1646 | err = f(tool, evsel, sample, machine); |
ee29be62 | 1647 | } |
0a02ad93 | 1648 | |
a116e05d | 1649 | return err; |
0a02ad93 IM |
1650 | } |
1651 | ||
ae536acf | 1652 | static int perf_sched__read_events(struct perf_sched *sched) |
0a02ad93 | 1653 | { |
ee29be62 ACM |
1654 | const struct perf_evsel_str_handler handlers[] = { |
1655 | { "sched:sched_switch", process_sched_switch_event, }, | |
1656 | { "sched:sched_stat_runtime", process_sched_runtime_event, }, | |
1657 | { "sched:sched_wakeup", process_sched_wakeup_event, }, | |
1658 | { "sched:sched_wakeup_new", process_sched_wakeup_event, }, | |
ee29be62 ACM |
1659 | { "sched:sched_migrate_task", process_sched_migrate_task_event, }, |
1660 | }; | |
da378962 | 1661 | struct perf_session *session; |
f5fc1412 JO |
1662 | struct perf_data_file file = { |
1663 | .path = input_name, | |
1664 | .mode = PERF_DATA_MODE_READ, | |
f0dd330f | 1665 | .force = sched->force, |
f5fc1412 | 1666 | }; |
ae536acf | 1667 | int rc = -1; |
da378962 | 1668 | |
f5fc1412 | 1669 | session = perf_session__new(&file, false, &sched->tool); |
a116e05d ACM |
1670 | if (session == NULL) { |
1671 | pr_debug("No Memory for session\n"); | |
1672 | return -1; | |
1673 | } | |
94c744b6 | 1674 | |
0a7e6d1b | 1675 | symbol__init(&session->header.env); |
04934106 | 1676 | |
a116e05d ACM |
1677 | if (perf_session__set_tracepoints_handlers(session, handlers)) |
1678 | goto out_delete; | |
ee29be62 | 1679 | |
cee75ac7 | 1680 | if (perf_session__has_traces(session, "record -R")) { |
b7b61cbe | 1681 | int err = perf_session__process_events(session); |
a116e05d ACM |
1682 | if (err) { |
1683 | pr_err("Failed to process events, error %d", err); | |
1684 | goto out_delete; | |
1685 | } | |
4c09bafa | 1686 | |
75be989a ACM |
1687 | sched->nr_events = session->evlist->stats.nr_events[0]; |
1688 | sched->nr_lost_events = session->evlist->stats.total_lost; | |
1689 | sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; | |
cee75ac7 | 1690 | } |
d549c769 | 1691 | |
ae536acf | 1692 | rc = 0; |
a116e05d ACM |
1693 | out_delete: |
1694 | perf_session__delete(session); | |
ae536acf | 1695 | return rc; |
0a02ad93 IM |
1696 | } |
1697 | ||
49394a2a DA |
1698 | /* |
1699 | * scheduling times are printed as msec.usec | |
1700 | */ | |
1701 | static inline void print_sched_time(unsigned long long nsecs, int width) | |
1702 | { | |
1703 | unsigned long msecs; | |
1704 | unsigned long usecs; | |
1705 | ||
1706 | msecs = nsecs / NSEC_PER_MSEC; | |
1707 | nsecs -= msecs * NSEC_PER_MSEC; | |
1708 | usecs = nsecs / NSEC_PER_USEC; | |
1709 | printf("%*lu.%03lu ", width, msecs, usecs); | |
1710 | } | |
1711 | ||
1712 | /* | |
1713 | * returns runtime data for event, allocating memory for it the | |
1714 | * first time it is used. | |
1715 | */ | |
1716 | static struct evsel_runtime *perf_evsel__get_runtime(struct perf_evsel *evsel) | |
1717 | { | |
1718 | struct evsel_runtime *r = evsel->priv; | |
1719 | ||
1720 | if (r == NULL) { | |
1721 | r = zalloc(sizeof(struct evsel_runtime)); | |
1722 | evsel->priv = r; | |
1723 | } | |
1724 | ||
1725 | return r; | |
1726 | } | |
1727 | ||
1728 | /* | |
1729 | * save last time event was seen per cpu | |
1730 | */ | |
1731 | static void perf_evsel__save_time(struct perf_evsel *evsel, | |
1732 | u64 timestamp, u32 cpu) | |
1733 | { | |
1734 | struct evsel_runtime *r = perf_evsel__get_runtime(evsel); | |
1735 | ||
1736 | if (r == NULL) | |
1737 | return; | |
1738 | ||
1739 | if ((cpu >= r->ncpu) || (r->last_time == NULL)) { | |
1740 | int i, n = __roundup_pow_of_two(cpu+1); | |
1741 | void *p = r->last_time; | |
1742 | ||
1743 | p = realloc(r->last_time, n * sizeof(u64)); | |
1744 | if (!p) | |
1745 | return; | |
1746 | ||
1747 | r->last_time = p; | |
1748 | for (i = r->ncpu; i < n; ++i) | |
1749 | r->last_time[i] = (u64) 0; | |
1750 | ||
1751 | r->ncpu = n; | |
1752 | } | |
1753 | ||
1754 | r->last_time[cpu] = timestamp; | |
1755 | } | |
1756 | ||
1757 | /* returns last time this event was seen on the given cpu */ | |
1758 | static u64 perf_evsel__get_time(struct perf_evsel *evsel, u32 cpu) | |
1759 | { | |
1760 | struct evsel_runtime *r = perf_evsel__get_runtime(evsel); | |
1761 | ||
1762 | if ((r == NULL) || (r->last_time == NULL) || (cpu >= r->ncpu)) | |
1763 | return 0; | |
1764 | ||
1765 | return r->last_time[cpu]; | |
1766 | } | |
1767 | ||
1768 | static int comm_width = 20; | |
1769 | ||
1770 | static char *timehist_get_commstr(struct thread *thread) | |
1771 | { | |
1772 | static char str[32]; | |
1773 | const char *comm = thread__comm_str(thread); | |
1774 | pid_t tid = thread->tid; | |
1775 | pid_t pid = thread->pid_; | |
1776 | int n; | |
1777 | ||
1778 | if (pid == 0) | |
1779 | n = scnprintf(str, sizeof(str), "%s", comm); | |
1780 | ||
1781 | else if (tid != pid) | |
1782 | n = scnprintf(str, sizeof(str), "%s[%d/%d]", comm, tid, pid); | |
1783 | ||
1784 | else | |
1785 | n = scnprintf(str, sizeof(str), "%s[%d]", comm, tid); | |
1786 | ||
1787 | if (n > comm_width) | |
1788 | comm_width = n; | |
1789 | ||
1790 | return str; | |
1791 | } | |
1792 | ||
a407b067 | 1793 | static void timehist_header(struct perf_sched *sched) |
49394a2a | 1794 | { |
a407b067 DA |
1795 | u32 ncpus = sched->max_cpu + 1; |
1796 | u32 i, j; | |
1797 | ||
49394a2a DA |
1798 | printf("%15s %6s ", "time", "cpu"); |
1799 | ||
a407b067 DA |
1800 | if (sched->show_cpu_visual) { |
1801 | printf(" "); | |
1802 | for (i = 0, j = 0; i < ncpus; ++i) { | |
1803 | printf("%x", j++); | |
1804 | if (j > 15) | |
1805 | j = 0; | |
1806 | } | |
1807 | printf(" "); | |
1808 | } | |
1809 | ||
49394a2a DA |
1810 | printf(" %-20s %9s %9s %9s", |
1811 | "task name", "wait time", "sch delay", "run time"); | |
1812 | ||
1813 | printf("\n"); | |
1814 | ||
1815 | /* | |
1816 | * units row | |
1817 | */ | |
1818 | printf("%15s %-6s ", "", ""); | |
1819 | ||
a407b067 DA |
1820 | if (sched->show_cpu_visual) |
1821 | printf(" %*s ", ncpus, ""); | |
1822 | ||
49394a2a DA |
1823 | printf(" %-20s %9s %9s %9s\n", "[tid/pid]", "(msec)", "(msec)", "(msec)"); |
1824 | ||
1825 | /* | |
1826 | * separator | |
1827 | */ | |
1828 | printf("%.15s %.6s ", graph_dotted_line, graph_dotted_line); | |
1829 | ||
a407b067 DA |
1830 | if (sched->show_cpu_visual) |
1831 | printf(" %.*s ", ncpus, graph_dotted_line); | |
1832 | ||
49394a2a DA |
1833 | printf(" %.20s %.9s %.9s %.9s", |
1834 | graph_dotted_line, graph_dotted_line, graph_dotted_line, | |
1835 | graph_dotted_line); | |
1836 | ||
1837 | printf("\n"); | |
1838 | } | |
1839 | ||
fc1469f1 DA |
1840 | static void timehist_print_sample(struct perf_sched *sched, |
1841 | struct perf_sample *sample, | |
6c973c90 | 1842 | struct addr_location *al, |
853b7407 DA |
1843 | struct thread *thread, |
1844 | u64 t) | |
49394a2a DA |
1845 | { |
1846 | struct thread_runtime *tr = thread__priv(thread); | |
a407b067 | 1847 | u32 max_cpus = sched->max_cpu + 1; |
49394a2a DA |
1848 | char tstr[64]; |
1849 | ||
853b7407 | 1850 | timestamp__scnprintf_usec(t, tstr, sizeof(tstr)); |
49394a2a DA |
1851 | printf("%15s [%04d] ", tstr, sample->cpu); |
1852 | ||
a407b067 DA |
1853 | if (sched->show_cpu_visual) { |
1854 | u32 i; | |
1855 | char c; | |
1856 | ||
1857 | printf(" "); | |
1858 | for (i = 0; i < max_cpus; ++i) { | |
1859 | /* flag idle times with 'i'; others are sched events */ | |
1860 | if (i == sample->cpu) | |
1861 | c = (thread->tid == 0) ? 'i' : 's'; | |
1862 | else | |
1863 | c = ' '; | |
1864 | printf("%c", c); | |
1865 | } | |
1866 | printf(" "); | |
1867 | } | |
1868 | ||
49394a2a DA |
1869 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); |
1870 | ||
1871 | print_sched_time(tr->dt_wait, 6); | |
1872 | print_sched_time(tr->dt_delay, 6); | |
1873 | print_sched_time(tr->dt_run, 6); | |
fc1469f1 DA |
1874 | |
1875 | if (sched->show_wakeups) | |
1876 | printf(" %-*s", comm_width, ""); | |
1877 | ||
6c973c90 DA |
1878 | if (thread->tid == 0) |
1879 | goto out; | |
1880 | ||
1881 | if (sched->show_callchain) | |
1882 | printf(" "); | |
1883 | ||
1884 | sample__fprintf_sym(sample, al, 0, | |
1885 | EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE | | |
2d9bbf6e NK |
1886 | EVSEL__PRINT_CALLCHAIN_ARROW | |
1887 | EVSEL__PRINT_SKIP_IGNORED, | |
6c973c90 DA |
1888 | &callchain_cursor, stdout); |
1889 | ||
1890 | out: | |
49394a2a DA |
1891 | printf("\n"); |
1892 | } | |
1893 | ||
1894 | /* | |
1895 | * Explanation of delta-time stats: | |
1896 | * | |
1897 | * t = time of current schedule out event | |
1898 | * tprev = time of previous sched out event | |
1899 | * also time of schedule-in event for current task | |
1900 | * last_time = time of last sched change event for current task | |
1901 | * (i.e, time process was last scheduled out) | |
1902 | * ready_to_run = time of wakeup for current task | |
1903 | * | |
1904 | * -----|------------|------------|------------|------ | |
1905 | * last ready tprev t | |
1906 | * time to run | |
1907 | * | |
1908 | * |-------- dt_wait --------| | |
1909 | * |- dt_delay -|-- dt_run --| | |
1910 | * | |
1911 | * dt_run = run time of current task | |
1912 | * dt_wait = time between last schedule out event for task and tprev | |
1913 | * represents time spent off the cpu | |
1914 | * dt_delay = time between wakeup and schedule-in of task | |
1915 | */ | |
1916 | ||
1917 | static void timehist_update_runtime_stats(struct thread_runtime *r, | |
1918 | u64 t, u64 tprev) | |
1919 | { | |
1920 | r->dt_delay = 0; | |
1921 | r->dt_wait = 0; | |
1922 | r->dt_run = 0; | |
1923 | if (tprev) { | |
1924 | r->dt_run = t - tprev; | |
1925 | if (r->ready_to_run) { | |
1926 | if (r->ready_to_run > tprev) | |
1927 | pr_debug("time travel: wakeup time for task > previous sched_switch event\n"); | |
1928 | else | |
1929 | r->dt_delay = tprev - r->ready_to_run; | |
1930 | } | |
1931 | ||
1932 | if (r->last_time > tprev) | |
1933 | pr_debug("time travel: last sched out time for task > previous sched_switch event\n"); | |
1934 | else if (r->last_time) | |
1935 | r->dt_wait = tprev - r->last_time; | |
1936 | } | |
1937 | ||
1938 | update_stats(&r->run_stats, r->dt_run); | |
1939 | r->total_run_time += r->dt_run; | |
1940 | } | |
1941 | ||
96039c7c NK |
1942 | static bool is_idle_sample(struct perf_sample *sample, |
1943 | struct perf_evsel *evsel) | |
49394a2a DA |
1944 | { |
1945 | /* pid 0 == swapper == idle task */ | |
96039c7c NK |
1946 | if (strcmp(perf_evsel__name(evsel), "sched:sched_switch") == 0) |
1947 | return perf_evsel__intval(evsel, sample, "prev_pid") == 0; | |
49394a2a | 1948 | |
96039c7c NK |
1949 | return sample->pid == 0; |
1950 | } | |
1951 | ||
1952 | static void save_task_callchain(struct perf_sched *sched, | |
1953 | struct perf_sample *sample, | |
1954 | struct perf_evsel *evsel, | |
1955 | struct machine *machine) | |
1956 | { | |
1957 | struct callchain_cursor *cursor = &callchain_cursor; | |
1958 | struct thread *thread; | |
6c973c90 DA |
1959 | |
1960 | /* want main thread for process - has maps */ | |
1961 | thread = machine__findnew_thread(machine, sample->pid, sample->pid); | |
1962 | if (thread == NULL) { | |
1963 | pr_debug("Failed to get thread for pid %d.\n", sample->pid); | |
96039c7c | 1964 | return; |
6c973c90 DA |
1965 | } |
1966 | ||
1967 | if (!symbol_conf.use_callchain || sample->callchain == NULL) | |
96039c7c | 1968 | return; |
6c973c90 DA |
1969 | |
1970 | if (thread__resolve_callchain(thread, cursor, evsel, sample, | |
8388deb3 | 1971 | NULL, NULL, sched->max_stack + 2) != 0) { |
6c973c90 DA |
1972 | if (verbose) |
1973 | error("Failed to resolve callchain. Skipping\n"); | |
1974 | ||
96039c7c | 1975 | return; |
6c973c90 | 1976 | } |
cdeb01bf | 1977 | |
6c973c90 | 1978 | callchain_cursor_commit(cursor); |
cdeb01bf NK |
1979 | |
1980 | while (true) { | |
1981 | struct callchain_cursor_node *node; | |
1982 | struct symbol *sym; | |
1983 | ||
1984 | node = callchain_cursor_current(cursor); | |
1985 | if (node == NULL) | |
1986 | break; | |
1987 | ||
1988 | sym = node->sym; | |
1989 | if (sym && sym->name) { | |
1990 | if (!strcmp(sym->name, "schedule") || | |
1991 | !strcmp(sym->name, "__schedule") || | |
1992 | !strcmp(sym->name, "preempt_schedule")) | |
1993 | sym->ignore = 1; | |
1994 | } | |
1995 | ||
1996 | callchain_cursor_advance(cursor); | |
1997 | } | |
49394a2a DA |
1998 | } |
1999 | ||
2000 | /* | |
2001 | * Track idle stats per cpu by maintaining a local thread | |
2002 | * struct for the idle task on each cpu. | |
2003 | */ | |
2004 | static int init_idle_threads(int ncpu) | |
2005 | { | |
2006 | int i; | |
2007 | ||
2008 | idle_threads = zalloc(ncpu * sizeof(struct thread *)); | |
2009 | if (!idle_threads) | |
2010 | return -ENOMEM; | |
2011 | ||
b336352b | 2012 | idle_max_cpu = ncpu; |
49394a2a DA |
2013 | |
2014 | /* allocate the actual thread struct if needed */ | |
2015 | for (i = 0; i < ncpu; ++i) { | |
2016 | idle_threads[i] = thread__new(0, 0); | |
2017 | if (idle_threads[i] == NULL) | |
2018 | return -ENOMEM; | |
2019 | ||
2020 | thread__set_comm(idle_threads[i], idle_comm, 0); | |
2021 | } | |
2022 | ||
2023 | return 0; | |
2024 | } | |
2025 | ||
2026 | static void free_idle_threads(void) | |
2027 | { | |
2028 | int i; | |
2029 | ||
2030 | if (idle_threads == NULL) | |
2031 | return; | |
2032 | ||
b336352b | 2033 | for (i = 0; i < idle_max_cpu; ++i) { |
49394a2a DA |
2034 | if ((idle_threads[i])) |
2035 | thread__delete(idle_threads[i]); | |
2036 | } | |
2037 | ||
2038 | free(idle_threads); | |
2039 | } | |
2040 | ||
2041 | static struct thread *get_idle_thread(int cpu) | |
2042 | { | |
2043 | /* | |
2044 | * expand/allocate array of pointers to local thread | |
2045 | * structs if needed | |
2046 | */ | |
2047 | if ((cpu >= idle_max_cpu) || (idle_threads == NULL)) { | |
2048 | int i, j = __roundup_pow_of_two(cpu+1); | |
2049 | void *p; | |
2050 | ||
2051 | p = realloc(idle_threads, j * sizeof(struct thread *)); | |
2052 | if (!p) | |
2053 | return NULL; | |
2054 | ||
2055 | idle_threads = (struct thread **) p; | |
b336352b | 2056 | for (i = idle_max_cpu; i < j; ++i) |
49394a2a DA |
2057 | idle_threads[i] = NULL; |
2058 | ||
2059 | idle_max_cpu = j; | |
2060 | } | |
2061 | ||
2062 | /* allocate a new thread struct if needed */ | |
2063 | if (idle_threads[cpu] == NULL) { | |
2064 | idle_threads[cpu] = thread__new(0, 0); | |
2065 | ||
2066 | if (idle_threads[cpu]) { | |
2067 | idle_threads[cpu]->tid = 0; | |
2068 | thread__set_comm(idle_threads[cpu], idle_comm, 0); | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | return idle_threads[cpu]; | |
2073 | } | |
2074 | ||
2075 | /* | |
2076 | * handle runtime stats saved per thread | |
2077 | */ | |
2078 | static struct thread_runtime *thread__init_runtime(struct thread *thread) | |
2079 | { | |
2080 | struct thread_runtime *r; | |
2081 | ||
2082 | r = zalloc(sizeof(struct thread_runtime)); | |
2083 | if (!r) | |
2084 | return NULL; | |
2085 | ||
2086 | init_stats(&r->run_stats); | |
2087 | thread__set_priv(thread, r); | |
2088 | ||
2089 | return r; | |
2090 | } | |
2091 | ||
2092 | static struct thread_runtime *thread__get_runtime(struct thread *thread) | |
2093 | { | |
2094 | struct thread_runtime *tr; | |
2095 | ||
2096 | tr = thread__priv(thread); | |
2097 | if (tr == NULL) { | |
2098 | tr = thread__init_runtime(thread); | |
2099 | if (tr == NULL) | |
2100 | pr_debug("Failed to malloc memory for runtime data.\n"); | |
2101 | } | |
2102 | ||
2103 | return tr; | |
2104 | } | |
2105 | ||
6c973c90 DA |
2106 | static struct thread *timehist_get_thread(struct perf_sched *sched, |
2107 | struct perf_sample *sample, | |
49394a2a DA |
2108 | struct machine *machine, |
2109 | struct perf_evsel *evsel) | |
2110 | { | |
2111 | struct thread *thread; | |
2112 | ||
96039c7c | 2113 | if (is_idle_sample(sample, evsel)) { |
49394a2a DA |
2114 | thread = get_idle_thread(sample->cpu); |
2115 | if (thread == NULL) | |
2116 | pr_err("Failed to get idle thread for cpu %d.\n", sample->cpu); | |
2117 | ||
2118 | } else { | |
5d92d96a NK |
2119 | /* there were samples with tid 0 but non-zero pid */ |
2120 | thread = machine__findnew_thread(machine, sample->pid, | |
2121 | sample->tid ?: sample->pid); | |
49394a2a DA |
2122 | if (thread == NULL) { |
2123 | pr_debug("Failed to get thread for tid %d. skipping sample.\n", | |
2124 | sample->tid); | |
2125 | } | |
96039c7c NK |
2126 | |
2127 | save_task_callchain(sched, sample, evsel, machine); | |
49394a2a DA |
2128 | } |
2129 | ||
2130 | return thread; | |
2131 | } | |
2132 | ||
52df138c DA |
2133 | static bool timehist_skip_sample(struct perf_sched *sched, |
2134 | struct thread *thread) | |
49394a2a DA |
2135 | { |
2136 | bool rc = false; | |
2137 | ||
52df138c | 2138 | if (thread__is_filtered(thread)) { |
49394a2a | 2139 | rc = true; |
52df138c DA |
2140 | sched->skipped_samples++; |
2141 | } | |
49394a2a DA |
2142 | |
2143 | return rc; | |
2144 | } | |
2145 | ||
fc1469f1 DA |
2146 | static void timehist_print_wakeup_event(struct perf_sched *sched, |
2147 | struct perf_sample *sample, | |
2148 | struct machine *machine, | |
2149 | struct thread *awakened) | |
2150 | { | |
2151 | struct thread *thread; | |
2152 | char tstr[64]; | |
2153 | ||
2154 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); | |
2155 | if (thread == NULL) | |
2156 | return; | |
2157 | ||
2158 | /* show wakeup unless both awakee and awaker are filtered */ | |
2159 | if (timehist_skip_sample(sched, thread) && | |
2160 | timehist_skip_sample(sched, awakened)) { | |
2161 | return; | |
2162 | } | |
2163 | ||
2164 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); | |
2165 | printf("%15s [%04d] ", tstr, sample->cpu); | |
a407b067 DA |
2166 | if (sched->show_cpu_visual) |
2167 | printf(" %*s ", sched->max_cpu + 1, ""); | |
fc1469f1 DA |
2168 | |
2169 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); | |
2170 | ||
2171 | /* dt spacer */ | |
2172 | printf(" %9s %9s %9s ", "", "", ""); | |
2173 | ||
2174 | printf("awakened: %s", timehist_get_commstr(awakened)); | |
2175 | ||
2176 | printf("\n"); | |
2177 | } | |
2178 | ||
2179 | static int timehist_sched_wakeup_event(struct perf_tool *tool, | |
49394a2a DA |
2180 | union perf_event *event __maybe_unused, |
2181 | struct perf_evsel *evsel, | |
2182 | struct perf_sample *sample, | |
2183 | struct machine *machine) | |
2184 | { | |
fc1469f1 | 2185 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
49394a2a DA |
2186 | struct thread *thread; |
2187 | struct thread_runtime *tr = NULL; | |
2188 | /* want pid of awakened task not pid in sample */ | |
2189 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); | |
2190 | ||
2191 | thread = machine__findnew_thread(machine, 0, pid); | |
2192 | if (thread == NULL) | |
2193 | return -1; | |
2194 | ||
2195 | tr = thread__get_runtime(thread); | |
2196 | if (tr == NULL) | |
2197 | return -1; | |
2198 | ||
2199 | if (tr->ready_to_run == 0) | |
2200 | tr->ready_to_run = sample->time; | |
2201 | ||
fc1469f1 | 2202 | /* show wakeups if requested */ |
853b7407 DA |
2203 | if (sched->show_wakeups && |
2204 | !perf_time__skip_sample(&sched->ptime, sample->time)) | |
fc1469f1 DA |
2205 | timehist_print_wakeup_event(sched, sample, machine, thread); |
2206 | ||
49394a2a DA |
2207 | return 0; |
2208 | } | |
2209 | ||
350f54fa DA |
2210 | static void timehist_print_migration_event(struct perf_sched *sched, |
2211 | struct perf_evsel *evsel, | |
2212 | struct perf_sample *sample, | |
2213 | struct machine *machine, | |
2214 | struct thread *migrated) | |
2215 | { | |
2216 | struct thread *thread; | |
2217 | char tstr[64]; | |
2218 | u32 max_cpus = sched->max_cpu + 1; | |
2219 | u32 ocpu, dcpu; | |
2220 | ||
2221 | if (sched->summary_only) | |
2222 | return; | |
2223 | ||
2224 | max_cpus = sched->max_cpu + 1; | |
2225 | ocpu = perf_evsel__intval(evsel, sample, "orig_cpu"); | |
2226 | dcpu = perf_evsel__intval(evsel, sample, "dest_cpu"); | |
2227 | ||
2228 | thread = machine__findnew_thread(machine, sample->pid, sample->tid); | |
2229 | if (thread == NULL) | |
2230 | return; | |
2231 | ||
2232 | if (timehist_skip_sample(sched, thread) && | |
2233 | timehist_skip_sample(sched, migrated)) { | |
2234 | return; | |
2235 | } | |
2236 | ||
2237 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); | |
2238 | printf("%15s [%04d] ", tstr, sample->cpu); | |
2239 | ||
2240 | if (sched->show_cpu_visual) { | |
2241 | u32 i; | |
2242 | char c; | |
2243 | ||
2244 | printf(" "); | |
2245 | for (i = 0; i < max_cpus; ++i) { | |
2246 | c = (i == sample->cpu) ? 'm' : ' '; | |
2247 | printf("%c", c); | |
2248 | } | |
2249 | printf(" "); | |
2250 | } | |
2251 | ||
2252 | printf(" %-*s ", comm_width, timehist_get_commstr(thread)); | |
2253 | ||
2254 | /* dt spacer */ | |
2255 | printf(" %9s %9s %9s ", "", "", ""); | |
2256 | ||
2257 | printf("migrated: %s", timehist_get_commstr(migrated)); | |
2258 | printf(" cpu %d => %d", ocpu, dcpu); | |
2259 | ||
2260 | printf("\n"); | |
2261 | } | |
2262 | ||
2263 | static int timehist_migrate_task_event(struct perf_tool *tool, | |
2264 | union perf_event *event __maybe_unused, | |
2265 | struct perf_evsel *evsel, | |
2266 | struct perf_sample *sample, | |
2267 | struct machine *machine) | |
2268 | { | |
2269 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); | |
2270 | struct thread *thread; | |
2271 | struct thread_runtime *tr = NULL; | |
2272 | /* want pid of migrated task not pid in sample */ | |
2273 | const u32 pid = perf_evsel__intval(evsel, sample, "pid"); | |
2274 | ||
2275 | thread = machine__findnew_thread(machine, 0, pid); | |
2276 | if (thread == NULL) | |
2277 | return -1; | |
2278 | ||
2279 | tr = thread__get_runtime(thread); | |
2280 | if (tr == NULL) | |
2281 | return -1; | |
2282 | ||
2283 | tr->migrations++; | |
2284 | ||
2285 | /* show migrations if requested */ | |
2286 | timehist_print_migration_event(sched, evsel, sample, machine, thread); | |
2287 | ||
2288 | return 0; | |
2289 | } | |
2290 | ||
52df138c | 2291 | static int timehist_sched_change_event(struct perf_tool *tool, |
49394a2a DA |
2292 | union perf_event *event, |
2293 | struct perf_evsel *evsel, | |
2294 | struct perf_sample *sample, | |
2295 | struct machine *machine) | |
2296 | { | |
fc1469f1 | 2297 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); |
853b7407 | 2298 | struct perf_time_interval *ptime = &sched->ptime; |
49394a2a DA |
2299 | struct addr_location al; |
2300 | struct thread *thread; | |
2301 | struct thread_runtime *tr = NULL; | |
853b7407 | 2302 | u64 tprev, t = sample->time; |
49394a2a DA |
2303 | int rc = 0; |
2304 | ||
2305 | if (machine__resolve(machine, &al, sample) < 0) { | |
2306 | pr_err("problem processing %d event. skipping it\n", | |
2307 | event->header.type); | |
2308 | rc = -1; | |
2309 | goto out; | |
2310 | } | |
2311 | ||
6c973c90 | 2312 | thread = timehist_get_thread(sched, sample, machine, evsel); |
49394a2a DA |
2313 | if (thread == NULL) { |
2314 | rc = -1; | |
2315 | goto out; | |
2316 | } | |
2317 | ||
52df138c | 2318 | if (timehist_skip_sample(sched, thread)) |
49394a2a DA |
2319 | goto out; |
2320 | ||
2321 | tr = thread__get_runtime(thread); | |
2322 | if (tr == NULL) { | |
2323 | rc = -1; | |
2324 | goto out; | |
2325 | } | |
2326 | ||
2327 | tprev = perf_evsel__get_time(evsel, sample->cpu); | |
2328 | ||
853b7407 DA |
2329 | /* |
2330 | * If start time given: | |
2331 | * - sample time is under window user cares about - skip sample | |
2332 | * - tprev is under window user cares about - reset to start of window | |
2333 | */ | |
2334 | if (ptime->start && ptime->start > t) | |
2335 | goto out; | |
2336 | ||
2337 | if (ptime->start > tprev) | |
2338 | tprev = ptime->start; | |
2339 | ||
2340 | /* | |
2341 | * If end time given: | |
2342 | * - previous sched event is out of window - we are done | |
2343 | * - sample time is beyond window user cares about - reset it | |
2344 | * to close out stats for time window interest | |
2345 | */ | |
2346 | if (ptime->end) { | |
2347 | if (tprev > ptime->end) | |
2348 | goto out; | |
2349 | ||
2350 | if (t > ptime->end) | |
2351 | t = ptime->end; | |
2352 | } | |
2353 | ||
2354 | timehist_update_runtime_stats(tr, t, tprev); | |
2355 | ||
52df138c | 2356 | if (!sched->summary_only) |
853b7407 | 2357 | timehist_print_sample(sched, sample, &al, thread, t); |
49394a2a DA |
2358 | |
2359 | out: | |
2360 | if (tr) { | |
2361 | /* time of this sched_switch event becomes last time task seen */ | |
2362 | tr->last_time = sample->time; | |
2363 | ||
2364 | /* sched out event for task so reset ready to run time */ | |
2365 | tr->ready_to_run = 0; | |
2366 | } | |
2367 | ||
2368 | perf_evsel__save_time(evsel, sample->time, sample->cpu); | |
2369 | ||
2370 | return rc; | |
2371 | } | |
2372 | ||
2373 | static int timehist_sched_switch_event(struct perf_tool *tool, | |
2374 | union perf_event *event, | |
2375 | struct perf_evsel *evsel, | |
2376 | struct perf_sample *sample, | |
2377 | struct machine *machine __maybe_unused) | |
2378 | { | |
2379 | return timehist_sched_change_event(tool, event, evsel, sample, machine); | |
2380 | } | |
2381 | ||
2382 | static int process_lost(struct perf_tool *tool __maybe_unused, | |
2383 | union perf_event *event, | |
2384 | struct perf_sample *sample, | |
2385 | struct machine *machine __maybe_unused) | |
2386 | { | |
2387 | char tstr[64]; | |
2388 | ||
2389 | timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr)); | |
2390 | printf("%15s ", tstr); | |
2391 | printf("lost %" PRIu64 " events on cpu %d\n", event->lost.lost, sample->cpu); | |
2392 | ||
2393 | return 0; | |
2394 | } | |
2395 | ||
2396 | ||
52df138c DA |
2397 | static void print_thread_runtime(struct thread *t, |
2398 | struct thread_runtime *r) | |
2399 | { | |
2400 | double mean = avg_stats(&r->run_stats); | |
2401 | float stddev; | |
2402 | ||
2403 | printf("%*s %5d %9" PRIu64 " ", | |
2404 | comm_width, timehist_get_commstr(t), t->ppid, | |
2405 | (u64) r->run_stats.n); | |
2406 | ||
2407 | print_sched_time(r->total_run_time, 8); | |
2408 | stddev = rel_stddev_stats(stddev_stats(&r->run_stats), mean); | |
2409 | print_sched_time(r->run_stats.min, 6); | |
2410 | printf(" "); | |
2411 | print_sched_time((u64) mean, 6); | |
2412 | printf(" "); | |
2413 | print_sched_time(r->run_stats.max, 6); | |
2414 | printf(" "); | |
2415 | printf("%5.2f", stddev); | |
350f54fa | 2416 | printf(" %5" PRIu64, r->migrations); |
52df138c DA |
2417 | printf("\n"); |
2418 | } | |
2419 | ||
2420 | struct total_run_stats { | |
2421 | u64 sched_count; | |
2422 | u64 task_count; | |
2423 | u64 total_run_time; | |
2424 | }; | |
2425 | ||
2426 | static int __show_thread_runtime(struct thread *t, void *priv) | |
2427 | { | |
2428 | struct total_run_stats *stats = priv; | |
2429 | struct thread_runtime *r; | |
2430 | ||
2431 | if (thread__is_filtered(t)) | |
2432 | return 0; | |
2433 | ||
2434 | r = thread__priv(t); | |
2435 | if (r && r->run_stats.n) { | |
2436 | stats->task_count++; | |
2437 | stats->sched_count += r->run_stats.n; | |
2438 | stats->total_run_time += r->total_run_time; | |
2439 | print_thread_runtime(t, r); | |
2440 | } | |
2441 | ||
2442 | return 0; | |
2443 | } | |
2444 | ||
2445 | static int show_thread_runtime(struct thread *t, void *priv) | |
2446 | { | |
2447 | if (t->dead) | |
2448 | return 0; | |
2449 | ||
2450 | return __show_thread_runtime(t, priv); | |
2451 | } | |
2452 | ||
2453 | static int show_deadthread_runtime(struct thread *t, void *priv) | |
2454 | { | |
2455 | if (!t->dead) | |
2456 | return 0; | |
2457 | ||
2458 | return __show_thread_runtime(t, priv); | |
2459 | } | |
2460 | ||
2461 | static void timehist_print_summary(struct perf_sched *sched, | |
2462 | struct perf_session *session) | |
2463 | { | |
2464 | struct machine *m = &session->machines.host; | |
2465 | struct total_run_stats totals; | |
2466 | u64 task_count; | |
2467 | struct thread *t; | |
2468 | struct thread_runtime *r; | |
2469 | int i; | |
2470 | ||
2471 | memset(&totals, 0, sizeof(totals)); | |
2472 | ||
2473 | if (comm_width < 30) | |
2474 | comm_width = 30; | |
2475 | ||
2476 | printf("\nRuntime summary\n"); | |
2477 | printf("%*s parent sched-in ", comm_width, "comm"); | |
350f54fa | 2478 | printf(" run-time min-run avg-run max-run stddev migrations\n"); |
52df138c DA |
2479 | printf("%*s (count) ", comm_width, ""); |
2480 | printf(" (msec) (msec) (msec) (msec) %%\n"); | |
350f54fa | 2481 | printf("%.117s\n", graph_dotted_line); |
52df138c DA |
2482 | |
2483 | machine__for_each_thread(m, show_thread_runtime, &totals); | |
2484 | task_count = totals.task_count; | |
2485 | if (!task_count) | |
2486 | printf("<no still running tasks>\n"); | |
2487 | ||
2488 | printf("\nTerminated tasks:\n"); | |
2489 | machine__for_each_thread(m, show_deadthread_runtime, &totals); | |
2490 | if (task_count == totals.task_count) | |
2491 | printf("<no terminated tasks>\n"); | |
2492 | ||
2493 | /* CPU idle stats not tracked when samples were skipped */ | |
2494 | if (sched->skipped_samples) | |
2495 | return; | |
2496 | ||
2497 | printf("\nIdle stats:\n"); | |
b336352b | 2498 | for (i = 0; i < idle_max_cpu; ++i) { |
52df138c DA |
2499 | t = idle_threads[i]; |
2500 | if (!t) | |
2501 | continue; | |
2502 | ||
2503 | r = thread__priv(t); | |
2504 | if (r && r->run_stats.n) { | |
2505 | totals.sched_count += r->run_stats.n; | |
2506 | printf(" CPU %2d idle for ", i); | |
2507 | print_sched_time(r->total_run_time, 6); | |
2508 | printf(" msec\n"); | |
2509 | } else | |
2510 | printf(" CPU %2d idle entire time window\n", i); | |
2511 | } | |
2512 | ||
2513 | printf("\n" | |
2514 | " Total number of unique tasks: %" PRIu64 "\n" | |
2515 | "Total number of context switches: %" PRIu64 "\n" | |
2516 | " Total run time (msec): ", | |
2517 | totals.task_count, totals.sched_count); | |
2518 | ||
2519 | print_sched_time(totals.total_run_time, 2); | |
2520 | printf("\n"); | |
2521 | } | |
2522 | ||
49394a2a DA |
2523 | typedef int (*sched_handler)(struct perf_tool *tool, |
2524 | union perf_event *event, | |
2525 | struct perf_evsel *evsel, | |
2526 | struct perf_sample *sample, | |
2527 | struct machine *machine); | |
2528 | ||
2529 | static int perf_timehist__process_sample(struct perf_tool *tool, | |
2530 | union perf_event *event, | |
2531 | struct perf_sample *sample, | |
2532 | struct perf_evsel *evsel, | |
2533 | struct machine *machine) | |
2534 | { | |
2535 | struct perf_sched *sched = container_of(tool, struct perf_sched, tool); | |
2536 | int err = 0; | |
2537 | int this_cpu = sample->cpu; | |
2538 | ||
2539 | if (this_cpu > sched->max_cpu) | |
2540 | sched->max_cpu = this_cpu; | |
2541 | ||
2542 | if (evsel->handler != NULL) { | |
2543 | sched_handler f = evsel->handler; | |
2544 | ||
2545 | err = f(tool, event, evsel, sample, machine); | |
2546 | } | |
2547 | ||
2548 | return err; | |
2549 | } | |
2550 | ||
6c973c90 DA |
2551 | static int timehist_check_attr(struct perf_sched *sched, |
2552 | struct perf_evlist *evlist) | |
2553 | { | |
2554 | struct perf_evsel *evsel; | |
2555 | struct evsel_runtime *er; | |
2556 | ||
2557 | list_for_each_entry(evsel, &evlist->entries, node) { | |
2558 | er = perf_evsel__get_runtime(evsel); | |
2559 | if (er == NULL) { | |
2560 | pr_err("Failed to allocate memory for evsel runtime data\n"); | |
2561 | return -1; | |
2562 | } | |
2563 | ||
2564 | if (sched->show_callchain && | |
2565 | !(evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) { | |
2566 | pr_info("Samples do not have callchains.\n"); | |
2567 | sched->show_callchain = 0; | |
2568 | symbol_conf.use_callchain = 0; | |
2569 | } | |
2570 | } | |
2571 | ||
2572 | return 0; | |
2573 | } | |
2574 | ||
49394a2a DA |
2575 | static int perf_sched__timehist(struct perf_sched *sched) |
2576 | { | |
2577 | const struct perf_evsel_str_handler handlers[] = { | |
2578 | { "sched:sched_switch", timehist_sched_switch_event, }, | |
2579 | { "sched:sched_wakeup", timehist_sched_wakeup_event, }, | |
2580 | { "sched:sched_wakeup_new", timehist_sched_wakeup_event, }, | |
2581 | }; | |
350f54fa DA |
2582 | const struct perf_evsel_str_handler migrate_handlers[] = { |
2583 | { "sched:sched_migrate_task", timehist_migrate_task_event, }, | |
2584 | }; | |
49394a2a DA |
2585 | struct perf_data_file file = { |
2586 | .path = input_name, | |
2587 | .mode = PERF_DATA_MODE_READ, | |
6fa94258 | 2588 | .force = sched->force, |
49394a2a DA |
2589 | }; |
2590 | ||
2591 | struct perf_session *session; | |
52df138c | 2592 | struct perf_evlist *evlist; |
49394a2a DA |
2593 | int err = -1; |
2594 | ||
2595 | /* | |
2596 | * event handlers for timehist option | |
2597 | */ | |
2598 | sched->tool.sample = perf_timehist__process_sample; | |
2599 | sched->tool.mmap = perf_event__process_mmap; | |
2600 | sched->tool.comm = perf_event__process_comm; | |
2601 | sched->tool.exit = perf_event__process_exit; | |
2602 | sched->tool.fork = perf_event__process_fork; | |
2603 | sched->tool.lost = process_lost; | |
2604 | sched->tool.attr = perf_event__process_attr; | |
2605 | sched->tool.tracing_data = perf_event__process_tracing_data; | |
2606 | sched->tool.build_id = perf_event__process_build_id; | |
2607 | ||
2608 | sched->tool.ordered_events = true; | |
2609 | sched->tool.ordering_requires_timestamps = true; | |
2610 | ||
6c973c90 DA |
2611 | symbol_conf.use_callchain = sched->show_callchain; |
2612 | ||
49394a2a DA |
2613 | session = perf_session__new(&file, false, &sched->tool); |
2614 | if (session == NULL) | |
2615 | return -ENOMEM; | |
2616 | ||
52df138c DA |
2617 | evlist = session->evlist; |
2618 | ||
49394a2a DA |
2619 | symbol__init(&session->header.env); |
2620 | ||
853b7407 DA |
2621 | if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) { |
2622 | pr_err("Invalid time string\n"); | |
2623 | return -EINVAL; | |
2624 | } | |
2625 | ||
6c973c90 DA |
2626 | if (timehist_check_attr(sched, evlist) != 0) |
2627 | goto out; | |
2628 | ||
49394a2a DA |
2629 | setup_pager(); |
2630 | ||
2631 | /* setup per-evsel handlers */ | |
2632 | if (perf_session__set_tracepoints_handlers(session, handlers)) | |
2633 | goto out; | |
2634 | ||
f45bf8d3 DA |
2635 | /* sched_switch event at a minimum needs to exist */ |
2636 | if (!perf_evlist__find_tracepoint_by_name(session->evlist, | |
2637 | "sched:sched_switch")) { | |
2638 | pr_err("No sched_switch events found. Have you run 'perf sched record'?\n"); | |
49394a2a | 2639 | goto out; |
f45bf8d3 | 2640 | } |
49394a2a | 2641 | |
350f54fa DA |
2642 | if (sched->show_migrations && |
2643 | perf_session__set_tracepoints_handlers(session, migrate_handlers)) | |
2644 | goto out; | |
2645 | ||
49394a2a DA |
2646 | /* pre-allocate struct for per-CPU idle stats */ |
2647 | sched->max_cpu = session->header.env.nr_cpus_online; | |
2648 | if (sched->max_cpu == 0) | |
2649 | sched->max_cpu = 4; | |
2650 | if (init_idle_threads(sched->max_cpu)) | |
2651 | goto out; | |
2652 | ||
52df138c DA |
2653 | /* summary_only implies summary option, but don't overwrite summary if set */ |
2654 | if (sched->summary_only) | |
2655 | sched->summary = sched->summary_only; | |
2656 | ||
2657 | if (!sched->summary_only) | |
a407b067 | 2658 | timehist_header(sched); |
49394a2a DA |
2659 | |
2660 | err = perf_session__process_events(session); | |
2661 | if (err) { | |
2662 | pr_err("Failed to process events, error %d", err); | |
2663 | goto out; | |
2664 | } | |
2665 | ||
52df138c DA |
2666 | sched->nr_events = evlist->stats.nr_events[0]; |
2667 | sched->nr_lost_events = evlist->stats.total_lost; | |
2668 | sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST]; | |
2669 | ||
2670 | if (sched->summary) | |
2671 | timehist_print_summary(sched, session); | |
2672 | ||
49394a2a DA |
2673 | out: |
2674 | free_idle_threads(); | |
2675 | perf_session__delete(session); | |
2676 | ||
2677 | return err; | |
2678 | } | |
2679 | ||
2680 | ||
0e9b07e5 | 2681 | static void print_bad_events(struct perf_sched *sched) |
0ec04e16 | 2682 | { |
0e9b07e5 | 2683 | if (sched->nr_unordered_timestamps && sched->nr_timestamps) { |
0ec04e16 | 2684 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", |
0e9b07e5 ACM |
2685 | (double)sched->nr_unordered_timestamps/(double)sched->nr_timestamps*100.0, |
2686 | sched->nr_unordered_timestamps, sched->nr_timestamps); | |
0ec04e16 | 2687 | } |
0e9b07e5 | 2688 | if (sched->nr_lost_events && sched->nr_events) { |
0ec04e16 | 2689 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", |
0e9b07e5 ACM |
2690 | (double)sched->nr_lost_events/(double)sched->nr_events * 100.0, |
2691 | sched->nr_lost_events, sched->nr_events, sched->nr_lost_chunks); | |
0ec04e16 | 2692 | } |
0e9b07e5 | 2693 | if (sched->nr_context_switch_bugs && sched->nr_timestamps) { |
0ec04e16 | 2694 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", |
0e9b07e5 ACM |
2695 | (double)sched->nr_context_switch_bugs/(double)sched->nr_timestamps*100.0, |
2696 | sched->nr_context_switch_bugs, sched->nr_timestamps); | |
2697 | if (sched->nr_lost_events) | |
0ec04e16 IM |
2698 | printf(" (due to lost events?)"); |
2699 | printf("\n"); | |
2700 | } | |
2701 | } | |
2702 | ||
2f80dd44 JB |
2703 | static void __merge_work_atoms(struct rb_root *root, struct work_atoms *data) |
2704 | { | |
2705 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
2706 | struct work_atoms *this; | |
2707 | const char *comm = thread__comm_str(data->thread), *this_comm; | |
2708 | ||
2709 | while (*new) { | |
2710 | int cmp; | |
2711 | ||
2712 | this = container_of(*new, struct work_atoms, node); | |
2713 | parent = *new; | |
2714 | ||
2715 | this_comm = thread__comm_str(this->thread); | |
2716 | cmp = strcmp(comm, this_comm); | |
2717 | if (cmp > 0) { | |
2718 | new = &((*new)->rb_left); | |
2719 | } else if (cmp < 0) { | |
2720 | new = &((*new)->rb_right); | |
2721 | } else { | |
2722 | this->num_merged++; | |
2723 | this->total_runtime += data->total_runtime; | |
2724 | this->nb_atoms += data->nb_atoms; | |
2725 | this->total_lat += data->total_lat; | |
2726 | list_splice(&data->work_list, &this->work_list); | |
2727 | if (this->max_lat < data->max_lat) { | |
2728 | this->max_lat = data->max_lat; | |
2729 | this->max_lat_at = data->max_lat_at; | |
2730 | } | |
2731 | zfree(&data); | |
2732 | return; | |
2733 | } | |
2734 | } | |
2735 | ||
2736 | data->num_merged++; | |
2737 | rb_link_node(&data->node, parent, new); | |
2738 | rb_insert_color(&data->node, root); | |
2739 | } | |
2740 | ||
2741 | static void perf_sched__merge_lat(struct perf_sched *sched) | |
2742 | { | |
2743 | struct work_atoms *data; | |
2744 | struct rb_node *node; | |
2745 | ||
2746 | if (sched->skip_merge) | |
2747 | return; | |
2748 | ||
2749 | while ((node = rb_first(&sched->atom_root))) { | |
2750 | rb_erase(node, &sched->atom_root); | |
2751 | data = rb_entry(node, struct work_atoms, node); | |
2752 | __merge_work_atoms(&sched->merged_atom_root, data); | |
2753 | } | |
2754 | } | |
2755 | ||
0e9b07e5 | 2756 | static int perf_sched__lat(struct perf_sched *sched) |
0ec04e16 IM |
2757 | { |
2758 | struct rb_node *next; | |
2759 | ||
2760 | setup_pager(); | |
ad9def7c | 2761 | |
ae536acf | 2762 | if (perf_sched__read_events(sched)) |
a116e05d | 2763 | return -1; |
ad9def7c | 2764 | |
2f80dd44 | 2765 | perf_sched__merge_lat(sched); |
0e9b07e5 | 2766 | perf_sched__sort_lat(sched); |
0ec04e16 | 2767 | |
80790e0b RR |
2768 | printf("\n -----------------------------------------------------------------------------------------------------------------\n"); |
2769 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms | Maximum delay at |\n"); | |
2770 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); | |
0ec04e16 | 2771 | |
0e9b07e5 | 2772 | next = rb_first(&sched->sorted_atom_root); |
0ec04e16 IM |
2773 | |
2774 | while (next) { | |
2775 | struct work_atoms *work_list; | |
2776 | ||
2777 | work_list = rb_entry(next, struct work_atoms, node); | |
0e9b07e5 | 2778 | output_lat_thread(sched, work_list); |
0ec04e16 | 2779 | next = rb_next(next); |
ae536acf | 2780 | thread__zput(work_list->thread); |
0ec04e16 IM |
2781 | } |
2782 | ||
80790e0b | 2783 | printf(" -----------------------------------------------------------------------------------------------------------------\n"); |
9486aa38 | 2784 | printf(" TOTAL: |%11.3f ms |%9" PRIu64 " |\n", |
4fc76e49 | 2785 | (double)sched->all_runtime / NSEC_PER_MSEC, sched->all_count); |
0ec04e16 IM |
2786 | |
2787 | printf(" ---------------------------------------------------\n"); | |
2788 | ||
0e9b07e5 | 2789 | print_bad_events(sched); |
0ec04e16 IM |
2790 | printf("\n"); |
2791 | ||
a116e05d | 2792 | return 0; |
0ec04e16 IM |
2793 | } |
2794 | ||
99623c62 JO |
2795 | static int setup_map_cpus(struct perf_sched *sched) |
2796 | { | |
73643bb6 JO |
2797 | struct cpu_map *map; |
2798 | ||
99623c62 JO |
2799 | sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); |
2800 | ||
2801 | if (sched->map.comp) { | |
2802 | sched->map.comp_cpus = zalloc(sched->max_cpu * sizeof(int)); | |
cf294f24 JO |
2803 | if (!sched->map.comp_cpus) |
2804 | return -1; | |
99623c62 JO |
2805 | } |
2806 | ||
73643bb6 JO |
2807 | if (!sched->map.cpus_str) |
2808 | return 0; | |
2809 | ||
2810 | map = cpu_map__new(sched->map.cpus_str); | |
2811 | if (!map) { | |
2812 | pr_err("failed to get cpus map from %s\n", sched->map.cpus_str); | |
2813 | return -1; | |
2814 | } | |
2815 | ||
2816 | sched->map.cpus = map; | |
99623c62 JO |
2817 | return 0; |
2818 | } | |
2819 | ||
a151a37a JO |
2820 | static int setup_color_pids(struct perf_sched *sched) |
2821 | { | |
2822 | struct thread_map *map; | |
2823 | ||
2824 | if (!sched->map.color_pids_str) | |
2825 | return 0; | |
2826 | ||
2827 | map = thread_map__new_by_tid_str(sched->map.color_pids_str); | |
2828 | if (!map) { | |
2829 | pr_err("failed to get thread map from %s\n", sched->map.color_pids_str); | |
2830 | return -1; | |
2831 | } | |
2832 | ||
2833 | sched->map.color_pids = map; | |
2834 | return 0; | |
2835 | } | |
2836 | ||
cf294f24 JO |
2837 | static int setup_color_cpus(struct perf_sched *sched) |
2838 | { | |
2839 | struct cpu_map *map; | |
2840 | ||
2841 | if (!sched->map.color_cpus_str) | |
2842 | return 0; | |
2843 | ||
2844 | map = cpu_map__new(sched->map.color_cpus_str); | |
2845 | if (!map) { | |
2846 | pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str); | |
2847 | return -1; | |
2848 | } | |
2849 | ||
2850 | sched->map.color_cpus = map; | |
2851 | return 0; | |
2852 | } | |
2853 | ||
0e9b07e5 | 2854 | static int perf_sched__map(struct perf_sched *sched) |
0ec04e16 | 2855 | { |
99623c62 JO |
2856 | if (setup_map_cpus(sched)) |
2857 | return -1; | |
40749d0f | 2858 | |
a151a37a JO |
2859 | if (setup_color_pids(sched)) |
2860 | return -1; | |
2861 | ||
cf294f24 JO |
2862 | if (setup_color_cpus(sched)) |
2863 | return -1; | |
2864 | ||
0ec04e16 | 2865 | setup_pager(); |
ae536acf | 2866 | if (perf_sched__read_events(sched)) |
a116e05d | 2867 | return -1; |
0e9b07e5 | 2868 | print_bad_events(sched); |
a116e05d | 2869 | return 0; |
0ec04e16 IM |
2870 | } |
2871 | ||
0e9b07e5 | 2872 | static int perf_sched__replay(struct perf_sched *sched) |
0ec04e16 IM |
2873 | { |
2874 | unsigned long i; | |
2875 | ||
0e9b07e5 ACM |
2876 | calibrate_run_measurement_overhead(sched); |
2877 | calibrate_sleep_measurement_overhead(sched); | |
0ec04e16 | 2878 | |
0e9b07e5 | 2879 | test_calibrations(sched); |
0ec04e16 | 2880 | |
ae536acf | 2881 | if (perf_sched__read_events(sched)) |
a116e05d | 2882 | return -1; |
0ec04e16 | 2883 | |
0e9b07e5 ACM |
2884 | printf("nr_run_events: %ld\n", sched->nr_run_events); |
2885 | printf("nr_sleep_events: %ld\n", sched->nr_sleep_events); | |
2886 | printf("nr_wakeup_events: %ld\n", sched->nr_wakeup_events); | |
0ec04e16 | 2887 | |
0e9b07e5 ACM |
2888 | if (sched->targetless_wakeups) |
2889 | printf("target-less wakeups: %ld\n", sched->targetless_wakeups); | |
2890 | if (sched->multitarget_wakeups) | |
2891 | printf("multi-target wakeups: %ld\n", sched->multitarget_wakeups); | |
2892 | if (sched->nr_run_events_optimized) | |
0ec04e16 | 2893 | printf("run atoms optimized: %ld\n", |
0e9b07e5 | 2894 | sched->nr_run_events_optimized); |
0ec04e16 | 2895 | |
0e9b07e5 ACM |
2896 | print_task_traces(sched); |
2897 | add_cross_task_wakeups(sched); | |
0ec04e16 | 2898 | |
0e9b07e5 | 2899 | create_tasks(sched); |
0ec04e16 | 2900 | printf("------------------------------------------------------------\n"); |
0e9b07e5 ACM |
2901 | for (i = 0; i < sched->replay_repeat; i++) |
2902 | run_one_test(sched); | |
a116e05d ACM |
2903 | |
2904 | return 0; | |
0ec04e16 IM |
2905 | } |
2906 | ||
0e9b07e5 ACM |
2907 | static void setup_sorting(struct perf_sched *sched, const struct option *options, |
2908 | const char * const usage_msg[]) | |
daa1d7a5 | 2909 | { |
0e9b07e5 | 2910 | char *tmp, *tok, *str = strdup(sched->sort_order); |
daa1d7a5 FW |
2911 | |
2912 | for (tok = strtok_r(str, ", ", &tmp); | |
2913 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
0e9b07e5 | 2914 | if (sort_dimension__add(tok, &sched->sort_list) < 0) { |
c7118369 NK |
2915 | usage_with_options_msg(usage_msg, options, |
2916 | "Unknown --sort key: `%s'", tok); | |
daa1d7a5 FW |
2917 | } |
2918 | } | |
2919 | ||
2920 | free(str); | |
2921 | ||
0e9b07e5 | 2922 | sort_dimension__add("pid", &sched->cmp_pid); |
daa1d7a5 FW |
2923 | } |
2924 | ||
1fc35b29 IM |
2925 | static int __cmd_record(int argc, const char **argv) |
2926 | { | |
2927 | unsigned int rec_argc, i, j; | |
2928 | const char **rec_argv; | |
0e9b07e5 ACM |
2929 | const char * const record_args[] = { |
2930 | "record", | |
2931 | "-a", | |
2932 | "-R", | |
0e9b07e5 ACM |
2933 | "-m", "1024", |
2934 | "-c", "1", | |
2935 | "-e", "sched:sched_switch", | |
2936 | "-e", "sched:sched_stat_wait", | |
2937 | "-e", "sched:sched_stat_sleep", | |
2938 | "-e", "sched:sched_stat_iowait", | |
2939 | "-e", "sched:sched_stat_runtime", | |
0e9b07e5 ACM |
2940 | "-e", "sched:sched_process_fork", |
2941 | "-e", "sched:sched_wakeup", | |
7fff9597 | 2942 | "-e", "sched:sched_wakeup_new", |
0e9b07e5 ACM |
2943 | "-e", "sched:sched_migrate_task", |
2944 | }; | |
1fc35b29 IM |
2945 | |
2946 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
2947 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
2948 | ||
e462dc55 | 2949 | if (rec_argv == NULL) |
ce47dc56 CS |
2950 | return -ENOMEM; |
2951 | ||
1fc35b29 IM |
2952 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
2953 | rec_argv[i] = strdup(record_args[i]); | |
2954 | ||
2955 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
2956 | rec_argv[i] = argv[j]; | |
2957 | ||
2958 | BUG_ON(i != rec_argc); | |
2959 | ||
2960 | return cmd_record(i, rec_argv, NULL); | |
2961 | } | |
2962 | ||
1d037ca1 | 2963 | int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused) |
0a02ad93 | 2964 | { |
8a39df8f AH |
2965 | const char default_sort_order[] = "avg, max, switch, runtime"; |
2966 | struct perf_sched sched = { | |
2967 | .tool = { | |
2968 | .sample = perf_sched__process_tracepoint_sample, | |
2969 | .comm = perf_event__process_comm, | |
2970 | .lost = perf_event__process_lost, | |
2971 | .fork = perf_sched__process_fork_event, | |
0a8cb85c | 2972 | .ordered_events = true, |
8a39df8f AH |
2973 | }, |
2974 | .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid), | |
2975 | .sort_list = LIST_HEAD_INIT(sched.sort_list), | |
2976 | .start_work_mutex = PTHREAD_MUTEX_INITIALIZER, | |
2977 | .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER, | |
8a39df8f AH |
2978 | .sort_order = default_sort_order, |
2979 | .replay_repeat = 10, | |
2980 | .profile_cpu = -1, | |
2981 | .next_shortname1 = 'A', | |
2982 | .next_shortname2 = '0', | |
2f80dd44 | 2983 | .skip_merge = 0, |
6c973c90 DA |
2984 | .show_callchain = 1, |
2985 | .max_stack = 5, | |
8a39df8f | 2986 | }; |
77f02f44 NK |
2987 | const struct option sched_options[] = { |
2988 | OPT_STRING('i', "input", &input_name, "file", | |
2989 | "input file name"), | |
2990 | OPT_INCR('v', "verbose", &verbose, | |
2991 | "be more verbose (show symbol address, etc)"), | |
2992 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
2993 | "dump raw trace in ASCII"), | |
6fa94258 | 2994 | OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"), |
77f02f44 NK |
2995 | OPT_END() |
2996 | }; | |
0e9b07e5 ACM |
2997 | const struct option latency_options[] = { |
2998 | OPT_STRING('s', "sort", &sched.sort_order, "key[,key2...]", | |
2999 | "sort by key(s): runtime, switch, avg, max"), | |
0e9b07e5 ACM |
3000 | OPT_INTEGER('C', "CPU", &sched.profile_cpu, |
3001 | "CPU to profile on"), | |
2f80dd44 JB |
3002 | OPT_BOOLEAN('p', "pids", &sched.skip_merge, |
3003 | "latency stats per pid instead of per comm"), | |
77f02f44 | 3004 | OPT_PARENT(sched_options) |
0e9b07e5 ACM |
3005 | }; |
3006 | const struct option replay_options[] = { | |
3007 | OPT_UINTEGER('r', "repeat", &sched.replay_repeat, | |
3008 | "repeat the workload replay N times (-1: infinite)"), | |
77f02f44 | 3009 | OPT_PARENT(sched_options) |
0e9b07e5 | 3010 | }; |
99623c62 JO |
3011 | const struct option map_options[] = { |
3012 | OPT_BOOLEAN(0, "compact", &sched.map.comp, | |
3013 | "map output in compact mode"), | |
a151a37a JO |
3014 | OPT_STRING(0, "color-pids", &sched.map.color_pids_str, "pids", |
3015 | "highlight given pids in map"), | |
cf294f24 JO |
3016 | OPT_STRING(0, "color-cpus", &sched.map.color_cpus_str, "cpus", |
3017 | "highlight given CPUs in map"), | |
73643bb6 JO |
3018 | OPT_STRING(0, "cpus", &sched.map.cpus_str, "cpus", |
3019 | "display given CPUs in map"), | |
77f02f44 | 3020 | OPT_PARENT(sched_options) |
99623c62 | 3021 | }; |
49394a2a DA |
3022 | const struct option timehist_options[] = { |
3023 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, | |
3024 | "file", "vmlinux pathname"), | |
3025 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | |
3026 | "file", "kallsyms pathname"), | |
6c973c90 DA |
3027 | OPT_BOOLEAN('g', "call-graph", &sched.show_callchain, |
3028 | "Display call chains if present (default on)"), | |
3029 | OPT_UINTEGER(0, "max-stack", &sched.max_stack, | |
3030 | "Maximum number of functions to display backtrace."), | |
49394a2a DA |
3031 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
3032 | "Look for files with symbols relative to this directory"), | |
52df138c DA |
3033 | OPT_BOOLEAN('s', "summary", &sched.summary_only, |
3034 | "Show only syscall summary with statistics"), | |
3035 | OPT_BOOLEAN('S', "with-summary", &sched.summary, | |
3036 | "Show all syscalls and summary with statistics"), | |
fc1469f1 | 3037 | OPT_BOOLEAN('w', "wakeups", &sched.show_wakeups, "Show wakeup events"), |
350f54fa | 3038 | OPT_BOOLEAN('M', "migrations", &sched.show_migrations, "Show migration events"), |
a407b067 | 3039 | OPT_BOOLEAN('V', "cpu-visual", &sched.show_cpu_visual, "Add CPU visual"), |
853b7407 DA |
3040 | OPT_STRING(0, "time", &sched.time_str, "str", |
3041 | "Time span for analysis (start,stop)"), | |
49394a2a DA |
3042 | OPT_PARENT(sched_options) |
3043 | }; | |
3044 | ||
0e9b07e5 ACM |
3045 | const char * const latency_usage[] = { |
3046 | "perf sched latency [<options>]", | |
3047 | NULL | |
3048 | }; | |
3049 | const char * const replay_usage[] = { | |
3050 | "perf sched replay [<options>]", | |
3051 | NULL | |
3052 | }; | |
99623c62 JO |
3053 | const char * const map_usage[] = { |
3054 | "perf sched map [<options>]", | |
3055 | NULL | |
3056 | }; | |
49394a2a DA |
3057 | const char * const timehist_usage[] = { |
3058 | "perf sched timehist [<options>]", | |
3059 | NULL | |
3060 | }; | |
a83edb2d | 3061 | const char *const sched_subcommands[] = { "record", "latency", "map", |
49394a2a DA |
3062 | "replay", "script", |
3063 | "timehist", NULL }; | |
a83edb2d RR |
3064 | const char *sched_usage[] = { |
3065 | NULL, | |
0e9b07e5 ACM |
3066 | NULL |
3067 | }; | |
3068 | struct trace_sched_handler lat_ops = { | |
3069 | .wakeup_event = latency_wakeup_event, | |
3070 | .switch_event = latency_switch_event, | |
3071 | .runtime_event = latency_runtime_event, | |
0e9b07e5 ACM |
3072 | .migrate_task_event = latency_migrate_task_event, |
3073 | }; | |
3074 | struct trace_sched_handler map_ops = { | |
3075 | .switch_event = map_switch_event, | |
3076 | }; | |
3077 | struct trace_sched_handler replay_ops = { | |
3078 | .wakeup_event = replay_wakeup_event, | |
3079 | .switch_event = replay_switch_event, | |
3080 | .fork_event = replay_fork_event, | |
3081 | }; | |
156a2b02 AH |
3082 | unsigned int i; |
3083 | ||
3084 | for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++) | |
3085 | sched.curr_pid[i] = -1; | |
0e9b07e5 | 3086 | |
a83edb2d RR |
3087 | argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands, |
3088 | sched_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
f2858d8a IM |
3089 | if (!argc) |
3090 | usage_with_options(sched_usage, sched_options); | |
0a02ad93 | 3091 | |
c0777c5a | 3092 | /* |
133dc4c3 | 3093 | * Aliased to 'perf script' for now: |
c0777c5a | 3094 | */ |
133dc4c3 IM |
3095 | if (!strcmp(argv[0], "script")) |
3096 | return cmd_script(argc, argv, prefix); | |
c0777c5a | 3097 | |
1fc35b29 IM |
3098 | if (!strncmp(argv[0], "rec", 3)) { |
3099 | return __cmd_record(argc, argv); | |
3100 | } else if (!strncmp(argv[0], "lat", 3)) { | |
0e9b07e5 | 3101 | sched.tp_handler = &lat_ops; |
f2858d8a IM |
3102 | if (argc > 1) { |
3103 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); | |
3104 | if (argc) | |
3105 | usage_with_options(latency_usage, latency_options); | |
f2858d8a | 3106 | } |
0e9b07e5 ACM |
3107 | setup_sorting(&sched, latency_options, latency_usage); |
3108 | return perf_sched__lat(&sched); | |
0ec04e16 | 3109 | } else if (!strcmp(argv[0], "map")) { |
99623c62 | 3110 | if (argc) { |
a151a37a | 3111 | argc = parse_options(argc, argv, map_options, map_usage, 0); |
99623c62 JO |
3112 | if (argc) |
3113 | usage_with_options(map_usage, map_options); | |
3114 | } | |
0e9b07e5 ACM |
3115 | sched.tp_handler = &map_ops; |
3116 | setup_sorting(&sched, latency_options, latency_usage); | |
3117 | return perf_sched__map(&sched); | |
f2858d8a | 3118 | } else if (!strncmp(argv[0], "rep", 3)) { |
0e9b07e5 | 3119 | sched.tp_handler = &replay_ops; |
f2858d8a IM |
3120 | if (argc) { |
3121 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); | |
3122 | if (argc) | |
3123 | usage_with_options(replay_usage, replay_options); | |
3124 | } | |
0e9b07e5 | 3125 | return perf_sched__replay(&sched); |
49394a2a DA |
3126 | } else if (!strcmp(argv[0], "timehist")) { |
3127 | if (argc) { | |
3128 | argc = parse_options(argc, argv, timehist_options, | |
3129 | timehist_usage, 0); | |
3130 | if (argc) | |
3131 | usage_with_options(timehist_usage, timehist_options); | |
3132 | } | |
fc1469f1 DA |
3133 | if (sched.show_wakeups && sched.summary_only) { |
3134 | pr_err(" Error: -s and -w are mutually exclusive.\n"); | |
3135 | parse_options_usage(timehist_usage, timehist_options, "s", true); | |
3136 | parse_options_usage(NULL, timehist_options, "w", true); | |
3137 | return -EINVAL; | |
3138 | } | |
3139 | ||
49394a2a | 3140 | return perf_sched__timehist(&sched); |
f2858d8a IM |
3141 | } else { |
3142 | usage_with_options(sched_usage, sched_options); | |
3143 | } | |
3144 | ||
ec156764 | 3145 | return 0; |
0a02ad93 | 3146 | } |